Architecture Overview
The OAuth implementation uses:- Client-side state machine for flow orchestration
 - CORS proxy server for metadata discovery
 - localStorage for persistence
 - MCP SDK for OAuth operations
 
Complete OAuth Flow
The following diagram shows the complete OAuth flow from user initiation through token exchange:State Machine Transitions
The OAuth flow is managed by a state machine with 6 distinct states:Data Persistence (localStorage)
All OAuth data is persisted in the browser’s localStorage with server-specific keys:CORS Proxy Architecture
To bypass CORS restrictions when fetching OAuth metadata, we use a proxy server:Key Components
1. MCPOAuthProvider
Location:client/src/lib/mcp-oauth.ts:72
Implements the OAuthClientProvider interface from the MCP SDK:
2. OAuth State Machine
Location:client/src/lib/oauth-state-machine.ts:353
Manages the step-by-step OAuth flow:
- Each step has 
canTransitionandexecutemethods - Handles errors and retries automatically
 - Updates UI state via callbacks
 - Validates preconditions before transitions
 
3. CORS Proxy
Location:server/routes/mcp/oauth.ts:10
Proxies OAuth metadata requests to bypass CORS:
- Enforces HTTPS for security
 - Adds proper CORS headers
 - Validates URL format
 - Returns OAuth metadata JSON
 
4. Fetch Interceptor
Location:client/src/lib/mcp-oauth.ts:17
Intercepts and proxies OAuth metadata requests:
OAuth Flow States
Fromclient/src/lib/oauth-flow-types.ts:10-16:
| State | Description | 
|---|---|
metadata_discovery | Discover OAuth & resource metadata from server | 
client_registration | Register client (DCR) or use pre-configured | 
authorization_redirect | Generate auth URL with PKCE | 
authorization_code | Wait for user authorization | 
token_request | Exchange code for tokens | 
complete | OAuth flow finished | 
Security Features
PKCE
Code verifier stored at 
oauth-state-machine.ts:248 for secure authorization without client secretsCSRF Protection
Random state parameter generated at 
oauth-state-machine.ts:231-236HTTPS Enforcement
Proxy validates HTTPS at 
oauth.ts:22Scope Validation
Uses advertised scopes from metadata at 
oauth-state-machine.ts:222-229Error Handling
Each state handles errors gracefully:- Stores error in 
latestErrorstate field - Updates 
statusMessagewith user-friendly error - Provides helpful error messages for common issues:
invalid_client- Client ID verification failedunauthorized_client- Client not authorized for this serverinvalid_grant- Authorization code expired or invalid
 - Allows retry from current step without restarting flow
 
Token Management
1
Storage
Tokens stored in localStorage with server-specific keys
2
Refresh
Automatic token refresh using refresh_token at 
mcp-oauth.ts:4643
Usage
Tokens added as Bearer token in Authorization header at 
mcp-oauth.ts:577-5804
Cleanup
clearOAuthData() removes all OAuth-related localStorage entriesUsage Example
File References
| Component | Location | 
|---|---|
| OAuth Flow Types | client/src/lib/oauth-flow-types.ts | 
| State Machine | client/src/lib/oauth-state-machine.ts | 
| OAuth Provider | client/src/lib/mcp-oauth.ts | 
| CORS Proxy | server/routes/mcp/oauth.ts | 
The OAuth implementation follows the MCP specification and uses PKCE for
enhanced security. All sensitive data is stored in localStorage and never sent
to unauthorized servers.

