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(experiments): accept action-based exposure in MCP results schema (#66304)

Anderse716e7352f0d
diff --git a/services/mcp/src/schema/experiments.ts b/services/mcp/src/schema/experiments.ts
index ada8a92ef6dd..278eca41a839 100644
--- a/services/mcp/src/schema/experiments.ts
+++ b/services/mcp/src/schema/experiments.ts
@@ -21,9 +21,24 @@ const ExperimentEventExposureConfigSchema = z.object({
properties: z.array(z.any()),
})
+// Action-based exposure: the experiment counts a user as exposed when they match a
+// PostHog action (kind 'ActionsNode', identified by numeric `id`) rather than a custom
+// event. The backend resolves the action by id, so id + properties round-trip faithfully
+// into the ExperimentExposureQuery.
+const ExperimentActionExposureConfigSchema = z.object({
+ kind: z.literal('ActionsNode'),
+ id: z.number(),
+ properties: z.array(z.any()).optional(),
+})
+
+const ExperimentExposureConfigSchema = z.union([
+ ExperimentEventExposureConfigSchema,
+ ExperimentActionExposureConfigSchema,
+])
+
const ExperimentExposureCriteriaSchema = z.object({
filterTestAccounts: z.boolean().optional(),
- exposure_config: ExperimentEventExposureConfigSchema.optional(),
+ exposure_config: ExperimentExposureConfigSchema.optional(),
multiple_variant_handling: z.enum(['exclude', 'first_seen']).optional(),
})
diff --git a/services/mcp/tests/unit/experiments-results-transform.test.ts b/services/mcp/tests/unit/experiments-results-transform.test.ts
index a0d9be6c2a32..073e8f036bd0 100644
--- a/services/mcp/tests/unit/experiments-results-transform.test.ts
+++ b/services/mcp/tests/unit/experiments-results-transform.test.ts
@@ -19,7 +19,12 @@
import { describe, expect, it } from 'vitest'
import type { Experiment } from '@/schema/experiments'
-import { buildMetricEntries, ExperimentSchema, transformExperimentResults } from '@/schema/experiments'
+import {
+ buildMetricEntries,
+ ExperimentExposureQuerySchema,
+ ExperimentSchema,
+ transformExperimentResults,
+} from '@/schema/experiments'
const baseExposures = {
kind: 'ExperimentExposureQuery' as const,
@@ -399,3 +404,56 @@ describe('transformExperimentResults', () => {
expect(bdVariants[0]?.step_counts).toEqual([55, 27])
})
})
+
+describe('exposure_criteria parsing — action-based exposure', () => {
+ // Regression: experiment-results-get threw a ZodError ("expected
+ // ExperimentEventExposureConfig") whenever the experiment's exposure was gated
+ // on an action (kind 'ActionsNode') instead of a custom event, because the
+ // shared exposure schema only modeled the event variant. The experiment GET and
+ // the ExperimentExposureQuery built from it (client.ts) both run through this
+ // schema, so a narrow union broke results for every action-exposure experiment.
+ const actionExposureCriteria = {
+ filterTestAccounts: true,
+ exposure_config: { kind: 'ActionsNode' as const, id: 3, properties: [] },
+ }
+
+ it('parses an experiment whose exposure_config is an ActionsNode and preserves kind + id', () => {
+ const experiment = makeExperiment({ exposure_criteria: actionExposureCriteria })
+ expect(experiment.exposure_criteria?.exposure_config).toEqual({
+ kind: 'ActionsNode',
+ id: 3,
+ properties: [],
+ })
+ })
+
+ it('still parses the event-based exposure_config variant', () => {
+ const experiment = makeExperiment({
+ exposure_criteria: {
+ filterTestAccounts: true,
+ exposure_config: {
+ kind: 'ExperimentEventExposureConfig' as const,
+ event: '$pageview',
+ properties: [],
+ },
+ },
+ })
+ expect(experiment.exposure_criteria?.exposure_config).toEqual({
+ kind: 'ExperimentEventExposureConfig',
+ event: '$pageview',
+ properties: [],
+ })
+ })
+
+ it('accepts an ExperimentExposureQuery carrying action-based exposure criteria (the client.ts throw site)', () => {
+ // client.ts builds this query from experiment.exposure_criteria and parses it
+ // before POSTing to /query/. This is the exact call that surfaced the bug.
+ const parsed = ExperimentExposureQuerySchema.parse({
+ kind: 'ExperimentExposureQuery',
+ experiment_id: 9,
+ experiment_name: 'New upload button',
+ exposure_criteria: actionExposureCriteria,
+ start_date: '2026-01-01T00:00:00Z',
+ })
+ expect(parsed.exposure_criteria?.exposure_config).toEqual({ kind: 'ActionsNode', id: 3, properties: [] })
+ })
+})