{"slug": "oauth-for-mcp-servers-what-the-spec-requires-and-what-it-leaves-to-you", "title": "OAuth for MCP servers: what the spec requires and what it leaves to you", "summary": "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.", "body_md": "# OAuth for MCP servers: what the spec requires and what it leaves to you\n\nThe 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.\n\nThis article follows the MCP specification revision\n[2026-07-28](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization),\nthe latest at the time of writing, and Keycloak 26.8. Where earlier revisions differ, it says\nso.\n\n## Who does what\n\nThe specification gives OAuth roles to the three parties:\n\n- the **MCP server** is an OAuth 2.1 resource server: it accepts access tokens and checks them;\n- the **MCP client** (Claude Code, Cursor, an IDE plugin, your own agent) is an OAuth 2.1\nclient that acts on behalf of a user;\n- the **authorization server** signs the user in and issues tokens. It can be part of the MCP\nserver or a separate product such as Keycloak, and the spec does not describe how it works\ninside.\n\nAuthorization 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.\n\n## What the spec requires of your MCP server\n\nThe server’s side of the spec is short. You need four things.\n\n**Protected resource metadata (RFC 9728).** The server publishes a JSON document that tells\nclients which authorization server issues its tokens. It MUST contain `authorization_servers`\nwith at least one entry. Clients find it in one of two ways: a `resource_metadata` parameter in\nthe `WWW-Authenticate` header of a 401, or a well-known URL. For a server at\n`https://mcp.example.com/mcp`, the client first tries\n`https://mcp.example.com/.well-known/oauth-protected-resource/mcp` and then the root form\nwithout the path.\n\n```\n{\n  \"resource\": \"https://mcp.example.com/mcp\",\n  \"authorization_servers\": [\"https://sso.example.com/realms/acme\"],\n  \"scopes_supported\": [\"mcp:tools\"]\n}\n```\n\n**A useful 401.** A request without a valid token gets a 401, and the header points to the\nmetadata. The spec says the header SHOULD also carry the scope needed for the request:\n\n```\nHTTP/1.1 401 Unauthorized\nWWW-Authenticate: Bearer resource_metadata=\"https://mcp.example.com/.well-known/oauth-protected-resource/mcp\",\n                  scope=\"mcp:tools\"\n```\n\n**Audience validation.** The server MUST check that the token was issued for it, and reject\nanything else with a 401. In practice this means checking that the `aud` claim contains the\nserver’s own canonical URI (for example `https://mcp.example.com/mcp`, without a trailing\nslash). A server that only checks the signature and the expiry accepts any token from the same\nKeycloak realm, including tokens issued for other applications.\n\n**No token passthrough.** The server MUST NOT accept tokens issued for someone else, and MUST\nNOT forward the token it received to the APIs behind it. If a tool calls GitHub, a database or\nanother internal API, that call uses a separate token, issued for that API. The security best\npractices page lists what goes wrong otherwise: the downstream API cannot tell who the real\ncaller is, rate limits and audit logs see the wrong identity, and a stolen token works on more\nsystems than it should.\n\nThere is one more behaviour the spec recommends without requiring it. When a token is valid\nbut lacks a scope, the server SHOULD answer 403 with `error=\"insufficient_scope\"` and the\nmissing scopes. A client acting for a user then SHOULD ask the user to authorize again with the\nwider scope (step-up). This lets a client start with a read-only token and ask for more only\nwhen a prompt actually needs it.\n\n## What the spec requires of the client\n\nMost 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.\n\n1. It calls the server, gets the 401, and reads the protected resource metadata.\n2. It reads the authorization server’s metadata. It MUST try both RFC 8414\n(`/.well-known/oauth-authorization-server` ) and OpenID Connect Discovery\n(`/.well-known/openid-configuration` ), and MUST check that the`issuer` in the document is\nthe one it asked for.\n3. It gets a client ID. The preferred order is a pre-registered client, then a\n[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\nthat serves the client’s metadata, so the authorization server can fetch it instead of\nstoring a registration. Revision 2026-07-28 marks Dynamic Client Registration as deprecated\nin favour of CIMD.\n4. It runs the authorization code flow with PKCE, using `S256` . If the authorization server\nmetadata has no`code_challenge_methods_supported` , the client MUST refuse to continue.\n5. It sends the `resource` parameter (RFC 8707) with the server’s canonical URI in both the\nauthorization request and the token request, whether or not the authorization server\nsupports it.\n6. It validates the `iss` parameter of the authorization response (RFC 9207) when present.\nThis check is new in 2026-07-28.\n7. It sends the token in the `Authorization: Bearer` header on every request, never in the\nquery string.\n\nSteps 3 and 5 are the ones that usually fail against an existing identity provider. The client\ncan only use CIMD if the authorization server advertises `client_id_metadata_document_supported`.\nAnd the `resource` parameter is useless if the authorization server ignores it and issues a\ntoken without the right audience.\n\n## What it requires of the authorization server, and where Keycloak stands\n\nThe 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”.\n\nKeycloak publishes its own\n[MCP guide](https://www.keycloak.org/securing-apps/mcp-authz-server) with a conformance table.\nFor Keycloak 26.8:\n\n| Standard | MCP 2026-07-28 | Keycloak | \n|---|---|---|\n| OAuth 2.1, RFC 8414 / OIDC Discovery | MUST | supported | \n| Resource Indicators (RFC 8707) | MUST | experimental, `--features=resource-indicators` | \n| Client ID Metadata Document | SHOULD | experimental, `--features=cimd` | \n| Issuer identification (RFC 9207) | SHOULD | supported | \n| Dynamic Client Registration (RFC 7591) | MAY | supported | \n\nBy 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:\n\n- **Without the experimental feature** , Keycloak ignores`resource` . You create an optional\nclient scope (for example`mcp:tools` ) with an Audience mapper whose custom audience is the\nMCP server’s URL. A client that requests that scope gets a token with the right`aud` . The\nbinding then rests on the scope, not on the`resource` parameter.\n- **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\nrejects an unknown resource with`invalid_target` . If you also enable CIMD, you set the\nallowed resource URLs in the client policy executor instead of adding Audience mappers.\n\nExperimental features in Keycloak can change between minor versions. If you enable them, pin the Keycloak version and test the flow again before each upgrade.\n\n## What the spec leaves to you\n\nThis is the larger part of the work, and the spec is explicit that it does not cover it.\n\n**What a scope means.** The spec defines how scopes travel (in the challenge, in\n`scopes_supported`, in the 403) and tells you to keep `scopes_supported` minimal and to avoid\nwildcards such as `*` or `full-access`. It does not say how scopes map to tools. Whether\n`mcp:tools` means “every tool”, or whether you have `kube:read` and `kube:write`, or one scope\nper tool, is your decision. The coarser the scopes, the more a stolen token can do.\n\n**Per-call authorization.** A valid token with the right scope tells you the user consented to\na class of actions. It does not tell you whether this user may scale this deployment in this\nnamespace now. Rules on resources, arguments, time or approval belong in your server or in a\npolicy engine (OPA, Cedar, OpenFGA), and the spec has nothing to say about them.\n\n**Who gets which scope.** Which users or groups may receive `kube:write` is a decision for the\nauthorization server’s configuration: Keycloak roles, client scope assignments, consent\nsettings. The MCP spec does not describe it.\n\n**Tokens for the systems behind your server.** The spec forbids passthrough and says the\nupstream token is “a separate token, issued by the upstream authorization server”. It does not\nsay how to get one. The usual choices are a service account (simple, but the upstream sees the\nserver and not the user) or OAuth 2.0 Token Exchange (RFC 8693), which gives a token for the\nupstream on behalf of the same user. Keycloak 26 supports standard token exchange, enabled per\nclient. Token exchange is not mentioned in the core authorization pages.\n\n**Audit.** The spec does not ask for any log. If you need to know which person called which\ntool with which arguments, your server or a gateway has to write that record, with the\ntoken’s `sub` and the client ID.\n\n**Revocation and lifetimes.** “Short-lived” has no number, and the spec does not ask servers to\ncheck revocation. With five-minute access tokens, removing a user in Keycloak takes effect\nwithin five minutes. With one-hour tokens it takes an hour, unless your server uses token\nintrospection.\n\n**Agents without a user, and company-wide SSO.** Two optional extensions cover cases the core\nflow does not. [OAuth Client Credentials](https://modelcontextprotocol.io/extensions/auth/oauth-client-credentials)\nis for machine-to-machine access with no user present (CI jobs, daemons). [Enterprise-Managed\nAuthorization](https://modelcontextprotocol.io/extensions/auth/enterprise-managed-authorization)\nlets the company identity provider decide which MCP servers an employee may use: the client\nexchanges the user’s ID token for an ID-JAG (an IETF draft) and presents it to the MCP\nauthorization server. According to the MCP page, Keycloak accepts ID-JAG experimentally from\n26.7, behind the `identity-assertion-jwt` feature flag. Extensions are opt-in, so check that\nyour clients support them before you rely on them.\n\n**Stdio servers.** A local server started by the client reads its credentials from\nenvironment variables. The spec does not cover where those come from, how long they live, or\nhow they are revoked. In many teams these are personal access tokens with broad scope, which\nis the problem the HTTP flow was meant to remove.\n\n## A checklist for your MCP server\n\nBefore you put a remote MCP server in front of real users, check that:\n\n- the protected resource metadata is served at the well-known URL and lists your Keycloak realm as issuer;\n- the 401 carries `resource_metadata` and the scope needed;\n- the server rejects tokens whose `aud` does not contain its own canonical URI (test it with a\ntoken from another client in the same realm);\n- tools that change things require a different scope from read-only tools, and the server\nanswers 403 `insufficient_scope` when it is missing;\n- calls to upstream APIs use a token or credential issued for that API, never the incoming token;\n- access token lifetime in Keycloak is a number you chose, not the realm default you never looked at;\n- every call is logged with the user, the client, the tool and the outcome.\n\n## Where a gateway fits\n\nIf you run one MCP server, the work above belongs in that server. With several servers, each\none repeats the metadata, the audience check, the scope model, the upstream tokens and the\nlog, and each one can get them slightly wrong. That is the case for an\n[MCP gateway](https://izylifesolutions.com/articles/what-is-an-mcp-gateway-and-when-you-need-one/): the gateway is the\nsingle resource server that clients see, and it exchanges the user’s token for one token per\nupstream server.\n\n[IzyTrust](https://izylifesolutions.com/products/izytrust/), the gateway we build at Izylife Solutions, works this way with\nKeycloak. It maps each tool to a scope in a registry, denies tools that are not registered, and\nwrites one audit log. It is in early access. To discuss your MCP servers and your Keycloak\nsetup, [contact us](https://izylifesolutions.com/?service=izytrust-demo#contacts).", "url": "https://wpnews.pro/news/oauth-for-mcp-servers-what-the-spec-requires-and-what-it-leaves-to-you", "canonical_source": "https://izylifesolutions.com/articles/oauth-for-mcp-servers-what-the-spec-requires/", "published_at": "2026-12-02 12:00:00+00:00", "updated_at": "2026-12-02 18:14:31.659858+00:00", "lang": "en", "topics": ["agent-protocols", "ai-agents", "ai-tools"], "entities": ["Model Context Protocol", "Keycloak", "Claude Code", "Cursor", "RFC 9728"], "also_reported_by": [], "takeaway": "MCP servers must validate the token's aud claim against their own canonical URI and refuse to forward received tokens downstream, since checking only signature and expiry accepts any token from the same Keycloak realm.", "recommendation": {"advice": "Validate the aud claim against your server's canonical URI and issue separate downstream tokens instead of passing through client tokens; otherwise any token from the same realm is accepted.", "playbook": "agent-security", "playbook_url": "https://wpnews.pro/playbooks/agent-security"}, "alternates": {"html": "https://wpnews.pro/news/oauth-for-mcp-servers-what-the-spec-requires-and-what-it-leaves-to-you", "markdown": "https://wpnews.pro/news/oauth-for-mcp-servers-what-the-spec-requires-and-what-it-leaves-to-you.md", "text": "https://wpnews.pro/news/oauth-for-mcp-servers-what-the-spec-requires-and-what-it-leaves-to-you.txt", "jsonld": "https://wpnews.pro/news/oauth-for-mcp-servers-what-the-spec-requires-and-what-it-leaves-to-you.jsonld"}}