Refactor error handling and logging to use process.stderr for better output management. Disable color codes in JSON parsing to prevent issues. Update logger configuration for improved clarity in log outputs.
This commit is contained in:
+4
-4
@@ -70,13 +70,13 @@ async function main() {
|
||||
await runServer();
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error starting MCP server:', error);
|
||||
process.stderr.write(`Error starting MCP server: ${error}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
function showHelp() {
|
||||
console.log(`
|
||||
process.stderr.write(`
|
||||
Excalidraw MCP Server
|
||||
|
||||
Usage:
|
||||
@@ -94,10 +94,10 @@ function showHelp() {
|
||||
npx excalidraw-mcp --port 4000
|
||||
npx excalidraw-mcp --mode http
|
||||
npx excalidraw-mcp --debug
|
||||
`);
|
||||
\n`);
|
||||
}
|
||||
|
||||
main().catch(error => {
|
||||
console.error('Fatal error:', error);
|
||||
process.stderr.write(`Fatal error: ${error}\n`);
|
||||
process.exit(1);
|
||||
});
|
||||
+7
-10
@@ -1,3 +1,7 @@
|
||||
// Disable colors to prevent ANSI color codes from breaking JSON parsing
|
||||
process.env.NODE_DISABLE_COLORS = '1';
|
||||
process.env.NO_COLOR = '1';
|
||||
|
||||
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
||||
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
||||
import {
|
||||
@@ -716,7 +720,7 @@ async function runServer() {
|
||||
process.stdin.resume();
|
||||
} catch (error) {
|
||||
logger.error('Error starting server:', error);
|
||||
console.error('Failed to start MCP server:', error.message, error.stack);
|
||||
process.stderr.write(`Failed to start MCP server: ${error.message}\n${error.stack}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
@@ -724,23 +728,16 @@ async function runServer() {
|
||||
// Add global error handlers
|
||||
process.on('uncaughtException', (error) => {
|
||||
logger.error('Uncaught exception:', error);
|
||||
console.error('UNCAUGHT EXCEPTION:', error.message, error.stack);
|
||||
// Don't exit immediately to allow logging
|
||||
process.stderr.write(`UNCAUGHT EXCEPTION: ${error.message}\n${error.stack}\n`);
|
||||
setTimeout(() => process.exit(1), 1000);
|
||||
});
|
||||
|
||||
process.on('unhandledRejection', (reason, promise) => {
|
||||
logger.error('Unhandled promise rejection:', reason);
|
||||
console.error('UNHANDLED REJECTION:', reason);
|
||||
// Don't exit immediately to allow logging
|
||||
process.stderr.write(`UNHANDLED REJECTION: ${reason}\n`);
|
||||
setTimeout(() => process.exit(1), 1000);
|
||||
});
|
||||
|
||||
// Only run the server directly if this file is executed directly (not imported)
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
runServer();
|
||||
}
|
||||
|
||||
// For testing and debugging purposes
|
||||
if (process.env.DEBUG === 'true') {
|
||||
logger.debug('Debug mode enabled');
|
||||
|
||||
+3
-3
@@ -1,9 +1,9 @@
|
||||
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
||||
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
||||
|
||||
console.log('MCP SDK imports successful');
|
||||
console.log('Server:', Server);
|
||||
console.log('StdioServerTransport:', StdioServerTransport);
|
||||
process.stderr.write('MCP SDK imports successful\n');
|
||||
process.stderr.write(`Server: ${Server}\n`);
|
||||
process.stderr.write(`StdioServerTransport: ${StdioServerTransport}\n`);
|
||||
|
||||
// Exit gracefully
|
||||
process.exit(0);
|
||||
+4
-1
@@ -4,12 +4,15 @@ const logger = winston.createLogger({
|
||||
level: process.env.LOG_LEVEL || 'info',
|
||||
format: winston.format.combine(
|
||||
winston.format.timestamp(),
|
||||
winston.format.uncolorize(),
|
||||
winston.format.json()
|
||||
),
|
||||
transports: [
|
||||
new winston.transports.Console({
|
||||
stderrLevels: ['error', 'warn', 'info', 'debug', 'silly'],
|
||||
format: winston.format.combine(
|
||||
winston.format.colorize(),
|
||||
winston.format.timestamp(),
|
||||
winston.format.uncolorize(),
|
||||
winston.format.simple()
|
||||
)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user