Call a downstream API from an MCP server without passing the user's token through
Your MCP server, AI agent or service receives a user's access token and has to call another API for that user. Forwarding the same token is token passthrough, which the MCP authorization spec forbids: the token was issued for your server, not for the API behind it. RFC 8693 token exchange gives your server a new token, addressed to the downstream API, that names your server as the actor.
Why test it against HeyIdentity
Keycloak, Auth0, Okta and others support token exchange. If your test IdP doesn't, you end up writing your own token service, and your downstream API trusts a second issuer it never sees in production. A HeyIdentity Generic configuration answers grant_type=urn:ietf:params:oauth:grant-type:token-exchange at its own token endpoint, signs the new token with the same keys, and publishes the grant in discovery. It is available on every plan.
Set up three clients
On a Generic configuration, add these clients in the editor (or through the agent API):
- The MCP client the user signs in with, for example
weather-app: a public client (no secret) with PKCE and its redirect URI. It requests the MCP server's scope,https://weather.example/mcp/tools.read. - The MCP server's own client,
weather-mcp: a confidential client with a secret. It exposeshttps://weather.example/mcp/tools.read, so the user's token hasaud=https://weather.example/mcpand is addressed to it. Its allowed scopes list the downstream API's resource-qualified scopes, such asapi://forecast-api/forecast.read. - The downstream API,
forecast-api: it exposesapi://forecast-api/forecast.read, which makesapi://forecast-apia resource the configuration knows.
Setting https://weather.example/mcp only as the public client's access token audience isn't enough: the user's token must be addressed to a resource the exchanging client owns, its own exposed scopes or its own access token audience. Keep Access token audience in the token shape at aud; under client_id every exchange is refused.
Your runtime URLs come from the configuration's All endpoints and IDs. Discovery is in this shape, and its token_endpoint and grant_types_supported (which lists the token-exchange grant) are what your server reads:
https://app.heyidentity.dev/t/{tenant_route_id}/c/{configuration_id}/.well-known/openid-configuration
Exchange the user's token
After the user signs in through weather-app, your MCP server receives their access token. It authenticates as weather-mcp and exchanges that token:
POST /t/{tenant_route_id}/c/{configuration_id}/token
Host: app.heyidentity.dev
Authorization: Basic base64(weather-mcp:<secret>)
Content-Type: application/x-www-form-urlencoded
grant_type=urn:ietf:params:oauth:grant-type:token-exchange
&subject_token=<the user's access token>
&subject_token_type=urn:ietf:params:oauth:token-type:access_token
&audience=api://forecast-api
&scope=api://forecast-api/forecast.read
The response carries one new access token and no refresh token:
{
"access_token": "eyJ…",
"issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
"token_type": "Bearer",
"expires_in": "3600",
"scope": "api://forecast-api/forecast.read"
}
Decoded, the issued token is the same user's token for the downstream API, with your server as the actor:
{
"iss": "https://app.heyidentity.dev/t/{tenant_route_id}/c/{configuration_id}",
"sub": "<the user's sub>",
"aud": "api://forecast-api",
"scope": "api://forecast-api/forecast.read",
"roles": [ "reader" ],
"act": { "sub": "weather-mcp" },
"exp": …
}
Instead of audience you can send resource=api://forecast-api, or only the resource-qualified scope. With no scope, the token carries every allowed scope of weather-mcp for the target. If forecast-api exchanges this token again, the next token's act nests the first actor: {"sub": "forecast-api", "act": {"sub": "weather-mcp"}}. Only a token an exchange issued carries a delegation chain; its JWT header has heyidentity_exchanged: true. An act claim you configure yourself is ignored as a chain and never appears in an exchanged token.
Verify it downstream
- Have the downstream API validate the token as it would in production: signature against the configuration's JWKS from discovery, issuer, and audience
api://forecast-api. - Check that
act.subis your MCP server's client ID and thatsuband roles are the user's. - Send the user's original token to the downstream API too. It should be refused, because its audience is your MCP server.
- With the flow inspector (Pro), open Flows: the exchange shows the subject token decoded with its signature check, never the encoded token. If the inspector recorded the user's sign-in, the exchange joins that flow.
Limitations and refusals
- Token exchange is a Generic-profile grant. Microsoft configurations use Entra's On-Behalf-Of grant (
urn:ietf:params:oauth:grant-type:jwt-bearer) instead, and Azure AD B2C configurations have neither. - The setup uses three clients, the client maximum of an unclaimed tenant. Claiming the tenant raises it.
audiencemust be a resource URI such asapi://forecast-api, not a client ID:audience=forecast-apigets400 invalid_targetlisting the known resources.- A user token addressed to another client gets
400 invalid_request, as RFC 8693 specifies, with a description naming every audience found and how to fix it. So does an expired token, an ID token, an app-only token or a token from another configuration. - A scope outside the exchanging client's allowed scopes, or a target it has no resource-qualified allowed scope for, gets
400 invalid_scope. - The issued token never outlives the user's token. Actor tokens, refresh tokens from an exchange and tokens from other issuers are not supported. With Validate client secrets on, a public client gets
401 invalid_client; with it off, as a testing shortcut, any registered client passes client authentication. - A user whose sign-in outcome refuses sign-in (Pro) can't be exchanged either:
400 invalid_grant. - Each exchange counts toward the tenant's token request limits.