UNPKG

cpp-enhancement-mcp-server

Version:

MCP server for C++ programming enhancement with modern best practices, performance optimization, and real-time web research. Requires Python 3.8+.

148 lines (126 loc) 4.82 kB
#!/usr/bin/env node /** * Node.js wrapper for C++ Enhancement MCP Server * Ensures Python dependencies are installed and runs the Python server */ const { spawn, execSync } = require('child_process'); const path = require('path'); const fs = require('fs'); function findPython() { const pythonCommands = ['py', 'python3', 'python']; for (const cmd of pythonCommands) { try { const version = execSync(`${cmd} --version`, { encoding: 'utf8', stdio: 'pipe' }); if (version.includes('Python 3.')) { const versionMatch = version.match(/Python (\d+)\.(\d+)/); if (versionMatch) { const major = parseInt(versionMatch[1]); const minor = parseInt(versionMatch[2]); if (major === 3 && minor >= 8) { return cmd; } } } } catch (error) { // Command not found, continue to next } } console.error('Error: Python 3.8+ not found!'); console.error('Please install Python from: https://www.python.org/downloads/'); process.exit(1); } function installMcpIfNeeded(pythonCmd) { try { // Check if MCP is already installed execSync(`${pythonCmd} -c "import mcp"`, { stdio: 'pipe' }); return true; // Already installed } catch (error) { // MCP not installed, try to install it console.error('Installing MCP library...'); try { execSync(`${pythonCmd} -m pip install mcp`, { stdio: 'inherit' }); console.error('MCP library installed successfully.'); return true; } catch (installError) { console.error('Failed to install MCP library. Please install manually:'); console.error(`${pythonCmd} -m pip install mcp`); return false; } } } function installRequestsIfNeeded(pythonCmd) { try { // Check if requests is already installed execSync(`${pythonCmd} -c "import requests"`, { stdio: 'pipe' }); return true; // Already installed } catch (error) { // requests not installed, try to install it console.error('Installing requests library...'); try { execSync(`${pythonCmd} -m pip install requests`, { stdio: 'inherit' }); console.error('Requests library installed successfully.'); return true; } catch (installError) { console.error('Failed to install requests library. Please install manually:'); console.error(`${pythonCmd} -m pip install requests`); return false; } } } function main() { // Find Python const pythonCmd = findPython(); // Install dependencies if needed const mcpInstalled = installMcpIfNeeded(pythonCmd); const requestsInstalled = installRequestsIfNeeded(pythonCmd); if (!mcpInstalled || !requestsInstalled) { process.exit(1); } // Get the path to the Python server script const serverScript = path.join(__dirname, 'cpp_enhancement_server.py'); // Check if the server script exists if (!fs.existsSync(serverScript)) { console.error(`Error: Server script not found at ${serverScript}`); process.exit(1); } // Run the Python server with all arguments passed through const args = process.argv.slice(2); // Remove 'node' and script name // For MCP server mode (no args), use stdio pipes to handle MCP protocol const stdio = args.length === 0 ? ['pipe', 'pipe', 'pipe'] : 'inherit'; const pythonProcess = spawn(pythonCmd, [serverScript, ...args], { stdio: stdio, cwd: __dirname }); // If running as MCP server, pipe stdio if (args.length === 0) { process.stdin.pipe(pythonProcess.stdin); pythonProcess.stdout.pipe(process.stdout); pythonProcess.stderr.pipe(process.stderr); } // Handle process events pythonProcess.on('error', (error) => { console.error(`Failed to start Python server: ${error.message}`); process.exit(1); }); pythonProcess.on('close', (code, signal) => { if (code !== 0) { console.error(`Python server exited with code ${code}, signal ${signal}`); } process.exit(code || 0); }); pythonProcess.on('exit', (code, signal) => { if (code !== 0) { console.error(`Python server process exited with code ${code}, signal ${signal}`); } }); // Handle signals process.on('SIGINT', () => { pythonProcess.kill('SIGINT'); }); process.on('SIGTERM', () => { pythonProcess.kill('SIGTERM'); }); } if (require.main === module) { main(); }