weather-alert-mcp-v2
weather-alert-mcp v2 — 一个 MCP 工具,两种实现
一个 MCP(模型上下文协议)服务器,将美国国家气象局的实时警报暴露为可调用工具,因此任何 MCP 客户端——Claude Desktop、Claude Code 或 SDK 客户端——都可以按州查询当前生效的天气警报。
实现两次:一次用 Python,一次用 TypeScript,遵循相同的工具契约。客户端无法分辨它正在与哪一个通信。这正是基于协议而非 API 进行构建的意义所在,而这两种实现正是 MCP 的有趣之处变得可见的地方。
v1(仅 Python)位于 weather-alert-mcp,保持 原样不动。本仓库是其延续。
工具
工具 | 参数 | 返回 |
|
| 最多 5 条当前警报:事件、严重程度、影响区域、标题 |
数据源:https://api.weather.gov/alerts/active — 公开,无需 API 密钥,无需注册。
Related MCP server: Weather MCP Server
同一契约,两种派生方式
Python ( | TypeScript ( | |
Schema 来源 | 从类型提示生成 | 从 |
参数验证 | 信任客户端对 schema 的符合性 | 在边界处验证,在任何 HTTP 调用之前拒绝 |
传输 | stdio | stdio |
离线模式 |
|
|
工具契约 |
|
|
两者都从实现中派生客户端所看到的 JSON Schema,而不是单独维护它,因此契约不会与代码发生漂移。
TypeScript 服务器还会在协议边界进行验证。{"state": "Illinois"} 会在发出任何网络请求之前作为协议级错误返回,因此格式错误的工具调用不会产生任何代价:
MCP error -32602: Input validation error: Invalid arguments for tool get_alertsPython
pip install -r requirements.txt
python server.py # speaks MCP over stdio; a client launches this验证 — test_client.py 是一个真正的 MCP 客户端:它作为子进程启动服务器,完成 initialize 握手,列出所通告的工具,并调用其中一个。
python test_client.py # live
WX_MOCK=1 python test_client.py # protocol only, no networkTOOLS: ['get_alerts']
DESC: Get active National Weather Service alerts for a US state.
SCHEMA: {'properties': {'state': {'title': 'State', 'type': 'string'}}, 'required': ['state'], ...}
RESULT for IL:
Severe Thunderstorm Warning (Severe) — Lake County, IL
Severe Thunderstorm Warning issued for Lake County until 7:15 PM CDTTypeScript
cd ts
npm install
npm run build
node build/index.js # speaks MCP over stdio验证 — ts/test-client.mjs 执行相同的往返过程,并额外断言格式错误的参数会被拒绝:
cd ts
WX_MOCK=1 node test-client.mjs # protocol only, no network
node test-client.mjs TX # live NWS dataTOOLS: [ 'get_alerts' ]
SCHEMA: {"type":"object","properties":{"state":{"type":"string","minLength":2,"maxLength":2,
"pattern":"^[A-Za-z]{2}$","description":"Two-letter US state code, e.g. \"IL\" or \"CA\""}},
"required":["state"],"additionalProperties":false}
RESULT:
Severe Thunderstorm Warning (Severe) — Lake County, IL
Severe Thunderstorm Warning issued for Lake County until 7:15 PM CDT
VALIDATION: rejected bad input as expected — MCP error -32602: Input validation error有关 TypeScript 实现的详细信息,请参阅 ts/README.md。
从 Claude Desktop 使用
将其中一个(或两个)添加到 claude_desktop_config.json:
{
"mcpServers": {
"weather-alerts": {
"command": "python",
"args": ["/absolute/path/to/server.py"]
},
"weather-alerts-ts": {
"command": "node",
"args": ["/absolute/path/to/ts/build/index.js"]
}
}
}两者注册相同的工具名称,因此一次运行一个,除非你想对它们进行比较。
备注
stdout是协议通道。 任何写入那里的内容都会破坏 JSON-RPC 流,这就是启动日志输出到stderr的原因。错误会浮出水面,而不是被吞掉。 上游 API 故障会抛给客户端,而不是返回“无警报”,因为“无警报”与真正平静的状态无法区分。
WX_MOCK=1在没有网络调用的情况下运行完整路径 — 在 CI 中很有用,也用于在出现问题时将协议故障与上游 API 故障区分开来。警报被截断为前 5 个要素,以将工具输出保持在合理的上下文预算之内。
已知限制
只有一个只读工具。没有资源、没有提示、没有采样——MCP 中除工具注册之外的部分未在此处涉及。
仅支持 stdio 传输;不支持 HTTP/SSE 传输。
NWS 端点要求提供
User-Agent;根据 API 的既定政策,两种实现都将其设置为联系字符串。
This server cannot be deployed
Maintenance
Related MCP Connectors
US weather for AI agents: active NWS alerts by state, 5-period forecasts by lat/lon. Paid per call.
Hosted MCP server for Xweather weather data: conditions, forecasts, alerts, and more.
US weather alerts (NWS): warnings, watches. $0.01/query. Register in-session — free testnet funds.
Get US weather forecasts, active alerts, and current observations.
Related MCP Servers
- FlicenseNot gradedqualityDmaintenanceThis is a Model Context Protocol (MCP) server that provides weather information using the National Weather Service (NWS) API. Features Get weather alerts for a US state Get weather forecast for a specific location (using latitude and longitude)-
- AlicenseNot gradedqualityDmaintenanceProvides weather forecasts and active alerts for US locations using the National Weather Service API. It supports both local stdio and remote HTTP/SSE transport modes for flexible integration with MCP clients.159 npmGPL 3.0
- FlicenseBqualityDmaintenanceProvides weather forecasts and alerts for US locations using the National Weather Service API. Enables users to retrieve active alerts by state and detailed forecasts by coordinates via MCP tools.2-
- FlicenseNot gradedqualityDmaintenanceReal-time US weather data via MCP tools: fetch weather alerts by state and forecasts by coordinates using the NOAA NWS API.-