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

MCP tool schema validation fails with HTTP 400 ("moonshot flavored json schema") due to missing schema sanitization in new CLI

Open creatiVision opened this issue 5 months ago • 6 comments

What version of Kimi Code is running?

0.14.3

Which open platform/subscription were you using?

platform.kimi.ai

Which model were you using?

kimi-k2.7-code thinking

What platform is your computer?

Linux 6.8.0-124-generic x86_64 x86_64

What issue are you seeing?

Bug Description When connecting standard MCP servers (such as an n8n Docker container) to the new kimi-code-cli and utilizing a native Moonshot API model (e.g., moonshot-ai/kimi-k2.7-code), the CLI crashes when attempting to parse the MCP tool capabilities.

The native API returns an HTTP 400 error stating that the tools.function.parameters is not a valid "moonshot flavored json schema." The new TypeScript-based CLI appears to be passing the raw, standard JSON schema emitted by the MCP server directly to the backend without adapting it to Kimi's strict flavor requirements.

Regression Context This is a regression from the older Python-based kimi-cli. In the previous architecture, the kosong abstraction layer contained explicit interceptors (e.g., fix(kosong/kimi): fill in missing JSON Schema type for MCP tool parameters #1182) that would automatically repair standard JSON schemas. It successfully reformatted structures like anyOf items and missing type constraints to satisfy the strict Moonshot API validator before transmission.

Sanitized Debug Data / Session Export

WIPED SESSION DEBUG EXPORT - PII REDACTED Session ID: session_e1894fd0-de3c-44ce-b4cf-78957c1026ed Provider: kimi Model: moonshot-ai/kimi-k2.7-code Capabilities: ["thinking"]

[User Input] ✨ make n8n mcp (probably a docker container on xchg) available for hermes, also make sure n8n docker container or native runs on this system

[Provider API Error] Error: [provider.api_error] 400 Invalid request: tools.function.parameters is not a valid moonshot flavored json schema, details: <At path 'properties.pageSetup.properties.size.anyOf.items': items must be an object>

The MCP server provided a standard JSON schema containing an anyOf array under pageSetup.size. The Moonshot API rejected the payload because standard JSON schema structures were passed directly without the required "moonshot flavor" type coercion for the array items.

What steps can reproduce the bug?

Run a local or Docker-based MCP server that outputs standard JSON Schema parameters with nested anyOf objects (e.g., n8n).

Add the MCP server to the CLI configuration.

Switch the active model to moonshot-ai/kimi-k2.7-code with thinking enabled.

Prompt the agent to evaluate or list the available tools from the MCP server.

The API immediately rejects the tool schema payload.

What is the expected behavior?

The CLI should feature a translation or sanitization layer that intercepts the inputSchema from external MCP servers. It should automatically coerce standard JSON Schema structures into the strict "Moonshot Flavored JSON Schema" (e.g., ensuring items inside anyOf are strictly typed as objects) before serializing the API request.

Additional information

Deep-Dive: Moonshot Flavored JSON Schema (MFJS) & Walle The Moonshot API relies on a strictly enforced server-side Go schema validator called Walle (MoonshotAI/walle). Walle evaluates parameters against Moonshot Flavored JSON Schema (MFJS), which is a highly restrictive subset of the JSON Schema standard optimized for LLM readability.

MFJS introduces strict limitations that directly clash with standard MCP tool schemas:

Explicit Types: Every single property must have an explicitly defined type.

anyOf / oneOf Parent Constraints: While standard JSON Schema allows declaring a type in the parent of an anyOf array, MFJS mandates that type definitions reside strictly inside the children objects.

The anyOf.items Crash: In a typical document setup tool, parameters like size utilize anyOf to accept either a predefined string (like "LETTER") or an array of dimensions (like ``):

JSON "size": { "anyOf": [ { "type": "string" }, { "type": "array", "items": { "type": "number" } } ] } When the raw schema is sent, walle flattens and parses the array items inside the anyOf child. Because the schema has not been sanitized locally, Walle's type engine fails and throws the error: At path 'properties.pageSetup.properties.size.anyOf.items': items must be an object.

Why This is a Regression In the legacy Python-based kimi-cli, this was managed seamlessly by the kosong orchestration layer. kosong featured built-in schema-repair adapters (such as PR #1182: fix(kosong/kimi): fill in missing JSON Schema type for MCP tool parameters) which intercepted standard schemas and compiled them down to compliant MFJS before dispatching the payload.

The new TypeScript CLI lacks this pre-flight translation layer, sending raw schemas over the wire and triggering backend validation crashes.

Steps to Reproduce Configure an MCP server (such as n8n or mcp-pdf) that defines an input schema using anyOf types or nested arrays.

Set the active model to moonshot-ai/kimi-k2.7-code with thinking on.

Prompt the agent with a task that triggers tool discovery or use.

The system immediately crashes with an HTTP 400 error.

Expected Behavior The CLI should feature a local JSON Schema adapter layer (replicating the legacy Python kosong behavior) to automatically normalize and sanitize standard JSON Schemas to conform to MFJS requirements prior to network transmission.

creatiVision avatar Jun 15 '26 14:06 creatiVision

Still reproducible on 0.23.5, with a second variant of this bug class — the current TS kosong normalizer doesn't cover it.

Error

[provider.api_error] 400 tools.function.parameters is not a valid moonshot flavored json schema,
details: <At path 'root': when using anyOf, type should be defined in anyOf items instead of the parent schema>

Root cause

normalizeKimiToolSchema in packages/kosong (dist: kimi-*.mjs) fills in missing type fields, but:

  1. The root schema object is explicitly skipped — ensureKimiPropertyTypes only calls recurseSchema(normalized), which visits children but never normalizes the root node itself.
  2. Neither root nor nested nodes handle the MFJS rule above: a node declaring both type and a combinator (anyOf / oneOf / allOf) is passed through unchanged, so walle rejects it. Any tool whose parameter root is { "type": "object", ..., "anyOf": [...] } (or nests that shape under properties / not / if/then/else) triggers the 400.

Tested fix

Distribute the parent type into combinator items that lack one, then drop it from the parent — applied to the root and to every visited node:

function fixCombinatorParentType(node) {
	if (!isRecord(node) || !hasOwn(node, "type")) return;
	for (const key of ["anyOf", "oneOf", "allOf"]) {
		const items = node[key];
		if (!Array.isArray(items) || items.length === 0) continue;
		for (const item of items)
			if (isRecord(item) && !hasOwn(item, "type")) item["type"] = node["type"];
		delete node["type"];
	}
}

function ensureKimiPropertyTypes(schema) {
	const normalized = cloneJsonValue(schema);
	if (!isRecord(normalized)) throw new Error("JSON Schema root must normalize to an object.");
	fixCombinatorParentType(normalized); // was missing: root never normalized
	recurseSchema(normalized);
	return normalized;
}

function normalizeProperty(node) {
	if (!isRecord(node)) return;
	fixCombinatorParentType(node); // was missing: nested type+combinator nodes passed through
	// ... existing logic unchanged
}

Verification — ran the patched module against three failing shapes:

  • {type:"object", anyOf:[{required:["input"]},{required:["runInput"]}]} → {anyOf:[{required:["input"],type:"object"},{required:["runInput"],type:"object"}]} ✓
  • nested combinator under properties.<name> → fixed recursively ✓
  • items that already declare their own type → preserved; only the parent type is dropped ✓

The existing TYPE_COMPLETION_SKIP_KEYS already includes anyOf/oneOf/allOf, so the type-inference logic doesn't re-add a parent type afterwards.

josephkehan-prog avatar Jul 25 '26 08:07 josephkehan-prog

Thanks @josephkehan-prog for the detailed reproduction and tested fix. Let me validate the technical analysis.

Root cause confirmed: schema sanitization gap at the normalization boundary

The issue is exactly what you identified — normalizeKimiToolSchema in packages/kosong skips the root schema node and does not handle the type + combinator conflict that Moonshot's validator enforces.

Your fixCombinatorParentType approach is correct. Distributing the parent type into combinator items and removing it from the parent is the right transformation for the MFJS constraint. Two additional observations:

1. The root skip is a systematic pattern, not a one-off bug. The same pattern (normalizer visits children but skips root) appears in other MCP integrations. The fix should explicitly handle the root node as your code does — fixCombinatorParentType(normalized) before recurseSchema.

2. There is a deeper architectural issue. The MCP protocol does not define a standard schema interchange format — each model provider has its own "flavored" JSON Schema variant. This means every MCP client that bridges standard servers to non-standard model APIs needs its own normalization layer, and the normalization is fragile by design.

For the Kimi team, a more robust approach would be a schema conformance check at tool registration time — validate the normalized schema against a known MFJS grammar before sending it to the API, and fail with a clear diagnostic instead of a raw 400 from the backend. This makes the normalization gap visible at development time rather than at runtime.

Reproduction environment for Kimi team:

  • MCP server: any standard server with anyOf/oneOf in tool parameters (e.g., n8n MCP)
  • CLI: kimi-code 0.23.5
  • Model: moonshot-ai/kimi-k2.7-code with thinking
  • Expected: schema normalization transparently converts standard JSON Schema to MFJS
  • Actual: 400 because root schema with type: object + anyOf is passed through unchanged

Correctover avatar Jul 27 '26 02:07 Correctover

Just a quick update for anyone following this: this issue is still fully viable and currently blocking the use of many standard MCP servers (like mcp-pdf, n8n, or the Xcode MCP) on kimi-code when using Moonshot models.

The root cause remains the missing pre-flight schema adapter for Moonshot Flavored JSON Schema (MFJS), which was present in the legacy python CLI but got dropped in the TS rewrite.

This is fully addressed by the open pull request #830, which introduces sanitizeMcpSchema during MCP tool discovery. It automatically intercepts, unpacks $ref pointers, and repairs anyOf / items structures into valid MFJS before the network call is ever made.

Hopefully it gets merged soon!

creatiVision avatar Aug 24 '26 05:08 creatiVision

Another shape of this bug — root cause pinned + minimal repro (OpenAI-compatible gateway fronting Vertex AI / Gemini)

kimi-code 0.7.5 (VS Code extension), 11 MCP servers ≈ 280 tools. Every subagent spawn fails with the gateway wrapping a 400 into a 503:

Invalid value at 'request.tools[0].function_declarations[89].parameters.properties[2].value' (type.googleapis.com/google.cloud.aiplatform.master.Schema), "object"

(plus indices 98, 99, 110)

Root cause. Mapping the rejected indices against the session's tool snapshot, then running a feature matrix over all 280 tool schemas: the 4 rejected tools are exactly the only 4 tools that have a parameter literally named properties — all from the official Notion MCP server (API-create-a-data-source, API-patch-page, API-post-page, API-update-a-data-source).

Minimal repro (direct call, single tool):

  • {"type":"object","properties":{"foo":{"type":"string"}}} → accepted
  • {"type":"object","properties":{"properties":<anything>}} → same INVALID_ARGUMENT, value "object" — regardless of the value (anyOf, {"type":"object"}, {"type":"string"}, even {}). The converter appears to mistranslate a properties-map entry named properties into the bare string "object".

Full-payload verification. Replaying the real 280-tool request: raw → exactly the 4 production violations; after dropping only the properties-named entries (and their required mentions) → accepted.

Relation to the open PRs. Neither #808 nor #830 covers this shape — both dereference $ref and fill missing type but leave combinator-carrying properties untouched and never special-case a properties-map entry named properties. Suggest adding this case (drop/escape such entries client-side, or fix the properties key-name collision in the converter) so Vertex-fronted backends are covered too.

Local workaround that unblocked me: wrapped toolToOpenAI in bundled dist/extension.js with a sanitizer that deletes properties-named entries from properties maps; node --check passes and the gateway replay succeeds.

crycrywhy avatar Sep 09 '26 08:09 crycrywhy

Confirming this normalization breakdown with standard MCP server schemas.

The underlying disconnect is that the MCP specification allows full JSON Schema (draft 2020-12), whereas Moonshot / OpenAI-compatible function calling endpoints enforce strict structural constraints:

  1. type: "object" must be explicitly declared at the root of parameters.
  2. Meta-schema keywords like $schema, $defs, and definitions are rejected by the API parameter validator.
  3. Root-level oneOf / anyOf constructs that are not encapsulated within an object wrapper trigger the 400 tools.function.parameters is not a valid schema error.

A lightweight sanitizer function applied before submitting tool manifests to the API resolves the rejection:

export function sanitizeMcpToolParameters(schema: Record<string, any>): Record<string, any> {
  const clean = { ...schema };
  delete clean.$schema;
  delete clean.$defs;
  delete clean.definitions;

  if (!clean.type || clean.type !== "object") {
    clean.type = "object";
  }
  if (!clean.properties && !clean.additionalProperties) {
    clean.properties = {};
  }
  return clean;
}

In our Agent Assurance test suite, we evaluate MCP server tool definitions against diverse LLM gateway validation rules to prevent runtime dispatch crashes. If helpful, I can provide a comprehensive parameter normalizer handling recursive union properties.

genesiscode2026 avatar Sep 27 '26 16:09 genesiscode2026

Another shape of this bug on 0.28.1 — boolean items (tuple schemas), reproduced with the Vercel MCP server

Environment: kimi-code 0.28.1, macOS, model kimi-code/k3.1-pro, Vercel plugin 0.45.10 (MCP server https://mcp.vercel.com, HTTP + OAuth).

Error

provider.api_error · HTTP 400
tools.function.parameters is not a valid moonshot flavored json schema,
details: <At path 'properties.fallback.properties.probabilityBetween.items': items must be an object>
request id: b2583b31068deaa78b3d51829ca94e52

Offending tool

generate_ai_gateway_decision from the Vercel MCP server. Its fallback.probabilityBetween is a JSON Schema 2020-12 tuple:

"probabilityBetween": {
  "type": "array",
  "prefixItems": [
    { "type": "number", "minimum": 0, "maximum": 1 },
    { "type": "number", "minimum": 0, "maximum": 1 }
  ],
  "items": false,
  "minItems": 2,
  "maxItems": 2
}

items: false is a valid boolean schema in 2020-12 (it forbids elements after prefixItems). Walle requires items to be an object, so the whole request is rejected.

Impact / repro

  • Enable the Vercel plugin, select k3.1-pro, send any message. The request fails before generation.
  • Retrying fails identically, because the full tool list is sent on every turn.
  • Disable the Vercel plugin: the same session and model work again.
  • One MCP tool is enough to break the session. #5704 is the size variant; this is a subschema-shape variant.

Suggestion

The sanitizer should also handle boolean items and tuples:

  • items: false → items: {} (or drop items).
  • prefixItems + items: false → a single items schema, e.g. the union/type of the prefix entries ({"type": "number"} here) and drop prefixItems, since MFJS does not support tuple validation.

This is separate from the $ref / anyOf / parent-type work in #830. A regression test with the Vercel tool above would cover it.

Temporary workaround for users: disable the Vercel plugin until this is fixed.

wangzhefeng avatar Nov 30 '26 07:11 wangzhefeng

Another concrete reproduction of this (Windows 11, kimi-code 2.8.1, model kimi-for-coding):

Setup: Added the official remote Vercel MCP server via ~/.kimi-code/mcp.json:

{
  "mcpServers": {
    "vercel": { "transport": "http", "url": "https://mcp.vercel.com" }
  }
}

Symptom: Every single prompt fails — interactive and kimi -p — with:

Error: [provider.api_error] tools.function.parameters is not a valid moonshot flavored json schema, details: <At path 'properties.fallback.properties.probabilityBetween.items': items must be an object>

Root cause: One of the Vercel MCP tools ships an input schema that Moonshot's flavored JSON Schema subset rejects (probabilityBetween.items is not an object). Because the CLI forwards all MCP tool schemas verbatim in every request, one bad third-party schema breaks the entire session — the model never even gets the prompt.

Workaround: Remove the offending MCP server from mcp.json ("mcpServers": {}), after which prompts work normally.

Suggested fix: Sanitize/validate MCP tool schemas client-side at server-connect time (or at minimum strip/wrap offending subschemas and warn), so a single incompatible tool degrades that tool instead of failing every request. Related: #1610, #2661.

mazemax avatar Nov 30 '26 16:11 mazemax