Skip to content

All posts

AI

How to configure OAuth for remote MCP servers in Claude Code

Ritesh Patel

AI: How to configure OAuth for remote MCP servers in Claude Code

To configure OAuth for a remote MCP server in Claude Code, add the server with claude mcp add --transport http, then sign in from /mcp or with claude mcp login. Claude Code refreshes stored tokens automatically. Removing a server deletes the tokens Claude Code stored for it, but does not revoke them at the authorization server.

This post follows the Claude Code MCP docs as fetched on 2026-11-27. The docs tie some behavior to version numbers, so check the output on your own build before you rely on it.

Add a remote server that uses OAuth

Claude Code supports OAuth 2.0 for remote MCP servers. It flags a server as needing sign-in when the server responds with 401 Unauthorized or 403 Forbidden. The docs recommend the HTTP transport for remote servers, because HTTP supports OAuth. The WebSocket transport supports neither OAuth nor the claude mcp add --transport flag, so use HTTP for any server that needs sign-in.

  1. Add the server. The example below uses the Sentry server from the docs. Running claude mcp add again with the same name at the same scope fails, so skip this step if the server is already configured.
claude mcp add --transport http sentry https://mcp.sentry.dev/mcp
  1. Start Claude Code, run /mcp, and follow the steps in your browser to log in.
  2. Run /mcp again and check that the server’s row shows a checkmark. A server with bad credentials shows a cross instead, and its failure detail includes the HTTP status the server returned.

If your browser does not open, copy the URL that Claude Code prints and open it yourself. If the browser redirect fails with a connection error, paste the full callback URL from the address bar into the URL prompt in Claude Code.

Sign in from the shell

The claude mcp login command runs the same OAuth flow from your shell, so you do not need the /mcp panel open. This matters over SSH or on Linux without a display server, where the command prints the authorization URL instead of opening a browser. You open that URL on your own machine and paste the redirect URL back at the prompt. The paste step needs an interactive terminal, so connect with ssh -t.

claude mcp login sentry
claude mcp login sentry --no-browser

The --no-browser flag forces the URL prompt even when Claude Code detects a local browser.

Sign in before you run a script

A claude -p run or an Agent SDK run has no /mcp panel, so Claude Code cannot run the OAuth flow there. Complete the sign-in from an interactive session first, using /mcp or claude mcp login. When a configured server needs sign-in during a claude -p or Agent SDK run with tool search enabled, Claude Code tells the model that the server’s tools are unavailable until you authorize it.

Register an OAuth app when dynamic registration is not supported

Some servers do not support automatic setup through Dynamic Client Registration. The error text is “Incompatible auth server: does not support dynamic client registration”. In that case, register an OAuth app with the server first, then pass its client ID and secret when you add the server.

  1. In the server’s developer portal, create an app and note the client ID and client secret. If the form asks for a redirect URI, enter http://localhost:PORT/callback with a port you choose.
  2. Add the server with the credentials. --callback-port must match the port in the redirect URI. Omit --client-secret for a public client that has no secret.
claude mcp add --transport http \
  --client-id your-client-id --client-secret --callback-port 8080 \
  my-server https://mcp.example.com/mcp
  1. Run /mcp in Claude Code and follow the browser login flow.

The --client-secret flag prompts for the secret with masked input. Setting the MCP_CLIENT_SECRET environment variable skips the prompt. You can set the secret only when you add the server. To change it later, remove the server and add it again with --client-secret and the same --scope. Run claude mcp get my-server to confirm that OAuth credentials are configured.

Restrict the OAuth scopes Claude Code requests

By default, Claude Code requests the scopes the server determines. To pin them, set oauth.scopes in the server’s entry in .mcp.json. The value is one string, with scopes separated by spaces, in the format of the scope parameter in RFC 6749.

{
  "mcpServers": {
    "slack": {
      "type": "http",
      "url": "https://mcp.slack.com/mcp",
      "oauth": {
        "scopes": "channels:read chat:write search:read"
      }
    }
  }
}

The docs say oauth.scopes takes precedence over the scopes discovered from the server’s metadata. If the authorization server advertises offline_access, Claude Code appends it to your pinned list so the access token can be refreshed without a new browser sign-in. If you add that scope to the list later, run /mcp and authenticate again. Claude Code requests the pinned scopes rather than the scope the server names, so if you authenticate again without adding the scope, the new token still lacks it.

Where tokens are stored and how they are cleared

The docs say authentication tokens “are stored securely and refreshed automatically”. They do not name the file or keychain entry that holds access tokens, so this post does not give one. The client secret from the previous section is different: the docs say it is stored in the system keychain on macOS or in a credentials file, and not in your configuration.

The docs describe four actions that touch a server’s stored sign-in. Their effects differ, so choose the one that matches what you need.

Action What the docs say it does
/mcp > Clear authentication Revokes access, per the docs. It also discards the server’s cache entry.
claude mcp logout <name> Clears the stored credentials. Where the authorization server supports revocation, it revokes the tokens.
claude mcp remove <name> Deletes the OAuth tokens and client registration stored for the server. It does not revoke them at the authorization server.
/mcp > Re-authenticate Signs in again, for example after the server rejects the stored refresh token.

