MCP Shipyard
← Guides

MCP invalid_token after OAuth: sign-in works, calls return 401

Why an MCP server rejects a token it just issued: audience, issuer, signing keys, expiry and refresh. Decode the token, replay the call with curl, and fix the cause.

oauthinvalid_tokenaudiencejwtresource-indicatorsclaudechatgpt

MCP invalid_token after OAuth

When sign-in succeeds and the next MCP call returns 401 with error="invalid_token", your server is rejecting a token your own authorization server issued. In most cases the token's audience, issuer or signing key does not match what the MCP server checks, so decode the token and compare each claim with your validation code.

invalid_token is the Bearer token error code from RFC 6750. The MCP authorization spec says servers return 401 when authorization is required or the token is invalid, and 403 for invalid scopes or insufficient permissions. The exact message a client shows for this varies and is not verified here.

What the spec requires of your server

The MCP authorization spec (2025-11-25 revision) says clients must send the RFC 8707 resource parameter on authorization and token requests, and servers must validate that tokens were issued for them. It also says a server must not pass the token through to upstream APIs. Anthropic's docs say Claude sends resource in canonical form (lowercase scheme and host, no trailing slash, no fragment, no default port) and that your server should accept that canonical value when checking the audience. Sources: the MCP authorization spec and Claude's authentication docs.

Diagnosis tree

1. Capture a token and the exact rejection

Get a token from the same authorization server and flow the client used. Then replay the call.

curl -i -X POST https://mcp.acme.example/mcp \
  -H "Authorization: Bearer $TOKEN" \
  -H 'Content-Type: application/json' \
  -H 'Accept: application/json, text/event-stream' \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'

Read the WWW-Authenticate header on the 401. A well-built server includes an error_description. If yours does not, add one in development, because the next steps depend on knowing which check failed.

If the token is a JWT, decode the payload. Do this on a development token only, and do not paste real tokens into online decoders.

echo "$TOKEN" | cut -d. -f2 | tr '_-' '/+' | base64 -d 2>/dev/null | jq '{iss, aud, exp, iat, nbf, scope, scp, client_id}'

Opaque tokens cannot be decoded: use your authorization server's introspection endpoint instead.

2. Compare aud with the resource the client sent

Token audMeaning
https://mcp.acme.example/mcpMatches the canonical MCP URL. Good
https://mcp.acme.example/mcp/Trailing slash. Your check may be a strict string compare
the OAuth client IDThe provider ignored the resource parameter and used a default
an API identifier such as acme-apiThe provider maps resources to API identifiers: register the MCP URL as one
missingThe provider does not support resource indicators

Fixes:

  • Compare against the canonical form of the URL, not the raw string a user typed.
  • Make the protected resource metadata resource equal the URL users enter (auth.resource-mismatch). Anthropic and OpenAI docs say clients copy it into the audience.
  • Register the MCP URL as an API or resource in your provider so it issues that audience. Anthropic's troubleshooting page documents one provider-specific case: Microsoft Entra ID returns AADSTS9010010 when the MCP URL is not registered as an Application ID URI on the API app registration.
  • If the provider cannot issue per-resource audiences, validate against the audience it does issue, and document that choice. Do not skip audience validation.

3. Compare iss with what you validate

curl -s https://auth.acme.example/.well-known/openid-configuration | jq -r .issuer
curl -s https://mcp.acme.example/.well-known/oauth-protected-resource | jq -r '.authorization_servers[0]'

The three strings, the token's iss, the metadata issuer and the authorization_servers entry, should be identical. OpenAI's auth docs say ChatGPT compares them without normalization, so a trailing slash is a difference. The scanner reports a mismatch as auth.issuer-mismatch.

4. Can the MCP server verify the signature?

curl -s https://auth.acme.example/.well-known/openid-configuration | jq -r .jwks_uri | xargs curl -s | jq '.keys[] | {kid, alg, use}'

Compare the kid in the token header with the keys the server fetches. Failures here look like invalid_token on every call:

  • The MCP server caches an old key set after the provider rotated keys. Refetch on an unknown kid.
  • The server fetches keys from an internal URL that differs from jwks_uri, or from a host behind a firewall.
  • The token is signed with an algorithm your validator does not allow.

5. Check time

Look at exp, iat and nbf in step 1 against the clock on your server.

date -u +%s

A server clock a few minutes ahead rejects fresh tokens (nbf in the future) and one behind accepts expired ones. Allow a small clock skew in the validator. Very short access token lifetimes also matter: per the scanner's rule text, Claude refreshes on a 401 and five minutes before expiry, so refresh must work. The scanner checks that refresh tokens are obtainable with the auth.refresh-token rule.

Refresh failing is a separate path. Return invalid_grant for a dead refresh token from your token endpoint, not invalid_token, and rotate refresh tokens for public clients.

6. Is the token being forwarded or exchanged wrongly?

If your MCP server calls an upstream API with the same token, the upstream will reject the audience. The spec prohibits passthrough: exchange the token for an upstream token or use server-side credentials.

7. Scopes

A token missing a scope your tool needs should produce 403 with insufficient_scope, not invalid_token. If your server uses 401 for scope failures, clients may restart the whole login. Publish narrow scopes and challenge for more only when a privileged tool is used.

Quick reference

SymptomLikely cause
Every call fails right after sign-inAudience or issuer mismatch
Works for a while, then fails all at onceKey rotation, expiry, clock skew
Works in Inspector with a hand-made token, fails in ClaudeToken minted with a different audience than Claude's resource
Works for some usersTenant-specific issuer or audience

Verify

Replay step 1 with a fresh token and expect 200 with a tool list. Then confirm a token with a wrong audience is rejected, so you know the check is real.

If the server answers 401 before any token exists, the problem is earlier: see 401 without resource_metadata.

Run a free scan at https://mcpshipyard.com/scan to check every rule at once.

Check your server against these rules

Scan runs the checks from this guide against a live URL and lists each failing rule with its fix.

Scan a server