天气信息应用

一个基于FastAPI和MCP服务器的天气信息应用,提供单城市/多城市天气查询及API健康检查功能,支持Gemini CLI集成。

  • by Raffay0177
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 Raffay0177/MCP-server-using-FAST-MCP

使用说明

带MCP服务器的天气信息应用程序

一个简单的天气信息应用程序,使用FastAPI构建,并与MCP(模型上下文协议)服务器集成,用于Gemini CLI。

📋 需求检查表

  • ✅ FastAPI天气应用程序
  • ✅ MCP服务器实现
  • ✅ Gemini CLI集成
  • ✅ MCP工具演示
  • ✅ 屏幕录制(参见 SCREEN_RECORDING_GUIDE.md)

🎥 屏幕录制

重要: 此存储库包括一个屏幕录制,演示:

  1. MCP服务器正在运行
  2. gemini mcp list 显示可用工具的命令
  3. 所有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配合使用
  • 多种工具:获取单个/多个城市的天气,健康检查

安装

  1. 克隆此存储库:
git clone <your-repo-url>
cd "MCp derver using FAST MCP"
  1. 安装依赖项:
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一起使用

  1. 启动Gemini CLI
  2. 列出可用的MCP工具:
gemini mcp list
  1. 使用工具:
# 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服务器的屏幕录制:

  1. 启动FastAPI服务器: python weather_api.py
  2. 打开Gemini CLI
  3. 展示 gemini mcp list 查看可用工具的命令
  4. 演示每个工具:
    • get_weather 对于一个城市
    • get_weather_batch 适用于多个城市
    • check_api_health
  5. 展示答案以及它们是如何协同工作的

项目文件

  • 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语法是否正确