|
| 1 | +import chalk from 'chalk'; |
| 2 | +import { |
| 3 | + addMCPServerToClientsStep, |
| 4 | + removeMCPServerFromClientsStep, |
| 5 | +} from './steps/add-mcp-server-to-clients'; |
| 6 | +import clack from './utils/clack'; |
| 7 | +import { abort, askForCloudRegion } from './utils/clack-utils'; |
| 8 | +import type { CloudRegion } from './utils/types'; |
| 9 | +import opn from 'opn'; |
| 10 | +import { getCloudUrlFromRegion } from './utils/urls'; |
| 11 | +import { sleep } from './lib/helper-functions'; |
| 12 | + |
| 13 | +export const runMCPInstall = async (options: { |
| 14 | + signup: boolean; |
| 15 | + region?: CloudRegion; |
| 16 | +}) => { |
| 17 | + clack.intro('Installing the PostHog MCP server.'); |
| 18 | + |
| 19 | + await addMCPServerToClientsStep({ |
| 20 | + cloudRegion: options.region, |
| 21 | + askPermission: false, |
| 22 | + }); |
| 23 | + |
| 24 | + clack.outro(`${chalk.green( |
| 25 | + 'You might need to restart your MCP clients to see the changes.', |
| 26 | + )} |
| 27 | +
|
| 28 | +Get started with some prompts like: |
| 29 | +
|
| 30 | + - What feature flags do I have active? |
| 31 | + - Add a new feature flag for our homepage redesign |
| 32 | + - What are my most common errors? |
| 33 | +`); |
| 34 | +}; |
| 35 | + |
| 36 | +export const runMCPRemove = async () => { |
| 37 | + const results = await removeMCPServerFromClientsStep({}); |
| 38 | + |
| 39 | + if (results.length === 0) { |
| 40 | + clack.outro(`No PostHog MCP servers found to remove.`); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + clack.outro(`PostHog MCP server removed from: |
| 45 | + ${results.map((c) => `- ${c}`).join('\n ')} |
| 46 | + |
| 47 | + ${chalk.green( |
| 48 | + 'You might need to restart your MCP clients to see the changes.', |
| 49 | + )}`); |
| 50 | +}; |
| 51 | + |
| 52 | +export const getPersonalApiKey = async (options: { |
| 53 | + region?: CloudRegion; |
| 54 | +}): Promise<string> => { |
| 55 | + const cloudRegion = options.region ?? (await askForCloudRegion()); |
| 56 | + |
| 57 | + const cloudUrl = getCloudUrlFromRegion(cloudRegion); |
| 58 | + |
| 59 | + const urlToOpen = `${cloudUrl}/settings/user-api-keys?preset=mcp_server`; |
| 60 | + |
| 61 | + const spinner = clack.spinner(); |
| 62 | + spinner.start( |
| 63 | + `Opening your project settings so you can get a Personal API key...`, |
| 64 | + ); |
| 65 | + |
| 66 | + await sleep(1500); |
| 67 | + |
| 68 | + spinner.stop( |
| 69 | + `Opened your project settings. If the link didn't open automatically, open the following URL in your browser to get a Personal API key: \n\n${chalk.cyan( |
| 70 | + urlToOpen, |
| 71 | + )}`, |
| 72 | + ); |
| 73 | + |
| 74 | + opn(urlToOpen, { wait: false }).catch(() => { |
| 75 | + // opn throws in environments that don't have a browser (e.g. remote shells) so we just noop here |
| 76 | + }); |
| 77 | + |
| 78 | + const personalApiKey = await clack.password({ |
| 79 | + message: 'Paste in your Personal API key:', |
| 80 | + validate(value) { |
| 81 | + if (value.length === 0) return `Value is required!`; |
| 82 | + |
| 83 | + if (!value.startsWith('phx_')) { |
| 84 | + return `That doesn't look right, are you sure you copied the right key? It should start with 'phx_'`; |
| 85 | + } |
| 86 | + }, |
| 87 | + }); |
| 88 | + |
| 89 | + if (!personalApiKey) { |
| 90 | + await abort('Unable to proceed without a personal API key.'); |
| 91 | + return ''; |
| 92 | + } |
| 93 | + |
| 94 | + return personalApiKey as string; |
| 95 | +}; |
0 commit comments