---
title: "MCP authorization step by step: OAuth 2.1, discovery and PKCE"
type: guide
url: https://agentsfordevs.com/guides/mcp-oauth-flow
markdown: https://agentsfordevs.com/guides/mcp-oauth-flow.md
publisher: Agents for Devs
author: Agents for Devs desk
reviewed_by: Agents for Devs desk
published: 2026-12-06
updated: 2026-12-06
license: https://agentsfordevs.com/reuse
citation: 'Agents for Devs. "MCP authorization step by step: OAuth 2.1, discovery and PKCE." Published Dec 6, 2026. https://agentsfordevs.com/guides/mcp-oauth-flow'
attribution: Credit Agents for Devs and link to https://agentsfordevs.com/guides/mcp-oauth-flow
format: Guide
topics:
  - auth
description: "The MCP OAuth flow in spec 2026-07-28: the 401 challenge, protected resource metadata, client registration, PKCE, the resource parameter and iss checks."
---

# MCP authorization step by step: OAuth 2.1, discovery and PKCE

Every request an MCP client makes to get a token for a protected remote MCP server, in order, per the 2026-07-28 specification.

## Short answer

MCP authorization is OAuth 2.1 for remote MCP servers over HTTP. In the 2026-07-28 specification, the client sends a request without a token and gets a `401` whose `WWW-Authenticate` header points to the server's protected resource metadata (RFC 9728). It reads `authorization_servers`, fetches that authorization server's RFC 8414 or OpenID Connect metadata, and obtains a client ID. It then runs the authorization code flow with PKCE (`S256`) and sends a `resource` parameter (RFC 8707) naming the MCP server the token is for.

**Checked against:** [MCP Authorization](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization), [Authorization Server Discovery](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/authorization-server-discovery), [Client Registration](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/client-registration) and [Authorization Security Considerations](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/security-considerations), December 6, 2026.

**Run by us:** no. Requests and responses are copied from the MCP specification.

## Does my MCP server need OAuth at all?

An MCP server needs this flow only if it is protected and reached over HTTP.

| Transport | What the spec says |
|---|---|
| HTTP-based (for example [Streamable HTTP](/glossary/streamable-http)) | Implementations SHOULD conform to the authorization spec |
| stdio | SHOULD NOT follow it; retrieve credentials from the environment |
| Other transports | MUST follow established security practice for that protocol |

Authorization is optional in MCP. When a server uses it, the MCP server is an OAuth 2.1 resource server, the [MCP client](/glossary/mcp-client) is an OAuth 2.1 client, and an authorization server signs the user in and issues tokens. The authorization server can be hosted with the MCP server or run separately.

