mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 19:53:50 +00:00
* refactor(mcp): leave input literal portability to the advertised dialect projection * test(mcp): leave listed-input literal portability to the dialect check * test(mcp): trigger normalization dependency cases with non-portable patterns * refactor(mcp): let the dialect projection own input patterns and dependency prose (#11238) * refactor(mcp): let the dialect projection own input patterns and dependency prose * refactor(mcp): drop the unused import from the output schema presentation * test(mcp): force the reduced form by depth now that the projection re-emits patterns * test(mcp): take advertised input from the one advertised-tool owner after #11268 * test(mcp): check the base64 pad-bit widening on the advertised input, not the registered one
151 lines
5.3 KiB
TypeScript
151 lines
5.3 KiB
TypeScript
import { Ajv2020 } from "ajv/dist/2020.js";
|
|
|
|
import {
|
|
schemaArrays as schemaArrayKeywords,
|
|
schemaChildren as schemaChildKeywords,
|
|
schemaMaps as schemaMapKeywords,
|
|
} from "../../src/contracts/jsonSchemaKeywords.js";
|
|
import { arrayItemSchemaError } from "./mcpClientSchemaConstraints.js";
|
|
|
|
/** Advertised schema pair as a client receives it from `tools/list`. */
|
|
interface AdvertisedToolSchemas {
|
|
readonly name: string;
|
|
readonly inputSchema: Record<string, unknown>;
|
|
readonly outputSchema?: Record<string, unknown> | undefined;
|
|
}
|
|
|
|
/**
|
|
* Validate registered schemas: Ajv's declared-dialect meta-schema and
|
|
* reference resolution, then array `items`. Compiling catches a projection
|
|
* that drops a referenced definition or leaves a local `$ref` without its
|
|
* target, which a byte-budget assertion alone would not. Literal portability
|
|
* belongs to the advertised dialect projection, so it is checked on the
|
|
* schemas a served catalog lists, not here.
|
|
*/
|
|
export function advertisedSchemaErrors(
|
|
tools: readonly AdvertisedToolSchemas[],
|
|
): string[] {
|
|
// Reuse Ajv's meta-schema validators, then clear each root's reference
|
|
// namespace, including nested IDs, before validating the next schema.
|
|
const ajv = new Ajv2020({
|
|
strict: false,
|
|
validateFormats: false,
|
|
addUsedSchema: false,
|
|
// This check compiles schemas to detect invalid references but never runs
|
|
// their validators. Generated-code optimization adds substantial work here
|
|
// without checking another contract; actual SDK validators keep defaults.
|
|
code: { optimize: false },
|
|
});
|
|
return tools.flatMap((tool) =>
|
|
["inputSchema", "outputSchema"].flatMap((kind) => {
|
|
const schema =
|
|
kind === "inputSchema" ? tool.inputSchema : tool.outputSchema;
|
|
if (schema === undefined) return [];
|
|
try {
|
|
if (!ajv.validateSchema(schema))
|
|
return [`${tool.name}.${kind}: ${ajv.errorsText(ajv.errors)}`];
|
|
ajv.compile(schema);
|
|
const clientError = arrayItemSchemaError(
|
|
schema,
|
|
`${tool.name}.${kind}`,
|
|
);
|
|
if (clientError !== undefined) return [clientError];
|
|
return [];
|
|
} catch (cause: unknown) {
|
|
return [
|
|
`${tool.name}.${kind}: ${cause instanceof Error ? cause.message : String(cause)}`,
|
|
];
|
|
} finally {
|
|
ajv.removeSchema();
|
|
}
|
|
}),
|
|
);
|
|
}
|
|
|
|
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
typeof value === "object" && value !== null && !Array.isArray(value);
|
|
|
|
/** Resolve a local JSON Pointer reference inside one schema document. */
|
|
export function resolveReference(schema: unknown, reference: string): unknown {
|
|
if (reference === "#") return schema;
|
|
if (!reference.startsWith("#/")) return undefined;
|
|
let value = schema;
|
|
for (const token of reference.slice(2).split("/")) {
|
|
const key = token.replaceAll("~1", "/").replaceAll("~0", "~");
|
|
if (
|
|
typeof value !== "object" ||
|
|
value === null ||
|
|
!Object.hasOwn(value, key)
|
|
)
|
|
return undefined;
|
|
value = Reflect.get(value, key);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
/** Local `$ref` cycles in each advertised schema; clients reject recursion. */
|
|
export function recursiveReferences(
|
|
tools: readonly AdvertisedToolSchemas[],
|
|
): string[] {
|
|
return tools.flatMap((tool) =>
|
|
(["inputSchema", "outputSchema"] as const).flatMap((kind) => {
|
|
const schema = tool[kind];
|
|
const found = new Set<string>();
|
|
const visit = (node: unknown, active: readonly string[]): void => {
|
|
if (typeof node !== "object" || node === null) return;
|
|
for (const [key, child] of Object.entries(node)) {
|
|
if (
|
|
[
|
|
"$defs",
|
|
"definitions",
|
|
"examples",
|
|
"default",
|
|
"const",
|
|
"enum",
|
|
].includes(key)
|
|
)
|
|
continue;
|
|
if (key !== "$ref" || typeof child !== "string") visit(child, active);
|
|
else if (active.includes(child)) found.add(child);
|
|
else visit(resolveReference(schema, child), [...active, child]);
|
|
}
|
|
};
|
|
visit(schema, []);
|
|
return [...found].map(
|
|
(reference) => `${tool.name}.${kind}: ${reference}`,
|
|
);
|
|
}),
|
|
);
|
|
}
|
|
|
|
/** Local `$ref`s in input schema positions, which some clients cannot resolve. */
|
|
export function localInputReferences(
|
|
tools: readonly AdvertisedToolSchemas[],
|
|
): string[] {
|
|
return tools.flatMap((tool) => {
|
|
const references: string[] = [];
|
|
const visit = (node: unknown, path: string): void => {
|
|
if (!isRecord(node)) return;
|
|
if (typeof node.$ref === "string") references.push(`${path}.$ref`);
|
|
for (const [key, value] of Object.entries(node)) {
|
|
if (schemaMapKeywords.has(key) && isRecord(value)) {
|
|
for (const [name, schema] of Object.entries(value))
|
|
visit(schema, `${path}.${key}.${name}`);
|
|
} else if (schemaArrayKeywords.has(key) && Array.isArray(value)) {
|
|
value.forEach((schema, index) =>
|
|
visit(schema, `${path}.${key}[${index}]`),
|
|
);
|
|
} else if (schemaChildKeywords.has(key)) {
|
|
if (Array.isArray(value))
|
|
value.forEach((schema, index) =>
|
|
visit(schema, `${path}.${key}[${index}]`),
|
|
);
|
|
else visit(value, `${path}.${key}`);
|
|
}
|
|
}
|
|
};
|
|
visit(tool.inputSchema, `${tool.name}.inputSchema`);
|
|
return references;
|
|
});
|
|
}
|