fastapi-mcp python 库

安装

使用 uv(推荐) uv add fastapi-mcp 或使用 pip pip install fastapi-mcp

from fastapi import FastAPI
from fastapi_mcp import add_mcp_server
# 创建 FastAPI 应用
app = FastAPI()

# 挂载 MCP 服务器
add_mcp_server(
 app, # FastAPI 应用
 mount_path="/mcp", # MCP 服务器路径
 name="My API MCP", # MCP 服务器名称
)

FastAPI-MCP 支持 SSE(Server-Sent Events) 和 mcp-proxy 两种方式,轻 松对接 AI 工具:

SSE 方式(适用于 Cursor)

  1. 运行 FastAPI 应用
  2. 在 Cursor 设置中,填写 MCP 服务器 URL(如 http://localhost:8000/mcp )
  3. Cursor 将自动发现所有 API 端点

mcp-proxy 方式(适用于 Claude Desktop)

uv tool install mcp-proxy

在 claude_desktop_config.json 中添加 MCP 代理配置


作者:spike

分类: Python

创作时间:2025-04-17

更新时间:2025-04-18

联系方式放在中括号之中例如[[email protected]],回复评论在开头加上标号例如:#1