Skip to content
API Reference

MCP

OAuth authentication for MCP servers. Bearer middleware, metadata endpoints, and grant decorators.

The MCP package adds OAuth-based authentication to your MCP server. It handles bearer token verification, serves OAuth metadata endpoints, and provides grant decorators for delegated access to external APIs.

  • Building an MCP server that requires user authentication
  • Adding delegated access to external APIs (GitHub, Google, etc.) from MCP tools
  • Serving OAuth .well-known metadata for MCP clients
Terminal window
# Standard MCP SDK
pip install keycardai-mcp
# FastMCP framework (see FastMCP section below)
pip install keycardai-mcp-fastmcp

keycardai.mcp.server.auth

ExportDescription
AuthProviderCore auth provider. Wraps your MCP app with OAuth middleware
AccessContextToken exchange result. Check errors, access tokens per resource
TokenVerifierVerify incoming bearer tokens
ClientSecretApplication credential using client ID + secret
WebIdentityApplication credential using private key JWT
EKSWorkloadIdentityApplication credential for EKS environments
from mcp.server.fastmcp import FastMCP
from keycardai.mcp.server.auth import AuthProvider
mcp = FastMCP("Hello World Server")
auth_provider = AuthProvider(
zone_id="your-zone-id",
mcp_server_name="Hello World Server",
mcp_server_url="http://localhost:8000/",
)
@mcp.tool()
async def hello() -> str:
return "Hello from a protected MCP server!"
# Wrap the MCP app with authentication
app = auth_provider.app(mcp)
import httpx
from mcp.server.fastmcp import FastMCP
from keycardai.mcp.server.auth import AuthProvider, AccessContext, ClientSecret
auth_provider = AuthProvider(
zone_id="your-zone-id",
mcp_server_name="GitHub Server",
mcp_server_url="http://localhost:8000/",
application_credential=ClientSecret(("client_id", "client_secret")),
)
mcp = FastMCP("GitHub Server")
@mcp.tool()
@auth_provider.grant("https://api.github.com")
async def get_repos(ctx) -> dict:
access_context: AccessContext = await ctx.get_state("keycardai")
if access_context.has_errors():
return {"error": access_context.get_errors()}
token = access_context.access("https://api.github.com").access_token
async with httpx.AsyncClient() as client:
resp = await client.get(
"https://api.github.com/user/repos",
headers={"Authorization": f"Bearer {token}"},
)
return resp.json()
app = auth_provider.app(mcp)

The keycardai-mcp-fastmcp package provides a dedicated integration for Python’s FastMCP framework. It wraps the same auth primitives with FastMCP-specific APIs.

Terminal window
pip install keycardai-mcp-fastmcp
Featurekeycardai-mcpkeycardai-mcp-fastmcp
FrameworkStandard MCP SDKFastMCP 3.x
Auth setupauth_provider.app(mcp)auth_provider.get_remote_auth_provider()
Grant decorator@auth_provider.grant(resource)Same
Access contextawait ctx.get_state("keycardai")Same
TestingN/Amock_access_context test utility
from fastmcp import Context, FastMCP
from keycardai.mcp.integrations.fastmcp import AuthProvider, ClientSecret, AccessContext
auth_provider = AuthProvider(
zone_id="your-zone-id",
mcp_server_name="GitHub API Server",
mcp_base_url="http://localhost:8000/",
application_credential=ClientSecret(("client_id", "client_secret")),
)
auth = auth_provider.get_remote_auth_provider()
mcp = FastMCP("GitHub API Server", auth=auth)
@mcp.tool()
@auth_provider.grant("https://api.github.com")
async def get_github_user(ctx: Context) -> dict:
access_context: AccessContext = await ctx.get_state("keycardai")
if access_context.has_errors():
return {"error": access_context.get_errors()}
token = access_context.access("https://api.github.com").access_token
# Use token to call GitHub API...