Skip to content

fix(agent-core/mcp): sanitize recursive MCP JSON Schemas for Moonshot validator compatibility#2216

Closed
creatiVision wants to merge 13 commits into
MoonshotAI:mainfrom
creatiVision:main
Closed

fix(agent-core/mcp): sanitize recursive MCP JSON Schemas for Moonshot validator compatibility#2216
creatiVision wants to merge 13 commits into
MoonshotAI:mainfrom
creatiVision:main

Conversation

@creatiVision

@creatiVision creatiVision commented Jul 26, 2026

Copy link
Copy Markdown

Related Issue

Supersedes / continues the work from #830 and #1605.

Problem

Some MCP servers advertise tool input schemas that are valid standard JSON Schema but fail Moonshot's stricter “MFJS” validator, or crash Kimi during discovery:

  1. Circular / recursive $ref (e.g. self-referential $defs) caused Maximum call stack size exceeded during schema processing.
  2. Tuple-style items arrays (items: [{...}, {...}]) are rejected with items must be an object.
  3. Property schemas that omit type (relying on enum / structural keywords) are rejected with type is not defined.

Without sanitization, affected MCP servers fail to connect or poison inference requests with HTTP 400.

What changed

  • Add sanitizeMcpSchema (TS port of the legacy kosong JSON Schema interceptor) to:
    • safely dereference local $refs (including circular roots)
    • fill missing type on property schemas where safe
    • normalize array-tuple items into { anyOf: items }
  • Wire sanitization into McpConnectionManager after tool discovery
  • Small config compatibility mappings: disabledenabled, max_tokens / max_output_tokensmax_output_size
  • Tests for sanitizer + config compatibility

Notes for reviewers

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked related issues / explained the problem above.
  • I have added tests that prove my feature works.
  • Changeset included for agent-core / kimi-code.
  • Docs update (not required for this fix).

@changeset-bot

changeset-bot Bot commented Jul 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 424d2b5

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: 424d2b587f

ℹ️ 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 pnpm-lock.yaml
ssh2@1.17.0>cpu-features: '-'
ssh2@1.17.0>nan: '-'

importers:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Restore the workspace overrides in the lockfile

pnpm-workspace.yaml still declares the two ssh2 overrides, but this lockfile no longer records them. The CI workflow at .github/workflows/ci.yml:26 runs pnpm install --frozen-lockfile; running that command against this tree stops before the build with ERR_PNPM_LOCKFILE_CONFIG_MISMATCH: The current "overrides" configuration doesn't match the value found in the lockfile. Regenerate the lockfile while preserving the workspace overrides.

Useful? React with 👍 / 👎.

{
"name": "frontend-design",
"version": "1.1.0",
"description": "Frontend design skill for UI/UX implementation",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Declare the nested skills directory

Installing this plugin produces a manifest with no skills: parseManifest only uses the explicit skills field or falls back to a SKILL.md at the plugin root, while this plugin's only skill is under skills/frontend-design/SKILL.md. Consequently the newly added plugin installs without exposing its advertised frontend-design skill; add a "skills": "./skills/" entry.

Useful? React with 👍 / 👎.

}
if (inferred.size === 1) return [...inferred][0]!;
if (inferred.size === 2 && inferred.has('integer') && inferred.has('number')) return 'number';
return 'string';

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve mixed enum value types

For a valid MCP schema such as enum: [1, "auto"], this fallback adds type: "string", making the numeric enum member invalid because JSON Schema applies type and enum together. Since McpConnectionManager applies this sanitizer to every discovered MCP tool before provider selection, providers that previously accepted mixed enums now receive a corrupted schema and may reject the request or prevent the model from selecting the numeric option.

Useful? React with 👍 / 👎.

if (pathStr === '') {
return root;
}
const parts = pathStr.split('/');

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Decode JSON Pointer path segments

Local JSON Pointer segments must decode ~1 to / and ~0 to ~, but this split leaves them escaped before looking up the definition. A valid schema with $ref: "#/$defs/a~1b" and a definition named a/b therefore throws during tool discovery and can mark the MCP server failed; unescape each segment before indexing the schema.

Useful? React with 👍 / 👎.

Comment thread .npmrc
@@ -1,3 +1,3 @@
auto-install-peers=true
engine-strict=true
engine-strict=false

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Keep the repository's Node engine check enabled

When a developer runs pnpm install with Node older than 24.15.0, this setting now permits installation even though package.json and .nvmrc still require 24.15.0, deferring failures to later scripts that may rely on unsupported runtime behavior. The repository explicitly requires installs to fail on an unsatisfied Node engine, so this must remain enabled unless all version declarations and runtime requirements are intentionally lowered together.

AGENTS.md reference: AGENTS.md:L30-L33

Useful? React with 👍 / 👎.

@creatiVision creatiVision changed the title upate to new version kimi-code fix(agent-core/mcp): sanitize recursive MCP JSON Schemas for Moonshot validator compatibility Jul 27, 2026
@creatiVision

Copy link
Copy Markdown
Author

Superseded by the cleaned-up #830. This branch had merge conflicts and unrelated plugin/lockfile/engine-strict changes. Canonical PR: #830

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.

2 participants