Skip to main content

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:
  1. Receives authorization requests from MCP clients
  2. Redirects users to the external identity provider
  3. Exchanges the IdP’s code for tokens
  4. Maps external tokens/user info to your internal tokens
  5. Returns your tokens to the MCP client

OAuth Proxy

The OAuthProxy class handles the complete OAuth flow with external providers.

Basic Setup

Configuration

Token Mapper

The tokenMapper function is called after successful external authentication. Use it to create your internal tokens:

Pre-configured Providers

Import ready-to-use provider configurations:

Google

GitHub

Azure AD

GitLab

Slack

Discord


Custom Providers

Use customProvider for any OAuth 2.0 compatible identity provider:

Provider Configuration


OAuth Authorization Server

For full MCP OAuth compliance, use OAuthAuthorizationServer 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:
Response:

MCP Auth Error Responses

Use createAuthError from @leanmcp/core to return MCP-compliant authentication errors that trigger ChatGPT’s OAuth linking UI:
The 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: