Skip to content

Commit 671e40a

Browse files
author
MauryaQbit
committed
fix(schema-to-json): handle Zod 4 dates when converting to JSON Schema
1 parent 3f4ac50 commit 671e40a

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎.changeset/tidy-dates-shout.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/schema-to-json": patch
3+
---
4+
5+
Fixed MCP `tools/list` failing with "Date cannot be represented in JSON Schema" when a tool used `z.date()`. Date fields now show up as date-time strings instead of breaking the whole tool list.

‎packages/schema-to-json/src/index.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,17 @@ function convertZod4Schema(schema: any, options?: ConversionOptions): JSONSchema
156156
target: "draft-7",
157157
io: "output",
158158
reused: useReferences ? "ref" : "inline",
159+
// Dates (and other unrepresentable types) throw by default, which fails the
160+
// whole conversion. Fall back to `any` and handle dates in override instead.
161+
unrepresentable: "any",
159162
override: ({ zodSchema, jsonSchema }) => {
160163
const def = zodSchema._zod.def;
164+
if (def.type === "date") {
165+
jsonSchema.type = "string";
166+
jsonSchema.format = "date-time";
167+
return;
168+
}
169+
161170
if (def.type === "undefined") {
162171
throw new Error("Undefined cannot be represented in JSON Schema");
163172
}

‎packages/schema-to-json/tests/index.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ describe("schemaToJsonSchema", () => {
5252
});
5353
});
5454

55+
it("should convert a Zod 4 object schema with a date field", () => {
56+
const schema = z4.object({
57+
when: z4.date(),
58+
});
59+
60+
const result = schemaToJsonSchema(schema);
61+
62+
expect(result).toBeDefined();
63+
expect(result?.jsonSchema).toMatchObject({
64+
type: "object",
65+
properties: {
66+
when: { type: "string", format: "date-time" },
67+
},
68+
required: ["when"],
69+
});
70+
});
71+
5572
it("should convert a Zod schema with optional fields", () => {
5673
const schema = z3.object({
5774
id: z3.string(),

0 commit comments

Comments
 (0)