#!/usr/bin/env node /** * WHOOSH MCP Server * * Exposes the WHOOSH Distributed AI Orchestration Platform via Model Context Protocol (MCP) * Allows AI assistants like Claude to directly orchestrate distributed development tasks */ import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { CallToolRequestSchema, ListResourcesRequestSchema, ListToolsRequestSchema, ReadResourceRequestSchema, } from '@modelcontextprotocol/sdk/types.js'; import { WHOOSHClient } from './whoosh-client.js'; import { WHOOSHTools } from './whoosh-tools.js'; import { WHOOSHResources } from './whoosh-resources.js'; class WHOOSHMCPServer { server; whooshClient; whooshTools; whooshResources; discoveryInterval; isDaemonMode = false; constructor() { this.server = new Server({ name: 'whoosh-mcp-server', version: '1.0.0', }, { capabilities: { tools: {}, resources: {}, }, }); // Initialize WHOOSH client and handlers this.whooshClient = new WHOOSHClient(); this.whooshTools = new WHOOSHTools(this.whooshClient); this.whooshResources = new WHOOSHResources(this.whooshClient); this.setupHandlers(); } setupHandlers() { // Tools handler - exposes WHOOSH operations as MCP tools this.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: this.whooshTools.getAllTools(), }; }); this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; return await this.whooshTools.executeTool(name, args || {}); }); // Resources handler - exposes WHOOSH cluster state as MCP resources this.server.setRequestHandler(ListResourcesRequestSchema, async () => { return { resources: await this.whooshResources.getAllResources(), }; }); this.server.setRequestHandler(ReadResourceRequestSchema, async (request) => { const { uri } = request.params; return await this.whooshResources.readResource(uri); }); // Error handling this.server.onerror = (error) => { console.error('[MCP Server Error]:', error); }; process.on('SIGINT', async () => { await this.shutdown(); }); process.on('SIGTERM', async () => { await this.shutdown(); }); process.on('SIGHUP', async () => { console.log('🔄 Received SIGHUP, triggering agent discovery...'); await this.autoDiscoverAgents(); }); } async start() { console.log('🐝 Starting WHOOSH MCP Server...'); // Check for daemon mode this.isDaemonMode = process.argv.includes('--daemon'); if (this.isDaemonMode) { console.log('🔧 Running in daemon mode'); } // Test connection to WHOOSH backend try { await this.whooshClient.testConnection(); console.log('✅ Connected to WHOOSH backend successfully'); } catch (error) { console.error('❌ Failed to connect to WHOOSH backend:', error); process.exit(1); } // Auto-discover and register agents on startup console.log('🔍 Auto-discovering agents...'); try { await this.autoDiscoverAgents(); console.log('✅ Auto-discovery completed successfully'); } catch (error) { console.warn('⚠️ Auto-discovery failed, continuing without it:', error); } // Set up periodic auto-discovery if enabled if (this.isDaemonMode && process.env.AUTO_DISCOVERY !== 'false') { this.setupPeriodicDiscovery(); } if (this.isDaemonMode) { console.log('🚀 WHOOSH MCP Server running in daemon mode'); console.log('🔗 Monitoring cluster and auto-discovering agents...'); // Keep the process alive in daemon mode setInterval(() => { // Health check - could add cluster monitoring here }, 30000); } else { const transport = new StdioServerTransport(); await this.server.connect(transport); console.log('🚀 WHOOSH MCP Server running on stdio'); console.log('🔗 AI assistants can now orchestrate your distributed cluster!'); } } setupPeriodicDiscovery() { const interval = parseInt(process.env.DISCOVERY_INTERVAL || '300000', 10); // Default 5 minutes console.log(`🔄 Setting up periodic auto-discovery every ${interval / 1000} seconds`); this.discoveryInterval = setInterval(async () => { console.log('🔍 Periodic agent auto-discovery...'); try { await this.autoDiscoverAgents(); console.log('✅ Periodic auto-discovery completed'); } catch (error) { console.warn('⚠️ Periodic auto-discovery failed:', error); } }, interval); } async autoDiscoverAgents() { // Use the existing whoosh_bring_online functionality const result = await this.whooshTools.executeTool('whoosh_bring_online', { force_refresh: false, subnet_scan: true }); if (result.isError) { throw new Error(`Auto-discovery failed: ${result.content[0]?.text || 'Unknown error'}`); } } async shutdown() { console.log('🛑 Shutting down WHOOSH MCP Server...'); if (this.discoveryInterval) { clearInterval(this.discoveryInterval); console.log('✅ Stopped periodic auto-discovery'); } await this.server.close(); console.log('✅ WHOOSH MCP Server stopped'); process.exit(0); } } // Start the server const server = new WHOOSHMCPServer(); server.start().catch((error) => { console.error('Failed to start WHOOSH MCP Server:', error); process.exit(1); }); //# sourceMappingURL=index.js.map