Skip to content

zod-compat fails to detect standard Zod v4 schemas (missing _zod property), causing metadata loss #1277

@gitenstuff

Description

@gitenstuff

Summary

The current zod-compat.ts implementation fails to correctly identify standard Zod v4 schemas (e.g. from [email protected]) because it relies on checking for an internal _zod property, which is absent in standard builds. This causes Zod v4 schemas to be treated as v3, wrapped in v3 objects, and subsequently lose metadata like description in prompt arguments and additionalProperties: false in tool input schemas.

Reproduction

  1. Install zod@4 alongside the SDK.
  2. Register a prompt with arguments using .describe():
    server.prompt("test", "test prompt", { arg: z.string().describe("My description") }, ...);
  3. Call listPrompts().
  4. Expected: The argument has description: "My description".
  5. Actual: The description field is missing.

Root Cause Analysis

In src/server/zod-compat.ts:

export function isZ4Schema(s: AnySchema): s is z4.$ZodType {
    // Present on Zod 4 (Classic & Mini) schemas; absent on Zod 3
    const schema = s as unknown as ZodV4Internal;
    return !!schema._zod;
}

Standard Zod v4 schemas do not have _zod.

Consequently, objectFromShape falls through to the v3 path:

    if (allV3) return z3rt.object(shape as Record<string, z3.ZodTypeAny>);

It wraps the Zod v4 schema in a Zod v3 object. This mismatch appears to break getSchemaDescription or the schema normalization process down the line.

Context

This is relevant to #1251 (Migrate to Zod 4).

Metadata

Metadata

Labels

P1Significant bug affecting many users, highly requested featurebugSomething isn't workingready for workEnough information for someone to start working on

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions