🌩️ 使用FastMCP+Claude桌面的MCP天气警报工具
该项目演示了如何构建 MCP(模型上下文协议) 使用官方工具 FastMCP Python SDK。它与 克劳德桌面 使AI助手能够检索 天气警报 美国各州通过 api.weather.gov API
______________________________________________________________________
🚀 这个项目做什么
- ✅ 实现自定义MCP服务器
FastMCP - 🌐 用途
httpx从以下位置调用实时天气警报 api.weather.gov - 📦 返回结构化信息:事件、严重性、描述和指令
- 🧠 与Claude Desktop集成
- 🌈 可以通过MCP Inspector进行交互式测试
______________________________________________________________________
🧱 技术栈
| 工具/库 | 目的 |
|---|---|
mcp[cli] | 带CLI工具的MCP服务器工具包 |
httpx | 异步HTTP客户端 |
uv | 现代Python包/依赖管理器 |
| Claude Desktop | LLM+MCP的人工智能界面 |
| MCP检查器 | 调试MCP端点的UI |
______________________________________________________________________
📁 项目结构
mcpcrashcourse/
├── server/
│ └── weather.py # MCP server logic (get_alerts)
├── pyproject.toml # Project metadata + dependencies
├── uv.lock # Locked versions (auto-generated by uv)
└── README.md # You're reading it!______________________________________________________________________
🧠 工具逻辑: get_alerts
@mcp.tool()
async def get_alerts(state: str) -> list[dict]:
# Fetch weather alerts for a given U.S. state (e.g., 'CA')
url = "https://api.weather.gov/alerts/active"
params = {"area": state.upper()}
async with httpx.AsyncClient() as client:
response = await client.get(url, params=params)
data = response.json()
return [
{
"event": alert.get("event"),
"severity": alert.get("severity"),
"description": alert.get("description"),
"instruction": alert.get("instruction"),
}
for alert in data.get("features", [])
if alert.get("properties")
]______________________________________________________________________
🛠️ 安装和设置
1.创建和配置项目
pipx install uv # install uv if you haven't
uv init mcpcrashcourse # start new project
cd mcpcrashcourse
uv add "mcp[cli]" httpx # add dependencies2.添加您的工具
放置您的 weather.py 在 server/ 文件夹,包含以下逻辑 get_alerts.
______________________________________________________________________
🧪 运行和检查
使用MCP Inspector启动
uv run mcp dev server/weather.py然后打开 http://localhost:6757 致:
- 查看您的
get_alerts工具 - 尝试使用输入调用它,如下所示
"TX"
______________________________________________________________________
🤖 Claude桌面集成
- 更新您的
claude_desktop_config.json:
{
"mcpServers": {
"weather": {
"command": "uv",
"args": [
"run",
"--with",
"mcp[cli]",
"mcp",
"run",
"/server/weather.py"
]
}
}
}- 重新启动克劳德桌面
- 提示:
> “德克萨斯州的天气警报是什么?”
Claude将调用您的MCP工具并显示结构化输出。
______________________________________________________________________
💡 什么是MCP?
这 模型上下文协议 规范应用程序的发送方式 上下文 (数据、工具、提示)发送给LLM。它将 “上下文提供程序” 从LLM层开始。
三个核心图元:
| 类型 | 用例 | 类比 |
|---|---|---|
| 工具 | 执行函数 | POST端点 |
| 资源 | 提供静态数据 | GET端点 |
| 提示 | 可重用交互 | LLM模板 |
______________________________________________________________________
📎 鸣谢
- FastMCP Python SDK
- api.weather.gov
- 克劳德桌面
- 基于以下示例的项目结构 MCP官方文件
______________________________________________________________________
📄 许可证
这个项目是在MIT许可证下开源的。
