test: reuse validators for identical MCP schemas
This commit is contained in:
Vendored
+31
-25
@@ -6,6 +6,30 @@ import { fileURLToPath } from "node:url";
|
|||||||
import { expect, onTestFinished } from "vitest";
|
import { expect, onTestFinished } from "vitest";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
|
// validateSchema checks against the meta-schema without registering user $ids.
|
||||||
|
const schemaValidator = new Ajv2020({
|
||||||
|
strict: false,
|
||||||
|
validateFormats: false,
|
||||||
|
});
|
||||||
|
const validatedSchemas = new Set<string>();
|
||||||
|
const compiledValidators = new Map<string, ValidateFunction<unknown>>();
|
||||||
|
|
||||||
|
const compileValidator = (schema: unknown): ValidateFunction<unknown> => {
|
||||||
|
const schemaRecord = z.record(z.string(), z.unknown()).parse(schema);
|
||||||
|
const serialized = JSON.stringify(schemaRecord);
|
||||||
|
const cached = compiledValidators.get(serialized);
|
||||||
|
if (cached !== undefined) return cached;
|
||||||
|
|
||||||
|
// Keep a private Ajv registry per schema: distinct tool schemas can carry
|
||||||
|
// identical $id values, while this cache only reuses exact schema content.
|
||||||
|
const validate = new Ajv2020({
|
||||||
|
strict: false,
|
||||||
|
validateFormats: false,
|
||||||
|
}).compile<unknown>(schemaRecord);
|
||||||
|
compiledValidators.set(serialized, validate);
|
||||||
|
return validate;
|
||||||
|
};
|
||||||
|
|
||||||
/** Start the built production server without configuring deep binary providers. */
|
/** Start the built production server without configuring deep binary providers. */
|
||||||
export async function connectLocalToolsMcp() {
|
export async function connectLocalToolsMcp() {
|
||||||
const transport = new StdioClientTransport({
|
const transport = new StdioClientTransport({
|
||||||
@@ -28,50 +52,32 @@ export async function connectLocalToolsMcp() {
|
|||||||
});
|
});
|
||||||
await client.connect(transport);
|
await client.connect(transport);
|
||||||
const tools = (await client.listTools()).tools;
|
const tools = (await client.listTools()).tools;
|
||||||
const schemaValidator = new Ajv2020({
|
|
||||||
strict: false,
|
|
||||||
validateFormats: false,
|
|
||||||
});
|
|
||||||
for (const tool of tools)
|
for (const tool of tools)
|
||||||
for (const [kind, schema] of [
|
for (const [kind, schema] of [
|
||||||
["inputSchema", tool.inputSchema],
|
["inputSchema", tool.inputSchema],
|
||||||
["outputSchema", tool.outputSchema],
|
["outputSchema", tool.outputSchema],
|
||||||
] as const) {
|
] as const) {
|
||||||
if (schema === undefined) continue;
|
if (schema === undefined) continue;
|
||||||
const valid = schemaValidator.validateSchema(
|
const serialized = JSON.stringify(schema);
|
||||||
z.record(z.string(), z.unknown()).parse(schema),
|
if (validatedSchemas.has(serialized)) continue;
|
||||||
);
|
const schemaRecord = z.record(z.string(), z.unknown()).parse(schema);
|
||||||
|
const valid = schemaValidator.validateSchema(schemaRecord);
|
||||||
expect(
|
expect(
|
||||||
valid,
|
valid,
|
||||||
`${tool.name}.${kind}: ${schemaValidator.errorsText(schemaValidator.errors)}`,
|
`${tool.name}.${kind}: ${schemaValidator.errorsText(schemaValidator.errors)}`,
|
||||||
).toBe(true);
|
).toBe(true);
|
||||||
|
validatedSchemas.add(serialized);
|
||||||
}
|
}
|
||||||
const definitions = new Map(tools.map((tool) => [tool.name, tool]));
|
const definitions = new Map(tools.map((tool) => [tool.name, tool]));
|
||||||
const compiledInputs = new Set<string>();
|
|
||||||
const validators = new Map<string, ValidateFunction<unknown>>();
|
|
||||||
const call = async (name: string, arguments_: Record<string, unknown>) => {
|
const call = async (name: string, arguments_: Record<string, unknown>) => {
|
||||||
const definition = definitions.get(name);
|
const definition = definitions.get(name);
|
||||||
if (definition !== undefined && !compiledInputs.has(name)) {
|
if (definition !== undefined) compileValidator(definition.inputSchema);
|
||||||
new Ajv2020({ strict: false, validateFormats: false }).compile(
|
|
||||||
definition.inputSchema,
|
|
||||||
);
|
|
||||||
compiledInputs.add(name);
|
|
||||||
}
|
|
||||||
const response = await client.callTool({ name, arguments: arguments_ });
|
const response = await client.callTool({ name, arguments: arguments_ });
|
||||||
if (
|
if (
|
||||||
response.structuredContent !== undefined &&
|
response.structuredContent !== undefined &&
|
||||||
definition?.outputSchema !== undefined
|
definition?.outputSchema !== undefined
|
||||||
) {
|
) {
|
||||||
let validate = validators.get(name);
|
const validate = compileValidator(definition.outputSchema);
|
||||||
if (validate === undefined) {
|
|
||||||
validate = new Ajv2020({
|
|
||||||
strict: false,
|
|
||||||
validateFormats: false,
|
|
||||||
}).compile<unknown>(
|
|
||||||
z.record(z.string(), z.unknown()).parse(definition.outputSchema),
|
|
||||||
);
|
|
||||||
validators.set(name, validate);
|
|
||||||
}
|
|
||||||
expect(
|
expect(
|
||||||
validate(response.structuredContent),
|
validate(response.structuredContent),
|
||||||
JSON.stringify(validate.errors),
|
JSON.stringify(validate.errors),
|
||||||
|
|||||||
Reference in New Issue
Block a user