Files
rea/src/contracts/toolSchemaMetadata.test.ts
T
Kaoru0822andClaude Opus 5.5 5ccb9241be perf(mcp): advertise each tool JSON Schema once per target (#1059)
The MCP SDK reconverts every registered Zod schema on each tools/list.
With 139 tools that is ~45 ms of Zod JSON Schema work per listing, for a
catalog that cannot change after registration. Memoize the advertised
input and output projections per target on the registration clones.

Listings are byte-identical; repeated tools/list drops from ~62 ms to
~18 ms on the stdio server.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 17:31:08 +08:00

56 lines
2.1 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { TOOL_CONTRACTS, toolContract } from "./toolContracts.js";
import {
toolInputSchemaWithMetadata,
toolOutputSchemaWithMetadata,
} from "./toolSchemaMetadata.js";
const target = "draft-2020-12";
describe("advertised tool JSON Schema", () => {
it("projects each canonical contract once per target", () => {
for (const contract of TOOL_CONTRACTS) {
const input = toolInputSchemaWithMetadata(contract)["~standard"];
const output = toolOutputSchemaWithMetadata(contract)["~standard"];
const advertisedInput = input.jsonSchema.input({ target });
const advertisedOutput = output.jsonSchema.output({ target });
expect(input.jsonSchema.input({ target }), contract.name).toBe(
advertisedInput,
);
expect(output.jsonSchema.output({ target }), contract.name).toBe(
advertisedOutput,
);
expect(advertisedInput.examples, contract.name).toEqual(
contract.examples.map(({ input: example }) => example),
);
expect(advertisedOutput, contract.name).toEqual(
contract.outputSchema["~standard"].jsonSchema.output({ target }),
);
}
});
it("keeps other targets and library options distinct", () => {
const contract = toolContract("open_binary");
const input = toolInputSchemaWithMetadata(contract)["~standard"];
const latest = input.jsonSchema.input({ target });
const draft07 = input.jsonSchema.input({ target: "draft-07" });
const libraryOptions = { unrepresentable: "any" };
const configured = input.jsonSchema.input({ target, libraryOptions });
expect(latest.$schema).toBe("https://json-schema.org/draft/2020-12/schema");
expect(draft07.$schema).toBe("http://json-schema.org/draft-07/schema#");
expect(input.jsonSchema.input({ target: "draft-07" })).toBe(draft07);
expect(configured).not.toBe(latest);
expect(input.jsonSchema.input({ target, libraryOptions })).not.toBe(
configured,
);
expect(
toolInputSchemaWithMetadata(contract).safeParse(
contract.examples[0]?.input,
).success,
).toBe(true);
});
});