使用说明
带MCP服务器的天气信息应用程序
一个简单的天气信息应用程序,使用FastAPI构建,并与MCP(模型上下文协议)服务器集成,用于Gemini CLI。
📋 需求检查表
- ✅ FastAPI天气应用程序
- ✅ MCP服务器实现
- ✅ Gemini CLI集成
- ✅ MCP工具演示
- ✅ 屏幕录制(参见
SCREEN_RECORDING_GUIDE.md)
🎥 屏幕录制
重要: 此存储库包括一个屏幕录制,演示:
- MCP服务器正在运行
gemini mcp list显示可用工具的命令- 所有MCP工具的使用(
get_weather,get_weather_batch,check_api_health)
看 SCREEN_RECORDING_GUIDE.md 有关详细的录制说明。
项目结构
.
├── weather_api.py # FastAPI weather application
├── mcp_server.py # MCP server exposing weather tools
├── requirements.txt # Python dependencies
├── mcp_config.json # Gemini CLI MCP configuration
├── demo.py # Demo script for testing
└── README.md # This file
特性
- FastAPI天气API:提供天气信息的RESTful API
- MCP服务器:将天气功能作为MCP工具公开
- Gemini CLI集成:已准备好与谷歌的Gemini CLI配合使用
- 多种工具:获取单个/多个城市的天气,健康检查
安装
- 克隆此存储库:
git clone <your-repo-url>
cd "MCp derver using FAST MCP"
- 安装依赖项:
pip install -r requirements.txt
运行应用程序
步骤1:启动FastAPI天气服务器
在一个终端中:
python weather_api.py
API将于 http://localhost:8000
您可以测试它:
# Using curl
curl http://localhost:8000/weather?city=London
# Or using the browser
http://localhost:8000/weather?city=Paris
步骤2:为MCP配置Gemini CLI
MCP服务器使用stdio传输。创建或更新Gemini CLI配置文件:
在Windows上:
%APPDATA%\Google\Gemini CLI\mcp_config.json
在macOS/Linux上:
~/.config/google-gemini-cli/mcp_config.json
配置示例:
{
"mcpServers": {
"weather-info": {
"command": "python",
"args": ["<absolute-path-to-mcp_server.py>"],
"env": {}
}
}
}
对于Windows,使用完整路径,如:
{
"mcpServers": {
"weather-info": {
"command": "python",
"args": ["B:\\MCp derver using FAST MCP\\mcp_server.py"],
"env": {}
}
}
}
步骤3:与Gemini CLI一起使用
- 启动Gemini CLI
- 列出可用的MCP工具:
gemini mcp list
- 使用工具:
# Get weather for a city
gemini mcp call weather-info get_weather --city "Tokyo"
# Get weather for multiple cities
gemini mcp call weather-info get_weather_batch --cities "London,Paris,New York"
# Check API health
gemini mcp call weather-info check_api_health
可用的MCP工具
1. get_weather
获取单个城市的当前天气信息。
参数:
city(必填):城市名称country(可选):国家名称
例子:
gemini mcp call weather-info get_weather --city "London" --country "UK"
2. get_weather_batch
同时获取多个城市的天气信息。
参数:
cities(必填):以逗号分隔的城市列表
例子:
gemini mcp call weather-info get_weather_batch --cities "Tokyo,Seoul,Beijing"
3. check_api_health
检查天气API是否正常运行。
例子:
gemini mcp call weather-info check_api_health
测试
运行演示脚本以测试设置:
python demo.py
API终点
FastAPI服务器提供:
GET /-API信息GET /health-健康检查GET /weather?city=<name>&country=<name>-获取天气(Get)POST /weather-获取天气(使用JSON正文进行POST)
屏幕录制说明
要创建演示MCP服务器的屏幕录制:
- 启动FastAPI服务器:
python weather_api.py - 打开Gemini CLI
- 展示
gemini mcp list查看可用工具的命令 - 演示每个工具:
get_weather对于一个城市get_weather_batch适用于多个城市check_api_health
- 展示答案以及它们是如何协同工作的
项目文件
weather_api.py-FastAPI天气应用程序mcp_server.py-MCP服务器暴露天气工具demo.py-测试和演示脚本get_path.py-帮助获取正确的配置路径test_mcp_structure.py-验证MCP导入和结构requirements.txt-Python依赖关系mcp_config.json-Gemini CLI配置示例
文档
README.md-此文件(主要文档)QUICK_START.md-快速设置指南setup_instructions.md-详细的设置说明SCREEN_RECORDING_GUIDE.md-演示视频制作指南PROJECT_SUMMARY.md-完整的项目概述
备注
- 天气数据是模拟/仿真的,用于演示目的
- 在使用MCP工具之前,请确保FastAPI服务器正在运行
- MCP服务器通过HTTP与FastAPI服务器通信
- 配置中的所有路径都必须是绝对路径
故障排除
MCP服务器未连接:
- 确保FastAPI服务器在端口8000上运行
- 检查以下路径
mcp_server.py配置中的内容是正确和绝对的 - 验证Python是否在您的PATH中
工具未出现:
- 更新配置后重新启动Gemini CLI
- 检查MCP服务器日志是否有错误
- 验证配置JSON语法是否正确
