SuperAPI AI Directory

FastAPI-MCP - An open-source tool to convert FastAPI to an MCP server with one click

Author: AI小集 · Original source: AI工具集

What is FastAPI-MCP

FastAPI-MCP is an open-source tool that automatically converts endpoints of a FastAPI application into ones compliant with the Model Context Protocol (MCP). It features zero configuration: simply pointing it at a FastAPI application automatically discovers and converts all endpoints into MCP tools, with no additional setup required. 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 a FastAPI application and can also be flexibly deployed separately. Users can install it via uv or pip and integrate it into a FastAPI application with simple code.

FastAPI-MCP

Main features of FastAPI-MCP

  • 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 required, enabling automatic identification and conversion of endpoints.
  • Preserve schemas and documentation: The tool can preserve the schemas of FastAPI's request models and response models, and preserve the documentation of all endpoints. The documentation remains consistent with that in Swagger, making it easy for developers and users to understand and use.
  • Flexible deployment: FastAPI-MCP supports mounting the MCP server directly into a FastAPI application, or deploying it separately. This lets developers choose the most suitable deployment method according to their needs.
  • Custom tool naming: FastAPI-MCP uses the operation_id in FastAPI routes as the name of the MCP tool. Developers can explicitly define operation_id to specify clearer and more intuitive tool names, avoiding overly complex automatically generated names.
  • Filter exposed endpoints: Developers can control which FastAPI endpoints are exposed as MCP tools through OpenAPI operation IDs or tags. It supports including or excluding specific endpoints or tags for more fine-grained control.
  • Support SSE and proxy connections: FastAPI-MCP supports direct connections to clients that support Server-Sent Events (SSE), such as Cursor. For clients that do not support SSE, such as Claude Desktop, connections can be made through the mcp-proxy proxy.
  • Dynamic updates: If new FastAPI endpoints are added after the MCP server is created, the MCP server can be refreshed by calling the setup_server() method to include the newly added endpoints.

Technical principles of FastAPI-MCP

  • Leveraging 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, and more. This metadata is used to generate the corresponding MCP tools, ensuring the accuracy and consistency of the conversion.
  • Reflective metadata capture and dynamic route registration: Based on FastAPI's type system and reflection mechanism, 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.
  • AST syntax tree parsing: FastAPI-MCP uses Abstract Syntax Tree (AST) parsing technology to perform static analysis on @app.get() and other decorators, extracting interface metadata. This enables the tool to automatically discover all endpoints in a FastAPI application and convert them into MCP tools.
  • Asynchronous task orchestration: FastAPI-MCP relies on Starlette's event loop mechanism to implement real-time monitoring of API call chains and asynchronous task orchestration. This enables the MCP server to efficiently handle concurrent requests, perfectly matching FastAPI's asynchronous characteristics.
  • Preserve request and response schemas: FastAPI-MCP preserves the request and response schemas of FastAPI endpoints, as well as the Swagger documentation. This ensures that AI models can obtain accurate and consistent data when calling APIs, improving the reliability and stability of interactions.

FastAPI-MCP project address

FastAPI-MCP application scenarios

  • Enterprise internal automation: FastAPI-MCP can quickly convert an enterprise's internal FastAPI API endpoints into MCP tools for AI models or other automation tools to call.
  • AI-driven application development: In AI development, FastAPI-MCP allows AI models to directly call the APIs of backend services.
  • Data analysis and processing: AI agents can directly access data processing endpoints through MCP tools converted by FastAPI-MCP, enabling real-time data analysis.
  • Content management: AI tools can call the interfaces of a content management system (CMS) to efficiently complete content creation and updates.
  • E-commerce: In e-commerce scenarios, AI assistants can query inventory, place orders, or obtain product information through APIs, enhancing the user experience.