FastAPI与MCP服务器集成工具

该项目展示了如何构建FastAPI应用,将其封装为MCP服务器,并与Gemini CLI集成以实现直接工具调用。

  • by arrehman3
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 arrehman3/MCP

使用说明

FastAPI+MCP服务器与Gemini CLI集成

本项目演示了如何构建FastAPI应用程序,将其包装为MCP(模型上下文协议)服务器,并将其与Gemini CLI集成以进行直接工具调用。

项目结构

├── sample_app.py      # FastAPI application with user and task management
├── mcp_server.py      # MCP server that wraps the FastAPI app
├── requirements.txt   # Python dependencies
├── setup.sh          # Setup script
├── demo.sh           # Interactive demonstration script
├── test_integration.py # Integration test script
├── venv/             # Python virtual environment
└── README.md         # This file

特性

FastAPI应用程序(sample_app.py)

  • 用户管理:创建、读取具有姓名、电子邮件和年龄的用户
  • 任务管理:创建、读取、更新、删除任务
  • 统计:获取用户和任务的概述
  • 健康检查:基本健康监测终点

MCP服务器(mcp_server.py)

  • 工具集成:将所有FastAPI端点作为MCP工具公开
  • 错误处理:正确的HTTP错误处理和日志记录
  • 类型安全:完整类型注释和模式验证

可用的MCP工具

  1. get_app_info -获取基本应用程序信息
  2. get_health -检查应用程序运行状况
  3. get_users -列出所有用户
  4. create_user -创建新用户
  5. get_user -按ID获取用户
  6. get_tasks -列出所有任务
  7. create_task -创建新任务
  8. get_task -按ID获取任务
  9. update_task -更新现有任务
  10. delete_task -删除任务
  11. get_stats -获取用户和任务统计信息

快速开始

选项1:自动演示

./demo.sh

此交互式脚本将指导您完成整个设置和测试过程。

选项2:手动设置

1.运行安装脚本

./setup.sh

2.启动FastAPI应用程序

source venv/bin/activate
python sample_app.py

FastAPI应用程序将在 http://localhost:8000

3.启动MCP服务器(在另一个终端中)

source venv/bin/activate
python mcp_server.py

4.安装Gemini CLI

npm install -g @google/gemini-cli@latest

5.将MCP服务器添加到Gemini CLI

gemini mcp add fastapi-sample stdio python $(pwd)/mcp_server.py

6.测试集成

# List available tools
gemini mcp list

# Call a tool
gemini call fastapi-sample get_app_info

# Create a user
gemini call fastapi-sample create_user --name "John Doe" --email "john@example.com" --age 30

# Get all users
gemini call fastapi-sample get_users

# Create a task
gemini call fastapi-sample create_task --title "Learn MCP" --description "Study Model Context Protocol" --user_id 1

# Get statistics
gemini call fastapi-sample get_stats

手动设置

如果您更喜欢手动设置:

1.安装Python依赖项

pip3 install -r requirements.txt

2.启动服务

  • FastAPI应用程序: python3 sample_app.py
  • MCP服务器: python3 mcp_server.py

3.安装和配置Gemini CLI

npm install -g @google/gemini-cli@latest
gemini mcp add fastapi-sample stdio python3 /path/to/mcp_server.py

API终点

FastAPI应用程序提供以下REST端点:

  • GET / -应用程序信息
  • GET /health -健康检查
  • GET /users -列出用户
  • POST /users -创建用户
  • GET /users/{user_id} -按ID获取用户
  • GET /tasks -列出任务
  • POST /tasks -创建任务
  • GET /tasks/{task_id} -按ID获取任务
  • PUT /tasks/{task_id} -更新任务
  • DELETE /tasks/{task_id} -删除任务
  • GET /stats -获取统计数据

MCP工具示例

创建和管理用户

# Create a user
gemini call fastapi-sample create_user --name "Alice Smith" --email "alice@example.com" --age 25

# Get user by ID
gemini call fastapi-sample get_user --user_id 1

# List all users
gemini call fastapi-sample get_users

创建和管理任务

# Create a task
gemini call fastapi-sample create_task --title "Complete project" --description "Finish the MCP integration" --user_id 1

# Update a task
gemini call fastapi-sample update_task --task_id 1 --title "Complete project" --description "Finish the MCP integration" --user_id 1 --completed true

# Delete a task
gemini call fastapi-sample delete_task --task_id 1

获取统计信息

gemini call fastapi-sample get_stats

故障排除

常见问题

  1. 端口已在使用中:确保端口8000可用于FastAPI应用程序
  2. MCP服务器连接失败:启动MCP服务器之前,请确保FastAPI应用程序正在运行
  3. 未找到Gemini CLI:确保安装了Node.js和npm,然后全局安装Gemini CLI

调试模式

要在调试模式下运行FastAPI应用程序:

uvicorn sample_app:app --reload --host 0.0.0.0 --port 8000

检查MCP服务器状态

gemini mcp list

发展

添加新端点

  1. 将端点添加到 sample_app.py
  2. 将相应的工具添加到 mcp_server.py 在 handle_list_tools() 函数
  3. 在中添加工具处理程序 handle_call_tool() 函数

测试

您可以使用curl直接测试FastAPI端点:

# Test app info
curl http://localhost:8000/

# Create a user
curl -X POST http://localhost:8000/users \
  -H "Content-Type: application/json" \
  -d '{"name": "Test User", "email": "test@example.com", "age": 30}'

# Get users
curl http://localhost:8000/users

许可证

该项目用于教育目的,展示了MCP集成模式。