sqlite-mcp-server
Version:
SQLite MCP Server - A Model Context Protocol server for SQLite database operations
125 lines (107 loc) • 4.17 kB
JavaScript
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
const requirementsPath = path.join(__dirname, '..', 'python', 'requirements.txt');
console.log('SQLite MCP Server - Post-install setup');
console.log('=====================================');
// 检查Python是否可用
function checkPython() {
return new Promise((resolve, reject) => {
console.log('Checking Python installation...');
const python3 = spawn('python3', ['--version']);
python3.on('close', (code) => {
if (code === 0) {
console.log('✓ Python3 found');
resolve('python3');
} else {
console.log('Python3 not found, trying python...');
const python = spawn('python', ['--version']);
python.on('close', (code2) => {
if (code2 === 0) {
console.log('✓ Python found');
resolve('python');
} else {
console.log('✗ Python not found');
reject(new Error('Python not found'));
}
});
}
});
});
}
// 检查pip是否可用
function checkPip() {
return new Promise((resolve, reject) => {
console.log('Checking pip installation...');
const pip = spawn('pip', ['--version']);
pip.on('close', (code) => {
if (code === 0) {
console.log('✓ pip found');
resolve('pip');
} else {
console.log('pip not found, trying pip3...');
const pip3 = spawn('pip3', ['--version']);
pip3.on('close', (code2) => {
if (code2 === 0) {
console.log('✓ pip3 found');
resolve('pip3');
} else {
console.log('✗ pip not found');
reject(new Error('pip not found'));
}
});
}
});
});
}
// 安装Python依赖
function installDependencies(pipCmd) {
return new Promise((resolve, reject) => {
if (!fs.existsSync(requirementsPath)) {
console.log('Requirements file not found, skipping dependency installation.');
resolve();
return;
}
console.log('Installing Python dependencies...');
console.log(`Using: ${pipCmd} install -r ${requirementsPath}`);
const pip = spawn(pipCmd, ['install', '-r', requirementsPath]);
pip.stdout.on('data', (data) => {
process.stdout.write(data);
});
pip.stderr.on('data', (data) => {
process.stderr.write(data);
});
pip.on('close', (code) => {
if (code === 0) {
console.log('✓ Python dependencies installed successfully');
resolve();
} else {
console.log('✗ Failed to install some Python dependencies');
reject(new Error(`pip install failed with code ${code}`));
}
});
});
}
// 主安装流程
async function main() {
try {
await checkPython();
const pipCmd = await checkPip();
await installDependencies(pipCmd);
console.log('\n✓ Setup completed successfully!');
console.log('\nYou can now run: npx sqlite-mcp-server');
console.log('Or with custom database: npx sqlite-mcp-server --db_path ./my_db.sqlite');
} catch (error) {
console.log('\n⚠️ Setup completed with warnings:');
console.log(error.message);
console.log('\nYou may need to manually install Python dependencies:');
console.log('pip install -r python/requirements.txt');
console.log('\nThe package will still work if dependencies are installed manually.');
// 不要因为依赖安装失败而导致npm install失败
process.exit(0);
}
}
if (require.main === module) {
main();
}