使用说明
🌤️ MCP气象服务器
A. 模型上下文协议(MCP) 服务器,该服务器使用OpenWeatherMap API提供天气数据工具和资源。
🎯 什么是MCP?
模型上下文协议(MCP) 是AI助手连接到外部工具和数据源的标准化方式。该气象服务器实现MCP协议,以提供:
- 🛠️ MCP工具:
get_weather,get_forecast,search_cities - 📚 MCP资源:热门城市的天气数据和搜索功能
- 🔄 实时数据:来自OpenWeatherMap API的实时天气信息
- 📊 结构化访问:通过MCP协议基于JSON的天气数据
✨ MCP功能
可用工具
- get_weather:获取任何城市的当前天气状况
- 获取预测:获取任何城市的5天天气预报
- 搜索城市:搜索和地理编码全球城市
可用资源
- weather://new-york:纽约的天气数据
- weather://london:伦敦的天气数据
- weather://tokyo:东京的天气数据
- weather://paris:巴黎的天气数据
- weather://sydney:悉尼的天气数据
- weather://los-angeles:洛杉矶的天气数据
- weather://berlin:柏林的天气数据
- weather://search:常规搜索功能
🚀 快速开始
运行MCP服务器
- 克隆存储库
git clone https://github.com/yourusername/mcp-weather-server.git
cd mcp-weather-server
- 创建虚拟环境
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- 安装依赖项
pip install -r requirements.txt
- 设置API密钥
export API_KEY=your_openweathermap_api_key_here
- 运行MCP服务器
python run_mcp_server.py
与Claude Desktop一起使用
将此配置添加到您的Claude Desktop MCP设置中:
{
"mcpServers": {
"weather": {
"command": "python",
"args": ["/path/to/your/project/mcp_weather_server.py"],
"env": {
"API_KEY": "your_openweathermap_api_key_here"
}
}
}
}
获取OpenWeatherMap API密钥
- 注册地址: OpenWeatherMap
- 获取免费的API密钥
- 在环境变量中使用它
API_KEY
🏗️ MCP架构
- 协议:stdio上的模型上下文协议(MCP)
- 运输:JSON-RPC通信
- 服务器:带mcp库的Python
- API:OpenWeatherMap用于天气数据和地理编码
- 工具:3个MCP工具(获取天气、获取预测、搜索城市)
- 资源:8个MCP资源(热门城市+搜索)
📁 项目结构
mcp-weather-server/
├── mcp_weather_server.py # Main MCP server implementation
├── run_mcp_server.py # Server runner script
├── mcp_config.json # MCP configuration example
├── requirements.txt # Python dependencies (mcp library)
├── app.py # Legacy Flask app (for reference)
├── README.md # This file
└── .env # Environment variables (API_KEY)
🔧 MCP工具使用
get_weather工具
{
"name": "get_weather",
"arguments": {
"city": "London",
"units": "metric"
}
}
get_forecast工具
{
"name": "get_forecast",
"arguments": {
"city": "Tokyo",
"units": "imperial"
}
}
search_cities工具
{
"name": "search_cities",
"arguments": {
"query": "New York",
"limit": 5
}
}
📚 MCP资源访问
可以通过URI模式访问资源:
weather://new-york-纽约的天气数据weather://london-伦敦的天气数据weather://search-一般搜索信息
🛡️ 安全
- ✅ 通过环境变量正确外部化的API键
- ✅ MCP服务器在隔离进程中运行
- ✅ 所有工具参数的输入验证
- ✅ API故障的错误处理
- ✅ 存储库中没有硬编码的秘密
🧪 测试MCP服务器
您可以直接运行服务器来测试其功能:
# Test the server
python run_mcp_server.py
# The server communicates via stdio using JSON-RPC
# It's designed to be used by MCP clients like Claude Desktop
🤝 贡献
- 分叉存储库
- 创建要素分支
- 进行更改(确保符合MCP协议)
- 使用MCP客户端进行测试
- 提交拉取请求
📄 许可证
该项目是开源的,可在 MIT许可证.
🎉 MCP功能正在发挥作用
- 工具集成 → 人工智能可以直接调用天气工具
- 资源访问 → 通过URI获取结构化天气数据
- 实时数据 → 通过MCP获取实时天气信息
- 错误处理 → 优雅的MCP错误响应
- 协议遵从 → 完全支持MCP规范
旨在了解MCP。