OAuth Server & Proxy
The@leanmcp/auth/proxy and @leanmcp/auth/server modules enable you to build OAuth 2.1 authorization servers for your MCP applications. Use them to proxy authentication to external identity providers (Google, GitHub, etc.) while issuing your own tokens.
Features
External Provider Proxy
Authenticate users via Google, GitHub, Azure, and more
RFC 8414 Metadata
Standard OAuth authorization server metadata
RFC 7591 DCR
Dynamic Client Registration for MCP clients
PKCE Required
Enforces PKCE per MCP security requirements
Installation
Architecture Overview
The OAuth Proxy:- Receives authorization requests from MCP clients
- Redirects users to the external identity provider
- Exchanges the IdP’s code for tokens
- Maps external tokens/user info to your internal tokens
- Returns your tokens to the MCP client
OAuth Proxy
TheOAuthProxy class handles the complete OAuth flow with external providers.
Basic Setup
Configuration
Token Mapper
ThetokenMapper function is called after successful external authentication. Use it to create your internal tokens:
Pre-configured Providers
Import ready-to-use provider configurations:GitHub
Azure AD
GitLab
Slack
Discord
Custom Providers
UsecustomProvider for any OAuth 2.0 compatible identity provider:
Provider Configuration
OAuth Authorization Server
For full MCP OAuth compliance, useOAuthAuthorizationServer which adds RFC 8414 metadata and RFC 7591 Dynamic Client Registration:
Server Metadata (RFC 8414)
The server automatically exposes OAuth metadata at/.well-known/oauth-authorization-server:
Dynamic Client Registration (RFC 7591)
MCP clients can register dynamically:MCP Auth Error Responses
UsecreateAuthError from @leanmcp/core to return MCP-compliant authentication errors that trigger ChatGPT’s OAuth linking UI:
createAuthError function returns a response with _meta["mcp/www_authenticate"] that signals to MCP clients (including ChatGPT) to initiate OAuth authentication.
Complete Example
Here’s a complete OAuth proxy server:Related
- OAuth Client - Client-side OAuth with PKCE and token storage
- Authentication Overview - Server-side authentication with
@Authenticateddecorator - Auth & Payment Guide - Complete authentication integration guide