HuggingFaceProvider uses FastMCP’s OAuth Proxy
pattern with Hugging Face’s OAuth and OpenID Connect endpoints. It works with
manually created confidential apps, public PKCE apps, and Client ID Metadata
Documents (CIMD).
When deploying your MCP server to Hugging Face Spaces, Spaces can create and
manage the OAuth app for you.
Configuration
Prerequisites
Before you begin, you will need:- A Hugging Face account with access to create OAuth apps
- Your FastMCP server’s URL (can be localhost for development, e.g.,
http://localhost:8000)
Step 1: Create a Hugging Face OAuth app
Create an OAuth app from your Hugging Face application settings. For details, see Hugging Face’s OAuth documentation.1
Create the OAuth app
Go to your Hugging Face application settings
and create a new OAuth application.Choose a name users will recognize, then configure the redirect URL for
your FastMCP server:
- Development:
http://localhost:8000/auth/callback - Production:
https://your-domain.com/auth/callback
2
Save your credentials
After creating the app, save:
- Client ID: The public identifier for your Hugging Face OAuth app
- Client Secret: The app secret, if you created a confidential app
Step 2: Configure FastMCP
server.py
Public OAuth apps, DCR, and CIMD
Hugging Face supports public OAuth apps (no client secret). For public apps, omitclient_secret and provide a jwt_signing_key so FastMCP can sign its
own proxy tokens:
HuggingFaceProvider inherits FastMCP’s OAuth Proxy behavior, which handles
client registration locally and forwards authorization to Hugging Face using
your configured Hugging Face OAuth app. In other words, MCP clients register
with FastMCP, while FastMCP uses your Hugging Face client_id and optional
client_secret for the upstream OAuth flow.
You can also use a Client ID Metadata Document URL as the client_id when your
client metadata is hosted at a stable HTTPS URL:
Testing
Running the Server
Start your server with HTTP transport:Testing with a Client
Create a test client that authenticates with your Hugging Face-protected server:test_client.py
- Your browser will open to Hugging Face’s authorization page
- Sign in with your Hugging Face account and grant the requested permissions
- After authorization, you’ll be redirected back
- The client receives the token and can make authenticated requests
The client caches tokens locally, so you won’t need to re-authenticate for
subsequent runs unless the token expires or you explicitly clear the cache.
Hugging Face Spaces
When deploying to Hugging Face Spaces, Spaces can create and manage the OAuth app for you. Add OAuth metadata to your Space README:OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_SCOPES,
OPENID_PROVIDER_URL, and SPACE_HOST environment variables:
JWT_SIGNING_KEY as a Space secret.
Hugging Face scopes
The default scopes areopenid and profile. Add more scopes when your tools
need Hub capabilities:
orgIds
authorization parameter. The value is the organization ID from the
organizations.sub field in the Hugging Face userinfo response:
Production Configuration
For production deployments with persistent token management across server restarts, configurejwt_signing_key and client_storage:
server.py
Parameters (
jwt_signing_key and client_storage) work together to ensure
tokens and client registrations survive server restarts. Wrap your storage in
FernetEncryptionWrapper to encrypt sensitive OAuth tokens at rest - without
it, tokens are stored in plaintext. Store secrets in environment variables and
use a persistent storage backend like Redis for distributed deployments.For complete details on these parameters, see the OAuth Proxy documentation.
