Skip to main content

@leanmcp/auth

Authentication module for LeanMCP providing token-based authentication decorators and multi-provider support for protecting MCP tools, prompts, and resources.

Features

@Authenticated Decorator

Protect tools, prompts, and resources with a simple decorator

Multi-Provider Support

AWS Cognito, Clerk, Auth0, and LeanMCP providers

Automatic authUser

Decoded user info injected as global authUser variable

Concurrency Safe

Uses AsyncLocalStorage for request-isolated context

Installation

Provider Dependencies

Quick Start

1. Initialize Auth Provider

2. Protect Methods with @Authenticated

3. Protect Entire Service

The authUser Variable

When using @Authenticated, a global authUser variable is automatically injected containing the decoded JWT payload:

Provider-Specific User Data

Controlling User Fetch

Supported Providers

AWS Cognito

Environment Variables:

Clerk

Auth0

LeanMCP

For LeanMCP platform deployments with user secrets support:

Client Usage

Authentication tokens are passed via the _meta field following MCP protocol standards:

Error Handling

API Reference

AuthProvider

@Authenticated Decorator

AuthenticationError

Helper Functions

Best Practices

  • Always use HTTPS in production
  • Store tokens securely (keychain, encrypted storage)
  • Implement token refresh before expiration
  • Add rate limiting to protect against brute force
  • Use environment variables for credentials
  • Never hardcode secrets in code
  • Use _meta for auth, not business arguments
  • Use getUser: false when you only need token validation
  • JWKS keys are cached automatically for performance

OAuth 2.1 Support

Beyond server-side token verification, @leanmcp/auth provides complete OAuth 2.1 infrastructure:

OAuth Client

Browser-based OAuth flows with PKCE, token storage, and automatic refresh

OAuth Server & Proxy

Build authorization servers with external provider proxy support

Submodule Imports