天气MCP工具

一个基于Model Context Protocol (MCP)的服务器,通过wttr.in API提供实时天气信息,适用于开发者学习MCP系统和天气数据集成。

  • by Namangarg-01
  • Shell
  • 本地部署
  • 更新于
在 GitHub 查看 Namangarg-01/Mcp-server-weather-tool

使用说明

气象MCP工具🌤️

模型上下文协议(MCP)服务器,使用 wttr.in API该项目是在了解MCP系统的学习阶段创建的。


📖 关于本项目

这是一个定制的MCP工具,演示了构建MCP服务器的核心概念。工具:

  1. 需要一个 位置 作为用户输入(字符串格式)
  2. 使用构造天气API URL wttr.in
  3. 获取实时温度和天气数据
  4. 将格式化结果返回给MCP客户端(Claude)

🎓 主要学习内容

  • ✅ 如何创建自定义MCP工具 -了解MCP服务器架构和工具注册
  • ✅ 使用MCP检查器 -部署前在本地测试和调试MCP工具
  • ✅ 将MCP工具与Claude连接 -配置通过 .config 与Claude Desktop集成的文件
  • ✅ 使用FastMCP -使用装饰器简化MCP服务器创建
  • ✅ 异步Python模式 -对非阻塞I/O操作使用async/await

🌟 特性

  • 🌤️ 实时天气信息 适用于全球任何地点
  • 🌍 不需要API密钥 -使用免费的wttr.in服务
  • 🔧 易于MCP集成 使用克劳德桌面
  • ⚡ 快速轻便 -使用Python内置的urllib
  • 📚 初学者友好的代码库 -非常适合学习MCP概念
  • 🎯 生产就绪 -一个命令设置和执行

📋 先决条件

  • Python 3.8或更高版本
  • 互联网连接 (获取天气数据)
  • 克劳德桌面 (可选,用于集成)

不需要API密钥! ✨


🚀 快速开始

选项1:自动设置(推荐)

窗户:

setup.bat
run.bat

Linux/Mac:

chmod +x setup.sh run.sh
./setup.sh
./run.sh

选项2:手动设置

  1. 克隆存储库
   git clone https://github.com/Namangarg-01/Mcp-server-weather-tool.git
   cd Mcp-server-weather-tool
  1. 创建虚拟环境
   python -m venv .venv
   
   # Activate:
   # Windows:
   .venv\Scripts\activate
   # Linux/Mac:
   source .venv/bin/activate
  1. 安装依赖项
   pip install -r requirements.txt
  1. 运行服务器
   python main.py

💻 用法

MCP检验员测试

在连接到Claude之前,请在本地测试您的工具:

# Install MCP Inspector (one-time)
npm install -g @modelcontextprotocol/inspector

# Run inspector
npx @modelcontextprotocol/inspector python main.py

这将打开一个web界面,您可以在其中:

  • 查看可用工具
  • 测试 check_weather 功能
  • 实时查看请求/响应

连接到克劳德桌面

要将此MCP服务器与Claude Desktop一起使用,请更新配置文件:

配置文件位置:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • 视窗: %APPDATA%\Claude\claude_desktop_config.json

添加此配置:

{
  "mcpServers": {
    "weather": {
      "command": "python",
      "args": ["C:/Users/YourName/Desktop/Mcp-server-weather-tool/main.py"]
    }
  }
}

重要提示:

  • 使用 绝对路径 到 main.py
  • 替换 YourName 使用您的实际用户名
  • 使用正斜杠 / 即使在Windows上

在更新之后, 重新启动克劳德桌面.

在Claude中使用

连接后,问克劳德:

"What's the weather in London?"
"Check the temperature in Tokyo"
"Tell me about the weather in Paris, France"
"How's the weather in Mumbai right now?"

Claude将自动使用您的MCP工具获取实时天气数据! 🎉


🔍 运作原理

架构概述

User Query → Claude Desktop → MCP Server → wttr.in API → Response
                  ↓              ↓              ↓
              (stdio)      check_weather()   HTTP GET

代码流

  1. 用户询问Claude 关于天气
  2. Claude调用MCP工具 带位置参数
  3. MCP服务器 通过stdio接收请求
  4. check_weather() 工具 被调用
  5. get_weather() 功能:
    • URL对位置进行编码
    • 构造wttr.in URL
    • 通过HTTP获取天气数据
  6. 返回响应 致克劳德
  7. 克劳德格式 并向用户显示

关键组件

main.py -MCP服务器

@mcp.tool()
async def check_weather(location: str) -> Dict[str, Any]:
    """Get weather info for specific location."""
    return get_weather(location)

工具/weather.py -天气费彻

def get_weather(location: str) -> dict:
    """Fetches weather using wttr.in API"""
    url = f"https://wttr.in/{location}?format=3"
    # Returns: "London: ⛅️  +15°C"

📁 项目结构

Mcp-server-weather-tool/
├── tools/
│   └── weather.py          # Weather fetching logic
├── main.py                 # MCP server entry point
├── requirements.txt        # Python dependencies
├── .gitignore             # Git ignore rules
├── setup.bat              # Windows setup script
├── setup.sh               # Linux/Mac setup script
├── run.bat                # Windows run script
├── run.sh                 # Linux/Mac run script
├── Screenshot 2026-02-13 205352.png
└── README.md              # This file

🐛 故障排除

服务器无法启动

错误: ModuleNotFoundError: No module named 'mcp'

# Reinstall dependencies
pip install -r requirements.txt

克劳德无法连接

错误: MCP server not responding

  1. 检查路径是否正确 claude_desktop_config.json 是 绝对的
  2. 验证Python是否可访问: python --version
  3. 配置更改后重新启动Claude Desktop
  4. 手动检查服务器运行情况: python main.py

无法获取天气数据

错误: urllib.error.URLError

  • 检查您的互联网连接
  • 尝试访问https://wttr.in/London在浏览器中
  • 服务可能暂时关闭

MCP检查员问题

# If npx command not found, install Node.js first
# Then install inspector globally:
npm install -g @modelcontextprotocol/inspector

🚀 未来的增强功能

要添加的潜在功能:

  • []多日预报支持
  • []天气警报和警告
  • []历史天气数据
  • []支持坐标(纬度/经度)
  • []缓存经常请求的位置
  • []单位转换(摄氏度↔ 华氏温度)
  • []更详细的天气信息(湿度、风、气压)

🤝 贡献

这是一个学习项目,欢迎投稿!

如何做出贡献:

  1. 分叉存储库
  2. 创建特征分支: git checkout -b feature/amazing-feature
  3. 进行更改
  4. 使用MCP检查员进行彻底测试
  5. 承诺: git commit -m 'Add amazing feature'
  6. 推: git push origin feature/amazing-feature
  7. 打开拉取请求

贡献领域:

  • 文档改进
  • 错误处理增强功能
  • 其他天气特征
  • 测试和错误修复
  • 性能优化

📄 许可证

MIT许可证-有关详细信息,请参阅许可证文件。


📧 联系

Garg。


🙏 致谢

  • 边学习边构建 模型上下文协议(MCP)
  • 气象数据由 wttr.in
  • 多亏了 Anthropic 用于Claude和MCP文档
  • 特别感谢 FastMCP 用于简化MCP服务器创建的库

📊 项目统计

  • 语言: Python 3.8+
  • 依赖关系: 2(mcp、fastmcp)
  • 代码行: ~50
  • 设置时间: <2分钟
  • 所需API密钥: 0 ✨

由...制作❤️ 在MCP学习过程中

天气检查愉快! 🌤️