test(mcp): consolidate reduced output schema proof at the SDK boundary (#12823)

* test(mcp): consolidate reduced output schema proof at the SDK boundary

* test(mcp): verify both required fields on the reduced output root
This commit is contained in:
morluto authored and GitHub committed 2026-11-20 08:24:46 -05:00
1 parent 68ed9b0e67
commit 40c6dade8e
2 files changed
+15 -75

No files matched your search

-67
View File
@@ -48,27 +48,6 @@ const referencedSchema = {
},
} as const;
const oversizedSchema = (): Record<string, unknown> => ({
type: "object",
description: "A complete result envelope.",
properties: {
envelope: { type: "string" },
result: {
type: "object",
properties: Object.fromEntries(
Array.from({ length: 200 }, (_, index) => [
`field_${index}`,
{ type: "string", description: `Field ${index}` },
]),
),
required: Array.from({ length: 200 }, (_, index) => `field_${index}`),
additionalProperties: false,
},
},
required: ["envelope", "result"],
additionalProperties: false,
});
describe("compact advertised output schema", () => {
it("drops nested annotation prose while keeping the root guidance and validation", () => {
const { schema, presentation } = compactAdvertisedOutputSchema(
@@ -102,46 +81,6 @@ describe("compact advertised output schema", () => {
expect(entries.items).toMatchObject({ type: "object" });
});
it("reduces an oversized contract to a widening root shape within budget", () => {
const budgetBytes = 4 * 1024;
const { schema, presentation } = compactAdvertisedOutputSchema(
oversizedSchema(),
budgetBytes,
);
expect(presentation).toBe("reduced");
expect(serializedBytes(schema)).toBeLessThanOrEqual(budgetBytes);
expect(schema.type).toBe("object");
expect(schema.required).toEqual(["envelope", "result"]);
expect(schema.additionalProperties).toBe(false);
expect(schema.description).toContain("compact output schema profile");
const result = (schema.properties as Record<string, unknown>)
.result as Record<string, unknown>;
expect(result.type).toBe("object");
expect(result.properties).toBeUndefined();
});
it("accepts a valid canonical result after reduction", () => {
const budgetBytes = 4 * 1024;
const { schema } = compactAdvertisedOutputSchema(
oversizedSchema(),
budgetBytes,
);
const validate = new Ajv2020({ strict: false }).compile(schema);
const result = {
envelope: "ok",
result: Object.fromEntries(
Array.from({ length: 200 }, (_, index) => [
`field_${index}`,
`value ${index}`,
]),
),
};
expect(validate(result)).toBe(true);
// The advertisement is wider than the canonical contract: dropping nested
// detail must never reject a valid result, but it can accept an invalid one.
expect(validate({ envelope: "ok", result: { unknown: 1 } })).toBe(true);
});
it("keeps a non-object root accepting every JSON value when it cannot fit", () => {
const arraySchema = {
type: "array",
@@ -162,10 +101,4 @@ describe("compact advertised output schema", () => {
expect(validate(["anything"])).toBe(true);
expect(validate({ also: "anything" })).toBe(true);
});
it("is deterministic across equivalent inputs", () => {
expect(compactAdvertisedOutputSchema(oversizedSchema(), 4 * 1024)).toEqual(
compactAdvertisedOutputSchema(oversizedSchema(), 4 * 1024),
);
});
});
+15 -8
View File
@@ -198,14 +198,16 @@ it("rejects canonical-invalid output that the reduced advertisement accepts", as
z.string().optional(),
]),
);
const outputSchema = z.strictObject({
value: z.string(),
payload: z.object({
inner: z.string(),
count: z.number().int(),
...nestedDetail,
}),
});
const outputSchema = z
.strictObject({
value: z.string(),
payload: z.object({
inner: z.string(),
count: z.number().int(),
...nestedDetail,
}),
})
.describe("Fixture result envelope.");
const server = createServer("compact");
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
@@ -255,6 +257,7 @@ it("rejects canonical-invalid output that the reduced advertisement accepts", as
const tools = (await client.listTools()).tools;
const advertised = tools.find((tool) => tool.name === contract.name)!
.outputSchema as Record<string, unknown>;
expect(advertised.description).toContain("Fixture result envelope.");
expect(advertised.description).toContain("reduced form");
expect((advertised.properties as Record<string, unknown>).payload).toEqual({
type: "object",
@@ -270,6 +273,10 @@ it("rejects canonical-invalid output that the reduced advertisement accepts", as
validateFormats: false,
}).compile(advertised);
expect(validateAdvertised(malformed)).toBe(true);
expect(validateAdvertised([])).toBe(false);
expect(validateAdvertised({ payload: malformed.payload })).toBe(false);
expect(validateAdvertised({ value: malformed.value })).toBe(false);
expect(validateAdvertised({ ...malformed, extra: true })).toBe(false);
const success = await client.callTool({
name: contract.name,