Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(mcp): propagate JSON Schema array constraints to Zod (#57687)

Georgis Andonis01611c2e705e
diff --git a/frontend/src/queries/schema.json b/frontend/src/queries/schema.json
index 6c3915740715..758174651247 100644
--- a/frontend/src/queries/schema.json
+++ b/frontend/src/queries/schema.json
@@ -2426,7 +2426,7 @@
"items": {
"$ref": "#/definitions/AssistantLifecycleSeriesNode"
},
- "maxLength": 1,
+ "maxItems": 1,
"type": "array"
}
},
@@ -3733,7 +3733,7 @@
"items": {
"$ref": "#/definitions/AssistantMultipleBreakdownFilter"
},
- "maxLength": 3,
+ "maxItems": 3,
"type": "array"
}
},
diff --git a/frontend/src/queries/schema/schema-assistant-queries.ts b/frontend/src/queries/schema/schema-assistant-queries.ts
index 57a72ab4dc96..a3629ad6f396 100644
--- a/frontend/src/queries/schema/schema-assistant-queries.ts
+++ b/frontend/src/queries/schema/schema-assistant-queries.ts
@@ -435,7 +435,7 @@ export interface AssistantBreakdownFilter {
export interface AssistantTrendsBreakdownFilter extends AssistantBreakdownFilter {
/**
* Use this field to define breakdowns.
- * @maxLength 3
+ * @maxItems 3
*/
breakdowns: AssistantMultipleBreakdownFilter[]
/**
@@ -1208,7 +1208,7 @@ export interface AssistantLifecycleQuery extends AssistantInsightsQueryBase {
/**
* Event or action to analyze. Lifecycle insights only support a single series.
- * @maxLength 1
+ * @maxItems 1
*/
series: AssistantLifecycleSeriesNode[]
diff --git a/services/mcp/scripts/lib/json-schema-to-zod.ts b/services/mcp/scripts/lib/json-schema-to-zod.ts
index ea734895f457..44460f4c73be 100644
--- a/services/mcp/scripts/lib/json-schema-to-zod.ts
+++ b/services/mcp/scripts/lib/json-schema-to-zod.ts
@@ -26,6 +26,8 @@ export interface JsonSchema {
default?: unknown
description?: string
maxLength?: number
+ minItems?: number
+ maxItems?: number
minimum?: number
maximum?: number
format?: string
@@ -169,7 +171,14 @@ function schemaToZod(schema: JsonSchema, ctx: ConvertContext): string {
// array
if (schema.type === 'array') {
const itemSchema = schema.items ? schemaToZod(schema.items, ctx) : 'z.unknown()'
- return `z.array(${itemSchema})`
+ let zodExpr = `z.array(${itemSchema})`
+ if (typeof schema.minItems === 'number') {
+ zodExpr += `.min(${schema.minItems})`
+ }
+ if (typeof schema.maxItems === 'number') {
+ zodExpr += `.max(${schema.maxItems})`
+ }
+ return zodExpr
}
// integer special ref — use coerce for MCP client compatibility (some clients send numbers as strings)
diff --git a/services/mcp/src/tools/generated/query-wrappers.ts b/services/mcp/src/tools/generated/query-wrappers.ts
index 44719486ebce..69c22760f23b 100644
--- a/services/mcp/src/tools/generated/query-wrappers.ts
+++ b/services/mcp/src/tools/generated/query-wrappers.ts
@@ -47,7 +47,7 @@ const AssistantTrendsBreakdownFilter = z.object({
"When `true`, applies the project's configured path cleaning rules to URL or path breakdown values (e.g. `$pathname`, `$current_url`). Use this whenever the user asks for a breakdown by a URL or path property and there is no specific reason to keep the raw values. The user does not need to provide a regex — path cleaning rules come from the project's settings."
)
.optional(),
- breakdowns: z.array(AssistantMultipleBreakdownFilter).describe('Use this field to define breakdowns.'),
+ breakdowns: z.array(AssistantMultipleBreakdownFilter).max(3).describe('Use this field to define breakdowns.'),
})
const CompareFilter = z.object({
@@ -447,6 +447,7 @@ const AssistantTrendsGroupNode = z.object({
name: z.string().describe('Display name for the combined series.').optional(),
nodes: z
.array(z.union([AssistantTrendsEventsNode, AssistantTrendsActionsNode]))
+ .min(2)
.describe(
"Events and actions combined into the series. Mirror the group's `math*` on each node for UI round-trip; they're ignored at execution time."
),
@@ -717,6 +718,7 @@ const AssistantFunnelsGroupNode = z.object({
name: z.string().describe('Display name for the combined step.').optional(),
nodes: z
.array(z.union([AssistantFunnelsEventsNode, AssistantFunnelsActionsNode]))
+ .min(2)
.describe(
'Events and actions combined into the step. Use per-node `properties` to filter each event; there is no step-wide filter on a grouped step.'
),
@@ -1129,6 +1131,7 @@ const AssistantLifecycleQuery = z.object({
properties: z.array(AssistantPropertyFilter).describe('Property filters for all series').default([]).optional(),
series: z
.array(AssistantLifecycleSeriesNode)
+ .max(1)
.describe('Event or action to analyze. Lifecycle insights only support a single series.'),
})
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/v2/query-funnel.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/v2/query-funnel.json
index 60b416ac3b99..c09bffae2cce 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/v2/query-funnel.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/v2/query-funnel.json
@@ -3073,6 +3073,7 @@
}
]
},
+ "minItems": 2,
"type": "array"
},
"operator": {
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/v2/query-lifecycle.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/v2/query-lifecycle.json
index 5b06aae18ace..f016e9deb47a 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/v2/query-lifecycle.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/v2/query-lifecycle.json
@@ -1661,6 +1661,7 @@
}
]
},
+ "maxItems": 1,
"type": "array"
}
},
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/v2/query-trends-actors.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/v2/query-trends-actors.json
index 4284c2d5ffb3..401215374a2a 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/v2/query-trends-actors.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/v2/query-trends-actors.json
@@ -127,6 +127,7 @@
}
]
},
+ "maxItems": 3,
"type": "array"
}
},
@@ -3746,6 +3747,7 @@
}
]
},
+ "minItems": 2,
"type": "array"
},
"operator": {
diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/v2/query-trends.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/v2/query-trends.json
index c681a2c3ecb6..bad1bffa6f52 100644
--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/v2/query-trends.json
+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/v2/query-trends.json
@@ -86,6 +86,7 @@
}
]
},
+ "maxItems": 3,
"type": "array"
}
},
@@ -3558,6 +3559,7 @@
}
]
},
+ "minItems": 2,
"type": "array"
},
"operator": {
diff --git a/services/mcp/tests/unit/json-schema-to-zod.test.ts b/services/mcp/tests/unit/json-schema-to-zod.test.ts
new file mode 100644
index 000000000000..5d5e19d54e2f
--- /dev/null
+++ b/services/mcp/tests/unit/json-schema-to-zod.test.ts
@@ -0,0 +1,53 @@
+import { describe, expect, it } from 'vitest'
+
+import { generateZodFromSchemaRef } from '../../scripts/lib/json-schema-to-zod'
+
+describe('generateZodFromSchemaRef — array constraints', () => {
+ it.each([
+ { label: 'minItems only', constraints: { minItems: 2 }, expected: 'z.array(z.string()).min(2)' },
+ { label: 'maxItems only', constraints: { maxItems: 5 }, expected: 'z.array(z.string()).max(5)' },
+ {
+ label: 'minItems and maxItems together',
+ constraints: { minItems: 1, maxItems: 3 },
+ expected: 'z.array(z.string()).min(1).max(3)',
+ },
+ ])('propagates $label to the generated Zod array', ({ constraints, expected }) => {
+ const root = {
+ definitions: {
+ Subject: {
+ type: 'object',
+ properties: {
+ items: {
+ type: 'array',
+ items: { type: 'string' },
+ ...constraints,
+ },
+ },
+ },
+ },
+ }
+
+ const out = generateZodFromSchemaRef(root, 'Subject')
+
+ expect(out).toContain(expected)
+ })
+
+ it('emits a bare z.array() when no array constraints are present', () => {
+ const root = {
+ definitions: {
+ Plain: {
+ type: 'object',
+ properties: {
+ items: { type: 'array', items: { type: 'string' } },
+ },
+ },
+ },
+ }
+
+ const out = generateZodFromSchemaRef(root, 'Plain')
+
+ expect(out).toContain('z.array(z.string())')
+ expect(out).not.toMatch(/\.min\(/)
+ expect(out).not.toMatch(/\.max\(/)
+ })
+})