|
| 1 | +Below is the translated Chinese version of the provided README.md file. The original Markdown format is fully preserved, with all English text translated into Chinese while keeping code blocks, links, images, and other formatting unchanged. |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +<p align="center"><a href="https://github.com/tadata-org/fastapi_mcp"><img src="https://github.com/user-attachments/assets/609d5b8b-37a1-42c4-87e2-f045b60026b1" alt="fastapi-to-mcp" height="100"/></a></p> |
| 6 | +<h1 align="center">FastAPI-MCP</h1> |
| 7 | +<p align="center">一个零配置工具,用于自动将FastAPI端点暴露为模型上下文协议(MCP)工具。</p> |
| 8 | +<div align="center"> |
| 9 | + |
| 10 | +[](https://pypi.org/project/fastapi-mcp/) |
| 11 | +[](https://pypi.org/project/fastapi-mcp/) |
| 12 | +[](#) |
| 13 | + |
| 14 | +[](https://github.com/tadata-org/fastapi_mcp/actions/workflows/ci.yml) |
| 15 | +[](https://codecov.io/gh/tadata-org/fastapi_mcp) |
| 16 | + |
| 17 | +</div> |
| 18 | + |
| 19 | +<p align="center"><a href="https://github.com/tadata-org/fastapi_mcp"><img src="https://github.com/user-attachments/assets/1cba1bf2-2fa4-46c7-93ac-1e9bb1a95257" alt="fastapi-mcp-usage" height="400"/></a></p> |
| 20 | + |
| 21 | +## 特点 |
| 22 | + |
| 23 | +- **直接集成** - 直接将MCP服务器挂载到您的FastAPI应用 |
| 24 | +- **零配置** - 只需指向您的FastAPI应用即可工作 |
| 25 | +- **自动发现**所有FastAPI端点并转换为MCP工具 |
| 26 | +- **保留模式** - 保留您的请求模型和响应模型的模式 |
| 27 | +- **保留文档** - 保留所有端点的文档,就像在Swagger中一样 |
| 28 | +- **灵活部署** - 将MCP服务器挂载到同一应用,或单独部署 |
| 29 | + |
| 30 | +## 安装 |
| 31 | + |
| 32 | +我们推荐使用[uv](https://docs.astral.sh/uv/),一个快速的Python包安装器: |
| 33 | + |
| 34 | +```bash |
| 35 | +uv add fastapi-mcp |
| 36 | +``` |
| 37 | + |
| 38 | +或者,您可以使用pip安装: |
| 39 | + |
| 40 | +```bash |
| 41 | +pip install fastapi-mcp |
| 42 | +``` |
| 43 | + |
| 44 | +## 基本用法 |
| 45 | + |
| 46 | +使用FastAPI-MCP的最简单方法是直接将MCP服务器添加到您的FastAPI应用中: |
| 47 | + |
| 48 | +```python |
| 49 | +from fastapi import FastAPI |
| 50 | +from fastapi_mcp import FastApiMCP |
| 51 | + |
| 52 | +app = FastAPI() |
| 53 | + |
| 54 | +mcp = FastApiMCP( |
| 55 | + app, |
| 56 | + |
| 57 | + # 可选参数 |
| 58 | + name="我的API MCP", |
| 59 | + description="我的API描述", |
| 60 | + base_url="http://localhost:8000", |
| 61 | +) |
| 62 | + |
| 63 | +# 直接将MCP服务器挂载到您的FastAPI应用 |
| 64 | +mcp.mount() |
| 65 | +``` |
| 66 | + |
| 67 | +就是这样!您的自动生成的MCP服务器现在可以在`https://app.base.url/mcp`访问。 |
| 68 | + |
| 69 | +> **关于`base_url`的注意事项**:虽然`base_url`是可选的,但强烈建议您明确提供它。`base_url`告诉MCP服务器在调用工具时向何处发送API请求。如果不提供,库将尝试自动确定URL,这在部署环境中内部和外部URL不同时可能无法正确工作。 |
| 70 | +
|
| 71 | +## 工具命名 |
| 72 | + |
| 73 | +FastAPI-MCP使用FastAPI路由中的`operation_id`作为MCP工具的名称。如果您不指定`operation_id`,FastAPI会自动生成一个,但这些名称可能比较晦涩。 |
| 74 | + |
| 75 | +比较以下两个端点定义: |
| 76 | + |
| 77 | +```python |
| 78 | +# 自动生成的operation_id(类似于"read_user_users__user_id__get") |
| 79 | +@app.get("/users/{user_id}") |
| 80 | +async def read_user(user_id: int): |
| 81 | + return {"user_id": user_id} |
| 82 | + |
| 83 | +# 显式operation_id(工具将被命名为"get_user_info") |
| 84 | +@app.get("/users/{user_id}", operation_id="get_user_info") |
| 85 | +async def read_user(user_id: int): |
| 86 | + return {"user_id": user_id} |
| 87 | +``` |
| 88 | + |
| 89 | +为了获得更清晰、更直观的工具名称,我们建议在FastAPI路由定义中添加显式的`operation_id`参数。 |
| 90 | + |
| 91 | +要了解更多信息,请阅读FastAPI官方文档中关于[路径操作的高级配置](https://fastapi.tiangolo.com/advanced/path-operation-advanced-configuration/)的部分。 |
| 92 | + |
| 93 | +## 高级用法 |
| 94 | + |
| 95 | +FastAPI-MCP提供了多种方式来自定义和控制MCP服务器的创建和配置。以下是一些高级用法模式: |
| 96 | + |
| 97 | +### 自定义模式描述 |
| 98 | + |
| 99 | +```python |
| 100 | +from fastapi import FastAPI |
| 101 | +from fastapi_mcp import FastApiMCP |
| 102 | + |
| 103 | +app = FastAPI() |
| 104 | + |
| 105 | +mcp = FastApiMCP( |
| 106 | + app, |
| 107 | + name="我的API MCP", |
| 108 | + base_url="http://localhost:8000", |
| 109 | + describe_all_responses=True, # 在工具描述中包含所有可能的响应模式 |
| 110 | + describe_full_response_schema=True # 在工具描述中包含完整的JSON模式 |
| 111 | +) |
| 112 | + |
| 113 | +mcp.mount() |
| 114 | +``` |
| 115 | + |
| 116 | +### 自定义暴露的端点 |
| 117 | + |
| 118 | +您可以使用Open API操作ID或标签来控制哪些FastAPI端点暴露为MCP工具: |
| 119 | + |
| 120 | +```python |
| 121 | +from fastapi import FastAPI |
| 122 | +from fastapi_mcp import FastApiMCP |
| 123 | + |
| 124 | +app = FastAPI() |
| 125 | + |
| 126 | +# 仅包含特定操作 |
| 127 | +mcp = FastApiMCP( |
| 128 | + app, |
| 129 | + include_operations=["get_user", "create_user"] |
| 130 | +) |
| 131 | + |
| 132 | +# 排除特定操作 |
| 133 | +mcp = FastApiMCP( |
| 134 | + app, |
| 135 | + exclude_operations=["delete_user"] |
| 136 | +) |
| 137 | + |
| 138 | +# 仅包含具有特定标签的操作 |
| 139 | +mcp = FastApiMCP( |
| 140 | + app, |
| 141 | + include_tags=["users", "public"] |
| 142 | +) |
| 143 | + |
| 144 | +# 排除具有特定标签的操作 |
| 145 | +mcp = FastApiMCP( |
| 146 | + app, |
| 147 | + exclude_tags=["admin", "internal"] |
| 148 | +) |
| 149 | + |
| 150 | +# 结合操作ID和标签(包含模式) |
| 151 | +mcp = FastApiMCP( |
| 152 | + app, |
| 153 | + include_operations=["user_login"], |
| 154 | + include_tags=["public"] |
| 155 | +) |
| 156 | + |
| 157 | +mcp.mount() |
| 158 | +``` |
| 159 | + |
| 160 | +关于过滤的注意事项: |
| 161 | +- 您不能同时使用`include_operations`和`exclude_operations` |
| 162 | +- 您不能同时使用`include_tags`和`exclude_tags` |
| 163 | +- 您可以将操作过滤与标签过滤结合使用(例如,使用`include_operations`和`include_tags`) |
| 164 | +- 当结合过滤器时,将采取贪婪方法。匹配任一标准的端点都将被包含 |
| 165 | + |
| 166 | +### 与原始FastAPI应用分开部署 |
| 167 | + |
| 168 | +您不限于在创建MCP的同一个FastAPI应用上提供MCP服务。 |
| 169 | + |
| 170 | +您可以从一个FastAPI应用创建MCP服务器,并将其挂载到另一个应用上: |
| 171 | + |
| 172 | +```python |
| 173 | +from fastapi import FastAPI |
| 174 | +from fastapi_mcp import FastApiMCP |
| 175 | + |
| 176 | +# 您的API应用 |
| 177 | +api_app = FastAPI() |
| 178 | +# ... 在api_app上定义您的API端点 ... |
| 179 | + |
| 180 | +# 一个单独的MCP服务器应用 |
| 181 | +mcp_app = FastAPI() |
| 182 | + |
| 183 | +# 从API应用创建MCP服务器 |
| 184 | +mcp = FastApiMCP( |
| 185 | + api_app, |
| 186 | + base_url="http://api-host:8001", # API应用将运行的URL |
| 187 | +) |
| 188 | + |
| 189 | +# 将MCP服务器挂载到单独的应用 |
| 190 | +mcp.mount(mcp_app) |
| 191 | + |
| 192 | +# 现在您可以分别运行两个应用: |
| 193 | +# uvicorn main:api_app --host api-host --port 8001 |
| 194 | +# uvicorn main:mcp_app --host mcp-host --port 8000 |
| 195 | +``` |
| 196 | + |
| 197 | +### 在MCP服务器创建后添加端点 |
| 198 | + |
| 199 | +如果您在创建MCP服务器后向FastAPI应用添加端点,您需要刷新服务器以包含它们: |
| 200 | + |
| 201 | +```python |
| 202 | +from fastapi import FastAPI |
| 203 | +from fastapi_mcp import FastApiMCP |
| 204 | + |
| 205 | +app = FastAPI() |
| 206 | +# ... 定义初始端点 ... |
| 207 | + |
| 208 | +# 创建MCP服务器 |
| 209 | +mcp = FastApiMCP(app) |
| 210 | +mcp.mount() |
| 211 | + |
| 212 | +# 在MCP服务器创建后添加新端点 |
| 213 | +@app.get("/new/endpoint/", operation_id="new_endpoint") |
| 214 | +async def new_endpoint(): |
| 215 | + return {"message": "Hello, world!"} |
| 216 | + |
| 217 | +# 刷新MCP服务器以包含新端点 |
| 218 | +mcp.setup_server() |
| 219 | +``` |
| 220 | + |
| 221 | +## 示例 |
| 222 | + |
| 223 | +请参阅[examples](examples)目录以获取完整示例。 |
| 224 | + |
| 225 | +## 使用SSE连接到MCP服务器 |
| 226 | + |
| 227 | +一旦您的集成了MCP的FastAPI应用运行,您可以使用任何支持SSE的MCP客户端连接到它,例如Cursor: |
| 228 | + |
| 229 | +1. 运行您的应用。 |
| 230 | + |
| 231 | +2. 在Cursor -> 设置 -> MCP中,使用您的MCP服务器端点的URL(例如,`http://localhost:8000/mcp`)作为sse。 |
| 232 | + |
| 233 | +3. Cursor将自动发现所有可用的工具和资源。 |
| 234 | + |
| 235 | +## 使用[mcp-proxy stdio](https://github.com/sparfenyuk/mcp-proxy?tab=readme-ov-file#1-stdio-to-sse)连接到MCP服务器 |
| 236 | + |
| 237 | +如果您的MCP客户端不支持SSE,例如Claude Desktop: |
| 238 | + |
| 239 | +1. 运行您的应用。 |
| 240 | + |
| 241 | +2. 安装[mcp-proxy](https://github.com/sparfenyuk/mcp-proxy?tab=readme-ov-file#installing-via-pypi),例如:`uv tool install mcp-proxy`。 |
| 242 | + |
| 243 | +3. 在Claude Desktop的MCP配置文件(`claude_desktop_config.json`)中添加: |
| 244 | + |
| 245 | +在Windows上: |
| 246 | +```json |
| 247 | +{ |
| 248 | + "mcpServers": { |
| 249 | + "my-api-mcp-proxy": { |
| 250 | + "command": "mcp-proxy", |
| 251 | + "args": ["http://127.0.0.1:8000/mcp"] |
| 252 | + } |
| 253 | + } |
| 254 | +} |
| 255 | +``` |
| 256 | +在MacOS上: |
| 257 | +```json |
| 258 | +{ |
| 259 | + "mcpServers": { |
| 260 | + "my-api-mcp-proxy": { |
| 261 | + "command": "/Full/Path/To/Your/Executable/mcp-proxy", |
| 262 | + "args": ["http://127.0.0.1:8000/mcp"] |
| 263 | + } |
| 264 | + } |
| 265 | +} |
| 266 | +``` |
| 267 | +通过在终端运行`which mcp-proxy`来找到mcp-proxy的路径。 |
| 268 | + |
| 269 | +4. Claude Desktop将自动发现所有可用的工具和资源 |
| 270 | + |
| 271 | +## 开发和贡献 |
| 272 | + |
| 273 | +感谢您考虑为FastAPI-MCP做出贡献!我们鼓励社区发布问题和拉取请求。 |
| 274 | + |
| 275 | +在开始之前,请参阅我们的[贡献指南](CONTRIBUTING.md)。 |
| 276 | + |
| 277 | +## 社区 |
| 278 | + |
| 279 | +加入[MCParty Slack社区](https://join.slack.com/t/themcparty/shared_invite/zt-30yxr1zdi-2FG~XjBA0xIgYSYuKe7~Xg),与其他MCP爱好者联系,提问,并分享您使用FastAPI-MCP的经验。 |
| 280 | + |
| 281 | +## 要求 |
| 282 | + |
| 283 | +- Python 3.10+(推荐3.12) |
| 284 | +- uv |
| 285 | + |
| 286 | +## 许可证 |
| 287 | + |
| 288 | +MIT许可证。版权所有 (c) 2024 Tadata Inc. |
| 289 | + |
| 290 | +--- |
| 291 | + |
| 292 | +This translation retains 100% of the original format, with all code blocks, links, and images unchanged, while translating the English text into fluent and accurate Chinese. |
0 commit comments