@sqlx-mcp/sqlx-mcp
Version:
Rust MCP server for SQLx database management
181 lines (154 loc) โข 6.54 kB
JavaScript
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
function getPlatformPackage() {
const platform = process.platform;
const arch = process.arch;
let platformName;
if (platform === 'win32' && arch === 'x64') {
platformName = 'windows-x64';
} else if (platform === 'linux' && arch === 'x64') {
platformName = 'linux-x64';
} else if (platform === 'darwin' && arch === 'x64') {
platformName = 'macos-x64';
} else if (platform === 'darwin' && arch === 'arm64') {
platformName = 'macos-arm64';
} else {
throw new Error(`Unsupported platform: ${platform}-${arch}`);
}
return `@sqlx-mcp/${platformName}`;
}
function getBinaryPath() {
console.log('๐ Debug: Starting binary path detection...');
console.log(`๐ Debug: Platform: ${process.platform}, Arch: ${process.arch}`);
try {
// ่ทๅๅนณๅฐ็นๅฎ็ๅ
ๅ
const platformPackageName = getPlatformPackage();
console.log(`๐ Debug: Expected platform package: ${platformPackageName}`);
// ๅฐ่ฏๅ ่ฝฝๅนณๅฐ็นๅฎ็ๅ
try {
console.log(`๐ Debug: Attempting to require('${platformPackageName}')...`);
const platformPackage = require(platformPackageName);
console.log(`๐ Debug: Platform package loaded successfully`);
const binaryPath = platformPackage.getBinaryPath();
console.log(`๐ Debug: Binary path from platform package: ${binaryPath}`);
if (fs.existsSync(binaryPath)) {
console.log(`๐ Debug: Binary file exists at ${binaryPath}`);
return binaryPath;
} else {
console.log(`๐ Debug: Binary file NOT found at ${binaryPath}`);
throw new Error(`Binary not found at ${binaryPath}`);
}
} catch (requireError) {
console.log(`๐ Debug: Failed to require platform package: ${requireError.message}`);
console.log(`๐ Debug: Error code: ${requireError.code}`);
// ๆฃๆฅ node_modules ไธญๆฏๅฆๅญๅจๅนณๅฐๅ
const nodeModulesPath = path.join(__dirname, 'node_modules', platformPackageName.replace('/', path.sep));
console.log(`๐ Debug: Checking for platform package at: ${nodeModulesPath}`);
if (fs.existsSync(nodeModulesPath)) {
console.log(`๐ Debug: Platform package directory exists`);
const packageJsonPath = path.join(nodeModulesPath, 'package.json');
if (fs.existsSync(packageJsonPath)) {
console.log(`๐ Debug: package.json exists in platform package`);
const indexJsPath = path.join(nodeModulesPath, 'index.js');
if (fs.existsSync(indexJsPath)) {
console.log(`๐ Debug: index.js exists in platform package`);
} else {
console.log(`๐ Debug: index.js NOT found in platform package`);
}
} else {
console.log(`๐ Debug: package.json NOT found in platform package`);
}
} else {
console.log(`๐ Debug: Platform package directory does NOT exist`);
}
throw requireError;
}
} catch (error) {
console.log(`๐ Debug: Platform package approach failed, trying fallback...`);
// Fallback: ๅฐ่ฏไปๆฌๅฐๆๅปบ็ไบ่ฟๅถๆไปถ
const localBinaryPath = path.join(__dirname, 'target', 'release',
process.platform === 'win32' ? 'sqlx-mcp.exe' : 'sqlx-mcp');
console.log(`๐ Debug: Checking local binary at: ${localBinaryPath}`);
if (fs.existsSync(localBinaryPath)) {
console.log('๐ Debug: Using local build binary');
return localBinaryPath;
} else {
console.log(`๐ Debug: Local binary NOT found at ${localBinaryPath}`);
}
// ๅๅบๅฝๅ็ฎๅฝๅ
ๅฎน็จไบ่ฐ่ฏ
console.log(`๐ Debug: Current directory (__dirname): ${__dirname}`);
console.log(`๐ Debug: Directory contents:`);
try {
const files = fs.readdirSync(__dirname);
files.forEach(file => {
const filePath = path.join(__dirname, file);
const stats = fs.statSync(filePath);
console.log(`๐ Debug: ${stats.isDirectory() ? '[DIR]' : '[FILE]'} ${file}`);
});
} catch (e) {
console.log(`๐ Debug: Failed to list directory: ${e.message}`);
}
// ๆฃๆฅ node_modules ็ฎๅฝ
const nodeModulesPath = path.join(__dirname, 'node_modules');
console.log(`๐ Debug: node_modules path: ${nodeModulesPath}`);
if (fs.existsSync(nodeModulesPath)) {
console.log(`๐ Debug: node_modules exists, listing @sqlx-mcp packages:`);
try {
const nmContents = fs.readdirSync(nodeModulesPath);
const sqlxMcpPath = path.join(nodeModulesPath, '@sqlx-mcp');
if (fs.existsSync(sqlxMcpPath)) {
const sqlxMcpPackages = fs.readdirSync(sqlxMcpPath);
console.log(`๐ Debug: @sqlx-mcp packages: ${sqlxMcpPackages.join(', ')}`);
} else {
console.log(`๐ Debug: @sqlx-mcp directory not found in node_modules`);
}
} catch (e) {
console.log(`๐ Debug: Error reading node_modules: ${e.message}`);
}
} else {
console.log(`๐ Debug: node_modules does NOT exist`);
}
// ๅฆๆ้ฝๆพไธๅฐ๏ผๆไพ้่ฏฏไฟกๆฏ
const supportedPlatforms = [
'win32-x64 (Windows 64-bit)',
'linux-x64 (Linux 64-bit)',
'darwin-x64 (macOS Intel)',
'darwin-arm64 (macOS Apple Silicon)'
];
throw new Error(
`Platform package not available.\n` +
`Current platform: ${process.platform}-${process.arch}\n` +
`Supported platforms:\n${supportedPlatforms.map(p => ` - ${p}`).join('\n')}\n\n` +
`To fix this issue:\n` +
`1. Check if your platform is supported\n` +
`2. Try reinstalling: npm install @sqlx-mcp/sqlx-mcp\n` +
`3. Build from source: cargo build --release\n` +
`4. Download binary from GitHub releases`
);
}
}
function main() {
try {
const executablePath = getBinaryPath();
// ๅฏๅจMCPๆๅกๅจ
const child = spawn(executablePath, process.argv.slice(2), {
stdio: 'inherit'
});
child.on('exit', (code) => {
process.exit(code);
});
child.on('error', (err) => {
console.error('Failed to start sqlx-mcp:', err.message);
process.exit(1);
});
} catch (error) {
console.error('โ Error:', error.message);
process.exit(1);
}
}
if (require.main === module) {
main();
}
module.exports = { getBinaryPath, main };