用 TypeScript 把 GPT Action 接入 MCP
作者用天气接口跑通了把单个 OpenAPI 操作封装为 MCP 工具的最小示例,演示了输入校验和错误映射。示例采用无密钥接口,生产环境需另配认证。
AI 深度解读
已有一个通过 Action 调用 HTTP API 的 Custom GPT,想让 Claude 等 MCP 客户端也能调用同一个接口,可以从迁移一项操作开始。作者用 Open-Meteo 的当前天气查询做了个最小示例:下游 API 照常接收 HTTP 请求,Node 服务负责把这次调用包装成 MCP 工具。
作者实测了工具发现、正常查询和越界参数拦截。这篇最值得看的地方,是它把迁移时真正要重写的部分摆了出来:工具怎么描述、参数怎么校验、错误怎么告诉模型。
一个 Action 操作怎么变成 MCP 工具
示例把 OpenAPI 里 operationId 为 getCurrentWeather 的操作注册成 get_current_weather。它调用 GET /v1/forecast?latitude=..&longitude=..¤t_weather=true,Open-Meteo 的这个接口无需 API key。模型只填经纬度,current_weather=true 由服务端固定,响应也只取当前温度和风速。
作者用官方 TypeScript SDK @modelcontextprotocol/sdk 1.32.2、zod 4 和 Node 24 跑通了示例。安装命令是 npm i @modelcontextprotocol/sdk zod,服务端存为 server.mjs,测试客户端存为 test.mjs,两者都用 ES modules。
下面的服务端代码负责注册工具、校验经纬度,并把天气接口的响应整理成 MCP 客户端可读的结果。
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const server = new McpServer({ name: "weather-actions", version: "1.0.0" });
// OpenAPI operation: GET /v1/forecast?latitude=..&longitude=..¤t_weather=true (operationId: getCurrentWeather)
server.registerTool(
"get_current_weather",
{
description: "Current temperature (°C) and wind speed (km/h) for a latitude/longitude.",
inputSchema: { latitude: z.number().min(-90).max(90), longitude: z.number().min(-180).max(180) },
},
async ({ latitude, longitude }) => {
const url = `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t_weather=true`;
const res = await fetch(url);
if (!res.ok) {
return { isError: true, content: [{ type: "text", text: `Weather API returned ${res.status}. Check the coordinates and retry.` }] };
}
const { current_weather: w } = await res.json();
return { content: [{ type: "text", text: JSON.stringify({ temperature_c: w.temperature, windspeed_kmh: w.windspeed }) }] };
}
);
await server.connect(new StdioServerTransport());
看 inputSchema 那一行:纬度限制在 −90 到 90,经度限制在 −180 到 180。越界输入会在进入异步处理函数之前被 SDK 拦住。
两次调用能验证到哪一层
下面的测试客户端通过标准输入输出(stdio)连接服务端,先列工具,再分别传入合法坐标和越界坐标。
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
const c = new Client({ name: "t", version: "1" });
await c.connect(new StdioClientTransport({ command: "node", args: ["server.mjs"] }));
console.log(JSON.stringify((await c.listTools()).tools.map(t=>({name:t.name,required:t.inputSchema.required}))));
console.log(JSON.stringify(await c.callTool({ name: "get_current_weather", arguments: { latitude: 52.52, longitude: 13.41 } })));
console.log(JSON.stringify(await c.callTool({ name: "get_current_weather", arguments: { latitude: 999, longitude: 0 } })).slice(0,200));
await c.close();
留意第二次 callTool 传入的 latitude: 999。这次调用验证的是 SDK 的输入校验,拿不到天气接口返回非成功状态时的结果。
作者实测 listTools 只列出 get_current_weather,latitude 和 longitude 都是必填项。柏林坐标 52.52, 13.41 当时返回 {"temperature_c":2.6,"windspeed_kmh":9},这是实时读数。纬度 999 则得到 isError: true,报错指出 latitude 最大只能为 90。作者没有覆盖 !res.ok 分支,代码也没有处理 fetch 因网络失败抛出的异常。
迁移后,工具描述、错误和密钥归谁管
Custom GPT Action 根据 OpenAPI schema 配置。到了 MCP,模型通常靠工具名、描述和参数 schema 判断何时调用、该传什么。作者建议只注册模型确实会用到的操作,每项操作对应一个工具,描述用一句以动词开头的话写清用途。示例里的描述交代了返回值和单位,但实际以 Current 开头,和这条建议有出入。
作者也建议把错误写成模型能据此调整下一步的提示,通过 isError: true 返回。校验报错能直接指向出问题的字段。示例的 HTTP 错误提示只让调用方检查坐标后重试,遇到上游服务故障时,这句就给错了排查方向。
如果原 Action 用 API key,密钥原先配置在 GPT 编辑器里,迁移后可以放进 MCP 服务的环境变量。作者还提到通过 HTTP 暴露服务时可以用 OAuth。示例使用免密钥接口和 stdio,这两种鉴权配置都没演示。
我的判断
这个示例适合估算一次单操作迁移的代码量。它用真实的 HTTP 请求跑通了工具发现、调用和本地校验。若团队已经有 Node API 客户端,可以沿用现有请求封装,把工具注册、参数约束和返回内容放在 MCP 这一层。若只是想在 Custom GPT 内继续调用现有接口,保留 Action 就能满足需求。需要多一种客户端入口时,才有理由多维护一层适配。
描述以动词开头、错误提示给出修正方向,都是可操作的经验。作者没给对照数据,无法据此判断改写描述能提高多少工具选中率。作者只用 MCP 测试客户端验证过,没有在 ChatGPT 里测试。能否面向用户开放,还取决于目标套餐和工作区允许什么。
能拿来做什么
- 从现有 OpenAPI 里挑一个只读操作,用
registerTool写清必填参数和返回单位,先用listTools核对客户端看到的 schema。 - 在 Node 测试里分别覆盖正常输入、越界输入、上游非成功状态和网络异常。对天气这类实时接口,断言返回字段和类型,避免把某次读数写成固定期望值。
- 如果迁移的接口需要密钥,把它放在 MCP 服务端的环境变量里。交付前用目标客户端和实际工作区跑一遍连接与调用。