Skip to content

MCP OAuth 2.1: How the Flow Works and Where Tokens Still Leak

MCP OAuth 2.1

A remote Model Context Protocol (MCP) server can pass every check in the authorization spec and still give away the credential worth stealing. It answers anonymous calls with an HTTP 401 and rejects any token not minted for its own URL, yet at startup it reads an organization-wide GitHub token with write scope from an environment variable. The OAuth flow never touches that token, so nothing in the flow would notice it leaving.

MCP OAuth is how an AI application gets a user’s permission to call a remote MCP server, which exposes tools to agents. The MCP server is an OAuth 2.1 resource server, and the client gets its token from the authorization server named in the server’s Protected Resource Metadata.

That flow protects one hop, client to server, and the last two revisions hardened it. The leaks sit behind it, where the flow cannot see: servers that forward tokens never meant for them, proxies that let a cached consent stand in for the user, and the long-lived credential a server uses to reach GitHub or a database. Get the flow right, then secure what sits behind it, starting with whichever server’s upstream credential could do the most damage.

How Does the MCP Authorization Flow Work?

The MCP authorization flow is an OAuth 2.1 authorization code grant with PKCE, preceded by two rounds of discovery so that a client can find a new server’s authorization server unaided.

Sequence diagram of the five-step MCP authorization flow

  1. The client calls without a token and gets an HTTP 401 whose WWW-Authenticate header carries a resource_metadata URL, or falls back to the server’s well-known URI.
  2. It fetches that Protected Resource Metadata document, defined in RFC 9728, and reads authorization_servers.
  3. It loads the authorization server’s metadata through RFC 8414 or OpenID Connect Discovery, supporting both, and stops if code_challenge_methods_supported is missing, its only proof of PKCE support.
  4. With a client_id (pre-registered, a Client ID Metadata Document URL, or deprecated Dynamic Client Registration), the client sends the user to the authorization endpoint with an S256 PKCE challenge and a resource parameter (RFC 8707) naming the server’s canonical URI. PKCE makes an intercepted code useless without the client’s verifier, and the resource value asks for a token bound to this server. The code returns with iss when the authorization server supports RFC 9207, and the client checks it against the issuer it recorded.
  5. The client redeems the code with its code_verifier and the same resource value. The token then rides in an Authorization: Bearer header on every request, never in a query string, which servers log and browsers keep in history.

A short Protected Resource Metadata document looks like this:

{
  "resource": "https://mcp.example.com/mcp",
  "authorization_servers": ["https://auth.example.com"],
  "scopes_supported": ["files:read"],
  "bearer_methods_supported": ["header"]
}

The server itself must reject any token whose audience is not its canonical URI. RFC 8707 only asks authorization servers to restrict audience, so when one ignores the resource parameter, that server-side check is the only binding left.

Recent MCP Specification Updates and Auth Changes

Three parts of that flow changed in the two releases after 2025-06-18, as the 2025-11-25 and 2026-07-28 changelogs record:

AreaBefore (2025-06-18)Now
RegistrationDynamic Client Registration first, a hardcoded client ID or user entry as fallbacksPre-registration, then Client ID Metadata Documents, then Dynamic Client Registration, then the user (2025-11-25). Dynamic Client Registration deprecated (2026-07-28)
ScopesNo step-up flowHTTP 403 with error=”insufficient_scope” names the scopes needed (2025-11-25)
IssuerNo iss requirementClients verify a returned iss before redeeming the code and key client credentials by issuer (2026-07-28)

The iss check stops the mix-up attacks RFC 9207 describes, a risk for any client that talks to several authorization servers, as MCP clients do. PKCE alone cannot, since the client sends its code_verifier to whichever token endpoint it trusts. Step-up lets a token start with minimal scopes instead of everything up front.

Check the Revision Date
Before copying an MCP OAuth sample, find the specification revision it targets. Code older than 2025-11-25 knows nothing of metadata documents or step-up scopes and treats Dynamic Client Registration as the default, and code older than 2026-07-28 skips the iss check.

Where Do Tokens Still Leak?

Tokens still leak after an MCP OAuth 2.1 flow has done its job, inside and behind the server, out of the client’s sight.

Token Passthrough

Forwarding the client’s bearer token to the API your server wraps saves you a second OAuth integration, and the MCP Security Best Practices forbid it: “MCP servers MUST NOT accept any tokens that were not explicitly issued for the MCP server.” The page names four risks:

  • Security Control Circumvention: rate limits and monitoring keyed to the token’s audience stop applying.
  • Accountability and Audit Trail Issues: upstream logs record the wrong caller.
  • Trust Boundary Issues: one token that several services accept opens all of them.
  • Future Compatibility Risk: controls are hard to add later without audience separation.

The Confused Deputy in Proxy Servers

A proxy that fronts a third-party API as one OAuth client is exposed when four conditions hold: a static client ID, MCP clients that register their own client_id, a third-party authorization server that remembers consent in a cookie, and no per-client consent on the proxy. A link carrying a fresh client and the attacker’s redirect URI then rides the cookie past the consent screen, and the code goes to the attacker. The fix is a per-user registry of approved client IDs, checked before any third-party authorization. Metadata documents do not retire it, since a proxy that accepts any Client ID Metadata Document URL faces the same attack.

