From 61eabd25dcea5399bf74a8764cafc3f297bbb49c Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sat, 21 Nov 2026 20:01:01 -0500 Subject: [PATCH] test(mcp): distinguish schema recursion from instance data (#13249) --- tests/boundary/mcp/toolSchemaValidity.test.ts | 44 +++++++++---------- tests/fixtures/mcpAdvertisedSchemaValidity.ts | 44 ++++++++++--------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/tests/boundary/mcp/toolSchemaValidity.test.ts b/tests/boundary/mcp/toolSchemaValidity.test.ts index 938f59af61..fd4b07b4f6 100644 --- a/tests/boundary/mcp/toolSchemaValidity.test.ts +++ b/tests/boundary/mcp/toolSchemaValidity.test.ts @@ -786,19 +786,7 @@ describe("MCP process input JSON Schema", () => { }); }); -it("rejects unresolved output-schema references even for uncalled tools", () => { - const errors = advertisedSchemaErrors([ - { - name: "dangling", - inputSchema: { type: "object" }, - outputSchema: { $ref: "#/$defs/missing" }, - }, - ]); - expect(errors).toHaveLength(1); - expect(errors[0]).toContain("dangling.outputSchema"); -}); - -it("validates each tool independently when output schema IDs repeat", () => { +it("compiles every output schema independently when IDs repeat, including uncalled tools", () => { const outputSchema = { $id: "urn:rea:test:shared-output", type: "string" }; const errors = advertisedSchemaErrors([ { name: "first", inputSchema: { type: "object" }, outputSchema }, @@ -918,7 +906,7 @@ it.each([false, true])( }, ); -it("distinguishes recursive references from shared definitions", () => { +it("finds recursion through schema-valued keywords and ignores literal metadata", () => { const diamond = { type: "object", properties: { @@ -926,18 +914,26 @@ it("distinguishes recursive references from shared definitions", () => { right: { $ref: "#/$defs/shared" }, }, $defs: { shared: { type: "string" } }, + default: { $ref: "#" }, + "x-fixture": { $ref: "#" }, }; - expect( - recursiveReferences([{ name: "diamond", inputSchema: diamond }]), - ).toEqual([]); - expect( - recursiveReferences([{ name: "root", inputSchema: { $ref: "#" } }]), - ).toEqual(["root.inputSchema: #"]); const escaped = { - $ref: "#/$defs/a~1b", - $defs: { "a/b": { $ref: "#/$defs/a~1b" } }, + $ref: "#/$defs/a%20~1b", + $defs: { "a /b": { $ref: "#/$defs/a%20~1b" } }, }; expect( - recursiveReferences([{ name: "escaped", inputSchema: escaped }]), - ).toEqual(["escaped.inputSchema: #/$defs/a~1b"]); + recursiveReferences([ + { name: "diamond", inputSchema: diamond }, + { name: "root", inputSchema: { $ref: "#" } }, + { name: "escaped", inputSchema: escaped }, + { + name: "property", + inputSchema: { properties: { default: { $ref: "#" } } }, + }, + ]), + ).toEqual([ + "root.inputSchema: #", + "escaped.inputSchema: #/$defs/a%20~1b", + "property.inputSchema: #", + ]); }); diff --git a/tests/fixtures/mcpAdvertisedSchemaValidity.ts b/tests/fixtures/mcpAdvertisedSchemaValidity.ts index 2c86bb14bd..6633e21e8a 100644 --- a/tests/fixtures/mcpAdvertisedSchemaValidity.ts +++ b/tests/fixtures/mcpAdvertisedSchemaValidity.ts @@ -1,9 +1,14 @@ import { Ajv2020 } from "ajv/dist/2020.js"; +import { + jsonPointerSyntaxProblem, + jsonPointerTokens, +} from "../../src/domain/jsonPointer.js"; import { schemaArrays as schemaArrayKeywords, schemaChildren as schemaChildKeywords, schemaMaps as schemaMapKeywords, + schemaSubschemas, } from "../../src/contracts/jsonSchemaKeywords.js"; import { arrayItemSchemaError } from "./mcpClientSchemaConstraints.js"; @@ -70,11 +75,16 @@ const isRecord = (value: unknown): value is Record => /** Resolve a local JSON Pointer reference inside one schema document. */ export function resolveReference(schema: unknown, reference: string): unknown { - if (reference === "#") return schema; - if (!reference.startsWith("#/")) return undefined; + if (!reference.startsWith("#")) return undefined; + let pointer: string; + try { + pointer = decodeURIComponent(reference.slice(1)); + } catch { + return undefined; + } + if (jsonPointerSyntaxProblem(pointer) !== null) return undefined; let value = schema; - for (const token of reference.slice(2).split("/")) { - const key = token.replaceAll("~1", "/").replaceAll("~0", "~"); + for (const key of jsonPointerTokens(pointer)) { if ( typeof value !== "object" || value === null || @@ -95,23 +105,17 @@ export function recursiveReferences( const schema = tool[kind]; const found = new Set(); const visit = (node: unknown, active: readonly string[]): void => { - if (typeof node !== "object" || node === null) return; - for (const [key, child] of Object.entries(node)) { - if ( - [ - "$defs", - "definitions", - "examples", - "default", - "const", - "enum", - ].includes(key) - ) - continue; - if (key !== "$ref" || typeof child !== "string") visit(child, active); - else if (active.includes(child)) found.add(child); - else visit(resolveReference(schema, child), [...active, child]); + if (!isRecord(node)) return; + const reference = node.$ref; + if (typeof reference === "string") { + if (active.includes(reference)) found.add(reference); + else + visit(resolveReference(schema, reference), [...active, reference]); } + for (const child of schemaSubschemas(node, { + includeDefinitions: false, + })) + visit(child, active); }; visit(schema, []); return [...found].map(