test(mcp): consolidate input schema compatibility contracts (#13323)

This commit is contained in:
morluto authored and GitHub committed 2026-11-21 23:06:54 -05:00
1 parent 27181314cb
commit 664c0c5267
1 file changed
+194 -307
+194 -307
View File
@@ -1,4 +1,4 @@
import { Ajv } from "ajv";
import { Ajv, type Options } from "ajv";
import { Ajv2020 } from "ajv/dist/2020.js";
import { describe, expect, it } from "vitest";
import { z } from "zod";
@@ -8,13 +8,34 @@ import { ADVERTISED_INPUT_SCHEMA_MAX_BYTES } from "./advertisedDialect.js";
import { advertiseToolInput } from "./advertisedTool.js";
import { presentAdvertisedInput } from "./compactInputSchema.js";
import { presentInputJsonSchema } from "./inputSchemaPresentation.js";
import { toolContract } from "./toolContracts.js";
import { ANALYSIS_VIEW_TOOL_CONTRACTS } from "./analysisViewToolContracts.js";
import { NATIVE_TOOL_CONTRACTS } from "./native/nativeToolContracts.js";
import { REVERSE_ENGINEERING_TOOL_CONTRACTS } from "./reverseEngineeringToolContracts.js";
import { SESSION_TOOL_CONTRACTS } from "./sessionToolContracts.js";
import type { ToolContract } from "./toolContractTypes.js";
import {
toolInputSchemaWithMetadata,
transformAdvertisedInputJsonSchema,
} from "./toolSchemaMetadata.js";
import { JAVASCRIPT_APPLICATION_EVIDENCE_EXAMPLE } from "./javascript/javascriptRuntimeReconciliationExample.js";
const drafts = ["draft-2020-12", "draft-07"] as const;
type Draft = (typeof drafts)[number];
const validatorFor = (
target: Draft = "draft-2020-12",
options: Options = {},
) =>
target === "draft-2020-12"
? new Ajv2020({ strict: false, ...options })
: new Ajv({ strict: false, ...options });
const contractFrom = (family: readonly ToolContract[], name: string) => {
const contract = family.find((entry) => entry.name === name);
if (contract === undefined)
throw new Error(`Missing test contract for ${name}`);
return contract;
};
/** The input schema a client receives: presentation, then the portable dialect. */
const advertise = (canonical: Record<string, unknown>) =>
advertiseToolInput(
@@ -23,7 +44,7 @@ const advertise = (canonical: Record<string, unknown>) =>
{ budgetBytes: ADVERTISED_INPUT_SCHEMA_MAX_BYTES },
).inputSchema;
it.each(["draft-2020-12", "draft-07"] as const)(
it.each(drafts)(
"honors projection options independently of cached wire transformations in %s",
(target) => {
const range = z.strictObject({ offset: z.number(), length: z.number() });
@@ -52,16 +73,13 @@ it.each(["draft-2020-12", "draft-07"] as const)(
},
);
it.each(["draft-2020-12", "draft-07"] as const)(
it.each(drafts)(
"preserves the complete inline Evidence contract before deferred authentication in %s",
(target) => {
const schema = toolInputSchemaWithMetadata(
toolContract("inspect_analysis_view"),
contractFrom(ANALYSIS_VIEW_TOOL_CONTRACTS, "inspect_analysis_view"),
)["~standard"].jsonSchema.input({ target });
const ajv =
target === "draft-2020-12"
? new Ajv2020({ strict: false, validateFormats: false })
: new Ajv({ strict: false, validateFormats: false });
const ajv = validatorFor(target, { validateFormats: false });
expect(ajv.validateSchema(schema)).toBe(true);
const validate = ajv.compile(schema);
const input = {
@@ -79,18 +97,19 @@ it.each(["draft-2020-12", "draft-07"] as const)(
},
);
it.each(["draft-2020-12", "draft-07"] as const)(
it.each(drafts)(
"keeps objdump Unicode validation and portable SDK patterns in %s",
(target) => {
const canonical = toolInputSchemaWithMetadata(
toolContract("inspect_with_objdump"),
contractFrom(REVERSE_ENGINEERING_TOOL_CONTRACTS, "inspect_with_objdump"),
);
const schema = canonical["~standard"].jsonSchema.input({ target });
const input = { path: "sample.elf", operation: "disassemble" };
for (const unicodeRegExp of [false, true]) {
const options = { strict: false, validateFormats: false, unicodeRegExp };
const ajv =
target === "draft-2020-12" ? new Ajv2020(options) : new Ajv(options);
const ajv = validatorFor(target, {
validateFormats: false,
unicodeRegExp,
});
expect(ajv.validateSchema(schema)).toBe(true);
const validate = ajv.compile(schema);
for (const symbol of [
@@ -152,39 +171,37 @@ describe("advertised object dependencies", () => {
});
describe("advertised production selector contracts", () => {
it.each(["draft-2020-12", "draft-07"] as const)(
it.each(drafts)(
"preserves production native-selector constraints in %s",
(target) => {
const contract = toolContract("observe_native_calls");
const contract = contractFrom(
NATIVE_TOOL_CONTRACTS,
"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 ajv = validatorFor(target);
const validate = ajv.compile(advertised);
for (const selector of [
"setFoo:",
"dataTaskWithRequest:completionHandler:",
"",
"bad selector:",
"bad[selector]:",
"set\0Foo:",
"setFoo:\n",
]) {
for (const [selector, accepted] of [
["setFoo:", true],
["dataTaskWithRequest:completionHandler:", true],
["", false],
["bad selector:", false],
["bad[selector]:", false],
["set\0Foo:", false],
["setFoo:\n", false],
] as const) {
const sample = {
breakpoints: [
{
kind: "objc-method",
class_name: "NSURLSession",
selector,
},
{ kind: "objc-method", class_name: "NSURLSession", selector },
],
};
expect(validate(sample)).toBe(
expect(
contract.inputSchema.safeParse(sample).success,
);
JSON.stringify(selector),
).toBe(accepted);
expect(validate(sample), JSON.stringify(selector)).toBe(accepted);
}
const unexpected = {
breakpoints: [
@@ -203,7 +220,7 @@ describe("advertised production selector contracts", () => {
});
describe("advertised schema metadata and validation", () => {
it.each(["draft-2020-12", "draft-07"] as const)(
it.each(drafts)(
"retains specialized descriptions, literal metadata and strict validation in %s",
(target) => {
const literal = {
@@ -227,17 +244,12 @@ describe("advertised schema metadata and validation", () => {
constructor: z.string(),
})
.strict();
const advertised = toolInputSchemaWithMetadata({
...toolContract("open_binary"),
const project = toolInputSchemaWithMetadata({
...contractFrom(SESSION_TOOL_CONTRACTS, "open_binary"),
inputSchema: canonical,
})["~standard"].jsonSchema.input({ target });
const inline = toolInputSchemaWithMetadata({
...toolContract("open_binary"),
inputSchema: canonical,
})["~standard"].jsonSchema.input({
target,
libraryOptions: { reused: "inline" },
});
})["~standard"].jsonSchema.input;
const advertised = project({ target });
const inline = project({ target, libraryOptions: { reused: "inline" } });
expect(inline).toMatchObject({
properties: {
before: { description: "Exact byte range in the selected artifact." },
@@ -251,10 +263,7 @@ describe("advertised schema metadata and validation", () => {
"constructor",
"description",
]);
const ajv =
target === "draft-2020-12"
? new Ajv2020({ strict: false })
: new Ajv({ strict: false });
const ajv = validatorFor(target);
const validate = ajv.compile(advertised);
const valid = {
before: { offset: 0, length: 1 },
@@ -313,7 +322,7 @@ describe("root input presentation", () => {
expect(Object.hasOwn(properties, "__proto__")).toBe(true);
});
it.each(["draft-2020-12", "draft-07"] as const)(
it.each(drafts)(
"advertises complete root groups while runtime keeps the exact union in %s",
(target) => {
const schema = z.union([
@@ -335,10 +344,7 @@ describe("root input presentation", () => {
expect(advertised.description).toBe(
"Provide a complete input group: before + after; before_scenario + after_scenario + [normalization].",
);
const ajv =
target === "draft-2020-12"
? new Ajv2020({ strict: false })
: new Ajv({ strict: false });
const ajv = validatorFor(target);
const validate = ajv.compile(advertised);
for (const input of [
{ before: "first", after: "second" },
@@ -389,7 +395,7 @@ describe("root input presentation", () => {
};
const presented = presentInputJsonSchema(root, (property) => property);
expect(presented.anyOf).toEqual([branch]);
const ajv = new Ajv2020({ strict: false });
const ajv = validatorFor();
for (const sample of [{}, { value: "value" }, { other: 1 }, null])
expect(ajv.compile(presented)(sample)).toBe(ajv.compile(root)(sample));
});
@@ -430,69 +436,46 @@ describe("root input presentation", () => {
});
});
const registerBooleanLiteralCompatibilityTests = (): void => {
it("keeps canonical boolean-literal inputs accepted after advertisement", () => {
const canonical = {
type: "object",
properties: {
enabled: { type: "boolean", const: false },
it("preserves canonical callers when Boolean literals and predicates widen", () => {
const canonical = {
type: "object",
properties: {
enabled: { type: "boolean", const: false },
flag: { type: "boolean", not: { const: false } },
choice: {
if: { const: false },
then: { type: "string" },
else: { type: "boolean" },
},
required: ["enabled"],
additionalProperties: false,
};
const advertised = advertise(canonical);
const canonicalValidate = new Ajv2020({ strict: false }).compile(canonical);
const advertisedValidate = new Ajv2020({ strict: false }).compile(
advertised,
);
expect(canonicalValidate({ enabled: false })).toBe(true);
expect(canonicalValidate({ enabled: true })).toBe(false);
expect(advertisedValidate({ enabled: false })).toBe(true);
expect(advertisedValidate({ enabled: true })).toBe(true);
expect(advertised.properties).toEqual({ enabled: { type: "boolean" } });
values: {
type: "array",
not: { items: { type: "boolean", const: false } },
},
},
};
const advertised = advertise(canonical);
const original = validatorFor().compile(canonical);
const presented = validatorFor().compile(advertised);
const valid = { enabled: false, flag: true, choice: true, values: [true] };
expect(original(valid)).toBe(true);
expect(presented(valid)).toBe(true);
for (const change of [
{ enabled: true },
{ flag: false },
{ choice: "text" },
{ values: [false] },
]) {
const input = { ...valid, ...change };
expect(original(input), JSON.stringify(change)).toBe(false);
expect(presented(input), JSON.stringify(change)).toBe(true);
}
expect(advertised.properties).toEqual({
enabled: { type: "boolean" },
flag: { type: "boolean" },
choice: {},
values: { type: "array", items: {} },
});
it("drops affected negation and conditional groups without rejecting canonical inputs", () => {
const negated = {
type: "object",
properties: {
flag: { type: "boolean", not: { const: false } },
},
};
const negatedAdvertisement = advertise(negated);
const canonicalNegated = new Ajv2020({ strict: false }).compile(negated);
const advertisedNegated = new Ajv2020({ strict: false }).compile(
negatedAdvertisement,
);
expect(canonicalNegated({ flag: true })).toBe(true);
expect(canonicalNegated({ flag: false })).toBe(false);
expect(advertisedNegated({ flag: true })).toBe(true);
const conditional = {
type: "object",
properties: {
choice: {
if: { const: false },
then: { type: "string" },
else: { type: "boolean" },
},
},
};
const conditionalAdvertisement = advertise(conditional);
const canonicalConditional = new Ajv2020({ strict: false }).compile(
conditional,
);
const advertisedConditional = new Ajv2020({ strict: false }).compile(
conditionalAdvertisement,
);
expect(canonicalConditional({ choice: true })).toBe(true);
expect(advertisedConditional({ choice: true })).toBe(true);
expect(conditionalAdvertisement.properties).toEqual({
choice: {},
});
});
};
});
describe("advertised client-compatible literals", () => {
it.each([{ const: "" }, { enum: ["", "named"] }])(
@@ -510,12 +493,8 @@ describe("advertised client-compatible literals", () => {
},
};
const advertised = advertise(canonical);
expect(
new Ajv2020({ strict: false }).compile(canonical)({ value: "" }),
).toBe(true);
expect(
new Ajv2020({ strict: false }).compile(advertised)({ value: "" }),
).toBe(true);
expect(validatorFor().compile(canonical)({ value: "" })).toBe(true);
expect(validatorFor().compile(advertised)({ value: "" })).toBe(true);
expect(advertised).toHaveProperty(["properties", "value"], {
type: "string",
});
@@ -535,8 +514,8 @@ describe("advertised client-compatible literals", () => {
},
};
const advertised = advertise(canonical);
const original = new Ajv2020({ strict: false }).compile(canonical);
const presented = new Ajv2020({ strict: false }).compile(advertised);
const original = validatorFor().compile(canonical);
const presented = validatorFor().compile(advertised);
for (const value of ["", "named"]) {
expect(original({ value })).toBe(true);
expect(presented({ value })).toBe(true);
@@ -544,46 +523,6 @@ describe("advertised client-compatible literals", () => {
expect(advertised).toHaveProperty(["properties", "value", "anyOf"]);
expect(advertised).not.toHaveProperty(["properties", "value", "oneOf"]);
});
it("keeps only defaults shared by every root input group", () => {
const schema = z.union([
z.strictObject({
left: z.string(),
shared: z.boolean().default(false),
only_left: z.string().default("left"),
selection: z.string().default("left"),
}),
z.strictObject({
right: z.string(),
shared: z.boolean().default(false),
selection: z.string().default("right"),
}),
]);
const advertised = presentInputJsonSchema(
z.toJSONSchema(schema, { io: "input" }),
() => undefined,
);
expect(advertised).toHaveProperty(
["properties", "shared", "default"],
false,
);
expect(advertised).not.toHaveProperty([
"properties",
"only_left",
"default",
]);
expect(advertised).toHaveProperty(["properties", "selection"], {
type: "string",
});
const input = { right: "selected" };
const validate = new Ajv2020({ strict: false, useDefaults: true }).compile(
advertised,
);
expect(validate(input)).toBe(true);
expect(schema.safeParse(input).success).toBe(true);
expect(input).not.toHaveProperty("only_left");
});
registerBooleanLiteralCompatibilityTests();
});
describe("advertised Boolean schema composition", () => {
@@ -597,10 +536,8 @@ describe("advertised Boolean schema composition", () => {
},
};
const advertised = advertise(canonical);
const canonicalValidate = new Ajv2020({ strict: false }).compile(canonical);
const advertisedValidate = new Ajv2020({ strict: false }).compile(
advertised,
);
const canonicalValidate = validatorFor().compile(canonical);
const advertisedValidate = validatorFor().compile(advertised);
expect(canonicalValidate({ flag: true })).toBe(true);
expect(canonicalValidate({ flag: false })).toBe(false);
expect(advertisedValidate({ flag: true })).toBe(true);
@@ -637,10 +574,8 @@ describe("advertised Boolean schema composition", () => {
},
};
const advertised = advertise(canonical);
const canonicalValidate = new Ajv2020({ strict: false }).compile(canonical);
const advertisedValidate = new Ajv2020({ strict: false }).compile(
advertised,
);
const canonicalValidate = validatorFor().compile(canonical);
const advertisedValidate = validatorFor().compile(advertised);
const example = { selection: { source: "capture.json", mode: false } };
expect(canonicalValidate(example)).toBe(true);
expect(advertisedValidate(example)).toBe(true);
@@ -655,32 +590,9 @@ describe("advertised Boolean schema composition", () => {
},
});
});
it("removes a nested not when widening a boolean item predicate", () => {
const canonical = {
type: "object",
properties: {
values: {
type: "array",
not: { items: { type: "boolean", const: false } },
},
},
};
const advertised = advertise(canonical);
const canonicalValidate = new Ajv2020({ strict: false }).compile(canonical);
const advertisedValidate = new Ajv2020({ strict: false }).compile(
advertised,
);
expect(canonicalValidate({ values: [true] })).toBe(true);
expect(advertisedValidate({ values: [true] })).toBe(true);
expect(advertised).toHaveProperty(["properties", "values"], {
type: "array",
items: {},
});
});
});
it.each(["draft-2020-12", "draft-07"] as const)(
it.each(drafts)(
"describes references from their containing resource in %s",
(dialect) => {
const presented = presentInputJsonSchema(
@@ -713,7 +625,7 @@ it.each(["draft-2020-12", "draft-07"] as const)(
},
);
it.each(["draft-2020-12", "draft-07"] as const)(
it.each(drafts)(
"keeps root union resource identities when lifting would change relative refs in %s",
(dialect) => {
const source = {
@@ -732,10 +644,7 @@ it.each(["draft-2020-12", "draft-07"] as const)(
};
const presented = presentInputJsonSchema(source, () => undefined);
const createAjv = () => {
const ajv =
dialect === "draft-07"
? new Ajv({ strict: false })
: new Ajv2020({ strict: false });
const ajv = validatorFor(dialect);
ajv.addSchema({
$id: "https://example.test/first/child.json",
type: "string",
@@ -770,8 +679,8 @@ it("does not lift properties that still refer to deleted root-union locations",
],
};
const presented = presentInputJsonSchema(source, () => undefined);
const canonical = new Ajv2020({ strict: false }).compile(source);
const advertised = new Ajv2020({ strict: false }).compile(presented);
const canonical = validatorFor().compile(source);
const advertised = validatorFor().compile(presented);
for (const value of [7, "seven", null])
expect(advertised({ value })).toBe(canonical({ value }));
});
@@ -797,8 +706,8 @@ it.each(["$ref", "$dynamicRef"])(
};
const presented = presentInputJsonSchema(source, () => undefined);
expect(presented.anyOf).toBeDefined();
const canonical = new Ajv2020({ strict: false }).compile(source);
const advertised = new Ajv2020({ strict: false }).compile(presented);
const canonical = validatorFor().compile(source);
const advertised = validatorFor().compile(presented);
for (const value of [
{ end: "done" },
{ next: { end: "done" } },
@@ -810,114 +719,94 @@ it.each(["$ref", "$dynamicRef"])(
);
describe("root union default presentation", () => {
it.each(["draft-2020-12", "draft-07"] as const)(
"omits a nested default the other root group would refuse in %s",
it.each(drafts)(
"keeps shared defaults while omitting conflicting and group-only defaults in %s",
(target) => {
const normalization = z
.strictObject({ rules: z.array(z.string()).default([]) })
.default({ rules: [] });
const schema = z.union([
z.strictObject({ before: z.string(), after: z.string() }),
z.strictObject({
before_scenario: z.string(),
after_scenario: z.string(),
normalization,
left: z.string(),
shared: z.boolean().default(false),
only_left: z.string().default("left"),
selection: z.string().default("left"),
}),
]);
const canonical = z.toJSONSchema(schema, { io: "input", target });
const advertised = presentInputJsonSchema(
canonical,
(property) => property,
);
const defaults: string[] = [];
const collect = (value: unknown, path: readonly string[]): void => {
if (Array.isArray(value)) {
value.forEach((child, index) =>
collect(child, [...path, String(index)]),
);
return;
}
if (typeof value !== "object" || value === null) return;
if (Object.hasOwn(value, "default")) defaults.push(path.join("."));
for (const [key, child] of Object.entries(value))
collect(child, [...path, key]);
};
collect(advertised, []);
// `normalization` belongs to the scenario group only. Leaving its
// `rules` default behind made a client that fills nested defaults send
// an object the strict passive group rejects.
expect(defaults).toEqual([]);
const ajv =
target === "draft-2020-12"
? new Ajv2020({ strict: false })
: new Ajv({ strict: false });
expect(
ajv.compile(advertised)({ before: "first", after: "second" }),
).toBe(true);
expect(
schema.safeParse({
before: "first",
after: "second",
normalization: { rules: [] },
}).success,
).toBe(false);
},
);
it.each(["draft-2020-12", "draft-07"] as const)(
"keeps a nested default a reference inside its resource targets in %s",
(target) => {
const schema = z.union([
z.strictObject({ before: z.string(), after: z.string() }),
z.strictObject({
before_scenario: z.string(),
after_scenario: z.string(),
right: z.string(),
shared: z.boolean().default(false),
selection: z.string().default("right"),
normalization: z
.object({ rules: z.array(z.string()).optional() })
.strictObject({ rules: z.array(z.string()).default([]) })
.default({ rules: [] }),
}),
]);
const canonical = z.toJSONSchema(schema, { io: "input", target }) as {
anyOf: {
properties: Record<string, Record<string, unknown>>;
required: string[];
additionalProperties: boolean;
}[];
};
// A nested resource keeps the group, so the union still flattens. Its
// sibling reference points into the default the flattening would drop.
const scenario = canonical.anyOf[1] as NonNullable<
(typeof canonical.anyOf)[1]
>;
const normalization = scenario.properties.normalization as Record<
string,
unknown
>;
canonical.anyOf[1] = {
...scenario,
properties: {
...scenario.properties,
normalization: {
$id: "https://example.test/normalization",
...normalization,
properties: {
...(normalization.properties as Record<string, unknown>),
alias: { $ref: "#/default/rules" },
},
},
},
};
const advertised = presentInputJsonSchema(
canonical,
(property) => property,
z.toJSONSchema(schema, { io: "input", target }),
() => undefined,
);
expect(advertised).toHaveProperty(
["properties", "shared", "default"],
false,
);
expect(advertised).toHaveProperty(["properties", "selection"], {
type: "string",
});
for (const path of [
["only_left", "default"],
["normalization", "default"],
["normalization", "properties", "rules", "default"],
])
expect(advertised).not.toHaveProperty(["properties", ...path]);
const validate = validatorFor(target, { useDefaults: true }).compile(
advertised,
);
for (const selected of [{ left: "before" }, { right: "after" }]) {
const input = { ...selected };
expect(validate(input)).toBe(true);
expect(input).toEqual({ ...selected, shared: false });
expect(schema.safeParse(input).success).toBe(true);
}
},
);
it.each(drafts)(
"keeps a nested default targeted by a reference in its resource in %s",
(target) => {
const canonical = {
$schema:
target === "draft-07"
? "http://json-schema.org/draft-07/schema#"
: "https://json-schema.org/draft/2020-12/schema",
anyOf: [
{
type: "object",
properties: { text: { type: "string" } },
required: ["text"],
additionalProperties: false,
},
{
type: "object",
properties: {
normalization: {
$id: "https://example.test/normalization",
type: "object",
properties: {
rules: { type: "object" },
alias: { $ref: "#/default/rules" },
},
default: { rules: { type: "string" } },
},
},
required: ["normalization"],
additionalProperties: false,
},
],
};
const advertised = presentInputJsonSchema(canonical, () => undefined);
expect(advertised).not.toHaveProperty("anyOf");
const ajv =
target === "draft-2020-12"
? new Ajv2020({ strict: false })
: new Ajv({ strict: false });
const validate = ajv.compile(advertised);
expect(validate({ before: "first", after: "second" })).toBe(true);
const validate = validatorFor(target).compile(advertised);
expect(validate({ text: "raw" })).toBe(true);
expect(validate({ normalization: { alias: "rule" } })).toBe(true);
expect(validate({ normalization: { alias: 7 } })).toBe(false);
},
);
});
@@ -941,10 +830,8 @@ describe("advertised empty-string literal edge cases", () => {
);
const advertised = advertise(canonical);
expect(JSON.stringify(advertised)).not.toContain('"const":""');
const canonicalValidate = new Ajv2020({ strict: false }).compile(canonical);
const advertisedValidate = new Ajv2020({ strict: false }).compile(
advertised,
);
const canonicalValidate = validatorFor().compile(canonical);
const advertisedValidate = validatorFor().compile(advertised);
const emptyRoute = { kind: "route", value: "", match: "exact" };
expect(canonicalValidate({ seed: emptyRoute })).toBe(true);
expect(advertisedValidate({ seed: emptyRoute })).toBe(true);