Skip to main content
Glama

天气 MCP 服务器

这是一个模型上下文协议 (MCP) 服务器,使用国家气象局 (NWS) API 提供天气信息。

特征

  • 获取美国某州的天气警报

  • 获取特定位置的天气预报(使用纬度和经度)

Related MCP server: MCP Weather Server

要求

  • Python 3.10+

  • FastMCP

  • httpx

安装

  1. 克隆此存储库

  2. 设置虚拟环境:

    python -m venv .venv source .venv/bin/activate # On Windows, use `.venv\Scripts\activate`
  3. 使用 uv 安装所需的软件包:

    uv add "mcp[cli]" httpx

配置

服务器使用具有以下设置的配置文件config.py

  • NWS_API_BASE :国家气象局 API 的基本 URL

  • USER_AGENT :向 NWS API 发出请求时使用的 User-Agent 字符串

  • LOG_LEVEL :日志记录级别(例如“INFO”、“DEBUG”)

  • LOG_FORMAT :日志消息的格式字符串

  • REQUEST_TIMEOUT :API 请求的超时时间(秒)

您可以在config.py文件中修改这些设置来定制服务器行为。

用法

独立运行服务器

要独立运行服务器:

  1. 如果尚未激活,请激活虚拟环境:

    source .venv/bin/activate # On Windows, use `.venv\Scripts\activate`
  2. 运行服务器:

    python weather.py

与 Cline 一起使用

Weather MCP 服务器已添加到 Cline 配置中。在 Cline 中使用它:

  1. 确保 Cline 已正确设置并运行。

  2. Weather MCP 服务器将作为名为“weather”的工具提供。

  3. 您可以在 Cline 中使用以下工具:

    a. get_alerts(state: str) :获取美国某个州的天气警报(使用两个字母的州代码,例如“CA”代表加利福尼亚州)b. get_forecast(latitude: float, longitude: float) :获取特定位置的天气预报

Cline 中的用法示例:

# Get alerts for California result = await mcp.call_tool("weather.get_alerts", state="CA") print(result) # Get forecast for San Francisco (approximate coordinates) result = await mcp.call_tool("weather.get_forecast", latitude=37.7749, longitude=-122.4194) print(result)

注意:调用 MCP 工具的具体语法可能因您的 Cline 设置而异。请参阅 Cline 文档以获取最新的使用说明。

错误处理和日志记录

该服务器改进了 API 请求和日志记录的错误处理功能。如果请求过程中发生错误,服务器将记录该错误并返回相应的错误消息。日志将以配置的日志级别和格式打印到控制台。

测试

单元测试

运行单元测试:

python -m unittest test_weather.py

这些测试涵盖了get_alertsget_forecast工具的基本功能。

手动测试

要手动测试服务器,您可以使用 MCP 客户端调用提供的工具。以下是一些示例命令:

# Get alerts for California result = await mcp.call_tool("get_alerts", state="CA") print(result) # Get forecast for San Francisco (approximate coordinates) result = await mcp.call_tool("get_forecast", latitude=37.7749, longitude=-122.4194) print(result)

贡献

欢迎贡献代码!欢迎提交 Pull 请求。

执照

该项目是开源的,可根据MIT 许可证使用。

-
security - not tested
F
license - not found
-
quality - not tested

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/JackKuo666/Weather-MCP-Server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server