This guide follows protocol revision 2026-07-28, which the [versioning page](https://modelcontextprotocol.io/specification/versioning) lists as current as of December 6, 2026. The spec builds on [OAuth 2.1 draft 13](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-13).

## What comes back on the first request?

The server answers an unauthenticated request with `401 Unauthorized` and a `WWW-Authenticate` header that points to its protected resource metadata.

```http
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer resource_metadata="https://mcp.example.com/.well-known/oauth-protected-resource",
                         scope="files:read"
```

MCP servers MUST implement [RFC 9728](https://datatracker.ietf.org/doc/html/rfc9728) Protected Resource Metadata. They advertise its location in the header, at a well-known URI, or both. Clients use the header's `resource_metadata` URL when present. Otherwise they try two well-known URIs in order ([discovery spec](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/authorization-server-discovery)):

1. At the endpoint's path: `https://example.com/public/mcp` maps to `https://example.com/.well-known/oauth-protected-resource/public/mcp`
2. At the root: `https://example.com/.well-known/oauth-protected-resource`

The metadata document MUST list at least one entry in `authorization_servers`. If it lists several, the client picks one, and it MUST keep separate credentials and tokens for each authorization server.

## How does the client find the authorization server's endpoints?

The client builds well-known URLs from the authorization server's issuer and tries them in a fixed order.

| Issuer | Try, in order |
|---|---|
| With a path, e.g. `https://auth.example.com/tenant1` | `/.well-known/oauth-authorization-server/tenant1`, then `/.well-known/openid-configuration/tenant1`, then `/tenant1/.well-known/openid-configuration` |
| Without a path, e.g. `https://auth.example.com` | `/.well-known/oauth-authorization-server`, then `/.well-known/openid-configuration` |

Authorization servers MUST offer [RFC 8414](https://datatracker.ietf.org/doc/html/rfc8414) metadata or OpenID Connect Discovery 1.0, and clients MUST support both. The `issuer` in the returned document MUST be identical to the issuer used to build the URL. A document fetched from `https://attacker.example` that claims `"issuer": "https://honest.example"` is rejected.

The client also checks for PKCE before going further. If `code_challenge_methods_supported` is missing from the metadata, the client MUST refuse to proceed ([security considerations](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/security-considerations)). OpenID providers that serve MCP clients MUST include that field.

## How does an MCP client get a client ID?

A client gets its `client_id` in one of three ways, and the spec gives a priority order ([client registration](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/client-registration)).

| Priority | Mechanism | Signal in authorization server metadata | Status in 2026-07-28 |
|---|---|---|---|
| 1 | Pre-registered client ID (and secret, if any) | None; configured ahead of time | SHOULD support |
| 2 | Client ID Metadata Document | `client_id_metadata_document_supported: true` | SHOULD support |
| 3 | [Dynamic Client Registration](/glossary/dynamic-client-registration) (RFC 7591) | `registration_endpoint` | MAY support; deprecated |
| 4 | Ask the user to enter client details | None | Last resort |

With a Client ID Metadata Document, the `client_id` is an HTTPS URL with a path, and the authorization server fetches the JSON at that URL. The document MUST contain at least `client_id`, `client_name` and `redirect_uris`, and its `client_id` MUST match the URL exactly. The spec's example:

```json
{
  "client_id": "https://app.example.com/oauth/client-metadata.json",
  "client_name": "Example MCP Client",
  "client_uri": "https://app.example.com",
  "logo_uri": "https://app.example.com/logo.png",
  "redirect_uris": [
    "http://127.0.0.1:3000/callback",
    "http://localhost:3000/callback"
  ],
  "grant_types": ["authorization_code"],
  "response_types": ["code"],
  "token_endpoint_auth_method": "none"
}
```

Dynamic Client Registration was deprecated in 2026-07-28 ([PR #2858](https://github.com/modelcontextprotocol/modelcontextprotocol/pull/2858)) and stays in the spec for at least twelve months. Clients that still use it MUST send an `application_type`: `"native"` for desktop apps, CLI tools and `localhost` apps, `"web"` for remote browser apps.

Pre-registered and dynamically registered credentials are bound to the issuing authorization server's `issuer`. If the protected resource metadata names a different authorization server, the client MUST re-register. Metadata-document client IDs are portable, so they need no re-registration.

## What goes into the authorization and token requests?

The client sends a PKCE challenge, the selected scopes and a `resource` parameter naming the MCP server, then repeats `resource` with the `code_verifier` at the token endpoint.

```text
&resource=https%3A%2F%2Fmcp.example.com
```

| Parameter | Rule |
|---|---|
| `code_challenge` | MUST use PKCE; MUST use `S256` when technically capable |
| `resource` | MUST appear in both the authorization and token requests, set to the server's canonical URI, even if the authorization server ignores it ([RFC 8707](https://www.rfc-editor.org/rfc/rfc8707.html)) |
| `scope` | Use the `scope` from the 401 challenge; if absent, request all of `scopes_supported`; if that is undefined, omit `scope` |
| `state` | SHOULD use and verify; discard responses with a missing or mismatched value |

A canonical URI has a scheme and no fragment. `https://mcp.example.com/mcp` and `https://mcp.example.com:8443` are valid. `mcp.example.com` and `https://mcp.example.com#fragment` are not. The spec recommends omitting a trailing slash unless it matters to the resource.

Before redirecting the browser, the client MUST record the authorization server's validated `issuer` alongside the PKCE verifier. Redirect URIs MUST be `localhost` or HTTPS, and every authorization server endpoint MUST use HTTPS.

## How does the client check the callback before redeeming the code?

The client compares the `iss` value in the authorization response with the issuer it recorded, as defined in [RFC 9207](https://datatracker.ietf.org/doc/html/rfc9207), before sending the code to any token endpoint.

| `authorization_response_iss_parameter_supported` | `iss` in response | Client action |
|---|---|---|
| `true` | present | Compare to the recorded issuer |
| `true` | absent | Reject the response |
| `false` or absent | present | Compare to the recorded issuer |
| `false` or absent | absent | Proceed |

The comparison is a simple string comparison with no case folding, port or trailing-slash normalization. This defends against mix-up attacks, where a malicious authorization server tries to collect a code issued by an honest one. Authorization servers SHOULD send `iss` today, and the spec expects a future revision to make that a MUST.

## How is the token sent and checked?

The client sends the token in the `Authorization` header on every HTTP request, never in the query string.

```http
POST /mcp HTTP/1.1
Host: mcp.example.com
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
Content-Type: application/json
Accept: application/json, text/event-stream
MCP-Protocol-Version: 2026-07-28
Mcp-Method: tools/list
```

These are the headers of the spec's example request, which continues with a JSON-RPC `tools/list` body. The MCP server MUST validate that the token was issued for it as the audience. Invalid or expired tokens get a `401`. The server "MUST NOT accept or transit any other tokens," and if it calls an upstream API it uses a separate token from that API's authorization server. See [token passthrough](/guides/token-passthrough) for why.

## What happens when a token lacks a scope?

The server SHOULD answer `403 Forbidden` with `error="insufficient_scope"` and the scopes the operation needs.

```http
HTTP/1.1 403 Forbidden
WWW-Authenticate: Bearer error="insufficient_scope",
                         scope="files:write",
                         resource_metadata="https://mcp.example.com/.well-known/oauth-protected-resource",
                         error_description="File write permission required for this operation"
```

A client acting for a user SHOULD re-authorize with the union of its earlier scopes and the challenged ones, then retry only a few times before treating the failure as permanent. Servers SHOULD put every scope an operation needs into one challenge rather than asking for them one at a time.

## What changed for authorization in 2026-07-28?

Four authorization changes landed in the [2026-07-28 changelog](https://modelcontextprotocol.io/specification/2026-07-28/changelog):

- Dynamic Client Registration is deprecated in favor of Client ID Metadata Documents (PR #2858).
- Authorization servers SHOULD send `iss`, and clients MUST validate it when present (SEP-2468).
- Clients MUST set `application_type` during Dynamic Client Registration (SEP-837).
- Client credentials are keyed to the issuing authorization server (SEP-2352).

## What to read next

- [Token passthrough: why MCP servers must not forward client tokens](/guides/token-passthrough)
- [Run a remote MCP server: Streamable HTTP, sessions and auth](/guides/deploy-remote-mcp-server)
- [OAuth 2.1](/glossary/oauth-2-1)
- [Dynamic Client Registration](/glossary/dynamic-client-registration)

## Sources

1. [Authorization (specification 2026-07-28), Model Context Protocol](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization)
2. [Authorization Server Discovery (specification 2026-07-28), Model Context Protocol](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/authorization-server-discovery)
3. [Client Registration (specification 2026-07-28), Model Context Protocol](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/client-registration)
4. [Authorization Security Considerations (specification 2026-07-28), Model Context Protocol](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization/security-considerations)
5. [Key Changes (specification 2026-07-28), Model Context Protocol](https://modelcontextprotocol.io/specification/2026-07-28/changelog)
6. [Versioning, Model Context Protocol](https://modelcontextprotocol.io/specification/versioning)
7. [RFC 9728: OAuth 2.0 Protected Resource Metadata, IETF](https://datatracker.ietf.org/doc/html/rfc9728)
8. [RFC 8414: OAuth 2.0 Authorization Server Metadata, IETF](https://datatracker.ietf.org/doc/html/rfc8414)
9. [RFC 8707: Resource Indicators for OAuth 2.0, IETF](https://www.rfc-editor.org/rfc/rfc8707.html)
10. [RFC 9207: OAuth 2.0 Authorization Server Issuer Identification, IETF](https://datatracker.ietf.org/doc/html/rfc9207)
11. [The OAuth 2.1 Authorization Framework (draft-ietf-oauth-v2-1-13), IETF](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-1-13)
12. [OAuth Client ID Metadata Document (draft-ietf-oauth-client-id-metadata-document-00), IETF](https://datatracker.ietf.org/doc/html/draft-ietf-oauth-client-id-metadata-document-00)

## Changelog

- 2026-12-06: First published.

## Cite this page

Agents for Devs. "MCP authorization step by step: OAuth 2.1, discovery and PKCE." Published Dec 6, 2026. https://agentsfordevs.com/guides/mcp-oauth-flow

- Page: https://agentsfordevs.com/guides/mcp-oauth-flow
- Markdown: https://agentsfordevs.com/guides/mcp-oauth-flow.md
- Reuse: quote or summarize with a link to the page. Terms: https://agentsfordevs.com/reuse
