FastAPI MCP服务器与Gemini CLI的集成
该项目演示了如何创建FastAPI应用程序,用MCP(模型上下文协议)服务器包装它,并将其与Gemini CLI集成以进行自然语言交互。
项目结构
.
├── app.py # FastAPI application with REST endpoints
├── mcp_server.py # MCP server that exposes FastAPI endpoints as tools
├── requirements.txt # Python dependencies
├── gemini-config.json # Gemini CLI configuration file
└── README.md # This file特性
FastAPI应用程序(app.py)
- 用户管理:创建、读取和删除用户
- 任务管理:创建、读取、更新(完成)和删除任务
- 计算:执行数学运算(加、减、乘、除)
- 健康检查:监视应用程序状态
MCP服务器(mcp_server.py)
将11个FastAPI端点作为MCP工具公开:
get_health-检查API运行状况get_users-检索所有用户get_user-通过ID获取特定用户create_user-创建新用户delete_user-按ID删除用户get_tasks-检索所有任务get_task-按ID获取特定任务create_task-创建新任务complete_task-将任务标记为已完成delete_task-按ID删除任务calculate-进行数学计算
安装说明
1.安装依赖项
pip install -r requirements.txt2.启动FastAPI应用程序
在一个终端中,启动FastAPI服务器:
python app.py或者直接使用uvicorn:
uvicorn app:app --reload --host 0.0.0.0 --port 8000API将于 http://localhost:8000
3.安装Gemini CLI
使用npm全局安装Gemini CLI:
npm install -g @google/gemini-cli@latest4.配置Gemini CLI
将MCP服务器添加到Gemini CLI配置中。配置文件通常位于:
- 视窗:
%APPDATA%\Gemini CLI\config.json - macOS/Linux:
~/.config/gemini-cli/config.json
或者使用Gemini CLI命令:
gemini mcp add fastapi-mcp-server python mcp_server.py或者,您可以手动将其添加到Gemini CLI配置文件中:
{
"mcpServers": {
"fastapi-mcp-server": {
"command": "python",
"args": ["mcp_server.py"],
"env": {
"PYTHONPATH": "."
}
}
}
}备注:确保提供完整的路径 mcp_server.py 如果从其他目录运行。
5.验证设置
验证MCP服务器是否配置正确:
gemini mcp list使用Gemini CLI进行实际演示
设置好所有内容后,您可以使用自然语言通过Gemini CLI与FastAPI应用程序进行交互。以下是一些示例:
示例1:检查API运行状况
gemini> Check the health of the FastAPI applicationGemini CLI将调用 get_health 工具并返回API状态。
示例2:创建用户
gemini> Create a new user named John Doe with email john@example.com and age 30这将调用 create_user 工具具有提供的参数。
示例3:列出所有用户
gemini> Show me all users这使用了 get_users 从API检索所有用户的工具。
示例4:创建任务
gemini> Create a task titled "Complete MCP integration" with description "Integrate MCP server with Gemini CLI"这 create_task 将调用工具来创建任务。
示例5:执行计算
gemini> Calculate 25 multiplied by 4或者:
gemini> What is 100 divided by 5?这将使用 calculate 执行数学运算的工具。
示例6:完成任务
gemini> Mark task with ID 1 as completed这将调用 complete_task 工具。
示例7:获取任务详细信息
gemini> Show me task number 1使用 get_task 用于检索任务详细信息的工具。
示例8:删除用户
gemini> Delete user with ID 1调用 delete_user 工具。
直接测试API
您还可以直接使用curl或REST客户端测试FastAPI端点:
# Health check
curl http://localhost:8000/health
# Get all users
curl http://localhost:8000/users
# Create a user
curl -X POST http://localhost:8000/users \
-H "Content-Type: application/json" \
-d '{"name": "John Doe", "email": "john@example.com", "age": 30}'
# Create a task
curl -X POST http://localhost:8000/tasks \
-H "Content-Type: application/json" \
-d '{"title": "Test Task", "description": "This is a test task"}'
# Perform calculation
curl -X POST http://localhost:8000/calculate \
-H "Content-Type: application/json" \
-d '{"operation": "multiply", "a": 25, "b": 4}'API文档
FastAPI应用程序运行后,您可以访问以下位置访问交互式API文档:
- Swagger用户界面:
http://localhost:8000/docs - 重新记录:
http://localhost:8000/redoc
故障排除
未找到MCP服务器
如果Gemini CLI找不到MCP服务器:
- 启动Gemini CLI时,确保您位于正确的目录中
- 检查以下路径
mcp_server.py配置正确 - 验证Python是否在您的PATH中
连接错误
如果您遇到连接错误:
- 确保FastAPI应用程序在端口8000上运行
- 检查一下
API_URL在mcp_server.py匹配您的FastAPI服务器URL - 验证没有防火墙限制
导入错误
如果遇到导入错误:
- 确保安装了所有依赖项:
pip install -r requirements.txt - 验证您使用的是正确的Python环境
- 检查一下
mcp软件包安装正确
需求
- Python 3.8或更高版本
- Node.js和npm(用于Gemini CLI)
- 中列出的所有软件包
requirements.txt
许可证
这是一个用于教育目的的示例项目。
