Skip to content

fix(agent-core): prevent stack overflow on circular schema refs & add compatibility mappings#830

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

fix(agent-core): prevent stack overflow on circular schema refs & add compatibility mappings#830
creatiVision wants to merge 6 commits into
MoonshotAI:mainfrom
creatiVision:fix/mcp-schema-limits

Conversation

@creatiVision

Copy link
Copy Markdown

Description

This PR resolves a critical startup crash and implements much-needed compatibility mappings for standard MCP configurations (such as Claude Desktop and Antigravity).

1. Fix Circular Schema Reference Stack Overflow (sanitizeMcpSchema)

  • Problem: Some generative PDF and document MCP servers (e.g., @mcp-z/mcp-pdf) define layout shapes with recursive definitions (where elements contain children arrays referencing their parent schema, i.e., #definitions/__schema0). During tool discovery, Kimi Code's schema-sanitize engine recursively inlined local $ref pointers without tracking visited refs, leading to infinite recursion and an immediate startup crash (Maximum call stack size exceeded).
  • Solution: Added a cycle detector tracking the active recursion path (activeRefs: Set<string>). If a circular reference is detected, the recursion halts and gracefully returns a safe, compliant { type: 'object', description: 'Circular reference' } placeholder instead.
  • Testing: Added robust recursive/circular schema tests in schema-sanitize.test.ts.

2. Add Key Compatibility for Standard MCP Configurations (config.toml & mcp.json)

  • Problem 1 (disabled): Many tools export/save configurations with "disabled": true (standard Claude Desktop & Antigravity settings). Kimi only looked for "enabled": false to recognize disabled servers, causing it to connect to and spin up unwanted/disabled servers on startup.
  • Problem 2 (max_tokens): Standard TOML/YAML definitions use max_tokens or max_output_tokens to set response size limits. Kimi Code only checked for max_output_size. When absent, Kimi's completion-budget fallback defaulted to requesting the entire context size (e.g., 1,048,576 tokens for custom reasoning models), which triggers an immediate 400 Bad Request from providers (e.g., OpenRouter) and crashes with an unhelpful [compaction.unable] error on Turn 1.
  • Solution:
    • Added a preprocess converter mapping "disabled": true config objects to enabled: false inside McpServerConfigSchema (packages/agent-core/src/config/schema.ts).
    • Added compatibility mapping in transformModelData (packages/agent-core/src/config/toml.ts) to automatically fallback to max_tokens and max_output_tokens when max_output_size is not declared.
  • Testing: Added new unit tests verifying these conversions in config-loader.test.ts and configs.test.ts.

Verification Results

Automated Tests

Ran the entire agent-core configuration and schema test suite; all 39 tests passed flawlessly in < 1s:

pnpm --dir packages/agent-core test test/mcp/schema-sanitize.test.ts test/mcp/config-loader.test.ts test/config/configs.test.ts

Manual Verification

Built the native single executable binary using pnpm --dir apps/kimi-code run build:native:sea.
The TUI CLI now loads and runs cleanly with no Maximum call stack size exceeded errors or [compaction.unable] crashes, respecting disabled flags correctly.

@changeset-bot

changeset-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5b7612a

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
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