diff --git a/.DS_Store b/.DS_Store index e4b12fa..615e03b 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/src/.DS_Store b/src/.DS_Store index 1c3fbf4..c60d577 100644 Binary files a/src/.DS_Store and b/src/.DS_Store differ diff --git a/src/db/sqlite-adapter.ts b/src/db/sqlite-adapter.ts index ab8edea..2b0b2d9 100644 --- a/src/db/sqlite-adapter.ts +++ b/src/db/sqlite-adapter.ts @@ -18,13 +18,13 @@ export class SqliteAdapter implements DbAdapter { async init(): Promise { return new Promise((resolve, reject) => { // Ensure the dbPath is accessible - console.log(`Opening SQLite database at: ${this.dbPath}`); + console.error(`[INFO] Opening SQLite database at: ${this.dbPath}`); this.db = new sqlite3.Database(this.dbPath, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => { if (err) { - console.error(`SQLite connection error: ${err.message}`); + console.error(`[ERROR] SQLite connection error: ${err.message}`); reject(err); } else { - console.log("SQLite database opened successfully"); + console.error("[INFO] SQLite database opened successfully"); resolve(); } }); diff --git a/src/db/sqlserver-adapter.ts b/src/db/sqlserver-adapter.ts index 3341658..d9d8c8a 100644 --- a/src/db/sqlserver-adapter.ts +++ b/src/db/sqlserver-adapter.ts @@ -49,8 +49,11 @@ export class SqlServerAdapter implements DbAdapter { */ async init(): Promise { try { + console.error(`[INFO] Connecting to SQL Server: ${this.server}, Database: ${this.database}`); this.pool = await new sql.ConnectionPool(this.config).connect(); + console.error(`[INFO] SQL Server connection established successfully`); } catch (err) { + console.error(`[ERROR] SQL Server connection error: ${(err as Error).message}`); throw new Error(`Failed to connect to SQL Server: ${(err as Error).message}`); } } diff --git a/src/index.ts b/src/index.ts index 2cefb33..0fb8084 100644 --- a/src/index.ts +++ b/src/index.ts @@ -16,6 +16,14 @@ import { initDatabase, closeDatabase, getDatabaseMetadata } from './db/index.js' import { handleListResources, handleReadResource } from './handlers/resourceHandlers.js'; import { handleListTools, handleToolCall } from './handlers/toolHandlers.js'; +// Setup a logger that uses stderr instead of stdout to avoid interfering with MCP communications +const logger = { + log: (...args: any[]) => console.error('[INFO]', ...args), + error: (...args: any[]) => console.error('[ERROR]', ...args), + warn: (...args: any[]) => console.error('[WARN]', ...args), + info: (...args: any[]) => console.error('[INFO]', ...args), +}; + // Configure the server const server = new Server( { @@ -33,9 +41,9 @@ const server = new Server( // Parse command line arguments const args = process.argv.slice(2); if (args.length === 0) { - console.error("Please provide database connection information"); - console.error("Usage for SQLite: node index.js "); - console.error("Usage for SQL Server: node index.js --sqlserver --server --database [--user --password ]"); + logger.error("Please provide database connection information"); + logger.error("Usage for SQLite: node index.js "); + logger.error("Usage for SQL Server: node index.js --sqlserver --server --database [--user --password ]"); process.exit(1); } @@ -70,14 +78,14 @@ if (args.includes('--sqlserver')) { // Validate SQL Server connection info if (!connectionInfo.server || !connectionInfo.database) { - console.error("Error: SQL Server requires --server and --database parameters"); + logger.error("Error: SQL Server requires --server and --database parameters"); process.exit(1); } } else { // SQLite mode (default) dbType = 'sqlite'; connectionInfo = args[0]; // First argument is the SQLite file path - console.log(`Using SQLite database at path: ${connectionInfo}`); + logger.info(`Using SQLite database at path: ${connectionInfo}`); } // Set up request handlers @@ -99,24 +107,24 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => { // Handle shutdown gracefully process.on('SIGINT', async () => { - console.log('Shutting down gracefully...'); + logger.info('Shutting down gracefully...'); await closeDatabase(); process.exit(0); }); process.on('SIGTERM', async () => { - console.log('Shutting down gracefully...'); + logger.info('Shutting down gracefully...'); await closeDatabase(); process.exit(0); }); // Add global error handler process.on('uncaughtException', (error) => { - console.error('Uncaught exception:', error); + logger.error('Uncaught exception:', error); }); process.on('unhandledRejection', (reason, promise) => { - console.error('Unhandled Rejection at:', promise, 'reason:', reason); + logger.error('Unhandled Rejection at:', promise, 'reason:', reason); }); /** @@ -124,32 +132,32 @@ process.on('unhandledRejection', (reason, promise) => { */ async function runServer() { try { - console.log(`Initializing ${dbType} database...`); + logger.info(`Initializing ${dbType} database...`); if (dbType === 'sqlite') { - console.log(`Database path: ${connectionInfo}`); + logger.info(`Database path: ${connectionInfo}`); } else if (dbType === 'sqlserver') { - console.log(`Server: ${connectionInfo.server}, Database: ${connectionInfo.database}`); + logger.info(`Server: ${connectionInfo.server}, Database: ${connectionInfo.database}`); } // Initialize the database await initDatabase(connectionInfo, dbType); const dbInfo = getDatabaseMetadata(); - console.log(`Connected to ${dbInfo.name} database`); + logger.info(`Connected to ${dbInfo.name} database`); - console.log('Starting MCP server...'); + logger.info('Starting MCP server...'); const transport = new StdioServerTransport(); await server.connect(transport); - console.log('Server running. Press Ctrl+C to exit.'); + logger.info('Server running. Press Ctrl+C to exit.'); } catch (error) { - console.error("Failed to initialize:", error); + logger.error("Failed to initialize:", error); process.exit(1); } } // Start the server runServer().catch(error => { - console.error("Server initialization failed:", error); + logger.error("Server initialization failed:", error); process.exit(1); }); \ No newline at end of file