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
string
required
Base URL of the proxy server (e.g., “https://api.example.com”)
string
required
Upstream OAuth provider’s authorization endpoint URL
string
required
Upstream OAuth provider’s token endpoint URL
string
required
Pre-registered client ID with upstream provider
string
required
Pre-registered client secret with upstream provider
string[]
OAuth scopes to request from upstream provider
boolean
default:"true"
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
boolean
default:"true"
Require user consent screen before authorizing
string
default:"/oauth/callback"
OAuth callback path (relative to baseUrl)
string[]
default:"[\"https://*\", \"http://localhost:*\"]"
Allowed redirect URI patterns for client registration (supports wildcards)
'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
number
default:"3600"
Access token TTL in seconds (for token swap mode)
number
default:"2592000"
Refresh token TTL in seconds (30 days, for token swap mode)
number
default:"300"
Authorization code TTL in seconds (5 minutes)
number
default:"600"
OAuth transaction TTL in seconds (10 minutes)
string
Secret key for signing JWTs (auto-generated if not provided, required for token swap mode)
string | false
Encryption key for token storage (auto-generated if not provided, set to false to disable)
string
Secret key for signing consent cookies (auto-generated if not provided)
TokenStorage
Custom token storage backend (defaults to encrypted MemoryTokenStorage)
boolean | CustomClaimsPassthroughConfig
default:"true"
Extract custom claims from upstream tokens and include them in proxy JWTs
boolean
default:"false"
Forward client’s PKCE to upstream provider (experimental)
Methods
registerClient()
Handle Dynamic Client Registration (RFC 7591) request.DCRRequest
required
DCRResponse
authorize()
Handle OAuth authorization request.AuthorizationParams
required
Response
HTTP redirect response (302) to upstream provider or consent screen
handleCallback()
Handle OAuth callback from upstream provider.Request
required
Web API Request object with callback parameters
Response
HTTP redirect response (302) to client callback URL with authorization code
handleConsent()
Handle user consent form submission.Request
required
Web API Request object with form data
Response
HTTP redirect response based on user action
exchangeAuthorizationCode()
Exchange authorization code for access token.TokenRequest
required
TokenResponse
exchangeRefreshToken()
Refresh access token using refresh token.RefreshRequest
required
TokenResponse
New access token and optionally rotated refresh token
loadUpstreamTokens()
Load upstream tokens from a FastMCP JWT (token swap mode only).string
required
FastMCP JWT access token from token swap
UpstreamTokenSet | null
Upstream token set or null if invalid/expired
getAuthorizationServerMetadata()
Get OAuth Authorization Server metadata (RFC 8414).object
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