Skip to content

Optional fields don't work in prompt arguments with Zod 4 #1198

@filip-bartuska-ipf

Description

@filip-bartuska-ipf

Describe the bug
Because of the way prompt arguments are constructed from Zod 4 schemas, there is currently no way to mark them as optional.

To Reproduce
Steps to reproduce the behavior:

  1. Create an mcp server
  2. Register a prompt and add a Zod 4 defined optional field to it's argsSchema
  3. When testing the response from the server, it will be marked as required: true

I will also link a branch/MR with a commit testing this behaviour in unit test.

Expected behavior
Fields defined as optional should not be presented as required on the MCP interface.

Additional context
I've looked into the code causing this, I'm pretty sure it's src/server/zod-compat.ts:241. There, specifically for the v4 branch, typeName is checked on the _def object, however Zod v4 has no typeName property. It should probably be replaced with .type === "optional" (and the ZodV4Internal type should be adjusted as well).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions