fastmcp/examples/auth/scalekit_oauth/server.py
Akshay Parihar 7bcf8b562c
Add Scalekit Provider for Enterprise Authentication (#1927)
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Ravi Madabhushi <ravi.madabhushi@scalekit.com>
Co-authored-by: Ravi Madabhushi <innovativeravi@gmail.com>
Co-authored-by: saif-at-scalekit <saif.shaik@scalekit.com>
2025-09-26 16:58:03 -04:00

48 lines
1.3 KiB
Python

"""Scalekit OAuth server example for FastMCP.
This example demonstrates how to protect a FastMCP server with Scalekit OAuth.
Required environment variables:
- SCALEKIT_ENVIRONMENT_URL: Your Scalekit environment URL (e.g., "https://your-env.scalekit.com")
- SCALEKIT_CLIENT_ID: Your Scalekit OAuth application client ID
- SCALEKIT_RESOURCE_ID: Your Scalekit resource ID
To run:
python server.py
"""
import os
from fastmcp import FastMCP
from fastmcp.server.auth.providers.scalekit import ScalekitProvider
auth = ScalekitProvider(
environment_url=os.getenv("SCALEKIT_ENVIRONMENT_URL")
or "https://your-env.scalekit.com",
client_id=os.getenv("SCALEKIT_CLIENT_ID") or "",
resource_id=os.getenv("SCALEKIT_RESOURCE_ID") or "",
mcp_url=os.getenv("MCP_URL", "http://localhost:8000/mcp"),
)
mcp = FastMCP("Scalekit OAuth Example Server", auth=auth)
@mcp.tool
def echo(message: str) -> str:
"""Echo the provided message."""
return message
@mcp.tool
def auth_status() -> dict:
"""Show Scalekit authentication status."""
# In a real implementation, you would extract user info from the JWT token
return {
"message": "This tool requires authentication via Scalekit",
"authenticated": True,
"provider": "Scalekit",
}
if __name__ == "__main__":
mcp.run(transport="http", port=8000)