Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | /** * MCP Process Detection Module * * Detects running MCP stdio processes spawned by AI clients. * This helps verify that MCP configuration is not just present, * but actually functional with processes running. * * @packageDocumentation */ import { exec } from "node:child_process"; import { promisify } from "node:util"; const execAsync = promisify(exec); /** * Running MCP process information */ export interface MCPProcess { /** Process ID */ pid: number; /** Command being executed */ command: string; /** MCP server name (e.g., "snapback", "context7") */ serverName: string; /** Whether this is a SnapBack MCP process */ isSnapback: boolean; } /** * MCP process health status */ export interface MCPProcessHealth { /** All detected MCP stdio processes */ allProcesses: MCPProcess[]; /** SnapBack-specific processes */ snapbackProcesses: MCPProcess[]; /** Whether any SnapBack MCP process is running */ snapbackRunning: boolean; /** Total count of all MCP processes */ totalCount: number; /** Timestamp of check */ checkedAt: Date; } /** * Detect running MCP stdio processes * * Uses `ps` command to find processes matching MCP stdio patterns: * - node ... mcp --stdio * - snapback mcp --stdio * - uvx mcp-server-* * - npx ... mcp ... * * @returns Health status of MCP processes * * @example * ```ts * const health = await detectMCPProcesses(); * if (health.snapbackRunning) { * console.log(`✓ SnapBack MCP running (${health.snapbackProcesses.length} processes)`); * } else { * console.log('⚠ SnapBack MCP not running'); * } * ``` */ export async function detectMCPProcesses(): Promise<MCPProcessHealth> { try { // Use ps to find MCP-related processes // grep for common patterns: stdio, mcp, server names const { stdout } = await execAsync( 'ps aux | grep -E "(mcp.*--stdio|--stdio.*mcp|uvx.*mcp-server|npx.*mcp|node.*mcp)" | grep -v grep', { timeout: 5000, // 5s timeout }, ); const processes = parseProcessOutput(stdout); const snapbackProcesses = processes.filter((p) => p.isSnapback); return { allProcesses: processes, snapbackProcesses, snapbackRunning: snapbackProcesses.length > 0, totalCount: processes.length, checkedAt: new Date(), }; } catch { // ps command failed or no processes found (grep returns exit code 1) // Return empty result rather than throwing return { allProcesses: [], snapbackProcesses: [], snapbackRunning: false, totalCount: 0, checkedAt: new Date(), }; } } /** * Parse ps output into MCPProcess objects */ function parseProcessOutput(output: string): MCPProcess[] { const lines = output .trim() .split("\n") .filter((line) => line.trim()); const processes: MCPProcess[] = []; for (const line of lines) { // ps aux output format: USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND // We need PID and COMMAND const match = line.match(/^(\S+)\s+(\d+)\s+.*?\s+(.+)$/); if (!match) { continue; } const [, , pidStr, command] = match; const pid = Number.parseInt(pidStr, 10); if (Number.isNaN(pid)) { continue; } // Determine server name from command const serverName = extractServerName(command); const isSnapback = command.toLowerCase().includes("snapback"); processes.push({ pid, command: command.trim(), serverName, isSnapback, }); } return processes; } /** * Extract MCP server name from command string */ function extractServerName(command: string): string { // Try to extract server name from common patterns const patterns = [ // snapback mcp --stdio /snapback[/\s]/i, // mcp-server-fetch /mcp-server-(\w+)/, // @modelcontextprotocol/server-sequential-thinking /server-(\w+)/, // context7-mcp /(\w+)-mcp/, // fly mcp server /fly.*mcp/i, // supabase-mcp /supabase-mcp/i, ]; for (const pattern of patterns) { const match = command.match(pattern); if (match) { // Return first capture group or full match return match[1] || match[0]; } } // Fallback: just say "mcp" return "mcp"; } /** * Quick check if SnapBack MCP is running * * Lightweight version that just returns boolean. * * @returns true if SnapBack MCP process detected */ export async function isSnapbackMCPRunning(): Promise<boolean> { const health = await detectMCPProcesses(); return health.snapbackRunning; } |