FastAPI-MCP - An open-source tool that converts FastAPI into an MCP server with a single click.
FastAPI-MCP is an open-source tool that automatically converts endpoints of FastAPI applications to conform to the Model Context Protocol (MCP). It features zero configuration; simply point it at your FastAPI application and it will automatically discover and convert all endpoints...
What is FastAPI-MCP?
FastAPI-MCP is an open-source tool that automatically converts endpoints of FastAPI applications to conform to the Model Context Protocol (MCP). It features zero configuration; simply point it at your FastAPI application and it will automatically discover and convert all endpoints to the MCP tool without any additional setup. By preserving the FastAPI request and response model and Swagger documentation, FastAPI-MCP ensures the integrity and ease of use of the interfaces. The tool supports direct integration into FastAPI applications and can be flexibly deployed independently. Users can install it via UV or pip and integrate it into their FastAPI applications with simple code.
Main functions of FastAPI-MCP
- Automatic discovery and conversionFastAPI-MCP can automatically discover all endpoints in a FastAPI application and convert them into MCP tools. No manual configuration is required; it enables automatic endpoint identification and conversion.
- Retention Mode and DocumentsThe tool preserves the request and response models of the FastAPI and retains documentation for all endpoints. The documentation is consistent with that in Swagger, making it easy for developers and users to understand and use.
- Flexible deploymentFastAPI-MCP supports mounting the MCP server directly into FastAPI applications, or deploying it independently. This allows developers to choose the most suitable deployment method based on their needs.
- Custom tool namingFastAPI-MCP uses FastAPI routing.
operation_idAs the name of the MCP tool. Developers can define it explicitly.operation_idThis allows you to specify clearer and more intuitive tool names, avoiding overly complex automatically generated names. - Filter exposed endpointsDevelopers can control which FastAPI endpoints are exposed to MCP tools using OpenAPI operation IDs or tags. This allows for more granular control by including or excluding specific endpoints or tags.
- Supports SSE and proxy connectionsFastAPI-MCP supports direct connections to clients that support the protocol (such as Cursor) via Server Send Events (SSE). For clients that do not support SSE (such as Claude Desktop), connections can be made via...
mcp-proxyA proxy enables the connection. - Dynamic updatesIf a new FastAPI endpoint is added after the MCP server is created, it can be accessed by calling...
setup_server()The method refreshes the MCP server, including newly added endpoints.
The technical principle of FastAPI-MCP
- Utilizing the OpenAPI specification of FastAPIFastAPI-MCP automatically extracts metadata from the OpenAPI documentation of FastAPI applications, including request and response models, routing information, etc. This metadata is used to generate the corresponding MCP tool, ensuring the accuracy and consistency of the transformation.
- Reflective metadata capture and dynamic route registrationFastAPI-MCP leverages FastAPI's type system and reflection mechanism to automatically capture metadata such as interface parameters, response models, and permission declarations. It also performs real-time parsing through dynamic route registration.
APIRouterThe object is generated and a service directory tree is created. - AST syntax tree parsingFastAPI-MCP uses Abstract Syntax Tree (AST) parsing technology to...
@app.get()Static analysis is performed on decorators to extract the metadata of the interfaces. This enables tools to automatically discover all endpoints in FastAPI applications and convert them into MCP tools. - Asynchronous task orchestrationFastAPI-MCP relies on Starlette's event loop mechanism to achieve real-time monitoring of API call chains and asynchronous task orchestration. This enables the MCP server to efficiently handle concurrent requests, perfectly complementing the asynchronous nature of FastAPI.
- Preserve request and response patternsFastAPI-MCP retains the request and response patterns of FastAPI endpoints, as well as the Swagger documentation. This ensures that AI models obtain accurate and consistent data when calling the API, improving the reliability and stability of the interaction.
FastAPI-MCP project address
- Github repository:https://github.com/tadata-org/fastapi_mcp
Application scenarios of FastAPI-MCP
- Enterprise internal automationFastAPI-MCP can quickly convert an enterprise's internal FastAPI API endpoints into MCP tools for use by AI models or other automation tools.
- AI-driven application developmentIn AI development, FastAPI-MCP allows AI models to directly call the APIs of backend services.
- Data Analysis and ProcessingAI agents can directly access data processing endpoints through the MCP tool converted from FastAPI-MCP to achieve real-time data analysis.
- Content ManagementAI tools can call the interfaces of content management systems (CMS) to efficiently complete content creation and updates.
- e-commerceIn e-commerce scenarios, AI assistants can use APIs to check inventory, place orders, or obtain product information, thereby improving the user experience.