All posts
AI

MCP OAuth Explained

MCP OAuth Explained

When an AI agent connects to a remote MCP server, something has to prove the agent is allowed in and decide what it can do. In the Model Context Protocol, that something is OAuth. MCP OAuth is how clients like AI assistants, IDEs, and custom agents get access tokens for MCP servers without anyone pasting API keys into config files.

This guide explains MCP OAuth step by step: the roles involved, how discovery and client registration work, what each part of the flow protects against, and the mistakes that cause most MCP OAuth problems in practice. For the broader picture, including where OAuth stops and tool-level authorization starts, see our complete guide to MCP authentication and authorization.

What is MCP OAuth?

MCP OAuth is the authorization method the Model Context Protocol specification defines for remote MCP servers. It’s based on OAuth 2.1: an MCP client gets an access token from an authorization server on a user’s behalf, then sends that token to the MCP server with every request. The server checks that the token is valid and was issued for it before running any tool.

It applies to MCP servers reached over HTTP. Local servers that run over stdio on a user’s machine aren’t expected to use it. They take credentials from the environment instead.

The three roles in MCP OAuth

Role In MCP What it does
Resource server The MCP server Hosts the tools and checks tokens on every request
Client The MCP client inside an AI app or agent Gets tokens and sends them with each request
Authorization server Often your identity provider, or one run by the server’s vendor Signs users in, asks for consent, and issues tokens

The MCP server and the authorization server can be the same service or separate ones. Separating them is common in the enterprise, because it lets you use the identity provider you already run.

How the MCP OAuth flow works, step by step

1. The client tries the server and gets a 401

The client sends an MCP request with no token. The server answers 401 Unauthorized and a WWW-Authenticate header that points to its Protected Resource Metadata. It can also say which scopes the request needs.

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer resource_metadata="https://mcp.example.com/.well-known/oauth-protected-resource",
                         scope="tickets:read"

2. The client discovers the authorization server

The client reads the server’s Protected Resource Metadata (RFC 9728), which names the authorization server it trusts. Then it fetches that authorization server’s own metadata, using OAuth Authorization Server Metadata or OpenID Connect Discovery, to find its endpoints and what it supports.

This is what lets an AI app connect to an MCP server it has never seen before, with no manual setup.

3. The client identifies itself

Before it can ask for a token, the client needs a client ID. The current specification (2026-07-28) gives clients this order of preference:

  1. A pre-registered client ID, if the client already has one for this authorization server.
  2. A Client ID Metadata Document (CIMD). The client ID is an HTTPS URL that serves a small JSON file describing the client, such as its name and redirect URLs. The authorization server fetches it when it sees the URL.
  3. Dynamic Client Registration (DCR). The client registers itself at a registration endpoint. This is now deprecated and kept for backward compatibility.
  4. Asking the user to enter client details.

A Client ID Metadata Document looks like this:

{
  "client_id": "https://app.example.com/oauth/client-metadata.json",
  "client_name": "Example AI Assistant",
  "redirect_uris": ["http://127.0.0.1:3000/callback"],
  "grant_types": ["authorization_code"],
  "response_types": ["code"],
  "token_endpoint_auth_method": "none"
}

CIMD replaced DCR as the default because DCR let any client create registrations at will, which piled up stale records and gave authorization servers little to go on when deciding whether to trust a client. With CIMD, the client’s identity is tied to a domain it controls.

4. The user signs in and consents

The client opens a browser to the authorization server with:

  • PKCE, a one-time secret that proves the client finishing the flow is the one that started it
  • A resource parameter (RFC 8707) naming the exact MCP server the token is for
  • The scopes it needs, starting with the minimum

The user signs in, sees what the client is asking for, and approves.

5. The client checks where the response came from

The authorization server redirects back with a code and, ideally, an iss value naming itself. The client must compare that iss to the issuer it expected (RFC 9207) before using the code. This blocks mix-up attacks, where a code from one authorization server is sent to another.

6. The client exchanges the code for tokens

The client sends the code, the PKCE verifier, and the resource parameter to the token endpoint. It gets back an access token, and possibly a refresh token.

7. The client calls the MCP server with the token

Every MCP request carries the token in a header:

POST /mcp HTTP/1.1
Host: mcp.example.com
Authorization: Bearer eyJhbGciOiJSUzI1NiIs...

The server checks the token on every request, including that it was issued for this server. Tokens never go in the URL.

Scopes and step-up authorization

MCP OAuth is designed around asking for the least access first and more only when needed.

