Skip to content

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

Open
creatiVision wants to merge 3 commits into
MoonshotAI:mainfrom
creatiVision:fix/mcp-schema-limits
Open

fix(agent-core/mcp): sanitize MCP JSON Schemas for Moonshot MFJS validator#830
creatiVision wants to merge 3 commits into
MoonshotAI:mainfrom
creatiVision:fix/mcp-schema-limits

Conversation

@creatiVision

@creatiVision creatiVision commented Jun 16, 2026

Copy link
Copy Markdown

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 arraysitems 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: disabledenabled, max_tokens / max_output_tokensmaxOutputSize
  • Unit tests for sanitizer + config aliases
  • Changeset included

Out of scope

Checklist

  • CONTRIBUTING read
  • Problem explained / related PRs linked
  • Tests added (schema-sanitize, config-loader, configs)
  • Changeset included
  • No unrelated plugin / engine / lockfile noise

@changeset-bot

changeset-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 90653c4

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

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6f20830c5c

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread packages/agent-core/src/mcp/schema-sanitize.ts Outdated
creatiVision added a commit to creatiVision/kimi-code that referenced this pull request Jul 5, 2026
Cherry-pick PR MoonshotAI#830's changes onto current upstream main, resolving the
merge conflict in transformModelData (toml.ts) introduced by main's
overrides-transform refactor:

- schema.ts: add incompatible 'disabled' -> 'enabled' preprocess mapping
- toml.ts: keep the overrides transform and add the max_tokens /
  max_output_tokens -> maxOutputSize alias fallback
- Add sanitizeMcpSchema (MFJS emitter) + cycle detector + root-pointer
  comment addressing Codex review feedback on PR MoonshotAI#830
- Integrate the sanitizer into connectAndDiscoverTools
- Update schema-sanitize export in index.ts
- Add tests + changeset
@creatiVision
creatiVision force-pushed the fix/mcp-schema-limits branch from 6fc34e7 to c359747 Compare July 5, 2026 20:07
@creatiVision
creatiVision force-pushed the fix/mcp-schema-limits branch from ca113cd to 03873ad Compare July 13, 2026 10:14
…dator

Resolve local $ref (with RFC 6901 pointer unescaping and circular-safe
inlining), fill missing property types, normalize tuple items arrays,
and split mixed-type enums into typed anyOf branches. Also map common
config aliases (disabled→enabled, max_tokens→maxOutputSize).
@creatiVision
creatiVision force-pushed the fix/mcp-schema-limits branch from dd82ba7 to a0fc9fe Compare July 27, 2026 08:36
@creatiVision creatiVision changed the title fix(agent-core): prevent stack overflow on circular schema refs & add compatibility mappings fix(agent-core/mcp): sanitize MCP JSON Schemas for Moonshot MFJS validator Jul 27, 2026
- Keep draft-07 tuple `items` as positional `prefixItems` (+ min/max)
  instead of a homogeneous anyOf union
- Resolve RFC 6901 numeric array segments in local JSON Pointers
- Traverse $ref sibling keywords so nested local refs are inlined
  before $defs are stripped
@creatiVision

Copy link
Copy Markdown
Author

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.

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