Method SetBearerToken
SetBearerToken(string)
Sets or clears the Bearer token used for authentication with the MCP server.
public void SetBearerToken(string bearerToken)
Parameters
bearerTokenstringThe Bearer token string to use for authentication, or
null/empty to clear authentication. This token is included in theAuthorizationheader as"Bearer {token}"on all requests.
Examples
// Set initial token
client.SetBearerToken("eyJhbGciOiJIUzI1NiIs...");
// Clear authentication
client.SetBearerToken(null);
// Token refresh scenario
client.OnAuthFailed = statusCode =>
{
var newToken = RefreshAuthToken();
client.SetBearerToken(newToken);
};
Remarks
Changing the bearer token invalidates the current session, causing the client to automatically re-initialize on the next operation. This ensures that the new authentication credentials are properly negotiated with the server.
If you're implementing token refresh logic, call this method with the new token when the old one expires. The client handles the re-initialization transparently.