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): show metrics in the experiment-results-get MCP app (#124815)

Anders45a4ba0a57fb
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 37cb0b2da256..d0562faa2cbf 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -6231,11 +6231,11 @@ snapshots:
mcp-apps-experiments--list--light:
hash: v1.k794b7964.451b15f153e10361704c68b9044d5b21972a3228b462a743c70b951d633934af.bdxRqm5NR3HwV2Z0wfvGauANrrvulMQobYyNez3m2bI
mcp-apps-experiments--results--light:
- hash: v1.k794b7964.c71e75f71935551635fd7a397a9931d44b6b697097cf90909df59baf69ae22a9.V5fKzUyc9sF4z1gwfvmKLzifNjUDEL0Z0lMt2DUUz3E
+ hash: v1.k794b7964.802da40733fd0caa503157578bc65e4ffea6e867525cbcdc501d1f11493cfe2b.yW2II1weyB_9pS1VLUvSedcLzX-riHY__1PY4f-pQPM
mcp-apps-experiments--results-fallback--light:
hash: v1.k794b7964.acf3b4c3a462ecfad12108c59a385eef961fc8e1eb11ea0624d80f8b9d551066.3dv3-Kwam5rJplQL4amAeThpXBH5RRnEi62PXnK4uz8
- mcp-apps-experiments--results-legacy--light:
- hash: v1.k794b7964.fb16e88ea3b384dd8f67fe6012124373f57c30e37c09a786aa222664bb7fcb8b.mgcHhsY-mwox6xCW6CCwDrmhgLfHnLRPosHWNigYE_Y
+ mcp-apps-experiments--results-get--light:
+ hash: v1.k794b7964.0428e0158176377706c6c967ad17a53458b07e37bdca63449a44575fc4e7cb66.fhmtKOu9UmdtGsj5NMIfFylEcM2XuPPv7Ab9gDjBSkc
mcp-apps-experiments--running--light:
hash: v1.k794b7964.f7ae67feb87956b98204afeb00f52012a62fc090b18d186d0575110924e5cb69.lWvegECniqeejO8WWNUVam5X0NQTkwxq2iifV-mN5JI
mcp-apps-explain-chart--chapter-beside-the-chart--light:
diff --git a/products/experiments/mcp/apps/ExperimentResultsView.tsx b/products/experiments/mcp/apps/ExperimentResultsView.tsx
index 5315591b92ab..7702946c597a 100644
--- a/products/experiments/mcp/apps/ExperimentResultsView.tsx
+++ b/products/experiments/mcp/apps/ExperimentResultsView.tsx
@@ -1,4 +1,4 @@
-import type { ReactElement } from 'react'
+import { Fragment, type ReactElement } from 'react'
import { DataTable, type DataTableColumn } from '@posthog/mcp-ui'
import { Badge, Card, CardContent, Progress } from '@posthog/quill'
@@ -29,14 +29,11 @@ export interface ExperimentMetricRun {
error_message?: string | null
}
-export interface LegacyMetricResult {
- name?: string
- variant: string
- count?: number
- exposure?: number
- absolute_exposure?: number
- probability?: number
- significant?: boolean
+export interface ExperimentMetricResultRow {
+ index: number
+ metric?: { uuid?: string | null; name?: string | null } | null
+ // null when the metric query failed
+ data?: ExperimentMetricResult | null
}
export interface ExperimentResultsData {
@@ -52,13 +49,14 @@ export interface ExperimentResultsData {
result_source?: string
active_run?: { id?: string } | null
results?: ExperimentMetricRun[]
- // The legacy fields below are produced by the handwritten experiment-results-get tool
+ // The fields below come from the handwritten experiment-results-get tool
// (services/mcp/src/tools/experiments/getResults.ts), which shares this app with
- // experiment-metrics-recalculation-latest. Drop them when that tool is removed.
+ // experiment-metrics-recalculation-latest-retrieve.
experiment?: { id: number; name?: string }
- primaryMetricsResults?: LegacyMetricResult[][]
- secondaryMetricsResults?: LegacyMetricResult[][]
- exposures?: Record<string, number>
+ metrics?: {
+ primary?: { results?: ExperimentMetricResultRow[] }
+ secondary?: { results?: ExperimentMetricResultRow[] }
+ }
_posthogUrl?: string
}
@@ -147,7 +145,9 @@ function significanceCell(row: VariantRow): ReactElement | string {
if (parts.length === 0) {
return '—'
}
- return <div className="flex items-center gap-2 justify-end">{parts}</div>
+ // The table grows to the width of its widest row and scrolls sideways past the card edge,
+ // so stack the parts to keep this column narrow.
+ return <div className="flex flex-col items-end gap-1">{parts}</div>
}
function buildRows(result: ExperimentMetricResult): VariantRow[] {
@@ -181,7 +181,48 @@ function buildRows(result: ExperimentMetricResult): VariantRow[] {
return rows
}
-function MetricCard({ entry, index }: { entry: ExperimentMetricRun; index: number }): ReactElement {
+interface MetricEntry {
+ key: string
+ name: string
+ result: ExperimentMetricResult | null
+ failed: boolean
+ errorMessage: string | null
+}
+
+interface MetricSection {
+ label: string | null
+ entries: MetricEntry[]
+}
+
+function entriesFromResultRows(rows: ExperimentMetricResultRow[]): MetricEntry[] {
+ return rows.map((row) => ({
+ key: String(row.index),
+ name: row.metric?.name || `Metric ${row.index + 1}`,
+ result: row.data ?? null,
+ failed: row.data == null,
+ errorMessage: null,
+ }))
+}
+
+function metricSections(data: ExperimentResultsData): MetricSection[] {
+ if (data.metrics) {
+ const sections: MetricSection[] = [
+ { label: 'Primary metrics', entries: entriesFromResultRows(data.metrics.primary?.results ?? []) },
+ { label: 'Secondary metrics', entries: entriesFromResultRows(data.metrics.secondary?.results ?? []) },
+ ]
+ return sections.filter((section) => section.entries.length > 0)
+ }
+ const entries = (data.results ?? []).map((run, index) => ({
+ key: run.metric_uuid,
+ name: `Metric ${index + 1}`,
+ result: run.result ?? null,
+ failed: Boolean(run.error_message),
+ errorMessage: run.error_message ?? null,
+ }))
+ return entries.length > 0 ? [{ label: null, entries }] : []
+}
+
+function MetricCard({ entry }: { entry: MetricEntry }): ReactElement {
const result = entry.result
const rows = result ? buildRows(result) : []
const isFunnel =
@@ -223,10 +264,12 @@ function MetricCard({ entry, index }: { entry: ExperimentMetricRun; index: numbe
<Card>
<CardContent>
<div className="flex flex-col gap-2">
- <span className="text-sm font-semibold">Metric {index + 1}</span>
- {entry.error_message ? (
+ <span className="text-sm font-semibold">{entry.name}</span>
+ {entry.failed ? (
<span className="text-sm text-muted-foreground">
- This metric failed to calculate: {entry.error_message}
+ {entry.errorMessage
+ ? `This metric failed to calculate: ${entry.errorMessage}`
+ : 'This metric failed to calculate. Recalculate the experiment to try again.'}
</span>
) : (
<DataTable<VariantRow>
@@ -241,95 +284,15 @@ function MetricCard({ entry, index }: { entry: ExperimentMetricRun; index: numbe
)
}
-function LegacyResultsView({ data }: ExperimentResultsViewProps): ReactElement {
- const exposureEntries = Object.entries(data.exposures ?? {}).map(([variant, count]) => ({ variant, count }))
-
- const exposureColumns: DataTableColumn<{ variant: string; count: number }>[] = [
- { key: 'variant', header: 'Variant', sortable: true },
- { key: 'count', header: 'Exposures', align: 'right', sortable: true },
- ]
-
- const metricColumns: DataTableColumn<LegacyMetricResult>[] = [
- { key: 'variant', header: 'Variant', sortable: true },
- { key: 'count', header: 'Count', align: 'right' },
- {
- key: 'probability',
- header: 'Probability',
- align: 'right',
- render: (row) => (row.probability != null ? <ChanceToWinBar value={row.probability} /> : '—'),
- },
- {
- key: 'significant',
- header: 'Significant',
- render: (row) =>
- row.significant != null ? (
- <Badge variant={row.significant ? 'success' : 'default'}>{row.significant ? 'Yes' : 'No'}</Badge>
- ) : (
- '—'
- ),
- },
- ]
-
- const sections: { label: string; rows: LegacyMetricResult[] }[] = [
- { label: 'Primary metrics', rows: (data.primaryMetricsResults ?? []).flat() },
- { label: 'Secondary metrics', rows: (data.secondaryMetricsResults ?? []).flat() },
- ]
-
- return (
- <div className="p-4">
- <div className="flex flex-col gap-3">
- {data.experiment?.name && <span className="text-lg font-semibold">{data.experiment.name}</span>}
-
- {exposureEntries.length > 0 && (
- <Card>
- <CardContent>
- <div className="flex flex-col gap-2">
- <span className="text-sm font-semibold">Exposures</span>
- <DataTable<{ variant: string; count: number }>
- columns={exposureColumns}
- data={exposureEntries}
- emptyMessage="No exposure data"
- />
- </div>
- </CardContent>
- </Card>
- )}
-
- {sections.map(
- ({ label, rows }) =>
- rows.length > 0 && (
- <Card key={label}>
- <CardContent>
- <div className="flex flex-col gap-2">
- <span className="text-sm font-semibold">{label}</span>
- <DataTable<LegacyMetricResult>
- columns={metricColumns}
- data={rows}
- emptyMessage="No metric results"
- />
- </div>
- </CardContent>
- </Card>
- )
- )}
- </div>
- </div>
- )
-}
-
export function ExperimentResultsView({ data }: ExperimentResultsViewProps): ReactElement {
- const results = data.results ?? []
+ const sections = metricSections(data)
const isPreliminary = data.result_source === 'timeseries_fallback'
- if (results.length === 0 && (data.primaryMetricsResults || data.secondaryMetricsResults || data.exposures)) {
- return <LegacyResultsView data={data} />
- }
-
return (
<div className="p-4">
<div className="flex flex-col gap-3">
- <div className="flex items-center gap-2">
- <span className="text-lg font-semibold">Experiment results</span>
+ <div className="flex items-center gap-2 flex-wrap">
+ <span className="text-lg font-semibold">{data.experiment?.name || 'Experiment results'}</span>
{isPreliminary && <Badge variant="warning">Preliminary</Badge>}
{data.status === 'failed' && <Badge variant="destructive">Failed</Badge>}
</div>
@@ -348,16 +311,29 @@ export function ExperimentResultsView({ data }: ExperimentResultsViewProps): Rea
</span>
)}
- {results.length === 0 ? (
+ {sections.length === 0 ? (
<Card>
<CardContent>
<span className="text-sm text-muted-foreground">
- No results yet. Recalculate, or wait for the experiment to collect more data.
+ {data.metrics
+ ? 'This experiment has no metrics. Add a metric to see results.'
+ : 'No results yet. Recalculate, or wait for the experiment to collect more data.'}
</span>
</CardContent>
</Card>
) : (
- results.map((entry, index) => <MetricCard key={entry.metric_uuid} entry={entry} index={index} />)
+ sections.map((section) => (
+ <Fragment key={section.label ?? 'metrics'}>
+ {section.label && (
+ <span className="text-xs font-medium text-muted-foreground uppercase tracking-wide">
+ {section.label}
+ </span>
+ )}
+ {section.entries.map((entry) => (
+ <MetricCard key={entry.key} entry={entry} />
+ ))}
+ </Fragment>
+ ))
)}
</div>
</div>
diff --git a/products/experiments/mcp/apps/Experiments.stories.tsx b/products/experiments/mcp/apps/Experiments.stories.tsx
index f3eff6cb6e61..2324d9880996 100644
--- a/products/experiments/mcp/apps/Experiments.stories.tsx
+++ b/products/experiments/mcp/apps/Experiments.stories.tsx
@@ -259,22 +259,89 @@ const bayesianFallbackResults: ExperimentResultsData = {
_posthogUrl: 'https://us.posthog.com/project/1/experiments/1',
}
-const legacyResults: ExperimentResultsData = {
+const resultsGetResults: ExperimentResultsData = {
experiment: { id: 2, name: 'Pricing page CTA' },
- exposures: { control: 5200, 'test-a': 5100, 'test-b': 4900 },
- primaryMetricsResults: [
- [
- { variant: 'control', count: 312, probability: 0.12, significant: false },
- { variant: 'test-a', count: 410, probability: 0.87, significant: true },
- { variant: 'test-b', count: 295, probability: 0.01, significant: false },
- ],
- ],
- secondaryMetricsResults: [
- [
- { variant: 'control', count: 1050, probability: 0.45 },
- { variant: 'test-a', count: 1120, probability: 0.55 },
- ],
- ],
+ metrics: {
+ primary: {
+ results: [
+ {
+ index: 0,
+ metric: { uuid: 'e1f5a4b3-0000-4000-8000-000000000020', name: 'Checkout started' },
+ data: {
+ baseline: {
+ key: 'control',
+ number_of_samples: 5200,
+ sum: 312,
+ sum_squares: 312,
+ step_counts: [312],
+ },
+ variant_results: [
+ {
+ key: 'test-a',
+ method: 'bayesian',
+ number_of_samples: 5100,
+ sum: 410,
+ sum_squares: 410,
+ step_counts: [410],
+ chance_to_win: 0.997,
+ significant: true,
+ credible_interval: [0.011, 0.049],
+ },
+ {
+ key: 'test-b',
+ method: 'bayesian',
+ number_of_samples: 4900,
+ sum: 295,
+ sum_squares: 295,
+ step_counts: [295],
+ chance_to_win: 0.46,
+ significant: false,
+ credible_interval: [-0.019, 0.021],
+ },
+ ],
+ },
+ },
+ {
+ index: 1,
+ metric: { uuid: 'f2a6b5c4-0000-4000-8000-000000000021', name: 'Plan upgraded' },
+ data: null,
+ },
+ ],
+ },
+ secondary: {
+ results: [
+ {
+ index: 0,
+ metric: { uuid: 'a3b7c6d5-0000-4000-8000-000000000022', name: null },
+ data: {
+ baseline: { key: 'control', number_of_samples: 5200, sum: 14560, sum_squares: 61152 },
+ variant_results: [
+ {
+ key: 'test-a',
+ method: 'bayesian',
+ number_of_samples: 5100,
+ sum: 15300,
+ sum_squares: 68850,
+ chance_to_win: 0.81,
+ significant: false,
+ credible_interval: [-0.012, 0.21],
+ },
+ {
+ key: 'test-b',
+ method: 'bayesian',
+ number_of_samples: 4900,
+ sum: 13230,
+ sum_squares: 55566,
+ chance_to_win: 0.22,
+ significant: false,
+ credible_interval: [-0.11, 0.09],
+ },
+ ],
+ },
+ },
+ ],
+ },
+ },
_posthogUrl: 'https://us.posthog.com/project/1/experiments/2',
}
@@ -288,7 +355,7 @@ export const ResultsFallback: Story = {
name: 'Experiment results (preliminary)',
}
-export const ResultsLegacy: Story = {
- render: () => <ExperimentResultsView data={legacyResults} />,
- name: 'Experiment results (legacy tool)',
+export const ResultsGet: Story = {
+ render: () => <ExperimentResultsView data={resultsGetResults} />,
+ name: 'Experiment results (experiment-results-get)',
}