Skip to content
mentoor

Topic 2 of 5 · 5 min read

The MCP OAuth 2.1 authorization flow

Goal: Put the steps from a 401 response to an authorized tool call in order and identify the step a broken client skipped.

A remote MCP server is an OAuth 2.1 resource server: the client learns where to log in from the server itself, gets a token bound to that server, and sends it on every request. The server never needs to know the client in advance.

Check yourself · 1 of 3

A client calls a protected MCP server with no token. What does the server send back to start discovery?

Choose one answer

Why it matters

Without a standard flow, every client would need hand-made setup for every server. MCP reuses OAuth so that a client it has never seen can connect, the user approves once, and the server only has to check tokens. When the flow breaks, it usually breaks at one step, and the error message names a different one.

The steps, in order

  1. Call without a token. The server answers 401 with WWW-Authenticate: Bearer resource_metadata="https://mcp.example.com/.well-known/oauth-protected-resource" and may add scope.
  2. Read the protected resource metadata (RFC 9728). Its authorization_servers field lists at least one authorization server. If the header is missing, the client falls back to the well-known URLs.
  3. Read the authorization server metadata (RFC 8414 or OpenID Connect discovery). Confirm PKCE is supported.
  4. Get a client ID. In priority order: pre-registered credentials, a Client ID Metadata Document, dynamic registration, or ask the user. A Client ID Metadata Document is an HTTPS URL used as the client_id that points to a JSON file describing the client. Dynamic client registration is deprecated as of 2026-07-28.
  5. Send the user to log in. The request uses PKCE with the S256 method and a resource parameter set to the server's canonical URI, such as https://mcp.example.com. The client records the issuer and checks the iss value that comes back.
  6. Exchange the code at the token endpoint with the PKCE verifier and the same resource.
  7. Call again with Authorization: Bearer <token> on every request. Never put the token in the URL.

Before and after

A client that fails after login:

GET /authorize?client_id=app&code_challenge=...&scope=files:read

It sent no resource, so the token is not bound to the MCP server and the server rejects it for the wrong audience. The corrected request adds &resource=https%3A%2F%2Fmcp.example.com.

What goes wrong

  • The client skips step 2 and guesses the login URL from the server's host name.
  • A new build relies on dynamic registration, which is kept only for older authorization servers.
  • The client accepts an authorization response whose iss differs from the issuer it recorded.

Examples

Client ID Metadata Document

A desktop client hosts https://app.example.com/oauth/client-metadata.json containing client_id (the same URL), client_name and redirect_uris. The authorization server fetches it when it sees that URL as a client_id, checks that the redirect URI matches, and no registration call is needed. The server advertises support with client_id_metadata_document_supported: true.

Reading the failure

A tool call returns 401 after a successful login. The token request in the log has no resource parameter. The token was issued without the MCP server as its audience, so the server correctly refuses it. The broken step is the resource parameter, not the login.

Key points

  • The MCP server is an OAuth 2.1 resource server; it points clients to its authorization server through protected resource metadata.
  • The first call without a token gets 401 with a WWW-Authenticate header that carries resource_metadata and, optionally, scope.
  • Clients get a client ID by priority: pre-registered, Client ID Metadata Document, dynamic registration, then user entry.
  • Authorization requests use PKCE with S256 and a resource parameter, and token requests repeat the resource.
  • Bearer tokens go in the Authorization header of every request and never in the query string.

Common mistakes

  • Hard-coding the authorization server instead of reading protected resource metadata.
  • Leaving out the resource parameter because the authorization server does not require it; the client must send it anyway.
  • Sending the access token as a query parameter in the MCP URL.
  • Treating dynamic client registration as the default for new clients.

Check yourself · 2 of 3

Which client registration method should a client with no prior relationship to the server prefer when the authorization server supports it?

Choose one answer

Check yourself · 3 of 3

A client logs in successfully but the MCP server rejects the token. The token request had PKCE but no resource parameter. What is the likely cause?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic