GeezerVison is showing github.com — fix(mcp): prune over-budget tool-schema subtrees instead of injecting… · anomalyco/opencode@6db8ab6
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/mcp/catalog.ts +33-12Lines changed: 33 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,11 +46,12 @@ export function defs(client: Client, timeout?: number) { | 46 | 46 | } | 47 | 47 |
| 48 | 48 | 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) | 50 | 51 | const inputSchema: JSONSchema7 = { | 51 | | - ...bounded, | | 52 | + ...schema, | 52 | 53 | type: "object", | 53 | | - properties: (bounded.properties ?? {}) as JSONSchema7["properties"], | | 54 | + properties: (schema.properties ?? {}) as JSONSchema7["properties"], | 54 | 55 | additionalProperties: false, | 55 | 56 | } | 56 | 57 |
| @@ -162,19 +163,39 @@ function describeTool(description: string | undefined, server: string | undefine | 162 | 163 | return server ? `[${server}] ${capped}` : capped | 163 | 164 | } | 164 | 165 |
| 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 { | 171 | 184 | 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 | 173 | 186 | seen.add(value) | 174 | 187 | 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 | + } | 176 | 194 | 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 | + }), | 178 | 199 | ) | 179 | 200 | } | 180 | 201 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/mcp/catalog.test.ts +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { describe, expect, test } from "bun:test" | | 2 | +import { asSchema } from "ai" | 2 | 3 | import { Client } from "@modelcontextprotocol/sdk/client/index.js" | 3 | 4 | import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js" | 4 | 5 | import { Server } from "@modelcontextprotocol/sdk/server/index.js" | @@ -154,4 +155,35 @@ describe("McpCatalog.convertTool bounds untrusted server input", () => { | 154 | 155 |
| 155 | 156 | expect(() => McpCatalog.convertTool(deep, clientReturning({ content: [], structuredContent: {} }))).not.toThrow() | 156 | 157 | }) | | 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 | + }) | 157 | 189 | }) |
|