fix(agent-core/mcp): sanitize MCP JSON Schemas for Moonshot MFJS validator
Related Issue
Continues / supersedes the earlier drafts in #1605 and #2216.
Problem
MCP servers often advertise standard JSON Schema tool parameters. Moonshot’s stricter MFJS validator (and Kimi’s discovery path) reject or crash on several common shapes:
- Circular / recursive
$ref→ stack overflow during processing - Missing
typeon properties (enum/structure-only schemas) →type is not defined - Tuple-style
itemsarrays →items must be an object - Mixed-type enums (e.g.
[1, "auto"]) must not be collapsed to a single forcedtype
What changed
- Add
sanitizeMcpSchemaand run it after MCP tool discovery inMcpConnectionManager - Local
$refinlining with RFC 6901 pointer unescaping (~1/~0) and circular-safe placeholders - Missing-
typefilling from enum/const/structure - Tuple
items→{ anyOf: items } - Mixed enums → typed
anyOfbranches (preserves all members) - Config compatibility:
disabled→enabled,max_tokens/max_output_tokens→maxOutputSize - Unit tests for sanitizer + config aliases
- Changeset included
Out of scope
- Does not change Node
engine-strict - Does not bundle unrelated plugins or lockfile churn
- Does not fix NVIDIA / custom OpenAI
prompt_cache_key400s (see #2203)
Checklist
- [x] CONTRIBUTING read
- [x] Problem explained / related PRs linked
- [x] Tests added (
schema-sanitize, config-loader, configs) - [x] Changeset included
- [x] No unrelated plugin / engine / lockfile noise
🦋 Changeset detected
Latest commit: 90653c456bb633c7150f7fa486b09b64d4893d6e
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 2 packages
| Name | Type |
|---|---|
| @moonshot-ai/agent-core | Patch |
| @moonshot-ai/kimi-code | Patch |
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR
Addressed the three Codex review notes on schema-sanitize.ts (commit on this branch):
- Tuple
items— no longer collapsed toitems: { anyOf: [...] }. Draft-07 tuple arrays are rewritten toprefixItems+minItems/maxItemsso positional validation is preserved (and Moonshot still gets a non-arrayitemsshape avoided). - JSON Pointer array segments —
#/$defs/X/anyOf/0style paths now index into arrays per RFC 6901 instead of throwing and failing the whole MCP server. $refsiblings — sibling keywords are run throughtraversebefore merge so nested local$refs are inlined before$defs/definitionsare stripped.
Tests: schema-sanitize.test.ts (28) green locally.