Lightweight FastAPI integration for Model Context Protocol (MCP)
Project description
fastapi-mcp-router
Add MCP to your existing FastAPI app. Register tools, resources, and prompts with decorators. Use Depends(), Request, and BackgroundTasks the same way you already do.
Why fastapi-mcp-router
- It's just an
APIRouter. Mount it like any other router. No separate framework, no new server process. - Your DI still works.
Depends(),Request,BackgroundTasks— same patterns, same middleware. - 2 dependencies. FastAPI and Pydantic. Nothing else.
- No lock-in. Tools are regular async functions. Call them from tests, CLI scripts, or other endpoints without MCP.
- Lambda-ready. Stateless mode + Mangum. No adapter layer.
Use FastMCP instead if you need STDIO transport, OpenAPI spec imports, or managed hosting.
Install
pip install fastapi-mcp-router
Quick Start
from fastapi import FastAPI
from fastapi_mcp_router import MCPRouter
app = FastAPI()
mcp = MCPRouter()
@mcp.tool()
async def write_message(payload: str) -> dict:
"""Write coordination message."""
return {"success": True, "message_id": "msg-123"}
@mcp.resource("project://{project_id}/config")
async def project_config(project_id: str) -> dict:
return {"project_id": project_id, "env": "production"}
@mcp.prompt()
async def review_code(file_path: str, language: str = "python") -> list[dict]:
return [{"role": "user", "content": f"Review {file_path} ({language})"}]
app.include_router(mcp, prefix="/mcp")
That's it. Your FastAPI app now speaks MCP over Streamable HTTP.
What You Get
- Full MCP 2025-06-18 spec — tools, resources, prompts, sampling, logging, completions, elicitation
- Streamable HTTP — JSON or SSE response based on
Acceptheader - Streaming tools — return
AsyncGeneratorfor incremental results - Session management — in-memory and Redis stores for stateful connections
- Progress reporting — inject
ProgressCallbackinto tool signatures - Auth —
auth_validatorcallback + OAuth 2.1 PRM (RFC 9728) - OpenTelemetry — opt-in spans and counters via
pip install fastapi-mcp-router[otel] - Lambda-ready — stateless mode works with Mangum, no adapter overhead
Documentation
- Quick Start — installation, first tool, stateful mode, auth, Lambda
- Guide — resources, prompts, streaming, sessions, telemetry
- API Reference — all exports, types, and configuration options
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fastapi_mcp_router-0.2.1.tar.gz.
File metadata
- Download URL: fastapi_mcp_router-0.2.1.tar.gz
- Upload date:
- Size: 157.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea713840b20781aff2794d637ec92fed8596a556ab9e2fd0198ddc1c6c4dde29
|
|
| MD5 |
6087254bc905bd3810abe0062bb808ad
|
|
| BLAKE2b-256 |
1ce2af99c7e3a5ac650aa5fe81a141ef99c25e09f7056600ffff95e9a0c8b161
|
Provenance
The following attestation bundles were made for fastapi_mcp_router-0.2.1.tar.gz:
Publisher:
release.yml on rcrsr/fastapi-mcp-router
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_mcp_router-0.2.1.tar.gz -
Subject digest:
ea713840b20781aff2794d637ec92fed8596a556ab9e2fd0198ddc1c6c4dde29 - Sigstore transparency entry: 1016643586
- Sigstore integration time:
-
Permalink:
rcrsr/fastapi-mcp-router@58aeedc933ff30616cc891cd9613710194f957ca -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/rcrsr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@58aeedc933ff30616cc891cd9613710194f957ca -
Trigger Event:
push
-
Statement type:
File details
Details for the file fastapi_mcp_router-0.2.1-py3-none-any.whl.
File metadata
- Download URL: fastapi_mcp_router-0.2.1-py3-none-any.whl
- Upload date:
- Size: 61.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
44a57a8dcde8530ddad0def87eff4d804f72d1c7a815337c697c058639287515
|
|
| MD5 |
1a61995f10178dc2e71ba38591a45896
|
|
| BLAKE2b-256 |
cacbed70e1cf73abc1ab21041171c25c6635e4dbaf9e4fb66bb6b894ac9e9a6f
|
Provenance
The following attestation bundles were made for fastapi_mcp_router-0.2.1-py3-none-any.whl:
Publisher:
release.yml on rcrsr/fastapi-mcp-router
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_mcp_router-0.2.1-py3-none-any.whl -
Subject digest:
44a57a8dcde8530ddad0def87eff4d804f72d1c7a815337c697c058639287515 - Sigstore transparency entry: 1016643765
- Sigstore integration time:
-
Permalink:
rcrsr/fastapi-mcp-router@58aeedc933ff30616cc891cd9613710194f957ca -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/rcrsr
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@58aeedc933ff30616cc891cd9613710194f957ca -
Trigger Event:
push
-
Statement type: