import { ToolDefinition, ToolCall, ToolResult, ToolContext } from './types'; import { createMCPClient, MCPClient } from './mcp-client'; /** * Tool Router - Routes tool calls to the MCP server. * * Uses the GoHighLevel MCP server which provides 269+ tools * for CRM operations, messaging, pipelines, and more. */ export class ToolRouter { private mcpClient: MCPClient | null = null; constructor(mcpClient: MCPClient | null) { this.mcpClient = mcpClient; } /** * Get all available tools from MCP server. */ async getAllTools(): Promise { if (!this.mcpClient) { console.warn('[ToolRouter] MCP client not available'); return []; } try { return await this.mcpClient.getTools(); } catch (error) { console.error('[ToolRouter] Failed to get tools:', error); return []; } } /** * Execute a single tool call via MCP. */ async execute(toolCall: ToolCall, _context: ToolContext): Promise { if (!this.mcpClient) { return { toolCallId: toolCall.id, success: false, error: 'MCP server not configured. Please start the MCP server.' }; } const result = await this.mcpClient.executeTool(toolCall.name, toolCall.input); return { ...result, toolCallId: toolCall.id }; } /** * Execute multiple tool calls. * Note: Executed sequentially to avoid overwhelming the MCP server. */ async executeAll(toolCalls: ToolCall[], context: ToolContext): Promise { if (toolCalls.length === 0) return []; const results: ToolResult[] = []; for (const tc of toolCalls) { const result = await this.execute(tc, context); results.push(result); } return results; } /** * Check if MCP server is healthy */ async isHealthy(): Promise { if (!this.mcpClient) return false; return this.mcpClient.healthCheck(); } } /** * Create a new ToolRouter instance connected to MCP. */ export async function createToolRouter(): Promise { const mcpClient = await createMCPClient(); return new ToolRouter(mcpClient); }