DEV Community

Emil Hüseynov
Emil Hüseynov

Posted on

The Authorization Layer AI Agents Depend On

When an AI agent reads your email, queries a database, or files a support ticket, a specific sequence of validations must occur. Something must verify that the agent is permitted to hold the credential it is presenting, and that this credential was minted for the exact service it is talking to right now. In the Model Context Protocol (MCP), that decision layer relies on an OAuth 2.1 profile defined in the MCP authorization specification.

Adoption has grown rapidly. As of November 18, 2026, the official modelcontextprotocol/python-sdk repository has reached 24,891 GitHub stars, while typescript-sdk sits at 13,781, totaling over 38,000 stars combined. On December 9, 2025, Anthropic donated MCP to the Agentic AI Foundation, a Linux Foundation fund co-founded by Anthropic, Block, and OpenAI. Yet when auditing these implementations, I find that safety does not live inside any single HTTP message. It depends entirely on comparisons across messages.

How the Authorization Flow Works

To see why cross-message checks matter, it helps to trace the seven steps of a standard handshake in the 2026-07-28 MCP specification. Four parties take part: the MCP client running inside the agent host, the browser, the MCP server acting as an OAuth resource server, and the authorization server (AS).

  1. Discovery on rejection: The client sends an initial request to the MCP server without a token. The server responds with 401 Unauthorized and a WWW-Authenticate header pointing to its Protected Resource Metadata document (RFC 9728). Per the spec, "MCP servers MUST implement" this discovery mechanism.
  2. Metadata validation and issuer recording: The client fetches that metadata, selects an authorization server, and validates the AS metadata according to RFC 8414 Section 3.3. Before any browser redirection occurs, the spec requires that the client "MUST record the issuer value."
  3. Client registration: The client identifies itself to the AS. Per the 2026-07-28 revision, the order of precedence starts with pre-registered client metadata, followed by Client ID Metadata Document (CIMD) lookups, with dynamic client registration acting as the fallback route.
  4. Browser redirect with PKCE and resource binding: The client sends the browser to the authorization endpoint with a PKCE code challenge and a resource parameter (RFC 8707). Clients "MUST implement PKCE," "MUST use the S256 code challenge method," and must abort if the AS metadata omits code_challenge_methods_supported.
  5. Authorization response: The user approves the request, and the AS redirects back with an authorization code and an iss parameter (RFC 9207).
  6. Token exchange: The client sends a direct request to the token endpoint carrying the code, the original code_verifier, and the exact same resource URI. The client receives the access token in return.
  7. Authenticated tool calls and step-up: The client invokes tools on the MCP server using the token. If an action requires higher privileges, the server triggers a step-up flow using insufficient_scope.

Five Cross-Message Invariants

Inspecting any of those steps in isolation can make a flawed implementation look correct. Real security depends on state maintained across messages. Five invariants form the spine of the 2026-07-28 authorization specification.

1. Audience validation

  • The rule: An MCP server must verify that incoming access tokens were issued specifically for it.
  • What breaks without it: If Server B accepts valid tokens issued for Server A, a compromised Server A can replay a user's token against Server B to execute unauthorized tools.
  • Spec requirement: "MCP servers MUST validate that access tokens were issued specifically for them as the intended audience."

2. Issuer binding (mix-up protection)

  • The rule: When the authorization code returns in the redirect, the client must compare the returned iss parameter against the issuer URL it recorded during discovery using exact string matching.
  • What breaks without it: In an OAuth mix-up attack, a rogue authorization server can trick the client into sending a valid code to the wrong token endpoint.
  • Spec requirement: Before sending the code to any token endpoint, the client "MUST apply the validation in RFC 9207 Section 2.4." On the server side, returning iss is currently a SHOULD, though the spec notes that a future revision "is expected to upgrade ... from SHOULD to MUST." Clients that skip this check work fine in happy-path testing but fail silently against malicious AS instances.

3. Code binding (PKCE)

  • The rule: An authorization code can only be redeemed by presenting the exact code_verifier whose hash produced the initial challenge using S256.
  • What breaks without it: An intercepted redirect URL allows an attacker or rogue extension to race the legitimate client to the token endpoint and claim the session.
  • Spec requirement: Clients "MUST implement PKCE" and "MUST use the S256 code challenge method."

4. Resource consistency

  • The rule: The resource URI sent in the initial authorization redirect must match the resource URI sent during the token exchange.
  • What breaks without it: Discrepancies allow tokens requested for low-privilege endpoints to be redirected or used against sensitive internal servers.
  • Spec requirement: "MCP clients MUST include the resource parameter in authorization and token requests" (RFC 8707).

5. No token passthrough

  • The rule: An MCP server proxying requests downstream must never forward the client's access token directly to upstream APIs.
  • What breaks without it: Passthrough breaks audience boundaries, obscures audit trails, and hands downstream services credentials they were never meant to hold. The authorization spec points to the security best-practices document, which explains why "token passthrough is explicitly forbidden" and requires a separate token issued by the upstream authorization server.

None of these checks can be verified by reviewing a single HTTP message. A request header never tells you whether iss matches metadata retrieved seconds earlier, or whether an upstream bearer token matches the one the client provided.

Why Implementations Keep Breaking

Translating these rules into robust production codebases remains difficult for two main reasons.

First, the protocol spec has evolved at a fast pace. The repository history shows progression from 2024-11-05 to adding authorization in 2025-03-26, integrating RFC 9728 and RFC 8707 in 2025-06-18, introducing Client ID Metadata Documents in 2025-11-25, and reaching the 2026-07-28 revision. Teams building custom OAuth integrations early on often lag behind specification updates.

While auditing an open-source MCP OAuth provider, I found that a token minted for one resource server was accepted by another. The vendor's security team confirmed the gap. Even experienced developers can miss cross-message checks when connecting fast-moving specifications.

Second, a large portion of modern MCP glue code is generated by LLMs. When a model reviews an individual file or route handler in isolation, it evaluates syntax and signature validation while missing the stateful cross-message relationship required by the broader protocol.

How to Test Your MCP Authorization Layer

Ensuring your client and server correctly enforce these rules requires end-to-end integration tests rather than isolated unit tests.

The official conformance suite at github.com/modelcontextprotocol/conformance is active and provides a foundation for verifying standard discovery and metadata flows.

In addition to the conformance suite, adding dedicated negative tests to your CI pipeline catches regressions quickly:

  • Audience test: Mint a token with aud set to Server A, send it to Server B, and assert a 401 response.
  • Issuer mix-up test: Configure a mock AS to return a mismatched iss parameter in the redirect, and assert that the client aborts immediately without hitting any token endpoint.
  • PKCE test: Initiate an authorization flow with a valid challenge, then attempt token exchange with an incorrect code_verifier, expecting an invalid_grant error.
  • Resource consistency test: Use one resource=https://mcp-a.example.com parameter during authorization and a different resource=https://mcp-b.example.com one during token exchange, ensuring the AS rejects the mismatch.
  • Token passthrough test: Inspect outbound headers from your MCP server when handling client requests, ensuring the client's token never propagates to upstream services.

Pre-Ship Checklist

Before deploying an MCP integration to production, verify these items:

  • Audience: The server rejects tokens where the aud claim does not match its own canonical resource identifier.
  • Issuer binding: The client records the issuer before browser redirection and verifies it against the return parameter using exact string matching.
  • Code binding: The client enforces PKCE with S256 and rejects servers lacking code_challenge_methods_supported.
  • Resource consistency: The client sends identical resource parameters across authorization and token requests.
  • No passthrough: The server obtains independent upstream tokens instead of forwarding client bearer tokens.
  • CI coverage: Automated negative tests run for all five invariants in your test suite.

Building secure agentic workflows means accepting that isolated code reviews are not enough. Because MCP authorization relies on state spanning multiple independent protocol steps, any security posture built on single-message assumptions will eventually fail. Treat these invariants as non-negotiable architectural boundaries, bake them into your automated test suites, and audit your flows before handing agents keys to production systems.

Top comments (0)