Repository navigation
fix(mcp): drop JSON Schema annotations from tool schemas - #3560
Conversation
Client Capability validation intentionally keeps a strict keyword allowlist. Normalize MCP tool descriptors during discovery by removing only the non-semantic $schema and $comment annotations from supported schema positions. Keep $id because it changes $ref base resolution and must remain subject to protocol validation. Generated-by: pi (gpt-5.6-sol)
Astro-Han
left a comment
There was a problem hiding this comment.
Reviewed at exact head 2613e4b2b9a990499386eb786d8c4e6564a32496.
Coverage: which annotation keywords are stripped, whether equivalent constraint information stays reachable on both the model side and the MCP client side, and which layer the stripping happens in. Not covered: wording.
No P0–P2 findings. One [P3] on traversal completeness.
The strip set is correctly scoped
normalizeToolInputSchema (packages/mcp/src/index.ts:2300-2333) deletes exactly two keywords: $schema and $comment. Both are pure annotations/processor directives under the JSON Schema spec with zero validation semantics. Nothing descriptive or validating is touched — title, description, default, examples, deprecated, type, required, enum, items all survive, and the new test explicitly pins that $id is preserved.
The stripping is confined to the model-facing descriptor
descriptorFromTool is the only call site (:2256, via scrubDescriptor), and its output becomes the model tool definition's parameters through packages/mcp/src/mcp-tools.ts:99. Execution goes provider.callTool → the MCP server validates against its own original schema. Local validation never depended on the stripped keywords, and those keywords carry no validation meaning anyway, so both sides stay equivalent.
[P3] Traversal misses several schema composition keywords
visit recurses through properties, $defs, definitions, additionalProperties, propertyNames, items, allOf, anyOf, and oneOf — but not prefixItems, contains, not, if/then/else, patternProperties, or dependentSchemas. $schema/$comment nested inside those survive the scrub.
Reachable on the ordinary user path: these shapes are perfectly legal and appear in hand-written server schemas or output from generators other than the TypeScript SDK's converter, even if they are not typical of it. The consequence is exactly the failure this PR exists to fix — if a downstream model provider rejects $schema anywhere in the schema tree, these corners keep tripping it, so the fix is incomplete for those shapes rather than wrong.
Anchor: packages/mcp/src/index.ts:2308-2330. Smallest fix is adding those keywords to the traversal list; alternatively recurse into every unknown key whose value is a plain object, terminating on a whitelist.
Rated P3 because it affects only edge schema shapes, with no data or security dimension — the outcome is "an uncovered corner keeps producing the original error".
Verification and limits
The new end-to-end test injects a schema carrying $schema/$comment/$id including a nested property through a stdio fixture and asserts the nested result is clean with $id intact — it pins real behaviour rather than text. manager.test.js is 60/60 green locally against this head.
check-runs on this head is total_count 0 — hosted CI has not run, so this is a comment rather than an approval regardless of the outcome.
No real external MCP server was exercised to measure how tolerant various providers are of residual keywords; that part rests on the issue background and the code structure.
Add patternProperties, dependentSchemas, dependencies, prefixItems, additionalItems, unevaluatedItems, unevaluatedProperties, contains, not, if, then, and else to subschema traversal. Keep the traversal explicit so instance-data keywords such as default, const, examples, and enum retain payload keys named $schema or $comment. $id remains untouched and survives normalization. Generated-by: pi (gpt-5.6-sol)
|
@Astro-Han Thanks for the review. Pushed abd8d3f on top of the reviewed head: the traversal now also covers |
|
@Astro-Han The run for the current head abd8d3f was cancelled by the concurrency group when the run for the older head 2613e4b started a few seconds after it (that one went green). Could you re-run the cancelled one for abd8d3f? I cannot trigger a re-run from the fork. |
jackwener
left a comment
There was a problem hiding this comment.
Independent review of exact head abd8d3f6824149d1edadfadae2aeb8f190c61265.
三问
- 要解决什么:MCP tool schema 里的
$schema/$comment会让部分模型提供方拒 schema。这是注解/方言标识,不是校验约束。 - 怎么解决:发现时
normalizeToolInputSchema递归删掉这两项,只改发给模型的 descriptor;MCP server 仍拿它自己的原 schema 做执行期校验。 - 奥卡姆:删除集合只有
$schema和$comment。title/description/default/examples/deprecated/ MCPtool.annotations/tool.description都没动。上一 head 漏掉的 composition 关键字,这个 head 已经补进遍历。
校验会不会变松
不会从 schema 里拿掉任何 instance 约束。$schema 只影响「用哪份 dialect 去解释」。执行路径是 callTool → MCP server 原 schema,那一侧不变。本地若用这份 stripped descriptor 做 Ajv,没有 $schema 时会落到默认 draft(本仓库是 2020),那是方言选择,不是约束被删。patternProperties / not / if/then/else 等都还在。
UI 文案
tool.description 和 MCP annotations(含 title)原样留下。schema 上的 description / title 也不删。仓库里没有把 $comment 当 UI 文案读的路径。测试钉了 $id 保留、嵌套 $schema/$comment 删干净,没有单独钉 description 存活——代码路径上它不会被删。
CI
期望:test(永远)。未改 package.json / sandbox 路径,不应有 audit / windows_sandbox_*。
这个 head 上原先 test 是 concurrency 取消(cancelled ≠ 绿)。我重跑了,提交时是 in_progress。按门禁不能 APPROVE。
代码侧 GO(仅 [P3])。门禁 NO-GO,等 test completed+success。未 approve,未合。
| } | ||
| for (const key of [ | ||
| 'items', | ||
| 'prefixItems', |
There was a problem hiding this comment.
[P3] 第二轮遍历覆盖了上一 head 漏掉的 prefixItems / contains / not / if/then/else / patternProperties / dependentSchemas 等,方向对。还剩一个合法的嵌套 schema 容器没走进去:contentSchema(跟 contentMediaType 一起出现)。嵌在那里的 $schema / $comment 会漏洗,正好是这个 PR 要修的那类提供方拒收。
普通 object 工具 schema 走不到。要补的话把 contentSchema 加进这个列表即可。
Astro-Han
left a comment
There was a problem hiding this comment.
Gate check at exact head abd8d3f6: hosted test is terminal completed/success (the only check this path set triggers), mergeable=true, and no unresolved [P0]–[P2] — the single open thread is a [P3] on the second-pass keyword coverage in packages/mcp/src/index.ts.
Approving. The [P3] is fine to fold into a follow-up; it does not block this change.
* fix(mcp): drop JSON Schema annotations from tool schemas Client Capability validation intentionally keeps a strict keyword allowlist. Normalize MCP tool descriptors during discovery by removing only the non-semantic $schema and $comment annotations from supported schema positions. Keep $id because it changes $ref base resolution and must remain subject to protocol validation. Generated-by: pi (gpt-5.6-sol) * fix(mcp): scrub annotations under every subschema keyword Add patternProperties, dependentSchemas, dependencies, prefixItems, additionalItems, unevaluatedItems, unevaluatedProperties, contains, not, if, then, and else to subschema traversal. Keep the traversal explicit so instance-data keywords such as default, const, examples, and enum retain payload keys named $schema or $comment. $id remains untouched and survives normalization. Generated-by: pi (gpt-5.6-sol)
Summary
MCP servers built with the TypeScript SDK advertise Zod-derived input schemas with a root
$schema. The capability provider copied those schemas into Client Capability frames, and the protocol's strict keyword allowlist rejected them, so publication stopped before the first tool.This normalizes tool descriptors at MCP discovery (
descriptorFromToolinpackages/mcp): clone the input schema and delete exactly$schemaand$commentfrom the root and from the child positions the validator walks (properties,items,additionalProperties,propertyNames,$defs/definitions,allOf/anyOf/oneOf).$idstays because it changes$refbase resolution; every other unsupported keyword still fails closed. The validator itself is unchanged.The descriptor is the one place every MCP consumer reads from. Desktop's Client Capability projection covers Desktop-native Zod tools, not MCP descriptors, and already strips the
$schemait generates.Evidence for how common this is: a direct
tools/listfrom XcodeBuildMCP 2.7.0 had$schemaon 44/44 tools and no other disallowed keyword; an earlier probe throughmcp_routersaw it on 31/31. The SDK'sserver/zod-json-schema-compat.jsemits it on both thezod-to-json-schemaandz.toJSONSchemapaths.Fixes #3559
Verification
Regression test
drops non-semantic JSON Schema annotations from discovered tools(stdio fixture with root$schema, nested$schema,$comment,$id): red before the change (descriptor still carried all three annotations), green after ($schemaand$commentgone,$idkept).biome lintandbiome formaton the three changed files reported no fixes.Live, with fresh Runtime Host roots and
bunx xcodebuildmcp mcpas the server:The live server advertised 24 tools in that fresh environment (XcodeBuildMCP exposes tools by workflow); the fixed provider published all 24 it received. The 44-tool figure comes from a separate direct capture of the same server.
Not run: the full monorepo
npm test, root-wide typecheck, the Desktop and runtime workspace suites, Windows or Linux, a model turn that invokes a published tool.AI use
Select exactly one:
Tool(s) and scope: pi (gpt-5.6-sol) traced the MCP-to-Client-Capability path, wrote the regression test and the normalization helper, ran the live publication and the suites, and drafted this description. Claude Code chose the design, reviewed the diff, re-ran the mcp and CLI suites, and edited this text. The commit carries the trailer.
Generated-by: pi (gpt-5.6-sol)
Checklist
Does this PR entail a change in behavior?