UNPKG

vibe-coder-mcp

Version:

Production-ready MCP server with complete agent integration, multi-transport support, and comprehensive development automation tools for AI-assisted workflows.

179 lines (178 loc) 6.29 kB
import { pino } from 'pino'; import path from 'path'; import { fileURLToPath } from 'url'; const isDevelopment = process.env.NODE_ENV === 'development'; const isStdioTransport = process.env.MCP_TRANSPORT === 'stdio' || process.argv.includes('--stdio'); const effectiveLogLevel = process.env.LOG_LEVEL || (isDevelopment ? 'debug' : 'info'); const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const logFilePath = path.resolve(__dirname, '../server.log'); const fileDestination = pino.destination(logFilePath); const consoleStream = (isDevelopment && !isStdioTransport) ? process.stdout : process.stderr; const streams = [ { level: effectiveLogLevel, stream: fileDestination }, { level: effectiveLogLevel, stream: consoleStream } ]; const configuredLogger = pino({ level: effectiveLogLevel, redact: { paths: [ 'apiKey', '*.apiKey', 'receivedConfig.apiKey', 'config.apiKey', 'openRouterConfig.apiKey', 'env.OPENROUTER_API_KEY', 'env.PERPLEXITY_API_KEY' ], censor: '[REDACTED]', }, transport: (isDevelopment && !isStdioTransport) ? { target: 'pino-pretty', options: { colorize: true, translateTime: 'SYS:standard', ignore: 'pid,hostname', }, } : undefined, }, pino.multistream(streams)); let shutdownInProgress = false; let loggerDestroyed = false; function createResilientLogger(baseLogger) { return new Proxy(baseLogger, { get(target, prop) { if (loggerDestroyed && typeof prop === 'string' && ['debug', 'info', 'warn', 'error', 'fatal', 'trace'].includes(prop)) { return function (obj, msg) { try { if (typeof obj === 'string') { console.log(`[${prop.toUpperCase()}] ${obj}`); } else if (msg) { console.log(`[${prop.toUpperCase()}] ${msg}`, obj); } else { console.log(`[${prop.toUpperCase()}]`, obj); } } catch { } }; } return target[prop]; } }); } export function shutdownLogger() { if (shutdownInProgress) { return Promise.resolve(); } shutdownInProgress = true; return new Promise((resolve) => { try { configuredLogger.info('Initiating logger shutdown'); if (fileDestination) { const isReady = fileDestination.ready !== false; if (isReady) { try { if (typeof fileDestination.flushSync === 'function') { fileDestination.flushSync(); } } catch (flushError) { console.warn('Warning: Could not flush logger during shutdown:', flushError.message); } } try { if (typeof fileDestination.end === 'function') { fileDestination.end(); } } catch (endError) { console.warn('Warning: Could not end logger stream during shutdown:', endError.message); } } loggerDestroyed = true; setTimeout(() => { resolve(); }, 150); } catch (error) { console.error('Error during logger shutdown:', error); loggerDestroyed = true; resolve(); } }); } const shutdownCallbacks = []; export function registerShutdownCallback(callback) { shutdownCallbacks.push(callback); } async function executeShutdownCallbacks() { for (const callback of shutdownCallbacks) { try { await callback(); } catch (error) { console.error('Error in shutdown callback:', error); } } } export function resetLoggerForTesting() { if (process.env.NODE_ENV !== 'test' && !process.env.VITEST) { console.warn('resetLoggerForTesting() should only be used in test environments'); return; } shutdownInProgress = false; loggerDestroyed = false; } function setupShutdownHandlers() { let shutdownInitiated = false; const handleShutdown = async (signal) => { if (shutdownInitiated) { console.log(`\nForced shutdown on second ${signal}`); process.exit(1); } shutdownInitiated = true; try { console.log(`\nReceived ${signal}, shutting down gracefully...`); await executeShutdownCallbacks(); await shutdownLogger(); console.log('Graceful shutdown completed'); process.exit(0); } catch (error) { console.error('Error during graceful shutdown:', error); process.exit(1); } }; process.on('SIGINT', () => handleShutdown('SIGINT')); process.on('SIGTERM', () => handleShutdown('SIGTERM')); process.on('SIGQUIT', () => handleShutdown('SIGQUIT')); process.on('uncaughtException', async (error) => { console.error('Uncaught Exception:', error); try { await executeShutdownCallbacks(); await shutdownLogger(); } catch (shutdownError) { console.error('Error during emergency shutdown:', shutdownError); } process.exit(1); }); process.on('unhandledRejection', async (reason, promise) => { console.error('Unhandled Rejection at:', promise, 'reason:', reason); try { await executeShutdownCallbacks(); await shutdownLogger(); } catch (shutdownError) { console.error('Error during emergency shutdown:', shutdownError); } process.exit(1); }); } setupShutdownHandlers(); const resilientLogger = createResilientLogger(configuredLogger); export default resilientLogger;