mirror of
https://github.com/executeautomation/mcp-database-server.git
synced 2025-12-09 21:12:57 +08:00
Added support for SQL Server
This commit is contained in:
75
src/index.ts
75
src/index.ts
@@ -10,7 +10,7 @@ import {
|
||||
} from "@modelcontextprotocol/sdk/types.js";
|
||||
|
||||
// Import database utils
|
||||
import { initDatabase, closeDatabase } from './db/index.js';
|
||||
import { initDatabase, closeDatabase, getDatabaseMetadata } from './db/index.js';
|
||||
|
||||
// Import handlers
|
||||
import { handleListResources, handleReadResource } from './handlers/resourceHandlers.js';
|
||||
@@ -33,11 +33,52 @@ const server = new Server(
|
||||
// Parse command line arguments
|
||||
const args = process.argv.slice(2);
|
||||
if (args.length === 0) {
|
||||
console.error("Please provide a database file path as a command-line argument");
|
||||
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>]");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const databasePath = args[0];
|
||||
// Parse arguments to determine database type and connection info
|
||||
let dbType = 'sqlite';
|
||||
let connectionInfo: any = null;
|
||||
|
||||
// Check if using SQL Server
|
||||
if (args.includes('--sqlserver')) {
|
||||
dbType = 'sqlserver';
|
||||
connectionInfo = {
|
||||
server: '',
|
||||
database: '',
|
||||
user: undefined,
|
||||
password: undefined
|
||||
};
|
||||
|
||||
// Parse SQL Server connection parameters
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i] === '--server' && i + 1 < args.length) {
|
||||
connectionInfo.server = args[i + 1];
|
||||
} else if (args[i] === '--database' && i + 1 < args.length) {
|
||||
connectionInfo.database = args[i + 1];
|
||||
} else if (args[i] === '--user' && i + 1 < args.length) {
|
||||
connectionInfo.user = args[i + 1];
|
||||
} else if (args[i] === '--password' && i + 1 < args.length) {
|
||||
connectionInfo.password = args[i + 1];
|
||||
} else if (args[i] === '--port' && i + 1 < args.length) {
|
||||
connectionInfo.port = parseInt(args[i + 1], 10);
|
||||
}
|
||||
}
|
||||
|
||||
// Validate SQL Server connection info
|
||||
if (!connectionInfo.server || !connectionInfo.database) {
|
||||
console.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}`);
|
||||
}
|
||||
|
||||
// Set up request handlers
|
||||
server.setRequestHandler(ListResourcesRequestSchema, async () => {
|
||||
@@ -69,13 +110,32 @@ process.on('SIGTERM', async () => {
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
// Add global error handler
|
||||
process.on('uncaughtException', (error) => {
|
||||
console.error('Uncaught exception:', error);
|
||||
});
|
||||
|
||||
process.on('unhandledRejection', (reason, promise) => {
|
||||
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
|
||||
});
|
||||
|
||||
/**
|
||||
* Start the server
|
||||
*/
|
||||
async function runServer() {
|
||||
try {
|
||||
console.log(`Initializing database: ${databasePath}`);
|
||||
await initDatabase(databasePath);
|
||||
console.log(`Initializing ${dbType} database...`);
|
||||
if (dbType === 'sqlite') {
|
||||
console.log(`Database path: ${connectionInfo}`);
|
||||
} else if (dbType === 'sqlserver') {
|
||||
console.log(`Server: ${connectionInfo.server}, Database: ${connectionInfo.database}`);
|
||||
}
|
||||
|
||||
// Initialize the database
|
||||
await initDatabase(connectionInfo, dbType);
|
||||
|
||||
const dbInfo = getDatabaseMetadata();
|
||||
console.log(`Connected to ${dbInfo.name} database`);
|
||||
|
||||
console.log('Starting MCP server...');
|
||||
const transport = new StdioServerTransport();
|
||||
@@ -89,4 +149,7 @@ async function runServer() {
|
||||
}
|
||||
|
||||
// Start the server
|
||||
runServer().catch(console.error);
|
||||
runServer().catch(error => {
|
||||
console.error("Server initialization failed:", error);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user