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(mcp): render SQL time-series results with quill charts (#69304)

Sam Pennington1890762a99c4
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 2d3135818bab..a1bf979386fd 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -4416,6 +4416,12 @@ snapshots:
hash: v1.k794b7964.5c33ba0a4dbe6cf279197eadea244d7b9838b99d80b8ba2b899df7ca881e066b.GNSIWjjq4NaJTgY9UUDYIXoxNuzz74X5yRL5TuFPO_Y
mcp-apps-lifecycle--stacked--light:
hash: v1.k794b7964.ba05f9228b85cb77c43ddf41d7c4612123546422dc1ab7adea5c2f0ac57fede1.5HaCtwm-U2r0pTewvvTY96rwrsRFfyk9dh5liLtVxkI
+ mcp-apps-query-results--single-number--light:
+ hash: v1.k794b7964.b53e77f7c428fe5d86903e3bee082eb7c752f67fb3225c043229e4b713debe69.hhBuSmExHBpDioDG9Ij-4qfvVhG77IC5FNYierdlZq8
+ mcp-apps-query-results--table--light:
+ hash: v1.k794b7964.902b5373ca5305edcf1bd3440def4e5e2918ae9ddc32f21c780a3bb4a006b9a7.zBD1FDF7WTERVfwWCIZhb5T9D8FPJNbD3_UFCWB8mhc
+ mcp-apps-query-results--time-series--light:
+ hash: v1.k794b7964.db641b56d0ea4fbeda26aa548d2490f69b1e4cc3702f8462d44ac550e6759644.tIrsyH43vI28LEEGZKQKzbZAx3QQz44eLcqYpEYVW08
mcp-apps-retention-actors--daily--light:
hash: v1.k794b7964.98ed364bc169b666ee69ff6e44c7fa385360df9410875c5ee3cd85ff2e53104f.-p5rL3gbD8LRZ-kriXkNM3NHeYm8nf0v0VfVq5XWZFs
mcp-apps-stickiness--multiple-series--light:
diff --git a/services/mcp/src/ui-apps/components/Table.stories.tsx b/services/mcp/src/ui-apps/components/Table.stories.tsx
new file mode 100644
index 000000000000..83d76b89c241
--- /dev/null
+++ b/services/mcp/src/ui-apps/components/Table.stories.tsx
@@ -0,0 +1,74 @@
+import type { Meta, StoryFn, StoryObj } from '@storybook/react'
+import type { ReactElement } from 'react'
+
+import { McpThemeDecorator } from '@posthog/mcp-ui/storybook/decorator'
+
+import { TableVisualizer } from './TableVisualizer'
+import type { HogQLResult } from './types'
+
+// A timestamp column plus a numeric column triggers the time-series detection path,
+// which renders a chart instead of a table — the shape execute-sql results most often take.
+const TIME_SERIES: HogQLResult = {
+ columns: ['day', 'unique_users'],
+ results: [
+ ['2025-05-26T00:00:00', 1450],
+ ['2025-05-27T00:00:00', 1620],
+ ['2025-05-28T00:00:00', 1390],
+ ['2025-05-29T00:00:00', 1810],
+ ['2025-05-30T00:00:00', 2050],
+ ['2025-05-31T00:00:00', 980],
+ ['2025-06-01T00:00:00', 1240],
+ ],
+}
+
+// 1 row × 1 numeric column triggers the big-number path.
+const SINGLE_NUMBER: HogQLResult = {
+ columns: ['total_events'],
+ results: [[1_284_942]],
+}
+
+// Mixed column types fall through to the plain table, exercising per-type cell
+// formatting (numbers, booleans, nulls, nested objects).
+const TABLE: HogQLResult = {
+ columns: ['event', 'count', 'is_internal', 'last_seen', 'properties'],
+ results: [
+ ['$pageview', 48210, false, '2025-06-01T12:34:56', { $browser: 'Chrome' }],
+ ['$autocapture', 21984, false, '2025-06-01T11:20:03', null],
+ ['sign_up', 312, true, '2025-05-31T09:12:44', { plan: 'free' }],
+ ],
+}
+
+// The visualizer sizes its canvas off a ResizeObserver, so give it a definite width — a `w-full` box
+// can measure 0 at mount in the headless snapshot runner and paint nothing.
+const FixedWidth = (Story: StoryFn): ReactElement => (
+ <div className="w-[680px]">
+ <Story />
+ </div>
+)
+
+const meta: Meta = {
+ title: 'MCP Apps/Query results',
+ decorators: [McpThemeDecorator, FixedWidth],
+ parameters: {
+ testOptions: {
+ skipDarkMode: true,
+ },
+ },
+}
+export default meta
+
+type Story = StoryObj<{}>
+
+export const TimeSeries: Story = {
+ render: () => <TableVisualizer results={TIME_SERIES} />,
+ name: 'Time series',
+}
+
+export const SingleNumber: Story = {
+ render: () => <TableVisualizer results={SINGLE_NUMBER} />,
+ name: 'Single number',
+}
+
+export const Table: Story = {
+ render: () => <TableVisualizer results={TABLE} />,
+}
diff --git a/services/mcp/src/ui-apps/components/TableVisualizer.tsx b/services/mcp/src/ui-apps/components/TableVisualizer.tsx
index f0ad97a295df..52b88916761e 100644
--- a/services/mcp/src/ui-apps/components/TableVisualizer.tsx
+++ b/services/mcp/src/ui-apps/components/TableVisualizer.tsx
@@ -2,11 +2,19 @@ import type { ReactElement } from 'react'
import { emptyStateIllustration } from '@posthog/mcp-ui'
import { DataTable, type DataTableProps, Empty, EmptyDescription, EmptyHeader, EmptyMedia } from '@posthog/quill'
+import {
+ DefaultTooltip,
+ type Series,
+ TimeSeriesLineChart,
+ type TimeSeriesLineChartConfig,
+ type TooltipContext,
+} from '@posthog/quill-charts'
import { ChartHeader } from './ChartHeader'
-import { BigNumber, LineChart, type Series } from './charts'
+import { BigNumber } from './charts'
+import { colorAt, useMcpChartTheme } from './charts/theme'
import type { TableVisualizerProps } from './types'
-import { formatNumber } from './utils'
+import { formatDate, formatNumber, formatTooltipDate } from './utils'
const TITLE = 'Query results'
@@ -100,26 +108,33 @@ function transformToSeries(
timeIdx: number,
valueIdx: number,
valueLabel: string
-): { series: Series[]; labels: string[]; maxValue: number } {
- const labels: string[] = []
- let maxValue = 0
-
- const points = rows.map((row, i) => {
- const label = String(row[timeIdx])
- const value = row[valueIdx] as number
- labels.push(label)
- maxValue = Math.max(maxValue, value)
- return { x: i, y: value, label }
- })
+): { series: Series[]; labels: string[] } {
+ const labels = rows.map((row) => String(row[timeIdx]))
+ const data = rows.map((row) => row[valueIdx] as number)
return {
- series: [{ label: valueLabel, points }],
+ series: [{ key: '0', label: valueLabel, data, color: colorAt(0) }],
labels,
- maxValue: maxValue || 1,
}
}
+const TOOLTIP_CONFIG = { pinnable: true, placement: 'cursor' as const }
+
+// DefaultTooltip shows the raw x label; format it like the axis.
+const renderDateTooltip = (ctx: TooltipContext): ReactElement => (
+ <DefaultTooltip {...ctx} label={formatTooltipDate(ctx.label)} />
+)
+
+const TIME_SERIES_CONFIG: TimeSeriesLineChartConfig = {
+ xAxis: { tickFormatter: (value) => formatDate(value) },
+ yAxis: { tickFormatter: formatNumber, showGrid: true },
+ showCrosshair: true,
+ tooltip: TOOLTIP_CONFIG,
+ legend: { show: false },
+}
+
export function TableVisualizer({ results }: TableVisualizerProps): ReactElement {
+ const theme = useMcpChartTheme()
const columns = results?.columns || []
const rows = results?.results || []
@@ -140,16 +155,19 @@ export function TableVisualizer({ results }: TableVisualizerProps): ReactElement
format.valueColumnIndex !== undefined
) {
const valueLabel = columns[format.valueColumnIndex] || 'Value'
- const { series, labels, maxValue } = transformToSeries(
- rows,
- format.timeColumnIndex,
- format.valueColumnIndex,
- valueLabel
- )
+ const { series, labels } = transformToSeries(rows, format.timeColumnIndex, format.valueColumnIndex, valueLabel)
return (
<div>
<ChartHeader title={TITLE} />
- <LineChart series={series} labels={labels} maxValue={maxValue} showLegend={false} />
+ <div className="flex flex-col w-full h-[400px]">
+ <TimeSeriesLineChart
+ series={series}
+ labels={labels}
+ theme={theme}
+ config={TIME_SERIES_CONFIG}
+ tooltip={renderDateTooltip}
+ />
+ </div>
</div>
)
}
diff --git a/services/mcp/src/ui-apps/components/charts/BarChart.tsx b/services/mcp/src/ui-apps/components/charts/BarChart.tsx
deleted file mode 100644
index 2901b46797a7..000000000000
--- a/services/mcp/src/ui-apps/components/charts/BarChart.tsx
+++ /dev/null
@@ -1,171 +0,0 @@
-import type { ReactElement } from 'react'
-
-import { formatDate, formatNumber } from '../utils'
-
-// TODO(quill): replace with a Quill chart primitive (e.g. `BarChart` /
-// `Charts.Bar`) once Quill ships one. Quill currently has no chart layer —
-// it stops at primitives/components/blocks, none of which render data viz.
-// This bespoke SVG renderer uses Quill design tokens (`--text-secondary`
-// via the host bridge, `--posthog-chart-*` palette declared in base.css)
-// so the visual language already matches.
-
-const CHART_HEIGHT = 200
-const CHART_WIDTH = 400
-const PADDING = { top: 20, right: 20, bottom: 40, left: 50 }
-
-const COLORS = [
- 'var(--posthog-chart-1, #1d4aff)',
- 'var(--posthog-chart-2, #621da6)',
- 'var(--posthog-chart-3, #42827e)',
- 'var(--posthog-chart-4, #ce0e74)',
- 'var(--posthog-chart-5, #f14f58)',
- 'var(--posthog-chart-6, #7c440e)',
- 'var(--posthog-chart-7, #529a0a)',
- 'var(--posthog-chart-8, #0476fb)',
-]
-
-export interface DataPoint {
- x: number
- y: number
- label: string
-}
-
-export interface Series {
- label: string
- points: DataPoint[]
-}
-
-export interface BarChartProps {
- series: Series[]
- labels: string[]
- maxValue: number
- showLegend?: boolean
- yAxisLabel?: string | undefined
-}
-
-export function BarChart({ series, labels, maxValue, showLegend = true, yAxisLabel }: BarChartProps): ReactElement {
- const innerWidth = CHART_WIDTH - PADDING.left - PADDING.right
- const innerHeight = CHART_HEIGHT - PADDING.top - PADDING.bottom
-
- const numBars = labels.length
- const numSeries = series.length
- const barGroupWidth = innerWidth / numBars
- const barWidth = Math.min(barGroupWidth * 0.8, 40) / numSeries
- const barGap = barWidth * 0.1
-
- return (
- <div>
- <svg viewBox={`0 0 ${CHART_WIDTH} ${CHART_HEIGHT}`} style={{ width: '100%', height: '400px' }}>
- {yAxisLabel && (
- <text
- x={12}
- y={PADDING.top + innerHeight / 2}
- textAnchor="middle"
- fontSize="11"
- fill="var(--color-text-secondary, #6b7280)"
- transform={`rotate(-90, 12, ${PADDING.top + innerHeight / 2})`}
- >
- {yAxisLabel}
- </text>
- )}
-
- {/* Y-axis labels */}
- {[0, 0.5, 1].map((ratio) => {
- const value = maxValue * ratio
- const y = PADDING.top + innerHeight - (value / maxValue) * innerHeight
- return (
- <g key={ratio}>
- <line
- x1={PADDING.left}
- y1={y}
- x2={CHART_WIDTH - PADDING.right}
- y2={y}
- stroke="var(--color-border-primary, #e5e7eb)"
- strokeDasharray={ratio === 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 */}
- {labels.map((label, i) => {
- if (labels.length > 7 && i % Math.ceil(labels.length / 7) !== 0) {
- return null
- }
- const x = PADDING.left + (i + 0.5) * barGroupWidth
- return (
- <text
- key={i}
- x={x}
- y={CHART_HEIGHT - 8}
- textAnchor="middle"
- fontSize="10"
- fill="var(--color-text-secondary, #6b7280)"
- >
- {formatDate(label)}
- </text>
- )
- })}
-
- {/* Bars */}
- {series.map((s, seriesIndex) =>
- s.points.map((p, i) => {
- const barHeight = (p.y / maxValue) * innerHeight
- const groupX = PADDING.left + i * barGroupWidth
- const barX = groupX + (barGroupWidth - numSeries * barWidth - (numSeries - 1) * barGap) / 2
- const x = barX + seriesIndex * (barWidth + barGap)
- const y = PADDING.top + innerHeight - barHeight
-
- return (
- <rect
- key={`${seriesIndex}-${i}`}
- x={x}
- y={y}
- width={barWidth}
- height={barHeight}
- fill={COLORS[seriesIndex % COLORS.length]}
- rx="2"
- />
- )
- })
- )}
- </svg>
-
- {showLegend && series.length > 1 && (
- <div
- style={{
- display: 'flex',
- flexWrap: 'wrap',
- gap: '1rem',
- justifyContent: 'center',
- marginTop: '0.5rem',
- fontSize: '0.75rem',
- }}
- >
- {series.map((s, i) => (
- <div key={i} style={{ display: 'flex', alignItems: 'center', gap: '0.25rem' }}>
- <div
- style={{
- width: '12px',
- height: '12px',
- borderRadius: '2px',
- backgroundColor: COLORS[i % COLORS.length],
- }}
- />
- <span style={{ color: 'var(--color-text-secondary, #6b7280)' }}>{s.label}</span>
- </div>
- ))}
- </div>
- )}
- </div>
- )
-}
diff --git a/services/mcp/src/ui-apps/components/charts/LineChart.tsx b/services/mcp/src/ui-apps/components/charts/LineChart.tsx
deleted file mode 100644
index 68574dbb4341..000000000000
--- a/services/mcp/src/ui-apps/components/charts/LineChart.tsx
+++ /dev/null
@@ -1,170 +0,0 @@
-import type { ReactElement } from 'react'
-
-import { formatDate, formatNumber } from '../utils'
-
-// TODO(quill): replace with a Quill chart primitive (e.g. `LineChart` /
-// `Charts.Line`) once Quill ships one. Quill currently has no chart layer —
-// it stops at primitives/components/blocks, none of which render data viz.
-// This bespoke SVG renderer uses Quill design tokens via the host bridge so
-// the visual language already matches.
-
-const CHART_HEIGHT = 200
-const CHART_WIDTH = 400
-const PADDING = { top: 20, right: 20, bottom: 40, left: 50 }
-
-const COLORS = [
- 'var(--posthog-chart-1, #1d4aff)',
- 'var(--posthog-chart-2, #621da6)',
- 'var(--posthog-chart-3, #42827e)',
- 'var(--posthog-chart-4, #ce0e74)',
- 'var(--posthog-chart-5, #f14f58)',
- 'var(--posthog-chart-6, #7c440e)',
- 'var(--posthog-chart-7, #529a0a)',
- 'var(--posthog-chart-8, #0476fb)',
-]
-
-export interface DataPoint {
- x: number
- y: number
- label: string
-}
-
-export interface Series {
- label: string
- points: DataPoint[]
-}
-
-export interface LineChartProps {
- series: Series[]
- labels: string[]
- maxValue: number
- showLegend?: boolean
- yAxisLabel?: string | undefined
-}
-
-export function LineChart({ series, labels, maxValue, showLegend = true, yAxisLabel }: LineChartProps): ReactElement {
- const innerWidth = CHART_WIDTH - PADDING.left - PADDING.right
- const innerHeight = CHART_HEIGHT - PADDING.top - PADDING.bottom
-
- const xScale = (index: number): number => PADDING.left + (index / Math.max(labels.length - 1, 1)) * innerWidth
- const yScale = (value: number): number => PADDING.top + innerHeight - (value / maxValue) * innerHeight
-
- return (
- <div>
- <svg viewBox={`0 0 ${CHART_WIDTH} ${CHART_HEIGHT}`} style={{ width: '100%', height: '400px' }}>
- {yAxisLabel && (
- <text
- x={12}
- y={PADDING.top + innerHeight / 2}
- textAnchor="middle"
- fontSize="11"
- fill="var(--color-text-secondary, #6b7280)"
- transform={`rotate(-90, 12, ${PADDING.top + innerHeight / 2})`}
- >
- {yAxisLabel}
- </text>
- )}
-
- {/* Y-axis labels */}
- {[0, 0.5, 1].map((ratio) => {
- const value = maxValue * ratio
- const y = yScale(value)
- return (
- <g key={ratio}>
- <line
- x1={PADDING.left}
- y1={y}
- x2={CHART_WIDTH - PADDING.right}
- y2={y}
- stroke="var(--color-border-primary, #e5e7eb)"
- strokeDasharray={ratio === 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 */}
- {labels.map((label, i) => {
- if (labels.length > 7 && i % Math.ceil(labels.length / 7) !== 0) {
- return null
- }
-
- return (
- <text
- key={i}
- x={xScale(i)}
- y={CHART_HEIGHT - 8}
- textAnchor="middle"
- fontSize="10"
- fill="var(--color-text-secondary, #6b7280)"
- >
- {formatDate(label)}
- </text>
- )
- })}
-
- {/* Lines */}
- {series.map((s, seriesIndex) => {
- if (s.points.length === 0) {
- return null
- }
-
- const pathD = s.points
- .map((p, i) => `${i === 0 ? 'M' : 'L'} ${xScale(p.x)} ${yScale(p.y)}`)
- .join(' ')
-
- return (
- <g key={seriesIndex}>
- <path d={pathD} fill="none" stroke={COLORS[seriesIndex % COLORS.length]} strokeWidth="2" />
- {s.points.map((p, i) => (
- <circle
- key={i}
- cx={xScale(p.x)}
- cy={yScale(p.y)}
- r="3"
- fill={COLORS[seriesIndex % COLORS.length]}
- />
- ))}
- </g>
- )
- })}
- </svg>
-
- {showLegend && series.length > 1 && (
- <div
- style={{
- display: 'flex',
- flexWrap: 'wrap',
- gap: '1rem',
- justifyContent: 'center',
- marginTop: '0.5rem',
- fontSize: '0.75rem',
- }}
- >
- {series.map((s, i) => (
- <div key={i} style={{ display: 'flex', alignItems: 'center', gap: '0.25rem' }}>
- <div
- style={{
- width: '12px',
- height: '12px',
- borderRadius: '2px',
- backgroundColor: COLORS[i % COLORS.length],
- }}
- />
- <span style={{ color: 'var(--color-text-secondary, #6b7280)' }}>{s.label}</span>
- </div>
- ))}
- </div>
- )}
- </div>
- )
-}
diff --git a/services/mcp/src/ui-apps/components/charts/index.ts b/services/mcp/src/ui-apps/components/charts/index.ts
index d0685b12494c..de10795ccced 100644
--- a/services/mcp/src/ui-apps/components/charts/index.ts
+++ b/services/mcp/src/ui-apps/components/charts/index.ts
@@ -1,4 +1,2 @@
export { BigNumber, type BigNumberProps } from './BigNumber'
-export { LineChart, type LineChartProps, type Series, type DataPoint } from './LineChart'
-export { BarChart, type BarChartProps } from './BarChart'
export { Select, type SelectProps, type SelectOption } from './Select'