UNPKG

@juspay/neurolink

Version:

Universal AI Development Platform with working MCP integration, multi-provider support, voice (TTS/STT/realtime), and professional CLI. 58+ external MCP servers discoverable, multimodal file processing, RAG pipelines. Build, test, and deploy AI applicatio

145 lines (144 loc) 4.38 kB
/** * Tool Discovery Service * Automatically discovers and registers tools from external MCP servers * Handles tool validation, transformation, and lifecycle management */ import { EventEmitter } from "events"; import type { Client } from "@modelcontextprotocol/sdk/client/index.js"; import type { ExternalMCPToolInfo, ExternalMCPToolResult, ToolDiscoveryResult, ExternalToolExecutionOptions, JsonObject } from "../types/index.js"; import type { McpOutputNormalizer } from "./mcpOutputNormalizer.js"; /** * ToolDiscoveryService * Handles automatic tool discovery and registration from external MCP servers */ export declare class ToolDiscoveryService extends EventEmitter { private serverToolStorage; private toolRegistry; private serverTools; private discoveryInProgress; /** Optional normalizer applied to every tool output before it is returned. */ private outputNormalizer?; constructor(); /** * Attach a McpOutputNormalizer. * When set, every raw callTool() result is passed through the normalizer * before being returned. Oversized outputs are replaced with compact * surrogates according to the configured strategy. */ setOutputNormalizer(normalizer: McpOutputNormalizer): void; /** * Discover tools from an external MCP server */ discoverTools(serverId: string, client: Client, timeout?: number): Promise<ToolDiscoveryResult>; /** * Perform the actual tool discovery */ private performToolDiscovery; /** * Register discovered tools */ private registerDiscoveredTools; /** * Create tool info from MCP tool definition */ private createToolInfo; /** * Infer tool category from tool definition */ private inferToolCategory; /** * Validate a tool */ private validateTool; /** * Infer tool complexity */ private inferComplexity; /** * Infer if tool requires authentication */ private inferAuthRequirement; /** * Execute a tool */ executeTool(toolName: string, serverId: string, client: Client, parameters: JsonObject, options?: ExternalToolExecutionOptions): Promise<ExternalMCPToolResult>; /** * Validate tool parameters */ private validateToolParameters; /** * Validate parameter type */ private validateParameterType; /** * Validate tool output with enhanced type safety */ private validateToolOutput; /** * Update tool statistics */ private updateToolStats; /** * Get tool by name and server */ getTool(toolName: string, serverId: string): ExternalMCPToolInfo | undefined; /** * Get all tools for a server */ getServerTools(serverId: string): ExternalMCPToolInfo[]; /** * Get all registered tools */ getAllTools(): ExternalMCPToolInfo[]; /** * Clear tools for a server */ clearServerTools(serverId: string): void; /** * Update tool availability */ updateToolAvailability(toolName: string, serverId: string, isAvailable: boolean): void; /** * Create tool key for registry */ private createToolKey; /** * Create timeout promise */ private createTimeoutPromise; /** * Destroy the tool discovery service and clean up all resources * This method should be called when the service is no longer needed * to prevent memory leaks from accumulated event listeners * * @example * ```typescript * const service = new ToolDiscoveryService(); * // ... use the service ... * service.destroy(); // Clean up when done * ``` */ destroy(): void; /** * Reset statistics for all tools * This clears execution counts, timing data, and other statistics * while preserving the tool registrations themselves */ resetStatistics(): void; /** * Get the count of active event listeners * Useful for monitoring potential memory leaks */ getListenerCount(): number; /** * Get discovery statistics */ getStatistics(): { totalTools: number; availableTools: number; unavailableTools: number; totalServers: number; toolsByServer: Record<string, number>; toolsByCategory: Record<string, number>; }; }