| 70 | } |
| 71 | |
| 72 | writeLog(logLevel: MCPLogLevel, mcpServer: McpServer | null, message: string, context?: unknown) { |
| 73 | const isServerConnected = mcpServer?.isConnected(); |
| 74 | const isStdIOMode = this.transportType === 'stdio'; |
| 75 | |
| 76 | // Send to server notifications when connected |
| 77 | if (isServerConnected) { |
| 78 | const params: LoggingMessageNotification['params'] = { |
| 79 | level: logLevel, |
| 80 | data: `[${this.serviceName}] ${message}`, |
| 81 | }; |
| 82 | |
| 83 | try { |
| 84 | mcpServer!.server.sendLoggingMessage(params); |
| 85 | } catch { |
| 86 | // Fall through to console fallback if notification fails |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (this.shouldServerLog(logLevel)) { |
| 91 | const logLine = this.formatLogEntry(logLevel, message, context); |
| 92 | // Console output handling based on mode and connection status |
| 93 | if (isStdIOMode) { |
| 94 | process.stderr.write(logLine + '\n'); |
| 95 | } else { |
| 96 | // HTTP mode: write to stderr for errors, stdout for other log levels |
| 97 | if (['warning', 'error', 'critical', 'alert', 'emergency'].includes(logLevel)) { |
| 98 | process.stderr.write(logLine + '\n'); |
| 99 | } else { |
| 100 | process.stdout.write(logLine + '\n'); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Tier 1 convenience methods |
| 107 | debug(mcpServer: McpServer | null, message: string, context?: unknown) { |