If a client’s token doesn’t cover an operation, the server answers 403 Forbidden with error="insufficient_scope" and the scopes required. The client then runs the flow again, asking for the new scopes plus the ones it already had, so it doesn’t lose access it still needs.

HTTP/1.1 403 Forbidden
WWW-Authenticate: Bearer error="insufficient_scope",
                         scope="tickets:write",
                         resource_metadata="https://mcp.example.com/.well-known/oauth-protected-resource"

This is how a user can approve read access on day one and only approve write access the first time an agent actually needs it.

Refresh tokens

Access tokens should be short-lived. For long-running agents, refresh tokens let the client get new access tokens without sending the user through the browser again. Under the specification, clients must keep refresh tokens confidential and can’t assume they’ll be issued. The authorization server decides.

MCP OAuth for enterprises: Enterprise-Managed Authorization

The standard flow asks each user to approve each MCP server. In a company with dozens of servers, that’s a lot of consent screens, and IT has little say in which apps connect to which servers.

Enterprise-Managed Authorization is an optional MCP authorization extension that addresses this. The user signs in to the AI app with the company’s identity provider. The app then asks the identity provider for an assertion that the user may access a specific MCP server, and trades that assertion for an access token at the server’s authorization server. IT sets the rules in the identity provider, and the user doesn’t see a separate consent screen for each server.

There’s also an authorization extension for machine-to-machine access using the OAuth client credentials grant, for agents and services that act on their own rather than for a user.

What MCP OAuth protects against

Threat What blocks it
Stolen authorization codes PKCE
A token for one server used at another The resource parameter and audience checks
Codes sent to the wrong authorization server Issuer (iss) validation
A server forwarding your token to other services The rule against token passthrough
Apps riding on another app’s consent Per-client consent for proxy servers (confused deputy protection)
Over-broad access Minimal starting scopes and step-up

Common MCP OAuth mistakes

  • Skipping audience checks. If the server accepts any valid token from the identity provider, a token meant for one server works at all of them.
  • Passing tokens through. An MCP server that forwards the user’s token to a downstream API breaks audit trails and widens the token’s reach. It should get its own token for that API.
  • Asking for every scope up front. It makes consent screens scary and gives agents more access than they need.
  • Building new clients on DCR. It still works, but it’s deprecated. Support Client ID Metadata Documents.
  • Long-lived tokens in config files. That’s API key sprawl with extra steps.
  • Treating OAuth as the whole answer. Scopes say what a token can reach. They don’t say which tools an agent may call, with which arguments.

Where an MCP gateway fits

At enterprise scale, an MCP gateway usually sits between clients and servers and splits OAuth into two hops:

  1. Client to gateway. Users and agents sign in once with corporate identity.
  2. Gateway to server. The gateway connects to each upstream server with that server’s own credentials, so agents never hold them and tokens aren’t passed through.

The gateway then adds the controls OAuth doesn’t cover: which tools each role can use, which argument values are allowed, and a log of every call.

How Airia handles MCP OAuth

Airia’s MCP Gateway is the control point for agent tool access in Airia Secure:

  • Connect once. Users connect an app through its OAuth sign-in flow, or with an API key for apps that use one, and the connection is saved to their account.
  • Approve before use. MCP servers aren’t usable until an administrator approves them.
  • Control beyond scopes. Access is set by role at the app, tool, and parameter level.
  • Log every call. Each tool call is recorded as succeeded, denied, or errored.
  • Add deterministic rules. Agent Constraints limit what agents can do, separate from the model’s judgment.

FAQ

Does MCP use OAuth 2.0 or OAuth 2.1? The MCP specification is based on OAuth 2.1, which builds on OAuth 2.0 and requires practices like PKCE.

Do all MCP servers need OAuth? No. Authorization is optional in the specification, and local stdio servers use credentials from the environment. Remote servers that touch real data should use it.

What is a Client ID Metadata Document in MCP? It’s a JSON file at an HTTPS URL that describes an MCP client. The URL itself is the client ID, and authorization servers fetch it to learn about the client. It’s now the preferred way for clients to register.

Is Dynamic Client Registration still supported in MCP? It still works for backward compatibility, but the 2026-07-28 specification deprecates it in favor of Client ID Metadata Documents.

Can AI agents use MCP OAuth without a user? Yes. An authorization extension covers the OAuth client credentials grant for machine-to-machine access, so agents and services can authenticate as themselves.

Make every MCP sign-in count toward the right access, and no more. Request a demo to see how Airia connects agents to MCP servers with role-based control over every tool call.

Put these ideas to work.

Schedule a 30-minute walkthrough with our team.

Talk through your use case