OAuth with Pre-configured Providers
The simplest way to add OAuth is using theauth option with a pre-configured provider:
Available Providers
Tool Authorization
Control which tools are available to authenticated users using thecanAccess property with built-in helper functions:
Require Authentication
Require Specific Scopes
Require Specific Role
Combine Requirements
Access Session Data
UsegetAuthSession for type-safe access to the OAuth session in your tool execute functions:
You can also access
session.accessToken directly, but you must handle the case where session is undefined. The getAuthSession helper throws a clear error if the session is not authenticated, making it safer when used with canAccess: requireAuth.Custom Authentication
For non-OAuth scenarios like API keys or custom tokens, use theauthenticate option:
OAuth Discovery Endpoints
FastMCP supports OAuth discovery endpoints for direct integration with OAuth providers, supporting both MCP Specification 2025-03-26 and MCP Specification 2025-06-18:/.well-known/oauth-authorization-server- Authorization server metadata (RFC 8414)/.well-known/oauth-protected-resource- Protected resource metadata (RFC 9728)/.well-known/oauth-protected-resource<endpoint>- Protected resource metadata at sub-path
Helper Functions Reference
Next Steps
OAuth Proxy
Learn about the built-in OAuth Proxy with DCR, PKCE, and token swap
Custom Routes
Add authenticated custom HTTP routes to your server