: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
fix(mcp): preserve filter/variable overrides in insight-query link (#64328)
Sam Penningtonf810181f51ff
diff --git a/services/mcp/src/tools/insights/query.ts b/services/mcp/src/tools/insights/query.tsindex ef5743b741c2..fe4b5584822a 100644--- a/services/mcp/src/tools/insights/query.ts+++ b/services/mcp/src/tools/insights/query.ts@@ -42,6 +42,9 @@ export const queryHandler: ToolBase<typeof schema, Result>['handler'] = async (cconst { insightId, output_format, variables_override, filters_override } = paramsconst projectId = await context.stateManager.getProjectId()+ const normalizedVariables = normalizeOverride(variables_override)+ const normalizedFilters = normalizeOverride(filters_override)+// Threading overrides through the .get() call lets the Django retrieve endpoint// merge them server-side (via apply_dashboard_variables_to_dict /// apply_dashboard_filters_to_dict). The merged query is then POSTed to /query/@@ -49,8 +52,8 @@ export const queryHandler: ToolBase<typeof schema, Result>['handler'] = async (c// mutating the saved insight.const insightResult = await context.api.insights({ projectId }).get({insightId,- variables_override: normalizeOverride(variables_override),- filters_override: normalizeOverride(filters_override),+ variables_override: normalizedVariables,+ filters_override: normalizedFilters,})if (!insightResult.success) {@@ -65,7 +68,18 @@ export const queryHandler: ToolBase<typeof schema, Result>['handler'] = async (cthrow new Error(`Failed to query insight: ${queryResult.error.message}`)}- const path = `/insights/${insightResult.data.short_id}`+ // Carry the overrides into the link as query params so opening the insight in+ // the UI reflects the same filtered/variabled view that was rendered — matching+ // the frontend's own `urls.insightView` encoding. Without this the link resolves+ // to the bare saved insight and silently drops the overrides.+ const overrideParams = [+ { key: 'variables_override', value: normalizedVariables },+ { key: 'filters_override', value: normalizedFilters },+ ]+ .filter((p): p is { key: string; value: string } => Boolean(p.value))+ .map((p) => `${p.key}=${encodeURIComponent(p.value)}`)+ .join('&')+ const path = `/insights/${insightResult.data.short_id}${overrideParams ? `?${overrideParams}` : ''}`const fullUrl = `${context.api.getProjectBaseUrl(projectId)}${path}`const queryInfo = analyzeQuery(insightResult.data.query)diff --git a/services/mcp/tests/unit/insights-query-handler.test.ts b/services/mcp/tests/unit/insights-query-handler.test.tsindex a5ec8bb1ac06..cfb04c017ddc 100644--- a/services/mcp/tests/unit/insights-query-handler.test.ts+++ b/services/mcp/tests/unit/insights-query-handler.test.ts@@ -27,6 +27,8 @@ interface MockHandles {interface QueryResult {query: unknownresults: unknown+ insight: { url: string }+ _posthogUrl: string[POSTHOG_FORMATTED_RESULTS_OVERRIDE_KEY]?: string}@@ -133,6 +135,49 @@ describe('queryHandler — overrides forwarding', () => {})})+describe('queryHandler — link reflects overrides', () => {+ it('links to the bare saved insight when no overrides are passed', async () => {+ const { context } = createContext()++ const result = (await queryHandler(context, { insightId: '42', output_format: 'json' })) as QueryResult++ expect(result._posthogUrl).toBe('https://us.posthog.com/project/1/insights/abc12345')+ expect(result.insight.url).toBe('https://us.posthog.com/project/1/insights/abc12345')+ })++ it('encodes filters_override into the link query string', async () => {+ const { context } = createContext()++ const result = (await queryHandler(context, {+ insightId: '42',+ output_format: 'json',+ filters_override: filtersOverrideObject as unknown as string,+ })) as QueryResult++ const expected = `https://us.posthog.com/project/1/insights/abc12345?filters_override=${encodeURIComponent(+ JSON.stringify(filtersOverrideObject)+ )}`+ expect(result._posthogUrl).toBe(expected)+ expect(result.insight.url).toBe(expected)+ })++ it('encodes both variables_override and filters_override into the link', async () => {+ const { context } = createContext()++ const result = (await queryHandler(context, {+ insightId: '42',+ output_format: 'json',+ variables_override: variablesOverrideObject as unknown as string,+ filters_override: JSON.stringify(filtersOverrideObject),+ })) as QueryResult++ const expected = `https://us.posthog.com/project/1/insights/abc12345?variables_override=${encodeURIComponent(+ JSON.stringify(variablesOverrideObject)+ )}&filters_override=${encodeURIComponent(JSON.stringify(filtersOverrideObject))}`+ expect(result._posthogUrl).toBe(expected)+ })+})+describe('queryHandler — result shape for UI rendering', () => {const formatted = 'c\n1'