在本教程中,我们将构建一个简单的 MCP 天气服务器,并将其连接到一个 host,即 Claude for Desktop。我们将从基本设置开始,然后逐步介绍更复杂的用例。

我们将构建什么

许多 LLM 目前不具备获取天气预报和恶劣天气警报的能力。让我们使用 MCP 来解决这个问题!

我们将构建一个服务器,它暴露两个 tools:get-alertsget-forecast。然后,我们将服务器连接到一个 MCP host(在本例中是 Claude for Desktop):

服务器可以连接到任何 client。我们在这里选择 Claude for Desktop 是为了简单起见,但我们也有关于构建你自己的 client 的指南以及其他 clients 列表

核心 MCP 概念

MCP 服务器可以提供三种主要类型的能力:

  1. Resources: 可以被 clients 读取的类文件数据(如 API 响应或文件内容)
  2. Tools: 可以被 LLM 调用的函数(需要用户批准)
  3. Prompts: 预先编写的模板,帮助用户完成特定任务

本教程将主要关注 tools。

让我们开始构建我们的天气服务器!你可以在这里找到我们将构建的完整代码。

前提知识

本快速入门假设你熟悉:

  • Python
  • LLMs,如 Claude

系统要求

  • 已安装 Python 3.10 或更高版本。
  • 你必须使用 Python MCP SDK 1.2.0 或更高版本。

设置你的环境

首先,让我们安装 uv 并设置我们的 Python 项目和环境:

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

之后请务必重启你的终端,以确保 uv 命令被识别。

现在,让我们创建并设置我们的项目:

# 为我们的项目创建一个新 directory
uv init weather
cd weather

# 创建 virtual environment 并激活它
uv venv
source .venv/bin/activate

# 安装 dependencies
uv add "mcp[cli]" httpx

# 创建我们的 server file
touch weather.py

现在让我们深入构建你的服务器。

构建你的服务器

导入 packages 并设置 instance

将这些添加到你的 weather.py 文件的顶部:

from typing import Any
import httpx
from mcp.server.fastmcp import FastMCP

# 初始化 FastMCP server
mcp = FastMCP("weather")

# Constants
NWS_API_BASE = "https://api.weather.gov"
USER_AGENT = "weather-app/1.0"

FastMCP class 使用 Python type hints 和 docstrings 来自动生成 tool definitions,从而轻松创建和维护 MCP tools。

Helper functions

接下来,让我们添加 helper functions,用于查询和格式化来自 National Weather Service API 的数据:

async def make_nws_request(url: str) -> dict[str, Any] | None:
    """向 NWS API 发送请求,并进行适当的错误处理。"""
    headers = {
        "User-Agent": USER_AGENT,
        "Accept": "application/geo+json"
    }
    async with httpx.AsyncClient() as client:
        try:
            response = await client.get(url, headers=headers, timeout=30.0)
            response.raise_for_status()
            return response.json()
        except Exception:
            return None

def format_alert(feature: dict) -> str:
    """将警报 feature 格式化为可读的字符串。"""
    props = feature["properties"]
    return f"""
事件: {props.get('event', 'Unknown')}
区域: {props.get('areaDesc', 'Unknown')}
严重性: {props.get('severity', 'Unknown')}
描述: {props.get('description', 'No description available')}
指示: {props.get('instruction', 'No specific instructions provided')}
"""

实现 tool execution

Tool execution handler 负责实际执行每个 tool 的逻辑。让我们添加它:

@mcp.tool()
async def get_alerts(state: str) -> str:
    """获取美国州的天气警报。

    Args:
        state: 两个字母的美国州代码(例如 CA、NY)
    """
    url = f"{NWS_API_BASE}/alerts/active/area/{state}"
    data = await make_nws_request(url)

    if not data or "features" not in data:
        return "无法获取警报或未找到警报。"

    if not data["features"]:
        return "该州没有活跃的警报。"

    alerts = [format_alert(feature) for feature in data["features"]]
    return "\n---\n".join(alerts)

