Authenticate your FastMCP client via OAuth 2.1.
New in version: 2.6.0
fastmcp.client.auth.OAuth
helper to simplify this entire process.
This flow is common for user-facing applications where the application acts on behalf of the user.
"oauth"
to the auth
parameter of the Client
or transport instance. FastMCP will automatically configure the client to use OAuth with default settings:
OAuth
HelperOAuth
helper and pass it to the auth
parameter of the Client
or transport instance. OAuth
manages the complexities of the OAuth 2.1 Authorization Code Grant with PKCE (Proof Key for Code Exchange) for enhanced security, and implements the full httpx.Auth
interface.
OAuth
Parametersmcp_url
(str
): The full URL of the target MCP server endpoint. Used to discover OAuth server metadatascopes
(str | list[str]
, optional): OAuth scopes to request. Can be space-separated string or list of stringsclient_name
(str
, optional): Client name for dynamic registration. Defaults to "FastMCP Client"
token_storage_cache_dir
(Path
, optional): Token cache directory. Defaults to ~/.fastmcp/oauth-mcp-client-cache/
additional_client_metadata
(dict[str, Any]
, optional): Extra metadata for client registrationClient
configured to use OAuth.
Token Check
token_storage_cache_dir
for existing, valid tokens for the target server. If one is found, it will be used to authenticate the client.OAuth Server Discovery
/.well-known/oauth-authorization-server
) based on the mcp_url
.Dynamic Client Registration
Local Callback Server
http://127.0.0.1:<port>/callback
) acts as the redirect_uri
for the OAuth flow.Browser Interaction
scopes
.Authorization Code & Token Exchange
authorization_code
. The client captures this code and exchanges it with the OAuth server’s token endpoint for an access_token
(and often a refresh_token
) using PKCE for security.Token Caching
token_storage_cache_dir
for future use, eliminating the need for repeated browser interactions.Authenticated Requests
Authorization
header for requests to the MCP server.Refresh Token
~/.fastmcp/oauth-mcp-client-cache/
and persist between application runs. Files are keyed by the OAuth server’s base URL.
FileTokenStorage
instance and call the clear
method:
clear_all
method on the FileTokenStorage
class: