UNPKG

azureai-optimizer

Version:

AI-Powered Azure Infrastructure Optimization via Model Context Protocol

183 lines 7.38 kB
#!/usr/bin/env node /** * AzureAI Optimizer CLI Entry Point * TypeScript version for development and testing */ import { program } from 'commander'; import { main } from '../index.js'; import { Config } from '../config/config.js'; import { Logger } from '../utils/logger.js'; const logger = new Logger('CLI'); // ASCII Art Banner const banner = ` ╔═══════════════════════════════════════════════════════════════╗ ║ AzureAI Optimizer ║ ║ AI-Powered Azure Infrastructure ║ ║ Optimization via MCP ║ ╚═══════════════════════════════════════════════════════════════╝ `; console.log('\x1b[36m%s\x1b[0m', banner); // Version check const packageJson = await import('../../package.json', { assert: { type: 'json' } }); console.log(`Version: ${packageJson.default.version}`); console.log(`Node.js: ${process.version}`); console.log(''); // Environment validation function validateEnvironment() { const validation = Config.validateEnvironment(); if (!validation.valid) { console.error('❌ Missing required environment variables:'); validation.missing.forEach(env => console.error(` - ${env}`)); console.error(''); console.error('💡 Quick setup:'); console.error(' export AZURE_SUBSCRIPTION_ID="your-subscription-id"'); console.error(' export AZURE_TENANT_ID="your-tenant-id"'); console.error(''); console.error('🔗 Documentation: https://docs.azureai-optimizer.com/setup'); process.exit(1); } if (validation.warnings.length > 0) { console.warn('⚠️ Recommended environment variables not set:'); validation.warnings.forEach(env => console.warn(` - ${env}`)); console.warn(''); } } // Configuration display function showConfiguration() { console.log('🔧 Configuration:'); console.log(` Subscription ID: ${process.env.AZURE_SUBSCRIPTION_ID?.substring(0, 8)}...`); console.log(` Tenant ID: ${process.env.AZURE_TENANT_ID?.substring(0, 8) || 'Auto-detect'}...`); console.log(` Log Level: ${process.env.AZUREAI_LOG_LEVEL || 'info'}`); console.log(''); } // Server startup async function startServer(options) { try { logger.info('🚀 Starting AzureAI Optimizer MCP Server...'); // Validate environment validateEnvironment(); showConfiguration(); // Set environment variables for the server configuration process.env.AZUREAI_TRANSPORT = options.transport || 'stdio'; if (options.port) { process.env.AZUREAI_PORT = options.port; } if (options.logLevel) { process.env.AZUREAI_LOG_LEVEL = options.logLevel; } // Start the main server await main(); } catch (error) { logger.error('❌ Failed to start server:', error); if (error instanceof Error && error.message.includes('EAUTH')) { console.error(''); console.error('🔑 Authentication failed. Please check:'); console.error(' 1. Azure CLI login: az login'); console.error(' 2. Subscription access permissions'); console.error(' 3. Environment variables are correct'); } process.exit(1); } } // CLI Commands program .name('azureai-optimizer') .description('AzureAI Optimizer MCP Server') .version(packageJson.default.version); program .command('server') .description('Server management commands'); program .command('server start') .description('Start the MCP server') .option('-p, --port <port>', 'Server port (for SSE transport)', '8080') .option('-t, --transport <transport>', 'Transport type (stdio|sse|websocket)', 'stdio') .option('-l, --log-level <level>', 'Log level (debug|info|warn|error)', 'info') .action(startServer); program .command('config') .description('Show configuration help') .action(() => { Config.displayConfigurationHelp(); }); program .command('health') .description('Check system health and Azure connectivity') .action(async () => { try { logger.info('🏥 Running health checks...'); // Basic environment check validateEnvironment(); logger.info('✅ Environment variables configured'); // Load and validate configuration const config = Config.load(); logger.info('✅ Configuration loaded successfully'); // Test Azure authentication const { AzureAuthProvider } = await import('../auth/azure-auth.js'); const auth = new AzureAuthProvider(config.azure); await auth.initialize(); logger.info('✅ Azure authentication successful'); console.log(''); console.log('🎉 All health checks passed! Ready to optimize Azure infrastructure.'); } catch (error) { logger.error('❌ Health check failed:', error); process.exit(1); } }); program .command('tools') .description('List available optimization tools') .action(async () => { try { logger.info('🛠️ Loading available tools...'); const { ToolRegistry } = await import('../tools/registry.js'); const registry = new ToolRegistry(); await registry.initialize(); const tools = await registry.getTools(); console.log('🛠️ Available AzureAI Optimizer Tools:'); console.log(''); const categories = new Map(); tools.forEach(tool => { const category = tool.name.includes('cost') ? 'Cost Management' : tool.name.includes('security') ? 'Security' : tool.name.includes('performance') ? 'Performance' : tool.name.includes('right') ? 'Resource Optimization' : 'General'; if (!categories.has(category)) { categories.set(category, []); } categories.get(category).push(tool); }); categories.forEach((tools, category) => { console.log(`\x1b[36m${category}:\x1b[0m`); tools.forEach(tool => { console.log(` • ${tool.name}`); console.log(` ${tool.description}`); console.log(''); }); }); console.log('📖 Documentation: https://docs.azureai-optimizer.com/tools'); } catch (error) { logger.error('❌ Failed to load tools:', error); process.exit(1); } }); // Handle unknown commands program.on('command:*', () => { console.error('❌ Unknown command. Use --help for available commands.'); process.exit(1); }); // Parse command line arguments program.parse(); // Show help if no command provided if (!process.argv.slice(2).length) { program.outputHelp(); console.log(''); console.log('🚀 Quick start:'); console.log(' npx @azureai/optimizer server start'); console.log(''); console.log('📖 Documentation: https://docs.azureai-optimizer.com'); } //# sourceMappingURL=azureai-optimizer.js.map