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.2.1.tar.gz (157.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.2.1-py3-none-any.whl (61.8 kB view details)

Uploaded Python 3

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

Hashes for fastapi_mcp_router-0.2.1.tar.gz
Algorithm Hash digest
SHA256 ea713840b20781aff2794d637ec92fed8596a556ab9e2fd0198ddc1c6c4dde29
MD5 6087254bc905bd3810abe0062bb808ad
BLAKE2b-256 1ce2af99c7e3a5ac650aa5fe81a141ef99c25e09f7056600ffff95e9a0c8b161

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_mcp_router-0.2.1.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.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_mcp_router-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 44a57a8dcde8530ddad0def87eff4d804f72d1c7a815337c697c058639287515
MD5 1a61995f10178dc2e71ba38591a45896
BLAKE2b-256 cacbed70e1cf73abc1ab21041171c25c6635e4dbaf9e4fb66bb6b894ac9e9a6f

See more details on using hashes here.

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

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