@juspay/neurolink 9.26.2 → 9.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +59 -9
  3. package/dist/cli/commands/config.d.ts +4 -4
  4. package/dist/cli/commands/mcp.d.ts +87 -0
  5. package/dist/cli/commands/mcp.js +1524 -0
  6. package/dist/cli/loop/optionsSchema.js +4 -0
  7. package/dist/core/modules/ToolsManager.js +29 -2
  8. package/dist/index.d.ts +2 -1
  9. package/dist/index.js +27 -1
  10. package/dist/lib/core/modules/ToolsManager.js +29 -2
  11. package/dist/lib/index.d.ts +2 -1
  12. package/dist/lib/index.js +27 -1
  13. package/dist/lib/mcp/agentExposure.d.ts +228 -0
  14. package/dist/lib/mcp/agentExposure.js +357 -0
  15. package/dist/lib/mcp/batching/index.d.ts +11 -0
  16. package/dist/lib/mcp/batching/index.js +11 -0
  17. package/dist/lib/mcp/batching/requestBatcher.d.ts +202 -0
  18. package/dist/lib/mcp/batching/requestBatcher.js +442 -0
  19. package/dist/lib/mcp/caching/index.d.ts +11 -0
  20. package/dist/lib/mcp/caching/index.js +11 -0
  21. package/dist/lib/mcp/caching/toolCache.d.ts +221 -0
  22. package/dist/lib/mcp/caching/toolCache.js +434 -0
  23. package/dist/lib/mcp/elicitation/elicitationManager.d.ts +169 -0
  24. package/dist/lib/mcp/elicitation/elicitationManager.js +377 -0
  25. package/dist/lib/mcp/elicitation/index.d.ts +11 -0
  26. package/dist/lib/mcp/elicitation/index.js +12 -0
  27. package/dist/lib/mcp/elicitation/types.d.ts +278 -0
  28. package/dist/lib/mcp/elicitation/types.js +11 -0
  29. package/dist/lib/mcp/elicitationProtocol.d.ts +228 -0
  30. package/dist/lib/mcp/elicitationProtocol.js +376 -0
  31. package/dist/lib/mcp/enhancedToolDiscovery.d.ts +205 -0
  32. package/dist/lib/mcp/enhancedToolDiscovery.js +482 -0
  33. package/dist/lib/mcp/index.d.ts +38 -1
  34. package/dist/lib/mcp/index.js +36 -3
  35. package/dist/lib/mcp/mcpRegistryClient.d.ts +332 -0
  36. package/dist/lib/mcp/mcpRegistryClient.js +489 -0
  37. package/dist/lib/mcp/mcpServerBase.d.ts +227 -0
  38. package/dist/lib/mcp/mcpServerBase.js +374 -0
  39. package/dist/lib/mcp/multiServerManager.d.ts +310 -0
  40. package/dist/lib/mcp/multiServerManager.js +580 -0
  41. package/dist/lib/mcp/routing/index.d.ts +11 -0
  42. package/dist/lib/mcp/routing/index.js +11 -0
  43. package/dist/lib/mcp/routing/toolRouter.d.ts +219 -0
  44. package/dist/lib/mcp/routing/toolRouter.js +417 -0
  45. package/dist/lib/mcp/serverCapabilities.d.ts +341 -0
  46. package/dist/lib/mcp/serverCapabilities.js +503 -0
  47. package/dist/lib/mcp/toolAnnotations.d.ts +154 -0
  48. package/dist/lib/mcp/toolAnnotations.js +240 -0
  49. package/dist/lib/mcp/toolConverter.d.ts +178 -0
  50. package/dist/lib/mcp/toolConverter.js +259 -0
  51. package/dist/lib/mcp/toolIntegration.d.ts +136 -0
  52. package/dist/lib/mcp/toolIntegration.js +335 -0
  53. package/dist/lib/memory/hippocampusInitializer.d.ts +2 -2
  54. package/dist/lib/memory/hippocampusInitializer.js +1 -1
  55. package/dist/lib/neurolink.d.ts +275 -2
  56. package/dist/lib/neurolink.js +596 -56
  57. package/dist/lib/providers/litellm.d.ts +10 -0
  58. package/dist/lib/providers/litellm.js +104 -2
  59. package/dist/lib/types/configTypes.d.ts +56 -0
  60. package/dist/lib/types/conversation.d.ts +2 -2
  61. package/dist/lib/types/generateTypes.d.ts +4 -0
  62. package/dist/lib/types/index.d.ts +2 -1
  63. package/dist/lib/types/modelTypes.d.ts +6 -6
  64. package/dist/lib/types/streamTypes.d.ts +2 -0
  65. package/dist/lib/types/tools.d.ts +2 -0
  66. package/dist/lib/utils/pricing.js +177 -17
  67. package/dist/lib/utils/schemaConversion.d.ts +6 -1
  68. package/dist/lib/utils/schemaConversion.js +50 -28
  69. package/dist/lib/workflow/config.d.ts +16 -16
  70. package/dist/mcp/agentExposure.d.ts +228 -0
  71. package/dist/mcp/agentExposure.js +356 -0
  72. package/dist/mcp/batching/index.d.ts +11 -0
  73. package/dist/mcp/batching/index.js +10 -0
  74. package/dist/mcp/batching/requestBatcher.d.ts +202 -0
  75. package/dist/mcp/batching/requestBatcher.js +441 -0
  76. package/dist/mcp/caching/index.d.ts +11 -0
  77. package/dist/mcp/caching/index.js +10 -0
  78. package/dist/mcp/caching/toolCache.d.ts +221 -0
  79. package/dist/mcp/caching/toolCache.js +433 -0
  80. package/dist/mcp/elicitation/elicitationManager.d.ts +169 -0
  81. package/dist/mcp/elicitation/elicitationManager.js +376 -0
  82. package/dist/mcp/elicitation/index.d.ts +11 -0
  83. package/dist/mcp/elicitation/index.js +11 -0
  84. package/dist/mcp/elicitation/types.d.ts +278 -0
  85. package/dist/mcp/elicitation/types.js +10 -0
  86. package/dist/mcp/elicitationProtocol.d.ts +228 -0
  87. package/dist/mcp/elicitationProtocol.js +375 -0
  88. package/dist/mcp/enhancedToolDiscovery.d.ts +205 -0
  89. package/dist/mcp/enhancedToolDiscovery.js +481 -0
  90. package/dist/mcp/index.d.ts +38 -1
  91. package/dist/mcp/index.js +36 -3
  92. package/dist/mcp/mcpRegistryClient.d.ts +332 -0
  93. package/dist/mcp/mcpRegistryClient.js +488 -0
  94. package/dist/mcp/mcpServerBase.d.ts +227 -0
  95. package/dist/mcp/mcpServerBase.js +373 -0
  96. package/dist/mcp/multiServerManager.d.ts +310 -0
  97. package/dist/mcp/multiServerManager.js +579 -0
  98. package/dist/mcp/routing/index.d.ts +11 -0
  99. package/dist/mcp/routing/index.js +10 -0
  100. package/dist/mcp/routing/toolRouter.d.ts +219 -0
  101. package/dist/mcp/routing/toolRouter.js +416 -0
  102. package/dist/mcp/serverCapabilities.d.ts +341 -0
  103. package/dist/mcp/serverCapabilities.js +502 -0
  104. package/dist/mcp/toolAnnotations.d.ts +154 -0
  105. package/dist/mcp/toolAnnotations.js +239 -0
  106. package/dist/mcp/toolConverter.d.ts +178 -0
  107. package/dist/mcp/toolConverter.js +258 -0
  108. package/dist/mcp/toolIntegration.d.ts +136 -0
  109. package/dist/mcp/toolIntegration.js +334 -0
  110. package/dist/memory/hippocampusInitializer.d.ts +2 -2
  111. package/dist/memory/hippocampusInitializer.js +1 -1
  112. package/dist/neurolink.d.ts +275 -2
  113. package/dist/neurolink.js +596 -56
  114. package/dist/providers/litellm.d.ts +10 -0
  115. package/dist/providers/litellm.js +104 -2
  116. package/dist/types/configTypes.d.ts +56 -0
  117. package/dist/types/conversation.d.ts +2 -2
  118. package/dist/types/generateTypes.d.ts +4 -0
  119. package/dist/types/index.d.ts +2 -1
  120. package/dist/types/streamTypes.d.ts +2 -0
  121. package/dist/types/tools.d.ts +2 -0
  122. package/dist/utils/pricing.js +177 -17
  123. package/dist/utils/schemaConversion.d.ts +6 -1
  124. package/dist/utils/schemaConversion.js +50 -28
  125. package/package.json +2 -2
@@ -9,11 +9,12 @@ import { type CompactionConfig, type CompactionResult } from "./context/contextC
9
9
  import { ConversationMemoryManager } from "./core/conversationMemoryManager.js";
10
10
  import type { RedisConversationMemoryManager } from "./core/redisConversationMemoryManager.js";
11
11
  import { ExternalServerManager } from "./mcp/externalServerManager.js";
12
+ import type { MCPToolAnnotations } from "./mcp/toolAnnotations.js";
12
13
  import { MCPToolRegistry } from "./mcp/toolRegistry.js";
13
14
  import type { MetricsSummary, TraceView } from "./observability/metricsAggregator.js";
14
15
  import type { SpanData } from "./observability/types/spanTypes.js";
15
16
  import type { JsonObject, NeuroLinkEvents, TypedEventEmitter } from "./types/common.js";
16
- import type { NeurolinkConstructorConfig } from "./types/configTypes.js";
17
+ import type { MCPEnhancementsConfig, NeurolinkConstructorConfig } from "./types/configTypes.js";
17
18
  import type { ChatMessage } from "./types/conversation.js";
18
19
  import type { ExternalMCPOperationResult, ExternalMCPServerInstance, ExternalMCPToolInfo } from "./types/externalMcp.js";
19
20
  import type { GenerateOptions, GenerateResult } from "./types/generateTypes.js";
