Skip to content

[v2] Unnecessary zod schema conversion #3132

Open

Description

@phawxby

What happened?

v2.3.0 introduced lazy tool-schema conversion, reducing the cost of per-request MCP servers.

However, passing the output of z.toJSONSchema() directly to registerTool() still results in conversion.

With Zod, the converted object retains ~standard property. Its JSON Schema converters reference the original Zod schema, so the SDK invokes those converters and regenerates JSON Schema rather than reusing the pre-converted result.

What did you expect?

Objects detected as JSON schemas to be used directly and not re-converted. However I can also see the case for this being a fix in zod.

Code to reproduce

const schema = z.object({}).toJSONSchema();

const server = new McpServer(
  { name: 'foo', version: '1.0.0' }
);

for (let i = 0; i < 1000; i++) {
  const name = `tool-${i}`;

  server.registerTool(
    name,
    { inputSchema: schema },
    async () => ({ content: [] })
  );

  server.toolInputSchemaJson(name);
}

SDK version

2.3.0

Area

Server

Activity

  1. added
    v2
    on Oct 30, 2026
  2. media-ctrl commented on Nov 3, 2026

    @media-ctrl
  3. SalyyS1 commented on Nov 12, 2026

    @SalyyS1

    One subtlety if the SDK distinguishes raw Zod v4 schemas from pre-converted z.toJSONSchema() objects:

    In Zod v4, both z.object({}) and z.object({}).toJSONSchema() have schema.type === "object" and '~standard' in schema === true! So checking 'type' in schema && schema.type === 'object' is not sufficient on its own to tell a plain JSON Schema apart from a Zod v4 schema instance.

    Instead, you can distinguish them reliably by checking for Zod's internal marker ('_zod' in schema or '_def' in schema) or checking whether '~standard' is only a non-enumerable own property attached to a plain JSON Schema object (Object.getPrototypeOf(schema) === Object.prototype && !('_zod' in schema)):

    function isAlreadyJsonSchema(schema: unknown): boolean {
      return (
        typeof schema === 'object' &&
        schema !== null &&
        Object.getPrototypeOf(schema) === Object.prototype &&
        !('_zod' in schema) &&
        !('_def' in schema) &&
        ('$schema' in schema || 'properties' in schema || 'type' in schema)
      );
    }

    As a zero-cost workaround for callers on v2.3.0 today, shallow-spreading the result (registerTool(name, { inputSchema: { ...z.object({}).toJSONSchema() } }, cb)) drops the non-enumerable ~standard property in O(1) so the SDK skips re-conversion.

    (Also worth noting for z.toJSONSchema() users: Zod v4's JSON Schema output includes "$schema": "https://json-schema.org/draft/2020-12/schema", "additionalProperties": false, and #/$defs/... pointers on every tool. If you are exposing a large tool catalog to LLM clients, running mcp-slim in front of the server strips redundant $schema/title metadata and inlines $defs/$ref pointers, cutting tools/list token size by ~49%.)

  4. PromptShield21 commented on Nov 14, 2026

    @PromptShield21

    A use case and numbers for this, from a server with 130 tools (SDK 1.32.2 today, zod 4.6.7).

    z.uuid() lists as "format": "uuid" plus a 190-byte pattern. In our tools/list answer that pattern appears 85 times. Removing it from the listing takes the answer from 103,656 to 88,441 bytes (14.7%). Validation does not change, because the server still parses arguments with the original Zod schema.

    Zod already has the switch for this:

    z.toJSONSchema(schema, {
      override: ctx => { if (ctx.jsonSchema.format === 'uuid') delete ctx.jsonSchema.pattern; },
    });

    The SDK calls toJSONSchema with fixed options (target, io), so a server cannot pass override or reused. That leaves two routes:

    1. Convert the schema ourselves with those options and pass the result to registerTool. That is the path this issue is about: it only pays off once a pre-converted schema is used as given and not converted again.
    2. An option on the server or on registerTool that is passed through to the conversion, for example jsonSchema: { override, reused }.

    Either one would let us drop a workaround. On v1 we wrap the tools/list handler that McpServer.setToolRequestHandlers registers and post-process each inputSchema, which depends on where the SDK registers that handler.

    So +1 for fixing the re-conversion, and if route 2 is of interest I can open a separate issue for it.

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

    v2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions