天气助手

一个基于Model Context Protocol (MCP)的对话式AI工具,通过连接Anthropic Claude模型和自定义天气MCP服务器,实时获取美国国家气象局(NWS API)的天气警报和预报。

  • by Ranxin2023
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 Ranxin2023/WeatherMCP

使用说明

🌤️ MCP 天气助手

引言

这个项目展示了如何构建一个 工具增强的对话式人工智能 使用 模型上下文协议(MCP)。
它连接了一个 Anthropic Claude(注:此处“Anthropic”可能指代一个公司或项目名,而“Claude”是其旗下的一个模型或系统名,直接翻译为“Anthropic 克劳德”即可,但具体含义需结合上下文或官方解释) 模型到a 定制天气MCP服务器 该系统从(某来源)获取实时天气警报和预报 美国国家气象局(NWS API)。

🚀 特性

  • FastMCP 服务器 (weather.py)

    • 暴露了诸如以下工具:
      • get_alerts(state) → 按美国各州获取当前的天气警报。
      • get_forecast(latitude, longitude) → 获取给定地点的详细天气预报。
    • 使用官方 NWS API。
  • MCP 客户端 (client.py)

    • 启动MCP服务器并通过标准输入输出连接。
    • 与……集成 Anthropic Claude(注:这里“Anthropic”是公司名,“Claude”是该公司的AI模型名,所以整体可翻译为“Anthropic公司的Claude”) (claude-sonnet-4-5-20250929)。
    • 让我们来试试克劳德 自动调用天气工具 在回应用户查询时。
    • 提供一个 交互式聊天循环 用户可以在此输入查询,例如:

      “给我看看加利福尼亚州的天气预警”
      “37.77,-122.42附近地区的天气预报是什么?”

  • .env 支持

    • 自动加载您的Anthropic API密钥 dotenv。

🧠 它的工作原理

🧠 系统流程

User Query  →  MCP Client  →  Claude Model (via Anthropic API)
                    ↓
              MCP Server Tools
              ↙︎               ↘︎
    get_alerts(state)     get_forecast(lat, lon)

项目结构

BASICMCP/
│
├── mcp_client/
│   ├── .venv/                     # Virtual environment for MCP client
│   ├── .env                       # Stores Anthropic API key
│   ├── client.py                  # MCP client that connects to the weather server
│   ├── list_models.py             # Lists available Anthropic models
│   ├── code_explanation.md        # Explanation of the MCP client code
│   ├── pyproject.toml             # Project dependencies for MCP client
│   ├── uv.lock                    # uv dependency lock file
│   └── README.md                  # Documentation for the MCP client
│
├── weather/
│   ├── .venv/                     # Virtual environment for weather server
│   ├── weather.py                 # FastMCP server defining get_alerts & get_forecast tools
│   ├── StateCodes.py              # Contains state code mappings for NWS API
│   ├── main.py                    # Entry point to run the weather MCP server
│   ├── code_explanation.md        # Explanation of the weather server logic
│   ├── pyproject.toml             # Project dependencies for weather server
│   ├── uv.lock                    # uv dependency lock file
│   └── README.md                  # Documentation for the weather module
│
└── README.md                      # Main project documentation


架构概述

  1. weather.py (在天气文件夹内)
  • 运行一个 FastMCP服务器 定义了两个可调用的工具:
    • get_alerts(state) → 获取美国某州的当前天气警报。
    • get_forecast(latitude, longitude) → 返回给定坐标的短期预报。
  1. client.py (在mcp_client文件夹内)
  • 启动一个 MCP客户端 连接到天气服务器,初始化MCP会话,并通过Anthropic的Claude API允许交互。
  1. 当你向克劳德提问时,它可以决定 调用一个工具。
  2. 结果被发送回给克劳德(Claude)进行推理和生成回复。

🛠️ 安装(使用 uv)

1. 安装紫外线(设备)

🪟 Windows(PowerShell)

powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

🐧macOS / Linux

curl -LsSf https://astral.sh/uv/install.sh | sh

1. 克隆仓库

git clone https://github.com/yourusername/mcp-weather-assistant.git
cd mcp-weather-assistant

2. 设置虚拟环境

# Create a new directory for your project
uv init weather
cd weather

# Create virtual environment and activate it
uv venv
.venv\Scripts\activate

# Install dependencies
uv add mcp[cli] httpx anthropic python-dotenv

# (Optional) Create a new server file
New-Item weather.py

3. 运行项目

python mcp_client/client.py weather/weather.py