FastAPI-MCP

0.4.0 · active · verified Wed Apr 01

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

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic FastAPI application, instantiate `FastApiMCP` with your app, and then mount the MCP server using the recommended `mount_http()` method. The `operation_id` is explicitly set for clarity in the exposed MCP tool. To run, save as `main.py` and execute `python main.py`. The MCP server will be available at `http://localhost:8000/mcp`.

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)

view raw JSON →