手把手教你开发一个 AI 可用的天气查询 MCP 服务

前言

最近 AI 生态里MCP(Model Context Protocol) 火出圈了!它能让 AI 大模型无缝调用外部工具、API、本地资源,相当于给 AI 装上了「手脚」。

今天我就带大家从零开发一个基于美国国家气象局 NWS API 的天气 MCP 服务,支持查询州级天气预警、经纬度精准天气预报,代码可直接运行、可对接 Claude / 其他支持 MCP 的 AI 模型。

全程 Node.js + TypeScript,新手也能轻松上手!

一、先搞懂:什么是 MCP?

MCP 全称 Model Context Protocol ,由 Anthropic 开源,是一套标准化协议,作用只有一个:让大模型安全、规范地调用外部服务 / 工具。

简单理解:

  • 你写一个 MCP 服务 = 给 AI 注册一套「API 工具」
  • 大模型触发 = 自动调用你写的方法
  • 返回结果 = 模型直接理解并自然语言回复你

我们今天要做的就是:天气查询 MCP 服务 ✅

二、项目准备

1. 初始化项目

bash 复制代码
mkdir mcp-weather-server 
cd mcp-weather-server 
npm init -y
  1. 安装依赖
bash 复制代码
npm install @modelcontextprotocol/sdk zod 
npm install -D typescript @types/node ts-node
  1. tsconfig.json 基础配置
json 复制代码
{ "compilerOptions": { "target": "ES2022", "module": "ESNext", "moduleResolution": "node", "esModuleInterop": true, "skipLibCheck": true, "strict": true, "outDir": "./dist" }, "include": ["src/**/*"] }

三、完整代码实现

新建 src/index.ts,直接复制以下完整代码:

ts 复制代码
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; import { z } from "zod"; // NWS 公共API配置(无需KEY,免费使用) const NWS_API_BASE = "https://api.weather.gov"; const USER_AGENT = "weather-app/1.0"; // 创建MCP服务实例 const server = new McpServer({ name: "weather", version: "1.0.0", }); // 请求工具函数 async function makeNWSRequest<T>(url: string): Promise<T | null> { const headers = { "User-Agent": USER_AGENT, Accept: "application/geo+json", }; try { const response = await fetch(url, { headers }); if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); return await response.json() as T; } catch (error) { console.error("API请求失败:", error); return null; } } // 类型定义 interface AlertFeature { properties: { event?: string; areaDesc?: string; severity?: string; status?: string; headline?: string; }; } interface ForecastPeriod { name?: string; temperature?: number; temperatureUnit?: string; windSpeed?: string; windDirection?: string; shortForecast?: string; } interface AlertsResponse { features: AlertFeature[] } interface PointsResponse { properties: { forecast?: string } } interface ForecastResponse { properties: { periods: ForecastPeriod[] } } // 格式化预警信息 function formatAlert(feature: AlertFeature): string { const p = feature.properties; return [ `事件: ${p.event || "未知"}`, `地区: ${p.areaDesc || "未知"}`, `等级: ${p.severity || "未知"}`, `状态: ${p.status || "未知"}`, `概要: ${p.headline || "无"}`, "---", ].join("\n"); } // ============================================== // 工具1:获取美国某州的天气预警 // ============================================== server.registerTool( "get_alerts", { description: "获取指定州的天气预警信息(美国)", inputSchema: z.object({ state: z.string().length(2).describe("州代码,如 CA、NY"), }), }, async ({ state }) => { const code = state.toUpperCase(); const data = await makeNWSRequest<AlertsResponse>(`${NWS_API_BASE}/alerts?area=${code}`); if (!data) return { content: [{ type: "text", text: "获取预警失败" }] }; if (!data.features.length) return { content: [{ type: "text", text: `${code}暂无预警` }] }; const text = `【${code} 天气预警】\n\n${data.features.map(formatAlert).join("\n")}`; return { content: [{ type: "text", text }] }; } ); // ============================================== // 工具2:根据经纬度获取天气预报 // ============================================== server.registerTool( "get_forecast", { description: "根据经纬度获取天气预报(仅支持美国)", inputSchema: z.object({ latitude: z.number().min(-90).max(90).describe("纬度"), longitude: z.number().min(-180).max(180).describe("经度"), }), }, async ({ latitude, longitude }) => { // 第一步:获取网格点 const pointData = await makeNWSRequest<PointsResponse>( `${NWS_API_BASE}/points/${latitude.toFixed(4)},${longitude.toFixed(4)}` ); if (!pointData || !pointData.properties.forecast) { return { content: [{ type: "text", text: "不支持该地区(仅美国境内)" }] }; } // 第二步:获取预报 const forecastData = await makeNWSRequest<ForecastResponse>(pointData.properties.forecast); if (!forecastData || !forecastData.properties.periods.length) { return { content: [{ type: "text", text: "暂无预报数据" }] }; } const forecastText = forecastData.properties.periods .map(p => [ `${p.name}:`, `温度:${p.temperature}°${p.temperatureUnit}`, `风速:${p.windSpeed} ${p.windDirection}`, p.shortForecast, "---", ].join("\n")) .join("\n"); return { content: [{ type: "text", text: `【${latitude},${longitude} 天气预报】\n\n${forecastText}` }] }; } ); // 启动服务 async function main() { const transport = new StdioServerTransport(); await server.connect(transport); console.error("Weather MCP 服务已启动"); } main().catch(err => { console.error("服务启动失败", err); process.exit(1); });
相关推荐
AskHarries5 分钟前
一个免费工具网站,每个月竟然有近 300 万访问量???
后端
周末程序猿3 小时前
当 99% 的代码由 AI 编写,Code Review 该怎么做?
后端·ai编程
ZhengEnCi3 小时前
前端日志和后端日志怎么记:分级、JSON 格式、按天分文件与保留策略
前端·后端·python
JellyfishMIX5 小时前
AgentScope-Java 核心机制(内循环) ReAct loop 源码分析
java·后端·agent
Rain的Java大神之路6 小时前
Spring Boot 4 把自动配置拆成 47 个模块后,我的 `dependency:tree` 翻天了:一份从排查到治理的完整实战手册
java·spring boot·后端
狗头大军之江苏分军6 小时前
前端转行干能干什么?我转行试了下(被迫)
前端·后端
孟陬6 小时前
Foobar 技术周刊#2:约会已死?用户急剧下滑,约会App大概率会弱化约会属性转向轻陪伴。我有个建议:
前端·后端·rust
NEIL_XU_8 小时前
自研提效工具——Bug通:打通测试与开发的「最后一公里」
后端·ai编程
Mikko79 小时前
Redis 主从切换期间可用吗?6.2 实测哨兵切换:应用不可写 33 秒,主从连不上、数据不一致,以及设了 ACL 后哨兵不切换
java·运维·redis·后端