kimi-code icon indicating copy to clipboard operation
kimi-code copied to clipboard

fix(agent-core/mcp): sanitize MCP JSON Schemas for Moonshot MFJS validator

Open creatiVision opened this issue 5 months ago • 2 comments

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:

  1. Circular / recursive $ref → stack overflow during processing
  2. Missing type on properties (enum/structure-only schemas) → type is not defined
  3. Tuple-style items arrays → items must be an object
  4. Mixed-type enums (e.g. [1, "auto"]) must not be collapsed to a single forced type

What changed

  • Add sanitizeMcpSchema and run it after MCP tool discovery in McpConnectionManager
  • Local $ref inlining with RFC 6901 pointer unescaping (~1/~0) and circular-safe placeholders
  • Missing-type filling from enum/const/structure
  • Tuple items → { anyOf: items }
  • Mixed enums → typed anyOf branches (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_key 400s (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

creatiVision avatar Jun 16 '26 21:06 creatiVision

🦋 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

changeset-bot[bot] avatar Jun 16 '26 21:06 changeset-bot[bot]

Addressed the three Codex review notes on schema-sanitize.ts (commit on this branch):

  1. Tuple items — no longer collapsed to items: { anyOf: [...] }. Draft-07 tuple arrays are rewritten to prefixItems + minItems/maxItems so positional validation is preserved (and Moonshot still gets a non-array items shape avoided).
  2. JSON Pointer array segments — #/$defs/X/anyOf/0 style paths now index into arrays per RFC 6901 instead of throwing and failing the whole MCP server.
  3. $ref siblings — sibling keywords are run through traverse before merge so nested local $refs are inlined before $defs/definitions are stripped.

Tests: schema-sanitize.test.ts (28) green locally.

creatiVision avatar Jul 27 '26 09:07 creatiVision