FastAPI转FastMCP服务

将现有的FastAPI应用程序转换为FastMCP服务器,以便连接Claude Desktop等客户端。

  • by GenerativeAiWithMariams
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 GenerativeAiWithMariams/ExpenseTrackerwithFastApi-McpServer-ClaudeDesktop

使用说明

快速API→ FastMCP→ 克劳德桌面版

本指南展示了如何将现有的FastAPI应用程序转换为FastMCP服务器,以便您可以连接以下客户端 克劳德桌面版.


1.️⃣ 再进行

pip install fastmcp fastapi uvicorn

确保您的FastAPI项目已准备就绪,例如。, main.py 包含您的 app.


2.️⃣ 创建MCP服务器包装器(MCPServer.py)

from fastmcp import FastMCP
from main import app  # Import your FastAPI app

# Convert FastAPI app into a FastMCP server
mcp = FastMCP.from_fastapi(
    app=app,
    name="Expense Tracker Server with FastAPI"
)

if __name__ == "__main__":
    # Start MCP server locally
    mcp.run()

3.️⃣ 在本地运行服务器

# Development mode (hot reload)
uv run fastmcp dev MCPServer.py

# Normal run
uv run fastmcp run MCPServer.py

服务器将在上运行 http://0.0.0.0:8000 默认情况下。


4.️⃣ 通过代理连接克劳德桌面

# Start proxy for remote connection
uv run fastmcp install claude-desktop proxyfile.py

5.️⃣ FastMCP云部署

  1. 登录地址: FastMCP云.
  2. 上传 MCPServer.py 作为一个新项目。
  3. 设置项目 公共 (开放获取)或 私人 (需要API令牌)。
  4. 使用 /mcp 指向像Claude这样的客户端的代理连接的URL。

✅ 现在你的 FastAPI应用程序已启用MCP,准备好了 克劳德整合本地开发或FastMCP云部署。