UNPKG

@sqlx-mcp/sqlx-mcp

Version:

Rust MCP server for SQLx database management

181 lines (154 loc) โ€ข 6.54 kB
#!/usr/bin/env node 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 };