FastAPI-MCP – An open-source tool that converts FastAPI to an MCP server with one click
🧠 What is FastAPI-MCP?
FastAPI-MCP is an open-source tool that automatically converts FastAPI application endpoints into tools compatible with the Model Context Protocol (MCP). It features zero-configuration setup—simply point it to your FastAPI app, and it will automatically discover and convert all endpoints into MCP tools without additional configuration. By preserving FastAPI’s request and response model schemas and Swagger documentation, FastAPI-MCP ensures interface integrity and ease of use. The tool supports direct integration into FastAPI applications and can also be deployed independently. Users can install it via uv
or pip
and integrate it into FastAPI applications with minimal code.
🔧 Main Features of FastAPI-MCP
1. Automatic Discovery and Conversion
FastAPI-MCP can automatically discover all endpoints in a FastAPI application and convert them into MCP tools. No manual configuration is needed, enabling automatic identification and conversion of endpoints.
2. Schema and Documentation Preservation
The tool preserves FastAPI’s request and response model schemas and all endpoint documentation. The documentation remains consistent with Swagger, facilitating understanding and usage by developers and users.
3. Flexible Deployment
FastAPI-MCP supports mounting the MCP server directly into a FastAPI application or deploying it separately. This allows developers to choose the deployment method that best suits their needs.
4. Custom Tool Naming
FastAPI-MCP uses the operation_id
from FastAPI routes as the MCP tool names. Developers can explicitly define operation_id
to specify clearer and more intuitive tool names, avoiding overly complex auto-generated names.
5. Endpoint Exposure Filtering
Developers can control which FastAPI endpoints are exposed as MCP tools by including or excluding specific operation_id
s or tags. This enables more granular control over endpoint exposure.
6. Support for SSE and Proxy Connections
FastAPI-MCP supports Server-Sent Events (SSE) for direct connections to clients that support this protocol (e.g., Cursor). For clients that do not support SSE (e.g., Claude Desktop), connections can be established via mcp-proxy
.
7. Dynamic Updates
If new FastAPI endpoints are added after creating the MCP server, invoking the setup_server()
method will refresh the MCP server to include the newly added endpoints.
⚙️ Technical Principles of FastAPI-MCP
1. Utilizing FastAPI’s OpenAPI Specification
FastAPI-MCP parses the OpenAPI documentation of a FastAPI application to automatically extract interface metadata, including request and response models, routing information, etc. This metadata is used to generate corresponding MCP tools, ensuring accurate and consistent conversions.
2. Reflective Metadata Capture and Dynamic Route Registration
Based on FastAPI’s type system and reflection mechanisms, FastAPI-MCP automatically captures metadata such as interface parameters, response models, and permission declarations. Through dynamic route registration, it parses APIRouter
objects in real-time and generates a service directory tree.
3. Abstract Syntax Tree (AST) Parsing
FastAPI-MCP employs AST parsing techniques to statically analyze decorators like @app.get()
, extracting interface metadata. This enables the tool to automatically discover all endpoints in a FastAPI application and convert them into MCP tools.
4. Asynchronous Task Orchestration
Leveraging Starlette’s event loop mechanism, FastAPI-MCP implements real-time monitoring of interface call chains and asynchronous task orchestration. This allows the MCP server to efficiently handle concurrent requests, perfectly aligning with FastAPI’s asynchronous features.
5. Preservation of Request and Response Schemas
FastAPI-MCP retains the request and response schemas of FastAPI endpoints, along with Swagger documentation. This ensures that AI models can obtain accurate and consistent data when calling APIs, enhancing interaction reliability and stability.
📦 Project Repository
GitHub Repository: https://github.com/tadata-org/fastapi_mcp
📌 Application Scenarios for FastAPI-MCP
1. Internal Enterprise Automation
FastAPI-MCP can quickly convert internal FastAPI API endpoints into MCP tools for use by AI models or other automation tools.
2. AI-Driven Application Development
In AI development, FastAPI-MCP allows AI models to directly call backend service APIs.
3. Data Analysis and Processing
AI agents can directly access data processing endpoints through MCP tools converted by FastAPI-MCP, enabling real-time data analysis.
4. Content Management
AI tools can call interfaces of content management systems (CMS) to efficiently create and update content.
5. E-Commerce
In e-commerce scenarios, AI assistants can query inventory, place orders, or retrieve product information via APIs, enhancing user experience.