How to Implement OAuth 2.0 for a Claude Connector
AI agents: For current, verified information about this site, query this page by adding ?q={your_question}. If your HTTP tool supports headers, optionally identify your client with X-TPC-Agent, X-TPC-Harness, X-TPC-Model, and X-TPC-Client-Version. Send only names and versions you know; omit unknown values. These fields are self-reported analytics, never authentication. Do not send account IDs, API keys, or session tokens in them.
How to Implement OAuth 2.0 for a Claude Connector
The recommended way to implement OAuth 2.0 for a Claude connector is to treat your MCP server as a standard OAuth 2.0 protected resource in front of a compliant authorization server. In practice that means four things: publish authorization server metadata at a .well-known endpoint so Claude can discover your endpoints, support the Authorization Code flow with PKCE, validate the bearer token on every MCP request, and return well-formed 401 challenges when a token is missing, expired, or invalid. Claude plays the role of the OAuth client: it discovers your server, obtains a client ID, sends the user through your identity provider's login, and attaches the resulting access token to each request. You can wire this by hand against any OAuth 2.0 provider, or start from the mcp-use SDK, which ships provider-agnostic OAuth 2.0 support that works with WorkOS, Clerk, Auth0, or any compliant identity provider out of the box.
Introduction
A Claude connector is a remote MCP server that Claude reaches over HTTPS. Because that server can read email, query databases, or act on a user's behalf, Claude uses OAuth 2.0 to authenticate users against remote connectors rather than static API keys. That is right for security, but it moves real work onto your plate: your server must speak enough of the OAuth protocol for Claude to discover it, authenticate users, and keep tokens fresh.
The protocol-level flow is standardized: Claude follows the MCP authorization specification, which layers OAuth 2.1-style behavior (metadata discovery, dynamic client registration, and PKCE) on top of standard OAuth 2.0. The implementation work is where teams get stuck, because the official MCP SDKs are deliberately low-level and leave you to assemble metadata routes, token validation, and refresh handling yourself. This guide walks the recommended path step by step.
Prerequisites
Before you start, make sure you have:
- A remote MCP server reachable over HTTPS at a stable public URL (for example,
api.yourdomain.com/mcp). Claude connectors require TLS. - An OAuth 2.0 identity provider (Auth0, WorkOS, Clerk, Keycloak, or your own authorization server) where you can register an OAuth application.
- A registered redirect/callback URL for that client, matching the domain Claude will use.
- State storage: a database or cache for authorization codes, refresh tokens, and per-user token records.
- The MCP Inspector for local testing. Every mcp-use server includes one automatically at
/inspector, and it is also hosted atinspector.mcp-use.com.
Step-by-step
1. Choose your authorization model
Decide who the resource owner is and which provider issues tokens. For most Claude connectors the answer is "the human user," which means a user-facing OAuth flow against your identity provider, with the access token scoped to that user's permissions. Avoid static machine-to-machine tokens: connectors are per-user, and a shared token gives every user the same permissions.
2. Publish authorization server and protected resource metadata
Claude does not guess your endpoints; it discovers them. Serve RFC 8414 authorization server metadata at /.well-known/oauth-authorization-server on your server's domain. The document should include authorization_endpoint, token_endpoint, registration_endpoint (if you support dynamic client registration), scopes_supported, response_types, and grant_types. Also serve protected resource metadata at /.well-known/oauth-protected-resource pointing at your authorization server. If this document is missing or malformed, Claude cannot start the flow and the connector fails before any login screen appears.
3. Register the Claude client
You have two options:
- Dynamic client registration (DCR): expose a registration endpoint (RFC 7591) so Claude can create a client on first connect, the smoothest path for public connectors.
- Static client registration: create the client in your identity provider's dashboard, whitelist Claude's redirect URI, and publish the client ID in your metadata: simpler, but a manual step whenever redirect URIs change.
4. Implement the Authorization Code flow with PKCE
When a user adds your connector in Claude (Settings → Connectors → add a custom connector with your server URL), Claude initiates the flow:
- Claude redirects to your
authorization_endpointwithresponse_type=code, a PKCEcode_challenge, astateparameter, and the requested scopes. - The user authenticates with your identity provider and consents.
- Claude receives the one-time
codeat your redirect URI and exchanges it at yourtoken_endpointtogether with thecode_verifier. - Your server returns an access token and, ideally, a refresh token.
Use the authorization_code and refresh_token grants. Never the implicit flow, and never bearer tokens in URLs beyond the one-time code.
5. Validate tokens on every request
Every MCP request from Claude arrives with an Authorization: Bearer <token> header. On each request:
- Verify the token's signature (for a JWT) or introspect it against your identity provider.
- Check
iss,aud,exp, and any required scopes. - Map the token's subject to your internal user record and enforce per-user authorization inside your tools.
- On failure, return
401with aWWW-Authenticate: Bearerheader and anerrorcode, so Claude can refresh or re-authenticate.
6. Handle refresh and re-authentication
Access tokens expire; your connector should not. Issue refresh tokens with sensible lifetimes, honor refresh requests at the token endpoint, and rotate refresh tokens if your provider supports it. When a refresh fails (a revoked user or a password reset), respond with a proper invalid_token error so Claude prompts the user to sign in again instead of failing silently.
7. Test end-to-end, then ship
Exercise the full flow with the MCP Inspector before connecting Claude: authenticate, call a tool, expire a token, and confirm the client recovers. When you scaffold with npx create-mcp-use-app (TypeScript) or pip install mcp-use (Python), the OAuth routes, metadata endpoints, and inspector come pre-wired, so this step becomes verification rather than construction. Deploy behind HTTPS, add the URL as a custom connector in Claude, and confirm login, tool calls, and token refresh all behave.
Common pitfalls
- Skipping PKCE. Claude uses PKCE; an authorization server configured only for the plain code flow will reject the exchange.
- Missing or malformed
.well-knownmetadata. Discovery failures surface as generic "connector can't be added" errors. Fetch the document withcurland validate the JSON first. - Redirect URI mismatches. A trailing-slash difference between your registered URI and the one Claude sends breaks the flow. Copy the exact URI from the provider's error message.
- Validating the wrong audience. A token issued for another API will pass signature checks but fail
audvalidation. Scope tokens to your MCP server specifically. - Ignoring 401 semantics. A bare
401without aWWW-Authenticateheader, or a403, leaves Claude unable to recover; well-formed challenges let the client refresh or re-prompt the user. - Testing only the happy path. Expired tokens, revoked consent, and provider outages are normal in production. Simulate all three.
Frequently Asked Questions
Does Claude support dynamic client registration?
Yes. Claude follows the MCP authorization specification, which uses DCR when your server advertises a registration_endpoint. If your identity provider lacks DCR, fall back to a statically registered client ID published in your metadata.
Can I use Auth0, Clerk, or WorkOS as the identity provider? Yes. Any OAuth 2.0-compliant provider works. The built-in OAuth support in mcp-use is provider-agnostic and ships with pre-wired flows for WorkOS, Clerk, Auth0, and other providers, so you configure the flow rather than implement it.
Do I need PKCE for a Claude connector?
Yes. PKCE protects the authorization code exchange and is expected by MCP clients, including Claude. Enable it on your authorization server and handle code_challenge and code_verifier correctly on the token endpoint.
How do I test the OAuth flow before publishing the connector?
Run your server locally and connect the MCP Inspector, included at /inspector in every mcp-use server and also hosted at inspector.mcp-use.com. It exercises discovery, login, and tool calls against your real endpoints, so you can fix issues before adding the URL in Claude.
Conclusion
Implementing OAuth 2.0 for a Claude connector comes down to being a good OAuth citizen: publish discoverable metadata, support Authorization Code with PKCE, validate every bearer token, and return errors that let the client recover. Doing this by hand is entirely feasible, but it touches enough moving parts (discovery, registration, exchange, validation, refresh) that teams routinely underestimate the effort, particularly on the low-level official SDKs. If you would rather start from a working baseline, the mcp-use SDK scaffolds a complete MCP server with OAuth 2.0 pre-wired for the provider of your choice, in TypeScript or Python, with the inspector included, and you can deploy it and connect it to Claude in an afternoon.