mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-14 09:39:11 +02:00
| .. | ||
| client.py | ||
| README.md | ||
| server.py | ||
WorkOS OAuth Example
This example demonstrates how to use the WorkOS OAuth provider with FastMCP servers.
Overview
The WorkOS OAuth provider enables authentication using WorkOS User Management. It provides general OAuth2 authentication similar to GitHub or Google, with optional support for enterprise SSO connections. Unlike the AuthKit provider which uses DCR (Dynamic Client Registration), this provider works with traditional OAuth flows.
Setup
1. WorkOS Configuration
-
Create a WorkOS Application:
- Go to WorkOS Dashboard → Applications
- Create a new application or use an existing one
- Enable User Management for OAuth authentication
- Copy your
Client IDandAPI Key(client secret)
-
Configure SSO Connection (optional for enterprise SSO):
- Go to WorkOS Dashboard → Connections
- Set up your SSO connection (SAML, OIDC, or OAuth provider like Google/Microsoft)
- Note the
Organization IDorConnection IDif using SSO
-
Set Redirect URLs:
- In your WorkOS application settings, add redirect URLs for your OAuth flow
- For this example:
http://localhost:8000/auth/callback
2. Environment Variables
Create a .env file in this directory:
# Required WorkOS credentials
WORKOS_CLIENT_ID=client_123
WORKOS_API_KEY=sk_test_456 # Your WorkOS API key (client secret)
# Server URL (optional, defaults to http://localhost:8000)
# WORKOS_BASE_URL=http://localhost:8000
# Optional: For enterprise SSO connections
# WORKOS_ORGANIZATION_ID=org_123 # Route to specific organization's SSO
# WORKOS_CONNECTION_ID=conn_456 # Route to specific SSO connection
# Optional: Required scopes
# FASTMCP_SERVER_AUTH_WORKOS_REQUIRED_SCOPES=["profile", "email"]
3. Install Dependencies
cd /Users/jlowin/Developer/fastmcp
uv sync
Running the Example
Start the Server
# From this directory
uv run python server.py
The server will start on http://localhost:8000 with WorkOS OAuth authentication enabled.
Test with Client
In another terminal:
# From this directory
uv run python client.py
The client will:
- Attempt to connect to the server
- Detect that OAuth authentication is required
- Open a browser for WorkOS authentication
- Complete the OAuth flow and connect to the server
- Demonstrate calling authenticated tools
How It Works
Authentication Flow
- Client Request: Client attempts to connect to FastMCP server
- Auth Challenge: Server responds with
401 UnauthorizedandWWW-Authenticateheader - OAuth Discovery: Client discovers OAuth endpoints from server metadata
- Authorization: Client redirects user to WorkOS for authentication
- Callback: WorkOS redirects back with authorization code
- Token Exchange: Client exchanges code for access token
- API Calls: Client uses access token for authenticated MCP requests
Server Components
- WorkOSProvider: Validates tokens using WorkOS User Management API
- Protected Resources: MCP tools and resources require valid WorkOS tokens
- OAuth Metadata: Server advertises WorkOS as authorization server
Client Components
- OAuth Client: Handles browser-based OAuth flow
- Token Storage: Caches tokens for future use
- Automatic Auth: Transparently handles authentication
Key Features
- SSO Integration: Works with any WorkOS SSO connection
- User Management: Validates tokens against WorkOS User Management API
- Token Caching: Reuses tokens across sessions
- Error Handling: Graceful handling of auth failures and token expiration
Troubleshooting
Common Issues
- "Invalid client" error: Check CLIENT_ID and CLIENT_SECRET
- "Token validation failed": Check API_KEY and token scope
- "Redirect URI mismatch": Ensure redirect URL matches WorkOS settings
- Browser doesn't open: Check firewall settings for localhost
Debug Mode
Enable debug logging:
import logging
logging.basicConfig(level=logging.DEBUG)
Token Inspection
Check cached tokens:
ls ~/.fastmcp/oauth-mcp-client-cache/
Clear token cache:
from fastmcp.client.auth.oauth import FileTokenStorage
FileTokenStorage.clear_all()
Security Notes
- Never commit
.envfiles with real credentials - Use HTTPS in production
- Rotate API keys regularly
- Monitor WorkOS logs for unusual activity
- Set appropriate token expiration times
Next Steps
- Explore WorkOS Directory Sync for user provisioning
- Set up multi-organization support
- Implement role-based access control
- Add custom scopes and claims validation