From 98d6529c77879aad8ebf0fc89613d8c8c4ab99f8 Mon Sep 17 00:00:00 2001 From: Ankur Kapuriya <89287944+Ankur1911@users.noreply.github.com> Date: Fri, 27 Nov 2026 23:07:22 +0530 Subject: [PATCH] fix(mcp): ask before adding the PostHog MCP to coding agents The MCP step of the setup flow wrote a posthog server into every detected coding agent's user-wide config right after the outro, with no question. McpScreen now takes a confirmInstall prop. The setup flow sets it: the screen lists the detected agents and offers Add or Skip (esc also skips) before it touches any config. Skipping completes the step as skipped and still goes on to the AI assistants step when there is one. mcp add leaves the prop off, since running that command is the request. --- .../keyboard-equivalence.test.tsx.snap | 15 ++ .../__tests__/keyboard-equivalence.test.tsx | 20 ++- .../__snapshots__/frames/mcp-120x40.txt | 9 +- .../__snapshots__/frames/mcp-80x28.txt | 10 +- src/tui/__tests__/mcp-screen-outcome.test.tsx | 133 ++++++++++++++++++ src/tui/screen-registry.tsx | 6 +- src/tui/screens/McpScreen.tsx | 67 ++++++++- 7 files changed, 244 insertions(+), 16 deletions(-) diff --git a/e2e-harness/__tests__/__snapshots__/keyboard-equivalence.test.tsx.snap b/e2e-harness/__tests__/__snapshots__/keyboard-equivalence.test.tsx.snap index 04fd8b5b9..6c62c359a 100644 --- a/e2e-harness/__tests__/__snapshots__/keyboard-equivalence.test.tsx.snap +++ b/e2e-harness/__tests__/__snapshots__/keyboard-equivalence.test.tsx.snap @@ -77,6 +77,21 @@ exports[`keyboard commit vs control action commit > mcp: enter on install result } `; +exports[`keyboard commit vs control action commit > mcp: esc on the install question vs set_mcp_outcome skipped 1`] = ` +{ + "action": { + "mcpComplete": true, + "mcpOutcome": "skipped", + "screen": "exit", + }, + "keyboard": { + "mcpComplete": true, + "mcpOutcome": "skipped", + "screen": "exit", + }, +} +`; + exports[`keyboard commit vs control action commit > outro: any key vs dismiss_outro 1`] = ` { "action": { diff --git a/e2e-harness/__tests__/keyboard-equivalence.test.tsx b/e2e-harness/__tests__/keyboard-equivalence.test.tsx index 8dc527df4..d7c0762a4 100644 --- a/e2e-harness/__tests__/keyboard-equivalence.test.tsx +++ b/e2e-harness/__tests__/keyboard-equivalence.test.tsx @@ -193,17 +193,33 @@ const PAIRS: Pair[] = [ name: 'mcp: enter on install results vs set_mcp_outcome installed', program: Program.PostHogIntegration, screen: ScreenId.Mcp, - ready: (_, frame) => frame.includes('Press enter'), + ready: (_, frame) => frame.includes('Add the PostHog MCP to your coding'), arrange: (s) => { confirmed(s); authed(s); ran(s); s.setOutroDismissed(); }, - keys: [ENTER], + // Agree to the install, then leave the results. + keys: [ENTER, ENTER], action: 'set_mcp_outcome', params: { outcome: 'installed', clients: ['Cursor', 'Claude Code'] }, }, + { + name: 'mcp: esc on the install question vs set_mcp_outcome skipped', + program: Program.PostHogIntegration, + screen: ScreenId.Mcp, + ready: (_, frame) => frame.includes('Add the PostHog MCP to your coding'), + arrange: (s) => { + confirmed(s); + authed(s); + ran(s); + s.setOutroDismissed(); + }, + keys: [ESC], + action: 'set_mcp_outcome', + params: { outcome: 'skipped' }, + }, { name: 'slack-connect: skip vs dismiss_slack', // Only `wizard slack` shows this screen now that no program flow does. diff --git a/src/tui/__tests__/__snapshots__/frames/mcp-120x40.txt b/src/tui/__tests__/__snapshots__/frames/mcp-120x40.txt index 7bccaa1f6..a8de33099 100644 --- a/src/tui/__tests__/__snapshots__/frames/mcp-120x40.txt +++ b/src/tui/__tests__/__snapshots__/frames/mcp-120x40.txt @@ -2,9 +2,10 @@ Cost (running): $0.00 · no agent turns yet Ctrl+T to hide - Adding the PostHog MCP to your coding agents + Add the PostHog MCP to your coding agents? - Found 2 coding agents. Setting each one up on this computer. + Found 2 coding agents: Claude Code, Cursor. + Adding it writes a posthog server to each agent's own config, outside this project. - ⠋ Claude Code plugin + MCP Installing… - · Cursor MCP Waiting + ▸ Add the PostHog MCP + Skip diff --git a/src/tui/__tests__/__snapshots__/frames/mcp-80x28.txt b/src/tui/__tests__/__snapshots__/frames/mcp-80x28.txt index caf9b0f3c..b5c182c26 100644 --- a/src/tui/__tests__/__snapshots__/frames/mcp-80x28.txt +++ b/src/tui/__tests__/__snapshots__/frames/mcp-80x28.txt @@ -2,9 +2,11 @@ Cost (running): $0.00 · no agent turns yet Ctrl+T to hide - Adding the PostHog MCP to your coding agents + Add the PostHog MCP to your coding agents? - Found 2 coding agents. Setting each one up on this computer. + Found 2 coding agents: Claude Code, Cursor. + Adding it writes a posthog server to each agent's own config, outside this + project. - ⠋ Claude Code plugin + MCP Installing… - · Cursor MCP Waiting + ▸ Add the PostHog MCP + Skip diff --git a/src/tui/__tests__/mcp-screen-outcome.test.tsx b/src/tui/__tests__/mcp-screen-outcome.test.tsx index 32019a229..dcd234478 100644 --- a/src/tui/__tests__/mcp-screen-outcome.test.tsx +++ b/src/tui/__tests__/mcp-screen-outcome.test.tsx @@ -485,3 +485,136 @@ it('reports a failed detection once, as its error shows, before Enter', async () ]); expect(store.mcpComplete).toBe(false); }); + +it('asks before installing when confirmInstall is set, and writes nothing until the user agrees', async () => { + const store = new WizardStore(Tool.McpAdd); + store.session = buildSession({ installDir: '/app' }); + const install = vi.fn((names) => + Promise.resolve( + names.map((name) => ({ name, status: McpClientStatus.Changed })), + ), + ); + const { stdin, lastFrame } = render( + , + ); + await vi.waitFor(() => + expect(lastFrame()).toContain('Add the PostHog MCP to your coding agents?'), + ); + expect(lastFrame()).toContain('Found 1 coding agent: Cursor.'); + await tick(); + expect(install).not.toHaveBeenCalled(); + expect(mcpCompleteCalls()).toEqual([]); + + // "Add the PostHog MCP" is the first option. + stdin.write(ENTER); + await vi.waitFor(() => expect(lastFrame()).toContain('Press enter')); + expect(install).toHaveBeenCalledWith( + ['Cursor'], + expect.anything(), + undefined, + ); + expect(lastFrame()).toContain('The PostHog MCP is ready in 1 coding agent'); +}); + +it('installs nothing when the user skips the confirmation', async () => { + const store = new WizardStore(Tool.McpAdd); + store.session = buildSession({ installDir: '/app' }); + const install = vi.fn(() => Promise.resolve([])); + const { stdin, lastFrame } = render( + , + ); + await vi.waitFor(() => expect(lastFrame()).toContain('Skip')); + await tick(); + for (const key of [DOWN, ENTER]) { + stdin.write(key); + await tick(); + } + await vi.waitFor(() => expect(store.mcpComplete).toBe(true)); + expect(store.mcpOutcome).toBe(McpOutcome.Skipped); + expect(install).not.toHaveBeenCalled(); + expect(mcpCompleteCalls()).toEqual([ + [ + 'mcp complete', + expect.objectContaining({ + mcp_outcome: McpOutcome.Skipped, + mcp_installed_clients: [], + }), + ], + ]); +}); + +it('installs nothing on esc from the confirmation', async () => { + const store = new WizardStore(Tool.McpAdd); + store.session = buildSession({ installDir: '/app' }); + const install = vi.fn(() => Promise.resolve([])); + const { stdin, lastFrame } = render( + , + ); + await vi.waitFor(() => + expect(lastFrame()).toContain('Add the PostHog MCP to your coding agents?'), + ); + await tick(); + stdin.write(ESC); + await vi.waitFor(() => expect(store.mcpComplete).toBe(true)); + expect(store.mcpOutcome).toBe(McpOutcome.Skipped); + expect(install).not.toHaveBeenCalled(); +}); + +it('still offers the AI assistants after the coding-agent install is skipped', async () => { + const store = new WizardStore(Tool.McpAdd); + store.session = buildSession({ installDir: '/app' }); + const detectClients = () => + Promise.resolve([ + { + name: 'Cursor', + kind: 'local' as const, + supportsPlugin: false, + pluginBundlesMcp: false, + }, + { + name: 'Example Web App', + kind: 'account' as const, + supportsPlugin: false, + pluginBundlesMcp: false, + finish: { + url: 'https://example.com/connectors/posthog', + instruction: 'Sign in and click "Connect" to finish.', + }, + }, + ]); + const install = vi.fn(() => Promise.resolve([])); + const { stdin, lastFrame } = render( + , + ); + await vi.waitFor(() => expect(lastFrame()).toContain('Skip')); + await tick(); + for (const key of [DOWN, ENTER]) { + stdin.write(key); + await tick(); + } + await vi.waitFor(() => expect(lastFrame()).toContain('Step 2 of 2')); + expect(install).not.toHaveBeenCalled(); + + await tick(); + stdin.write(ESC); + await vi.waitFor(() => expect(store.mcpComplete).toBe(true)); + expect(store.mcpOutcome).toBe(McpOutcome.Skipped); + expect(store.mcpInstalledClients).toEqual([]); + expect(install).not.toHaveBeenCalled(); +}); diff --git a/src/tui/screen-registry.tsx b/src/tui/screen-registry.tsx index 2eb4c5951..cc21aaf51 100644 --- a/src/tui/screen-registry.tsx +++ b/src/tui/screen-registry.tsx @@ -93,7 +93,11 @@ export function createScreens( [ScreenId.ProjectPicker]: , [ScreenId.Run]: , [ScreenId.Mcp]: ( - + ), [ScreenId.SlackConnect]: , [ScreenId.Outro]: , diff --git a/src/tui/screens/McpScreen.tsx b/src/tui/screens/McpScreen.tsx index 834dd488b..8c743ff0b 100644 --- a/src/tui/screens/McpScreen.tsx +++ b/src/tui/screens/McpScreen.tsx @@ -5,8 +5,9 @@ * importing business logic directly. Testable, no dynamic imports. * * Supports two modes via the `mode` prop: - * - 'install': detect clients → install in every local client → results - * → open the connector page of each account client the user picks + * - 'install': detect clients → (with `confirmInstall`, ask first) → install + * in every local client → results → open the connector page of each + * account client the user picks * - 'remove': detect installed local clients → pick which (esc cancels) → remove * * A successful install ends on one screen: a row per client, any login @@ -50,6 +51,12 @@ interface McpScreenProps { samplePrompts?: string[]; /** The last menu item, which leaves the screen. `mcp add` says Exit. */ exitLabel?: string; + /** + * Ask before writing to any coding agent's user-wide config. The setup flow + * sets it: there the install is a side step the user didn't ask for. `mcp + * add` leaves it off, since running that command is the request. + */ + confirmInstall?: boolean; } enum Phase { @@ -309,6 +316,7 @@ export const McpScreen = ({ mode = 'install', samplePrompts = [], exitLabel = 'Continue', + confirmInstall = false, }: McpScreenProps) => { useSyncExternalStore( (cb) => store.subscribe(cb), @@ -385,6 +393,9 @@ export const McpScreen = ({ } else if (local.length === 0) { setClients(detected); setPhase(Phase.Assistants); + } else if (confirmInstall) { + setClients(detected); + setPhase(Phase.Ask); } else { setClients(detected); void installAll(local); @@ -607,7 +618,23 @@ export const McpScreen = ({ : [], ); - // The remove picker has no escape binding of its own; esc removes nothing. + /** The user declined the install: write nothing, and go on to the assistants. */ + const declineInstall = () => { + if (accountClients.length === 0) { + markDone(store, McpOutcome.Skipped); + return; + } + stepOne.current = { + outcome: McpOutcome.Skipped, + ready: [], + features: reportFeatures(store.mcpFeatures ?? [...ALL_FEATURE_VALUES]), + logins: [], + }; + setPhase(Phase.Assistants); + }; + + // The pickers have no escape binding of their own; esc removes or installs + // nothing. useKeyBindings( 'mcp-remove-cancel', phase === Phase.Ask @@ -616,7 +643,9 @@ export const McpScreen = ({ match: KeyMatch.Escape, label: 'esc', action: 'cancel', - handler: () => markDone(store, McpOutcome.Skipped), + handler: isRemove + ? () => markDone(store, McpOutcome.Skipped) + : declineInstall, }, ] : [], @@ -660,6 +689,8 @@ export const McpScreen = ({ const heading = isRemove ? 'Remove the PostHog MCP' + : phase === Phase.Ask + ? 'Add the PostHog MCP to your coding agents?' : phase === Phase.Assistants ? 'Connect PostHog to your AI assistants' : phase !== Phase.Done @@ -742,7 +773,33 @@ export const McpScreen = ({ ))} - {phase === Phase.Ask && ( + {phase === Phase.Ask && !isRemove && ( + + + + Found {plural(localClients.length, 'coding agent')}:{' '} + {localClients.map((c) => c.name).join(', ')}. + + + Adding it writes a posthog server to each agent's own + config, outside this project. + + + { + const picked = Array.isArray(value) ? value[0] : value; + if (picked === 'install') void installAll(localClients); + else declineInstall(); + }} + /> + + )} + + {phase === Phase.Ask && isRemove && (