Skip to main content

Lightweight FastAPI integration for Model Context Protocol (MCP)

Project description

fastapi-mcp-router

CI PyPI version Python versions License

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 Accept header
  • Streaming tools — return AsyncGenerator for incremental results
  • Session management — in-memory and Redis stores for stateful connections
  • Progress reporting — inject ProgressCallback into tool signatures
  • Authauth_validator callback + 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

fastapi_mcp_router-0.3.0.tar.gz (160.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_mcp_router-0.3.0-py3-none-any.whl (62.4 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_mcp_router-0.3.0.tar.gz.

File metadata

  • Download URL: fastapi_mcp_router-0.3.0.tar.gz
  • Upload date:
  • Size: 160.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fastapi_mcp_router-0.3.0.tar.gz
Algorithm Hash digest
SHA256 bdb8ac3b3bc53bacc3e65c83db3ad97c8e1998eaaa9e691aa3a6a3b55557e579
MD5 1ea024b801197da08c2f686e6eb5e851
BLAKE2b-256 b48922bec4167a90630ecf755464b82e4f63812e8aa57bc47f645e60f7c462aa

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_mcp_router-0.3.0.tar.gz:

Publisher: release.yml on rcrsr/fastapi-mcp-router

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastapi_mcp_router-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_mcp_router-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1ef25921cd3365ec67a1ee7deb7540c4babb493008eb878cefa8122aab9aa585
MD5 b4e5cbec690f64ebd231fa1662e4b3fe
BLAKE2b-256 829a4e8535a4ad7e1c226c2e1b96d4886524dc3a7f0f94eac427a0b9f8b20d22

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_mcp_router-0.3.0-py3-none-any.whl:

Publisher: release.yml on rcrsr/fastapi-mcp-router

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page