Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

perf: remember a tool's compiled input validator by its schema

mattmillartech1c13c2999b53
diff --git a/server/mcp-schema-validator.test.ts b/server/mcp-schema-validator.test.ts
index 9d887f7f45..bed25ab4d1 100644
--- a/server/mcp-schema-validator.test.ts
+++ b/server/mcp-schema-validator.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { compileToolSchema } from "./mcp-schema-validator.ts";
+import { compileToolSchema, serverToolValidator } from "./mcp-schema-validator.ts";
describe("compileToolSchema", () => {
it("accepts the numeric formats Rust's schemars writes and checks their ranges", () => {
@@ -42,3 +42,18 @@ describe("compileToolSchema", () => {
expect(() => compileToolSchema({ type: "object", properties: { x: { type: "string", format: "made-up" } } })).toThrow("could not be validated");
});
});
+
+describe("serverToolValidator", () => {
+ it("remembers the validator it built for a schema, and builds another for a different one", () => {
+ const schema = { type: "object", properties: { name: { type: "string" } } };
+ const first = serverToolValidator(schema);
+ expect(serverToolValidator({ ...schema })).toBe(first);
+ expect(first.check).toBe("full");
+ expect(first.validate({ name: "x" })).toBe(true);
+ expect(first.validate({ name: 1 })).toBe(false);
+ const other = serverToolValidator({ type: "object", properties: { count: { type: "integer" } } });
+ expect(other).not.toBe(first);
+ expect(other.validate({ count: 1 })).toBe(true);
+ expect(other.validate({ name: "x" })).toBe(true);
+ });
+});
diff --git a/server/mcp-schema-validator.ts b/server/mcp-schema-validator.ts
index e94341eac2..8fa2d3a559 100644
--- a/server/mcp-schema-validator.ts
+++ b/server/mcp-schema-validator.ts
@@ -77,8 +77,25 @@ export function compileServerToolSchema(schema: Record<string, unknown>): Valida
export type ServerToolCheck = "full" | "lenient" | "object";
/** A check for every tool a server lists, so one schema this checker cannot compile never costs the server its other tools.
- * Security: no compiler here loads a schema, so an external `$ref` is never fetched; it leaves the check to the server. */
+ * Security: no compiler here loads a schema, so an external `$ref` is never fetched; it leaves the check to the server.
+ * The build compiles a fresh Ajv and its result depends only on the schema, so it is remembered by the schema's JSON text. */
export function serverToolValidator(schema: Record<string, unknown>): { validate: ValidateFunction; check: ServerToolCheck } {
+ const memoKey = JSON.stringify(schema);
+ const memo = serverToolValidators.get(memoKey);
+ if (memo !== undefined) return memo;
+ const made = buildServerToolValidator(schema);
+ if (serverToolValidators.size >= SERVER_TOOL_VALIDATOR_LIMIT) {
+ const oldest = serverToolValidators.keys().next().value;
+ if (oldest !== undefined) serverToolValidators.delete(oldest);
+ }
+ serverToolValidators.set(memoKey, made);
+ return made;
+}
+
+const SERVER_TOOL_VALIDATOR_LIMIT = 1_024;
+const serverToolValidators = new Map<string, { validate: ValidateFunction; check: ServerToolCheck }>();
+
+function buildServerToolValidator(schema: Record<string, unknown>): { validate: ValidateFunction; check: ServerToolCheck } {
try { return { validate: compileServerToolSchema(schema), check: "full" }; }
catch {}
const lenient = lenientToolValidator(schema);