: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(mcp): document insight description limit and clarify rejection (#72814)
Thomas Obermüller1bdae9587c88
diff --git a/products/product_analytics/mcp/tools.yaml b/products/product_analytics/mcp/tools.yamlindex 34dc4cdc8423..e1cc9302bda2 100644--- a/products/product_analytics/mcp/tools.yaml+++ b/products/product_analytics/mcp/tools.yaml@@ -100,6 +100,9 @@ tools:description: >Dashboard IDs this insight should belong to. This is a full replacement — always include allexisting dashboard IDs when adding a new one.+ description:+ description: |+ Human-readable summary of what the insight shows. Max 400 characters (longer values are rejected).response:exclude:- result@@ -201,6 +204,9 @@ tools:description: >Dashboard IDs this insight should belong to. This is a full replacement — always include allexisting dashboard IDs when adding a new one.+ description:+ description: |+ Human-readable summary of what the insight shows. Max 400 characters (longer values are rejected).response:exclude:- resultdiff --git a/services/mcp/src/tools/exec.ts b/services/mcp/src/tools/exec.tsindex 1778203715a9..3e096a55aa0b 100644--- a/services/mcp/src/tools/exec.ts+++ b/services/mcp/src/tools/exec.ts@@ -193,6 +193,15 @@ export function formatInputValidationError(toolName: string, error: z.ZodError):if (issue.code === 'unrecognized_keys') {return `unexpected ${issue.keys.length > 1 ? 'properties' : 'property'}: ${issue.keys.join(', ')}`}+ // A too-long string names the limit and the input's actual length so the+ // agent knows how much to trim (zod's default names only the limit).+ // Surfaces the LENGTH only, never the value: the message is returned to+ // the caller and recorded as the analytics error_message. `issue.input`+ // is only present under `reportInput: true`; without it, or for+ // non-string origins, fall through to zod's limit-naming default.+ if (issue.code === 'too_big' && issue.origin === 'string' && typeof issue.input === 'string') {+ return `parameter "${path}" is too long: ${issue.input.length} characters (max ${issue.maximum})`+ }return path ? `parameter "${path}": ${issue.message}` : issue.message})return `Invalid input for "${toolName}": ${[...new Set(parts)].join('; ')}`diff --git a/services/mcp/src/tools/generated/product_analytics.ts b/services/mcp/src/tools/generated/product_analytics.tsindex d53d97b07cb7..f7837f37c25c 100644--- a/services/mcp/src/tools/generated/product_analytics.ts+++ b/services/mcp/src/tools/generated/product_analytics.ts@@ -201,6 +201,9 @@ const InsightCreateSchema = InsightsCreateBody.omit({dashboards: InsightsCreateBody.shape['dashboards'].describe('Dashboard IDs this insight should belong to. This is a full replacement — always include all existing dashboard IDs when adding a new one.'),+ description: InsightsCreateBody.shape['description'].describe(+ 'Human-readable summary of what the insight shows. Max 400 characters (longer values are rejected).'+ ),})const insightCreate = (): ToolBase<typeof InsightCreateSchema, WithPostHogUrl<Schemas.Insight>> => ({@@ -323,6 +326,9 @@ const InsightUpdateSchema = z.preprocess(dashboards: InsightsPartialUpdateBody.shape['dashboards'].describe('Dashboard IDs this insight should belong to. This is a full replacement — always include all existing dashboard IDs when adding a new one.'),+ description: InsightsPartialUpdateBody.shape['description'].describe(+ 'Human-readable summary of what the insight shows. Max 400 characters (longer values are rejected).'+ ),}))diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-create.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-create.jsonindex fc634ea38613..d6da833dfd6a 100644--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-create.json+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-create.json@@ -17,7 +17,8 @@{"type": "null"}- ]+ ],+ "description": "Human-readable summary of what the insight shows. Max 400 characters (longer values are rejected)."},"favorited": {"type": "boolean"diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-update.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-update.jsonindex d56fb0e3d126..4b257e9f6697 100644--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-update.json+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/insight-update.json@@ -17,7 +17,8 @@{"type": "null"}- ]+ ],+ "description": "Human-readable summary of what the insight shows. Max 400 characters (longer values are rejected)."},"favorited": {"type": "boolean"diff --git a/services/mcp/tests/unit/exec.test.ts b/services/mcp/tests/unit/exec.test.tsindex 966f1e7fc345..27c3a54f49d2 100644--- a/services/mcp/tests/unit/exec.test.ts+++ b/services/mcp/tests/unit/exec.test.ts@@ -15,6 +15,7 @@ import {describeValidationError,type ExecInnerCallProperties,type ExecToolOptions,+ formatInputValidationError,parseExecCallInnerToolName,} from '@/tools/exec'import { ExecHelpCatalog } from '@/tools/exec-help'@@ -514,10 +515,17 @@ describe('exec tool', () => {input: '{"actionId":277664}',expected: /missing required parameter: id/,},+ {+ // Requires `reportInput: true` at the dispatch site — without it the+ // message degrades to zod's default, which omits the actual length.+ case: 'a string over its max length, naming actual length and limit',+ input: `{"id":1,"description":"${'x'.repeat(401)}"}`,+ expected: /parameter "description" is too long: 401 characters \(max 400\)/,+ },])('rejects a call with $case', async ({ input, expected }) => {const tool = makeMockTool({name: 'action-get',- schema: z.object({ id: z.number() }),+ schema: z.object({ id: z.number(), description: z.string().max(400).optional() }),handler: async (_ctx, params) => params,})const exec = createExec([tool])@@ -1349,4 +1357,32 @@ describe('exec tool', () => {expect(JSON.stringify(detail)).not.toContain('not-a-number')})})++ describe('formatInputValidationError', () => {+ it('names actual length and limit for an over-long string without echoing the value', () => {+ const schema = z.object({ description: z.string().max(5) })+ const result = schema.safeParse({ description: 'secret-value' }, { reportInput: true })+ expect(result.success).toBe(false)++ const message = formatInputValidationError('insight-create', result.error!)++ expect(message).toBe(+ 'Invalid input for "insight-create": parameter "description" is too long: 12 characters (max 5)'+ )+ // The message flows into the tool response and analytics error_message,+ // so it may carry the input's length but never the value itself.+ expect(message).not.toContain('secret-value')+ })++ it('falls back to the zod default for a non-string too_big instead of a bogus length', () => {+ const schema = z.object({ tags: z.array(z.string()).max(2) })+ const result = schema.safeParse({ tags: ['a', 'b', 'c'] }, { reportInput: true })+ expect(result.success).toBe(false)++ const message = formatInputValidationError('insight-create', result.error!)++ expect(message).toMatch(/parameter "tags": /)+ expect(message).not.toContain('undefined')+ })+ })})diff --git a/services/mcp/tests/unit/insight-description-limit.test.ts b/services/mcp/tests/unit/insight-description-limit.test.tsnew file mode 100644index 000000000000..823b683cd95c--- /dev/null+++ b/services/mcp/tests/unit/insight-description-limit.test.ts@@ -0,0 +1,39 @@+import { describe, expect, it } from 'vitest'+import { z } from 'zod'++import { formatInputValidationError } from '@/tools/exec'+import { GENERATED_TOOLS } from '@/tools/generated/product_analytics'++// Minimal valid input per tool: create requires `query`, update requires `id`.+const cases = [+ { tool: 'insight-create', base: { query: { source: { kind: 'TrendsQuery' } } } },+ { tool: 'insight-update', base: { id: 1 } },+]++// The Insight model caps `description` at 400 characters; these tests keep the+// advertised limit, the enforced limit, and the rejection message in lockstep.+describe('insight description 400-character limit', () => {+ it.each(cases)('$tool accepts a description of exactly 400 characters', ({ tool, base }) => {+ const schema = GENERATED_TOOLS[tool]!().schema++ expect(schema.safeParse({ ...base, description: 'a'.repeat(400) }).success).toBe(true)+ })++ it.each(cases)('$tool rejects a 401-character description, naming actual length and limit', ({ tool, base }) => {+ const { name, schema } = GENERATED_TOOLS[tool]!()+ const result = schema.safeParse({ ...base, description: 'a'.repeat(401) }, { reportInput: true })+ expect(result.success).toBe(false)++ const message = formatInputValidationError(name, result.error!)++ expect(message).toContain('parameter "description" is too long: 401 characters (max 400)')+ })++ it.each(cases)('$tool advertises the 400-character limit on the description param', ({ tool }) => {+ const inputSchema = z.toJSONSchema(GENERATED_TOOLS[tool]!().schema, { io: 'input' }) as {+ properties: Record<string, { description?: string }>+ }++ expect(inputSchema.properties['description']?.description).toMatch(/[Mm]ax 400 characters/)+ })+})