@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.
- package/CHANGELOG.md +12 -0
- package/README.md +59 -9
- package/dist/cli/commands/config.d.ts +4 -4
- package/dist/cli/commands/mcp.d.ts +87 -0
- package/dist/cli/commands/mcp.js +1524 -0
- package/dist/cli/loop/optionsSchema.js +4 -0
- package/dist/core/modules/ToolsManager.js +29 -2
- package/dist/index.d.ts +2 -1
- package/dist/index.js +27 -1
- package/dist/lib/core/modules/ToolsManager.js +29 -2
- package/dist/lib/index.d.ts +2 -1
- package/dist/lib/index.js +27 -1
- package/dist/lib/mcp/agentExposure.d.ts +228 -0
- package/dist/lib/mcp/agentExposure.js +357 -0
- package/dist/lib/mcp/batching/index.d.ts +11 -0
- package/dist/lib/mcp/batching/index.js +11 -0
- package/dist/lib/mcp/batching/requestBatcher.d.ts +202 -0
- package/dist/lib/mcp/batching/requestBatcher.js +442 -0
- package/dist/lib/mcp/caching/index.d.ts +11 -0
- package/dist/lib/mcp/caching/index.js +11 -0
- package/dist/lib/mcp/caching/toolCache.d.ts +221 -0
- package/dist/lib/mcp/caching/toolCache.js +434 -0
- package/dist/lib/mcp/elicitation/elicitationManager.d.ts +169 -0
- package/dist/lib/mcp/elicitation/elicitationManager.js +377 -0
- package/dist/lib/mcp/elicitation/index.d.ts +11 -0
- package/dist/lib/mcp/elicitation/index.js +12 -0
- package/dist/lib/mcp/elicitation/types.d.ts +278 -0
- package/dist/lib/mcp/elicitation/types.js +11 -0
- package/dist/lib/mcp/elicitationProtocol.d.ts +228 -0
- package/dist/lib/mcp/elicitationProtocol.js +376 -0
- package/dist/lib/mcp/enhancedToolDiscovery.d.ts +205 -0
- package/dist/lib/mcp/enhancedToolDiscovery.js +482 -0
- package/dist/lib/mcp/index.d.ts +38 -1
- package/dist/lib/mcp/index.js +36 -3
- package/dist/lib/mcp/mcpRegistryClient.d.ts +332 -0
- package/dist/lib/mcp/mcpRegistryClient.js +489 -0
- package/dist/lib/mcp/mcpServerBase.d.ts +227 -0
- package/dist/lib/mcp/mcpServerBase.js +374 -0
- package/dist/lib/mcp/multiServerManager.d.ts +310 -0
- package/dist/lib/mcp/multiServerManager.js +580 -0
- package/dist/lib/mcp/routing/index.d.ts +11 -0
- package/dist/lib/mcp/routing/index.js +11 -0
- package/dist/lib/mcp/routing/toolRouter.d.ts +219 -0
- package/dist/lib/mcp/routing/toolRouter.js +417 -0
- package/dist/lib/mcp/serverCapabilities.d.ts +341 -0
- package/dist/lib/mcp/serverCapabilities.js +503 -0
- package/dist/lib/mcp/toolAnnotations.d.ts +154 -0
- package/dist/lib/mcp/toolAnnotations.js +240 -0
- package/dist/lib/mcp/toolConverter.d.ts +178 -0
- package/dist/lib/mcp/toolConverter.js +259 -0
- package/dist/lib/mcp/toolIntegration.d.ts +136 -0
- package/dist/lib/mcp/toolIntegration.js +335 -0
- package/dist/lib/memory/hippocampusInitializer.d.ts +2 -2
- package/dist/lib/memory/hippocampusInitializer.js +1 -1
- package/dist/lib/neurolink.d.ts +275 -2
- package/dist/lib/neurolink.js +596 -56
- package/dist/lib/providers/litellm.d.ts +10 -0
- package/dist/lib/providers/litellm.js +104 -2
- package/dist/lib/types/configTypes.d.ts +56 -0
- package/dist/lib/types/conversation.d.ts +2 -2
- package/dist/lib/types/generateTypes.d.ts +4 -0
- package/dist/lib/types/index.d.ts +2 -1
- package/dist/lib/types/modelTypes.d.ts +6 -6
- package/dist/lib/types/streamTypes.d.ts +2 -0
- package/dist/lib/types/tools.d.ts +2 -0
- package/dist/lib/utils/pricing.js +177 -17
- package/dist/lib/utils/schemaConversion.d.ts +6 -1
- package/dist/lib/utils/schemaConversion.js +50 -28
- package/dist/lib/workflow/config.d.ts +16 -16
- package/dist/mcp/agentExposure.d.ts +228 -0
- package/dist/mcp/agentExposure.js +356 -0
- package/dist/mcp/batching/index.d.ts +11 -0
- package/dist/mcp/batching/index.js +10 -0
- package/dist/mcp/batching/requestBatcher.d.ts +202 -0
- package/dist/mcp/batching/requestBatcher.js +441 -0
- package/dist/mcp/caching/index.d.ts +11 -0
- package/dist/mcp/caching/index.js +10 -0
- package/dist/mcp/caching/toolCache.d.ts +221 -0
- package/dist/mcp/caching/toolCache.js +433 -0
- package/dist/mcp/elicitation/elicitationManager.d.ts +169 -0
- package/dist/mcp/elicitation/elicitationManager.js +376 -0
- package/dist/mcp/elicitation/index.d.ts +11 -0
- package/dist/mcp/elicitation/index.js +11 -0
- package/dist/mcp/elicitation/types.d.ts +278 -0
- package/dist/mcp/elicitation/types.js +10 -0
- package/dist/mcp/elicitationProtocol.d.ts +228 -0
- package/dist/mcp/elicitationProtocol.js +375 -0
- package/dist/mcp/enhancedToolDiscovery.d.ts +205 -0
- package/dist/mcp/enhancedToolDiscovery.js +481 -0
- package/dist/mcp/index.d.ts +38 -1
- package/dist/mcp/index.js +36 -3
- package/dist/mcp/mcpRegistryClient.d.ts +332 -0
- package/dist/mcp/mcpRegistryClient.js +488 -0
- package/dist/mcp/mcpServerBase.d.ts +227 -0
- package/dist/mcp/mcpServerBase.js +373 -0
- package/dist/mcp/multiServerManager.d.ts +310 -0
- package/dist/mcp/multiServerManager.js +579 -0
- package/dist/mcp/routing/index.d.ts +11 -0
- package/dist/mcp/routing/index.js +10 -0
- package/dist/mcp/routing/toolRouter.d.ts +219 -0
- package/dist/mcp/routing/toolRouter.js +416 -0
- package/dist/mcp/serverCapabilities.d.ts +341 -0
- package/dist/mcp/serverCapabilities.js +502 -0
- package/dist/mcp/toolAnnotations.d.ts +154 -0
- package/dist/mcp/toolAnnotations.js +239 -0
- package/dist/mcp/toolConverter.d.ts +178 -0
- package/dist/mcp/toolConverter.js +258 -0
- package/dist/mcp/toolIntegration.d.ts +136 -0
- package/dist/mcp/toolIntegration.js +334 -0
- package/dist/memory/hippocampusInitializer.d.ts +2 -2
- package/dist/memory/hippocampusInitializer.js +1 -1
- package/dist/neurolink.d.ts +275 -2
- package/dist/neurolink.js +596 -56
- package/dist/providers/litellm.d.ts +10 -0
- package/dist/providers/litellm.js +104 -2
- package/dist/types/configTypes.d.ts +56 -0
- package/dist/types/conversation.d.ts +2 -2
- package/dist/types/generateTypes.d.ts +4 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/streamTypes.d.ts +2 -0
- package/dist/types/tools.d.ts +2 -0
- package/dist/utils/pricing.js +177 -17
- package/dist/utils/schemaConversion.d.ts +6 -1
- package/dist/utils/schemaConversion.js +50 -28
- package/package.json +2 -2
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent and Workflow Exposure as MCP Tools
|
|
3
|
+
*
|
|
4
|
+
* Enables exposing NeuroLink agents and workflows as MCP tools,
|
|
5
|
+
* allowing external MCP clients to invoke complex AI operations
|
|
6
|
+
* through the standardized MCP protocol.
|
|
7
|
+
*
|
|
8
|
+
* @module mcp/agentExposure
|
|
9
|
+
* @since 8.39.0
|
|
10
|
+
*/
|
|
11
|
+
import { logger } from "../utils/logger.js";
|
|
12
|
+
import { withTimeout } from "../utils/async/withTimeout.js";
|
|
13
|
+
import { ErrorFactory } from "../utils/errorHandling.js";
|
|
14
|
+
/**
|
|
15
|
+
* Expose an agent as an MCP tool
|
|
16
|
+
*/
|
|
17
|
+
export function exposeAgentAsTool(agent, options = {}) {
|
|
18
|
+
const { prefix = "agent", defaultAnnotations = {}, includeMetadataInDescription = true, nameTransformer = (name) => name.toLowerCase().replace(/\s+/g, "_"), wrapWithContext = true, executionTimeout = 300000, // 5 minutes default
|
|
19
|
+
enableLogging = true, } = options;
|
|
20
|
+
// Generate tool name
|
|
21
|
+
const baseName = nameTransformer(agent.name);
|
|
22
|
+
const toolName = prefix ? `${prefix}_${baseName}` : baseName;
|
|
23
|
+
// Build description
|
|
24
|
+
let description = agent.description;
|
|
25
|
+
if (includeMetadataInDescription && agent.metadata) {
|
|
26
|
+
const metaParts = [];
|
|
27
|
+
if (agent.metadata.version) {
|
|
28
|
+
metaParts.push(`v${agent.metadata.version}`);
|
|
29
|
+
}
|
|
30
|
+
if (agent.metadata.category) {
|
|
31
|
+
metaParts.push(`category: ${agent.metadata.category}`);
|
|
32
|
+
}
|
|
33
|
+
if (agent.metadata.estimatedDuration) {
|
|
34
|
+
metaParts.push(`~${agent.metadata.estimatedDuration}ms`);
|
|
35
|
+
}
|
|
36
|
+
if (metaParts.length > 0) {
|
|
37
|
+
description += ` [${metaParts.join(", ")}]`;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
// Build annotations
|
|
41
|
+
const annotations = {
|
|
42
|
+
...defaultAnnotations,
|
|
43
|
+
complexity: defaultAnnotations.complexity ?? "complex",
|
|
44
|
+
estimatedDuration: agent.metadata?.estimatedDuration ?? defaultAnnotations.estimatedDuration,
|
|
45
|
+
costHint: agent.metadata?.costHint ?? defaultAnnotations.costHint,
|
|
46
|
+
tags: [
|
|
47
|
+
...(defaultAnnotations.tags ?? []),
|
|
48
|
+
"agent",
|
|
49
|
+
...(agent.metadata?.tags ?? []),
|
|
50
|
+
],
|
|
51
|
+
};
|
|
52
|
+
// Build input schema with agent context
|
|
53
|
+
const inputSchema = agent.inputSchema ?? {
|
|
54
|
+
type: "object",
|
|
55
|
+
properties: {},
|
|
56
|
+
};
|
|
57
|
+
// Create execution wrapper
|
|
58
|
+
const execute = async (params, context) => {
|
|
59
|
+
const startTime = Date.now();
|
|
60
|
+
if (enableLogging) {
|
|
61
|
+
logger.debug(`[AgentExposure] Executing agent '${agent.id}' as tool '${toolName}'`);
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
// Execute agent with context wrapper if enabled
|
|
65
|
+
const existingConfig = context?.config && typeof context.config === "object"
|
|
66
|
+
? context.config
|
|
67
|
+
: {};
|
|
68
|
+
const executionContext = wrapWithContext
|
|
69
|
+
? {
|
|
70
|
+
...context,
|
|
71
|
+
config: {
|
|
72
|
+
...existingConfig,
|
|
73
|
+
sourceType: "mcp-exposed-agent",
|
|
74
|
+
agentId: agent.id,
|
|
75
|
+
toolName,
|
|
76
|
+
},
|
|
77
|
+
}
|
|
78
|
+
: (context ?? {});
|
|
79
|
+
const result = await withTimeout(agent.execute(params, executionContext), executionTimeout);
|
|
80
|
+
const duration = Date.now() - startTime;
|
|
81
|
+
if (enableLogging) {
|
|
82
|
+
logger.debug(`[AgentExposure] Agent '${agent.id}' completed in ${duration}ms`);
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
success: true,
|
|
86
|
+
data: result,
|
|
87
|
+
metadata: {
|
|
88
|
+
agentId: agent.id,
|
|
89
|
+
toolName,
|
|
90
|
+
executionTime: duration,
|
|
91
|
+
sourceType: "agent",
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
const duration = Date.now() - startTime;
|
|
97
|
+
const agentError = error instanceof Error
|
|
98
|
+
? error
|
|
99
|
+
: ErrorFactory.toolExecutionFailed(toolName, new Error(String(error)));
|
|
100
|
+
if (enableLogging) {
|
|
101
|
+
logger.error(`[AgentExposure] Agent '${agent.id}' failed after ${duration}ms: ${agentError.message}`);
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
success: false,
|
|
105
|
+
error: agentError.message,
|
|
106
|
+
metadata: {
|
|
107
|
+
agentId: agent.id,
|
|
108
|
+
toolName,
|
|
109
|
+
executionTime: duration,
|
|
110
|
+
sourceType: "agent",
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
const tool = {
|
|
116
|
+
name: toolName,
|
|
117
|
+
description,
|
|
118
|
+
inputSchema,
|
|
119
|
+
outputSchema: agent.outputSchema,
|
|
120
|
+
annotations,
|
|
121
|
+
execute,
|
|
122
|
+
metadata: {
|
|
123
|
+
sourceType: "agent",
|
|
124
|
+
sourceId: agent.id,
|
|
125
|
+
originalName: agent.name,
|
|
126
|
+
...agent.metadata,
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
return {
|
|
130
|
+
tool,
|
|
131
|
+
sourceType: "agent",
|
|
132
|
+
sourceId: agent.id,
|
|
133
|
+
toolName,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Expose a workflow as an MCP tool
|
|
138
|
+
*/
|
|
139
|
+
export function exposeWorkflowAsTool(workflow, options = {}) {
|
|
140
|
+
const { prefix = "workflow", defaultAnnotations = {}, includeMetadataInDescription = true, nameTransformer = (name) => name.toLowerCase().replace(/\s+/g, "_"), wrapWithContext = true, executionTimeout = 600000, // 10 minutes default for workflows
|
|
141
|
+
enableLogging = true, } = options;
|
|
142
|
+
// Generate tool name
|
|
143
|
+
const baseName = nameTransformer(workflow.name);
|
|
144
|
+
const toolName = prefix ? `${prefix}_${baseName}` : baseName;
|
|
145
|
+
// Build description
|
|
146
|
+
let description = workflow.description;
|
|
147
|
+
if (includeMetadataInDescription) {
|
|
148
|
+
const metaParts = [];
|
|
149
|
+
if (workflow.metadata?.version) {
|
|
150
|
+
metaParts.push(`v${workflow.metadata.version}`);
|
|
151
|
+
}
|
|
152
|
+
if (workflow.steps?.length) {
|
|
153
|
+
metaParts.push(`${workflow.steps.length} steps`);
|
|
154
|
+
}
|
|
155
|
+
if (workflow.metadata?.estimatedDuration) {
|
|
156
|
+
metaParts.push(`~${workflow.metadata.estimatedDuration}ms`);
|
|
157
|
+
}
|
|
158
|
+
if (metaParts.length > 0) {
|
|
159
|
+
description += ` [${metaParts.join(", ")}]`;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
// Build annotations
|
|
163
|
+
const annotations = {
|
|
164
|
+
...defaultAnnotations,
|
|
165
|
+
complexity: defaultAnnotations.complexity ?? "complex",
|
|
166
|
+
estimatedDuration: workflow.metadata?.estimatedDuration ??
|
|
167
|
+
defaultAnnotations.estimatedDuration,
|
|
168
|
+
idempotentHint: workflow.metadata?.idempotent ?? defaultAnnotations.idempotentHint,
|
|
169
|
+
tags: [
|
|
170
|
+
...(defaultAnnotations.tags ?? []),
|
|
171
|
+
"workflow",
|
|
172
|
+
...(workflow.metadata?.tags ?? []),
|
|
173
|
+
],
|
|
174
|
+
};
|
|
175
|
+
// Build input schema
|
|
176
|
+
const inputSchema = workflow.inputSchema ?? {
|
|
177
|
+
type: "object",
|
|
178
|
+
properties: {},
|
|
179
|
+
};
|
|
180
|
+
// Create execution wrapper
|
|
181
|
+
const execute = async (params, context) => {
|
|
182
|
+
const startTime = Date.now();
|
|
183
|
+
if (enableLogging) {
|
|
184
|
+
logger.debug(`[WorkflowExposure] Executing workflow '${workflow.id}' as tool '${toolName}'`);
|
|
185
|
+
}
|
|
186
|
+
try {
|
|
187
|
+
// Execute workflow with context wrapper if enabled
|
|
188
|
+
const existingConfig = context?.config && typeof context.config === "object"
|
|
189
|
+
? context.config
|
|
190
|
+
: {};
|
|
191
|
+
const executionContext = wrapWithContext
|
|
192
|
+
? {
|
|
193
|
+
...context,
|
|
194
|
+
config: {
|
|
195
|
+
...existingConfig,
|
|
196
|
+
sourceType: "mcp-exposed-workflow",
|
|
197
|
+
workflowId: workflow.id,
|
|
198
|
+
toolName,
|
|
199
|
+
},
|
|
200
|
+
}
|
|
201
|
+
: (context ?? {});
|
|
202
|
+
const result = await withTimeout(workflow.execute(params, executionContext), executionTimeout);
|
|
203
|
+
const duration = Date.now() - startTime;
|
|
204
|
+
if (enableLogging) {
|
|
205
|
+
logger.debug(`[WorkflowExposure] Workflow '${workflow.id}' completed in ${duration}ms`);
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
success: true,
|
|
209
|
+
data: result,
|
|
210
|
+
metadata: {
|
|
211
|
+
workflowId: workflow.id,
|
|
212
|
+
toolName,
|
|
213
|
+
executionTime: duration,
|
|
214
|
+
sourceType: "workflow",
|
|
215
|
+
stepsCount: workflow.steps?.length,
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
catch (error) {
|
|
220
|
+
const duration = Date.now() - startTime;
|
|
221
|
+
const workflowError = error instanceof Error
|
|
222
|
+
? error
|
|
223
|
+
: ErrorFactory.toolExecutionFailed(toolName, new Error(String(error)));
|
|
224
|
+
if (enableLogging) {
|
|
225
|
+
logger.error(`[WorkflowExposure] Workflow '${workflow.id}' failed after ${duration}ms: ${workflowError.message}`);
|
|
226
|
+
}
|
|
227
|
+
return {
|
|
228
|
+
success: false,
|
|
229
|
+
error: workflowError.message,
|
|
230
|
+
metadata: {
|
|
231
|
+
workflowId: workflow.id,
|
|
232
|
+
toolName,
|
|
233
|
+
executionTime: duration,
|
|
234
|
+
sourceType: "workflow",
|
|
235
|
+
},
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
const tool = {
|
|
240
|
+
name: toolName,
|
|
241
|
+
description,
|
|
242
|
+
inputSchema,
|
|
243
|
+
outputSchema: workflow.outputSchema,
|
|
244
|
+
annotations,
|
|
245
|
+
execute,
|
|
246
|
+
metadata: {
|
|
247
|
+
sourceType: "workflow",
|
|
248
|
+
sourceId: workflow.id,
|
|
249
|
+
originalName: workflow.name,
|
|
250
|
+
steps: workflow.steps,
|
|
251
|
+
...workflow.metadata,
|
|
252
|
+
},
|
|
253
|
+
};
|
|
254
|
+
return {
|
|
255
|
+
tool,
|
|
256
|
+
sourceType: "workflow",
|
|
257
|
+
sourceId: workflow.id,
|
|
258
|
+
toolName,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Batch expose agents as MCP tools
|
|
263
|
+
*/
|
|
264
|
+
export function exposeAgentsAsTools(agents, options = {}) {
|
|
265
|
+
return agents.map((agent) => exposeAgentAsTool(agent, options));
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Batch expose workflows as MCP tools
|
|
269
|
+
*/
|
|
270
|
+
export function exposeWorkflowsAsTools(workflows, options = {}) {
|
|
271
|
+
return workflows.map((workflow) => exposeWorkflowAsTool(workflow, options));
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Agent Exposure Manager
|
|
275
|
+
*
|
|
276
|
+
* Manages the lifecycle of exposed agents and workflows,
|
|
277
|
+
* providing registration, lookup, and invocation capabilities.
|
|
278
|
+
*/
|
|
279
|
+
export class AgentExposureManager {
|
|
280
|
+
exposedTools = new Map();
|
|
281
|
+
options;
|
|
282
|
+
constructor(options = {}) {
|
|
283
|
+
this.options = options;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Expose an agent and register it
|
|
287
|
+
*/
|
|
288
|
+
exposeAgent(agent) {
|
|
289
|
+
const result = exposeAgentAsTool(agent, this.options);
|
|
290
|
+
this.exposedTools.set(result.toolName, result);
|
|
291
|
+
return result.tool;
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Expose a workflow and register it
|
|
295
|
+
*/
|
|
296
|
+
exposeWorkflow(workflow) {
|
|
297
|
+
const result = exposeWorkflowAsTool(workflow, this.options);
|
|
298
|
+
this.exposedTools.set(result.toolName, result);
|
|
299
|
+
return result.tool;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Get all exposed tools
|
|
303
|
+
*/
|
|
304
|
+
getExposedTools() {
|
|
305
|
+
return Array.from(this.exposedTools.values()).map((r) => r.tool);
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Get exposed tool by name
|
|
309
|
+
*/
|
|
310
|
+
getExposedTool(toolName) {
|
|
311
|
+
return this.exposedTools.get(toolName)?.tool;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Get exposure result by tool name
|
|
315
|
+
*/
|
|
316
|
+
getExposureResult(toolName) {
|
|
317
|
+
return this.exposedTools.get(toolName);
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Get tools by source type
|
|
321
|
+
*/
|
|
322
|
+
getToolsBySourceType(sourceType) {
|
|
323
|
+
return Array.from(this.exposedTools.values())
|
|
324
|
+
.filter((r) => r.sourceType === sourceType)
|
|
325
|
+
.map((r) => r.tool);
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Remove exposed tool
|
|
329
|
+
*/
|
|
330
|
+
unexpose(toolName) {
|
|
331
|
+
return this.exposedTools.delete(toolName);
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Clear all exposed tools
|
|
335
|
+
*/
|
|
336
|
+
clear() {
|
|
337
|
+
this.exposedTools.clear();
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Get statistics
|
|
341
|
+
*/
|
|
342
|
+
getStatistics() {
|
|
343
|
+
const results = Array.from(this.exposedTools.values());
|
|
344
|
+
return {
|
|
345
|
+
totalExposed: results.length,
|
|
346
|
+
exposedAgents: results.filter((r) => r.sourceType === "agent").length,
|
|
347
|
+
exposedWorkflows: results.filter((r) => r.sourceType === "workflow")
|
|
348
|
+
.length,
|
|
349
|
+
toolNames: results.map((r) => r.toolName),
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Global agent exposure manager instance
|
|
355
|
+
*/
|
|
356
|
+
export const globalAgentExposureManager = new AgentExposureManager();
|
|
357
|
+
//# sourceMappingURL=agentExposure.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Batching Module - Request batching for efficiency
|
|
3
|
+
*
|
|
4
|
+
* Provides intelligent batching of MCP tool calls:
|
|
5
|
+
* - Automatic batch size management
|
|
6
|
+
* - Configurable wait times
|
|
7
|
+
* - Server-grouped batching
|
|
8
|
+
* - Parallel batch execution
|
|
9
|
+
*/
|
|
10
|
+
export type { BatchConfig, BatchExecutor, BatcherEvents, BatchResult, } from "./requestBatcher.js";
|
|
11
|
+
export { createRequestBatcher, createToolCallBatcher, DEFAULT_BATCH_CONFIG, RequestBatcher, ToolCallBatcher, } from "./requestBatcher.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Batching Module - Request batching for efficiency
|
|
3
|
+
*
|
|
4
|
+
* Provides intelligent batching of MCP tool calls:
|
|
5
|
+
* - Automatic batch size management
|
|
6
|
+
* - Configurable wait times
|
|
7
|
+
* - Server-grouped batching
|
|
8
|
+
* - Parallel batch execution
|
|
9
|
+
*/
|
|
10
|
+
export { createRequestBatcher, createToolCallBatcher, DEFAULT_BATCH_CONFIG, RequestBatcher, ToolCallBatcher, } from "./requestBatcher.js";
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request Batcher - Batches multiple tool calls for efficiency
|
|
3
|
+
*
|
|
4
|
+
* Provides intelligent batching of MCP tool calls to reduce overhead
|
|
5
|
+
* and improve throughput. Supports automatic flushing based on:
|
|
6
|
+
* - Maximum batch size
|
|
7
|
+
* - Maximum wait time
|
|
8
|
+
* - Manual flush triggers
|
|
9
|
+
*/
|
|
10
|
+
import { EventEmitter } from "events";
|
|
11
|
+
/**
|
|
12
|
+
* Batch configuration options
|
|
13
|
+
*/
|
|
14
|
+
export type BatchConfig = {
|
|
15
|
+
/**
|
|
16
|
+
* Maximum number of requests to batch together (default: 10)
|
|
17
|
+
*/
|
|
18
|
+
maxBatchSize: number;
|
|
19
|
+
/**
|
|
20
|
+
* Maximum time to wait for a full batch in milliseconds (default: 100ms)
|
|
21
|
+
*/
|
|
22
|
+
maxWaitMs: number;
|
|
23
|
+
/**
|
|
24
|
+
* Enable parallel execution of batched requests (default: true).
|
|
25
|
+
* Reserved for future parallel batch execution; currently stored but not read.
|
|
26
|
+
*/
|
|
27
|
+
enableParallel?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Maximum concurrent batches in flight (default: 5)
|
|
30
|
+
*/
|
|
31
|
+
maxConcurrentBatches?: number;
|
|
32
|
+
/**
|
|
33
|
+
* Group requests by server ID (default: true)
|
|
34
|
+
*/
|
|
35
|
+
groupByServer?: boolean;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Batch execution result
|
|
39
|
+
*/
|
|
40
|
+
export type BatchResult<T> = {
|
|
41
|
+
id: string;
|
|
42
|
+
success: boolean;
|
|
43
|
+
result?: T;
|
|
44
|
+
error?: Error;
|
|
45
|
+
executionTime: number;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Batch executor function type
|
|
49
|
+
*/
|
|
50
|
+
export type BatchExecutor<T> = (requests: Array<{
|
|
51
|
+
tool: string;
|
|
52
|
+
args: unknown;
|
|
53
|
+
serverId?: string;
|
|
54
|
+
}>) => Promise<Array<{
|
|
55
|
+
success: boolean;
|
|
56
|
+
result?: T;
|
|
57
|
+
error?: Error;
|
|
58
|
+
}>>;
|
|
59
|
+
/**
|
|
60
|
+
* Batcher events
|
|
61
|
+
*/
|
|
62
|
+
export type BatcherEvents<T> = {
|
|
63
|
+
batchStarted: {
|
|
64
|
+
batchId: string;
|
|
65
|
+
size: number;
|
|
66
|
+
};
|
|
67
|
+
batchCompleted: {
|
|
68
|
+
batchId: string;
|
|
69
|
+
results: BatchResult<T>[];
|
|
70
|
+
};
|
|
71
|
+
batchFailed: {
|
|
72
|
+
batchId: string;
|
|
73
|
+
error: Error;
|
|
74
|
+
};
|
|
75
|
+
requestQueued: {
|
|
76
|
+
requestId: string;
|
|
77
|
+
queueSize: number;
|
|
78
|
+
};
|
|
79
|
+
flushTriggered: {
|
|
80
|
+
reason: "size" | "timeout" | "manual";
|
|
81
|
+
queueSize: number;
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Request Batcher - Efficient batch processing for MCP tool calls
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* const batcher = new RequestBatcher<ToolResult>({
|
|
90
|
+
* maxBatchSize: 10,
|
|
91
|
+
* maxWaitMs: 100,
|
|
92
|
+
* });
|
|
93
|
+
*
|
|
94
|
+
* // Set the batch executor
|
|
95
|
+
* batcher.setExecutor(async (requests) => {
|
|
96
|
+
* // Execute all requests in a batch
|
|
97
|
+
* return await Promise.all(requests.map(r => executeTool(r.tool, r.args)));
|
|
98
|
+
* });
|
|
99
|
+
*
|
|
100
|
+
* // Add requests - they'll be batched automatically
|
|
101
|
+
* const result1 = await batcher.add('getUserById', { id: 1 });
|
|
102
|
+
* const result2 = await batcher.add('getUserById', { id: 2 });
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export declare class RequestBatcher<T = unknown> extends EventEmitter {
|
|
106
|
+
private config;
|
|
107
|
+
private pending;
|
|
108
|
+
private serverQueues;
|
|
109
|
+
private flushTimer?;
|
|
110
|
+
private executor?;
|
|
111
|
+
private activeBatches;
|
|
112
|
+
private batchCounter;
|
|
113
|
+
private requestCounter;
|
|
114
|
+
private isDestroyed;
|
|
115
|
+
constructor(config: BatchConfig);
|
|
116
|
+
/**
|
|
117
|
+
* Set the batch executor function
|
|
118
|
+
*/
|
|
119
|
+
setExecutor(executor: BatchExecutor<T>): void;
|
|
120
|
+
/**
|
|
121
|
+
* Add a request to the batch queue
|
|
122
|
+
*/
|
|
123
|
+
add(tool: string, args: unknown, serverId?: string): Promise<T>;
|
|
124
|
+
/**
|
|
125
|
+
* Manually flush the current batch
|
|
126
|
+
*/
|
|
127
|
+
flush(): Promise<void>;
|
|
128
|
+
/**
|
|
129
|
+
* Get current queue size
|
|
130
|
+
*/
|
|
131
|
+
get queueSize(): number;
|
|
132
|
+
/**
|
|
133
|
+
* Get number of active batches
|
|
134
|
+
*/
|
|
135
|
+
get activeBatchCount(): number;
|
|
136
|
+
/**
|
|
137
|
+
* Check if the batcher is idle (no pending requests)
|
|
138
|
+
*/
|
|
139
|
+
get isIdle(): boolean;
|
|
140
|
+
/**
|
|
141
|
+
* Wait for all pending requests to complete
|
|
142
|
+
*/
|
|
143
|
+
drain(): Promise<void>;
|
|
144
|
+
/**
|
|
145
|
+
* Destroy the batcher and reject all pending requests
|
|
146
|
+
*/
|
|
147
|
+
destroy(): void;
|
|
148
|
+
private generateRequestId;
|
|
149
|
+
private generateBatchId;
|
|
150
|
+
private scheduleFlush;
|
|
151
|
+
private clearFlushTimer;
|
|
152
|
+
private executeBatch;
|
|
153
|
+
private selectBatchRequests;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Factory function to create a RequestBatcher instance
|
|
157
|
+
*/
|
|
158
|
+
export declare const createRequestBatcher: <T = unknown>(config: BatchConfig) => RequestBatcher<T>;
|
|
159
|
+
/**
|
|
160
|
+
* Default batcher configuration
|
|
161
|
+
*/
|
|
162
|
+
export declare const DEFAULT_BATCH_CONFIG: BatchConfig;
|
|
163
|
+
/**
|
|
164
|
+
* Tool Call Batcher - Specialized batcher for MCP tool calls
|
|
165
|
+
*/
|
|
166
|
+
export declare class ToolCallBatcher {
|
|
167
|
+
private batcher;
|
|
168
|
+
private toolExecutor?;
|
|
169
|
+
constructor(config?: Partial<BatchConfig>);
|
|
170
|
+
/**
|
|
171
|
+
* Set the tool executor function
|
|
172
|
+
*/
|
|
173
|
+
setToolExecutor(executor: (tool: string, args: unknown, serverId?: string) => Promise<unknown>): void;
|
|
174
|
+
/**
|
|
175
|
+
* Execute a tool call (will be batched automatically)
|
|
176
|
+
*/
|
|
177
|
+
execute(tool: string, args: unknown, serverId?: string): Promise<unknown>;
|
|
178
|
+
/**
|
|
179
|
+
* Flush pending tool calls
|
|
180
|
+
*/
|
|
181
|
+
flush(): Promise<void>;
|
|
182
|
+
/**
|
|
183
|
+
* Wait for all pending tool calls to complete
|
|
184
|
+
*/
|
|
185
|
+
drain(): Promise<void>;
|
|
186
|
+
/**
|
|
187
|
+
* Get current queue size
|
|
188
|
+
*/
|
|
189
|
+
get queueSize(): number;
|
|
190
|
+
/**
|
|
191
|
+
* Check if idle
|
|
192
|
+
*/
|
|
193
|
+
get isIdle(): boolean;
|
|
194
|
+
/**
|
|
195
|
+
* Destroy the batcher
|
|
196
|
+
*/
|
|
197
|
+
destroy(): void;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Create a tool call batcher instance
|
|
201
|
+
*/
|
|
202
|
+
export declare const createToolCallBatcher: (config?: Partial<BatchConfig>) => ToolCallBatcher;
|