FastAPI-MCP
FastAPI-MCP is a Python library that automatically converts FastAPI endpoints into Model Context Protocol (MCP) tools, enabling seamless integration with Large Language Models (LLMs) and AI agents. It provides a straightforward way to expose existing FastAPI APIs as discoverable and callable tools for AI. The library is currently at version 0.4.0 and maintains an active release cadence with frequent updates and feature additions.
Warnings
- breaking The `mount()` method is deprecated as of v0.4.0 and will be removed in future versions. It previously handled SSE transport by default.
- breaking The `base_url` argument was removed from the `FastApiMCP` constructor. It is now redundant due to the default use of ASGI transport.
- breaking Version 0.2.0 introduced a complete refactor from a function-based API to a new class-based `FastApiMCP` API. This changed how MCP instances are created and mounted.
- gotcha FastAPI-MCP uses the `operation_id` from your FastAPI routes as the MCP tool names. If `operation_id` is not explicitly provided, FastAPI auto-generates a cryptic one.
- gotcha FastAPI-MCP depends on the `mcp` package. Breaking changes in the `mcp` SDK itself (e.g., `mcp` SDK 1.8.0) can cause issues with `fastapi-mcp` if not explicitly handled.
Install
-
pip install fastapi-mcp uvicorn -
uv add fastapi-mcp uvicorn
Imports
- FastApiMCP
from fastapi_mcp import FastApiMCP
Quickstart
from fastapi import FastAPI
from fastapi_mcp import FastApiMCP
# Create a FastAPI app
app = FastAPI(title="My MCP API")
@app.get("/hello")
def read_root():
return {"message": "Hello, World!"}
@app.get("/items/{item_id}", operation_id="get_item_by_id")
def read_item(item_id: int):
"""Get a specific item by its ID"""
return {"item_id": item_id, "name": f"Item {item_id}"}
# Create an MCP server based on this app
mcp = FastApiMCP(app)
# Mount the MCP server to your app using the recommended HTTP transport
mcp.mount_http()
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)