This example demonstrates how to build an MCP server using the FastMCP framework with Scalekit as the OAuth provider for authentication and authorization.
- OAuth 2.1 Authentication: Uses Scalekit for secure user authentication
- Dynamic Client Registration: Supports DCR for automated client setup
- JWT Token Validation: Validates access tokens from Scalekit
- Scope-based Authorization: Fine-grained access control for different operations
- Resource Discovery: Provides OAuth resource metadata endpoint
pip install -r requirements.txt
Copy the example environment file and fill in your Scalekit details:
cp .env.example .env
Edit .env
with your Scalekit configuration:
SCALEKIT_ENVIRONMENT_URL=https://your-env.scalekit.com
SCALEKIT_CLIENT_ID=your_client_id_from_scalekit
SCALEKIT_CLIENT_SECRET=your_client_secret_from_scalekit
SCALEKIT_RESOURCE_ID=your_resource_id_from_scalekit
SERVER_BASE_URL=https://your-server-domain.com
-
Create a Scalekit Account: Sign up at Scalekit
-
Register Your MCP Server:
- Go to your Scalekit dashboard
- Create a new application
- Note down your Client ID and Client Secret
-
Register MCP Server Resource:
- In your Scalekit dashboard, navigate to "Resources" or "MCP Servers"
- Click "Register New MCP Server" or "Add Resource"
- Fill in your MCP server details:
- Name: Your MCP server name (e.g., "My FastMCP Server")
- Base URL: Your server's base URL (e.g.,
http://localhost:8000/mcp
) - Description: Brief description of your server's functionality
- After registration, copy the Resource ID from the dashboard
- This Resource ID should be used as
SCALEKIT_RESOURCE_ID
in your.env
file
-
Configure Scopes: Set up the required scopes in your Scalekit application:
profile:read
organizations:read
resources:read
resources:write
metrics:read
python server.py
fastmcp run server.py:mcp
The server provides the following authenticated tools:
get_user_profile(user_id)
: Get user profile informationlist_organizations()
: List accessible organizations (requiresorganizations:read
scope)create_resource(name, description, organization_id)
: Create resources (requiresresources:write
scope)get_api_metrics()
: Get API usage metrics (requiresmetrics:read
scope)
- Client Registration: MCP clients can use Scalekit's Dynamic Client Registration
- Authorization: Users authenticate via Scalekit's OAuth flow
- Token Issuance: Scalekit issues JWT access tokens with appropriate scopes
- API Access: Clients include tokens in requests to the MCP server
- Token Validation: Server validates tokens using Scalekit's JWKS endpoint
- JWT Validation: All tokens are validated against Scalekit's JWKS
- Scope Enforcement: Tools can check for required scopes
- Audience Validation: Ensures tokens are intended for this server
- Expiration Checking: Automatically rejects expired tokens
# Get an access token from Scalekit first, then:
curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-X POST \
-d '{"method": "tools/call", "params": {"name": "list_organizations", "arguments": {}}}' \
http://localhost:8000
@mcp.tool
def your_new_tool(param: str) -> dict:
"""Your tool description"""
# Add scope checking if needed
# Implement your business logic
return {"result": "success"}
- Use HTTPS in production
- Set appropriate CORS policies
- Implement rate limiting
- Add comprehensive logging
- Use environment-specific Scalekit configurations
- Consider token caching strategies for performance