天气查询工具

一个基于Model Context Protocol (MCP)的天气查询工具,通过Anthropic Claude模型和自定义天气MCP服务器,提供实时天气警报和预报查询功能。

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

使用说明

🌤️ MCP天气助理

介绍

该项目演示了如何构建 工具增强会话人工智能 使用 模型上下文协议(MCP).
它连接了一个 人物克劳德 模型到a 自定义天气MCP服务器 从 美国国家气象局(NWS API).

🚀 特性

  • FastMCP服务器 (weather.py)

    • 显示以下工具:
      • get_alerts(state) → 获取美国各州的活跃天气警报。
      • get_forecast(latitude, longitude) → 获取给定位置的详细天气预报。
    • 使用官方 NWS API.
  • MCP客户端 (client.py)

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

🛠️ 安装(使用紫外线)

1.安装uv

🪟 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

使用代理技能的首次运行结果