UNPKG

sqlite-mcp-server

Version:

SQLite MCP Server - A Model Context Protocol server for SQLite database operations

147 lines (120 loc) 4.34 kB
#!/usr/bin/env node const { spawn } = require('child_process'); const path = require('path'); const fs = require('fs'); // 获取Python脚本的路径 const pythonScriptPath = path.join(__dirname, '..', 'python', 'sqlite.py'); const requirementsPath = path.join(__dirname, '..', 'python', 'requirements.txt'); // 检查Python是否可用 function checkPython() { return new Promise((resolve, reject) => { const python = spawn('python3', ['--version']); python.on('close', (code) => { if (code === 0) { resolve('python3'); } else { const python2 = spawn('python', ['--version']); python2.on('close', (code2) => { if (code2 === 0) { resolve('python'); } else { reject(new Error('Python not found. Please install Python 3.8 or higher.')); } }); } }); }); } // 检查并安装Python依赖 function checkDependencies() { return new Promise((resolve, reject) => { if (!fs.existsSync(requirementsPath)) { console.log('Requirements file not found, skipping dependency check.'); resolve(); return; } const pip = spawn('pip', ['install', '-r', requirementsPath]); pip.stdout.on('data', (data) => { console.log(`Installing dependencies: ${data}`); }); pip.stderr.on('data', (data) => { console.error(`Dependency installation warning: ${data}`); }); pip.on('close', (code) => { if (code === 0) { console.log('Dependencies installed successfully.'); resolve(); } else { console.warn('Some dependencies may not have been installed correctly, but continuing...'); resolve(); // 继续执行,不阻止启动 } }); }); } // 启动Python MCP服务器 async function startServer() { try { const pythonCmd = await checkPython(); console.log(`Using Python: ${pythonCmd}`); // 检查依赖(非阻塞) await checkDependencies().catch(err => { console.warn('Dependency check failed:', err.message); }); // 传递命令行参数给Python脚本 const args = process.argv.slice(2); console.log(`Starting SQLite MCP Server...`); console.log(`Script path: ${pythonScriptPath}`); console.log(`Arguments: ${args.join(' ')}`); const pythonProcess = spawn(pythonCmd, [pythonScriptPath, ...args], { stdio: 'inherit', cwd: path.dirname(pythonScriptPath) }); pythonProcess.on('error', (err) => { console.error('Failed to start Python process:', err); process.exit(1); }); pythonProcess.on('close', (code) => { console.log(`SQLite MCP Server exited with code ${code}`); process.exit(code); }); // 处理进程信号 process.on('SIGINT', () => { console.log('\nReceived SIGINT, shutting down gracefully...'); pythonProcess.kill('SIGINT'); }); process.on('SIGTERM', () => { console.log('\nReceived SIGTERM, shutting down gracefully...'); pythonProcess.kill('SIGTERM'); }); } catch (error) { console.error('Error starting server:', error.message); console.error('\nPlease ensure Python 3.8+ is installed and available in your PATH.'); process.exit(1); } } // 显示帮助信息 function showHelp() { console.log(` SQLite MCP Server v1.0.0 Usage: npx sqlite-mcp-server [options] Options: --db_path <path> SQLite database path (default: test.db) --help, -h Show this help message Examples: npx sqlite-mcp-server npx sqlite-mcp-server --db_path ./my_database.db For more information, visit: https://github.com/yourusername/sqlite-mcp-server `); } // 主函数 function main() { const args = process.argv.slice(2); if (args.includes('--help') || args.includes('-h')) { showHelp(); return; } startServer(); } if (require.main === module) { main(); }