: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): keep structuredContent for render-ui app data fetches (#92078)
posthog[bot]faa21962e8b4
diff --git a/services/mcp/src/hono/tool-executor.ts b/services/mcp/src/hono/tool-executor.tsindex bb88f6c8b77b..6f6c8254e4af 100644--- a/services/mcp/src/hono/tool-executor.ts+++ b/services/mcp/src/hono/tool-executor.ts@@ -62,6 +62,36 @@ interface ExecMetricState {commandMeta: ExecCommandMeta | undefined}+/**+ * Whether a direct `tools/call` response should drop `structuredContent` and leave the+ * formatted table in the text channel to stand alone.+ *+ * CLI-mode clients read `content[].text`, so for them the structured copy only adds+ * tokens. A render-ui host in single-exec mode is the exception, because there+ * `buildAdvertisedTools` offers `exec` and `render-ui` only, and `handleToolCall` routes+ * both of those before this path. Any other tool name that reaches here is therefore the+ * render-ui app calling `callServerTool` to load its own data. That app reads+ * `structuredContent` and ignores the text channel, so dropping the structured payload+ * leaves it with nothing to draw, and it shows its error state instead of the chart. The+ * `handleToolsList` tests pin the roster this reasoning depends on.+ *+ * MCP gives the executor no way to prove that a call came from the app, so this reads+ * connection state instead of call provenance. In tools mode the model calls tools+ * directly, which makes the two indistinguishable, so suppression still applies there and+ * a UI app loaded that way still gets nothing. To fix that case, pass `forceUiDataToMeta`+ * and `includeUiResponseMeta` together, which moves the app payload to `_meta` instead of+ * widening what the model reads. `buildToolResultPayload` writes that payload only when+ * both flags are set.+ */+function shouldSuppressStructuredContent(args: {+ isCliModeEnabled: boolean+ useSingleExec: boolean+ renderUiEnabled: boolean+}): boolean {+ const isRenderUiHostInSingleExec = args.useSingleExec && args.renderUiEnabled+ return args.isCliModeEnabled && !isRenderUiHostInSingleExec+}+export class ToolExecutor {private readonly catalog: ToolCatalogprivate readonly instructionsBuilder: InstructionsBuilder@@ -268,7 +298,11 @@ export class ToolExecutor {toolMeta: tool._meta,toolName: tool.name,params: validation.data,- suppressStructuredContentForFormattedResults: state.clientProfile.isCliModeEnabled(),+ suppressStructuredContentForFormattedResults: shouldSuppressStructuredContent({+ isCliModeEnabled: state.clientProfile.isCliModeEnabled(),+ useSingleExec: state.useSingleExec,+ renderUiEnabled: state.renderUiEnabled,+ }),distinctId,})}diff --git a/services/mcp/tests/hono/tool-executor.test.ts b/services/mcp/tests/hono/tool-executor.test.tsindex 668c2292ff97..58805fff51e6 100644--- a/services/mcp/tests/hono/tool-executor.test.ts+++ b/services/mcp/tests/hono/tool-executor.test.ts@@ -1,4 +1,5 @@-import { beforeAll, describe, expect, it, vi } from 'vitest'+import { afterEach, beforeAll, describe, expect, it, vi, type MockInstance } from 'vitest'+import { z } from 'zod'vi.mock('@/resources/internals', () => ({fetchContextMillResources: vi.fn().mockRejectedValue(new Error('mocked')),@@ -18,6 +19,7 @@ import { ToolExecutor } from '@/hono/tool-executor'import { buildToolDomainsCompact } from '@/lib/instructions'import { RENDER_UI_RESOURCE_URI, URI_MAP } from '@/resources/ui-apps.generated'import { getToolDefinition } from '@/tools/toolDefinitions'+import { POSTHOG_FORMATTED_RESULTS_OVERRIDE_KEY } from '@/tools/types'// A tool with a renderable (dispatchable) UI app — used to exercise the render-ui path.const uiAppTool = {@@ -333,4 +335,58 @@ describe('ToolExecutor', () => {expect(result.content[0].text).toContain('not found')})})++ // A render-ui app loads its own data with a direct `tools/call`, reading+ // `structuredContent` and ignoring the text channel. The formatted-table+ // suppression that serves text-reading CLI clients must not reach that fetch,+ // or the app has nothing to draw and shows its error state instead of the chart.+ describe('structuredContent on a UI-app data fetch', () => {+ const formattedTable = 'date|count\n2026-08-31|28'+ let getToolByNameSpy: MockInstance | undefined++ afterEach(() => {+ getToolByNameSpy?.mockRestore()+ getToolByNameSpy = undefined+ })++ it.each([+ {+ label: 'keeps it for a render-ui host, where this call is the app',+ useSingleExec: true,+ renderUiEnabled: true,+ expectStructuredContent: true,+ },+ {+ label: 'drops it for a CLI client without render-ui, which reads the table',+ useSingleExec: true,+ renderUiEnabled: false,+ expectStructuredContent: false,+ },+ {+ label: 'drops it in tools mode, where a direct call may be the model',+ useSingleExec: false,+ renderUiEnabled: true,+ expectStructuredContent: false,+ },+ ])('$label', async ({ useSingleExec, renderUiEnabled, expectStructuredContent }) => {+ getToolByNameSpy = vi.spyOn(catalog, 'getToolByName').mockReturnValue({+ base: {+ schema: z.object({}),+ handler: async () => ({+ results: [{ count: 28, label: '$pageview' }],+ [POSTHOG_FORMATTED_RESULTS_OVERRIDE_KEY]: formattedTable,+ }),+ _meta: uiAppTool._meta,+ },+ } as any)++ const state = makeState([uiAppTool], { useSingleExec, renderUiEnabled })+ vi.mocked(state.clientProfile.isCliModeEnabled).mockReturnValue(true)++ const result = (await executor.handleToolCall({ name: 'survey-get', arguments: {} }, state)) as any++ expect(result.content[0].text).toContain(formattedTable)+ expect('structuredContent' in result).toBe(expectStructuredContent)+ })+ })})