OAuth for MCP servers: what the spec requires and what it leaves to you The MCP authorization specification revision 2026-07-28 requires MCP servers acting as OAuth 2.1 resource servers to publish protected resource metadata under RFC 9728, return a 401 whose WWW-Authenticate header points to that metadata, validate the token audience against the server's own canonical URI, and never pass through tokens issued for other systems. The spec leaves scope policy, consent and post-issuance handling to implementers, with the article using Keycloak 26.8 as its reference authorization server. Authorization is optional in MCP: HTTP-transport servers SHOULD follow the spec, while stdio servers SHOULD NOT and instead read credentials from the environment. OAuth for MCP servers: what the spec requires and what it leaves to you The MCP authorization spec answers one question well: how does a client get a token that only your server will accept? It says very little about what that token should allow, who decides, or what your server does with it afterwards. Those parts are still your job. This article follows the MCP specification revision 2026-07-28 https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization , the latest at the time of writing, and Keycloak 26.8. Where earlier revisions differ, it says so. Who does what The specification gives OAuth roles to the three parties: - the MCP server is an OAuth 2.1 resource server: it accepts access tokens and checks them; - the MCP client Claude Code, Cursor, an IDE plugin, your own agent is an OAuth 2.1 client that acts on behalf of a user; - the authorization server signs the user in and issues tokens. It can be part of the MCP server or a separate product such as Keycloak, and the spec does not describe how it works inside. Authorization is optional in MCP. When a server uses an HTTP transport, it SHOULD follow the authorization spec. A server that runs over stdio SHOULD NOT: it reads its credentials from the environment, like any local process. Most of what follows is about remote servers over HTTP. What the spec requires of your MCP server The server’s side of the spec is short. You need four things. Protected resource metadata RFC 9728 . The server publishes a JSON document that tells clients which authorization server issues its tokens. It MUST contain authorization servers with at least one entry. Clients find it in one of two ways: a resource metadata parameter in the WWW-Authenticate header of a 401, or a well-known URL. For a server at https://mcp.example.com/mcp , the client first tries https://mcp.example.com/.well-known/oauth-protected-resource/mcp and then the root form without the path. { "resource": "https://mcp.example.com/mcp", "authorization servers": "https://sso.example.com/realms/acme" , "scopes supported": "mcp:tools" } A useful 401. A request without a valid token gets a 401, and the header points to the metadata. The spec says the header SHOULD also carry the scope needed for the request: HTTP/1.1 401 Unauthorized WWW-Authenticate: Bearer resource metadata="https://mcp.example.com/.well-known/oauth-protected-resource/mcp", scope="mcp:tools" Audience validation. The server MUST check that the token was issued for it, and reject anything else with a 401. In practice this means checking that the aud claim contains the server’s own canonical URI for example https://mcp.example.com/mcp , without a trailing slash . A server that only checks the signature and the expiry accepts any token from the same Keycloak realm, including tokens issued for other applications. No token passthrough. The server MUST NOT accept tokens issued for someone else, and MUST NOT forward the token it received to the APIs behind it. If a tool calls GitHub, a database or another internal API, that call uses a separate token, issued for that API. The security best practices page lists what goes wrong otherwise: the downstream API cannot tell who the real caller is, rate limits and audit logs see the wrong identity, and a stolen token works on more systems than it should. There is one more behaviour the spec recommends without requiring it. When a token is valid but lacks a scope, the server SHOULD answer 403 with error="insufficient scope" and the missing scopes. A client acting for a user then SHOULD ask the user to authorize again with the wider scope step-up . This lets a client start with a read-only token and ask for more only when a prompt actually needs it. What the spec requires of the client Most of the work is on the client side, and if you use an existing MCP client you get it for free. It is still worth knowing what the client does, because each step depends on something you configure. 1. It calls the server, gets the 401, and reads the protected resource metadata. 2. It reads the authorization server’s metadata. It MUST try both RFC 8414 /.well-known/oauth-authorization-server and OpenID Connect Discovery /.well-known/openid-configuration , and MUST check that the issuer in the document is the one it asked for. 3. It gets a client ID. The preferred order is a pre-registered client, then a Client ID Metadata Document https://datatracker.ietf.org/doc/draft-ietf-oauth-client-id-metadata-document/ CIMD , then Dynamic Client Registration RFC 7591 . With CIMD the client ID is an HTTPS URL that serves the client’s metadata, so the authorization server can fetch it instead of storing a registration. Revision 2026-07-28 marks Dynamic Client Registration as deprecated in favour of CIMD. 4. It runs the authorization code flow with PKCE, using S256 . If the authorization server metadata has no code challenge methods supported , the client MUST refuse to continue. 5. It sends the resource parameter RFC 8707 with the server’s canonical URI in both the authorization request and the token request, whether or not the authorization server supports it. 6. It validates the iss parameter of the authorization response RFC 9207 when present. This check is new in 2026-07-28. 7. It sends the token in the Authorization: Bearer header on every request, never in the query string. Steps 3 and 5 are the ones that usually fail against an existing identity provider. The client can only use CIMD if the authorization server advertises client id metadata document supported . And the resource parameter is useless if the authorization server ignores it and issues a token without the right audience. What it requires of the authorization server, and where Keycloak stands The authorization server MUST implement OAuth 2.1 and publish RFC 8414 or OpenID Connect Discovery metadata, SHOULD issue short-lived access tokens, and MUST rotate refresh tokens for public clients. The spec gives no number for “short-lived”. Keycloak publishes its own MCP guide https://www.keycloak.org/securing-apps/mcp-authz-server with a conformance table. For Keycloak 26.8: | Standard | MCP 2026-07-28 | Keycloak | |---|---|---| | OAuth 2.1, RFC 8414 / OIDC Discovery | MUST | supported | | Resource Indicators RFC 8707 | MUST | experimental, --features=resource-indicators | | Client ID Metadata Document | SHOULD | experimental, --features=cimd | | Issuer identification RFC 9207 | SHOULD | supported | | Dynamic Client Registration RFC 7591 | MAY | supported | By its own criteria, Keycloak fully supports MCP 2025-03-26 and supports the later revisions only experimentally. That leaves you two options for audience binding: - Without the experimental feature , Keycloak ignores resource . You create an optional client scope for example mcp:tools with an Audience mapper whose custom audience is the MCP server’s URL. A client that requests that scope gets a token with the right aud . The binding then rests on the scope, not on the resource parameter. - With resource-indicators enabled , you register the MCP server as a client with its resource url attribute set to its URL. Keycloak then puts only that value in aud , and rejects an unknown resource with invalid target . If you also enable CIMD, you set the allowed resource URLs in the client policy executor instead of adding Audience mappers. Experimental features in Keycloak can change between minor versions. If you enable them, pin the Keycloak version and test the flow again before each upgrade. What the spec leaves to you This is the larger part of the work, and the spec is explicit that it does not cover it. What a scope means. The spec defines how scopes travel in the challenge, in scopes supported , in the 403 and tells you to keep scopes supported minimal and to avoid wildcards such as or full-access . It does not say how scopes map to tools. Whether mcp:tools means “every tool”, or whether you have kube:read and kube:write , or one scope per tool, is your decision. The coarser the scopes, the more a stolen token can do. Per-call authorization. A valid token with the right scope tells you the user consented to a class of actions. It does not tell you whether this user may scale this deployment in this namespace now. Rules on resources, arguments, time or approval belong in your server or in a policy engine OPA, Cedar, OpenFGA , and the spec has nothing to say about them. Who gets which scope. Which users or groups may receive kube:write is a decision for the authorization server’s configuration: Keycloak roles, client scope assignments, consent settings. The MCP spec does not describe it. Tokens for the systems behind your server. The spec forbids passthrough and says the upstream token is “a separate token, issued by the upstream authorization server”. It does not say how to get one. The usual choices are a service account simple, but the upstream sees the server and not the user or OAuth 2.0 Token Exchange RFC 8693 , which gives a token for the upstream on behalf of the same user. Keycloak 26 supports standard token exchange, enabled per client. Token exchange is not mentioned in the core authorization pages. Audit. The spec does not ask for any log. If you need to know which person called which tool with which arguments, your server or a gateway has to write that record, with the token’s sub and the client ID. Revocation and lifetimes. “Short-lived” has no number, and the spec does not ask servers to check revocation. With five-minute access tokens, removing a user in Keycloak takes effect within five minutes. With one-hour tokens it takes an hour, unless your server uses token introspection. Agents without a user, and company-wide SSO. Two optional extensions cover cases the core flow does not. OAuth Client Credentials https://modelcontextprotocol.io/extensions/auth/oauth-client-credentials is for machine-to-machine access with no user present CI jobs, daemons . Enterprise-Managed Authorization https://modelcontextprotocol.io/extensions/auth/enterprise-managed-authorization lets the company identity provider decide which MCP servers an employee may use: the client exchanges the user’s ID token for an ID-JAG an IETF draft and presents it to the MCP authorization server. According to the MCP page, Keycloak accepts ID-JAG experimentally from 26.7, behind the identity-assertion-jwt feature flag. Extensions are opt-in, so check that your clients support them before you rely on them. Stdio servers. A local server started by the client reads its credentials from environment variables. The spec does not cover where those come from, how long they live, or how they are revoked. In many teams these are personal access tokens with broad scope, which is the problem the HTTP flow was meant to remove. A checklist for your MCP server Before you put a remote MCP server in front of real users, check that: - the protected resource metadata is served at the well-known URL and lists your Keycloak realm as issuer; - the 401 carries resource metadata and the scope needed; - the server rejects tokens whose aud does not contain its own canonical URI test it with a token from another client in the same realm ; - tools that change things require a different scope from read-only tools, and the server answers 403 insufficient scope when it is missing; - calls to upstream APIs use a token or credential issued for that API, never the incoming token; - access token lifetime in Keycloak is a number you chose, not the realm default you never looked at; - every call is logged with the user, the client, the tool and the outcome. Where a gateway fits If you run one MCP server, the work above belongs in that server. With several servers, each one repeats the metadata, the audience check, the scope model, the upstream tokens and the log, and each one can get them slightly wrong. That is the case for an MCP gateway https://izylifesolutions.com/articles/what-is-an-mcp-gateway-and-when-you-need-one/ : the gateway is the single resource server that clients see, and it exchanges the user’s token for one token per upstream server. IzyTrust https://izylifesolutions.com/products/izytrust/ , the gateway we build at Izylife Solutions, works this way with Keycloak. It maps each tool to a scope in a registry, denies tools that are not registered, and writes one audit log. It is in early access. To discuss your MCP servers and your Keycloak setup, contact us https://izylifesolutions.com/?service=izytrust-demo contacts .