Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(mcp): accept the numeric formats Rust's schemars writes
Fabio Nunes Cerqueira89b86f06544a
diff --git a/server/mcp-schema-validator.test.ts b/server/mcp-schema-validator.test.tsnew file mode 100644index 0000000000..e3333e8cef--- /dev/null+++ b/server/mcp-schema-validator.test.ts@@ -0,0 +1,33 @@+import { describe, expect, it } from "vitest";+import { compileToolSchema } from "./mcp-schema-validator.ts";++describe("compileToolSchema", () => {+ it("accepts the numeric formats Rust's schemars writes and checks their ranges", () => {+ const validate = compileToolSchema({+ $schema: "https://json-schema.org/draft/2020-12/schema", type: "object",+ properties: { limit: { type: "integer", format: "uint" }, depth: { type: "integer", format: "uint8" }, score: { type: "number", format: "float" }, offset: { type: "integer", format: "int32" } },+ });+ expect(validate({ limit: 10, depth: 3, score: 0.5, offset: -2 })).toBe(true);+ expect(validate({ depth: 256 })).toBe(false);+ expect(validate({ limit: -1 })).toBe(false);+ expect(validate({ offset: 2147483648 })).toBe(false);+ });++ it("holds every integer format to its range, with 64-bit ones capped at the safe integer", () => {+ const cases: Array<[string, number, number]> = [+ ["int8", -128, 127], ["int16", -32768, 32767], ["int32", -2147483648, 2147483647], ["int64", Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER],+ ["int", Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER], ["int128", Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER],+ ["uint", 0, Number.MAX_SAFE_INTEGER], ["uint128", 0, Number.MAX_SAFE_INTEGER], ["uint8", 0, 255], ["uint16", 0, 65535], ["uint32", 0, 4294967295], ["uint64", 0, Number.MAX_SAFE_INTEGER],+ ];+ for (const [format, min, max] of cases) {+ const validate = compileToolSchema({ type: "object", properties: { n: { type: "integer", format } } });+ expect([validate({ n: min }), validate({ n: max }), validate({ n: min - 1 }), validate({ n: max + 1 }), validate({ n: 1.5 })], format).toEqual([true, true, false, false, false]);+ }+ const double = compileToolSchema({ type: "object", properties: { x: { type: "number", format: "double" } } });+ expect([double({ x: -1e300 }), double({ x: 0.25 })]).toEqual([true, true]);+ });++ it("still refuses a format it does not know", () => {+ expect(() => compileToolSchema({ type: "object", properties: { x: { type: "string", format: "made-up" } } })).toThrow("could not be validated");+ });+});diff --git a/server/mcp-schema-validator.ts b/server/mcp-schema-validator.tsindex 3dc6c4036d..2bd4e33dae 100644--- a/server/mcp-schema-validator.ts+++ b/server/mcp-schema-validator.ts@@ -10,6 +10,16 @@ const validatorOptions = {strictRequired: false, strictTypes: false, strictTuples: false,};+// These replace ajv-formats' int32/int64. Its int64 takes any integer, but a value past the safe integer has+// already lost precision in JSON.parse, so it is refused rather than sent to the server rounded. schemars names+// isize/usize `int`/`uint`, and i128/u128 `int128`/`uint128`.+const INTEGER_FORMATS: ReadonlyArray<readonly [string, number, number]> = [+ ["int8", -128, 127], ["int16", -32768, 32767], ["int32", -2147483648, 2147483647],+ ...["int", "int64", "int128"].map((format) => [format, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER] as const),+ ["uint8", 0, 255], ["uint16", 0, 65535], ["uint32", 0, 4294967295],+ ...["uint", "uint64", "uint128"].map((format) => [format, 0, Number.MAX_SAFE_INTEGER] as const),+];+export function compileToolSchema(schema: Record<string, unknown>, options: { allErrors?: boolean; strict?: boolean } = {}): ValidateFunction {const dialect = schema.$schema;if (dialect !== undefined && dialect !== "http://json-schema.org/draft-07/schema#" && dialect !== "https://json-schema.org/draft/2020-12/schema") {@@ -24,8 +34,9 @@ export function compileToolSchema(schema: Record<string, unknown>, options: { alcompiler.addKeyword({ keyword: "x-mcp-header", schemaType: "string" });// Google's remote MCP servers (Maps, Gmail, Drive) annotate enums with per-value descriptions; it adds no constraint to check.compiler.addKeyword({ keyword: "x-google-enum-descriptions", schemaType: "array" });- compiler.addFormat("uint32", { type: "number", validate: value => Number.isInteger(value) && value >= 0 && value <= 4294967295 });- compiler.addFormat("uint64", { type: "number", validate: value => Number.isSafeInteger(value) && value >= 0 });+ // Rust's schemars (rmcp servers such as ai-memory) names each numeric type as a format. float and double+ // come from ajv-formats and take any number: JSON.parse never yields NaN or Infinity.+ for (const [format, min, max] of INTEGER_FORMATS) compiler.addFormat(format, { type: "number", validate: value => Number.isInteger(value) && value >= min && value <= max });try { return compiler.compile(schema); }catch { throw new Error("MCP tool schema could not be validated; check its constraints, formats, and references"); }}