开始构建你自己的服务器,以在 Claude for Desktop 和其他客户端中使用

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

我们要构建什么?

许多 LLM(包括 Claude)目前无法获取天气预报和严重天气警报。让我们使用 MCP 来解决这个问题!

我们将构建一个服务器,公开两个工具:get-alertsget-forecast,然后将服务器连接到 MCP 主机(在本例中是 Claude for Desktop):

Server开发者 - 图1

Server开发者 - 图2

服务器可以连接到任何客户端。这里我们选择 Claude for Desktop 只是为了简化流程,同时我们还提供了 构建你自己的客户端 的指南,以及其他客户端的列表

为什么选择 Claude for Desktop 而不是 Claude.ai?

因为服务器是在本地运行的,而 MCP 目前仅支持桌面端主机。远程主机的支持正在开发中。


MCP 核心概念

MCP 服务器可以提供三种主要的功能:

  1. 资源(Resources):客户端可以读取的类似文件的数据(例如 API 响应或文件内容)。
  2. 工具(Tools):LLM 可以调用的函数(需要用户批准)。
  3. 提示(Prompts):帮助用户完成特定任务的预设模板。

本教程主要关注 工具(Tools)


先决知识

本快速入门教程假设你熟悉以下内容:

  • Python
  • LLM(如 Claude)

系统要求

  • 安装 Python 3.10 或更高版本。
  • 你需要使用 Python MCP SDK 1.2.0 或更高版本。

设置开发环境

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

MacOS/Linux

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

请确保在安装后重启终端,以便 uv 命令能够被识别。

然后,创建并设置我们的项目:

  1. # 创建新项目目录
  2. uv init weather
  3. cd weather
  4. # 创建虚拟环境并激活
  5. uv venv
  6. source .venv/bin/activate
  7. # 安装依赖
  8. uv add "mcp[cli]" httpx
  9. # 创建服务器文件
  10. touch weather.py

构建你的服务器

导入必要的包并初始化 MCP 实例

weather.py 文件顶部添加以下内容:

  1. from typing import Any
  2. import httpx
  3. from mcp.server.fastmcp import FastMCP
  4. # 初始化 FastMCP 服务器
  5. mcp = FastMCP("weather")
  6. # 常量
  7. NWS_API_BASE = "https://api.weather.gov"
  8. USER_AGENT = "weather-app/1.0"

FastMCP 类使用 Python 类型提示和文档字符串自动生成工具定义,使得创建和维护 MCP 工具变得更加简单。


编写辅助函数

接下来,我们添加用于从美国国家气象局(NWS)API 获取和格式化数据的辅助函数:

  1. async def make_nws_request(url: str) -> dict[str, Any] | None:
  2. """向 NWS API 发送请求,并进行错误处理"""
  3. headers = {
  4. "User-Agent": USER_AGENT,
  5. "Accept": "application/geo+json"
  6. }
  7. async with httpx.AsyncClient() as client:
  8. try:
  9. response = await client.get(url, headers=headers, timeout=30.0)
  10. response.raise_for_status()
  11. return response.json()
  12. except Exception:
  13. return None
  14. def format_alert(feature: dict) -> str:
  15. """将天气警报格式化为可读的字符串"""
  16. props = feature["properties"]
  17. return f"""
  18. 事件: {props.get('event', '未知')}
  19. 地区: {props.get('areaDesc', '未知')}
  20. 严重程度: {props.get('severity', '未知')}
  21. 描述: {props.get('description', '无描述')}
  22. 指引: {props.get('instruction', '无具体指引')}
  23. """

实现工具方法

工具执行处理程序负责执行工具的具体逻辑。我们添加以下两个方法:

  1. @mcp.tool()
  2. async def get_alerts(state: str) -> str:
  3. """获取某个美国州的天气警报。
  4. 参数:
  5. state: 两个字母的州代码(如 CA、NY)
  6. """
  7. url = f"{NWS_API_BASE}/alerts/active/area/{state}"
  8. data = await make_nws_request(url)
  9. if not data or "features" not in data:
  10. return "无法获取警报或未找到警报信息。"
  11. if not data["features"]:
  12. return "该州当前没有活跃的天气警报。"
  13. alerts = [format_alert(feature) for feature in data["features"]]
  14. return "\n---\n".join(alerts)
  15. @mcp.tool()
  16. async def get_forecast(latitude: float, longitude: float) -> str:
  17. """获取指定位置的天气预报。
  18. 参数:
  19. latitude: 纬度
  20. longitude: 经度
  21. """
  22. # 获取天气预报的 API 端点
  23. points_url = f"{NWS_API_BASE}/points/{latitude},{longitude}"
  24. points_data = await make_nws_request(points_url)
  25. if not points_data:
  26. return "无法获取该位置的天气预报数据。"
  27. # 获取天气预报 URL
  28. forecast_url = points_data["properties"]["forecast"]
  29. forecast_data = await make_nws_request(forecast_url)
  30. if not forecast_data:
  31. return "无法获取详细的天气预报信息。"
  32. # 格式化天气预报
  33. periods = forecast_data["properties"]["periods"]
  34. forecasts = []
  35. for period in periods[:5]: # 仅显示最近 5 个时段的预报
  36. forecast = f"""
  37. {period['name']}:
  38. 温度: {period['temperature']}°{period['temperatureUnit']}
  39. 风速: {period['windSpeed']} {period['windDirection']}
  40. 天气: {period['detailedForecast']}
  41. """
  42. forecasts.append(forecast)
  43. return "\n---\n".join(forecasts)

运行服务器

最后,我们初始化并运行服务器:

  1. if __name__ == "__main__":
  2. mcp.run(transport='stdio')

你的服务器现在已经完成!运行以下命令来确认是否正常工作:

  1. uv run weather.py

在 Claude for Desktop 上测试你的服务器

  1. 安装 Claude for Desktop下载最新版本)。
  2. 配置 Claude for Desktop 以使用 MCP 服务器。在 ~/Library/Application Support/Claude/claude_desktop_config.json 中添加以下内容:
  1. {
  2. "mcpServers": {
  3. "weather": {
  4. "command": "uv",
  5. "args": [
  6. "--directory",
  7. "/ABSOLUTE/PATH/TO/PARENT/FOLDER/weather",
  8. "run",
  9. "weather.py"
  10. ]
  11. }
  12. }
  13. }
  1. 重启 Claude for Desktop,然后你应该能在工具栏看到 MCP 工具的图标。

MCP 工作原理

当你向 Claude for Desktop 询问天气时:

  1. 客户端将你的问题发送给 Claude。
  2. Claude 识别并选择合适的 MCP 工具。
  3. 客户端通过 MCP 服务器执行该工具。
  4. 服务器返回查询结果。
  5. Claude 处理并生成自然语言回复。
  6. 你在 Claude for Desktop 上看到结果!

这样,你的 MCP 天气服务器就成功构建并集成到 Claude for Desktop 了! 🎉