: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): stop rejecting read-data-schema property lookups (#93052)
posthog[bot]7e9562a786a8
diff --git a/services/mcp/src/schema/tool-inputs.ts b/services/mcp/src/schema/tool-inputs.tsindex edb724346fd5..706ef21940fe 100644--- a/services/mcp/src/schema/tool-inputs.ts+++ b/services/mcp/src/schema/tool-inputs.ts@@ -686,7 +686,7 @@ const ReadActionSamplePropertyValuesQuerySchema = z.object({property_name: z.string().describe('Verified property name of an action.'),})-export const ReadDataSchemaSchema = z.object({+const ReadDataSchemaBodySchema = z.object({query: z.discriminatedUnion('kind', [ReadEventsQuerySchema,@@ -700,6 +700,42 @@ export const ReadDataSchemaSchema = z.object({.describe('The data schema query to execute.'),})+// Person and session properties are read through `entity_properties`, but the two names below+// read so naturally next to `event_properties` that callers keep sending them. Accept them:+// the call says exactly what it wants, and a rejection costs the caller a whole round trip.+const READ_DATA_SCHEMA_KIND_ALIASES: Record<string, { kind: string; entity: string }> = {+ person_properties: { kind: 'entity_properties', entity: 'person' },+ session_properties: { kind: 'entity_properties', entity: 'session' },+}++function isRecord(value: unknown): value is Record<string, unknown> {+ return typeof value === 'object' && value !== null && !Array.isArray(value)+}++/**+ * Accept a call that puts the query fields at the top level (`{kind, event_name}`) instead of+ * under `query`, and an aliased `kind`. Both say exactly what they want, so a rejection only+ * costs the caller a round trip. `query` still wins whenever it is an object, so a well-formed+ * call is never reinterpreted.+ *+ * Same `z.preprocess` seam as `normalizeParamAliases`, and transparent to JSON Schema output+ * for the same reason: the advertised shape stays the wrapped one.+ */+function normalizeReadDataSchemaInput(input: unknown): unknown {+ if (!isRecord(input)) {+ return input+ }+ const { query, ...topLevel } = input+ const source = isRecord(query) ? query : typeof topLevel['kind'] === 'string' ? topLevel : query+ if (!isRecord(source)) {+ return input+ }+ const alias = typeof source['kind'] === 'string' ? READ_DATA_SCHEMA_KIND_ALIASES[source['kind']] : undefined+ return { query: alias ? { ...source, ...alias } : source }+}++export const ReadDataSchemaSchema = z.preprocess(normalizeReadDataSchemaInput, ReadDataSchemaBodySchema)+// Mirrors the Django serializer's `validate` rule so the MCP layer fails fast// instead of forwarding an empty/ambiguous body and waiting for a 400.export function validateDistinctIdPersonIdExclusive(diff --git a/services/mcp/src/templates/sections/cli-data-discovery.md b/services/mcp/src/templates/sections/cli-data-discovery.mdindex 72a4d4e73932..92850aa944a8 100644--- a/services/mcp/src/templates/sections/cli-data-discovery.md+++ b/services/mcp/src/templates/sections/cli-data-discovery.md@@ -5,6 +5,7 @@ per team. If the event isn't in the schema, tell the user instead of querying aAlways run `info read-data-schema` first — the recipes below are common cases, not the full schema.-- Events/Actions: `call read-data-schema {"query": {"kind": "events"/"actions"}}` (paginate with `limit`/`offset` if needed)-- Properties: `call read-data-schema {"query": {"kind": "event_properties", "event_name": "<event>"}}`+- Events: `call read-data-schema {"query": {"kind": "events"}}` (paginate with `limit`/`offset` if needed). Actions come from `actions-get-all` instead.+- Event properties: `call read-data-schema {"query": {"kind": "event_properties", "event_name": "<event>"}}`+- Person, session, and group properties: `call read-data-schema {"query": {"kind": "entity_properties", "entity": "<entity>"}}` (`entity` is `person`, `session`, or a group type name)- Values: `call read-data-schema {"query": {"kind": "event_property_values", "event_name": "<event>", "property_name": "<prop>"}}`diff --git a/services/mcp/src/templates/sections/schema-workflow.md b/services/mcp/src/templates/sections/schema-workflow.mdindex fb725a535559..d0a00afa3e61 100644--- a/services/mcp/src/templates/sections/schema-workflow.md+++ b/services/mcp/src/templates/sections/schema-workflow.md@@ -4,9 +4,11 @@ Verify the data schema before constructing any insight query. Canonical-looking(`$pageview`, `$identify`, `$autocapture`, …) still need confirmation — they can be absent,renamed, or filtered per team.-1. **Discover events** - `read-data-schema` with `kind: events` to find events matching the user's intent.-2. **Discover properties** - `read-data-schema` with `kind: event_properties` (or `person_properties`, `session_properties`).-3. **Verify property values** - `read-data-schema` with `kind: event_property_values` when the value must match (e.g., "US" vs "United States").+Every `read-data-schema` call takes one `query` object, so send the whole payload under `query`.++1. **Discover events** - `{"query": {"kind": "events"}}` to find events matching the user's intent.+2. **Discover properties** - `{"query": {"kind": "event_properties", "event_name": "<event>"}}` for an event, or `{"query": {"kind": "entity_properties", "entity": "<entity>"}}` for a person, session, or group type (use the group type name for a group).+3. **Verify property values** - `{"query": {"kind": "event_property_values", "event_name": "<event>", "property_name": "<property>"}}` when the value must match (e.g., "US" vs "United States").4. **Then construct the query** using the appropriate `query-*` tool.If the required events or properties don't exist, tell the user instead of running an empty query.diff --git a/services/mcp/tests/unit/__snapshots__/instructions/exec-command-reference-claude-chat.txt b/services/mcp/tests/unit/__snapshots__/instructions/exec-command-reference-claude-chat.txtindex 457eac1434fa..3708bb1f3600 100644--- a/services/mcp/tests/unit/__snapshots__/instructions/exec-command-reference-claude-chat.txt+++ b/services/mcp/tests/unit/__snapshots__/instructions/exec-command-reference-claude-chat.txt@@ -46,8 +46,9 @@ per team. If the event isn't in the schema, tell the user instead of querying aAlways run `info read-data-schema` first — the recipes below are common cases, not the full schema.-- Events/Actions: `call read-data-schema {"query": {"kind": "events"/"actions"}}` (paginate with `limit`/`offset` if needed)-- Properties: `call read-data-schema {"query": {"kind": "event_properties", "event_name": "<event>"}}`+- Events: `call read-data-schema {"query": {"kind": "events"}}` (paginate with `limit`/`offset` if needed). Actions come from `actions-get-all` instead.+- Event properties: `call read-data-schema {"query": {"kind": "event_properties", "event_name": "<event>"}}`+- Person, session, and group properties: `call read-data-schema {"query": {"kind": "entity_properties", "entity": "<entity>"}}` (`entity` is `person`, `session`, or a group type name)- Values: `call read-data-schema {"query": {"kind": "event_property_values", "event_name": "<event>", "property_name": "<prop>"}}`**CORRECT usage pattern:**diff --git a/services/mcp/tests/unit/__snapshots__/instructions/exec-command-reference-full.txt b/services/mcp/tests/unit/__snapshots__/instructions/exec-command-reference-full.txtindex c821ee5afd60..54c5d8372ffe 100644--- a/services/mcp/tests/unit/__snapshots__/instructions/exec-command-reference-full.txt+++ b/services/mcp/tests/unit/__snapshots__/instructions/exec-command-reference-full.txt@@ -50,8 +50,9 @@ per team. If the event isn't in the schema, tell the user instead of querying aAlways run `info read-data-schema` first — the recipes below are common cases, not the full schema.-- Events/Actions: `call read-data-schema {"query": {"kind": "events"/"actions"}}` (paginate with `limit`/`offset` if needed)-- Properties: `call read-data-schema {"query": {"kind": "event_properties", "event_name": "<event>"}}`+- Events: `call read-data-schema {"query": {"kind": "events"}}` (paginate with `limit`/`offset` if needed). Actions come from `actions-get-all` instead.+- Event properties: `call read-data-schema {"query": {"kind": "event_properties", "event_name": "<event>"}}`+- Person, session, and group properties: `call read-data-schema {"query": {"kind": "entity_properties", "entity": "<entity>"}}` (`entity` is `person`, `session`, or a group type name)- Values: `call read-data-schema {"query": {"kind": "event_property_values", "event_name": "<event>", "property_name": "<prop>"}}`**CORRECT usage pattern:**@@ -224,9 +225,11 @@ Verify the data schema before constructing any insight query. Canonical-looking(`$pageview`, `$identify`, `$autocapture`, …) still need confirmation — they can be absent,renamed, or filtered per team.-1. **Discover events** - `read-data-schema` with `kind: events` to find events matching the user's intent.-2. **Discover properties** - `read-data-schema` with `kind: event_properties` (or `person_properties`, `session_properties`).-3. **Verify property values** - `read-data-schema` with `kind: event_property_values` when the value must match (e.g., "US" vs "United States").+Every `read-data-schema` call takes one `query` object, so send the whole payload under `query`.++1. **Discover events** - `{"query": {"kind": "events"}}` to find events matching the user's intent.+2. **Discover properties** - `{"query": {"kind": "event_properties", "event_name": "<event>"}}` for an event, or `{"query": {"kind": "entity_properties", "entity": "<entity>"}}` for a person, session, or group type (use the group type name for a group).+3. **Verify property values** - `{"query": {"kind": "event_property_values", "event_name": "<event>", "property_name": "<property>"}}` when the value must match (e.g., "US" vs "United States").4. **Then construct the query** using the appropriate `query-*` tool.If the required events or properties don't exist, tell the user instead of running an empty query.diff --git a/services/mcp/tests/unit/__snapshots__/instructions/tools-instructions.txt b/services/mcp/tests/unit/__snapshots__/instructions/tools-instructions.txtindex ec8f593d427a..c5dc7ba58256 100644--- a/services/mcp/tests/unit/__snapshots__/instructions/tools-instructions.txt+++ b/services/mcp/tests/unit/__snapshots__/instructions/tools-instructions.txt@@ -111,9 +111,11 @@ Verify the data schema before constructing any insight query. Canonical-looking(`$pageview`, `$identify`, `$autocapture`, …) still need confirmation — they can be absent,renamed, or filtered per team.-1. **Discover events** - `read-data-schema` with `kind: events` to find events matching the user's intent.-2. **Discover properties** - `read-data-schema` with `kind: event_properties` (or `person_properties`, `session_properties`).-3. **Verify property values** - `read-data-schema` with `kind: event_property_values` when the value must match (e.g., "US" vs "United States").+Every `read-data-schema` call takes one `query` object, so send the whole payload under `query`.++1. **Discover events** - `{"query": {"kind": "events"}}` to find events matching the user's intent.+2. **Discover properties** - `{"query": {"kind": "event_properties", "event_name": "<event>"}}` for an event, or `{"query": {"kind": "entity_properties", "entity": "<entity>"}}` for a person, session, or group type (use the group type name for a group).+3. **Verify property values** - `{"query": {"kind": "event_property_values", "event_name": "<event>", "property_name": "<property>"}}` when the value must match (e.g., "US" vs "United States").4. **Then construct the query** using the appropriate `query-*` tool.If the required events or properties don't exist, tell the user instead of running an empty query.diff --git a/services/mcp/tests/unit/read-data-schema-input.test.ts b/services/mcp/tests/unit/read-data-schema-input.test.tsnew file mode 100644index 000000000000..9818dbc181e8--- /dev/null+++ b/services/mcp/tests/unit/read-data-schema-input.test.ts@@ -0,0 +1,56 @@+import { describe, expect, it } from 'vitest'++import { toMcpInputSchema } from '@/hono/tool-catalog'+import { ReadDataSchemaSchema } from '@/schema/tool-inputs'++describe('read-data-schema input', () => {+ it.each([+ [+ 'query fields sent at the top level',+ { kind: 'event_properties', event_name: 'purchase' },+ { query: { kind: 'event_properties', event_name: 'purchase' } },+ ],+ [+ 'a top-level events query',+ { kind: 'events', limit: 50 },+ { query: { kind: 'events', limit: 50, offset: 0 } },+ ],+ [+ 'a top-level query alongside a natural-language description',+ { kind: 'events', query: 'which events exist?' },+ { query: { kind: 'events', limit: 500, offset: 0 } },+ ],+ [+ 'the person_properties alias',+ { query: { kind: 'person_properties' } },+ { query: { kind: 'entity_properties', entity: 'person' } },+ ],+ [+ 'the session_properties alias',+ { query: { kind: 'session_properties' } },+ { query: { kind: 'entity_properties', entity: 'session' } },+ ],+ ])('accepts %s', (_name, input, expected) => {+ expect(ReadDataSchemaSchema.parse(input)).toEqual(expected)+ })++ it('keeps a well-formed query even when a stray kind sits at the top level', () => {+ const parsed = ReadDataSchemaSchema.parse({+ kind: 'events',+ query: { kind: 'event_properties', event_name: 'purchase' },+ })++ expect(parsed).toEqual({ query: { kind: 'event_properties', event_name: 'purchase' } })+ })++ it('still rejects a call that names no kind', () => {+ expect(ReadDataSchemaSchema.safeParse({ event_name: 'purchase' }).success).toBe(false)+ })++ it('advertises the wrapped query as the only input shape', () => {+ const schema = toMcpInputSchema(ReadDataSchemaSchema) as Record<string, unknown>++ expect(schema['required']).toEqual(['query'])+ expect(Object.keys(schema['properties'] as Record<string, unknown>)).toEqual(['query'])+ })+})