Token exchange for tools

From RedHatProductSecurity/prodsec-skills · View interactive version

Token exchange for tools is an AI agent skill from the GitHub repository RedHatProductSecurity/prodsec-skills. Implement OAuth Token Exchange in MCP servers for secure downstream API access. Use when designing or reviewing how MCP servers authenticate to third-party APIs or web tools.

How to use this skill

Skill file

Token Exchange for MCP Server Tools

Security Recommendation

When an MCP server needs to access third-party APIs on behalf of a user, the preferred approach is to use SPIFFE/SPIRE+mTLS. When that is not feasible, the MCP server SHOULD perform an OAuth 2.0 Token Exchange (RFC 8693) to swap the user's original token for a scoped, short-lived token.

Why Token Exchange

Token Exchange provides:

Token Exchange Flow

1. User authenticates to MCP server (user gets token A)
2. MCP server needs to call third-party API
3. MCP server sends Token Exchange request to authorization server:
   - subject_token: user's token A
   - subject_token_type: urn:ietf:params:oauth:token-type:access_token
   - requested_token_type: urn:ietf:params:oauth:token-type:access_token
   - scope: only the scopes needed for the third-party API
   - actor_token: MCP server's own credential (optional)
4. Authorization server returns token B with:
   - sub: original user identity
   - act: { sub: MCP server identity }
   - scope: reduced scopes
   - exp: short TTL
5. MCP server calls third-party API with token B

Resulting Token Structure

{
  "sub": "user-123",
  "act": {
    "sub": "mcp-server-tools-xyz"
  },
  "scope": "read:specific-resource",
  "exp": 1700000000,
  "iss": "https://auth.example.com"
}

Implementation Checklist

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.