OAuth Flow
The OAuth flow is triggered when you call an MCP server that requires user authentication.Request Without Token
The SDK calls the MCP server. If no valid token exists, the server returns
401 with a WWW-Authenticate header.Protected Resource Discovery
The SDK fetches
/.well-known/oauth-protected-resource (RFC 9728) to discover the authorization server and supported scopes.AuthenticationError
The SDK raises
AuthenticationError containing a connect_url—the full OAuth authorization URL.Browser Interaction
Your app opens the user’s browser to the
connect_url. The user logs in and grants (or denies) the requested scopes.Token Exchange
Upon approval, the authorization server exchanges the authorization code for tokens using PKCE. DAuth stores the tokens server-side.
Retry Request
The user returns to your app and triggers a retry. The SDK re-sends the request, now with valid credentials.
Authenticated Requests
The access token is automatically included for subsequent requests to the MCP server.
How It Works
OAuth Retry Helper
Handle the OAuth flow with a retry wrapper:Full Example: DedalusRunner
Full Example: Raw Client
For single requests with full control over API response:Environment
When to Use
OAuth works for:- User-facing applications
- Delegated access (acting on behalf of users)
- Services like Gmail, Google Calendar, Linear, GitHub
- API keys and service tokens
- Backend integrations without user context
- Service-to-service calls