|
| 1 | +import { DefaultMCPClient } from '../MCPClient'; |
| 2 | +import { DefaultMCPClientConfig, getDefaultServerConfig } from '../defaults'; |
| 3 | +import { z } from 'zod'; |
| 4 | +import { execSync } from 'child_process'; |
| 5 | + |
| 6 | +export const ClaudeCodeMCPConfig = DefaultMCPClientConfig; |
| 7 | + |
| 8 | +export type ClaudeCodeMCPConfig = z.infer<typeof DefaultMCPClientConfig>; |
| 9 | + |
| 10 | +export class ClaudeCodeMCPClient extends DefaultMCPClient { |
| 11 | + name = 'Claude Code'; |
| 12 | + |
| 13 | + constructor() { |
| 14 | + super(); |
| 15 | + } |
| 16 | + |
| 17 | + isClientSupported(): Promise<boolean> { |
| 18 | + try { |
| 19 | + execSync('claude --version', { stdio: 'ignore' }); |
| 20 | + return Promise.resolve(true); |
| 21 | + } catch { |
| 22 | + return Promise.resolve(false); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + isServerInstalled(): Promise<boolean> { |
| 27 | + try { |
| 28 | + // check if posthog in output |
| 29 | + const output = execSync('claude mcp list', { |
| 30 | + stdio: 'pipe', |
| 31 | + }); |
| 32 | + |
| 33 | + if (output.toString().includes('posthog')) { |
| 34 | + return Promise.resolve(true); |
| 35 | + } |
| 36 | + } catch { |
| 37 | + // |
| 38 | + } |
| 39 | + |
| 40 | + return Promise.resolve(false); |
| 41 | + } |
| 42 | + |
| 43 | + getConfigPath(): Promise<string> { |
| 44 | + throw new Error('Not implemented'); |
| 45 | + } |
| 46 | + |
| 47 | + addServer(apiKey: string): Promise<{ success: boolean }> { |
| 48 | + const config = getDefaultServerConfig(apiKey, 'sse'); |
| 49 | + |
| 50 | + const command = `claude mcp add-json posthog -s user '${JSON.stringify( |
| 51 | + config, |
| 52 | + )}'`; |
| 53 | + |
| 54 | + execSync(command); |
| 55 | + |
| 56 | + return Promise.resolve({ success: true }); |
| 57 | + } |
| 58 | + |
| 59 | + removeServer(): Promise<{ success: boolean }> { |
| 60 | + const command = `claude mcp remove --scope user posthog`; |
| 61 | + |
| 62 | + execSync(command); |
| 63 | + |
| 64 | + return Promise.resolve({ success: true }); |
| 65 | + } |
| 66 | +} |
0 commit comments