UNPKG

ig-trading-mcp

Version:

IG Trading API with MCP (Model Context Protocol) server for AI integration

164 lines (132 loc) 5.2 kB
#!/usr/bin/env node /** * Setup Script for IG Trading API * * Interactive setup to create your .env configuration file */ import fs from 'fs'; import readline from 'readline'; import { fileURLToPath } from 'url'; import path from 'path'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); const question = (query) => new Promise((resolve) => rl.question(query, resolve)); async function setup() { console.log('\n╔════════════════════════════════════════╗'); console.log('║ IG Trading API Setup Wizard ║'); console.log('╚════════════════════════════════════════╝\n'); console.log('This wizard will help you create your .env configuration file.\n'); console.log('You will need:'); console.log(' • Your IG API Key'); console.log(' • Your IG username (identifier)'); console.log(' • Your IG password\n'); const envPath = path.join(__dirname, '..', '.env'); // Check if .env already exists if (fs.existsSync(envPath)) { const overwrite = await question('⚠️ .env file already exists. Overwrite? (y/N): '); if (overwrite.toLowerCase() !== 'y') { console.log('Setup cancelled.'); process.exit(0); } } console.log('\n📝 Please enter your IG credentials:\n'); // Get API Key console.log('To get your API key:'); console.log('1. Log into https://www.ig.com'); console.log('2. Go to My IG > Settings > API keys'); console.log('3. Create or copy your API key\n'); const apiKey = await question('Enter your IG API Key: '); if (!apiKey || apiKey.length < 20) { console.error('\n❌ Invalid API key. API keys are typically 20+ characters.'); process.exit(1); } // Get Username console.log('\nYour identifier is the username you use to log into IG.'); const identifier = await question('Enter your IG username/identifier: '); if (!identifier || identifier.length < 3) { console.error('\n❌ Invalid identifier.'); process.exit(1); } // Get Password console.log('\nYour password is the same one you use to log into IG.'); const password = await question('Enter your IG password: '); if (!password || password.length < 6) { console.error('\n❌ Invalid password.'); process.exit(1); } // Get Environment console.log('\nWhich environment do you want to use?'); console.log(' 1. Demo (recommended for testing)'); console.log(' 2. Live (real money trading)\n'); const envChoice = await question('Enter choice (1 or 2) [1]: ') || '1'; const isDemo = envChoice === '1' ? 'true' : 'false'; if (envChoice === '2') { console.log('\n⚠️ WARNING: You selected LIVE trading. Real money will be at risk!'); const confirm = await question('Are you sure? (y/N): '); if (confirm.toLowerCase() !== 'y') { console.log('Switching to demo mode for safety.'); isDemo = 'true'; } } // Get Log Level console.log('\nSelect logging level:'); console.log(' 1. Error only (minimal output)'); console.log(' 2. Info (standard output)'); console.log(' 3. Debug (verbose output)\n'); const logChoice = await question('Enter choice (1, 2, or 3) [1]: ') || '1'; const logLevel = logChoice === '3' ? 'debug' : logChoice === '2' ? 'info' : 'error'; // Create .env content const envContent = `# IG Trading API Configuration # Generated by setup wizard on ${new Date().toISOString()} # IG API Credentials IG_API_KEY=${apiKey} IG_IDENTIFIER=${identifier} IG_PASSWORD=${password} # Environment Settings IG_DEMO=${isDemo} LOG_LEVEL=${logLevel} # Security Settings (optional - uncomment to use) # MASTER_ENCRYPTION_KEY=your_secure_key_here # JWT_SECRET=your_jwt_secret_here `; // Write .env file try { fs.writeFileSync(envPath, envContent); console.log('\n✅ Configuration saved to .env file\n'); } catch (error) { console.error('\n❌ Failed to write .env file:', error.message); process.exit(1); } // Test the configuration console.log('Would you like to test your configuration now?'); const testNow = await question('Run test? (Y/n): ') || 'y'; if (testNow.toLowerCase() === 'y') { console.log('\n🧪 Testing configuration...\n'); rl.close(); // Import and run the test const { default: test } = await import('./simple-account-test.js'); } else { console.log('\n✅ Setup complete!\n'); console.log('You can now run:'); console.log(' npm run test:account - Test your account connection'); console.log(' node examples-modern.js - Run examples'); console.log(' npm run mcp - Start MCP server\n'); rl.close(); } } // Handle errors process.on('unhandledRejection', (error) => { console.error('\n❌ Setup failed:', error.message); rl.close(); process.exit(1); }); // Run setup setup().catch(error => { console.error('\n❌ Setup error:', error); rl.close(); process.exit(1); });