# mcp oauth 2.1 handshake lab

- Status: `experiment`
- Track: [agent discovery, auth & protocol bridges](https://aifoc.us/labs/agent-bridges-and-discovery/)
- Layer: 9. Agent Discovery, Auth & Protocol Bridges
- Updated: 2 December 2026
- Live demo: https://mcp-oauth-handshake-lab.kinlan-test.workers.dev
- Source: https://github.com/PaulKinlan/mcp-oauth-handshake-lab

## Inspired by

- [calling apis from the browser shouldn't mean exposing keys](https://aifoc.us/dangerous/)
- [webmcp is the new web intents ... maybe](https://aifoc.us/webmcp-is-the-new-web-intents/)
- [on-device ai needs a trust model](https://aifoc.us/on-device/)

## Why this matters

Connecting AI agents to remote [Model Context Protocol (MCP)](https://modelcontextprotocol.io/specification/2025-11-25/basic/authorization) servers without pasting long-lived API keys chains six specifications together: [RFC 9728 Protected Resource Metadata](https://www.rfc-editor.org/rfc/rfc9728), [RFC 8414](https://www.rfc-editor.org/rfc/rfc8414) server metadata, Dynamic Client Registration (DCR) vs. HTTPS Client ID Metadata Documents (CIMD), [OAuth 2.1 + PKCE (RFC 7636)](https://www.rfc-editor.org/rfc/rfc7636), [RFC 8707 Resource Indicators](https://www.rfc-editor.org/rfc/rfc8707), and refresh-token reuse revocation. Skipping a single check quietly opens cross-server token replay, clickjacked consent, or forged client names.

## What this opens up

Standard browser OAuth 2.1 + PKCE delegation for remote MCP tools—letting users grant scoped access to `/mcp/docs` or `/mcp/billing` with one approval click, zero copied API secrets, zero server-side client-registration bloat, and automatic token-family revocation if a refresh token is replayed.

## How the tests work

1. **Pick a client identity mode and target resource** (`/mcp/docs`, `/mcp/billing`, or `/mcp`) comparing Unsigned stateless DCR (`c_<b64url>`), HMAC-Signed DCR (`cs_<b64url>.<sig>`), and origin-verified HTTPS CIMD (`/cimd/valid-client.json`).
2. **Run the 6-step handshake** (either all at once or hop by hop) from unauthenticated `401 WWW-Authenticate` discovery through WebCrypto `S256` PKCE authorization, single-use code exchange, and an authorized MCP `tools/call`.
3. **Fire all 5 live red-team attacks** to test spent refresh-token family revocation, bad-PKCE burn-before-check code destruction, RFC 8707 cross-resource token replay (`/mcp/docs` -> `/mcp/billing`), opaque-iframe cross-origin DNS rebinding (`403`), and unsigned DCR name spoofing vs. CIMD validation.
4. **Inspect the Live HTTP Wire Trace and `OAuthLedger` SQLite Durable Object** to verify every header, CSP `frame-ancestors 'none'` consent lock, and hashed token-family state transition.


## Hypothesis

- **Claim:** Combining RFC 9728 path-scoped discovery, WebCrypto PKCE `S256` with burn-before-check codes, RFC 8707 `resource` audience binding (`/mcp/docs` vs. `/mcp/billing`), `frame-ancestors 'none'` consent CSP, and SQLite refresh-token family revocation prevents cross-resource replay, code interception, and clickjacked consent—while origin-verified CIMD closes unsigned DCR consent-name spoofing.
- **Confirms it:** Authorized handshakes complete all 6 steps cleanly against `OAuthLedger`, while replaying a `/mcp/docs` Bearer token on `/mcp/billing` fails with `401 RFC8707_RESOURCE_MISMATCH`, replaying a rotated refresh token revokes the entire token family in SQLite, a bad PKCE verifier burns the code before retry, spoofed CIMD fails validation, and opaque-origin DNS-rebinding POSTs fail with `403`.
- **Bounds or disproves it:** Stateless unsigned DCR (`c_<b64url>`) eliminates client-registration database rows at the cost of allowing any caller to forge an arbitrary `client_name` offline, and full OAuth redirect flows add latency and consent friction for short-lived agent tool sessions.


## Under the hood

[MCP OAuth 2.1 Handshake Lab](https://mcp-oauth-handshake-lab.kinlan-test.workers.dev) ([GitHub](https://github.com/PaulKinlan/mcp-oauth-handshake-lab)) executes the MCP OAuth 2.1 (`2025-11-25`) handshake end to end inside a Cloudflare Worker (`src/router.ts`, `src/oauth-core.ts`) backed by a SQLite Durable Object (`OAuthLedger`) that stores only SHA-256 digests of codes, access tokens, and refresh tokens:

- **Discovery & Client Identity** — Serves [RFC 9728](https://www.rfc-editor.org/rfc/rfc9728) `/.well-known/oauth-protected-resource` (including path-insertion routes for `/mcp/docs` and `/mcp/billing`), [RFC 8414](https://www.rfc-editor.org/rfc/rfc8414) `/.well-known/oauth-authorization-server`, valid vs. spoofed Client ID Metadata Documents (`/cimd/valid-client.json`, `/cimd/spoofed-client.json`), and `POST /oauth/register` comparing unsigned DCR (`c_<b64url>`), HMAC-signed DCR (`cs_<b64url>.<sig>`), and HTTPS CIMD URL matching.
- **Consent, PKCE & Token Family Lifecycle** — Serves `/oauth/authorize` with `frame-ancestors 'none'` and same-origin CSRF checks, verifies WebCrypto `S256` PKCE after atomically burning the authorization code in SQLite (preventing brute-force retries), binds tokens to their [RFC 8707](https://www.rfc-editor.org/rfc/rfc8707) `resource` audience, and revokes the entire `token_families` lineage whenever a spent refresh token is replayed.
- **Protected MCP Execution & DNS-Rebinding Defense** — Checks `Origin`/`Host` (`403` tested live from `<iframe sandbox="allow-scripts" src="/probe/rebind-frame">`), emits `401` with `WWW-Authenticate: Bearer resource_metadata="..."`, and enforces audience-scoped JSON-RPC 2.0 `tools/call` across `/mcp`, `/mcp/docs`, and `/mcp/billing`.


## Key terms & further reading

- [MCP Authorization (2025-11-25)](https://modelcontextprotocol.io/specification/2025-11-25/basic/authorization) — Model Context Protocol specification defining OAuth 2.1 discovery, client identity, and resource-scoped access for remote HTTP MCP servers.
- [RFC 9728 Protected Resource Metadata](https://www.rfc-editor.org/rfc/rfc9728) — Standard `/.well-known/oauth-protected-resource` document pointed to by `WWW-Authenticate` so clients discover the right authorization server per MCP path.
- [RFC 8414 Authorization Server Metadata](https://www.rfc-editor.org/rfc/rfc8414) — Discovery document advertising authorize, token, and registration routes plus supported PKCE challenge methods (`S256`).
- [OAuth 2.1 + PKCE (RFC 7636)](https://www.rfc-editor.org/rfc/rfc7636) — Proof Key for Code Exchange using a WebCrypto SHA-256 challenge so an intercepted authorization code cannot be redeemed without the original verifier.
- [RFC 8707 Resource Indicators](https://www.rfc-editor.org/rfc/rfc8707) — OAuth extension binding access tokens to a single target audience URL so a `/mcp/docs` token cannot be replayed against `/mcp/billing`.
- [Dynamic Client Registration (RFC 7591) & CIMD](https://www.rfc-editor.org/rfc/rfc7591) — Dynamic registration lets agents mint client IDs on the fly, whereas Client ID Metadata Documents tie `client_id` directly to a verifiable HTTPS JSON document.

## Open questions to iterate on

- How quickly can MCP clients migrate from open Dynamic Client Registration to origin-verified Client ID Metadata Documents (CIMD)?
- Where should consent boundaries sit when one host orchestrates calls across five separate OAuth-protected MCP servers?

## Iteration log

- **2026-12-02:** Deployed full MCP OAuth 2.1 (`2025-11-25`) Worker + SQLite Durable Object (`OAuthLedger`) implementing RFC 9728 path-insertion metadata, RFC 8414 discovery, unsigned/HMAC-signed DCR + valid/spoofed CIMD fixtures, `frame-ancestors 'none'` consent CSP, WebCrypto `S256` PKCE burn-before-check codes, RFC 8707 audience checks across `/mcp/docs` and `/mcp/billing`, refresh-token family revocation, and `Origin`/`Host` DNS-rebinding guards.

## Primitives tested

- `OAuth 2.1 (PKCE S256)`
- `RFC 9728 Protected Resource Metadata`
- `RFC 8414 Authorization Server Metadata`
- `RFC 8707 Resource Indicators`
- `DCR & Client ID Metadata Documents (CIMD)`
- `Durable Object SQLite (OAuthLedger)`

## Connected labs in other tracks

- [custodian narrow mcp gateway](https://aifoc.us/labs/delegated-authority/custodian-narrow-mcp-gateway/) — Tests narrow 4-tool MCP custody with dual ECDSA P-256 proofs instead of bearer OAuth tokens.
- [/agents.md vs. mcp onboarding lab](https://aifoc.us/labs/agent-bridges-and-discovery/agents-md-vs-mcp-onboarding-lab/) — Compares MCP transport handshake overhead and live LLM accuracy against zero-install `/agents.md` discovery.
- [agent well-known audit lab](https://aifoc.us/labs/agent-bridges-and-discovery/agent-well-known-audit-lab/) — Audits `/.well-known/oauth-protected-resource` alongside 11 other well-known agent endpoints.

---
Canonical: https://aifoc.us/labs/agent-bridges-and-discovery/mcp-oauth-handshake-lab/
