Understand the MCP OAuth Authorization Flow
mainThe Model Context Protocol (MCP) implements a secure OAuth 2.1-compliant authorization flow. The process follows these steps:
- Resource Metadata Discovery: The client probes the server for
WWW-Authenticateparameters (includingresource_metadataURL andscope). - Protected Resource Metadata: The client fetches resource server metadata (RFC 9728).
- AS Metadata Discovery: The client discovers authorization server metadata via RFC 8414 and OpenID Connect endpoints.
- Client Registration: The client may dynamically register itself or use a URL-based Client ID.
- Scope Selection: Scopes are selected based on a hierarchy:
WWW-Authenticate> PRM > AS metadata > caller defaults. - Authorization Request: An authorization URL is built using PKCE (S256) and RFC 8707 resource parameters.
- Authorization Code Exchange: The code is exchanged for an access token.
- Token Usage: Tokens are used via
AuthClientorAuthorizedHttpClient. - Token Refresh: The SDK automatically uses refresh tokens, forwarding previously granted scopes to ensure compatibility with providers like Azure AD v2.
- Scope Upgrade: If a
403 insufficient_scopeerror occurs, the SDK computes the scope union and re-authorizes with upgraded scopes.