test(mcp): distinguish schema recursion from instance data (#13249)

This commit is contained in:
morluto authored and GitHub committed 2026-11-22 01:01:01 +00:00
1 parent 00974e5a61
commit 61eabd25dc
2 files changed
+44 -44

No files matched your search

+20 -24
View File
@@ -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: #",
]);
});
+24 -20
View File
@@ -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<string, unknown> =>
/** 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<string>();
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(