Automatic MCP server generator for FastAPI applications - converts FastAPI endpoints to MCP tools for LLM integration
Project description
FastAPI-MCP
A zero-configuration tool for integrating Model Context Protocol (MCP) servers with FastAPI applications.
Features
- Direct integration - Mount an MCP server directly to your FastAPI app
- Zero configuration required - just point it at your FastAPI app and it works
- Automatic discovery of all FastAPI endpoints and conversion to MCP tools
- Type-safe conversion from FastAPI endpoints to MCP tools
- Documentation preservation from FastAPI to MCP
- Custom tools - Add custom MCP tools alongside your API endpoints
Installation
We recommend using uv, a fast Python package installer:
uv add fastapi-mcp
Alternatively, you can install with pip:
pip install fastapi-mcp
For detailed installation instructions and alternative methods, see INSTALL.md.
Basic Usage
The simplest way to use FastAPI-MCP is to add an MCP server directly to your FastAPI application:
from fastapi import FastAPI
from fastapi_mcp import add_mcp_server
# Your FastAPI app
app = FastAPI()
# Mount the MCP server to your app
add_mcp_server(
app, # Your FastAPI app
mount_path="/mcp", # Where to mount the MCP server
name="My API MCP", # Name for the MCP server
)
That's it! Your auto-generated MCP server is now available at https://app.base.url/mcp
.
Advanced Usage
FastAPI-MCP provides several ways to customize and control how your MCP server is created and configured. Here are some advanced usage patterns:
from fastapi import FastAPI
from fastapi_mcp import add_mcp_server
app = FastAPI()
mcp_server = add_mcp_server(
app, # Your FastAPI app
mount_path="/mcp", # Where to mount the MCP server
name="My API MCP", # Name for the MCP server
describe_all_responses=True, # False by default. Include all possible response schemas in tool descriptions, instead of just the successful response.
describe_full_response_schema=True # False by default. Include full JSON schema in tool descriptions, instead of just an LLM-friendly response example.
)
# Add custom tools in addition to existing APIs.
@mcp_server.tool()
async def get_server_time() -> str:
"""Get the current server time."""
from datetime import datetime
return datetime.now().isoformat()
Examples
See the examples directory for complete examples.
Simple integration example:
from fastapi import FastAPI
from fastapi_mcp import add_mcp_server
app = FastAPI(title="Simple API")
@app.get("/hello/{name}")
async def hello(name: str):
"""Say hello to someone"""
return {"message": f"Hello, {name}!"}
# Add MCP server
mcp_server = add_mcp_server(app, mount_path="/mcp")
# Optionally add custom tools
@mcp_server.tool()
async def get_current_time():
"""Get the current server time"""
from datetime import datetime
return datetime.now().isoformat()
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)
Connecting to the MCP Server
Once your FastAPI app with MCP integration is running, you can connect to it with any MCP client, such as Claude:
- Run your application
- In Claude, use the URL of your MCP server endpoint (e.g.,
http://localhost:8000/mcp
) - Claude will discover all available tools and resources automatically
Development and Contributing
If you're interested in contributing to FastAPI-MCP:
# Clone the repository
git clone https://github.com/tadata-org/fastapi_mcp.git
cd fastapi_mcp
# Create a virtual environment and install dependencies with uv
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
uv add -e ".[dev]"
# Run tests
uv run pytest
For more details about contributing, see CONTRIBUTING.md.
Requirements
- Python 3.10+
- uv
License
MIT License. Copyright (c) 2024 Tadata Inc.
About
Developed and maintained by Tadata Inc.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fastapi_mcp-0.1.3.tar.gz
.
File metadata
- Download URL: fastapi_mcp-0.1.3.tar.gz
- Upload date:
- Size: 57.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
121faf25141e0bc50bbc97c74f4123852b3f13db16f5bf3536d9481e04f9747f
|
|
MD5 |
bf3f238681cc3252b15d70f71ee083ad
|
|
BLAKE2b-256 |
7975aecd076b057aacee005c9eb1676b34a24167593ce37a51fa495f88b8ea40
|
File details
Details for the file fastapi_mcp-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: fastapi_mcp-0.1.3-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
345ced029aa8c4dee1d1e832e675e3e4777034fb3566a29129217ecf0333a59f
|
|
MD5 |
1c9ebcda825cdd90dafca14643b1d9aa
|
|
BLAKE2b-256 |
c6bad4d32aeb93ab4ab514af39247f6e668a20cb2fa46cb6c70ed6f66d974756
|