@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 variableConcurrency Safe
Uses AsyncLocalStorage for request-isolated context
Installation
Provider Dependencies
- AWS Cognito
- Clerk
- Auth0
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
- AWS Cognito
- Clerk
- Auth0
Controlling User Fetch
Supported Providers
AWS Cognito
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
Security
Security
- Always use HTTPS in production
- Store tokens securely (keychain, encrypted storage)
- Implement token refresh before expiration
- Add rate limiting to protect against brute force
Configuration
Configuration
- Use environment variables for credentials
- Never hardcode secrets in code
- Use
_metafor auth, not business arguments
Performance
Performance
- Use
getUser: falsewhen 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
Related Packages
- @leanmcp/core - Core decorators and server functionality
- @leanmcp/env-injection - Environment variable injection for user secrets
- OAuth Client - Client-side OAuth with PKCE and token storage
- OAuth Server & Proxy - Authorization servers with external provider support