Server Authentication
Add enterprise-grade OAuth 2.0/2.1 authentication to your MCP server with built-in support for popular identity providers. Secure your tools with bearer token authentication, implement role-based access control (RBAC), and access authenticated user information in your tool callbacks.Quick Start
Basic OAuth Server
OAuth Providers
mcp-use includes built-in support for major identity providers:Auth0
Full OAuth 2.1 with PKCE, JWKS verification, and custom claims.WorkOS
Direct mode OAuth where clients authenticate directly with WorkOS (recommended for enterprise SSO).Supabase
Authentication for Supabase projects with support for both HS256 and ES256 tokens.Keycloak
Enterprise SSO with realm roles and client-specific permissions.Custom Provider
Use any OAuth provider with custom JWT verification logic.OAuth Modes
mcp-use supports two OAuth modes:Proxy Mode (Default)
The MCP server proxies OAuth requests. Clients authenticate through your server’s endpoints. When to use:- Simple setup and deployment
- Full control over OAuth flow
- Single authentication endpoint
- Client requests authorization →
http://your-server.com/authorize - User authenticates with provider
- Server exchanges code for token →
http://your-server.com/token - Client uses bearer token for MCP requests
Direct Mode
Clients communicate directly with the auth provider. Your server only verifies bearer tokens. When to use:- Enterprise SSO requirements
- Reduce server load
- Provider requires direct authentication (e.g., WorkOS)
- Client requests authorization →
https://auth-provider.com/authorize - User authenticates with provider
- Client exchanges code for token →
https://auth-provider.com/token - Client uses bearer token for MCP requests → Your server verifies
Accessing User Context
Once OAuth is configured, all tool callbacks receive authenticated user information via the context parameter:User Info Type
Role-Based Access Control (RBAC)
Implement role-based access control by checking user roles in your tools:Create a Reusable Authorization Middleware
OAuth Endpoints
When OAuth is configured, your server automatically exposes these endpoints:Authorization Endpoint
response_type=code- Response typeclient_id- OAuth client IDredirect_uri- Callback URLscope- Requested scopesstate- CSRF protection tokencode_challenge- PKCE challengecode_challenge_method=S256- PKCE method
Token Endpoint
grant_type=authorization_code- Grant typecode- Authorization coderedirect_uri- Callback URLclient_id- OAuth client IDcode_verifier- PKCE verifier
Discovery Endpoints
Bearer Token Authentication
All/mcp/* endpoints require a valid bearer token when OAuth is configured:
Security Best Practices
JWT Verification
Always enable JWT verification in production:Environment Variables
Never hardcode credentials. Use environment variables:CORS Configuration
Configure CORS appropriately for your OAuth clients:Token Expiration
Implement token refresh logic in your clients to handle expired tokens gracefully.Rate Limiting
Consider adding rate limiting to prevent abuse:Testing OAuth
Development Mode
Skip JWT verification during development:verifyJwt: false to production!
MCP Inspector
Test your OAuth server using the built-in MCP Inspector:Manual Testing
Generate a test token from your provider’s dashboard and use it directly:Examples
Complete Auth0 Server
Troubleshooting
”Invalid token” errors
Symptoms: 401 Unauthorized responses withinvalid_token error
Solutions:
- Verify token is correctly formatted in Authorization header
- Check token hasn’t expired
- Ensure OAuth provider domain/issuer is correct
- Verify audience matches between client and server
- Check JWT signature verification is working
”Forbidden” errors despite valid token
Symptoms: Token verifies but role/permission checks fail Solutions:- Verify roles/permissions are included in JWT claims
- Check custom claim configuration in OAuth provider
- Ensure role claim path matches your extraction logic
- Use Inspector to view decoded token contents
OAuth discovery fails
Symptoms: Clients can’t discover OAuth endpoints Solutions:- Verify server is accessible at the configured URL
- Check CORS configuration allows OPTIONS requests
- Ensure
/.well-known/*endpoints are accessible - Try accessing discovery endpoint directly in browser
PKCE errors
Symptoms: “Invalid code verifier” or PKCE-related errors Solutions:- Ensure client supports PKCE
- Verify code_challenge and code_verifier match
- Check code hasn’t been used already
- Verify code hasn’t expired (typically 10 minutes)
Next Steps
- Client Authentication - Connect to OAuth servers from clients
- useMcp Hook - React hook with OAuth support
- Tools - Define tools with user context
- Examples - Complete OAuth examples