Files
reverse-engineering-anything/tests/fixtures/canonicalOutputSchema.ts
T
morluto 960a387731 refactor(contracts): own CLI command names, catalog identity and schema presentation in contracts (#11425)
* refactor(contracts): move the CLI command name owner into src/contracts

* refactor(contracts): own catalog identity in contracts and server identity in process (#11435)

* refactor(contracts): move catalog identity into contracts and server identity into process

* refactor(contracts): own the advertised MCP schema presentation in contracts

* test(mcp): import the advertised presentation from contracts in toolSchemaValidity

* test(release): expect the catalog identity at its contracts path in the canary

* test(mcp): import the catalog identity from contracts in syntheticContractSurfaces

* refactor(contracts): import CLI command names from contracts in the capability conformance test
2026-11-16 05:31:37 +08:00

44 lines
1.7 KiB
TypeScript

import { z } from "zod";
import { DISCOVER_TOOLS } from "../../src/contracts/catalog/catalogDiscovery.js";
import { TOOL_CONTRACTS } from "../../src/contracts/toolContracts.js";
import {
advertisedInputJsonSchema,
advertisedOutputJsonSchema,
} from "../../src/contracts/toolSchemaMetadata.js";
const contractNamed = (name: string) => {
const contract = TOOL_CONTRACTS.find((candidate) => candidate.name === name);
if (contract === undefined) throw new Error(`No tool contract named ${name}`);
return contract;
};
/**
* The canonical output JSON Schema a validating client checks a reply
* against. `tools/list` omits output schemas by default, so tests validate
* structured results against the contract rather than a listed definition.
* Transport discovery is registered from its own schema, not a tool contract.
*/
export const canonicalOutputSchema = (name: string): Record<string, unknown> =>
name === DISCOVER_TOOLS.name
? z.toJSONSchema(DISCOVER_TOOLS.outputSchema, { io: "output" })
: advertisedOutputJsonSchema(contractNamed(name));
/** The canonical input JSON Schema that the compact advertisement presents. */
export const canonicalInputSchema = (name: string): Record<string, unknown> =>
advertisedInputJsonSchema(contractNamed(name));
/**
* A listed tool definition carrying its canonical output schema, so the SDK
* client (and any caller-supplied validator) checks replies against the
* contract even though `tools/list` omits output schemas by default.
*/
export const withCanonicalOutputSchema = <
Tool extends { readonly name: string },
>(
tool: Tool,
): Tool & { outputSchema: Record<string, unknown> } => ({
...tool,
outputSchema: canonicalOutputSchema(tool.name),
});