Skip to content

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

fix(mcp): accept schemars' ip format as an IPv4 or IPv6 address

Fabio Nunes Cerqueira9c8fecd8a318
diff --git a/server/mcp-schema-validator.test.ts b/server/mcp-schema-validator.test.ts
index e3333e8cef..748233b13f 100644
--- a/server/mcp-schema-validator.test.ts
+++ b/server/mcp-schema-validator.test.ts
@@ -27,6 +27,12 @@ describe("compileToolSchema", () => {
expect([double({ x: -1e300 }), double({ x: 0.25 })]).toEqual([true, true]);
});
+ it("accepts schemars' `ip` (Rust's IpAddr) as an IPv4 or IPv6 address", () => {
+ const validate = compileToolSchema({ type: "object", properties: { host: { type: "string", format: "ip" } } });
+ expect(["192.168.0.1", "::1", "2001:db8::8a2e:370:7334"].map((host) => validate({ host }))).toEqual([true, true, true]);
+ expect(["256.1.1.1", "example.com", "192.168.0.1/24", ""].map((host) => validate({ host }))).toEqual([false, false, false, false]);
+ });
+
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.ts
index d3751be70d..013165e1b8 100644
--- a/server/mcp-schema-validator.ts
+++ b/server/mcp-schema-validator.ts
@@ -1,6 +1,7 @@
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";
// A schema is a contract: no coercion, defaults, removed fields or fetched external references.
const validatorOptions = {
@@ -46,6 +47,8 @@ export function compileToolSchema(schema: Record<string, unknown>, options: { al
// 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.
+ compiler.addFormat("ip", { type: "string", validate: value => isIP(value) !== 0 });
try { return compiler.compile(schema); }
catch { throw new Error("MCP tool schema could not be validated; check its constraints, formats, and references"); }
}