TodayAI

在 MCP Server 里声明并列出 Tools

按 MCP Tools 概念文档,声明工具 schema,并确认 Client 能列出它们。

基于 MCP Docs 整理 · MCP Tools · 官方资料

MCP Tool 是 Server 暴露给 Client 的可调用动作。没有清晰的 name、description 和 input schema,模型就无法稳定选择工具。

这篇聚焦:如何声明 tools/list 返回的工具,以及调用时校验参数。

声明一个最小 Tool

每个 tool 需要稳定 name、面向模型的 description,以及 JSON Schema 形式的 inputSchema。Client 通过 tools/list 发现它们,再通过 tools/call 调用。

Tool 描述结构

typescript
const tools = [
  {
    name: "get_weather",
    description: "按城市名查询当前天气摘要",
    inputSchema: {
      type: "object",
      properties: {
        city: { type: "string", description: "城市名,例如 Shanghai" },
      },
      required: ["city"],
    },
  },
];

最容易踩的坑

模型乱填参数

把 description 写具体,required 字段写全;枚举值用 enum 约束。

官方资料

MCP DocsTools