Skip to content

fix(mcp): avoid tool-schema constructs rejected by strict providers - #18

Open
albertyxw wants to merge 1 commit into
icebird1998:mainfrom
albertyxw:fix/strict-provider-tool-schemas
Open

albertyxw wants to merge 1 commit into
icebird1998:mainfrom
albertyxw:fix/strict-provider-tool-schemas

Conversation

@albertyxw

Copy link
Copy Markdown

问题

部分 OpenAI / Anthropic / Gemini 兼容端点会在请求注册阶段校验工具 schema。本仓库当前有两处构造会被拒绝,导致工具无法使用:

1. 根级 $defs + $ref 引用

server.mjs 的 drawio_create_diagram 用 $ref: "#/$defs/point" 引用同一 schema 根部的定义。会丢弃根级 $defs 的端点报:

Invalid schema for function 'drawio_create_diagram':
Pointer '/$defs/point' does not exist

2. items 中的联合类型数组

live-server.mjs 与 powerpoint-server.mjs 中,add_table 的 data 和 add_chart 的 categories 使用:

items: { type: ["string", "number"] }

严格端点(如 Gemini)对 items.type 做枚举字段谓词校验,联合类型数组会失败:

GenerateContentRequest.tools[0].function_declarations[N]
.parameters.properties[command].items: field predicate failed: $type == Type.ARRAY

影响为什么会被放大

MCP 客户端(如 Proma)会把所有已启用服务器的全部工具放进同一次请求。因此只要其中一个工具的 schema 非法,整批请求会被拒绝,用户侧表现为 agent 模式整体不可用,而非单个工具报错。

改动

文件 改动
scripts/server.mjs 将 3 处 $ref: "#/$defs/point" 就地内联,删除已无用的根级 $defs
scripts/live-server.mjs add_table.data、add_chart.categories:联合 type 数组 → anyOf
scripts/powerpoint-server.mjs 同上两处

语义不变,只调整发出的 JSON Schema 形状:

- items: { type: ["string", "number"] }
+ items: { anyOf: [{ type: "string" }, { type: "number" }] }

anyOf 是 JSON Schema 标准构造,Anthropic / OpenAI / Gemini 系端点均可接受;内联 $ref 等价于原有定义。

验证

在本机(Node v22.13.1)确认:

  • node --check 三个文件均通过
  • 读取各服务器 tools/list:$defs 引用与联合类型数组均为 0 处
  • 全量扫描 234 个工具:不再出现未解析 $ref 或联合类型 items

改动前后均未触及工具的执行逻辑,仅 schema 描述层。

参考

同类问题在其它项目中的处理方式(仅供参考,未逐行核对):


Made with Proma · GitHub

Some OpenAI/Anthropic/Gemini-compatible endpoints validate tool schemas
at request registration time. Two constructs used here are rejected:

1. Root-level `$defs` referenced via `$ref: "#/$defs/..."`. Endpoints
   that drop root `$defs` fail with:
   "Pointer '/$defs/point' does not exist"

2. Union type arrays inside `items`, e.g. `items: { type: ["string",
   "number"] }`. Strict endpoints fail with:
   "field predicate failed: $type == Type.ARRAY"

Because a client may send all tools from all enabled MCP servers in one
batch, a single invalid schema can cause the entire request to fail.

Changes:
- server.mjs: inline `$ref: "#/$defs/point"` (3 uses in
  drawio_create_diagram) and remove the now-unused root `$defs`.
- live-server.mjs, powerpoint-server.mjs: replace union `type` arrays
  with `anyOf` in add_table `data` and add_chart `categories`.

Behavior is unchanged; only the emitted JSON Schema shape is adjusted.

Made-with: Proma
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant