MCP token exchange
Last updated: 2026-11-15T01:35:00-03:00 (UTC-3)
An agent or other workload often calls tools for an end user: the tool’s server must see the user, with only the rights the call needs, and must know it came through the gateway. Token exchange (RFC 8693) gives an MCP server exactly that: the workload presents the user’s token, the gateway verifies it and exchanges it at the identity provider for a short, downscoped token of the server’s audience, and sends only that token upstream.
| Flag | Default | What it turns on |
|---|---|---|
mcp_token_exchange |
false |
Execution of servers declared auth: token_exchange; an operator control; requires mcp_execution |
The profile is explicit and finite: access tokens (JWT) in, access tokens out, one identity provider per server, verified through its JWKS. Other token types, actor tokens, refresh tokens and impersonation are not supported. Console OIDC sign-in and inference JWT mapping are separate features and do not enable this one.
Principals
Section titled “Principals”| Principal | Who | Proven by |
|---|---|---|
| Actor (workload) | The gateway key making the call | The key itself, live on every call; it must be in allowed_key_ids |
| Subject (end user) | The user the call is for | The access token in X-MCP-Subject-Token, verified by the gateway |
| Gateway client | The gateway at the identity provider | Its client credentials on the token request; the issued token’s azp |
| Resource | The MCP server | The issued token’s aud (audience) |
What the gateway checks
Section titled “What the gateway checks”Before contacting the identity provider:
- the calling key is one of
allowed_key_ids(otherwise403 mcp_delegation_forbidden), and it has the usual scopes and tool grant; X-MCP-Subject-Tokenholds one JWT (otherwise401 mcp_subject_token_required);- its signature verifies against
jwks_url, its issuer isissuerand it has not expired (otherwise401 mcp_subject_token_invalid); - it is an access token (JOSE
typempty,JWTorat+jwt; a providertypclaim, when present,Bearer) whoseaudcontainssubject_audience: the token was issued for the gateway. A token already delegated (with anactclaim) is not delegated again. A token issued for another client is never exchanged, which stops a confused deputy from turning someone else’s token into access (401 mcp_subject_token_invalid); - its
scopecontains every scope inscopes: the exchange can narrow rights, never widen them (403 mcp_subject_scope_insufficient).
Then one RFC 8693 request (grant_type=urn:ietf:params:oauth:grant-type:token-exchange, the subject token as an access token, audience, scope), authenticated with client_id and client_secret_env. A refusal answers 403 mcp_token_exchange_denied and logs the OAuth error code; a refusal of the gateway’s own client (invalid_client, for example a rotated secret) answers 503 mcp_token_exchange_misconfigured, an operator setting; an unreachable or failing provider 503 mcp_token_exchange_unavailable. Neither falls back to another credential, and nothing is sent to the server.
The issued token is verified before use: an access token with the same issuer and signature, the same subject as the subject token, aud exactly audience (so it is valid nowhere else, not even as a new subject token), azp equal to client_id, an act claim, if any, naming the gateway client, an expiry no later than the subject token’s, every requested scope and no scope beyond scopes and the identity scopes openid, profile and email. scopes is therefore a ceiling, not only a request. Otherwise 502 mcp_token_exchange_invalid.
The issued token is used for this call only. It is not cached, logged, returned or sent anywhere else; the subject token is never forwarded upstream. The execution trail records the call with the actor key, team, credential class token_exchange and the end user it acted for (subject_issuer, subject).
Configuration
Section titled “Configuration”Boot configuration on the server’s mcp_execution.servers[] entry. The token and JWKS endpoints must be in mcp_execution.allowed_origins; the egress guard applies to them as to the server.
features: mcp_execution: true mcp_token_exchange: truemcp_execution: protocol_version: "2025-11-25" allowed_origins: ["https://tickets.example.com", "https://idp.example.com"] servers: - id: tickets endpoint_url: https://tickets.example.com/mcp auth: token_exchange token_exchange: token_endpoint: https://idp.example.com/realms/corp/protocol/openid-connect/token issuer: https://idp.example.com/realms/corp jwks_url: https://idp.example.com/realms/corp/protocol/openid-connect/certs subject_audience: ai-gateway client_id: ai-gateway client_secret_env: MCP_EXCHANGE_CLIENT_SECRET audience: mcp-tickets scopes: [tickets-read] allowed_key_ids: [key-support-agent] tools: - name: search_tickets allowed_team_ids: [support]| Field | Rules |
|---|---|
token_endpoint, issuer, jwks_url |
absolute https (plain http only on loopback with allow_loopback_http); endpoint origins in allowed_origins |
subject_audience |
required; the audience a subject token must have |
client_id, client_secret_env |
required; the secret is an environment reference, never inline |
audience |
required; different from every server’s subject_audience, so an issued token can never be exchanged again as a subject |
scopes |
1-32 explicit scopes requested; each must already be in the subject token |
allowed_key_ids |
1-128 explicit key IDs (no wildcard) allowed to act for users |
credential_env and oauth are refused on such a server. Changes apply on restart.
Calling
Section titled “Calling”POST /v1/mcp/servers/tickets/tools/callAuthorization: Bearer <workload gateway key>X-MCP-Subject-Token: <the user's access token issued for ai-gateway>Content-Type: application/json
{"name": "search_tickets", "arguments": {"query": "printer"}}The same header works on the MCP ingress and the composed endpoint.
Identity provider setup
Section titled “Identity provider setup”Verified with Keycloak 26.7.5 standard token exchange (standard.token.exchange.enabled on the gateway’s confidential client):
- the client the user signs in with adds the gateway client to the token audience (an audience mapper), so the user’s token is issued for
ai-gateway; - an optional client scope carries an audience mapper for the resource (
mcp-tickets) and is requested as the exchangescope, which downscopes the issued token; - the resource server accepts tokens whose
audcontains its audience and whoseazpis the gateway client.
Keycloak refuses the exchange for a disabled user (403 mcp_token_exchange_denied) and for a rotated client secret (503 mcp_token_exchange_misconfigured until client_secret_env holds the new secret). Other identity providers are not certified by this profile.
Health checks
Section titled “Health checks”An explicit health check of a token-exchange server has no end user to act for, so it runs without a credential and the server is expected to refuse it.
Upgrade
Section titled “Upgrade”No migration. Declaring auth: token_exchange on a server with the flag off makes its tools answer 503 mcp_token_exchange_disabled.