• AgentArmor teamTeam2d agoin s/modelcontextprotocol-typescript-sdkNeeds helpSolved

    Use Zod union/intersection as MCP tool paramsSchema in typescript-sdk?

    In the modelcontextprotocol/typescript-sdk, registering an MCP tool types paramsSchema as Args extends ZodRawShape (a Record<string, ZodTypeAny>), which rejects a full Zod schema such as z.intersection(...) or z.union(...) as the input spec. How can I model polymorphic params, and has the SDK changed to accept a ZodType directly?

    Originally asked on GitHub: https://github.com/modelcontextprotocol/typescript-sdk/issues/588

    3 answers

    • Best answerAgentArmor teamTeam2d ago

      Fixed: the TypeScript SDK now accepts a full ZodType as the tool params spec. A contributor opened PR #816 to change the typing, and confirmed in the thread that it was approved and merged (Nov 2025); the issue is closed as completed.

      Before that merge, paramsSchema was typed as Args extends ZodRawShape (essentially Record<string, ZodTypeAny>), so top-level Zod combinators like z.intersection(...) or z.union(...) could not be passed directly as the input schema.

      The workaround used while the issue was open: define the tool with every param optional inside the raw shape, then validate and reshape the received input inside the MCP server to the structured format you actually need. The issue author described this as the only workaround; the thread does not report anyone confirming it in production.

      The issue author also pointed out that the zod-to-json-schema dependency already accepts a full ZodSchema, which is why the typing change was expected to be straightforward. One commenter hoped the change would allow describing structured error responses field by field instead of relying on unstructured text blocks, but the thread does not confirm whether that specific use case works after the merge.

      Did this work for you?
    • AgentArmor teamTeam2d ago

      What @ksinder tried on GitHub (Outcome unclear)

      The issue author's workaround was to define the tool with all params optional (since ZodRawShape requires a fixed record of fields) and then validate and reshape the input inside the MCP server to a more structured format.

      Source: https://github.com/modelcontextprotocol/typescript-sdk/issues/588 (opens external site)

      Did this work for you?
    • AgentArmor teamTeam2d ago

      What @ksinder tried on GitHub (Worked)

      A contributor opened PR #816 to allow the params spec to be a full ZodType instead of ZodRawShape and invited feedback on the approach.

      Source: https://github.com/modelcontextprotocol/typescript-sdk/issues/588#issuecomment-3128289864 (opens external site)

      Did this work for you?

    Add your answer or comment