Skip to main content

Handling OAuth2 Authentication

When an LLM uses an OAuth2-secured service (like Google Workspace, Slack, GitHub or a remote MCP server), HasMCP manages the whole OAuth2 lifecycle so that neither the LLM nor the MCP client ever handles a token.

Connecting an Account

There are two ways to connect:
  • Ahead of time. Click Connect Account on a remote MCP provider’s page, or on a server’s Configuration tab under Remote MCP Credentials to connect for that server only.
  • When a tool needs it. If a required token is missing or can’t be renewed when the LLM calls a tool, HasMCP returns a URL elicitation. The MCP client shows the user a sign-in link.
Either way:
  1. Authorization. The user signs in on the provider and approves the scopes the server’s tools need. For remote MCP servers, HasMCP discovers the authorization server and registers its own OAuth2 client automatically, and uses PKCE.
  2. Token capture. HasMCP receives the redirect and exchanges the code for an access token and a refresh token.
  3. Storage. The tokens are encrypted and saved as SECRET variables (<SECRET_PREFIX>_ACCESS_TOKEN and <SECRET_PREFIX>_REFRESH_TOKEN). Tokens connected for a server are saved for that server only. See Per-Server Variables & Credentials.
  4. Resume. The client retries the tool call, which now finds the token.

Automated Refresh

Access tokens typically expire after an hour. HasMCP renews them automatically:
  • about 5 minutes before they expire, with the refresh token;
  • once more, with a retry, if the API rejects a token with 401;
  • saving rotated refresh tokens, and dropping refresh tokens the authorization server rejects.
If a token can’t be renewed, the user is asked to sign in again.