天气警报工具

一个基于FastMCP和Claude Desktop的天气警报工具,通过api.weather.gov API获取美国各州的实时天气警报信息。

  • by nahilahmed
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 nahilahmed/mcp-weather-alert-tool

使用说明

🌩️ 使用FastMCP+Claude桌面的MCP天气警报工具

该项目演示了如何构建 MCP(模型上下文协议) 使用官方工具 FastMCP Python SDK。它与 克劳德桌面 使AI助手能够检索 天气警报 美国各州通过 api.weather.gov API


🚀 这个项目做什么

  • ✅ 实现自定义MCP服务器 FastMCP
  • 🌐 用途 httpx 从以下位置调用实时天气警报 api.weather.gov
  • 📦 返回结构化信息:事件、严重性、描述和指令
  • 🧠 与Claude Desktop集成
  • 🌈 可以通过MCP Inspector进行交互式测试

🧱 技术栈

工具/库 目的
mcp[cli] 带CLI工具的MCP服务器工具包
httpx 异步HTTP客户端
uv 现代Python包/依赖管理器
Claude Desktop LLM+MCP的人工智能界面
MCP检查器 调试MCP端点的UI

📁 项目结构

mcpcrashcourse/
├── server/
│   └── weather.py         # MCP server logic (get_alerts)
├── pyproject.toml         # Project metadata + dependencies
├── uv.lock                # Locked versions (auto-generated by uv)
└── README.md              # You're reading it!

🧠 工具逻辑: get_alerts

@mcp.tool()
async def get_alerts(state: str) -> list[dict]:
    # Fetch weather alerts for a given U.S. state (e.g., 'CA')
    url = "https://api.weather.gov/alerts/active"
    params = {"area": state.upper()}
    async with httpx.AsyncClient() as client:
        response = await client.get(url, params=params)
        data = response.json()

    return [
        {
            "event": alert.get("event"),
            "severity": alert.get("severity"),
            "description": alert.get("description"),
            "instruction": alert.get("instruction"),
        }
        for alert in data.get("features", [])
        if alert.get("properties")
    ]

🛠️ 安装和设置

1.创建和配置项目

pipx install uv               # install uv if you haven't
uv init mcpcrashcourse        # start new project
cd mcpcrashcourse
uv add "mcp[cli]" httpx       # add dependencies

2.添加您的工具

放置您的 weather.py 在 server/ 文件夹,包含以下逻辑 get_alerts.


🧪 运行和检查

使用MCP Inspector启动

uv run mcp dev server/weather.py

然后打开 http://localhost:6757 致:

  • 查看您的 get_alerts 工具
  • 尝试使用输入调用它,如下所示 "TX"

🤖 Claude桌面集成

  1. 更新您的 claude_desktop_config.json:
{
  "mcpServers": {
    "weather": {
      "command": "uv",
      "args": [
        "run",
        "--with",
        "mcp[cli]",
        "mcp",
        "run",
        "<your-project-directory>/server/weather.py"
      ]
    }
  }
}
  1. 重新启动克劳德桌面
  2. 提示:

    “德克萨斯州的天气警报是什么?”

Claude将调用您的MCP工具并显示结构化输出。


💡 什么是MCP?

这 模型上下文协议 规范应用程序的发送方式 上下文 (数据、工具、提示)发送给LLM。它将 “上下文提供程序” 从LLM层开始。

三个核心图元:

类型 用例 类比
工具 执行函数 POST端点
资源 提供静态数据 GET端点
提示 可重用交互 LLM模板

📎 鸣谢


📄 许可证

这个项目是在MIT许可证下开源的。