Skip to content

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

11.0 KB
Raw Blame
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,
  removeAdditional: false, validateFormats: true, ownProperties: true, logger: false as const,
  // Style diagnostics, not unsupported keywords: Ajv still enforces every constraint.
  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 !== 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.
  const settings = { ...validatorOptions, allErrors: options.allErrors === true, strict: options.strict !== false };
  const compiler = dialect === DRAFT_2020_12 ? new Ajv2020(settings) : new Ajv(settings);
  // ajv-formats is CommonJS and exports the plugin as both module.exports and .default.
  formats.default(compiler);
  // GitHub's MCP server tags header-routed arguments with this annotation; it adds no constraint to check.
  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" });
  for (const [format, min, max] of INTEGER_FORMATS) compiler.addFormat(format, { type: "number", validate: value => Number.isInteger(value) && value >= min && value <= max });
  // schemars writes `ip` for Rust's IpAddr (Ipv4Addr/Ipv6Addr get ajv-formats' ipv4/ipv6): either family. IpAddr has
  // no zone id (`fe80::1%eth0`), which node's isIP accepts.
  compiler.addFormat("ip", { type: "string", validate: value => isIP(value) !== 0 && !value.includes("%") });
  try { return compiler.compile(schema); }
  catch { throw new Error("MCP tool schema could not be validated; check its constraints, formats, and references"); }
}

/** A configured server's tool schema, strictly. MCP (2025-11-25) reads a schema without `$schema` as JSON Schema 2020-12,
 * while older servers write draft-07 without naming it, so such a schema is tried as each. Where strict compilation refuses
 * it, its copy without what asserts nothing is compiled instead, so a refusal still means a constraint this checker cannot hold. */
export function compileServerToolSchema(schema: Record<string, unknown>): ValidateFunction {
  const plain = withoutNonAssertions(schema);
  const candidates = [schema, ...(plain ? [plain] : [])]
    .flatMap((candidate) => candidate.$schema === undefined ? [candidate, { ...candidate, $schema: DRAFT_2020_12 }] : [candidate]);
  let refusal: unknown;
  for (const candidate of candidates) {
    try { return compileToolSchema(candidate); }
    catch (error) { refusal ??= error; }
  }
  throw refusal;
}

/** How much of a server tool's schema OpenMausBot checks a call against before the server checks it: all of it, the
 * lenient copy, or only that the arguments are an object. */
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.
 * 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);
  return lenient ? { validate: lenient, check: "lenient" } : { validate: compileToolSchema({ type: "object" }), check: "object" };
}

/** What tool generators emit that never asserts: OpenAPI's `example`, `discriminator`, `externalDocs` and `xml`, and VS
 * Code's editor hints. Vendor `x-` extensions too, except the two compileToolSchema checks. */
const NON_ASSERTIONS = new Set(["example", "discriminator", "externalDocs", "xml", "markdownDescription", "markdownEnumDescriptions", "enumDescriptions"]);
const CHECKED_EXTENSIONS = new Set(["x-mcp-header", "x-google-enum-descriptions"]);
const SCHEMA_KEYWORD_MAPS = ["properties", "$defs", "definitions", "dependencies", "dependentSchemas", "patternProperties"];

function localRefsOnly(schema: unknown, depth = 0): boolean {
  if (Array.isArray(schema)) return schema.every((child) => localRefsOnly(child, depth + 1));
  if (!isRecord(schema) || depth > 64) return true;
  if (typeof schema.$ref === "string" && !schema.$ref.startsWith("#/") && schema.$ref !== "#") return false;
  return Object.values(schema).every((child) => localRefsOnly(child, depth + 1));
}

/** Embedded `$id`s go too when every `$ref` is a JSON pointer: Effect Schema gives each `unknown` field the same
 * `"$id": "/schemas/unknown"`, and Ajv refuses a schema holding one id twice. Undefined when nothing would change. */
function withoutNonAssertions(schema: Record<string, unknown>): Record<string, unknown> | undefined {
  const dropIds = localRefsOnly(schema);
  let changed = false;
  const visit = (node: unknown, depth: number): unknown => {
    if (Array.isArray(node)) return node.map((child) => visit(child, depth));
    if (!isRecord(node) || depth > 64) return node;
    const copy: Json = {};
    for (const [key, value] of Object.entries(node)) {
      if (NON_ASSERTIONS.has(key) || (key.startsWith("x-") && !CHECKED_EXTENSIONS.has(key)) || (dropIds && depth > 0 && key === "$id")) { changed = true; continue; }
      if (SCHEMA_KEYWORD_MAPS.includes(key) && isRecord(value)) copy[key] = Object.fromEntries(Object.entries(value).map(([name, child]) => [name, visit(child, depth + 1)]));
      else if (SCHEMA_LISTS.includes(key) || SCHEMA_ONE.includes(key)) copy[key] = visit(value, depth + 1);
      else copy[key] = value;
    }
    return copy;
  };
  const plain = visit(schema, 0) as Json;
  if (plain.$schema === DRAFT_07.slice(0, -1)) { plain.$schema = DRAFT_07; changed = true; }
  return changed ? plain : undefined;
}

export function schemaProblems(errors: readonly ErrorObject[] | null | undefined, max = 8): string[] {
  const lines = (errors ?? []).map((error) => {
    const where = error.instancePath ? `${error.instancePath.slice(1).replace(/\//g, ".")}: ` : "";
    const params = error.params as Record<string, unknown>;
    const detail = error.keyword === "additionalProperties" ? ` (${String(params.additionalProperty)})`
      : error.keyword === "enum" ? `: ${JSON.stringify(params.allowedValues).slice(0, 200)}` : "";
    return `${where}${error.message ?? "is not valid"}${detail}`;
  });
  return [...new Set(lines)].slice(0, max);
}

type Json = Record<string, unknown>;
const isRecord = (value: unknown): value is Json => !!value && typeof value === "object" && !Array.isArray(value);

export const LENIENT_SCHEMA_CHARS = 64_000;
const SCHEMA_MAPS = ["properties", "$defs", "definitions", "dependentSchemas"];
const SCHEMA_LISTS = ["anyOf", "oneOf", "allOf", "prefixItems"];
const SCHEMA_ONE = ["items", "additionalItems", "additionalProperties", "not", "if", "then", "else", "contains", "propertyNames", "unevaluatedItems", "unevaluatedProperties"];

/** Drops what a checker in front of the server must never refuse on: `pattern`, `patternProperties` and `format` (ReDoS,
 * format disagreements), and `additionalProperties: false` beside patternProperties or combinators. */
export function lenientSchema(schema: unknown, depth = 0): unknown {
  if (Array.isArray(schema)) return schema.map((child) => lenientSchema(child, depth));
  if (!isRecord(schema) || depth > 64) return schema;
  const copy: Json = {};
  for (const [key, value] of Object.entries(schema)) {
    if (key === "pattern" || key === "patternProperties" || key === "format") continue;
    if (SCHEMA_MAPS.includes(key) && isRecord(value)) copy[key] = Object.fromEntries(Object.entries(value).map(([name, child]) => [name, lenientSchema(child, depth + 1)]));
    else if (SCHEMA_LISTS.includes(key) || SCHEMA_ONE.includes(key)) copy[key] = lenientSchema(value, depth + 1);
    else if (key === "dependencies" && isRecord(value)) copy[key] = Object.fromEntries(Object.entries(value).map(([name, child]) => [name, Array.isArray(child) ? child : lenientSchema(child, depth + 1)]));
    else copy[key] = value;
  }
  const opened = "patternProperties" in schema || SCHEMA_LISTS.slice(0, 3).some((key) => key in schema);
  if (opened && copy.additionalProperties === false) delete copy.additionalProperties;
  return copy;
}

export function lenientToolValidator(schema: unknown): ValidateFunction | null {
  if (!isRecord(schema)) return null;
  try {
    if (JSON.stringify(schema).length > LENIENT_SCHEMA_CHARS) return null;
    return compileToolSchema(lenientSchema(schema) as Json, { allErrors: true, strict: false });
  } catch {
    return null;
  }
}