/** * MCP Server plugin API client. * * Exposes LeoCRM tools to external MCP clients (Claude Desktop, etc.). */ import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { apiGet, apiPost } from './client'; // ─── Types ───────────────────────────────────────────────────────────────── export interface McpToolParameter { name: string; type: string; description: string; required: boolean; default?: unknown; } export interface McpToolDefinition { name: string; description: string; category: string; parameters: McpToolParameter[]; required_permission: string | null; } export interface McpToolListResponse { tools: McpToolDefinition[]; count: number; } export interface McpToolExecuteRequest { arguments: Record; } export interface McpToolExecuteResponse { tool: string; success: boolean; result: unknown; error: string | null; } export interface McpServerConfig { server_name: string; server_version: string; protocol_version: string; base_url: string; auth_method: string; available_tools: string[]; } // ─── API Functions ───────────────────────────────────────────────────────── export function fetchMcpTools(): Promise { return apiGet('/mcp/tools'); } export function executeMcpTool( toolName: string, args: Record ): Promise { return apiPost(`/mcp/tools/${toolName}/execute`, { arguments: args, }); } export function fetchMcpConfig(): Promise { return apiGet('/mcp/config'); } // ─── Hooks ───────────────────────────────────────────────────────────────── export function useMcpTools() { return useQuery({ queryKey: ['mcp', 'tools'], queryFn: fetchMcpTools, staleTime: 60 * 1000, }); } export function useMcpConfig() { return useQuery({ queryKey: ['mcp', 'config'], queryFn: fetchMcpConfig, staleTime: 60 * 1000, }); } export function useExecuteMcpTool() { const queryClient = useQueryClient(); return useMutation({ mutationFn: ({ toolName, args, }: { toolName: string; args: Record; }) => executeMcpTool(toolName, args), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['mcp'] }); }, }); }