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):

  1. 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.
  2. The MCP server's own client, weather-mcp: a confidential client with a secret. It exposes https://weather.example/mcp/tools.read, so the user's token has aud=https://weather.example/mcp and is addressed to it. Its allowed scopes list the downstream API's resource-qualified scopes, such as api://forecast-api/forecast.read.
  3. The downstream API, forecast-api: it exposes api://forecast-api/forecast.read, which makes api://forecast-api a 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

  1. 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.
  2. Check that act.sub is your MCP server's client ID and that sub and roles are the user's.
  3. Send the user's original token to the downstream API too. It should be refused, because its audience is your MCP server.
  4. 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

Test your MCP server's token exchange

Sign up Agent guide