UNPKG

bnbchain-mcp

Version:

---

115 lines (114 loc) • 3.97 kB
import prompts from 'prompts'; import figlet from 'figlet'; import chalk from 'chalk'; import path from 'path'; import fs from 'fs-extra'; import os from 'os'; import { fileURLToPath } from 'url'; // Binance Gold Color const yellow = chalk.hex('#F0B90B'); // ESModule __dirname workaround const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); // Cancel handler const onCancel = () => { console.log(chalk.red('\nāŒ Configuration cancelled by user (Ctrl+C or ESC). Exiting...')); process.exit(0); }; // Show Banner const showBanner = () => { const banner = figlet.textSync('BNB Chain MCP ', { font: 'Big' }); console.log(yellow(banner)); console.log(yellow('šŸš€ Welcome to the BNB Chain MCP Configurator\n')); }; // Ask for credentials const getInputs = async () => { const questions = [ { type: 'password', name: 'moralis', message: 'šŸ”‘ Enter Moralis API Key:( Checkout the link for more info : https://docs.moralis.com/web3-data-api/evm/get-your-api-key )', validate: (val) => val.trim() === '' ? 'Moralis API Key is required!' : true, }, { type: 'password', name: 'privateKey', message: 'šŸ” Enter your BSC Wallet Private Key:', validate: (val) => val.trim() === '' ? 'Private key is required!' : true, }, { type: 'text', name: 'rpcUrl', message: '🌐 Enter your BSC RPC URL (optional):', }, ]; return await prompts(questions, { onCancel }); }; // Generate .env file const generateEnvFile = async (privateKey, rpcUrl, moralis) => { const envContent = `WALLET_PRIVATE_KEY=${privateKey} BSC_RPC_URL=${rpcUrl || ''} MORALIS_API_KEY=${moralis || ''} `.trim(); await fs.writeFile('.env', envContent); console.log(yellow('āœ… .env file generated.')); }; // Generate config object const generateConfig = async (privateKey, rpcUrl, moralis) => { const indexPath = path.resolve(__dirname, '..', 'build', 'index.js'); // one level up from cli/ return { mcpServers: { 'bsc-mcp': { command: 'node', args: [indexPath], env: { BSC_WALLET_PRIVATE_KEY: privateKey, BSC_RPC_URL: rpcUrl || '', MORALIS_API_KEY: moralis || '' }, disabled: false, autoApprove: [] } } }; }; // Configure Claude Desktop const configureClaude = async (config) => { const userHome = os.homedir(); const claudePath = path.join(userHome, 'Library/Application Support/Claude/claude_desktop_config.json'); if (!fs.existsSync(claudePath)) { console.log(chalk.red('āŒ Claude is not installed or config path not found.')); return false; } await fs.writeJSON(claudePath, config, { spaces: 2 }); console.log(yellow('āœ… BNB Chain MCP configured for Claude Desktop.')); return true; }; // Save fallback config file const saveFallbackConfig = async (config) => { await fs.writeJSON('config.json', config, { spaces: 2 }); console.log(yellow('šŸ“ Saved config.json in root project folder.')); }; // Main logic const init = async () => { showBanner(); const { moralis, privateKey, rpcUrl } = await getInputs(); await generateEnvFile(privateKey, rpcUrl, moralis); const config = await generateConfig(privateKey, rpcUrl, moralis); const { setupClaude } = await prompts({ type: 'confirm', name: 'setupClaude', message: '🧠 Do you want to configure in Claude Desktop?', initial: true }, { onCancel }); if (setupClaude) { const success = await configureClaude(config); if (!success) { await saveFallbackConfig(config); } } else { await saveFallbackConfig(config); } }; init();