Skip to content

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

refactor(mcp): one comment and a flat table for the schemars formats

Fabio Nunes Cerqueira39a3be749a63
diff --git a/server/mcp-schema-validator.ts b/server/mcp-schema-validator.ts
index 2bd4e33dae..d3751be70d 100644
--- a/server/mcp-schema-validator.ts
+++ b/server/mcp-schema-validator.ts
@@ -10,14 +10,25 @@ 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 SAFE_MIN = Number.MIN_SAFE_INTEGER;
+const SAFE_MAX = Number.MAX_SAFE_INTEGER;
+// schemars 1.x writes one of these formats for each Rust integer type (isize/usize are `int`/`uint`); its `float` and
+// `double` come from ajv-formats, which takes any number, and JSON.parse never yields NaN or Infinity. These replace
+// ajv-formats' int32/int64: its int64 takes any integer, but past the safe integer JSON.parse may already have rounded
+// the value and cannot tell, so it is refused rather than sent on.
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),
+ ["int8", -128, 127],
+ ["int16", -32768, 32767],
+ ["int32", -2147483648, 2147483647],
+ ["int64", SAFE_MIN, SAFE_MAX],
+ ["int128", SAFE_MIN, SAFE_MAX],
+ ["int", SAFE_MIN, SAFE_MAX],
+ ["uint8", 0, 255],
+ ["uint16", 0, 65535],
+ ["uint32", 0, 4294967295],
+ ["uint64", 0, SAFE_MAX],
+ ["uint128", 0, SAFE_MAX],
+ ["uint", 0, SAFE_MAX],
];
export function compileToolSchema(schema: Record<string, unknown>, options: { allErrors?: boolean; strict?: boolean } = {}): ValidateFunction {
@@ -34,8 +45,6 @@ export function compileToolSchema(schema: Record<string, unknown>, options: { al
compiler.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" });
- // 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"); }