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.1.tar.gz (162.2 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.1-py3-none-any.whl (62.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_mcp_router-0.3.1.tar.gz
  • Upload date:
  • Size: 162.2 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.1.tar.gz
Algorithm Hash digest
SHA256 1c7bde355120621b9996d77fa67dc4abc12a4dbd900147e0c762cb2218ae4e92
MD5 072d511d4febd2def8141e0b5adebc3d
BLAKE2b-256 52c7fcfc5e5f823e3171a1e2652dbb9fdb851e8d520431d03a01d986536f0afb

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for fastapi_mcp_router-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 86ad08ba2a7e3a8a490f7d4f1a2aa8d1f89b396249c918f84d750574e0dd1902
MD5 a3a7b4a5d7b5c230d94d4da5cb1d941a
BLAKE2b-256 e9eb3a30bf391afb0065ff4047d5f48726c6cdfe24ea05ace1f451bd7e318aca

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_mcp_router-0.3.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