Skip to content

Commit 6db8ab6

Browse files
fix(mcp): prune over-budget tool-schema subtrees instead of injecting invalid placeholders
The wave 7-11 bound on MCP input schemas replaced any over-depth/over-count value with a placeholder. Array-valued keywords were hit too, so a deep `required: ["name"]` became `required: {}` — invalid JSON Schema. Strict providers reject the whole tool manifest when one tool is invalid: - Anthropic: tools.9.custom.input_schema: JSON schema is invalid. It must match JSON Schema draft 2020-12 (chart_generate_fishbone_diagram, index 9) - Meta/OpenRouter: [Meta] Invalid JSON schema ... not valid under any of the schemas listed in the 'anyOf' keyword (required:{} fragment) Affected the recursive chart tools (fishbone, mind map, organization chart) on the 2026-09-18 04:05 build. The bound now DROPS over-budget subtrees (BOUNDED sentinel) so the bounded document stays valid; the omitted subtree simply stops constraining. Wave A's {not:{}} replacement had the same wrong-type defect and is removed. Regression test walks a recursive children/required schema and asserts no non-array `required`/non-object `properties` keys survive.
1 parent 292de78 commit 6db8ab6

2 files changed

Lines changed: 65 additions & 12 deletions

File tree

‎packages/opencode/src/mcp/catalog.ts‎

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ export function defs(client: Client, timeout?: number) {
4646
}
4747

4848
export function convertTool(mcpTool: MCPToolDef, client: Client, timeout?: number, server?: string): Tool {
49-
const bounded = boundSchema(mcpTool.inputSchema) as JSONSchema7
49+
const bounded = boundSchema(mcpTool.inputSchema)
50+
const schema = bounded === BOUNDED ? {} : (bounded as JSONSchema7)
5051
const inputSchema: JSONSchema7 = {
51-
...bounded,
52+
...schema,
5253
type: "object",
53-
properties: (bounded.properties ?? {}) as JSONSchema7["properties"],
54+
properties: (schema.properties ?? {}) as JSONSchema7["properties"],
5455
additionalProperties: false,
5556
}
5657

@@ -162,19 +163,39 @@ function describeTool(description: string | undefined, server: string | undefine
162163
return server ? `[${server}] ${capped}` : capped
163164
}
164165

165-
// `{}` means "allow anything" in JSON Schema, so a bound that returns it *widens*
166-
// validation. Return an unsatisfiable schema instead: exceeding the depth/node cap
167-
// or hitting a cycle must reject that subtree, not accept it unchecked.
168-
const UNSATISFIABLE: JSONSchema7 = { not: {} }
169-
170-
function boundSchema(value: unknown, depth = 0, seen = new WeakSet<object>(), counter = { nodes: 0 }): unknown {
166+
// A hostile MCP server controls its own input schema, so bound its depth and node
167+
// count to keep it from dominating the prompt or forcing pathological serialization.
168+
// When a subtree exceeds the bound (or cycles) DROP it rather than substituting a
169+
// placeholder: `{}` widens validation, while any non-array placeholder substituted
170+
// into an array-valued keyword (`required: {}`) or a schema-object keyword
171+
// (`properties: { not: {} }`) emits invalid JSON Schema — strict providers then
172+
// reject the whole tool manifest (Anthropic: "must match JSON Schema draft
173+
// 2020-12"; Meta/OpenRouter: "not valid under any of the schemas"). Dropping keeps
174+
// the bounded document valid; the omitted subtree simply stops constraining.
175+
const BOUNDED = Symbol("opencode.mcp.boundedSchema")
176+
type Bounded = typeof BOUNDED
177+
178+
function boundSchema(
179+
value: unknown,
180+
depth = 0,
181+
seen = new WeakSet<object>(),
182+
counter = { nodes: 0 },
183+
): unknown | Bounded {
171184
if (value === null || typeof value !== "object") return value
172-
if (depth > MAX_SCHEMA_DEPTH || counter.nodes >= MAX_SCHEMA_NODES || seen.has(value)) return UNSATISFIABLE
185+
if (depth > MAX_SCHEMA_DEPTH || counter.nodes >= MAX_SCHEMA_NODES || seen.has(value)) return BOUNDED
173186
seen.add(value)
174187
counter.nodes++
175-
if (Array.isArray(value)) return value.map((item) => boundSchema(item, depth + 1, seen, counter))
188+
if (Array.isArray(value)) {
189+
return value.flatMap((item) => {
190+
const bounded = boundSchema(item, depth + 1, seen, counter)
191+
return bounded === BOUNDED ? [] : [bounded]
192+
})
193+
}
176194
return Object.fromEntries(
177-
Object.entries(value).map(([key, item]) => [key, boundSchema(item, depth + 1, seen, counter)]),
195+
Object.entries(value).flatMap(([key, item]) => {
196+
const bounded = boundSchema(item, depth + 1, seen, counter)
197+
return bounded === BOUNDED ? [] : [[key, bounded]]
198+
}),
178199
)
179200
}
180201

‎packages/opencode/test/mcp/catalog.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, expect, test } from "bun:test"
2+
import { asSchema } from "ai"
23
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
34
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"
45
import { Server } from "@modelcontextprotocol/sdk/server/index.js"
@@ -154,4 +155,35 @@ describe("McpCatalog.convertTool bounds untrusted server input", () => {
154155

155156
expect(() => McpCatalog.convertTool(deep, clientReturning({ content: [], structuredContent: {} }))).not.toThrow()
156157
})
158+
159+
test("drops over-budget subtrees instead of emitting invalid JSON Schema keywords", () => {
160+
const node = (depth: number): Record<string, unknown> =>
161+
depth === 0
162+
? { type: "object", properties: { name: { type: "string" } }, required: ["name"] }
163+
: {
164+
type: "object",
165+
properties: { name: { type: "string" }, children: { type: "array", items: node(depth - 1) } },
166+
required: ["name"],
167+
}
168+
169+
const deep = mcpTool()
170+
deep.inputSchema = { type: "object", properties: { data: node(6) }, required: ["data"] }
171+
172+
const tool = McpCatalog.convertTool(deep, clientReturning({ content: [], structuredContent: {} }))
173+
const schema = asSchema(tool.inputSchema!).jsonSchema as Record<string, unknown>
174+
175+
const invalid: string[] = []
176+
const walk = (value: unknown, path: string) => {
177+
if (value === null || typeof value !== "object") return
178+
if (Array.isArray(value)) return value.forEach((item, index) => walk(item, `${path}[${index}]`))
179+
const entries = value as Record<string, unknown>
180+
if ("required" in entries && !Array.isArray(entries.required)) invalid.push(`${path}.required`)
181+
if ("properties" in entries && (typeof entries.properties !== "object" || Array.isArray(entries.properties)))
182+
invalid.push(`${path}.properties`)
183+
Object.entries(entries).forEach(([key, item]) => walk(item, `${path}.${key}`))
184+
}
185+
walk(schema, "schema")
186+
187+
expect(invalid).toEqual([])
188+
})
157189
})

0 commit comments

Comments
 (0)