To decommission a remote server with revocation, run the two commands in this order. The docs say to log out first, because remove does not revoke tokens.

  1. Run claude mcp logout sentry in your shell.
  2. Run claude mcp remove sentry.

What happens when a stored token is rejected

When an OAuth server you already signed in to rejects your stored token with 401 Unauthorized, Claude Code reconnects with or without a new token. If the stored login has a refresh token, Claude Code refreshes the token, reconnects, and retries the request once. The server is flagged in /mcp only if that retry also fails.

If the stored login has no refresh token, Claude Code connects once without the token and deletes the rejected token. If the server answers, its tools still work while you are signed out. If the server rejects the connection too, /mcp shows that the server needs authentication.

When the server rejects the stored refresh token, Claude Code shows a notice that points to /mcp. Open /mcp and choose Re-authenticate on that server before the next tool call fails. In a claude -p or Agent SDK run on macOS, a request sent while Claude Code cannot read the stored token fails with a stored-credentials error.

What an admin should check first

Use this list when you review a remote server that signs in with OAuth. Each item comes from the same docs page.

  1. Run claude mcp list and claude mcp get <name>. A server blocked by enterprise managed policy appears with that status. A server blocked by an allowlist, a denylist or another managed restriction is hidden from both commands, so a missing entry can mean a policy block. Ask your administrator which policy applies. The restriction setup is covered in how to restrict which MCP servers Claude Code can use.
  2. Check for an Authorization header. If the server’s configuration sets headers.Authorization, directly or through a headersHelper, Claude Code uses that credential and does not fall back to OAuth. A 401 or 403 is then reported as a failed connection, not a sign-in prompt. Remove the header if you want the OAuth flow.
  3. Check for duplicate names. If the same server name appears in more than one scope with different endpoints, Claude Code warns about the conflict. Sign-ins are stored per endpoint, so a user may need to sign in again in a project where a different definition loads.

What an admin should check about scope and tokens

The next checks cover where a server is stored, how its endpoints are reached and who can add it.

  1. Check the scope. Claude Code adds a server to local scope by default, which is private to the user and the project where it was added. Project scope writes the server to .mcp.json for the team to share. Review that file the way the project MCP server review describes.
  2. Check token endpoints. On the v2 client runtime, the docs say Claude Code sends OAuth credentials only to a token endpoint served over HTTPS, or at localhost, 127.0.0.1 or ::1. Sign-in fails for a plain http:// token endpoint anywhere else, such as a device on your local network.
  3. Check claude.ai connectors. On Team and Enterprise plans, only admins can add servers at claude.ai/customize/connectors, according to the docs.

Where this fits in MCP security

Sign-in controls who can connect a server. It does not decide which tools the server exposes or what those tools may do. The docs cover the sign-in flow, the stored tokens and the scope request, and the rest of the review is separate work. The steps above cover the sign-in side only.

For the wider review, start with the MCP security guide, then read the allowlist post and the project server review linked above. Next, run claude mcp list on one developer machine and check each remote server’s status against the list in the admin section.

Frequently asked questions

Does removing a remote MCP server revoke its OAuth tokens?

No, according to the Claude Code docs. Running claude mcp remove deletes the OAuth tokens and client registration that Claude Code stored for that server, but it does not revoke the tokens at the authorization server that issued them. To revoke them where that server supports revocation, run claude mcp logout with the server name first, then run claude mcp remove.

What does Claude Code do when a stored OAuth token gets a 401?

If the stored login has a refresh token, Claude Code refreshes it, reconnects and retries the request once. The server is flagged in /mcp only if the retry fails too. If there is no refresh token, Claude Code connects once without the token. If the server answers, its tools keep working while you are signed out, and the rejected token is deleted. If the server rejects that connection too, /mcp shows that it needs authentication.

Can I limit the OAuth scopes Claude Code requests?

Yes. Set oauth.scopes in the server's configuration to a single space-separated string. The docs call this the supported way to restrict a server to an approved subset. A pinned value takes precedence over scopes discovered from the server. If the authorization server advertises offline_access, Claude Code appends it so tokens can refresh without a new browser sign-in. After you change the list, run /mcp and authenticate again.

Can I use OAuth with a server that does not support dynamic client registration?

Yes. Register an OAuth app in the server's developer portal, then add the server with --client-id, --client-secret and --callback-port. The docs say the client secret is stored in the system keychain on macOS or in a credentials file, not in your configuration. If sign-in fails with a redirect URI mismatch on v2.1.229, upgrade to v2.1.231 or later, which restored the localhost form of the callback URL.

Ritesh Patel, Founder & VP Product, Nirmata

Ritesh Patel is a founder of Nirmata and leads its products. He writes about governing AI coding agents, MCP security and Kubernetes policy as code.

LinkedIn

Govern AI agents with AIControlsControl what AI agents do, and prove it.
Share

Stay up-to-date with the latest in AI, governance, and infrastructure

Subscribe to our free monthly newsletter to get industry news, DevOps tips, and articles right to your inbox on Kyverno, containerization, Kubernetes deployment & management and more.