FastAPI模型上下文协议服务器

一个为FastAPI应用提供全面内省和操作工具的服务,适用于API发现、文档生成、调试和开发等场景。

  • by Nabeelshar
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 Nabeelshar/fastapi-mcp-server

使用说明

FastAPI MCP服务器

一个模型上下文协议(MCP)服务器,提供全面的工具来反思和使用FastAPI应用程序。

特性

🔧 工具

  1. load_fastapi_app -从模块路径加载FastAPI应用程序
  2. list_routes -列出所有具有可选过滤功能的路线
  3. get_route_details -获取特定路线的全面详细信息
  4. get_openapi_schema -提取OpenAPI模式
  5. list_models -列出应用程序中使用的所有Pydantic模型
  6. get_model_schema -获取特定模型的详细架构
  7. search_routes -按查询和条件搜索路线
  8. 分析依赖关系 -分析依赖注入使用情况
  9. get_route_source -查看路由处理程序的源代码

安装

pip install -e .

用法

使用克劳德桌面

添加到您的 claude_desktop_config.json:

{
  "mcpServers": {
    "fastapi": {
      "command": "python",
      "args": [
        "-m",
        "fastapi_mcp_server.server"
      ],
      "cwd": "/path/to/your/fastapi/project"
    }
  }
}

独立

# From your FastAPI project directory
python -m fastapi_mcp_server.server

示例用法

1.加载您的FastAPI应用程序

Tool: load_fastapi_app
Arguments: {"app_path": "main:app"}

2.列出所有路线

Tool: list_routes
Arguments: {}

或者使用过滤器:

Tool: list_routes
Arguments: {
  "method_filter": "GET",
  "tag_filter": "users"
}

3.获取路线详细信息

Tool: get_route_details
Arguments: {
  "path": "/users/{user_id}",
  "method": "GET"
}

4.搜索路线

Tool: search_routes
Arguments: {
  "query": "authentication",
  "has_auth": true
}

5.查看路由源代码

Tool: get_route_source
Arguments: {
  "path": "/users",
  "method": "POST"
}

6.列出所有型号

Tool: list_models
Arguments: {}

7.获取模型架构

Tool: get_model_schema
Arguments: {"model_name": "User"}

8.获取OpenAPI模式

Tool: get_openapi_schema
Arguments: {}

或者对于特定路径:

Tool: get_openapi_schema
Arguments: {
  "include_only": ["/users", "/users/{user_id}"]
}

9.分析依赖关系

Tool: analyze_dependencies
Arguments: {}

FastAPI应用程序示例

看 example_app.py 用于测试示例FastAPI应用程序。

用例

🔍 API发现

  • 快速了解存在哪些端点
  • 按功能或路径查找路线
  • 探索API结构

📚 文档生成

  • 提取OpenAPI模式
  • 获取详细的参数信息
  • 查看带有示例的模型模式

🐛 调试

  • 检查路线配置
  • 查看依赖注入层次结构
  • 检查参数验证

🔨 发展

  • 从模式生成客户端代码
  • 修改前了解现有API
  • 使用特定型号查找所有路线

🧪 测试

  • 列出测试覆盖率的所有端点
  • 获取测试用例的参数要求
  • 验证响应模型

需求

  • Python 3.10+
  • FastAPI 0.100.0+
  • MCP 1.0.0+
  • Pydantic 2.0.0+

贡献

欢迎投稿!此工具可帮助开发人员更有效地使用FastAPI应用程序。

许可证

麻省理工学院许可证-您可以在项目中自由使用!

作者

作为FastAPI生态系统的一部分创建,旨在改善开发人员使用AI辅助开发的体验。