Skip to main content
FastMCP provides flexible authentication options to secure your MCP server, from simple API keys to full OAuth 2.1 flows with pre-configured providers.

OAuth with Pre-configured Providers

The simplest way to add OAuth is using the auth option with a pre-configured provider:

Available Providers

Tool Authorization

Control which tools are available to authenticated users using the canAccess property with built-in helper functions:

Require Authentication

Require Specific Scopes

Require Specific Role

Combine Requirements

Access Session Data

Use getAuthSession 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 the authenticate 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:
This configuration automatically exposes OAuth discovery endpoints:
  • /.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