天气与温度转换服务

提供实时天气数据和温度转换服务的多服务器MCP实现,集成LangChain/LangGraph支持AI聊天功能。

  • by Aliaksei-Kharlap
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 aliaksei-kharlap/mcp_server

使用说明

MCP服务器项目

多服务器MCP(模型上下文协议)实现,通过LangChain/LangGraph集成提供天气信息和温度转换服务。

特性

  • 气象局:通过OpenWeatherMap API提供实时天气数据,并提供全面的气象信息
  • 温度转换器:摄氏、华氏和开尔文单位之间的温度转换
  • LangChain/LangGraph集成:具有智能工具使用功能的高级AI聊天客户端
  • 自动启动:一个命令部署,具有自动服务器管理功能
  • FastMCP服务器:具有HTTP传输的MCP服务器

项目结构

mcp_servers/
├── weather/
│   ├── server.py              # Weather MCP server
│   └── tools/
│       └── weather_provider.py   # Weather service implementation
├── temperature/
│   ├── server.py              # Temperature MCP server
│   └── tools/
│       └── temperature_converter.py  # Temperature conversion service
├── chat_client.py             # MCP chat client with LangChain integration
├── start.sh                   # Automated startup script
├── pyproject.toml            # Poetry configuration and dependencies
└── README.md                 # Project documentation

安装

先决条件

  • Python 3.11+
  • 诗歌(推荐)或pip
  • OpenWeatherMap API密钥
  • DeepSeek API密钥

设置

  1. 克隆存储库:
git clone <repository-url>
cd MCP_server
  1. 安装依赖项:
# Using Poetry (recommended)
poetry install
  1. 配置环境变量: 创建一个 .env 包含以下变量的文件:
# OpenWeatherMap API
OPENWEATHER_API_KEY=your_openweather_api_key

# LLM Configuration (for LangChain client)
DEEPSEEK_API_KEY=your_deepseek_api_key
OPENROUTER_BASE_URL=https://openrouter.ai/api/v1

用法

快速入门(推荐)

使用提供的启动脚本在一个命令中运行所有内容:

chmod +x start.sh
./start.sh

此脚本将:

  1. 在端口8000上启动气象服务器
  2. 启动端口9000上的温度服务器
  3. 等待服务器初始化(2秒)
  4. 启动聊天客户端
  5. 聊天结束时自动清理服务器

手动设置

如果您更喜欢手动启动服务器:

  1. 天气服务器 (端口8000):
poetry run python mcp_servers/weather/server.py
  1. 温度服务器 (端口9000):
poetry run python mcp_servers/temperature/server.py
  1. 聊天客户端:
poetry run python chat_client.py

气象局

端点: http://127.0.0.1:8000/mcp

可用工具: get_weather

参数:

  • city (str):城市名称(例如“伦敦”、“纽约”)
  • units (str,可选):温度单位-“公制”、“英制”或“开尔文”(默认值:“公制”)

示例响应:

{
  "success": true,
  "city": "London",
  "country": "GB",
  "temperature": {
    "current": 15.2,
    "feels_like": 14.1,
    "min": 12.3,
    "max": 17.8,
    "unit": "°C"
  },
  "weather": {
    "main": "Clouds",
    "description": "overcast clouds",
    "icon": "04d"
  },
  "wind": {
    "speed": 3.5,
    "direction_degrees": 220,
    "direction_name": "SW",
    "unit": "m/s"
  },
  "humidity": 73,
  "pressure": {
    "value": 1013,
    "unit": "hPa"
  },
  "coordinates": {
    "latitude": 51.5074,
    "longitude": -0.1278
  }
}

温度转换服务

端点: http://127.0.0.1:9000/mcp

可用工具: convert_temperature

参数:

  • temperature (浮动):要转换的温度值
  • from_unit (str):源单位(“摄氏”、“华氏”、“c”、“f”、“k”)
  • to_unit (str):目标单位(“摄氏”、“华氏”、“c”、“f”、“k”)

示例响应:

{
  "success": true,
  "original_temperature": 25.0,
  "original_unit": "celsius",
  "converted_temperature": 77.0,
  "converted_unit": "fahrenheit",
  "conversion_formula": "°F = (°C × 9/5) + 32"
}

详细功能

天气提供商

  • 动车组:支持公制、英制和开尔文单位
  • 综合数据:温度、湿度、压力、风、能见度
  • 验证:输入验证

温度转换器

  • 多种格式:支持全名和缩写
  • 验证:绝对零度温度验证
  • 精确度:适当的倒圆和精确处理
  • 公式:返回透明度的转换公式

聊天客户端

  • 基本客户端:简单的MCP集成
  • 朗链客户端:先进的人工智能对话
  • 错误处理:全面的错误处理和恢复
  • 会话管理:聊天记录跟踪

配置

服务器端口

  • 气象服务: 8000
  • 温度服务: 9000

错误处理

该项目实施了全面的错误处理:

  • 验证错误:输入验证,并显示有用的错误消息
  • 日志记录:用于调试和监控的结构化日志记录

安全考虑

  • API密钥是从环境变量加载的