Secure your FastMCP server’s HTTP endpoints by validating JWT Bearer tokens.
New in version: 2.6.0
Authorization
header with the “Bearer” scheme. The server then validates this token to grant or deny access.
FastMCP supports Bearer Token authentication for its HTTP-based transports (http
and sse
), allowing you to protect your server from unauthorized access.
BearerAuthProvider
class. This provider validates incoming JWTs by verifying signatures, checking expiration, and optionally validating claims.
BearerAuthProvider
validates tokens; it does not issue them (or implement any part of an OAuth flow). You’ll need to generate tokens separately, either using FastMCP utilities or an external Identity Provider (IdP) or OAuth 2.1 Authorization Server.BearerAuthProvider
instance and pass it to the auth
parameter of the FastMCP
instance.
The BearerAuthProvider
requires either a static public key or a JWKS URI (but not both!) in order to verify the token’s signature. All other parameters are optional — if they are provided, they will be used as additional validation criteria.
jwks_uri
is not providedpublic_key
is not providediss
claim valueaud
claim valueBearerAuthProvider
as a string.
RSAKeyPair
utility class to generate tokens without needing an external OAuth provider.
RSAKeyPair
utility is intended for development and testing only. For production, use a proper OAuth 2.1 Authorization Server or Identity Provider.create_token()
method accepts these parameters:
get_access_token()
dependency function: