Skip to content

OAuth for Codex and Claude Code on Your MCP Server

Securing an MCP Server explains how the authorization server and the MCP server of the MCP server sample work. This guide explains how an agent becomes known to the authorization server, then connects real agents: first to the sample on your machine, then to your own service.

CIMD vs DCR

An MCP client that has never met the authorization server becomes known to it in one of two ways. With dynamic client registration (DCR, RFC 7591), the client posts its metadata and gets a client_id the server generates. With a Client ID Metadata Document (CIMD), the client's client_id is the https URL of a JSON document its vendor publishes, and the authorization server fetches it, as draft-ietf-oauth-client-id-metadata-document-03 specifies.

The MCP specification marks DCR deprecated as of revision 2026-07-28 and has a client pick from what the authorization server's metadata offers: client information registered for it in advance if it has any, then CIMD if the server announces client_id_metadata_document_supported, then DCR if it has a registration endpoint, and otherwise ask the user. Both agents publish documents, so an authorization server with CIMD turned on admits them by their documents.

The two side by side, with the CIMD settings in Securing an MCP Server:

DCRCIMD
client_idGenerated by the authorization serverThe https URL of the client's metadata document
Registration stepThe client posts its metadata to the registration endpointNone: the authorization server fetches the document
What the identifier provesNothing: anybody can register any nameWhoever controls the vendor's web address published the document
Across authorization serversThe client registers again at eachThe same client_id everywhere
MCP specificationDeprecated, kept for clients of earlier revisionsRecommended
Who gets inHolders of an initial access token, or anybody when registration is open, whatever the prefix list saysDocuments under AllowedClientIdPrefixes, or any document when the list is unset
Abblix OIDC ServerAddDynamicClientRegistration()AddClientIdMetadataDocuments(), experimental

Only whoever controls the vendor's web address can publish a document under it, so a prefix trusts everything under it: never name a host where others can publish files. A document outside the prefixes is refused without a request leaving the server, and by default the fetch reaches no private network. Removing a prefix refuses that vendor's clients at their next request, token renewal included, once the new list takes effect.

The list says whose document it is, not which program is asking. A client_id is public, and with a loopback redirect URI any program on the user's computer can start a flow under a trusted vendor's identifier; PKCE keeps a stolen code useless to a program that did not start the flow, but not to one that starts its own. Against that, the warning on the consent page and the user's own judgment are the defense, which is why the sample's consent page asks even for a trusted vendor, and yours should too (Securing an MCP Server).

Try them on the sample

With the sample running, in Docker or with the .NET SDK (Securing an MCP Server), OpenAI Codex and Anthropic Claude Code can use the tracker on your machine. The agents need the host's development certificate, exported to the sample's certificate folder when the sample runs in Docker (Use the development certificate); each agent trusts it once the host does. What you need in the sample's README shows how to install both agents, sign them in to their vendors' accounts and trust the certificate.

Add the agents

Each agent registers the MCP server under a name of your choice, tracker in the sample:

OpenAI CodexAnthropic Claude Code
Adding the server[mcp_servers.tracker] with url = "https://localhost:5008/mcp" in ~/.codex/config.tomlclaude mcp add --transport http tracker https://localhost:5008/mcp
Signing incodex mcp login trackerclaude mcp login tracker
client_idhttps://chatgpt.com/oauth/codex/client.jsonhttps://claude.ai/oauth/claude-code-client-metadata
Prefix in the samplehttps://chatgpt.com/oauth/codex/https://claude.ai/oauth/
Redirect URI (callback)http://127.0.0.1/callback, a random porthttp://localhost/callback, a random port
A 403 for tasks.writeReports "Insufficient scope"; the user runs codex mcp login --scopes tasks.read,tasks.write trackerOpens the browser with tasks.write added and repeats the call once the user approves

How the agents sign in

The flow both agents go through:

Vendor's siteAuthorization serverUser's browserMCP serverAgent (MCP client)Vendor's siteAuthorization serverUser's browserMCP serverAgent (MCP client)First request without a token401 with resource_metadataRead protected resource metadataResource URI and authorization serverRead authorization server metadataclient_id_metadata_document_supportedOpen the authorization requestclient_id (document URL), redirect URI, scope, PKCE challenge, resourceFetch the client's metadata documentClient name and redirect URIsSign-in and consent pageThe user signs in and approvesRedirect with the code and issCodeToken request with the code, PKCE verifier and resourceAccess token for the MCP server, refresh tokenTool call with the access tokenResult, or 403 insufficient_scope

The sample's prefixes admit the two vendors. Codex's prefix covers only the documents under Codex's path. Claude Code's document sits directly under /oauth/, and a prefix ends with a slash, so its prefix covers every client document Anthropic publishes there. The consent page names the agent and its vendor's host, such as Codex at chatgpt.com.

Both agents receive the code on this computer. Codex's document lists http://127.0.0.1/callback and http://localhost/callback with no port, and the authorization server accepts a redirect to this computer on any port, as RFC 9700, section 2.1 and draft section 4.2 allow.

A session with Codex

Codex reports the refusal, and the user signs in again with the write scope:

Code
$ codex mcp login tracker
  (the browser: sign-in, then the consent page "Allow Codex?" with tasks.read and offline_access, Allow)
Successfully logged in to MCP server 'tracker'.

> Call who_am_i and show my tasks

  I'll check the available task tracker tools, identify your account, then list your tasks.
  mcp: tracker/who_am_i (completed)
  mcp: tracker/list_my_tasks (completed)

`who_am_i` returned subject `1234567890` with `tasks.read` access.

Your open tasks:

- Review the onboarding checklist
- Send the weekly status report
- Book the team offsite venue

