OAuthProxy class provides a transparent OAuth 2.1 proxy that acts as an intermediary between MCP clients and upstream OAuth providers. It implements Dynamic Client Registration (DCR) and supports both token swap and pass-through patterns.
Constructor
Create a new OAuthProxy instance.Configuration
Base URL of the proxy server (e.g., “https://api.example.com”)
Upstream OAuth provider’s authorization endpoint URL
Upstream OAuth provider’s token endpoint URL
Pre-registered client ID with upstream provider
Pre-registered client secret with upstream provider
OAuth scopes to request from upstream provider
Enable token swap pattern (issues short-lived JWTs instead of passing through upstream tokens)
- When
true: Issues short-lived FastMCP JWTs and stores upstream tokens securely - When
false: Returns upstream tokens directly to clients
Require user consent screen before authorizing
OAuth callback path (relative to baseUrl)
Allowed redirect URI patterns for client registration (supports wildcards)
upstreamTokenEndpointAuthMethod
'client_secret_basic' | 'client_secret_post'
default:"client_secret_basic"
Authentication method for upstream token endpoint
client_secret_basic: Credentials in Authorization header (RFC 6749 Section 2.3.1)client_secret_post: Credentials in request body
Access token TTL in seconds (for token swap mode)
Refresh token TTL in seconds (30 days, for token swap mode)
Authorization code TTL in seconds (5 minutes)
OAuth transaction TTL in seconds (10 minutes)
Secret key for signing JWTs (auto-generated if not provided, required for token swap mode)
Encryption key for token storage (auto-generated if not provided, set to false to disable)
Secret key for signing consent cookies (auto-generated if not provided)
Custom token storage backend (defaults to encrypted MemoryTokenStorage)
Extract custom claims from upstream tokens and include them in proxy JWTs
Forward client’s PKCE to upstream provider (experimental)
Methods
registerClient()
Handle Dynamic Client Registration (RFC 7591) request.authorize()
Handle OAuth authorization request.HTTP redirect response (302) to upstream provider or consent screen
handleCallback()
Handle OAuth callback from upstream provider.Web API Request object with callback parameters
HTTP redirect response (302) to client callback URL with authorization code
handleConsent()
Handle user consent form submission.Web API Request object with form data
HTTP redirect response based on user action
exchangeAuthorizationCode()
Exchange authorization code for access token.exchangeRefreshToken()
Refresh access token using refresh token.New access token and optionally rotated refresh token
loadUpstreamTokens()
Load upstream tokens from a FastMCP JWT (token swap mode only).FastMCP JWT access token from token swap
Upstream token set or null if invalid/expired
getAuthorizationServerMetadata()
Get OAuth Authorization Server metadata (RFC 8414).destroy()
Stop cleanup interval and destroy resources.Token Swap Pattern
WhenenableTokenSwap: true (default), the proxy uses a secure token swap pattern:
- Client authorizes: Client gets authorization code from proxy
- Code exchange: Proxy exchanges code with upstream provider
- Upstream tokens stored: Proxy securely stores upstream tokens (encrypted)
- FastMCP JWTs issued: Proxy issues short-lived JWTs to client
- JWT mapping: JWTs contain JTI that maps to upstream tokens
- Token refresh: Client refreshes FastMCP JWT, proxy refreshes upstream tokens
- Security: Upstream tokens never leave the proxy
- Short-lived: Client tokens expire quickly (default 1 hour)
- Auditable: All token usage tracked through proxy
- Claims extraction: Custom claims from upstream tokens included in JWTs
Pass-through Pattern
WhenenableTokenSwap: false, the proxy acts as a transparent pass-through:
- Client authorizes: Client gets authorization code from proxy
- Code exchange: Proxy exchanges code with upstream provider
- Upstream tokens returned: Proxy returns upstream tokens directly to client
- Direct API access: Client uses upstream tokens to call APIs directly
- Simplicity: No token mapping or storage
- Standard OAuth: Clients use standard upstream tokens
- Long-lived: Tokens live as long as upstream provider allows