天气数据服务

一个基于Weatherstack API的MCP服务器,提供全球城市天气信息查询、健康检查及SSE传输支持。

  • by sharu-91
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 sharu-91/mcp_project

使用说明

MCP气象服务器

模型上下文协议(MCP)服务器,使用Weatherstack API提供天气信息工具。

特性

  • 🌤️ 获取任何城市的天气信息
  • 📊 详细的天气数据,包括温度、湿度、风速等
  • 🔍 用于监控的健康检查端点
  • 🚀 MCP检查器的SSE(服务器发送事件)传输支持

先决条件

项目结构

mcp_project/
├── src/
│   └── mcp/
│       ├── mcptool.py          # Main MCP server with weather tools
│       ├── test_server_sse.py  # Test server with SSE support
│       ├── testmcp.py          # Simple test MCP server
│       └── weatherbylangandlat.py  # Weather by coordinates tool
├── main.py                     # Entry point
├── pyproject.toml              # Project dependencies
├── .env.example                # Environment variables template
└── README.md                   # This file

安装说明

1.克隆存储库

git clone <repository-url>
cd mcp_project

2.安装依赖项

使用 uv (推荐):

uv sync

或使用 pip:

pip install -e .

3.配置环境变量

  1. 创建一个 .env 项目根目录中的文件:

    touch .env
    
  2. 将Weatherstack API密钥添加到 .env 文件:

    WEATHERSTACK_API_KEY=your_actual_api_key_here
    

    注:

    • 从获取API密钥 风化堆
    • 这 .env 运行服务器时会自动加载文件(通过python-dotenv)
    • 或者,您可以导出变量: export WEATHERSTACK_API_KEY=your_key

4.运行服务器

python src/mcp/mcptool.py

服务器将于启动 http://localhost:4747

可用工具

get_weather(city: str)

获取任何城市的天气信息。

例子:

get_weather("New York")

退货:

{
  "city": "New York",
  "country": "United States",
  "temperature": "22°C",
  "description": "Partly cloudy",
  "humidity": "65%",
  "wind_speed": "15 km/h",
  "feels_like": "24°C",
  "status": "success"
}

logger_weather(city: str)

通过详细的日志记录获取天气信息。

get_info()

获取服务器信息和可用工具。

API终点

  • 健康检查: http://localhost:4747/health
  • SSE端点: http://localhost:4747/sse (供MCP检查员使用)

MCP检验员测试

  1. 启动服务器:

    python src/mcp/mcptool.py
    
  2. 打开 MCP检查员 在浏览器中

  3. 连接到: http://localhost:4747

发展

运行测试

您可以使用测试文件测试服务器:

  • test_server_sse.py -支持SSE的测试服务器
  • testmcp.py -简单测试MCP服务器

添加新工具

要添加新工具,请使用 @mcp.tool() 装饰师:

@mcp.tool()
async def my_new_tool(param: str) -> dict:
    """Tool description"""
    # Your tool logic here
    return {"result": "success"}

故障排除

API关键问题

  • 确保 .env 文件存在并包含 WEATHERSTACK_API_KEY
  • 在验证您的API密钥是否有效 Weatherstack仪表板

端口已在使用中

如果端口4747已在使用中,您可以在中修改该端口 mcptool.py:

uvicorn.run(starlette_app, host="0.0.0.0", port=4747)  # Change port number

安全说明

⚠️ 重要提示: 永远不要承诺你的 .env 文件或API密钥。这 .gitignore 文件已配置为排除 .env 文件夹。

许可证

[在此处添加您的许可证]

贡献

[在此处添加贡献指南]