: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
feat(mcp): track render-ui tool, render failures and insight display (#114349)
Sam Pennington5e433cb162d1
diff --git a/services/mcp/src/ui-apps/apps/render-ui.tsx b/services/mcp/src/ui-apps/apps/render-ui.tsxindex bdab8bb871ac..4c92d2c5e27d 100644--- a/services/mcp/src/ui-apps/apps/render-ui.tsx+++ b/services/mcp/src/ui-apps/apps/render-ui.tsx@@ -5,6 +5,7 @@ import { useEffect, useState } from 'react'import { createRoot } from 'react-dom/client'import type { UiAppKey } from '../../resources/ui-apps.generated'+import { capture } from '../analytics/posthog'import { AppErrorState } from '../components/AppErrorState'import { AppLoadingState } from '../components/AppLoadingState'import { AppWrapper } from '../components/AppWrapper'@@ -40,15 +41,18 @@ function RenderUiContent({openLink: (url: string) => void}): JSX.Element {const [data, setData] = useState<unknown>(null)- const [error, setError] = useState<string | null>(null)+ const [error, setError] = useState<{ reason: string; message: string } | null>(null)// Key the fetch on serialized values, not `envelope` identity — a parent// re-render with a fresh-but-equal envelope object must not refetch.const toolName = envelope.tool_nameconst toolInputJson = JSON.stringify(envelope.tool_input ?? {})+ useEffect(() => {+ capture('mcp_ui_app_render_requested', { rendered_tool_name: toolName, app_key: envelope.app_key })+ }, [toolName, envelope.app_key])useEffect(() => {if (!app) {- setError('Visualization unavailable: app context not provided.')+ setError({ reason: 'no_app', message: 'Visualization unavailable: app context not provided.' })return}let cancelled = false@@ -60,14 +64,14 @@ function RenderUiContent({return}if (result.isError || !result.structuredContent) {- setError(`Could not load data for ${toolName}.`)+ setError({ reason: 'tool_error', message: `Could not load data for ${toolName}.` })return}setData(result.structuredContent)}).catch((e: unknown) => {if (!cancelled) {- setError(e instanceof Error ? e.message : String(e))+ setError({ reason: 'exception', message: e instanceof Error ? e.message : String(e) })}})return () => {@@ -76,11 +80,22 @@ function RenderUiContent({}, [app, toolName, toolInputJson])const render = RENDER_DISPATCH[envelope.app_key]+ const failureReason = render ? error?.reason : 'no_visualization'+ useEffect(() => {+ if (failureReason) {+ capture('mcp_ui_app_render_failed', {+ rendered_tool_name: toolName,+ app_key: envelope.app_key,+ reason: failureReason,+ })+ }+ }, [failureReason, toolName, envelope.app_key])+if (!render) {return <AppErrorState message={`No visualization is available for ${envelope.tool_name}.`} />}if (error) {- return <AppErrorState message={error} />+ return <AppErrorState message={error.message} />}if (data === null) {return <AppLoadingState />diff --git a/services/mcp/src/ui-apps/components/utils.ts b/services/mcp/src/ui-apps/components/utils.tsindex cbe49d1c6ca9..d6d13c9b2a74 100644--- a/services/mcp/src/ui-apps/components/utils.ts+++ b/services/mcp/src/ui-apps/components/utils.ts@@ -20,12 +20,16 @@ export function insightQueryProperties(query: unknown): InsightQueryProperties {const node = query as Record<string, unknown>const hasSource = typeof node.source === 'object' && node.source !== nullconst source = (hasSource ? node.source : node) as Record<string, any>- const defaultDisplay =- source.kind === 'TrendsQuery' || source.kind === 'StickinessQuery' ? 'ActionsLineGraph' : undefinedreturn {queryKind: typeof node.kind === 'string' ? node.kind : undefined,querySourceKind: hasSource && typeof source.kind === 'string' ? source.kind : undefined,- display: source.trendsFilter?.display ?? source.stickinessFilter?.display ?? defaultDisplay,+ // Only what the query set, so an unset display stays empty instead of looking like a pick.+ // SQL insights keep their display on the wrapper node, not on the HogQL source.+ display:+ source.trendsFilter?.display ??+ source.stickinessFilter?.display ??+ source.retentionFilter?.display ??+ (node.kind === 'DataVisualizationNode' && typeof node.display === 'string' ? node.display : undefined),funnelVizType: source.funnelsFilter?.funnelVizType,}}diff --git a/services/mcp/tests/unit/insight-visualizations.test.ts b/services/mcp/tests/unit/insight-visualizations.test.tsindex 95a533a42580..53a94aabd403 100644--- a/services/mcp/tests/unit/insight-visualizations.test.ts+++ b/services/mcp/tests/unit/insight-visualizations.test.ts@@ -282,18 +282,27 @@ describe('insight visualizations', () => {describe('insightQueryProperties', () => {it.each([- [- 'bare trends without a display',- { kind: 'TrendsQuery' },- { queryKind: 'TrendsQuery', display: 'ActionsLineGraph' },- ],+ ['bare trends without a display', { kind: 'TrendsQuery' }, { queryKind: 'TrendsQuery' }],['trends wrapped in InsightVizNode',{kind: 'InsightVizNode',source: { kind: 'TrendsQuery', trendsFilter: { display: 'BoldNumber' } },},- { queryKind: 'InsightVizNode', querySourceKind: 'TrendsQuery', display: 'BoldNumber' },+ {+ queryKind: 'InsightVizNode',+ querySourceKind: 'TrendsQuery',+ display: 'BoldNumber',+ },+ ],+ [+ 'SQL insight with its display on the wrapper node',+ { kind: 'DataVisualizationNode', display: 'ActionsBar', source: { kind: 'HogQLQuery' } },+ {+ queryKind: 'DataVisualizationNode',+ querySourceKind: 'HogQLQuery',+ display: 'ActionsBar',+ },],])('%s', (_, query, expected) => {expect(insightQueryProperties(query)).toEqual(expected)