The Credential Underneath the Token

The server reaches GitHub or a database with its own credential, which never enters the flow. The spec requires a separate upstream token and secure token storage and leaves the rest to the deployer, so nothing stops that credential from being one of the static API keys in an environment variable, valid long after any access token expires. Akeyless’s MCP server secrets guide covers replacing it with workload identity.

What Does This Mean for STDIO Servers?

On a STDIO (standard input/output) server, the credential underneath is the only one. The spec sends STDIO implementations to the environment, and your client starts the server as a subprocess, passing whatever keys you list in its config. Trend Micro researchers found that 9,294 of 19,402 MCP server code bases, 48%, recommend a .env file for secrets. That is the .env pattern, with every key readable by any process running as the same user.

The fix is a server that never holds the target credential. Akeyless’s walkthrough on brokering credentials at runtime shows a first step: the server fetches its Gemini API key from Akeyless once at startup, so the key leaves the config file but still sits in memory. The Akeyless MCP server goes further. Its akeyless mcp-runtime-authority command lets an agent call tools such as query-db while the Gateway runs each call with just-in-time credentials and returns only results, and Akeyless’s Codex integration is an MCP server without stored keys built the same way. Each still needs one credential to reach Akeyless, the classic secret zero, which a cloud identity can fill.

SecretlessAI, for agents that never hold a secret, applies that model to every connection: the Akeyless Gateway uses the target credential inside the brokered session, so it never reaches the agent’s prompts or memory. Akeyless Agentic Runtime Authority makes the check no access token can: whether each call fits the prompt behind it, with destructive or out-of-scope requests stopped before they run and every action traced to the person who started it. The cost is an extra hop on every call.

An Implementation Checklist

Run these against every HTTP MCP server and proxy you operate:

  • Reject tokens whose audience is not the server’s canonical URI, and test with one minted elsewhere.
  • Send the resource parameter every time, so authorization servers that honor it bind the token.
  • Use pre-registration or Client ID Metadata Documents, with Dynamic Client Registration only as a fallback.
  • Store the issuer with the PKCE verifier and compare any returned iss before redeeming the code, or a mix-up sends the code to the wrong token endpoint.
  • Call upstream APIs with the server’s own token, never the client’s.
  • On a proxy, check approved client IDs per user before any third-party authorization.
  • Keep long-lived upstream credentials off the host.
  • Log the client ID and user behind each request.

A shared layer can run several of these, and Akeyless’s post on where MCP gateways fit sets it beside runtime enforcement.

After the MCP OAuth Flow Passes, Fix the Credentials Behind It

Call the MCP OAuth flow done when two tests pass: a token minted for another resource gets HTTP 401, and an authorization response with the wrong iss is refused before redemption. Then list every credential each server reads at startup and sort the list by two things you can see: whether a credential expires and whether it can write. An organization-wide GitHub token with write scope and no expiry goes to the top, and the MCP access token, which already expires on schedule, goes to the bottom. Broker the top entry before anything else, then confirm that any proxy with a static client ID checks consent per client.

FAQs About MCP OAuth

Does MCP require OAuth 2.1?

No. Authorization is optional in MCP. HTTP servers that add it are expected to follow the MCP authorization specification, whose authorization servers must implement OAuth 2.1, still an IETF draft.

Is Dynamic Client Registration still supported in MCP?

Yes, for backward compatibility. Revision 2026-07-28 deprecates it in favor of Client ID Metadata Documents but still permits it, and the deprecated features registry rules out removal before a revision released on or after 2027-07-28.

What is token passthrough and why does the MCP spec forbid it?

Token passthrough is an MCP server accepting a token not issued for it and forwarding it unchanged to an upstream API. The spec forbids it because the upstream service then sees the wrong caller and one stolen token works everywhere it is accepted.

Do STDIO MCP servers need OAuth?

No. The specification tells STDIO implementations not to follow its authorization flow and to read credentials from the environment instead. A STDIO server is a local subprocess with no HTTP hop for OAuth to protect, so the exposure moves to whatever keys that environment holds.

How does an autonomous AI agent authenticate to an MCP server with no user present?

With the OAuth client credentials grant, defined for MCP in the draft OAuth Client Credentials extension. The client authenticates as itself, ideally with a JWT signed by its private key (RFC 7523), and gets a token with no browser step. In this machine-to-machine access, the signing key is the secret to protect.

Related Resources

View all
Illustration of a secretless AI agent using Akeyless runtime authority
Blog How We Put Zero-Secret AI Agents to Work

Summary: Secretless access and runtime authorization are important concepts in AI agent security, but what do they look like in practice? At Akeyless, we…

November 15, 2026
  • G2 Fall 2026 Leader — Non-Human Identity Management
  • G2 Fall 2026 Momentum Leader — Privileged Access Management
  • G2 Fall 2026 High Performer — Certificate Lifecycle Management
  • G2 Fall 2026 Easiest To Do Business With — Secrets Management
  • G2 Fall 2026 Easiest To Use — Privileged Access Management, Enterprise
  • G2 Fall 2026 Best Support — Privileged Access Management, Enterprise