MCP Configs2026年4月2日·1 分钟阅读

FastAPI-MCP — Turn Any FastAPI into MCP Tools

Zero-config bridge that exposes existing FastAPI endpoints as MCP tools with authentication. Add one line to make your API AI-accessible.

MC
MCP Hub · Community
快速使用

先拿来用,再决定要不要深挖

这里应该同时让用户和 Agent 知道第一步该复制什么、安装什么、落到哪里。

pip install fastapi-mcp
from fastapi import FastAPI
from fastapi_mcp import FastApiMCP

app = FastAPI()

@app.get("/items/{item_id}")
async def get_item(item_id: int):
    return {"item_id": item_id, "name": "Example"}

# One line to expose all endpoints as MCP tools
mcp = FastApiMCP(app)
mcp.mount()

Your FastAPI app now serves MCP at /mcp — connect any AI assistant to it.

Introduction

FastAPI-MCP is a Python library that automatically converts your existing FastAPI endpoints into MCP-compatible tools. Instead of writing MCP server code from scratch, you add a single line to your existing FastAPI app and every endpoint becomes an AI-callable tool.

Key features:

  • Zero ConfigFastApiMCP(app).mount() is all you need. Every route becomes an MCP tool automatically.
  • Authentication Built-in — Leverages your existing FastAPI security dependencies (OAuth2, API keys, etc.) for MCP tool access.
  • Schema Preservation — Request/response models, validation rules, and documentation carry over to MCP tool definitions.
  • Selective Exposure — Include or exclude specific endpoints with include_operations and exclude_operations.
  • ASGI Transport — Direct in-process communication eliminates HTTP overhead when the MCP client and FastAPI run together.
  • Custom Tool Naming — Override auto-generated tool names for cleaner AI assistant integration.

Perfect for teams that already have FastAPI backends and want to make them AI-accessible without rewriting anything.

FAQ

Q: Does it work with existing authentication? A: Yes. FastAPI-MCP reuses your existing FastAPI Depends() security — OAuth2 bearer tokens, API keys, and custom auth all work transparently.

Q: Can I choose which endpoints to expose? A: Yes. Use include_operations=["get_item", "list_items"] or exclude_operations=["delete_all"] to control access.

Q: How do I connect Claude to my FastAPI-MCP server? A: Add this to your MCP config:

{
  "mcpServers": {
    "my-api": {
      "url": "http://localhost:8000/mcp"
    }
  }
}

Q: Does it handle complex request bodies? A: Yes. Pydantic models, query parameters, path parameters, and JSON bodies are all converted to MCP tool input schemas automatically.

Works With

  • FastAPI 0.100+
  • Python 3.10+
  • Claude Code / Claude Desktop / Any MCP client
  • Any existing FastAPI authentication setup
🙏

来源与感谢

Thanks to the Tadata team for the elegant "one line to MCP" approach that saves backend teams weeks of integration work.

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产