@mcp.tool()
async def get_forecast(latitude: float, longitude: float) -> str:
    """获取某个位置的天气预报。

    Args:
        latitude: 位置的纬度
        longitude: 位置的经度
    """
    # 首先获取预报网格 endpoint
    points_url = f"{NWS_API_BASE}/points/{latitude},{longitude}"
    points_data = await make_nws_request(points_url)

    if not points_data:
        return "无法获取此位置的预报数据。"

    # 从 points response 中获取预报 URL
    forecast_url = points_data["properties"]["forecast"]
    forecast_data = await make_nws_request(forecast_url)

    if not forecast_data:
        return "无法获取详细预报。"

    # 将 periods 格式化为可读的预报
    periods = forecast_data["properties"]["periods"]
    forecasts = []
    for period in periods[:5]:  # 仅显示接下来的 5 个 periods
        forecast = f"""
{period['name']}:
温度: {period['temperature']}°{period['temperatureUnit']}
: {period['windSpeed']} {period['windDirection']}
预报: {period['detailedForecast']}
"""
        forecasts.append(forecast)

    return "\n---\n".join(forecasts)

运行 server

最后,让我们初始化并运行 server:

if __name__ == "__main__":
    # 初始化并运行 server
    mcp.run(transport='stdio')

你的 server 已经完成!运行 uv run weather.py 以确认一切正常。

现在让我们从现有的 MCP host,Claude for Desktop 测试你的 server。

使用 Claude for Desktop 测试你的 server

Claude for Desktop 尚不适用于 Linux。Linux 用户可以继续阅读 构建 client 教程,以构建一个连接到我们刚刚构建的 server 的 MCP client。

首先,确保你已安装 Claude for Desktop。你可以在这里安装最新版本。 如果你已经安装了 Claude for Desktop,请确保它已更新到最新版本。

我们需要为你想要使用的任何 MCP servers 配置 Claude for Desktop。为此,请在文本编辑器中打开 ~/Library/Application Support/Claude/claude_desktop_config.json 中的 Claude for Desktop App configuration。如果该 file 不存在,请确保创建它。

例如,如果你安装了 VS Code

code ~/Library/Application\ Support/Claude/claude_desktop_config.json

然后,你将在 mcpServers key 中添加你的 servers。只有正确配置了至少一个 server,MCP UI 元素才会显示在 Claude for Desktop 中。

在本例中,我们将添加我们的单个天气服务器,如下所示:

Python
{
    "mcpServers": {
        "weather": {
            "command": "uv",
            "args": [
                "--directory",
                "/ABSOLUTE/PATH/TO/PARENT/FOLDER/weather",
                "run",
                "weather.py"
            ]
        }
    }
}

你可能需要在 command 字段中放入 uv executable 的完整 path。你可以在 MacOS/Linux 上运行 which uv 或在 Windows 上运行 where uv 来获取它。

确保你传入的是你的 server 的绝对 path。

这告诉 Claude for Desktop:

  1. 有一个名为 “weather” 的 MCP server
  2. 通过运行 uv --directory /ABSOLUTE/PATH/TO/PARENT/FOLDER/weather run weather.py 来启动它

保存 file,并重新启动 Claude for Desktop

使用 commands 测试

让我们确保 Claude for Desktop 正在接收我们在 weather server 中暴露的两个 tools。你可以通过查找锤子 icon 来做到这一点:

单击锤子 icon 后,你应该看到列出了两个 tools:

如果你的 server 未被 Claude for Desktop 接收,请继续阅读 故障排除 部分以获取 debug 提示。

如果锤子 icon 已经显示,你现在可以通过在 Claude for Desktop 中运行以下 commands 来测试你的 server:

  • Sacramento 的天气怎么样?
  • Texas 有哪些活跃的天气警报?

由于这是美国国家气象局,因此查询仅适用于美国的位置。

底层发生了什么

当你提出问题时:

  1. client 将你的问题发送给 Claude
  2. Claude 分析可用的 tools 并决定使用哪些 tool
  3. client 通过 MCP server 执行选择的 tool
  4. 结果被发回给 Claude
  5. Claude 制定自然语言响应
  6. 响应显示给你!

故障排除

有关更高级的故障排除,请查看我们的 MCP Debug 指南

后续步骤