OAuth 2.1 Integration Guide for Claude MCP Clients
This guide explains how to integrate your MCP servers with Claude Desktop and other AI agents using OAuth 2.1 with PKCE.Overview
The platform provides a complete OAuth 2.1 authorization server that enables secure, user-authenticated access to MCP servers. This is the recommended method for AI agents like Claude Desktop to access your deployed APIs.Why OAuth 2.1?
- User Consent: Users explicitly authorize AI agents to access their MCP servers
- Security: PKCE (Proof Key for Code Exchange) prevents authorization code interception
- Standard Protocol: Works with any OAuth 2.1 compatible client
- Token Management: Automatic token expiration and refresh handling
- Multi-Device: Same credentials work across devices
Quick Start
Step 1: Register an OAuth Client
You have two options to register a client:Option A: Use the Dashboard (Recommended)
- Navigate to your deployment in the dashboard
- Find the “OAuth 2.1 Client” section
- Select your platform: Claude Desktop, Claude.ai (Web), or Both
- Click “Set Up OAuth”
- The system will automatically register the correct redirect URIs for your selected platform
- Note: No client secret is required as we use PKCE for enhanced security.
Option B: Use the API
Step 2: Configure Claude Desktop or Claude.ai
IMPORTANT: Remote MCP servers with OAuth must be configured via Settings > Connectors, not the configuration file.For Claude Desktop:
- Open Claude Desktop
- Go to Settings > Connectors
- Click “Add custom connector”
- Fill in the following information:
- Name:
Tydli - [your-deployment-slug] - MCP Server URL:
https://your-project.supabase.co/functions/v1/mcp-router/[your-deployment-slug] - OAuth Client ID: (your client ID from Step 1)
- Authorization URL:
https://your-project.supabase.co/functions/v1/mcp-oauth-server/authorize - Token URL:
https://your-project.supabase.co/functions/v1/mcp-oauth-server/token
- Name:
- Click Connect
For Claude.ai (Web):
- Open https://claude.ai/settings/connectors
- Click “Add custom connector”
- Fill in the same information as above
- Click Connect
Step 3: Authorize in Claude
- Restart Claude Desktop
- Claude will automatically detect the new MCP server
- You’ll be prompted to authorize access in your browser
- Sign in with your account credentials (email must be verified)
- Review the authorization request on the consent page
- Click “Approve” to grant Claude access
- Claude can now access your MCP server!
Redirect URI Configuration by Client Type
Understanding redirect URIs is critical for OAuth integration. Different AI agents handle redirects differently:Claude Desktop
Recommended URIs (register all):- Opens your browser for authorization automatically
- Listens for the OAuth callback on local ports (6277-6279 range)
- Exchanges the authorization code for tokens internally
- Manages token refresh automatically
- Remote MCP servers with OAuth must be configured via Settings > Connectors
- Do NOT use
claude_desktop_config.jsonfor remote OAuth servers - Register multiple redirect URIs (ports 6277-6279) for maximum compatibility
- Claude Desktop handles all redirect URI logic automatically
- Open Claude Desktop → Settings → Connectors
- Click “Add custom connector”
- Enter your MCP server URL and OAuth client ID
- Claude Desktop will prompt for authorization in your browser
Claude Web (claude.ai)
Note: Web integration requires partnership with Anthropic for Custom Connectors. Claude’s web application handles OAuth on Anthropic’s servers. When integrated:- Redirect URIs are managed by Anthropic’s infrastructure
- Users authorize through Anthropic’s OAuth consent flow
- You’ll need to work with Anthropic to set up the integration
- Typical redirect URI format:
https://claude.ai/oauth/callback
Custom Web Applications
For your own web application integrating with MCP servers: Development:- HTTPS required for all non-localhost URIs
- Must be an exact match (no wildcards)
- Maximum 10 redirect URIs per OAuth client
- Each URI must be ≤500 characters
Other MCP Clients
For generic MCP clients or custom implementations:- Follow the OAuth 2.1 with PKCE specification
- Use
http://localhostorhttp://127.0.0.1with any available port for local testing - Implement a callback handler that listens on your redirect URI
- Exchange the authorization code using PKCE code_verifier
Mobile-Responsive Authorization Flow
The OAuth server provides a fully mobile-responsive authorization flow:Login Page
- Clean, modern design that works on all devices
- Email and password authentication
- Clear error messages
- Automatic focus on email field
- Secure password input
Consent Page
- Shows which application is requesting access
- Lists requested permissions
- User email displayed
- Clear “Approve” and “Deny” buttons
- Warning about trusting applications
- Smartphones (320px+)
- Tablets (768px+)
- Desktop (1024px+)
OAuth Flow Diagram
Protected Resource Metadata (RFC 9728)
Tydli implements RFC 9728 OAuth 2.0 Protected Resource Metadata for automatic service discovery. This allows OAuth clients to discover authorization server endpoints and supported authentication methods without manual configuration.Discovery Endpoint
Endpoint:GET /.well-known/oauth-protected-resource
Base URL: Your MCP server URL (e.g., https://your-project.supabase.co/functions/v1/mcp-router/your-slug)
Example Request
Example Response
Response Fields
| Field | Description |
|---|---|
resource | The protected resource identifier (your MCP server URL) |
authorization_servers | Array of OAuth 2.0 authorization server URLs |
bearer_methods_supported | Supported token transmission methods (header, body, query) |
resource_documentation | URL to resource documentation |
scopes_supported | Array of OAuth scopes this resource accepts |
Use Cases
1. Client Discovery OAuth clients can automatically discover where to send authorization requests:MCP Specification Compliance
✅ REQUIRED Features Implemented:- RFC 9728 Protected Resource Metadata endpoint
- RFC 8414 Authorization Server Metadata (
/.well-known/oauth-authorization-server) - RFC 8707 Resource Indicators (audience binding)
- OAuth 2.1 with PKCE (S256 mandatory)
- WWW-Authenticate header on 401 responses
- Token audience validation
- Dynamic Client Registration (RFC 7591)
- State parameter validation (CSRF protection)
- Refresh token rotation for public clients
- Tokens bound to specific MCP servers (no token passthrough)
- Authorization codes expire in 10 minutes
- Access tokens expire in 1 hour
- Refresh tokens expire in 90 days
- HTTPS enforced (except localhost)
- Exact redirect URI matching
Authorization Server Metadata
For completeness, the authorization server also exposes metadata: Endpoint:GET /.well-known/oauth-authorization-server
Security Best Practices
For OAuth Clients
-
Store Client Secrets Securely
- Never commit client secrets to version control
- Use environment variables or secure credential storage
- Rotate secrets if compromised
-
Use PKCE Correctly
- Generate cryptographically secure code verifiers (43-128 chars)
- Use S256 challenge method (SHA-256)
- Never reuse code verifiers
-
Validate Redirect URIs
- Use exact URI matches (no wildcards)
- Use HTTPS in production (HTTP only for localhost)
- Validate state parameter to prevent CSRF
For Users
-
Only Authorize Trusted Applications
- Review what permissions are being requested
- Check the application name and redirect URI
- Deny access if unsure
-
Keep Credentials Secure
- Use strong passwords
- Don’t share your account credentials
- Log out when finished
-
Monitor Access
- Review authorized applications regularly
- Revoke access for unused applications
- Check deployment logs for suspicious activity
Rate Limits
The OAuth server enforces rate limits to prevent abuse:| Endpoint | Limit | Window |
|---|---|---|
/register | 20 requests | Per hour per IP |
/authorize | 20 requests | Per hour per IP |
/token | 10 requests | Per minute per client |
- Free Plan: 20 requests/hour, 1000 requests/month
- Pro Plan: Higher limits (check your plan details)
Troubleshooting
”Invalid client_id”
- Verify your client ID is correct
- Check if the client was registered successfully
- Ensure you’re using the correct OAuth server URL
”Invalid redirect_uri”
- Redirect URI must exactly match registered URI
- Check for trailing slashes
- Verify HTTP vs HTTPS protocol
”Invalid code_verifier”
- Code verifier must match the code challenge
- Ensure you’re using S256 hashing
- Don’t reuse code verifiers
”Access token expired”
- Access tokens expire after 1 hour
- Use refresh token to get new access token (see “Using Refresh Tokens” below)
- Client should automatically request new token
- Check client’s token refresh implementation
”Rate limit exceeded”
- Wait for the specified retry period
- Reduce request frequency
- Consider upgrading your plan for higher limits
Using Refresh Tokens
Refresh tokens allow clients to obtain new access tokens without requiring user re-authentication, providing a seamless experience for long-running applications.Token Lifetimes
- Access Token: 1 hour (3600 seconds)
- Refresh Token: 90 days (7,776,000 seconds)
How It Works
-
During initial authorization (via
/tokenwithauthorization_codegrant), you receive both:access_token(short-lived, 1 hour)refresh_token(long-lived, 90 days)
- When the access token expires, use the refresh token to get a new access token
- The refresh token remains valid and can be reused multiple times (until it expires or is revoked)
Automatic Refresh (Recommended)
Most OAuth clients (like Claude Desktop when configured via Settings > Connectors) handle token refresh automatically:- Client detects access token expiration (via 401 response or checking
expires_in) - Client sends
refresh_tokengrant request to/tokenendpoint - Server validates refresh token and returns new access token
- Client continues without user intervention
Manual Refresh Example
If you’re implementing your own client, here’s how to manually refresh: Request:Implementation Example (JavaScript)
Best Practices
- Store Securely: Refresh tokens are long-lived and powerful - store them encrypted at rest
- Refresh Proactively: Refresh before expiration (e.g., when 90% of token lifetime has passed)
- Handle Failures: If refresh fails, prompt user to re-authenticate
- Revoke on Logout: Always revoke refresh tokens when user logs out
- One Refresh Token Per Client: Each device/client should have its own refresh token
- Monitor Usage: Check deployment logs for suspicious refresh patterns
Error Handling
Common refresh token errors:| Error | Reason | Solution |
|---|---|---|
invalid_grant | Refresh token expired, revoked, or invalid | Re-authenticate user |
invalid_client | Client ID doesn’t match | Verify client credentials |
invalid_request | Missing required parameters | Check request format |
Revoking Refresh Tokens
To revoke a refresh token (e.g., on logout):- Refresh token becomes invalid immediately
- Associated access tokens remain valid until expiration
- User must re-authorize to get new tokens
Security Considerations
- Never expose refresh tokens in URLs - always use request body
- Validate client_id on refresh - ensures token isn’t stolen across clients
- Rate limit refresh requests - current limit is 10/minute per client
- Monitor for abuse - unusual refresh patterns may indicate token theft
- Rotate on suspicious activity - revoke and require re-auth if compromise suspected
Advanced Configuration
Custom Scopes
Request specific scopes during authorization:openid: OpenID Connect authenticationemail: Access to user emailprofile: Access to user profile information
Token Introspection
Check token validity programmatically:Revoking Access
Revoke an access token:Testing Your Integration
1. Test Client Registration
2. Test Authorization Flow
Open in browser:3. Test Token Exchange
4. Test MCP Server Access
Migration from JWT to OAuth
If you’re currently using JWT authentication, here’s how to migrate:- Create OAuth Client: Register a new client for your application
- Update Configuration: Switch from JWT to OAuth in your client config
- Test Authorization: Verify the OAuth flow works correctly
- Update Documentation: Update any integration docs or scripts
- Deprecate JWT: Remove JWT-based access after migration complete
Support
Need help with OAuth integration?- Check the API Reference for detailed endpoint docs
- Review the Troubleshooting Guide
- Contact support with your deployment ID for assistance