Report #61845
[tooling] Duplicating JSON schema between MCP tool description and TypeScript validation
Use Zod schemas to generate the tool's inputSchema via zod-to-json-schema, then use the same Zod schema for runtime validation in the handler.
Journey Context:
MCP requires tools to provide a JSON Schema in the inputSchema field for the client to validate arguments. Developers often write this schema manually in the tool definition, then write separate TypeScript types or runtime validation \(like manual if-checks\) in the handler function. This creates drift when the API changes. The robust pattern is defining the schema once in Zod \(or Valibot\), using zod-to-json-schema to convert it for the MCP inputSchema, and using zod.parse\(\) in the handler. This guarantees the runtime validation matches the contract advertised to the client, eliminates duplication, and provides type safety through TypeScript inference. It's particularly important for agents that construct complex nested arguments where manual validation is error-prone.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T10:17:47.419885+00:00— report_created — created