Skip to content

MCPTool.inputSchema generates invalid or wrong JSON Schema for positive(), exclusive bounds, nullable(), union(), record(), refine(), and literal() #200

Open

Description

@trackoor

MCPTool.inputSchema (and outputSchema) produces invalid or inaccurate JSON Schema for several standard Zod types that runtime toolCall validation accepts, including positive() and z.union(...) shown in the README's AdvancedSchema example.

Reproduction

mkdir repro-mcpf && cd repro-mcpf
npm init -y && npm pkg set type=module
npm install [email protected] zod@3 ajv@8 ajv-draft-04
node repro.mjs

repro.mjs:

import { MCPTool } from "mcp-framework";
import { z } from "zod";
import Ajv2020 from "ajv/dist/2020.js";
import AjvDraft04 from "ajv-draft-04";

const schema = z.object({
  age: z.number().int().positive().describe("positive int"),
  ratio: z.number().positive().describe("positive float"),
  below: z.number().lt(10).describe("less than 10"),
  note: z.string().nullable().describe("nullable note"),
  mode: z.literal("fast").describe("literal"),
  id: z.union([z.string(), z.number()]).describe("union"),
  count: z.number().int().refine((n) => n % 2 === 0).describe("even int"),
  tags: z.record(z.string()).describe("record"),
  code: z.string().regex(/^[a-z]+$/i).describe("case-insensitive code"),
});

class DemoTool extends MCPTool {
  name = "demo";
  description = "demo tool";
  schema = schema;
  async execute(input) {
    return input;
  }
}

const tool = new DemoTool();
console.log(JSON.stringify(tool.inputSchema, null, 2));

const sample = {
  age: 1,
  ratio: 0.5,
  below: 3,
  note: null,
  mode: "fast",
  id: 7,
  count: 4,
  tags: { a: "b" },
  code: "ABC",
};
console.log("zod accepts sample:", schema.safeParse(sample).success);
console.log("zod accepts omitted note:", schema.safeParse({ ...sample, note: undefined }).success);

for (const [name, Ctor] of [["2020-12", Ajv2020], ["draft-04", AjvDraft04]]) {
  const ajv = new Ctor({ strict: false, allErrors: true });
  try {
    const validate = ajv.compile(tool.inputSchema);
    validate(sample);
    console.log(name, "errors:", (validate.errors || []).map((e) => `${e.instancePath} ${e.message}`).join("; "));
  } catch (e) {
    console.log(name, "schema invalid:", e.message);
  }
}

Actual output

{
  "type": "object",
  "properties": {
    "age": {
      "type": "integer",
      "description": "positive int",
      "minimum": 1,
      "exclusiveMinimum": true
    },
    "ratio": {
      "type": "number",
      "description": "positive float",
      "minimum": 1,
      "exclusiveMinimum": true
    },
    "below": {
      "type": "number",
      "description": "less than 10",
      "maximum": 10,
      "exclusiveMaximum": true
    },
    "note": {
      "type": "string",
      "description": "nullable note"
    },
    "mode": {
      "type": "string",
      "description": "literal"
    },
    "id": {
      "type": "string",
      "description": "union"
    },
    "count": {
      "type": "string",
      "description": "even int"
    },
    "tags": {
      "type": "string",
      "description": "record"
    },
    "code": {
      "type": "string",
      "description": "case-insensitive code",
      "pattern": "^[a-z]+$"
    }
  },
  "required": [
    "age",
    "ratio",
    "below",
    "mode",
    "id",
    "count",
    "tags",
    "code"
  ]
}
zod accepts sample: true
zod accepts omitted note: false
2020-12 schema invalid: schema is invalid: data/properties/age/exclusiveMinimum must be number, data/properties/ratio/exclusiveMinimum must be number, data/properties/below/exclusiveMaximum must be number
draft-04 errors: /age must be > 1; /ratio must be > 1; /note must be string; /id must be string; /count must be string; /tags must be string; /code must match pattern "^[a-z]+$"

Expected output

Valid JSON Schema 2020-12 matching the MCP tool schema specification and matching what schema.parse() accepts:

  • age: { "type": "integer", "minimum": 1 } (or "exclusiveMinimum": 0), accepting 1.
  • ratio: { "type": "number", "exclusiveMinimum": 0 }, accepting floats in (0, 1] such as 0.5.
  • below: { "type": "number", "exclusiveMaximum": 10 } (numeric keyword rather than Draft-04 boolean exclusiveMaximum: true).
  • note: { "type": ["string", "null"] } and kept in required (since .nullable() accepts null and rejects undefined).
  • mode: { "type": "string", "const": "fast" }.
  • id: { "anyOf": [{ "type": "string" }, { "type": "number" }] }.
  • count: { "type": "integer" } (unwrapping ZodEffects inner schema).
  • tags: { "type": "object", "additionalProperties": { "type": "string" } }.
  • code: omit pattern when unsupported JS RegExp flags like /i are present (or translate flags), rather than emitting case-sensitive "^[a-z]+$".

In src/tools/BaseTool.ts (extractFieldInfo and getJsonSchemaTypeFromZod), exclusive numeric bounds set boolean exclusiveMinimum/exclusiveMaximum and hasPositive overwrites minimum = 1 without clearing exclusiveMinimum or checking for .int(), ZodNullable unwraps to optional without adding "null", and unhandled Zod types (ZodUnion, ZodRecord, ZodEffects) fall back to "string".

Versions

  • mcp-framework: 0.2.22 and main (ead9c4b)
  • zod: 3.25.76
  • Node.js: v26.13.0

Credit: https://ucsbplse.github.io/

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions