D2 · Tool Design & MCP › Cluster 8 · MCP Security & Production

8.2 · Authentication & Authorization

OAuth 2.1 & scoped tokens

CONCEPT Remote MCP servers use OAuth 2.1

Authorization in MCP is defined at the transport level for HTTP-based transports, and it's optional — but when a remote server protects its resources, the spec says it SHOULD conform to the MCP authorization model, which is built on OAuth 2.1. (STDIO servers are the exception: they SHOULD NOT use this flow and instead read credentials from the environment.) The roles map cleanly onto OAuth:

MCP roleOAuth 2.1 roleResponsibility
MCP serverResource serverAccepts & validates access tokens on each request
MCP client (your host)ClientObtains tokens, sends Authorization: Bearer
Authorization serverAuthorization serverAuthenticates the user, issues tokens

The client doesn't need the auth server configured up front. The server advertises it: an unauthenticated request gets a 401 with a WWW-Authenticate header pointing at Protected Resource Metadata (RFC 9728), which names the authorization server. The client discovers the auth server's metadata (RFC 8414), runs the OAuth 2.1 flow with PKCE (MUST), and returns with a token.

🔑 Bearer tokens go in a header on every request: Authorization: Bearer <token> — never in the URL query string (MUST NOT), where they'd leak into logs and referrers. HTTPS is required for all auth endpoints.

CONCEPT Least privilege: scopes + audience binding

Two mechanisms keep a token from becoming a skeleton key.

Scoped, least-privilege access. The spec's own guidance warns against publishing every scope in scopes_supported and requesting them all up front — a broad token (files:*, admin:*) means a bigger blast radius if it leaks, noisier audit trails, and consent fatigue. Prefer a minimal initial scope and incrementally elevate (the server issues a WWW-Authenticate challenge naming the extra scope only when a privileged operation is first attempted). Servers may issue a subset of what was requested.

Token audience binding. This is the load-bearing rule. Clients MUST include the resource parameter (RFC 8707 Resource Indicators) in both authorization and token requests, naming the specific MCP server the token is for. Servers MUST validate that a presented token was issued for them — and MUST reject tokens whose audience doesn't include them. This stops a token minted for one service from being replayed against another.

⚠️ Token passthrough is explicitly forbidden. An MCP server that talks to an upstream API is a separate OAuth client to that API and MUST NOT forward the token it received from the MCP client. Passing tokens through breaks audience validation and enables the confused deputy (8.4).

DIAGRAM Discovery-driven OAuth 2.1 flow

sequenceDiagram participant C as MCP Client (host) participant M as MCP Server (resource server) participant A as Authorization Server C->>M: MCP request (no token) M-->>C: 401 + WWW-Authenticate → resource metadata C->>M: GET /.well-known/oauth-protected-resource M-->>C: metadata → authorization_servers C->>A: GET auth-server metadata (RFC 8414) Note over C,A: OAuth 2.1 + PKCE + resource= A-->>C: Access token (audience-bound) C->>M: MCP request + Authorization: Bearer M-->>C: Validate audience & scope → MCP response

CODE Sending a scoped, audience-bound token

The token request pins the resource (audience) and asks for the narrowest scope; every MCP request then carries the bearer token in the header.

# Token request — resource param binds the token to THIS server;
# scope is minimal, not the whole catalog.
POST /token HTTP/1.1
grant_type=authorization_code
code="..."
code_verifier="..."          # PKCE (required)
resource="https://mcp.example.com/mcp"   # RFC 8707 audience
scope="mcp:tools-basic"       # least privilege, elevate later

# Every MCP request — bearer in the HEADER, never the URL.
GET /mcp HTTP/1.1
Host: mcp.example.com
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...

# Server MUST: validate the token was issued for it (audience),
# return 401 on invalid/expired, 403 on insufficient scope.

TRADE-OFFS Broad token vs. scoped + audience-bound

✅ Minimal scope + audience binding

  • A leaked token unlocks little
  • Token can't be replayed across services
  • Cleaner audit trail per operation
  • Short-lived tokens + refresh rotation limit theft impact

⚠️ One broad, long-lived token

  • Fewer consent prompts up front
  • But: leak = full-scope compromise
  • Reusable across services if audience unchecked
  • Users decline dialogs listing excessive scopes

EXAM TRAPS

Trap: "MCP invented its own auth scheme." → No — remote MCP authorization is OAuth 2.1 (with PKCE, dynamic client registration, protected-resource & auth-server metadata). The MCP server is an OAuth resource server.
Trap: "Put the token in the URL for convenience." → Access tokens MUST NOT be in the query string — use Authorization: Bearer. URLs leak into logs and referrers.
Trap: "Any valid token the server receives is fine to accept." → Servers MUST validate the audience — reject tokens not issued for them. Accepting foreign-audience tokens breaks the OAuth trust boundary.
Trap: "An MCP server can reuse the client's token when calling an upstream API." → Token passthrough is forbidden. The server acts as its own OAuth client upstream with a separate token.

PRACTICE

Q1 · How does a client discover how to authenticate to a remote MCP server?

It makes a request; the server replies 401 with a WWW-Authenticate header pointing to its Protected Resource Metadata (RFC 9728), which names the authorization server. The client then fetches the auth server's metadata (RFC 8414) and runs the OAuth 2.1 flow with PKCE. No pre-configuration needed.

Q2 · What does the resource parameter do and why is it mandatory?

It's the RFC 8707 resource indicator that binds the token to a specific MCP server (its audience). Clients MUST send it on authorization and token requests; servers MUST validate that presented tokens were issued for them. This prevents a token minted for one service being replayed against another — the core defense against cross-service token misuse.

Q3 · Why prefer minimal scope over requesting everything up front?

Broad tokens expand the blast radius of a leak, obscure per-operation audit trails, and cause consent fatigue (users decline dialogs listing excessive scopes). Least-privilege scoping — a minimal initial scope with incremental elevation via WWW-Authenticate scope challenges — constrains compromise impact and keeps consent meaningful.