import fs from 'fs/promises'; import path from 'path'; import { execSync } from 'child_process'; import chalk from 'chalk'; export type IDEType = 'cursor' | 'windsurf' | 'vscode' | 'unknown'; /** * Detects the IDE currently in use based on file system markers and active processes. */ export async function detectCurrentIDE(): Promise { const cwd = process.cwd(); // 1. Check for directory markers const markers: Record = { '.cursor': 'cursor', '.windsurf': 'windsurf', '.vscode': 'vscode' }; for (const [marker, type] of Object.entries(markers)) { try { const stats = await fs.stat(path.join(cwd, marker)); if (stats.isDirectory()) return type; } catch { continue; } } // 2. Check for environment variables (some IDEs set these) if (process.env.TERM_PROGRAM === 'vscode') { // Unfortunately Cursor also identifies as vscode here sometimes // We can check for specific Cursor variables if they exist if (process.env.CURSOR_VERSION) return 'cursor'; return 'vscode'; } // 3. Process check (platform dependent) if (process.platform === 'darwin' || process.platform === 'linux') { try { const ps = execSync('ps aux', { stdio: 'pipe' }).toString(); if (ps.includes('Cursor.app') || ps.includes('cursor-server')) return 'cursor'; if (ps.includes('Windsurf.app') || ps.includes('windsurf-server')) return 'windsurf'; } catch { // ps command failed } } return 'unknown'; } /** * Automatically registers the MCP server with the detected IDE. */ export async function autoRegisterMCP(apiUrl: string, apiKey: string): Promise { const ide = await detectCurrentIDE(); if (ide === 'unknown') return false; console.log(chalk.dim(` Detected IDE: ${chalk.cyan(ide)}`)); // Mapping of IDEs to their config paths const home = process.env.HOME || ''; if (!home) return false; let configPath = ''; if (ide === 'cursor') { // macOS Cursor config path configPath = path.join(home, 'Library/Application Support/Cursor/User/globalStorage/pro.db.cursor.mcp/mcp.json'); } else if (ide === 'windsurf') { // macOS Windsurf config path configPath = path.join(home, 'Library/Application Support/Windsurf/User/globalStorage/pro.db.windsurf.mcp/mcp.json'); } if (!configPath) { console.log(chalk.yellow(` ⚠ Auto-registration for ${ide} not yet implemented for this platform.`)); return false; } try { // Ensure directory exists await fs.mkdir(path.dirname(configPath), { recursive: true }); let config: any = { mcpServers: {} }; try { const content = await fs.readFile(configPath, 'utf-8'); config = JSON.parse(content); } catch { // New config } // Add or update Rigstate MCP config.mcpServers = config.mcpServers || {}; config.mcpServers.rigstate = { command: 'npx', args: ['-y', '@rigstate/mcp'], env: { RIGSTATE_API_URL: apiUrl, RIGSTATE_API_KEY: apiKey }, disabled: false }; await fs.writeFile(configPath, JSON.stringify(config, null, 2), 'utf-8'); return true; } catch (error: any) { console.log(chalk.red(` ❌ Failed to register MCP for ${ide}: ${error.message}`)); return false; } }