perf(mcp): reuse input schemas without losing caller guidance (#1162)

This commit is contained in:
Ysopwnable
2026-10-09 11:38:07 +08:00
committed by GitHub
parent 4fb2f3e6ed
commit e1afbc1914
6 changed files with 599 additions and 158 deletions
+163
View File
@@ -0,0 +1,163 @@
const schemaMaps = new Set([
"$defs",
"definitions",
"properties",
"patternProperties",
"dependentSchemas",
"dependencies",
]);
const schemaArrays = new Set(["allOf", "anyOf", "oneOf", "prefixItems"]);
const schemaChildren = new Set([
"items",
"additionalItems",
"contains",
"additionalProperties",
"unevaluatedProperties",
"unevaluatedItems",
"propertyNames",
"not",
"if",
"then",
"else",
]);
const isObject = (value: unknown): value is Readonly<Record<string, unknown>> =>
typeof value === "object" && value !== null && !Array.isArray(value);
const resolveReference = (
root: Readonly<Record<string, unknown>>,
reference: string,
): unknown => {
if (!reference.startsWith("#/")) return undefined;
let value: unknown = root;
for (const token of reference.slice(2).split("/")) {
const key = token.replaceAll("~1", "/").replaceAll("~0", "~");
if (!isObject(value) || !Object.hasOwn(value, key)) return undefined;
value = value[key];
}
return value;
};
const referencedDescription = (
root: Readonly<Record<string, unknown>>,
schema: Readonly<Record<string, unknown>>,
): string | undefined => {
let value: unknown = schema;
const active = new Set<string>();
while (isObject(value)) {
if (typeof value.description === "string") return value.description;
if (typeof value.$ref !== "string" || active.has(value.$ref))
return undefined;
active.add(value.$ref);
value = resolveReference(root, value.$ref);
}
return undefined;
};
const describeSchema = (
schema: unknown,
root: Readonly<Record<string, unknown>>,
describeProperty: (property: string) => string,
): unknown => {
if (!isObject(schema)) return schema;
return Object.fromEntries(
Object.entries(schema).map(([key, value]) => {
if (schemaMaps.has(key) && isObject(value))
return [
key,
Object.fromEntries(
Object.entries(value).map(([name, child]) => {
const described = describeSchema(child, root, describeProperty);
return [
name,
key === "properties" && isObject(described)
? {
...described,
description:
referencedDescription(root, described) ??
describeProperty(name),
}
: described,
];
}),
),
];
if (schemaArrays.has(key) && Array.isArray(value))
return [
key,
value.map((child) => describeSchema(child, root, describeProperty)),
];
if (schemaChildren.has(key))
return [
key,
Array.isArray(value)
? value.map((child) =>
describeSchema(child, root, describeProperty),
)
: describeSchema(value, root, describeProperty),
];
// Annotation values and literal const/enum data are not schemas.
return [key, value];
}),
);
};
const flattenRootUnion = (
value: Record<string, unknown>,
describeProperty: (property: string) => string,
): Record<string, unknown> => {
if (!Array.isArray(value.anyOf)) return value;
const branches = value.anyOf;
const objects = branches.filter(isObject);
if (objects.length === 0 || objects.length !== branches.length) return value;
const properties = new Map<string, unknown>();
for (const branch of objects) {
if (!isObject(branch.properties)) return value;
for (const [name, schema] of Object.entries(branch.properties)) {
const previous = properties.get(name);
if (!properties.has(name)) properties.set(name, schema);
else if (JSON.stringify(previous) !== JSON.stringify(schema))
properties.set(name, {
anyOf: [previous, schema],
description: describeProperty(name),
});
}
}
// Invocation still uses the exact canonical union, including branch rules.
const { anyOf: _anyOf, required: _required, ...root } = value;
const requiredByBranch = objects.map((branch) =>
Array.isArray(branch.required)
? branch.required.filter(
(item): item is string => typeof item === "string",
)
: [],
);
const required = requiredByBranch.reduce((common, current) =>
common.filter((name) => current.includes(name)),
);
const minProperties = Math.max(
typeof root.minProperties === "number" ? root.minProperties : 0,
Math.min(...requiredByBranch.map((names) => names.length)),
);
return {
...root,
type: "object",
properties: Object.fromEntries(properties),
...(minProperties > 0 ? { minProperties } : {}),
...(required.length > 0 ? { required } : {}),
...(objects.every((branch) => branch.additionalProperties === false)
? { additionalProperties: false }
: {}),
};
};
/** Preserve property guidance and literal data while advertising object roots. */
export const presentInputJsonSchema = (
root: Record<string, unknown>,
describeProperty: (property: string) => string,
): Record<string, unknown> => {
const described = describeSchema(root, root, describeProperty);
return isObject(described)
? flattenRootUnion(described, describeProperty)
: root;
};
+247
View File
@@ -0,0 +1,247 @@
import { Ajv } from "ajv";
import { Ajv2020 } from "ajv/dist/2020.js";
import { describe, expect, it } from "vitest";
import { z } from "zod";
import { inlineLocalJsonSchemaReferences } from "../../tests/fixtures/localJsonSchemaReferences.js";
import { presentInputJsonSchema } from "./inputSchemaPresentation.js";
import { TOOL_CONTRACTS, toolContract } from "./toolContracts.js";
import { toolInputSchemaWithMetadata } from "./toolSchemaMetadata.js";
describe("advertised input references", () => {
it.each(["draft-2020-12", "draft-07"] as const)(
"preserves production native-selector constraints in %s",
(target) => {
const contract = toolContract("observe_native_calls");
const advertised = toolInputSchemaWithMetadata(contract)[
"~standard"
].jsonSchema.input({ target });
const ajv =
target === "draft-2020-12"
? new Ajv2020({ strict: false })
: new Ajv({ strict: false });
const validate = ajv.compile(advertised);
for (const selector of [
"setFoo:",
"dataTaskWithRequest:completionHandler:",
"",
"bad selector:",
"bad[selector]:",
"set\0Foo:",
"setFoo:\n",
])
for (const extra of [{}, { unexpected: true }]) {
const sample = {
breakpoints: [
{
kind: "objc-method",
class_name: "NSURLSession",
selector,
...extra,
},
],
};
expect(validate(sample)).toBe(
contract.inputSchema.safeParse(sample).success,
);
}
},
);
it.each(["draft-2020-12", "draft-07"] as const)(
"preserves complete input meaning and examples through %s references",
(target) => {
for (const contract of TOOL_CONTRACTS) {
const projection =
toolInputSchemaWithMetadata(contract)["~standard"].jsonSchema.input;
const referenced = projection({ target });
const inline = projection({
target,
libraryOptions: { reused: "inline" },
});
expect(referenced.type, contract.name).toBe("object");
expect(
inlineLocalJsonSchemaReferences(referenced),
contract.name,
).toEqual(inlineLocalJsonSchemaReferences(inline));
expect(referenced.examples, contract.name).toEqual(
contract.examples.map(({ input }) => input),
);
expect(projection({ target })).toBe(referenced);
}
},
);
it.each(["draft-2020-12", "draft-07"] as const)(
"retains specialized descriptions, literal metadata and strict validation in %s",
(target) => {
const literal = {
properties: { target: { $ref: "literal caller data" } },
examples: [{ properties: { path: {} } }],
};
const range = z
.object({
offset: z.number().int().nonnegative().max(65535),
length: z.number().int().min(1),
})
.strict()
.describe("Exact byte range in the selected artifact.");
const canonical = z
.object({
before: range,
after: range,
metadata: z
.record(z.string(), z.unknown())
.meta({ default: literal, examples: [literal] }),
constructor: z.string(),
})
.strict();
const advertised = toolInputSchemaWithMetadata({
...toolContract("open_binary"),
inputSchema: canonical,
})["~standard"].jsonSchema.input({ target });
expect(inlineLocalJsonSchemaReferences(advertised)).toMatchObject({
properties: {
before: { description: "Exact byte range in the selected artifact." },
after: { description: "Exact byte range in the selected artifact." },
metadata: { default: literal, examples: [literal] },
constructor: { description: "Value for constructor." },
},
});
const ajv =
target === "draft-2020-12"
? new Ajv2020({ strict: false })
: new Ajv({ strict: false });
const validate = ajv.compile(advertised);
const valid = {
before: { offset: 0, length: 1 },
after: { offset: 65535, length: 2 },
metadata: literal,
constructor: "constructor data",
};
const samples = [
valid,
...[
{ offset: -1 },
{ offset: 65536 },
{ offset: 0.5 },
{ length: 0 },
{ unexpected: true },
].map((change) => ({
...valid,
before: { ...valid.before, ...change },
})),
{ ...valid, after: null },
{ ...valid, unexpected: true },
];
expect(canonical.safeParse(valid).success).toBe(true);
for (const sample of samples)
expect(validate(sample)).toBe(canonical.safeParse(sample).success);
},
);
});
describe("root input presentation", () => {
it("normalizes repeated string intersections without widening validation", () => {
const root = {
type: "string",
minLength: 1,
description: "Exact selector.",
allOf: [
{ type: "string", pattern: "^[^\\s]+$" },
{ allOf: [{ type: "string", pattern: "^[^\\s]+$" }], minLength: 2 },
],
};
const normalized = inlineLocalJsonSchemaReferences(root);
expect(normalized).toEqual({
type: "string",
minLength: 2,
pattern: "^[^\\s]+$",
description: "Exact selector.",
});
const ajv = new Ajv2020({ strict: false });
const original = ajv.compile(root);
const projected = ajv.compile(
z.record(z.string(), z.unknown()).parse(normalized),
);
for (const sample of ["", "a", "aa", "a a", "a\n", null, {}])
expect(projected(sample)).toBe(original(sample));
});
it("preserves prototype-named properties in object-root presentation", () => {
const fields = Object.fromEntries([
["__proto__", { type: "string" }],
["constructor", { type: "number" }],
]);
const branch = {
type: "object",
properties: fields,
required: ["__proto__"],
additionalProperties: false,
};
const presented = presentInputJsonSchema(
{ anyOf: [branch, branch] },
(property) => `Parameter ${property}`,
);
expect(presented).toMatchObject({
type: "object",
required: ["__proto__"],
minProperties: 1,
additionalProperties: false,
properties: fields,
});
expect(presented.anyOf).toBeUndefined();
expect(Object.getPrototypeOf(presented.properties)).toBe(Object.prototype);
expect(Object.hasOwn(presented.properties ?? {}, "__proto__")).toBe(true);
});
it("retains reference siblings and does not manufacture root branch constraints", () => {
const branch = { $ref: "#/$defs/object", additionalProperties: false };
const root = {
$defs: {
object: { type: "object", properties: { value: { type: "string" } } },
},
anyOf: [branch],
};
const presented = presentInputJsonSchema(root, (property) => property);
expect(presented.anyOf).toEqual([branch]);
const ajv = new Ajv2020({ strict: false });
for (const sample of [{}, { value: "value" }, null])
expect(ajv.compile(presented)(sample)).toBe(ajv.compile(root)(sample));
});
it("preserves specialized guidance across reference chains without following literal references", () => {
const literal = { properties: { literal: {} }, $ref: "#/$defs/missing" };
const presented = presentInputJsonSchema(
{
$defs: {
"a~b": {
type: "string",
description: "Literal URL selected by the caller.",
},
alias: { $ref: "#/$defs/a~0b" },
},
type: "object",
properties: {
url: { $ref: "#/$defs/alias" },
other: { $ref: "#/$defs/alias", description: "Explicit override." },
},
default: literal,
const: literal,
examples: [literal],
enum: [literal],
},
(property) => `Parameter ${property}`,
);
expect(presented).toMatchObject({
properties: {
url: { description: "Literal URL selected by the caller." },
other: { description: "Explicit override." },
},
default: literal,
const: literal,
examples: [literal],
enum: [literal],
});
});
});
+22 -89
View File
@@ -1,6 +1,7 @@
import { z } from "zod";
import type { ToolContract } from "./toolContractTypes.js";
import { presentInputJsonSchema } from "./inputSchemaPresentation.js";
const PROPERTY_DESCRIPTIONS: Readonly<Record<string, string>> = {
addresses: "Ordered provider-normalized procedure addresses to analyze.",
@@ -56,11 +57,25 @@ export const toolInputSchemaWithMetadata = <Contract extends ToolContract>(
withAdvertisedJsonSchema(
contract.inputSchema,
"input",
(project) => (options) =>
flattenRootUnion({
...describeProperties(project(options)),
(project) => (options) => {
const shared = project({
...options,
libraryOptions: { reused: "ref", ...options.libraryOptions },
});
// Root-union flattening compares branch properties before widening them.
// Keep its inline representation: distinct references can name equal
// schemas and otherwise introduce redundant, deeper anyOf properties.
const compatible = Array.isArray(shared.anyOf)
? project({
...options,
libraryOptions: { ...options.libraryOptions, reused: "inline" },
})
: shared;
return {
...presentInputJsonSchema(compatible, fallbackPropertyDescription),
examples: contract.examples.map(({ input }) => input),
}),
};
},
);
/** Share repeated output definitions without changing canonical validation. */
@@ -122,89 +137,10 @@ const memoizeByTarget = (
};
};
/** Keep MCP tool roots object-shaped for clients that reject root unions. */
const flattenRootUnion = (
value: Record<string, unknown>,
): Record<string, unknown> => {
if (!Array.isArray(value.anyOf)) return value;
const branches = value.anyOf.filter(isObject);
if (branches.length === 0 || branches.length !== value.anyOf.length)
return value;
const properties: Record<string, unknown> = {};
for (const branch of branches) {
if (!isObject(branch.properties)) return value;
for (const [name, schema] of Object.entries(branch.properties)) {
const previous = properties[name];
if (previous === undefined) properties[name] = schema;
else if (JSON.stringify(previous) !== JSON.stringify(schema))
properties[name] = {
anyOf: [previous, schema],
description: fallbackPropertyDescription(name),
};
}
}
// The canonical Zod parser still enforces branch validation at invocation.
const { anyOf: _anyOf, required: _required, ...root } = value;
const requiredByBranch = branches.map((branch) =>
Array.isArray(branch.required)
? branch.required.filter(
(item): item is string => typeof item === "string",
)
: [],
);
const required = requiredByBranch.reduce((common, current) =>
common.filter((name) => current.includes(name)),
);
const minProperties = Math.max(
typeof root.minProperties === "number" ? root.minProperties : 0,
Math.min(...requiredByBranch.map((names) => names.length)),
);
return {
...root,
type: "object",
properties,
...(minProperties > 0 ? { minProperties } : {}),
...(required.length > 0 ? { required } : {}),
...(branches.every((branch) => branch.additionalProperties === false)
? { additionalProperties: false }
: {}),
};
};
const describeProperties = (
value: Readonly<Record<string, unknown>>,
): Record<string, unknown> =>
Object.fromEntries(
Object.entries(value).map(([key, child]) => {
if (key !== "properties" || !isObject(child))
return [key, describeValue(child)];
return [
key,
Object.fromEntries(
Object.entries(child).map(([property, propertySchema]) => [
property,
isObject(propertySchema) &&
typeof propertySchema.description !== "string"
? {
...describeProperties(propertySchema),
description: fallbackPropertyDescription(property),
}
: describeValue(propertySchema),
]),
),
];
}),
);
const describeValue = (value: unknown): unknown => {
if (Array.isArray(value)) return value.map(describeValue);
return isObject(value) ? describeProperties(value) : value;
};
const fallbackPropertyDescription = (property: string): string => {
const explicit = PROPERTY_DESCRIPTIONS[property];
const explicit = Object.hasOwn(PROPERTY_DESCRIPTIONS, property)
? PROPERTY_DESCRIPTIONS[property]
: undefined;
if (explicit !== undefined) return explicit;
const words = property.replaceAll("_", " ");
if (property.startsWith("max_"))
@@ -231,6 +167,3 @@ const fallbackPropertyDescription = (property: string): string => {
return `Whether ${words}.`;
return `Value for ${words}.`;
};
const isObject = (value: unknown): value is Readonly<Record<string, unknown>> =>
typeof value === "object" && value !== null && !Array.isArray(value);