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(insights): render retention and other charts in insight-query MCP app (#65067)

Sam Pennington9964b1914575
diff --git a/services/mcp/src/tools/insights/query.ts b/services/mcp/src/tools/insights/query.ts
index fe4b5584822a..ca413698edb1 100644
--- a/services/mcp/src/tools/insights/query.ts
+++ b/services/mcp/src/tools/insights/query.ts
@@ -83,16 +83,18 @@ export const queryHandler: ToolBase<typeof schema, Result>['handler'] = async (c
const fullUrl = `${context.api.getProjectBaseUrl(projectId)}${path}`
const queryInfo = analyzeQuery(insightResult.data.query)
- // Trends/funnel UI visualizers consume the raw results array; every other
- // visualization (HogQL/table, retention, lifecycle, paths) expects the
- // `{ columns, results }` shape the structural guards look for.
- const isSeries = queryInfo.visualization === 'trends' || queryInfo.visualization === 'funnel'
- const results = isSeries
- ? queryResult.data.results
- : {
+ // Only HogQL/table results carry a separate `columns` array, so they're the only
+ // shape the UI app wraps as `{ columns, results }`. Every chart visualizer
+ // (trends, funnel, retention, lifecycle, stickiness, paths) consumes the raw
+ // results array directly — wrapping those breaks the structural guards and renders
+ // an empty table.
+ const isTabular = queryInfo.visualization === 'table'
+ const results = isTabular
+ ? {
columns: queryResult.data.columns || [],
results: queryResult.data.results || [],
}
+ : queryResult.data.results
// Optimized output surfaces the server-formatted summary as the model-facing text, but the
// UI app still needs the structured results in structuredContent. Carry the formatted string
diff --git a/services/mcp/src/tools/shared.ts b/services/mcp/src/tools/shared.ts
index c2b02fa1135d..26aeb5d216cc 100644
--- a/services/mcp/src/tools/shared.ts
+++ b/services/mcp/src/tools/shared.ts
@@ -1,6 +1,9 @@
type QueryKind =
| 'TrendsQuery'
| 'FunnelsQuery'
+ | 'RetentionQuery'
+ | 'LifecycleQuery'
+ | 'StickinessQuery'
| 'PathsQuery'
| 'HogQLQuery'
| 'InsightVizNode'
@@ -8,13 +11,23 @@ type QueryKind =
| string
interface QueryInfo {
- visualization: 'trends' | 'funnel' | 'paths' | 'table'
+ visualization: 'trends' | 'funnel' | 'retention' | 'lifecycle' | 'stickiness' | 'paths' | 'table'
/** The inner query kind (e.g., TrendsQuery inside InsightVizNode) */
innerKind: QueryKind
/** The inner query object for insight queries */
innerQuery?: Record<string, unknown>
}
+/** Insight query kinds whose results are a raw array consumed directly by the chart visualizers. */
+const SOURCE_VISUALIZATIONS: Record<string, QueryInfo['visualization']> = {
+ TrendsQuery: 'trends',
+ FunnelsQuery: 'funnel',
+ RetentionQuery: 'retention',
+ LifecycleQuery: 'lifecycle',
+ StickinessQuery: 'stickiness',
+ PathsQuery: 'paths',
+}
+
/**
* Analyze the query to determine visualization type and extract inner query info.
*/
@@ -26,14 +39,9 @@ export function analyzeQuery(query: unknown): QueryInfo {
const q = query as Record<string, unknown>
// Direct insight queries
- if (q.kind === 'TrendsQuery') {
- return { visualization: 'trends', innerKind: 'TrendsQuery', innerQuery: q }
- }
- if (q.kind === 'FunnelsQuery') {
- return { visualization: 'funnel', innerKind: 'FunnelsQuery', innerQuery: q }
- }
- if (q.kind === 'PathsQuery') {
- return { visualization: 'paths', innerKind: 'PathsQuery', innerQuery: q }
+ const visualization = typeof q.kind === 'string' ? SOURCE_VISUALIZATIONS[q.kind] : undefined
+ if (visualization) {
+ return { visualization, innerKind: q.kind as string, innerQuery: q }
}
if (q.kind === 'HogQLQuery') {
return { visualization: 'table', innerKind: 'HogQLQuery' }
@@ -42,14 +50,9 @@ export function analyzeQuery(query: unknown): QueryInfo {
// InsightVizNode wraps insight queries
if (q.kind === 'InsightVizNode' && q.source && typeof q.source === 'object') {
const source = q.source as Record<string, unknown>
- if (source.kind === 'TrendsQuery') {
- return { visualization: 'trends', innerKind: 'TrendsQuery', innerQuery: source }
- }
- if (source.kind === 'FunnelsQuery') {
- return { visualization: 'funnel', innerKind: 'FunnelsQuery', innerQuery: source }
- }
- if (source.kind === 'PathsQuery') {
- return { visualization: 'paths', innerKind: 'PathsQuery', innerQuery: source }
+ const sourceVisualization = typeof source.kind === 'string' ? SOURCE_VISUALIZATIONS[source.kind] : undefined
+ if (sourceVisualization) {
+ return { visualization: sourceVisualization, innerKind: source.kind as string, innerQuery: source }
}
}
diff --git a/services/mcp/tests/unit/insights-query-handler.test.ts b/services/mcp/tests/unit/insights-query-handler.test.ts
index cfb04c017ddc..af560a6729ca 100644
--- a/services/mcp/tests/unit/insights-query-handler.test.ts
+++ b/services/mcp/tests/unit/insights-query-handler.test.ts
@@ -238,4 +238,27 @@ describe('queryHandler — result shape for UI rendering', () => {
expect(result.query).toEqual({ kind: 'TrendsQuery' })
expect(result[POSTHOG_FORMATTED_RESULTS_OVERRIDE_KEY]).toBe(formatted)
})
+
+ it.each([
+ ['RetentionQuery', [{ date: '2024-01-01', label: 'Day 0', values: [{ count: 10 }] }]],
+ ['LifecycleQuery', [{ status: 'new', data: [1, 2], days: ['a', 'b'] }]],
+ ['StickinessQuery', [{ count: 5, data: [1, 2], labels: ['1 day', '2 days'] }]],
+ ['PathsQuery', [{ source: '0_a', target: '1_b', value: 3 }]],
+ ])('passes the raw results array through for %s insights', async (kind, chartResults) => {
+ const { context } = createContext({
+ getData: {
+ id: 42,
+ short_id: 'abc12345',
+ query: { kind: 'InsightVizNode', source: { kind } },
+ },
+ queryData: { results: chartResults },
+ })
+
+ const result = (await queryHandler(context, { insightId: '42', output_format: 'json' })) as QueryResult
+
+ // Chart visualizers read the raw array; wrapping it in { columns, results } makes the
+ // structural guards fall through to the table renderer and show an empty table.
+ expect(result.results).toBe(chartResults)
+ expect(result.query).toEqual({ kind })
+ })
})