> Create a task: prepare the slides

  mcp: tracker/create_task (failed)
  tool call error: tool call failed for `tracker/create_task` ... error: Insufficient scope

I couldn't create the task because the tracker connection returned an "Insufficient scope" error. The task title is Prepare the slides. Please reconnect or grant the tracker access, then I can add it.

$ codex mcp login --scopes tasks.read,tasks.write tracker
  (the browser: the consent page "Allow Codex?", now with tasks.write, Allow)
Successfully logged in to MCP server 'tracker'.

> Create a task: prepare the slides

  I'll check the connected task tools and create a task titled "Prepare the slides."
  mcp: tracker/create_task (completed)

Created the task "Prepare the slides."

Codex's sign-in request carries https://chatgpt.com/oauth/codex/client.json as the client_id, a PKCE challenge, a redirect to 127.0.0.1 on a random port, the scopes, with tasks.write added on the second sign-in, and the MCP server as the resource.

A session with Claude Code

Claude Code reads with the token it has, then asks for the write scope:

Code
> Call who_am_i and show my tasks

  Called tracker 2 times

who_am_i
- Subject: 1234567890
- Client: https://claude.ai/oauth/claude-code-client-metadata
- Scopes: offline_access, tasks.read (read-only access to tasks)

Tasks (all open):
1. Review the onboarding checklist
2. Send the weekly status report
3. Book the team offsite venue

With these permissions I can only read tasks. Creating or closing them won't work: that needs the write scope.

> Create a task: prepare the slides

  Called tracker

● Opening your browser to re-authenticate MCP server "tracker". If it doesn't open, use this link to the server's sign-in page: https://localhost:5007/connect/authorize?response_type=code&client_id=https%3A%2F%2Fclaude.ai%2Foauth%2Fclaude-code-client-metadata&code_challenge=...&code_challenge_method=S256&redirect_uri=http%3A%2F%2Flocalhost%3A39975%2Fcallback&state=...&scope=offline_access+tasks.read+tasks.write&prompt=consent&resource=https%3A%2F%2Flocalhost%3A5008%2Fmcp

Task created: #4 "Prepare the slides", status: open.

I was wrong when I said creating tasks wouldn't work. Before the call, the tracker server re-authenticated and received the tasks.write scope, so writing is now allowed.

The request in the link carries what the flow above shows: Claude Code's document address as the client_id, a PKCE challenge, a redirect to localhost on the port it chose, the scopes with tasks.write added, and the MCP server as the resource.

Connect your own MCP server

Your own authorization server, built on Abblix OIDC Server, signs people in through your real identity system, and your MCP server sits in front of your service. Each is set up as Securing an MCP Server shows for the sample: the resource and its scopes, metadata documents and the prefixes, a consent page, and at the MCP server its metadata, token validation and scopes. External identity providers shows how to sign people in through the identity system you already run.

An agent that runs in its vendor's cloud rather than on the user's computer goes through the same steps, with two differences. Your authorization server redirects the browser to an https address on the vendor's domain, listed in its metadata document and matched exactly: the any-port exception covers redirect URIs on the user's own computer only. And the vendor's servers, not a program on the user's computer, redeem the code at your token endpoint, renew the access token there with the refresh token, and call your MCP server. Signing out at the authorization server revokes the session's refresh tokens, and the access token the vendor already holds works until it expires (Know what sign-out reaches).

Where the servers have to be reachable depends on where the agent runs. An agent in its vendor's cloud reaches your authorization server's metadata, its token endpoint and your MCP server from the internet, so they are served there over HTTPS; the authorization endpoint and the sign-in pages only need to be reachable from your users' browsers. Agents on your users' computers, like the two above, reach the servers from those computers, as the users' browsers do, so your internal network or VPN is enough. Either way the authorization server needs outbound HTTPS access to fetch the vendors' metadata documents.

Give each server a host name of its own, for example https://login.example.com and https://tasks.example.com/mcp, with certificates the agents trust, and use those addresses as the issuer and as the resource URI on both sides. In the tenant, AllowedClientIdPrefixes lists the vendors you trust and NewClientOptions.AllowedResources names your MCP server's resource URI; ClientIdMetadataDocuments.DocumentAddresses stays at its default, PublicOnly. The production hardening checklist lists what a deployment needs beyond MCP.

Troubleshooting

What you seeWhyWhat to do
The agent reports that the server does not support dynamic client registrationThe authorization server's metadata does not announce client_id_metadata_document_supported, so the agent looks for a registration endpointCall AddClientIdMetadataDocuments() and list the agent's vendor in AllowedClientIdPrefixes
The authorization request is answered 400 invalid_client and the browser is not redirectedThe client_id URL is outside AllowedClientIdPrefixes, or the document cannot be fetched or its client_id differs from its URLAdd the vendor's prefix with its trailing slash; the authorization server's log names the reason
The authorization request is answered 400 invalid_requestThe redirect URI is not in the client's documentUse a redirect URI the document lists; only the port of a redirect to the user's own computer may differ
The authorization request ends with invalid_targetThe resource the agent names is not in the tenant's Resources or not in NewClientOptions.AllowedResources, written exactly the same, trailing slash includedDeclare the MCP server's resource URI as the same string in both
The MCP server answers 401 invalid_token to a fresh tokenThe audience the MCP server expects differs from the resource URI its protected resource metadata publishes, or the token's issuer differs from the address the MCP server reads the authorization server's metadata from, often behind a reverse proxyConfigure the MCP server's audience and its published resource URI from one value, and its authority from the issuer the tokens carry
A tool call is answered 403 insufficient_scopeThe token lacks the scope the tool needsWith Codex, run codex mcp login --scopes tasks.read,tasks.write tracker; a client with the step-up flow, such as Claude Code, asks for the scope by itself

Further reading: