# OAuth for Codex and Claude Code on Your MCP Server

[Securing an MCP Server](https://www.abblix.com/en/docs/securing-an-mcp-server) explains how the authorization server and the MCP server of the [MCP server sample](https://github.com/Abblix/Oidc.Server.GettingStarted/tree/master/McpServerSample) 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](https://datatracker.ietf.org/doc/html/rfc7591)), 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](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-client-id-metadata-document-03) specifies.

The [MCP specification](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/client-registration) 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](https://www.abblix.com/en/docs/securing-an-mcp-server#admit-mcp-clients-with-cimd):

| | DCR | CIMD |
| --- | --- | --- |
| `client_id` | Generated by the authorization server | The `https` URL of the client's metadata document |
| Registration step | The client posts its metadata to the registration endpoint | None: the authorization server fetches the document |
| What the identifier proves | Nothing: anybody can register any name | Whoever controls the vendor's web address published the document |
| Across authorization servers | The client registers again at each | The same `client_id` everywhere |
| MCP specification | Deprecated, kept for clients of earlier revisions | Recommended |
| Who gets in | Holders of an initial access token, or anybody when registration is open, whatever the prefix list says | Documents under `AllowedClientIdPrefixes`, or any document when the list is unset |
| Abblix OIDC Server | `AddDynamicClientRegistration()` | `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](https://www.abblix.com/en/docs/securing-an-mcp-server#ask-the-user-on-a-consent-page)).

## Try them on the sample

With the sample running, in Docker or with the .NET SDK ([Securing an MCP Server](https://www.abblix.com/en/docs/securing-an-mcp-server#run-the-sample)), 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](https://github.com/Abblix/Oidc.Server.GettingStarted/tree/master/McpServerSample#use-the-development-certificate)); each agent trusts it once the host does. [What you need](https://github.com/Abblix/Oidc.Server.GettingStarted/tree/master/McpServerSample#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 Codex | Anthropic Claude Code |
| --- | --- | --- |
| Adding the server | `[mcp_servers.tracker]` with `url = "https://localhost:5008/mcp"` in `~/.codex/config.toml` | `claude mcp add --transport http tracker https://localhost:5008/mcp` |
| Signing in | `codex mcp login tracker` | `claude mcp login tracker` |
| `client_id` | `https://chatgpt.com/oauth/codex/client.json` | `https://claude.ai/oauth/claude-code-client-metadata` |
| Prefix in the sample | `https://chatgpt.com/oauth/codex/` | `https://claude.ai/oauth/` |
| Redirect URI (callback) | `http://127.0.0.1/callback`, a random port | `http://localhost/callback`, a random port |
| A 403 for `tasks.write` | Reports "Insufficient scope"; the user runs `codex mcp login --scopes tasks.read,tasks.write tracker` | Opens 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:

```mermaid
sequenceDiagram
    participant A as Agent (MCP client)
    participant M as MCP server
    participant B as User's browser
    participant S as Authorization server
    participant V as Vendor's site
    A->>M: First request without a token
    M-->>A: 401 with resource_metadata
    A->>M: Read protected resource metadata
    M-->>A: Resource URI and authorization server
    A->>S: Read authorization server metadata
    S-->>A: client_id_metadata_document_supported
    A->>B: Open the authorization request
    B->>S: client_id (document URL), redirect URI, scope, PKCE challenge, resource
    S->>V: Fetch the client's metadata document
    V-->>S: Client name and redirect URIs
    S-->>B: Sign-in and consent page
    B->>S: The user signs in and approves
    S-->>B: Redirect with the code and iss
    B->>A: Code
    A->>S: Token request with the code, PKCE verifier and resource
    S-->>A: Access token for the MCP server, refresh token
    A->>M: Tool call with the access token
    M-->>A: Result, 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](https://datatracker.ietf.org/doc/html/rfc9700#section-2.1) and [draft section 4.2](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-client-id-metadata-document-03#section-4.2) allow.

### A session with Codex

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

```text
$ 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:

```text
> 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](https://www.abblix.com/en/docs/securing-an-mcp-server) shows for the sample: [the resource and its scopes](https://www.abblix.com/en/docs/securing-an-mcp-server#declare-the-mcp-server-as-a-resource), [metadata documents and the prefixes](https://www.abblix.com/en/docs/securing-an-mcp-server#admit-mcp-clients-with-cimd), [a consent page](https://www.abblix.com/en/docs/securing-an-mcp-server#ask-the-user-on-a-consent-page), and at the MCP server [its metadata](https://www.abblix.com/en/docs/securing-an-mcp-server#publish-protected-resource-metadata), [token validation](https://www.abblix.com/en/docs/securing-an-mcp-server#validate-the-token-at-the-mcp-server) and [scopes](https://www.abblix.com/en/docs/securing-an-mcp-server#start-with-the-smallest-scope). [External identity providers](https://www.abblix.com/en/docs/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](https://www.abblix.com/en/docs/securing-an-mcp-server#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](https://www.abblix.com/en/docs/production-hardening-checklist) lists what a deployment needs beyond MCP.

## Troubleshooting

| What you see | Why | What to do |
| --- | --- | --- |
| The agent reports that the server does not support dynamic client registration | The authorization server's metadata does not announce `client_id_metadata_document_supported`, so the agent looks for a registration endpoint | Call `AddClientIdMetadataDocuments()` and list the agent's vendor in `AllowedClientIdPrefixes` |
| The authorization request is answered 400 `invalid_client` and the browser is not redirected | The `client_id` URL is outside `AllowedClientIdPrefixes`, or the document cannot be fetched or its `client_id` differs from its URL | Add the vendor's prefix with its trailing slash; the authorization server's log names the reason |
| The authorization request is answered 400 `invalid_request` | The redirect URI is not in the client's document | Use 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_target` | The `resource` the agent names is not in the tenant's `Resources` or not in `NewClientOptions.AllowedResources`, written exactly the same, trailing slash included | Declare the MCP server's resource URI as the same string in both |
| The MCP server answers 401 `invalid_token` to a fresh token | The 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 proxy | Configure 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_scope` | The token lacks the scope the tool needs | With 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:

- [Securing an MCP Server](https://www.abblix.com/en/docs/securing-an-mcp-server) explains each step these agents go through.
- The [MCP server sample README](https://github.com/Abblix/Oidc.Server.GettingStarted/tree/master/McpServerSample) shows how to install the agents and run the sample.
