Skip to content

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

Merge pull request #3886 from LittleGuava/fix/mcp-schemars-numeric-formats

milind-sonid479d0d514b9
diff --cc server/mcp-schema-validator.ts
index f0d88cc177,36004bc176..99f9defbce
--- a/server/mcp-schema-validator.ts
+++ b/server/mcp-schema-validator.ts
@@@ -1,10 -1,8 +1,11 @@@
import { Ajv, type ErrorObject, type ValidateFunction } from "ajv";
import { Ajv2020 } from "ajv/dist/2020.js";
import formats from "ajv-formats";
+ import { isIP } from "node:net";
+const DRAFT_07 = "http://json-schema.org/draft-07/schema#";
+const DRAFT_2020_12 = "https://json-schema.org/draft/2020-12/schema";
+
// A schema is a contract: no coercion, defaults, removed fields or fetched external references.
const validatorOptions = {
strict: true, allErrors: false, coerceTypes: false, useDefaults: false,
@@@ -13,9 -11,30 +14,30 @@@
strictRequired: false, strictTypes: false, strictTuples: false,
};
+ 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],
+ ["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 {
const dialect = schema.$schema;
- if (dialect !== undefined && dialect !== "http://json-schema.org/draft-07/schema#" && dialect !== "https://json-schema.org/draft/2020-12/schema") {
+ if (dialect !== undefined && dialect !== DRAFT_07 && dialect !== DRAFT_2020_12) {
throw new Error("MCP tool schema uses an unsupported dialect; use JSON Schema draft-07 or 2020-12");
}
// One compiler per schema keeps external IDs from resolving against unrelated tools.