1. 背景
在之前的介绍中,我们了解了 Model Context Protocol (MCP) 的概念、架构及解决的问题,但缺少具体的示例来理解整套框架如何落地。
本文基于官方示例——获取天气预报,来理解 MCP 落地的整条链路。
2. MCP 示例
该案例是构建一个简单的 MCP 天气预报服务器,并将其连接到主机(Claude for Desktop)。从基本设置开始,逐步发展到更复杂的使用场景。
大模型虽然能力非常强,但其弊端是内容具有非实时性。例如没有获取天气预报和严重天气警报的能力。因此我们将使用 MCP 来解决这一问题。
构建一个服务器,该服务器提供两个工具:获取警报(get-alerts)和获取预报(get-forecast)。然后,将该服务器连接到 MCP 主机(在本例中为 Claude for Desktop)。
首先配置环境:
(1)安装 uv
curl -LsSf https://astral.sh/uv/install.sh | sh
安装完成后,会提示:
downloading uv 0.6.9 aarch64-apple-darwin no checksums to verify installing to /Users/nicolas/.local/bin uv uvx everything's installed!
(2)安装所需的依赖包

(3)在 server.py 中构建相应的 get-alerts 和 get-forecast 工具:
from typing import Any
import asyncio
import httpx
from mcp.server.models import InitializationOptions
import mcp.types as types
from mcp.server import NotificationOptions, Server
import mcp.server.stdio
NWS_API_BASE = "https://api.weather.gov"
USER_AGENT = "weather-app/1.0"
server = Server("weather")
#@server.list_tools() - 注册用于列出可用工具的处理器
#@server.call_tool() - 注册用于执行工具调用的处理器
@server.list_tools()
async def handle_list_tools() -> list[types.Tool]:
""" List available tools. Each tool specifies its arguments using JSON Schema validation. """
[
types.Tool(
name=,
description=,
inputSchema={
: ,
: {
: {
: ,
: ,
},
},
: [],
},
),
types.Tool(
name=,
description=,
inputSchema={
: ,
: {
: {
: ,
: ,
},
: {
: ,
: ,
},
},
: [, ],
},
),
]
() -> [, ] | :
headers = {
: USER_AGENT,
:
}
:
response = client.get(url, headers=headers, timeout=)
response.raise_for_status()
response.json()
Exception:
() -> :
props = feature[]
(
)
() -> [types.TextContent | types.ImageContent | types.EmbeddedResource]:
arguments:
ValueError()
name == :
state = arguments.get()
state:
ValueError()
state = state.upper()
(state) != :
ValueError()
httpx.AsyncClient() client:
alerts_url =
alerts_data = make_nws_request(client, alerts_url)
alerts_data:
[types.TextContent(=, text=)]
features = alerts_data.get(, [])
features:
[types.TextContent(=, text=)]
formatted_alerts = [format_alert(feature) feature features[:]]
alerts_text = + .join(formatted_alerts)
[
types.TextContent(
=,
text=alerts_text
)
]
name == :
:
latitude = (arguments.get())
longitude = (arguments.get())
(TypeError, ValueError):
[types.TextContent(
=,
text=
)]
(- <= latitude <= ) (- <= longitude <= ):
[types.TextContent(
=,
text=
)]
httpx.AsyncClient() client:
lat_str =
lon_str =
points_url =
points_data = make_nws_request(client, points_url)
points_data:
[types.TextContent(=, text=)]
properties = points_data.get(, {})
forecast_url = properties.get()
forecast_url:
[types.TextContent(=, text=)]
forecast_data = make_nws_request(client, forecast_url)
forecast_data:
[types.TextContent(=, text=)]
periods = forecast_data.get(, {}).get(, [])
periods:
[types.TextContent(=, text=)]
formatted_forecast = []
period periods:
forecast_text = (
)
formatted_forecast.append(forecast_text)
forecast_text = + .join(formatted_forecast)
[types.TextContent(
=,
text=forecast_text
)]
:
ValueError()
():
mcp.server.stdio.stdio_server() (read_stream, write_stream):
server.run(
read_stream,
write_stream,
InitializationOptions(
server_name=,
server_version=,
capabilities=server.get_capabilities(
notification_options=NotificationOptions(),
experimental_capabilities={},
),
),
)
__name__ == :
asyncio.run(main())










