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.

feat(mcp): add lifecycle visualizer (#58004)

Georgis Andonis4c150b1c8eba
diff --git a/services/mcp/src/tools/query-wrapper-factory.ts b/services/mcp/src/tools/query-wrapper-factory.ts
index 3fbb07166803..dd8420381c32 100644
--- a/services/mcp/src/tools/query-wrapper-factory.ts
+++ b/services/mcp/src/tools/query-wrapper-factory.ts
@@ -77,7 +77,11 @@ export function createQueryWrapper<T extends ZodObjectAny>(config: QueryWrapperC
const data = await context.api.query({ projectId }).runQuery({ query })
const shouldSurfaceFormatted = effectiveOutputFormat !== 'json' && data.formatted_results
+ // Include `query` in the payload so UI apps (TrendsVisualizer, LifecycleVisualizer)
+ // can honor query-level filters like `lifecycleFilter.toggledLifecycles` and
+ // `trendsFilter.display`.
return {
+ query,
results: data.results,
_posthogUrl: buildInsightUrl('InsightVizNode', query, baseUrl, config.urlPrefix),
...(shouldSurfaceFormatted ? { [POSTHOG_FORMATTED_RESULTS_OVERRIDE_KEY]: data.formatted_results } : {}),
diff --git a/services/mcp/src/ui-apps/components/Component.tsx b/services/mcp/src/ui-apps/components/Component.tsx
index c45ac16805b6..d3bfbbd02227 100644
--- a/services/mcp/src/ui-apps/components/Component.tsx
+++ b/services/mcp/src/ui-apps/components/Component.tsx
@@ -3,11 +3,40 @@ import type { CSSProperties, ReactElement } from 'react'
import { EmptyState } from '@posthog/mosaic'
import { FunnelVisualizer } from './FunnelVisualizer'
+import { LifecycleVisualizer } from './LifecycleVisualizer'
import { TableVisualizer } from './TableVisualizer'
import { TrendsVisualizer } from './TrendsVisualizer'
-import type { FunnelResult, FunnelsQuery, HogQLResult, TrendsQuery, TrendsResult } from './types'
+import type {
+ FunnelResult,
+ FunnelsQuery,
+ HogQLResult,
+ LifecycleQuery,
+ LifecycleResult,
+ TrendsQuery,
+ TrendsResult,
+} from './types'
+
+type VisualizationType = 'trends' | 'funnel' | 'lifecycle' | 'table'
-type VisualizationType = 'trends' | 'funnel' | 'table'
+/**
+ * Lifecycle results share the trends shape but each item carries a `status` field
+ * (`new` / `returning` / `resurrecting` / `dormant`), and dormant counts come back
+ * negated from the backend so they render below zero.
+ */
+function isLifecycleResult(results: unknown): results is LifecycleResult {
+ if (!Array.isArray(results) || results.length === 0) {
+ return false
+ }
+ const first = results[0] as Record<string, unknown>
+ if (typeof first !== 'object' || first === null) {
+ return false
+ }
+ const status = first.status
+ return (
+ typeof status === 'string' &&
+ (status === 'new' || status === 'returning' || status === 'resurrecting' || status === 'dormant')
+ )
+}
/**
* Check if results look like TrendsResult (array of items with data/labels arrays).
@@ -78,6 +107,10 @@ function inferVisualizationType(data: unknown): VisualizationType | null {
if (isHogQLResult(results)) {
return 'table'
}
+ // Lifecycle must come before trends — its rows pass `isTrendsResult` too.
+ if (isLifecycleResult(results)) {
+ return 'lifecycle'
+ }
if (isTrendsResult(results)) {
return 'trends'
}
@@ -87,6 +120,9 @@ function inferVisualizationType(data: unknown): VisualizationType | null {
// Infer from query kind as fallback
const query = d.query as Record<string, unknown> | undefined
+ if (query?.kind === 'LifecycleQuery') {
+ return 'lifecycle'
+ }
if (query?.kind === 'TrendsQuery') {
return 'trends'
}
@@ -102,8 +138,8 @@ function inferVisualizationType(data: unknown): VisualizationType | null {
/** Data payload from MCP tools */
interface DataPayload {
- query?: TrendsQuery | FunnelsQuery | Record<string, unknown>
- results: TrendsResult | FunnelResult | HogQLResult
+ query?: TrendsQuery | FunnelsQuery | LifecycleQuery | Record<string, unknown>
+ results: TrendsResult | FunnelResult | LifecycleResult | HogQLResult
_posthogUrl?: string
}
@@ -155,6 +191,14 @@ export function Component({ data }: ComponentProps): ReactElement {
<FunnelVisualizer query={payload.query as FunnelsQuery} results={payload.results as FunnelResult} />
)
+ case 'lifecycle':
+ return (
+ <LifecycleVisualizer
+ query={payload.query as LifecycleQuery}
+ results={payload.results as LifecycleResult}
+ />
+ )
+
case 'table':
return <TableVisualizer results={payload.results as HogQLResult} />
@@ -173,6 +217,8 @@ export function Component({ data }: ComponentProps): ReactElement {
return 'Trends'
case 'funnel':
return 'Funnel'
+ case 'lifecycle':
+ return 'Lifecycle'
case 'table':
return 'Query results'
default:
diff --git a/services/mcp/src/ui-apps/components/LifecycleVisualizer.tsx b/services/mcp/src/ui-apps/components/LifecycleVisualizer.tsx
new file mode 100644
index 000000000000..01cb0df13dd0
--- /dev/null
+++ b/services/mcp/src/ui-apps/components/LifecycleVisualizer.tsx
@@ -0,0 +1,271 @@
+import { type ReactElement } from 'react'
+
+import { EmptyState } from '@posthog/mosaic'
+
+import type { LifecycleResultItem, LifecycleStatus, LifecycleVisualizerProps } from './types'
+import { formatDate, formatNumber } from './utils'
+
+const CHART_HEIGHT = 320
+const CHART_WIDTH = 600
+const PADDING = { top: 24, right: 24, bottom: 48, left: 56 }
+
+// Conventional lifecycle bucket colors — mirrors --color-lifecycle-* in frontend/src/styles/base.scss.
+const LIFECYCLE_COLORS: Record<LifecycleStatus, string> = {
+ new: '#1d4aff',
+ returning: '#388600',
+ resurrecting: '#a56eff',
+ dormant: '#db3707',
+}
+
+// Stack order — positive buckets stack upward, dormant goes below zero.
+const POSITIVE_STATUSES: LifecycleStatus[] = ['new', 'returning', 'resurrecting']
+const NEGATIVE_STATUSES: LifecycleStatus[] = ['dormant']
+const ALL_STATUSES: LifecycleStatus[] = [...POSITIVE_STATUSES, ...NEGATIVE_STATUSES]
+
+interface SeriesByStatus {
+ status: LifecycleStatus
+ label: string
+ data: number[]
+}
+
+function groupByStatus(results: LifecycleResultItem[]): {
+ byStatus: Map<LifecycleStatus, SeriesByStatus>
+ labels: string[]
+} {
+ const labels = results[0]?.days || results[0]?.labels || []
+ const byStatus = new Map<LifecycleStatus, SeriesByStatus>()
+
+ for (const item of results) {
+ if (!item.status || !ALL_STATUSES.includes(item.status)) {
+ continue
+ }
+ // Multiple series can share the same status (e.g. when grouping by event).
+ // Sum element-wise.
+ const existing = byStatus.get(item.status)
+ const data = item.data ?? []
+ if (existing) {
+ for (let i = 0; i < data.length; i++) {
+ existing.data[i] = (existing.data[i] ?? 0) + (data[i] ?? 0)
+ }
+ } else {
+ byStatus.set(item.status, {
+ status: item.status,
+ label: item.status,
+ data: [...data],
+ })
+ }
+ }
+
+ return { byStatus, labels }
+}
+
+function computeBounds(
+ byStatus: Map<LifecycleStatus, SeriesByStatus>,
+ dataLength: number,
+ isVisible: (status: LifecycleStatus) => boolean
+): { max: number; min: number } {
+ let max = 0
+ let min = 0
+ for (let i = 0; i < dataLength; i++) {
+ let positiveStack = 0
+ let negativeStack = 0
+ for (const status of POSITIVE_STATUSES) {
+ if (!isVisible(status)) {
+ continue
+ }
+ const value = byStatus.get(status)?.data[i] ?? 0
+ if (value > 0) {
+ positiveStack += value
+ }
+ }
+ for (const status of NEGATIVE_STATUSES) {
+ if (!isVisible(status)) {
+ continue
+ }
+ const value = byStatus.get(status)?.data[i] ?? 0
+ if (value < 0) {
+ negativeStack += value
+ }
+ }
+ if (positiveStack > max) {
+ max = positiveStack
+ }
+ if (negativeStack < min) {
+ min = negativeStack
+ }
+ }
+ return { max, min }
+}
+
+export function LifecycleVisualizer({ query, results }: LifecycleVisualizerProps): ReactElement {
+ if (!results || results.length === 0) {
+ return <EmptyState icon="chart" description="No data available" />
+ }
+
+ const { byStatus, labels } = groupByStatus(results)
+ const dataLength = labels.length
+ if (dataLength === 0 || byStatus.size === 0) {
+ return <EmptyState icon="chart" description="No data available" />
+ }
+
+ // `toggledLifecycles` is a client-side filter in the main app: the backend always returns
+ // all four buckets and the UI hides the toggled-off ones. Mirror that here.
+ const toggledLifecycles = query?.lifecycleFilter?.toggledLifecycles
+ const isVisible = (status: LifecycleStatus): boolean =>
+ byStatus.has(status) && (!toggledLifecycles || toggledLifecycles.includes(status))
+ const visibleBuckets = ALL_STATUSES.filter(isVisible)
+ const showLegend = query?.lifecycleFilter?.showLegend ?? true
+
+ const { max, min } = computeBounds(byStatus, dataLength, isVisible)
+ const range = Math.max(max - min, 1)
+
+ const innerWidth = CHART_WIDTH - PADDING.left - PADDING.right
+ const innerHeight = CHART_HEIGHT - PADDING.top - PADDING.bottom
+ const zeroY = PADDING.top + (max / range) * innerHeight
+ const valueToY = (value: number): number => zeroY - (value / range) * innerHeight
+ const barGroupWidth = innerWidth / dataLength
+ const barWidth = Math.min(barGroupWidth * 0.7, 36)
+
+ // Tick values: 0 plus a few evenly-spaced positions above and below.
+ const positiveTicks = [0.25, 0.5, 0.75, 1].map((ratio) => max * ratio).filter((v) => v > 0)
+ const negativeTicks = [0.25, 0.5, 0.75, 1].map((ratio) => min * ratio).filter((v) => v < 0)
+ const ticks = [...negativeTicks.reverse(), 0, ...positiveTicks]
+
+ return (
+ <div>
+ <svg viewBox={`0 0 ${CHART_WIDTH} ${CHART_HEIGHT}`} style={{ width: '100%' }}>
+ {/* Y-axis grid + labels */}
+ {ticks.map((value) => {
+ const y = valueToY(value)
+ return (
+ <g key={value}>
+ <line
+ x1={PADDING.left}
+ y1={y}
+ x2={CHART_WIDTH - PADDING.right}
+ y2={y}
+ stroke="var(--color-border-primary, #e5e7eb)"
+ strokeDasharray={value === 0 ? '0' : '4,4'}
+ />
+ <text
+ x={PADDING.left - 8}
+ y={y + 4}
+ textAnchor="end"
+ fontSize="10"
+ fill="var(--color-text-secondary, #6b7280)"
+ >
+ {formatNumber(value)}
+ </text>
+ </g>
+ )
+ })}
+
+ {/* X-axis labels — thin out when crowded. */}
+ {labels.map((label, i) => {
+ if (labels.length > 8 && i % Math.ceil(labels.length / 8) !== 0) {
+ return null
+ }
+ const x = PADDING.left + (i + 0.5) * barGroupWidth
+ return (
+ <text
+ key={i}
+ x={x}
+ y={CHART_HEIGHT - 16}
+ textAnchor="middle"
+ fontSize="10"
+ fill="var(--color-text-secondary, #6b7280)"
+ >
+ {formatDate(label)}
+ </text>
+ )
+ })}
+
+ {/* Stacked bars — positive buckets stack upward, dormant extends downward. */}
+ {Array.from({ length: dataLength }).map((_, i) => {
+ const groupX = PADDING.left + i * barGroupWidth
+ const x = groupX + (barGroupWidth - barWidth) / 2
+
+ let positiveCursor = 0
+ let negativeCursor = 0
+ return (
+ <g key={i}>
+ {POSITIVE_STATUSES.map((status) => {
+ if (!isVisible(status)) {
+ return null
+ }
+ const series = byStatus.get(status)
+ const value = series?.data[i] ?? 0
+ if (value <= 0) {
+ return null
+ }
+ const yTop = valueToY(positiveCursor + value)
+ const yBottom = valueToY(positiveCursor)
+ positiveCursor += value
+ return (
+ <rect
+ key={`${status}-${i}`}
+ x={x}
+ y={yTop}
+ width={barWidth}
+ height={Math.max(yBottom - yTop, 0)}
+ fill={LIFECYCLE_COLORS[status]}
+ />
+ )
+ })}
+ {NEGATIVE_STATUSES.map((status) => {
+ if (!isVisible(status)) {
+ return null
+ }
+ const series = byStatus.get(status)
+ const value = series?.data[i] ?? 0
+ if (value >= 0) {
+ return null
+ }
+ const yTop = valueToY(negativeCursor)
+ const yBottom = valueToY(negativeCursor + value)
+ negativeCursor += value
+ return (
+ <rect
+ key={`${status}-${i}`}
+ x={x}
+ y={yTop}
+ width={barWidth}
+ height={Math.max(yBottom - yTop, 0)}
+ fill={LIFECYCLE_COLORS[status]}
+ />
+ )
+ })}
+ </g>
+ )
+ })}
+ </svg>
+
+ {showLegend && visibleBuckets.length > 0 && (
+ <div
+ style={{
+ display: 'flex',
+ flexWrap: 'wrap',
+ gap: '1rem',
+ justifyContent: 'center',
+ marginTop: '0.5rem',
+ fontSize: '0.75rem',
+ }}
+ >
+ {visibleBuckets.map((status) => (
+ <div key={status} style={{ display: 'flex', alignItems: 'center', gap: '0.25rem' }}>
+ <div
+ style={{
+ width: '12px',
+ height: '12px',
+ borderRadius: '2px',
+ backgroundColor: LIFECYCLE_COLORS[status],
+ }}
+ />
+ <span style={{ color: 'var(--color-text-secondary, #6b7280)' }}>{status}</span>
+ </div>
+ ))}
+ </div>
+ )}
+ </div>
+ )
+}
diff --git a/services/mcp/src/ui-apps/components/types.ts b/services/mcp/src/ui-apps/components/types.ts
index 16c6c60fb8b9..04c01cf363f7 100644
--- a/services/mcp/src/ui-apps/components/types.ts
+++ b/services/mcp/src/ui-apps/components/types.ts
@@ -48,6 +48,23 @@ export interface FunnelsQuery {
}>
}
+export type LifecycleStatus = 'new' | 'returning' | 'resurrecting' | 'dormant'
+
+export interface LifecycleQuery {
+ kind: 'LifecycleQuery'
+ series?: Array<{
+ event?: string
+ name?: string
+ custom_name?: string
+ }>
+ lifecycleFilter?: {
+ toggledLifecycles?: LifecycleStatus[]
+ showLegend?: boolean
+ showValuesOnSeries?: boolean
+ stacked?: boolean
+ }
+}
+
export interface HogQLQuery {
kind: 'HogQLQuery'
query: string
@@ -67,6 +84,16 @@ export interface TrendsResultItem {
export type TrendsResult = TrendsResultItem[]
+export interface LifecycleResultItem extends TrendsResultItem {
+ /**
+ * Lifecycle bucket the series belongs to. Counts for `dormant` come back negated
+ * from the backend so the chart can render them below zero.
+ */
+ status?: LifecycleStatus
+}
+
+export type LifecycleResult = LifecycleResultItem[]
+
export interface FunnelStep {
name?: string
custom_name?: string
@@ -102,6 +129,11 @@ export interface FunnelPayload extends BasePayload {
results: FunnelResult
}
+export interface LifecyclePayload extends BasePayload {
+ query: LifecycleQuery
+ results: LifecycleResult
+}
+
export interface TablePayload extends BasePayload {
query?: HogQLQuery
results: HogQLResult
@@ -121,6 +153,11 @@ export interface FunnelVisualizerProps {
results: FunnelResult
}
+export interface LifecycleVisualizerProps {
+ query: LifecycleQuery | undefined
+ results: LifecycleResult
+}
+
export interface TableVisualizerProps {
results: HogQLResult
}