
MCP OAuth 2.1 授权从资源服务器发现起步,而非授权服务器
来源标题:MCP OAuth 2.1: PKCE, Resource Metadata and Client ID Metadata Documents
MCP 的 OAuth 流程与常规做法相反:客户端先发现 MCP 服务器,由其通过 authorization_servers 指明授权服务器位置,之后才启动 OAuth。客户端再按 RFC 8414 拉取授权服务器元数据,其中需包含 issuer、授权与令牌端点、PKCE 方法等信息。若支持 CIMD,元数据中须声明 client_id_metadata_document_supported。
以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。
This is the MCP-specific bit many teams miss. They start with the authorization server. MCP starts with the resource server. The client discovers the MCP server, the MCP server tells it where authorization lives, and only then does the OAuth flow begin.
Once the MCP client has an authorization server issuer from authorization_servers, it fetches authorization server metadata (RFC 8414). RFC 8414 defines the metadata document at /.well-known/oauth-authorization-server, using HTTPS, and says it contains claims about the server's configuration, including endpoint and public key information.
A practical MCP authorization server metadata response should include the issuer, authorization endpoint, token endpoint, client_id_metadata_document_supported if CIMD is supported, a registration endpoint only if legacy dynamic client registration is supported, JWKS URI if JWT validation is used, supported response types, grant types, scopes, and PKCE methods:
{ "issuer": "https://auth.example.com", "authorization_endpoint": "https://auth.example.com/oauth/authorize", "token_endpoint": "https://auth.example.com/oauth/token", "client_id_metadata_document_supported": true, "authorization_response_iss_parameter_supported": true, "registration_endpoint": "https://auth.example.com/oauth/register", "jwks_uri": "https://auth.example.com/.well-known/jwks.json", "response_types_supported": ["code"], "grant_types_supported": [ "authorization_code", "refresh_token", "urn:ietf:params:oauth:grant-type:device_code" ], "code_challenge_methods_supported": ["S256"], "scopes_supported": [ "mcp.tools.read", "mcp.tools.write", "mcp.tools.admin", "offline_access" ] }
RFC 8414 includes grant_types_supported, registration_endpoint, scopes_supported, jwks_uri, and code_challenge_methods_supported as metadata fields. It also says that if code_challenge_methods_supported is omitted, the authorization server does not support PKCE. That omission should be treated as a hard stop for MCP public clients, not as an invitation to "try anyway."
RFC 8414 also requires issuer validation: the returned issuer value must be identical to the issuer identifier used to build the metadata URL, or the metadata must not be used. This sounds pedantic until your client follows a malicious metadata document to an attacker-controlled token endpoint. OAuth security is mostly boring string comparison with consequences.
Traditional OAuth assumes the client developer and authorization server operator have a prior relationship. MCP breaks that assumption. A user may connect Claude Desktop, Cursor, VS Code, a custom internal agent, or some workflow runner to an MCP server the client has never seen before. Asking every MCP client vendor to pre-register with every MCP server's authorization server is enterprise theater. Everyone gets a spreadsheet, nobody gets security.
Client ID Metadata Documents solve this without a registration call. Under the 2026-07-28 client registration rules, the client hosts a JSON document at an HTTPS URL with a path, and that URL is its client_id. The document must include at least client_id, client_name, and redirect_uris, and its client_id must match the document URL exactly:
{ "client_id": "https://assistant.example.com/oauth/client-metadata.json", "client_name": "Example AI Assistant", "client_uri": "https://assistant.example.com", "redirect_uris": [ "http://127.0.0.1:39123/oauth/callback" ], "grant_types": ["authorization_code"], "response_types": ["code"], "token_endpoint_auth_method": "none" }
When the authorization server sees a URL-formatted client_id, it should fetch the document, must check that the client_id inside matches the URL, must validate the JSON and required fields, must check the request's redirect URI against redirect_uris, and should cache the document according to HTTP cache headers. Clients may use private_key_jwt with a JWKS if they need to authenticate at the token endpoint. A nice side effect: CIMD client IDs are portable. When protected resource metadata points to a new authorization server, the client doesn't need a new server-issued ID, because the new server can fetch the same URL. That server still decides whether to trust the client.
The server still owns trust. A URL is not a reputation. Authorization servers should follow the CIMD security considerations: consider SSRF risk when fetching the document, optionally apply domain-based trust policies, and clearly show the redirect URI hostname during authorization so a user can spot a lookalike.
Dynamic client registration (RFC 7591) lets a client register with an authorization server programmatically and get a client_id back. Earlier MCP revisions (2025-03-26, 2025-06-18) said clients and servers should support it. The 2026-07-28 spec deprecates it: support is now MAY, kept for authorization servers that don't support CIMD yet. If you maintain an MCP client today, support CIMD first and keep DCR only as the fallback when the server advertises a registration_endpoint and no CIMD support. Clients using DCR must send an application_type, and must tie any persisted credentials to the issuing authorization server's issuer, re-registering if it changes.
A legacy DCR registration request for a public, native MCP client might look like this:
{ "client_name": "Example AI Assistant", "application_type": "native", "redirect_uris": [ "http://127.0.0.1:39123/oauth/callback" ], "grant_types": [ "authorization_code", "refresh_token" ], "response_types": ["code"], "token_endpoint_auth_method": "none", "scope": "mcp.tools.read mcp.tools.write offline_access" }
Do not confuse dynamic registration with open registration. The authorization server can require an initial access token, reject redirect URI patterns, deny suspicious client metadata, issue software statements, restrict scopes, or apply tenant policy. RFC 7591 explicitly allows a registration endpoint to be protected and to require an initial access token when the server wants to limit who can register. Open DCR with sloppy redirect validation is not interoperability. It is a phishing kit with JSON.
For a human granting an AI assistant access to an MCP session, the right baseline is authorization code flow with PKCE.
The MCP client starts unauthenticated, receives a 401 challenge, discovers protected resource metadata, discovers authorization server metadata, dynamically registers if needed, then opens the user's browser to the authorization endpoint. The request includes the registered client_id, exact redirect_uri, requested scopes, state, PKCE code_challenge, code_challenge_method=S256, and the resource parameter identifying the MCP server.
The 2025-06-18 MCP spec requires MCP clients to implement resource indicators and include the resource parameter in both authorization and token requests; the value must identify the MCP server the token is intended for. It also says MCP clients must send the resource parameter regardless of whether the authorization server supports it. That is not paperwork. That is how you avoid a token issued for Server A being replayed to Server B.
The authorization request looks like this:
GET /oauth/authorize? response_type=code& client_id=mcp-client-123& redirect_uri=http%3A%2F%2F127.0.0.1%3A39123%2Foauth%2Fcallback& scope=mcp.tools.read%20mcp.tools.write& state=af0ifjsldkj& code_challenge=Z9E...& code_challenge_method=S256& resource=https%3A%2F%2Fmcp.example.com
After the user authenticates and consents, the authorization server redirects back with an authorization code. The client exchanges the code at the token endpoint and includes the PKCE code_verifier and the same resource value:
POST /oauth/token Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code& code=SplxlOBeZQQYbYS6WxSbIA& redirect_uri=http%3A%2F%2F127.0.0.1%3A39123%2Foauth%2Fcallback& client_id=mcp-client-123& code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk& resource=https%3A%2F%2Fmcp.example.com
The token response should return a short-lived access token, optionally a refresh token, expiration metadata, and scopes actually granted:
{ "access_token": "eyJhbGciOiJSUzI1NiIs...", "token_type": "Bearer", "expires_in": 900, "refresh_token": "def50200...", "scope": "mcp.tools.read mcp.tools.write" }
Then every MCP HTTP request carries the bearer token:
POST /mcp HTTP/1.1 Host: mcp.example.com Authorization: Bearer eyJhbGciOiJSUzI1NiIs... Content-Type: application/json
{ "jsonrpc": "2.0", "id": 12, "method": "tools/call", "params": { "name": "create_issue", "arguments": { "title": "Fix OAuth audience validation" } } }
The MCP server validates the token before doing anything useful. It checks signature or token introspection, issuer, expiry, audience, resource binding, scopes, client identity if relevant, and revocation status if supported. If validation fails, it returns 401. If the token is valid but insufficient for the requested action, it returns 403. The MCP spec uses exactly that distinction: 401 for missing or invalid authorization, 403 for invalid scopes or insufficient permissions.
Take a common architecture: a user wants an AI assistant to work with GitHub through an MCP server. The safe architecture has three different roles, not one magical token.
The MCP client is the AI assistant runtime. The protected resource is the GitHub MCP server or a gateway endpoint, for example Permit MCP Gateway deployed in front of GitHub's tools, representing it. The authorization server is either the MCP server's own auth service, a gateway consent service, or an enterprise IdP-backed authorization server. GitHub itself may also be an upstream OAuth provider behind the MCP server.
Here is the flow when it is done correctly.
First, the AI assistant calls the MCP endpoint without a token:
POST /mcp HTTP/1.1 Host: github-mcp.example.com
The MCP server responds:
HTTP/1.1 401 Unauthorized WWW-Authenticate: Bearer resource_metadata="https://github-mcp.example.com/.well-known/oauth-protected-resource"
The client fetches:
GET /.well-known/oauth-protected-resource HTTP/1.1 Host: github-mcp.example.com
The MCP server returns:
{ "resource": "https://github-mcp.example.com", "authorization_servers": [ "https://auth.example.com" ], "scopes_supported": [ "github.repos.read", "github.issues.write", "github.pr.write" ], "resource_name": "GitHub MCP" }
The client then fetches RFC 8414 metadata from the authorization server:
GET /.well-known/oauth-authorization-server HTTP/1.1 Host: auth.example.com
The response gives the authorization endpoint, token endpoint, JWKS URI, supported grant types, scopes, PKCE methods, the CIMD support flag, and a registration_endpoint only if legacy DCR is offered. If the client does not already have a client_id for that server, it uses its Client ID Metadata Document URL as the client_id when the server advertises client_id_metadata_document_supported, and falls back to RFC 7591 dynamic client registration only when it has to. Then it starts authorization code flow with PKCE, including:
client_id=https://assistant.example.com/oauth/client-metadata.json redirect_uri=http://127.0.0.1:39123/oauth/callback resource=https://github-mcp.example.com scope=github.repos.read github.issues.write code_challenge_method=S256
The human signs in and consents. When the authorization server redirects back, the client compares the iss parameter (if present) with the issuer it recorded during discovery and refuses to redeem the code on a mismatch, per RFC 9207. Either way, the client redeems the code only at the token endpoint of the issuer it chose during discovery, and checks state as usual. If the MCP server must access GitHub upstream, one of two things happens. Either the MCP authorization server already has a secure relationship to GitHub, or the MCP server/gateway performs a delegated third-party OAuth
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。