cre-sync/lib/control-center/tool-router.ts
BusyBee3333 4e6467ffb0 Add CRESync CRM application with Setup page
- Build complete Next.js CRM for commercial real estate
- Add authentication with JWT sessions and role-based access
- Add GoHighLevel API integration for contacts, conversations, opportunities
- Add AI-powered Control Center with tool calling
- Add Setup page with onboarding checklist (/setup)
- Add sidebar navigation with Setup menu item
- Fix type errors in onboarding API, GHL services, and control center tools
- Add Prisma schema with SQLite for local development
- Add UI components with clay morphism design system

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-14 17:30:55 -05:00

84 lines
2.1 KiB
TypeScript

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<ToolDefinition[]> {
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<ToolResult> {
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<ToolResult[]> {
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<boolean> {
if (!this.mcpClient) return false;
return this.mcpClient.healthCheck();
}
}
/**
* Create a new ToolRouter instance connected to MCP.
*/
export async function createToolRouter(): Promise<ToolRouter> {
const mcpClient = await createMCPClient();
return new ToolRouter(mcpClient);
}