@@ -36,6 +37,13 @@ export declare class NeuroLink {
36
37
  private lastCompactionMessageCount;
37
38
  /** Extract sessionId from options context for compaction watermark keying */
38
39
  private getCompactionSessionId;
40
+ private mcpToolResultCache?;
41
+ private mcpToolRouter?;
42
+ private mcpToolBatcher?;
43
+ private mcpEnhancedDiscovery?;
44
+ private mcpToolMiddlewares;
45
+ private _disableToolCacheForCurrentRequest;
46
+ private mcpEnhancementsConfig?;
39
47
  private toolCircuitBreakers;
40
48
  private toolExecutionMetrics;
41
49
  private currentStreamToolExecutions;
@@ -157,6 +165,11 @@ export declare class NeuroLink {
157
165
  * Initialize HITL (Human-in-the-Loop) if enabled
158
166
  */
159
167
  private initializeHITL;
168
+ /**
169
+ * Initialize MCP enhancement modules (cache, router, batcher, discovery).
170
+ * Wires standalone MCP modules into the core SDK execution path.
171
+ */
172
+ private initializeMCPEnhancements;
160
173
  /**
161
174
  * Register file reference tools with the MCP tool registry.
162
175
  *
@@ -921,6 +934,25 @@ export declare class NeuroLink {
921
934
  * @returns true if the tool was removed, false if it didn't exist
922
935
  */
923
936
  unregisterTool(name: string): boolean;
937
+ /**
938
+ * Register a global tool middleware that runs on every tool execution.
939
+ * Middleware receives the tool, params, context, and a next() function.
940
+ * @param middleware - The middleware function to register
941
+ * @returns this (for chaining)
942
+ */
943
+ useToolMiddleware(middleware: import("./mcp/toolIntegration.js").ToolMiddleware): this;
944
+ /**
945
+ * Get all registered tool middlewares
946
+ */
947
+ getToolMiddlewares(): import("./mcp/toolIntegration.js").ToolMiddleware[];
948
+ /**
949
+ * Flush any pending batched tool calls immediately
950
+ */
951
+ flushToolBatch(): Promise<void>;
952
+ /**
953
+ * Get the current MCP enhancements configuration
954
+ */
955
+ getMCPEnhancementsConfig(): MCPEnhancementsConfig | undefined;
924
956
  /**
925
957
  * Update agentic loop report metadata for a conversation session.
926
958
  * Upserts a report entry by reportId — updates existing or adds new.
@@ -996,6 +1028,10 @@ export declare class NeuroLink {
996
1028
  timeout?: number;
997
1029
  maxRetries?: number;
998
1030
  retryDelayMs?: number;
1031
+ /** Disable tool result caching for this call */
1032
+ disableToolCache?: boolean;
1033
+ /** Bypass the request batcher for this call */
1034
+ bypassBatcher?: boolean;
999
1035
  authContext?: {
1000
1036
  userId?: string;
1001
1037
  sessionId?: string;
@@ -1004,9 +1040,18 @@ export declare class NeuroLink {
1004
1040
  };
1005
1041
  }): Promise<T>;
1006
1042
  /**
1007
- * Internal tool execution method (extracted for better error handling)
1043
+ * Internal tool execution method with MCP enhancements wired in:
1044
+ * - ToolCache: check/store cached results for non-destructive tools
1045
+ * - ToolRouter: route to best server when same tool exists on multiple servers
1046
+ * - Annotations: skip cache for destructive tools, retry safe tools on failure
1047
+ * - Middleware: apply global middleware chain before execution
1008
1048
  */
1009
1049
  private executeToolInternal;
1050
+ /**
1051
+ * Get tool annotations for execution decisions (cache, retry).
1052
+ * Checks cached tool list first, falls back to inference from tool name.
1053
+ */
1054
+ private getToolAnnotationsForExecution;
1010
1055
  /**
1011
1056
  * Get all available tools including custom and in-memory ones
1012
1057
  * @returns Array of available tools with metadata
@@ -1343,6 +1388,234 @@ export declare class NeuroLink {
1343
1388
  * Called automatically on process exit
1344
1389
  */
1345
1390
  shutdownExternalMCPServers(): Promise<void>;
1391
+ /**
1392
+ * Get the global elicitation manager for interactive tool input
1393
+ * Elicitation allows tools to request additional information from users during execution
1394
+ * @returns The global ElicitationManager instance
1395
+ * @example
1396
+ * ```typescript
1397
+ * const elicitationManager = neurolink.getElicitationManager();
1398
+ *
1399
+ * // Register a handler for confirmations
1400
+ * elicitationManager.registerHandler(async (request) => {
1401
+ * if (request.type === 'confirmation') {
1402
+ * const answer = await askUser(request.message);
1403
+ * return { confirmed: answer === 'yes' };
1404
+ * }
1405
+ * });
1406
+ * ```
1407
+ */
1408
+ getElicitationManager(): Promise<any>;
1409
+ /**
1410
+ * Register an elicitation handler for interactive tool input
1411
+ * Handlers are called when tools need user input during execution
1412
+ * @param handler - Function to handle elicitation requests
1413
+ * @example
1414
+ * ```typescript
1415
+ * neurolink.registerElicitationHandler(async (request) => {
1416
+ * switch (request.type) {
1417
+ * case 'confirmation':
1418
+ * return { confirmed: await confirmWithUser(request.message) };
1419
+ * case 'text':
1420
+ * return { value: await promptUser(request.message) };
1421
+ * case 'select':
1422
+ * return { value: await selectFromOptions(request.options) };
1423
+ * }
1424
+ * });
1425
+ * ```
1426
+ */
1427
+ registerElicitationHandler(handler: (request: unknown) => Promise<unknown>): Promise<void>;
1428
+ /**
1429
+ * Get the multi-server manager for load balancing and coordination
1430
+ * Allows managing multiple MCP servers with failover and load balancing
1431
+ * @returns The global MultiServerManager instance
1432
+ * @example
1433
+ * ```typescript
1434
+ * const multiServer = neurolink.getMultiServerManager();
1435
+ *
1436
+ * // Create a server group with load balancing
1437
+ * await multiServer.createServerGroup('ai-tools', {
1438
+ * servers: ['openai-server', 'anthropic-server'],
1439
+ * strategy: 'round-robin'
1440
+ * });
1441
+ * ```
1442
+ */
1443
+ getMultiServerManager(): Promise<any>;
1444
+ /**
1445
+ * Get the enhanced tool discovery service
1446
+ * Provides advanced search, filtering, and compatibility checking for tools
1447
+ * @returns EnhancedToolDiscovery instance
1448
+ * @example
1449
+ * ```typescript
1450
+ * const discovery = neurolink.getEnhancedToolDiscovery();
1451
+ *
1452
+ * // Search for tools by criteria
1453
+ * const results = await discovery.searchTools({
1454
+ * category: 'data-processing',
1455
+ * capabilities: ['streaming', 'batch'],
1456
+ * minReliability: 0.9
1457
+ * });
1458
+ * ```
1459
+ */
1460
+ getEnhancedToolDiscovery(): Promise<any>;
1461
+ /**
1462
+ * Get the MCP registry client for discovering servers from registries
1463
+ * Supports multiple registry sources (official, community, custom)
1464
+ * @returns The global MCPRegistryClient instance
1465
+ * @example
1466
+ * ```typescript
1467
+ * const registryClient = neurolink.getMCPRegistryClient();
1468
+ *
1469
+ * // Search for servers
1470
+ * const servers = await registryClient.searchServers({
1471
+ * query: 'database',
1472
+ * categories: ['data', 'storage']
1473
+ * });
1474
+ *
1475
+ * // Get a well-known server config
1476
+ * const githubServer = registryClient.getWellKnownServer('github');
1477
+ * ```
1478
+ */
1479
+ getMCPRegistryClient(): Promise<any>;
1480
+ /**
1481
+ * Expose a NeuroLink agent as an MCP tool
1482
+ * This allows agents to be called by other systems via MCP
1483
+ * @param agent - The agent to expose (must include id, name, description, and execute)
1484
+ * @param options - Exposure configuration options (prefix, defaultAnnotations, etc.)
1485
+ * @returns The exposed tool definition
1486
+ * @example
1487
+ * ```typescript
1488
+ * const agent = {
1489
+ * id: 'my-agent',
1490
+ * name: 'My Agent',
1491
+ * description: 'An agent that processes data',
1492
+ * execute: async (params) => { ... }
1493
+ * };
1494
+ * const tool = await neurolink.exposeAgentAsTool(agent, {
1495
+ * prefix: 'agent_'
1496
+ * });
1497
+ * ```
1498
+ */
1499
+ exposeAgentAsTool(agent: {
1500
+ id: string;
1501
+ name: string;
1502
+ description: string;
1503
+ execute: (params: unknown, context?: unknown) => Promise<unknown>;
1504
+ }, options?: {
1505
+ prefix?: string;
1506
+ includeMetadataInDescription?: boolean;
1507
+ wrapWithContext?: boolean;
1508
+ executionTimeout?: number;
1509
+ enableLogging?: boolean;
1510
+ }): Promise<import("./index.js").ExposureResult>;
1511
+ /**
1512
+ * Expose a workflow as an MCP tool
1513
+ * This allows workflows to be called by other systems via MCP
1514
+ * @param workflow - The workflow to expose (must include id, name, description, and execute)
1515
+ * @param options - Exposure configuration options (prefix, defaultAnnotations, etc.)
1516
+ * @returns The exposed tool definition
1517
+ * @example
1518
+ * ```typescript
1519
+ * const workflow = {
1520
+ * id: 'data-pipeline',
1521
+ * name: 'Data Pipeline',
1522
+ * description: 'Runs the data processing pipeline',
1523
+ * execute: async (params) => { ... }
1524
+ * };
1525
+ * const tool = await neurolink.exposeWorkflowAsTool(workflow, {
1526
+ * prefix: 'workflow_'
1527
+ * });
1528
+ * ```
1529
+ */
1530
+ exposeWorkflowAsTool(workflow: {
1531
+ id: string;
1532
+ name: string;
1533
+ description: string;
1534
+ execute: (params: unknown, context?: unknown) => Promise<unknown>;
1535
+ steps?: Array<{
1536
+ id: string;
1537
+ name: string;
1538
+ description?: string;
1539
+ }>;
1540
+ }, options?: {
1541
+ prefix?: string;
1542
+ includeMetadataInDescription?: boolean;
1543
+ wrapWithContext?: boolean;
1544
+ executionTimeout?: number;
1545
+ enableLogging?: boolean;
1546
+ }): Promise<import("./index.js").ExposureResult>;
1547
+ /**
1548
+ * Get the tool integration manager for middleware and elicitation
1549
+ * Provides advanced tool wrapping with confirmation, timeout, retry, etc.
1550
+ * @returns The global ToolIntegrationManager instance
1551
+ * @example
1552
+ * ```typescript
1553
+ * const integration = neurolink.getToolIntegrationManager();
1554
+ *
1555
+ * // Register a tool with middleware
1556
+ * integration.registerTool(myTool, {
1557
+ * timeout: 30000,
1558
+ * retries: 3,
1559
+ * requireConfirmation: true
1560
+ * });
1561
+ * ```
1562
+ */
1563
+ getToolIntegrationManager(): Promise<any>;
1564
+ /**
1565
+ * Convert NeuroLink tools to MCP format
1566
+ * Useful for exposing local tools to external MCP clients
1567
+ * @param tools - Array of NeuroLink tool definitions
1568
+ * @param options - Conversion options
1569
+ * @returns Array of MCP-formatted tools
1570
+ * @example
1571
+ * ```typescript
1572
+ * const mcpTools = neurolink.convertToolsToMCPFormat([
1573
+ * { name: 'myTool', description: 'Does something', execute: async () => {} }
1574
+ * ]);
1575
+ * ```
1576
+ */
1577
+ convertToolsToMCPFormat(tools: Array<{
1578
+ name: string;
1579
+ description: string;
1580
+ execute?: (params: unknown) => unknown;
1581
+ }>, options?: {
1582
+ namespacePrefix?: string;
1583
+ }): Promise<any>;
1584
+ /**
1585
+ * Convert MCP tools to NeuroLink format
1586
+ * Useful for importing tools from external MCP servers
1587
+ * @param tools - Array of MCP tool definitions
1588
+ * @param options - Conversion options
1589
+ * @returns Array of NeuroLink-formatted tools
1590
+ * @example
1591
+ * ```typescript
1592
+ * const neurolinkTools = neurolink.convertToolsFromMCPFormat(externalTools, {
1593
+ * removeNamespacePrefix: 'external_'
1594
+ * });
1595
+ * ```
1596
+ */
1597
+ convertToolsFromMCPFormat(tools: Array<{
1598
+ name: string;
1599
+ description: string;
1600
+ inputSchema?: unknown;
1601
+ }>, options?: {
1602
+ removeNamespacePrefix?: string;
1603
+ }): Promise<any>;
1604
+ /**
1605
+ * Get tool annotations and safety information
1606
+ * Provides insights about tool behavior, safety levels, and retry-ability
1607
+ * @param toolName - Name of the tool to analyze
1608
+ * @returns Tool annotation summary
1609
+ * @example
1610
+ * ```typescript
1611
+ * const annotations = await neurolink.getToolAnnotations('deleteFile');
1612
+ * // Returns: { destructive: true, requiresConfirmation: true, safeToRetry: false }
1613
+ * ```
1614
+ */
1615
+ getToolAnnotations(toolName: string): Promise<{
1616
+ annotations: MCPToolAnnotations;
1617
+ summary: string;
1618
+ } | null>;
1346
1619
  /**
1347
1620
  * Convert external MCP tools to Vercel AI SDK tool format
1348
1621
  * This allows AI providers to use external tools directly