openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
129 lines (128 loc) • 4.16 kB
JavaScript
import { p as toErrorObject } from "./error-coercion-hDrHOSGy.mjs";
import "./errors-BKzCeTYb.mjs";
import { t as getRuntimeExecutionProfile } from "./standalone-runtime-profile-B12NhwDV.mjs";
import { o as normalizeJsonSchemaForTypeBox } from "./json-schema-DKEhiksH.mjs";
import { n as findJsonSchemaShapeError } from "./json-schema-defaults-CjJJ5H5Y.mjs";
import { t as createJsonSchemaValidatorProvider } from "./json-schema-validator-DhIq4Jw0.mjs";
import { Compile } from "typebox/compile";
import fastUri from "fast-uri";
import { Ajv, AjvJsonSchemaValidator, addFormats } from "@modelcontextprotocol/client/validators/ajv";
//#region src/agents/mcp-json-schema-validator.ts
const DRAFT_2020_12_SCHEMA = "https://json-schema.org/draft/2020-12/schema";
const annotationFormats = /* @__PURE__ */ new Map();
function isDraft202012Schema(e) {
return e.$schema === DRAFT_2020_12_SCHEMA;
}
function formatTypeBoxErrors(e) {
return e.map((e) => {
const t = e.message?.trim() || "schema validation failed";
return e.instancePath ? `${e.instancePath} ${t}` : t;
}).join(", ") || "schema validation failed";
}
/** MCP SDK validator with draft-2020-12 support for external tool schemas. */
function createMcpJsonSchemaValidator() {
const e = new Ajv({
strict: false,
validateFormats: true,
validateSchema: false,
allErrors: true,
uriResolver: fastUri
});
addFormats(e);
if (getRuntimeExecutionProfile() === "embedded-aot") return createInterpretedMcpJsonSchemaValidator(e.formats);
const t = new AjvJsonSchemaValidator(e);
return { getValidator(e) {
if (!isDraft202012Schema(e)) return t.getValidator(e);
let n;
try {
const t = findJsonSchemaShapeError(e);
if (t) throw new Error(t);
n = Compile(normalizeJsonSchemaForTypeBox(e, { format: "annotation" }));
} catch (e) {
const t = toErrorObject(e, "schema setup failed");
throw new Error(`Invalid MCP draft-2020-12 JSON Schema: ${t.message}`, { cause: e });
}
return (e) => {
if (n.Check(e)) return {
valid: true,
data: e,
errorMessage: void 0
};
return {
valid: false,
data: void 0,
errorMessage: formatTypeBoxErrors([...n.Errors(e)])
};
};
} };
}
function createFormatRefinement(e, t) {
const error = () => `must match format "${e}"`;
if (t === true) return;
if (typeof t === "function") return {
check: (e) => typeof e !== "string" || t(e),
error
};
if (t instanceof RegExp) return {
check: (e) => typeof e !== "string" || t.test(e),
error
};
if (t.async) throw new Error(`Unsupported MCP JSON Schema format definition: ${e}`);
if (t.type === "number") return {
check: (e) => typeof e !== "number" || t.validate(e),
error
};
const n = t.validate;
if (typeof n === "string") throw new Error(`Unsupported MCP JSON Schema format definition: ${e}`);
return {
check: (e) => {
if (typeof e !== "string") return true;
if (n instanceof RegExp) return n.test(e);
return Reflect.apply(n, void 0, [e]) === true;
},
error
};
}
function createFormatRefinements(e) {
const t = /* @__PURE__ */ new Map();
for (const [n, r] of Object.entries(e)) {
if (r === void 0) continue;
const e = createFormatRefinement(n, r);
if (e) t.set(n, e);
}
return t;
}
/** Code-generation-free MCP validation with the existing draft and format policies. */
function createInterpretedMcpJsonSchemaValidator(e) {
let t;
return { getValidator(n) {
const r = n.$schema === DRAFT_2020_12_SCHEMA;
let i;
try {
i = (r ? createJsonSchemaValidatorProvider({
dialect: "2020-12",
formats: annotationFormats
}) : t ??= createJsonSchemaValidatorProvider({
dialect: "draft7",
formats: createFormatRefinements(e)
})).getValidator(n);
} catch (e) {
const t = toErrorObject(e, "schema setup failed");
throw new Error(`Invalid MCP ${r ? "draft-2020-12 " : ""}JSON Schema: ${t.message}`, { cause: e });
}
return (e) => {
if (i.check(e)) return {
valid: true,
data: e,
errorMessage: void 0
};
return {
valid: false,
data: void 0,
errorMessage: formatTypeBoxErrors(i.errors(e))
};
};
} };
}
//#endregion
export { createMcpJsonSchemaValidator };