mirror of
https://github.com/executeautomation/mcp-database-server.git
synced 2025-12-09 21:12:57 +08:00
Fixed the logging format
This commit is contained in:
BIN
src/.DS_Store
vendored
BIN
src/.DS_Store
vendored
Binary file not shown.
@@ -18,13 +18,13 @@ export class SqliteAdapter implements DbAdapter {
|
||||
async init(): Promise<void> {
|
||||
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();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -49,8 +49,11 @@ export class SqlServerAdapter implements DbAdapter {
|
||||
*/
|
||||
async init(): Promise<void> {
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
|
||||
42
src/index.ts
42
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 <database_file_path>");
|
||||
console.error("Usage for SQL Server: node index.js --sqlserver --server <server> --database <database> [--user <user> --password <password>]");
|
||||
logger.error("Please provide database connection information");
|
||||
logger.error("Usage for SQLite: node index.js <database_file_path>");
|
||||
logger.error("Usage for SQL Server: node index.js --sqlserver --server <server> --database <database> [--user <user> --password <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);
|
||||
});
|
||||
Reference in New Issue
Block a user