|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Post-install script for Graphistry MCP |
| 4 | + * |
| 5 | + * Installs Python dependencies using uv (preferred) or pip (fallback). |
| 6 | + * Runs after npm install to ensure Python deps are available. |
| 7 | + */ |
| 8 | + |
| 9 | +const { execSync, spawnSync } = require('child_process'); |
| 10 | +const fs = require('fs'); |
| 11 | +const path = require('path'); |
| 12 | + |
| 13 | +// ANSI color codes |
| 14 | +const colors = { |
| 15 | + reset: '\x1b[0m', |
| 16 | + bright: '\x1b[1m', |
| 17 | + green: '\x1b[32m', |
| 18 | + yellow: '\x1b[33m', |
| 19 | + red: '\x1b[31m', |
| 20 | + cyan: '\x1b[36m', |
| 21 | +}; |
| 22 | + |
| 23 | +function log(msg, color = colors.reset) { |
| 24 | + console.log(`${color}${msg}${colors.reset}`); |
| 25 | +} |
| 26 | + |
| 27 | +function checkCommand(cmd) { |
| 28 | + try { |
| 29 | + spawnSync(cmd, ['--version'], { stdio: 'ignore' }); |
| 30 | + return true; |
| 31 | + } catch (e) { |
| 32 | + return false; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function findPython() { |
| 37 | + const candidates = ['python3', 'python']; |
| 38 | + for (const cmd of candidates) { |
| 39 | + if (checkCommand(cmd)) { |
| 40 | + return cmd; |
| 41 | + } |
| 42 | + } |
| 43 | + return null; |
| 44 | +} |
| 45 | + |
| 46 | +function installWithUv(pythonCmd) { |
| 47 | + log('\n🚀 Installing Python dependencies with uv...', colors.cyan); |
| 48 | + |
| 49 | + try { |
| 50 | + // Check if pyproject.toml exists |
| 51 | + const pyprojectPath = path.join(__dirname, 'pyproject.toml'); |
| 52 | + if (!fs.existsSync(pyprojectPath)) { |
| 53 | + throw new Error('pyproject.toml not found'); |
| 54 | + } |
| 55 | + |
| 56 | + // Install with uv sync (creates venv and installs deps) |
| 57 | + execSync('uv sync --no-dev', { |
| 58 | + cwd: __dirname, |
| 59 | + stdio: 'inherit', |
| 60 | + env: { |
| 61 | + ...process.env, |
| 62 | + PYTHONUNBUFFERED: '1', |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + log('✅ Python dependencies installed with uv\n', colors.green); |
| 67 | + return true; |
| 68 | + } catch (e) { |
| 69 | + log(`⚠️ uv installation failed: ${e.message}`, colors.yellow); |
| 70 | + return false; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +function installWithPip(pythonCmd) { |
| 75 | + log('\n📦 Installing Python dependencies with pip...', colors.cyan); |
| 76 | + |
| 77 | + try { |
| 78 | + // Install the package in editable mode from pyproject.toml |
| 79 | + execSync(`${pythonCmd} -m pip install -e ".[dev]"`, { |
| 80 | + cwd: __dirname, |
| 81 | + stdio: 'inherit', |
| 82 | + env: { |
| 83 | + ...process.env, |
| 84 | + PYTHONUNBUFFERED: '1', |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + log('✅ Python dependencies installed with pip\n', colors.green); |
| 89 | + return true; |
| 90 | + } catch (e) { |
| 91 | + log(`❌ pip installation failed: ${e.message}`, colors.red); |
| 92 | + return false; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +function main() { |
| 97 | + log('\n' + '='.repeat(60), colors.bright); |
| 98 | + log(' Graphistry MCP - Python Dependencies Setup', colors.bright); |
| 99 | + log('='.repeat(60) + '\n', colors.bright); |
| 100 | + |
| 101 | + // Check for Python |
| 102 | + const pythonCmd = findPython(); |
| 103 | + if (!pythonCmd) { |
| 104 | + log('❌ Python not found!', colors.red); |
| 105 | + log('\nPlease install Python 3.10+ from https://python.org', colors.yellow); |
| 106 | + log('Then run: npm install again\n', colors.yellow); |
| 107 | + process.exit(1); |
| 108 | + } |
| 109 | + |
| 110 | + log(`✓ Found Python: ${pythonCmd}`, colors.green); |
| 111 | + |
| 112 | + // Try uv first (recommended) |
| 113 | + const hasUv = checkCommand('uv'); |
| 114 | + |
| 115 | + if (hasUv) { |
| 116 | + log('✓ Found uv (recommended package manager)', colors.green); |
| 117 | + if (installWithUv(pythonCmd)) { |
| 118 | + printSuccessMessage(true); |
| 119 | + return; |
| 120 | + } |
| 121 | + log('\nFalling back to pip...', colors.yellow); |
| 122 | + } else { |
| 123 | + log('ℹ uv not found - using pip instead', colors.yellow); |
| 124 | + log(' (Install uv for faster dependency management: https://github.com/astral-sh/uv)\n', colors.cyan); |
| 125 | + } |
| 126 | + |
| 127 | + // Fallback to pip |
| 128 | + if (installWithPip(pythonCmd)) { |
| 129 | + printSuccessMessage(false); |
| 130 | + } else { |
| 131 | + log('\n❌ Failed to install Python dependencies!\n', colors.red); |
| 132 | + log('Please install manually:', colors.yellow); |
| 133 | + log(' uv sync', colors.cyan); |
| 134 | + log(' OR', colors.yellow); |
| 135 | + log(' pip install -e ".[dev]"\n', colors.cyan); |
| 136 | + process.exit(1); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +function printSuccessMessage(usedUv) { |
| 141 | + log('\n' + '='.repeat(60), colors.bright); |
| 142 | + log(' ✅ Installation Complete!', colors.green); |
| 143 | + log('='.repeat(60) + '\n', colors.bright); |
| 144 | + |
| 145 | + log('Next steps:', colors.cyan); |
| 146 | + log('1. Get a free Graphistry account:', colors.reset); |
| 147 | + log(' https://hub.graphistry.com\n', colors.cyan); |
| 148 | + |
| 149 | + log('2. Set your Graphistry credentials:', colors.reset); |
| 150 | + log(' export GRAPHISTRY_USERNAME="your_username"', colors.cyan); |
| 151 | + log(' export GRAPHISTRY_PASSWORD="your_password"\n', colors.cyan); |
| 152 | + |
| 153 | + log('3. Configure in Claude Code MCP settings:', colors.reset); |
| 154 | + log(' {', colors.reset); |
| 155 | + log(' "graphistry": {', colors.reset); |
| 156 | + log(' "command": "npx",', colors.reset); |
| 157 | + log(' "args": ["-y", "@silkspace/graphistry-mcp"],', colors.reset); |
| 158 | + log(' "env": {', colors.reset); |
| 159 | + log(' "GRAPHISTRY_USERNAME": "your_username",', colors.reset); |
| 160 | + log(' "GRAPHISTRY_PASSWORD": "your_password"', colors.reset); |
| 161 | + log(' }', colors.reset); |
| 162 | + log(' }', colors.reset); |
| 163 | + log(' }\n', colors.reset); |
| 164 | + |
| 165 | + log('Documentation: https://github.com/graphistry/graphistry-mcp\n', colors.cyan); |
| 166 | +} |
| 167 | + |
| 168 | +// Run installation |
| 169 | +main(); |
0 commit comments