气象MCP工具🌤️
模型上下文协议(MCP)服务器,使用 wttr.in API该项目是在了解MCP系统的学习阶段创建的。
______________________________________________________________________
📖 关于本项目
这是一个定制的MCP工具,演示了构建MCP服务器的核心概念。工具:
- 需要一个 位置 作为用户输入(字符串格式)
- 使用构造天气API URL wttr.in
- 获取实时温度和天气数据
- 将格式化结果返回给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.batLinux/Mac:
chmod +x setup.sh run.sh
./setup.sh
./run.sh选项2:手动设置
- 克隆存储库
git clone https://github.com/Namangarg-01/Mcp-server-weather-tool.git
cd Mcp-server-weather-tool- 创建虚拟环境
python -m venv .venv
# Activate:
# Windows:
.venv\Scripts\activate
# Linux/Mac:
source .venv/bin/activate- 安装依赖项
pip install -r requirements.txt- 运行服务器
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代码流
- 用户询问Claude 关于天气
- Claude调用MCP工具 带位置参数
- MCP服务器 通过stdio接收请求
check_weather()工具 被调用get_weather()功能:
- URL对位置进行编码 - 构造wttr.in URL - 通过HTTP获取天气数据
- 返回响应 致克劳德
- 克劳德格式 并向用户显示
关键组件
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
- 检查路径是否正确
claude_desktop_config.json是 绝对的 - 验证Python是否可访问:
python --version - 配置更改后重新启动Claude Desktop
- 手动检查服务器运行情况:
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🚀 未来的增强功能
要添加的潜在功能:
- \[\]多日预报支持
- \[\]天气警报和警告
- \[\]历史天气数据
- \[\]支持坐标(纬度/经度)
- \[\]缓存经常请求的位置
- \[\]单位转换(摄氏度↔ 华氏温度)
- \[\]更详细的天气信息(湿度、风、气压)
______________________________________________________________________
🤝 贡献
这是一个学习项目,欢迎投稿!
如何做出贡献:
- 分叉存储库
- 创建特征分支:
git checkout -b feature/amazing-feature - 进行更改
- 使用MCP检查员进行彻底测试
- 承诺:
git commit -m 'Add amazing feature' - 推:
git push origin feature/amazing-feature - 打开拉取请求
贡献领域:
- 文档改进
- 错误处理增强功能
- 其他天气特征
- 测试和错误修复
- 性能优化
______________________________________________________________________
📄 许可证
MIT许可证-有关详细信息,请参阅许可证文件。
______________________________________________________________________
📧 联系
Garg。
- github: @纳曼加尔-01
- 项目: Mcp服务器天气工具
______________________________________________________________________
🙏 致谢
- 边学习边构建 模型上下文协议(MCP)
- 气象数据由 wttr.in
- 多亏了 Anthropic 用于Claude和MCP文档
- 特别感谢 FastMCP 用于简化MCP服务器创建的库
______________________________________________________________________
📊 项目统计
- 语言: Python 3.8+
- 依赖关系: 2(mcp、fastmcp)
- 代码行: ~50
- 设置时间: \<2分钟
- 所需API密钥: 0 ✨
______________________________________________________________________
由...制作❤️ 在MCP学习过程中
*天气检查愉快!* 🌤️
