#!/usr/bin/env node
/**
* AgentBTC MCP CLI
* Main entry point for the AgentBTC MCP server NPM package
*/
import { readFileSync, writeFileSync, existsSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { spawn } from 'child_process';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const PACKAGE_DIR = dirname(__dirname);
const SERVER_PATH = join(PACKAGE_DIR, 'index.js');
// CLI usage
const USAGE = `
AgentBTC MCP Server - BYON Bitcoin payments for AI agents
USAGE:
agentbtc-mcp [command] [options]
COMMANDS:
start Start the MCP server (default)
setup Interactive setup for Claude Desktop
test Test Lightning node connection
config Show current configuration
version Show version information
OPTIONS:
--lnd-host <host> Lightning node host (e.g., localhost:10009)
--lnd-macaroon <path> Path to LND macaroon file
--api-key <key> AgentBTC API key (optional for BYON mode)
--api-url <url> AgentBTC API URL (default: http://localhost:8000)
--help Show this help message
ENVIRONMENT VARIABLES:
AGENTBTC_LND_HOST Lightning node host
AGENTBTC_LND_MACAROON Path to LND macaroon file
AGENTBTC_API_KEY AgentBTC API key
AGENTBTC_API_URL AgentBTC API URL
BK_BLOCK_NODE_PUBKEY BK Block routing node pubkey
EXAMPLES:
# Start with environment variables
agentbtc-mcp start
# Start with CLI options
agentbtc-mcp start --lnd-host localhost:10009 --lnd-macaroon ~/.lnd/data/chain/bitcoin/mainnet/readonly.macaroon
# Interactive setup for Claude Desktop
agentbtc-mcp setup
# Test connection
agentbtc-mcp test
For more info: https://docs.agentbtc.io/mcp-server
`;
// Get package version
function getVersion() {
try {
const packageJson = JSON.parse(readFileSync(join(PACKAGE_DIR, 'package.json'), 'utf8'));
return packageJson.version;
} catch (e) {
return 'unknown';
}
}
// Parse CLI arguments
function parseArgs(args) {
const parsed = {
command: 'start',
options: {}
};
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (!arg.startsWith('--')) {
if (i === 0) {
parsed.command = arg;
}
continue;
}
const key = arg.slice(2);
const value = args[i + 1];
if (value && !value.startsWith('--')) {
parsed.options[key] = value;
i++; // Skip next arg as it's the value
} else {
parsed.options[key] = true;
}
}
return parsed;
}
// Set environment variables from CLI options
function setEnvFromOptions(options) {
if (options['lnd-host']) {
process.env.AGENTBTC_LND_HOST = options['lnd-host'];
}
if (options['lnd-macaroon']) {
process.env.AGENTBTC_LND_MACAROON = options['lnd-macaroon'];
}
if (options['api-key']) {
process.env.AGENTBTC_API_KEY = options['api-key'];
}
if (options['api-url']) {
process.env.AGENTBTC_API_URL = options['api-url'];
}
}
// Start the MCP server
function startServer() {
console.log('π Starting AgentBTC MCP Server...');
console.log(`π‘ LND Host: ${process.env.AGENTBTC_LND_HOST || 'not set'}`);
console.log(`π Macaroon: ${process.env.AGENTBTC_LND_MACAROON || 'not set'}`);
console.log(`π API URL: ${process.env.AGENTBTC_API_URL || 'http://localhost:8000'}`);
console.log('');
const server = spawn('node', [SERVER_PATH], {
stdio: 'inherit',
cwd: PACKAGE_DIR
});
process.on('SIGINT', () => {
server.kill('SIGINT');
});
process.on('SIGTERM', () => {
server.kill('SIGTERM');
});
}
// Show current configuration
function showConfig() {
console.log('AgentBTC MCP Server Configuration:');
console.log('');
console.log(`Version: ${getVersion()}`);
console.log(`LND Host: ${process.env.AGENTBTC_LND_HOST || 'not set'}`);
console.log(`LND Macaroon: ${process.env.AGENTBTC_LND_MACAROON || 'not set'}`);
console.log(`API URL: ${process.env.AGENTBTC_API_URL || 'http://localhost:8000'}`);
console.log(`API Key: ${process.env.AGENTBTC_API_KEY ? '***set***' : 'not set'}`);
console.log(`BK Block Node: ${process.env.BK_BLOCK_NODE_PUBKEY || 'default'}`);
}
// Interactive setup for Claude Desktop
async function interactiveSetup() {
console.log('π οΈ AgentBTC MCP Setup for Claude Desktop');
console.log('');
console.log('This will help you configure the AgentBTC MCP server for Claude Desktop.');
console.log('');
// TODO: Add interactive prompts for LND connection details
// TODO: Generate Claude Desktop config snippet
// TODO: Detect Claude config location and offer to update it
console.log('β οΈ Interactive setup not yet implemented.');
console.log('');
console.log('For now, manually add this to your Claude Desktop config:');
console.log('');
console.log(JSON.stringify({
"agentbtc": {
"command": "agentbtc-mcp",
"env": {
"AGENTBTC_LND_HOST": "localhost:10009",
"AGENTBTC_LND_MACAROON": "/path/to/your/readonly.macaroon"
}
}
}, null, 2));
}
// Test Lightning node connection
async function testConnection() {
console.log('π Testing Lightning node connection...');
const lndHost = process.env.AGENTBTC_LND_HOST;
const lndMacaroon = process.env.AGENTBTC_LND_MACAROON;
if (!lndHost || !lndMacaroon) {
console.log('β Missing LND configuration');
console.log(' Set AGENTBTC_LND_HOST and AGENTBTC_LND_MACAROON');
process.exit(1);
}
if (!existsSync(lndMacaroon)) {
console.log(`β Macaroon file not found: ${lndMacaroon}`);
process.exit(1);
}
console.log(`π‘ Host: ${lndHost}`);
console.log(`π Macaroon: ${lndMacaroon}`);
console.log('');
console.log('β οΈ Connection test not yet implemented.');
console.log(' Try starting the server to test: agentbtc-mcp start');
}
// Main CLI handler
function main() {
const args = process.argv.slice(2);
if (args.includes('--help') || args.includes('-h')) {
console.log(USAGE);
process.exit(0);
}
const { command, options } = parseArgs(args);
// Set environment variables from CLI options
setEnvFromOptions(options);
switch (command) {
case 'start':
startServer();
break;
case 'setup':
interactiveSetup();
break;
case 'test':
testConnection();
break;
case 'config':
showConfig();
break;
case 'version':
console.log(getVersion());
break;
default:
console.log(`Unknown command: ${command}`);
console.log(USAGE);
process.exit(1);
}
}
main();