MCP OAuth for Claude and ChatGPT
How to set up OAuth for a remote MCP server so both Claude and ChatGPT can connect: discovery, PKCE, client registration, the token endpoint, and the identity provider traps.
On this page
- The moving parts
- Step 1: answer 401 with a pointer
- Step 2: serve protected resource metadata
- Step 3: authorization server metadata
- PKCE with S256
- Step 4: client registration, DCR versus CIMD
- Step 5: redirect URIs
- Step 6: the resource parameter and token audience
- Step 7: the token endpoint
- ChatGPT specifics
- Identity provider gotchas
- Debug in this order
- Check your server
Most failed connections to a remote MCP server are OAuth failures. The MCP server answers fine in a terminal, the client reports "Couldn't reach the MCP server" or "Authorization failed", and the authorization server's logs show nothing. This guide covers what both clients need, in the order they ask for it, based on the MCP authorization spec, Claude's authentication docs, Claude's troubleshooting guide and OpenAI's auth docs.
The moving parts
Three roles, as the spec defines them. Your MCP server is an OAuth resource server. The client (Claude, ChatGPT, Grok Build (formerly Cursor), VS Code) is an OAuth client. The authorization server issues tokens and can be a separate host or product from your MCP server, such as Auth0, Okta, Entra ID, Keycloak or Supabase Auth.
The flow:
- The client calls your MCP endpoint without a token.
- You answer
401with a pointer to your protected resource metadata (PRM). - The client reads PRM, finds your authorization server, and reads its metadata.
- The client gets a client ID by DCR, by CIMD, or from a pre-registered client.
- The user signs in through an authorization-code flow with PKCE and a
resourceparameter. - The client calls your endpoint with
Authorization: Bearer <token>. You validate signature, issuer, audience, expiry and scope.
Step 1: answer 401 with a pointer
Claude requires a real 401. Its docs say a WWW-Authenticate header on a 200 response is ignored.
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer resource_metadata="https://mcp.example.com/.well-known/oauth-protected-resource", scope="read"
The resource_metadata URL can be any HTTPS location serving the JSON, which is the most reliable option on serverless or edge platforms that only route a path prefix. If you leave the pointer out, the spec and Claude fall back to probing /.well-known/oauth-protected-resource/<your-mcp-path> and then /.well-known/oauth-protected-resource on your origin. That fallback only works if your platform serves /.well-known/*. Include the pointer. The scope parameter controls which scopes Claude requests; without it Claude uses scopes_supported from PRM.
For a ChatGPT tool that needs auth mid-conversation, OpenAI documents a second signal: the tool result has isError: true and _meta["mcp/www_authenticate"] carrying the same challenge, with securitySchemes declared per tool.
Step 2: serve protected resource metadata
{
"resource": "https://mcp.example.com/mcp",
"authorization_servers": ["https://auth.example.com"],
"scopes_supported": ["read", "write"],
"bearer_methods_supported": ["header"]
}
Rules from the Claude and OpenAI docs:
resourcemust equal the MCP server URL exactly as a user enters it, including the path. A mismatch is a common silent failure.- List your primary issuer first. Claude uses only the first entry of
authorization_serversand does not fall back to later ones. The spec lets clients choose; Claude's choice is first. - Use one identical issuer string in PRM and in the authorization server's
issuer. OpenAI requires an exact string match with no normalization of slashes, ports or case.
Step 3: authorization server metadata
Claude tries /.well-known/oauth-authorization-server (RFC 8414) first, then /.well-known/openid-configuration. You only need one. Claude's docs note that most hosted identity providers serve only the OpenID Connect document, and that is fine.
The fields that matter:
{
"issuer": "https://auth.example.com",
"authorization_endpoint": "https://auth.example.com/authorize",
"token_endpoint": "https://auth.example.com/token",
"registration_endpoint": "https://auth.example.com/register",
"code_challenge_methods_supported": ["S256"],
"client_id_metadata_document_supported": true,
"token_endpoint_auth_methods_supported": ["none", "client_secret_post"],
"grant_types_supported": ["authorization_code", "refresh_token"],
"authorization_response_iss_parameter_supported": true
}
The host serving this document must be reachable from the client's network. Claude's discovery requests to the authorization server come from the same Anthropic IP range as requests to your MCP server, so a WAF in front of your identity provider can break the flow even when your MCP server is reachable.
PKCE with S256
Both clients send code_challenge_method=S256 on every authorization request. The spec goes further: clients must verify support before they start, and if code_challenge_methods_supported is absent from metadata, a spec-compliant client must refuse to proceed. For OpenID Connect providers, which do not define that field, the spec says the provider must include it anyway. OpenAI says servers without S256 are unsupported. Check that your provider's metadata includes the field.
Step 4: client registration, DCR versus CIMD
The client needs a client ID at your authorization server. The spec lists these in priority order: a pre-registered client, a Client ID Metadata Document, then Dynamic Client Registration.
DCR (RFC 7591) means the client POSTs JSON to your registration_endpoint and gets credentials back. It works with older clients, and it is marked optional in the current spec. The cost: Claude registers a new client on every fresh connection, which can produce very large numbers of registered clients. Claude's docs say to prefer CIMD or Anthropic-held credentials for servers expecting high directory traffic. OpenAI's docs say ChatGPT registers once per MCP server connection and reuses the client_id, so keep registered clients and secrets valid or you will see invalid_client.
CIMD means the client_id is an HTTPS URL pointing to a JSON document the client hosts. Your authorization server fetches it, checks that client_id in the document matches the URL exactly, and validates the redirect URI against the document. No registration call and no stored clients. You advertise support with client_id_metadata_document_supported: true. Claude only picks CIMD when your metadata advertises that and also lists none in token_endpoint_auth_methods_supported, because its CIMD client is a public client at your token endpoint. Otherwise it falls back to DCR. ChatGPT's stable client document is https://chatgpt.com/oauth/client.json, per OpenAI's docs. Treat the fetch as an SSRF risk: restrict what hosts and address ranges your server will fetch.
Recommendation: support both. Advertise CIMD and keep a registration_endpoint. The spec lists CIMD as preferred and DCR as a backward-compatible fallback, so offering both covers the widest range of clients.
If your authorization server supports neither, Claude's docs offer a third route: create a confidential client for Claude and enter its ID and secret in the directory portal (Anthropic-held credentials), registering https://claude.ai/api/mcp/auth_callback. It does not work with URL-pattern listings.
Step 5: redirect URIs
- Claude on the web, Desktop, mobile and Cowork: exactly
https://claude.ai/api/mcp/auth_callback. - Claude Code: a loopback redirect on an ephemeral port such as
http://localhost:3118/callback. Claude Code declares bothhttp://localhost/callbackandhttp://127.0.0.1/callbackin its CIMD, so match both while ignoring the port. - ChatGPT:
https://chatgpt.com/connector_platform_oauth_redirectfor the stable case, orhttps://chatgpt.com/connector/oauth/{callback_id}otherwise. OpenAI says to copy the exact production URI from the MCP server's management page.
Under DCR the client sends its own redirect URIs, so your registration endpoint must accept them. Under pre-registration, allow-list them yourself.
Step 6: the resource parameter and token audience
Clients must send the RFC 8707 resource parameter on both the authorization and token requests, set to the canonical MCP server URL. Claude sends lowercase scheme and host, no trailing slash, no fragment, no default port. Your token must carry that audience, and your MCP server must check it. The spec is firm: servers must only accept tokens issued for them and must not pass tokens through to upstream APIs.
Compare the audience against the canonical form instead of the raw string a user typed. A strict byte comparison against a URL with a trailing slash is a classic cause of "works in curl, fails in Claude".
Step 7: the token endpoint
Two details cause real outages.
Form encoding. The token endpoint must accept application/x-www-form-urlencoded, per RFC 6749. Claude sends both the code exchange and refresh with that content type. A framework that only parses JSON returns 415 Unsupported Media Type. DCR at /register is JSON, so the two routes need different parsers.
import express from "express";
const app = express();
app.use("/register", express.json());
app.use("/token", express.urlencoded({ extended: false }));
Latency. Claude waits 10 seconds for discovery, registration and token responses and 30 seconds for refresh. Past that it treats the flow as failed even if your server finishes. Check any gateway in front of the endpoint for response buffering.
Refresh. Claude refreshes reactively on a 401 and proactively up to five minutes before expiry. Return invalid_grant for a dead refresh token, not invalid_request or a custom code. For public clients (DCR registers Claude as one), rotate refresh tokens and return the new one in the same response that invalidates the old one. Claude appends offline_access when your metadata lists it in scopes_supported.
ChatGPT specifics
- Set
authorization_response_iss_parameter_supported: trueonly if every authorization response, including errors, carries anissthat matchesissuer. ChatGPT rejects missing or mismatchediss. - ChatGPT requests the OIDC scopes your metadata advertises (
openid,email,profile). Make sure each is enabled for the OAuth client. - The public submission portal supports no auth, OAuth, or mixed. It does not support API keys, Basic or custom-header flows.
- Declare
securitySchemesper tool:noauthoroauth2with scopes.
Identity provider gotchas
- Entra ID. Token requests fail with
AADSTS9010010(sometimes surfaced asinvalid_target) because Claude sends the MCP URL asresourceand Entra needs it registered. Add the full MCP URL under Expose an API as an Application ID URI on the API app registration. If the domain is not verified in your tenant, Entra requires access token version 2 first (requestedAccessTokenVersion: 2), which changesissandaudfor every caller of that API, so check your validators first. - Metadata only at
openid-configuration. Auth0, Okta, Entra, Keycloak and Supabase Auth serve that document and not the RFC 8414 one. Fine for Claude. A custom client that only looks foroauth-authorization-serverwill miss it. - Registration support. Read your provider's metadata for
registration_endpointandclient_id_metadata_document_supportedinstead of assuming either exists. If neither is present, clients have no way to get a client ID without pre-registration. - Issuer mismatch. If tokens come from a third-party IdP but your metadata advertises a different issuer, validation fails. Serve the IdP's real issuer.
- Redirects. An apex-to-
wwwor region redirect on the MCP URL makes Claude drop theAuthorizationheader, and you see an authorization failure with no obvious cause. Register the final URL. - WAF on the IdP. Allowlist the client's egress ranges on the IdP host and on
/.well-known/paths, not only on the MCP host. - Token in the URL. Never accept access tokens or API keys in query strings. The spec prohibits it, and ChatGPT's guidelines forbid credentials in listings.
Debug in this order
curl -i https://mcp.example.com/mcp # expect 401 plus resource_metadata
curl -i https://mcp.example.com/.well-known/oauth-protected-resource # expect 200 JSON
curl -i https://auth.example.com/.well-known/openid-configuration # or oauth-authorization-server
curl -sI https://mcp.example.com/mcp # expect no 3xx to another host
If your MCP server sees the first request and the authorization server sees nothing, discovery failed. If both are reached and the token request fails, look at audience, PKCE, form encoding and latency.
Check your server
Run the free scan at /scan. It reads your 401 challenge, PRM and authorization server metadata, and reports the findings above.
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.