@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,579 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multi-Server Manager
|
|
3
|
+
*
|
|
4
|
+
* Coordinates multiple MCP servers with load balancing, failover,
|
|
5
|
+
* and unified tool discovery across all registered servers.
|
|
6
|
+
*
|
|
7
|
+
* Features:
|
|
8
|
+
* - Load balancing strategies (round-robin, least-loaded, random)
|
|
9
|
+
* - Health-aware routing
|
|
10
|
+
* - Automatic failover
|
|
11
|
+
* - Unified tool namespace management
|
|
12
|
+
* - Cross-server tool discovery
|
|
13
|
+
*
|
|
14
|
+
* @module mcp/multiServerManager
|
|
15
|
+
* @since 8.39.0
|
|
16
|
+
*/
|
|
17
|
+
import { EventEmitter } from "events";
|
|
18
|
+
import { logger } from "../utils/logger.js";
|
|
19
|
+
import { ErrorFactory } from "../utils/errorHandling.js";
|
|
20
|
+
/**
|
|
21
|
+
* Multi-Server Manager
|
|
22
|
+
*
|
|
23
|
+
* Coordinates multiple MCP servers for unified tool access
|
|
24
|
+
* with load balancing and failover capabilities.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const manager = new MultiServerManager({
|
|
29
|
+
* defaultStrategy: "round-robin",
|
|
30
|
+
* healthAwareRouting: true,
|
|
31
|
+
* autoNamespace: true,
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* // Add servers
|
|
35
|
+
* manager.addServer(server1Info);
|
|
36
|
+
* manager.addServer(server2Info);
|
|
37
|
+
*
|
|
38
|
+
* // Create a group for redundant servers
|
|
39
|
+
* manager.createGroup({
|
|
40
|
+
* id: "data-servers",
|
|
41
|
+
* name: "Data Processing Servers",
|
|
42
|
+
* servers: ["server1", "server2"],
|
|
43
|
+
* strategy: "least-loaded",
|
|
44
|
+
* });
|
|
45
|
+
*
|
|
46
|
+
* // Get unified tool list
|
|
47
|
+
* const tools = manager.getUnifiedTools();
|
|
48
|
+
*
|
|
49
|
+
* // Execute with automatic routing
|
|
50
|
+
* const result = await manager.executeTool("readFile", { path: "/data" });
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export class MultiServerManager extends EventEmitter {
|
|
54
|
+
config;
|
|
55
|
+
servers = new Map();
|
|
56
|
+
groups = new Map();
|
|
57
|
+
metrics = new Map();
|
|
58
|
+
roundRobinCounters = new Map();
|
|
59
|
+
toolPreferences = new Map(); // toolName -> preferred serverId
|
|
60
|
+
constructor(config = {}) {
|
|
61
|
+
super();
|
|
62
|
+
this.config = {
|
|
63
|
+
defaultStrategy: config.defaultStrategy ?? "round-robin",
|
|
64
|
+
healthAwareRouting: config.healthAwareRouting ?? true,
|
|
65
|
+
healthCheckInterval: config.healthCheckInterval ?? 30000,
|
|
66
|
+
maxFailoverRetries: config.maxFailoverRetries ?? 3,
|
|
67
|
+
namespaceSeparator: config.namespaceSeparator ?? ".",
|
|
68
|
+
autoNamespace: config.autoNamespace ?? false,
|
|
69
|
+
conflictResolution: config.conflictResolution ?? "first-wins",
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Add a server to the manager
|
|
74
|
+
*/
|
|
75
|
+
addServer(server) {
|
|
76
|
+
this.servers.set(server.id, server);
|
|
77
|
+
// Initialize metrics
|
|
78
|
+
this.metrics.set(server.id, {
|
|
79
|
+
activeRequests: 0,
|
|
80
|
+
totalRequests: 0,
|
|
81
|
+
completedRequests: 0,
|
|
82
|
+
averageResponseTime: 0,
|
|
83
|
+
errorRate: 0,
|
|
84
|
+
isHealthy: server.status === "connected",
|
|
85
|
+
});
|
|
86
|
+
this.emit("serverAdded", { serverId: server.id, server });
|
|
87
|
+
logger.debug(`[MultiServerManager] Added server: ${server.id} (${server.name})`);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Remove a server from the manager
|
|
91
|
+
*/
|
|
92
|
+
removeServer(serverId) {
|
|
93
|
+
const server = this.servers.get(serverId);
|
|
94
|
+
if (!server) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
// Remove from all groups
|
|
98
|
+
for (const [groupId, group] of this.groups) {
|
|
99
|
+
const index = group.servers.indexOf(serverId);
|
|
100
|
+
if (index !== -1) {
|
|
101
|
+
group.servers.splice(index, 1);
|
|
102
|
+
// Remove empty groups
|
|
103
|
+
if (group.servers.length === 0) {
|
|
104
|
+
this.groups.delete(groupId);
|
|
105
|
+
this.roundRobinCounters.delete(groupId);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
this.servers.delete(serverId);
|
|
110
|
+
this.metrics.delete(serverId);
|
|
111
|
+
// Clear tool preferences for this server
|
|
112
|
+
for (const [toolName, preferredServer] of this.toolPreferences) {
|
|
113
|
+
if (preferredServer === serverId) {
|
|
114
|
+
this.toolPreferences.delete(toolName);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
this.emit("serverRemoved", { serverId });
|
|
118
|
+
logger.debug(`[MultiServerManager] Removed server: ${serverId}`);
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Update server info
|
|
123
|
+
*/
|
|
124
|
+
updateServer(serverId, updates) {
|
|
125
|
+
const server = this.servers.get(serverId);
|
|
126
|
+
if (!server) {
|
|
127
|
+
throw ErrorFactory.invalidConfiguration("serverId", `Server '${serverId}' not found`, { serverId });
|
|
128
|
+
}
|
|
129
|
+
const updatedServer = { ...server, ...updates, id: serverId };
|
|
130
|
+
this.servers.set(serverId, updatedServer);
|
|
131
|
+
// Update health status in metrics
|
|
132
|
+
const metrics = this.metrics.get(serverId);
|
|
133
|
+
if (metrics && updates.status !== undefined) {
|
|
134
|
+
metrics.isHealthy = updates.status === "connected";
|
|
135
|
+
}
|
|
136
|
+
this.emit("serverUpdated", { serverId, server: updatedServer });
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Create a server group
|
|
140
|
+
*/
|
|
141
|
+
createGroup(group) {
|
|
142
|
+
// Validate servers exist
|
|
143
|
+
for (const serverId of group.servers) {
|
|
144
|
+
if (!this.servers.has(serverId)) {
|
|
145
|
+
throw ErrorFactory.invalidConfiguration("serverGroup.servers", `Server '${serverId}' not found when creating group '${group.id}'`, { serverId, groupId: group.id });
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
this.groups.set(group.id, group);
|
|
149
|
+
this.roundRobinCounters.set(group.id, 0);
|
|
150
|
+
this.emit("groupCreated", { group });
|
|
151
|
+
logger.debug(`[MultiServerManager] Created group: ${group.id} with ${group.servers.length} servers`);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Remove a server group
|
|
155
|
+
*/
|
|
156
|
+
removeGroup(groupId) {
|
|
157
|
+
const removed = this.groups.delete(groupId);
|
|
158
|
+
if (removed) {
|
|
159
|
+
this.roundRobinCounters.delete(groupId);
|
|
160
|
+
this.emit("groupRemoved", { groupId });
|
|
161
|
+
}
|
|
162
|
+
return removed;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Add a server to a group
|
|
166
|
+
*/
|
|
167
|
+
addServerToGroup(serverId, groupId) {
|
|
168
|
+
const group = this.groups.get(groupId);
|
|
169
|
+
if (!group) {
|
|
170
|
+
throw ErrorFactory.invalidConfiguration("groupId", `Group '${groupId}' not found`, { groupId });
|
|
171
|
+
}
|
|
172
|
+
if (!this.servers.has(serverId)) {
|
|
173
|
+
throw ErrorFactory.invalidConfiguration("serverId", `Server '${serverId}' not found`, { serverId, groupId });
|
|
174
|
+
}
|
|
175
|
+
if (!group.servers.includes(serverId)) {
|
|
176
|
+
group.servers.push(serverId);
|
|
177
|
+
this.emit("serverAddedToGroup", { serverId, groupId });
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Remove a server from a group
|
|
182
|
+
*/
|
|
183
|
+
removeServerFromGroup(serverId, groupId) {
|
|
184
|
+
const group = this.groups.get(groupId);
|
|
185
|
+
if (!group) {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
const index = group.servers.indexOf(serverId);
|
|
189
|
+
if (index !== -1) {
|
|
190
|
+
group.servers.splice(index, 1);
|
|
191
|
+
this.emit("serverRemovedFromGroup", { serverId, groupId });
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Get unified tool list from all servers
|
|
198
|
+
*/
|
|
199
|
+
getUnifiedTools() {
|
|
200
|
+
const toolMap = new Map();
|
|
201
|
+
for (const [serverId, server] of this.servers) {
|
|
202
|
+
const metrics = this.metrics.get(serverId);
|
|
203
|
+
const isHealthy = metrics?.isHealthy ?? true;
|
|
204
|
+
// Skip unhealthy servers in health-aware mode
|
|
205
|
+
if (this.config.healthAwareRouting && !isHealthy) {
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
for (const tool of server.tools || []) {
|
|
209
|
+
const existingTool = toolMap.get(tool.name);
|
|
210
|
+
if (existingTool) {
|
|
211
|
+
// Tool exists from another server - mark as conflict
|
|
212
|
+
existingTool.hasConflict = true;
|
|
213
|
+
existingTool.servers.push({
|
|
214
|
+
serverId,
|
|
215
|
+
serverName: server.name,
|
|
216
|
+
inputSchema: tool.inputSchema,
|
|
217
|
+
priority: this.getServerPriority(serverId),
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
// New tool
|
|
222
|
+
toolMap.set(tool.name, {
|
|
223
|
+
name: tool.name,
|
|
224
|
+
description: tool.description,
|
|
225
|
+
servers: [
|
|
226
|
+
{
|
|
227
|
+
serverId,
|
|
228
|
+
serverName: server.name,
|
|
229
|
+
inputSchema: tool.inputSchema,
|
|
230
|
+
priority: this.getServerPriority(serverId),
|
|
231
|
+
},
|
|
232
|
+
],
|
|
233
|
+
hasConflict: false,
|
|
234
|
+
preferredServerId: this.toolPreferences.get(tool.name),
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
// Sort servers by priority within each tool
|
|
240
|
+
for (const tool of toolMap.values()) {
|
|
241
|
+
tool.servers.sort((a, b) => a.priority - b.priority);
|
|
242
|
+
// Set preferred server if not already set
|
|
243
|
+
if (!tool.preferredServerId && tool.servers.length > 0) {
|
|
244
|
+
tool.preferredServerId = tool.servers[0].serverId;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return Array.from(toolMap.values());
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Get namespaced tools (server.toolName format)
|
|
251
|
+
*/
|
|
252
|
+
getNamespacedTools() {
|
|
253
|
+
const tools = [];
|
|
254
|
+
for (const [serverId, server] of this.servers) {
|
|
255
|
+
// Skip unhealthy servers in health-aware mode
|
|
256
|
+
if (this.config.healthAwareRouting) {
|
|
257
|
+
const metrics = this.metrics.get(serverId);
|
|
258
|
+
const isHealthy = metrics?.isHealthy ?? true;
|
|
259
|
+
if (!isHealthy) {
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
for (const tool of server.tools || []) {
|
|
264
|
+
tools.push({
|
|
265
|
+
fullName: `${serverId}${this.config.namespaceSeparator}${tool.name}`,
|
|
266
|
+
toolName: tool.name,
|
|
267
|
+
serverId,
|
|
268
|
+
serverName: server.name,
|
|
269
|
+
description: tool.description,
|
|
270
|
+
inputSchema: tool.inputSchema,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return tools;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Set tool preference for routing
|
|
278
|
+
*/
|
|
279
|
+
setToolPreference(toolName, serverId) {
|
|
280
|
+
if (!this.servers.has(serverId)) {
|
|
281
|
+
throw ErrorFactory.invalidConfiguration("serverId", `Server '${serverId}' not found`, { serverId, toolName });
|
|
282
|
+
}
|
|
283
|
+
this.toolPreferences.set(toolName, serverId);
|
|
284
|
+
this.emit("toolPreferenceSet", { toolName, serverId });
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Clear tool preference
|
|
288
|
+
*/
|
|
289
|
+
clearToolPreference(toolName) {
|
|
290
|
+
this.toolPreferences.delete(toolName);
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Select a server for a tool using load balancing
|
|
294
|
+
*/
|
|
295
|
+
selectServer(toolName, groupId) {
|
|
296
|
+
// Check for tool preference first
|
|
297
|
+
const preferredServerId = this.toolPreferences.get(toolName);
|
|
298
|
+
if (preferredServerId) {
|
|
299
|
+
const server = this.servers.get(preferredServerId);
|
|
300
|
+
const metrics = this.metrics.get(preferredServerId);
|
|
301
|
+
if (server && (!this.config.healthAwareRouting || metrics?.isHealthy)) {
|
|
302
|
+
// Check if server has the tool
|
|
303
|
+
if (server.tools?.some((t) => t.name === toolName)) {
|
|
304
|
+
return { serverId: preferredServerId, server };
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
// Get candidate servers (from group or all servers)
|
|
309
|
+
let candidates;
|
|
310
|
+
if (groupId) {
|
|
311
|
+
const group = this.groups.get(groupId);
|
|
312
|
+
if (!group) {
|
|
313
|
+
logger.warn(`[MultiServerManager] Group '${groupId}' not found`);
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
// Filter group servers to only those that have the requested tool
|
|
317
|
+
candidates = group.servers.filter((serverId) => {
|
|
318
|
+
const server = this.servers.get(serverId);
|
|
319
|
+
return server?.tools?.some((t) => t.name === toolName);
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
else {
|
|
323
|
+
// Find all servers that have this tool
|
|
324
|
+
candidates = [];
|
|
325
|
+
for (const [serverId, server] of this.servers) {
|
|
326
|
+
if (server.tools?.some((t) => t.name === toolName)) {
|
|
327
|
+
candidates.push(serverId);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
if (candidates.length === 0) {
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
// Filter by health if enabled (prefer group-level flag, fall back to global)
|
|
335
|
+
const healthAware = groupId
|
|
336
|
+
? (this.groups.get(groupId)?.healthAware ??
|
|
337
|
+
this.config.healthAwareRouting)
|
|
338
|
+
: this.config.healthAwareRouting;
|
|
339
|
+
if (healthAware) {
|
|
340
|
+
candidates = candidates.filter((id) => {
|
|
341
|
+
const metrics = this.metrics.get(id);
|
|
342
|
+
return metrics?.isHealthy ?? true;
|
|
343
|
+
});
|
|
344
|
+
if (candidates.length === 0) {
|
|
345
|
+
logger.warn(`[MultiServerManager] No healthy servers available for tool '${toolName}'`);
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
// Apply load balancing strategy
|
|
350
|
+
const strategy = groupId
|
|
351
|
+
? (this.groups.get(groupId)?.strategy ?? this.config.defaultStrategy)
|
|
352
|
+
: this.config.defaultStrategy;
|
|
353
|
+
const selectedId = this.applyStrategy(strategy, candidates, groupId);
|
|
354
|
+
if (!selectedId) {
|
|
355
|
+
return null;
|
|
356
|
+
}
|
|
357
|
+
const server = this.servers.get(selectedId);
|
|
358
|
+
return server ? { serverId: selectedId, server } : null;
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Apply load balancing strategy
|
|
362
|
+
*/
|
|
363
|
+
applyStrategy(strategy, candidates, groupId) {
|
|
364
|
+
if (candidates.length === 0) {
|
|
365
|
+
return null;
|
|
366
|
+
}
|
|
367
|
+
if (candidates.length === 1) {
|
|
368
|
+
return candidates[0];
|
|
369
|
+
}
|
|
370
|
+
switch (strategy) {
|
|
371
|
+
case "round-robin": {
|
|
372
|
+
const counterKey = groupId ?? "default";
|
|
373
|
+
const counter = this.roundRobinCounters.get(counterKey) ?? 0;
|
|
374
|
+
const selected = candidates[counter % candidates.length];
|
|
375
|
+
this.roundRobinCounters.set(counterKey, counter + 1);
|
|
376
|
+
return selected;
|
|
377
|
+
}
|
|
378
|
+
case "least-loaded": {
|
|
379
|
+
let minLoad = Infinity;
|
|
380
|
+
let selected = candidates[0];
|
|
381
|
+
for (const serverId of candidates) {
|
|
382
|
+
const metrics = this.metrics.get(serverId);
|
|
383
|
+
const load = metrics?.activeRequests ?? 0;
|
|
384
|
+
if (load < minLoad) {
|
|
385
|
+
minLoad = load;
|
|
386
|
+
selected = serverId;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
return selected;
|
|
390
|
+
}
|
|
391
|
+
case "random": {
|
|
392
|
+
const index = Math.floor(Math.random() * candidates.length);
|
|
393
|
+
return candidates[index];
|
|
394
|
+
}
|
|
395
|
+
case "weighted": {
|
|
396
|
+
if (!groupId) {
|
|
397
|
+
// Fall back to random for non-group selection
|
|
398
|
+
const index = Math.floor(Math.random() * candidates.length);
|
|
399
|
+
return candidates[index];
|
|
400
|
+
}
|
|
401
|
+
const group = this.groups.get(groupId);
|
|
402
|
+
if (!group?.weights) {
|
|
403
|
+
const index = Math.floor(Math.random() * candidates.length);
|
|
404
|
+
return candidates[index];
|
|
405
|
+
}
|
|
406
|
+
// Build effective weights: use configured weight or default of 1 for unlisted candidates
|
|
407
|
+
const DEFAULT_WEIGHT = 1;
|
|
408
|
+
const effectiveWeights = candidates.map((serverId) => {
|
|
409
|
+
const weights = group.weights ?? [];
|
|
410
|
+
const configured = weights.find((w) => w.serverId === serverId);
|
|
411
|
+
return {
|
|
412
|
+
serverId,
|
|
413
|
+
weight: configured?.weight ?? DEFAULT_WEIGHT,
|
|
414
|
+
};
|
|
415
|
+
});
|
|
416
|
+
const totalWeight = effectiveWeights.reduce((sum, w) => sum + w.weight, 0);
|
|
417
|
+
if (totalWeight === 0) {
|
|
418
|
+
const index = Math.floor(Math.random() * candidates.length);
|
|
419
|
+
return candidates[index];
|
|
420
|
+
}
|
|
421
|
+
let random = Math.random() * totalWeight;
|
|
422
|
+
for (const ew of effectiveWeights) {
|
|
423
|
+
random -= ew.weight;
|
|
424
|
+
if (random <= 0) {
|
|
425
|
+
return ew.serverId;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
return candidates[0];
|
|
429
|
+
}
|
|
430
|
+
case "failover-only": {
|
|
431
|
+
// Return first healthy server by priority
|
|
432
|
+
const serverPriorities = candidates
|
|
433
|
+
.map((id) => ({
|
|
434
|
+
id,
|
|
435
|
+
priority: this.getServerPriority(id, groupId),
|
|
436
|
+
}))
|
|
437
|
+
.sort((a, b) => a.priority - b.priority);
|
|
438
|
+
return serverPriorities[0]?.id ?? null;
|
|
439
|
+
}
|
|
440
|
+
default:
|
|
441
|
+
return candidates[0];
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Get server priority (lower = higher priority)
|
|
446
|
+
*
|
|
447
|
+
* @param serverId - The server to look up
|
|
448
|
+
* @param groupId - Optional group to scope the lookup to, avoiding
|
|
449
|
+
* nondeterministic iteration across all groups.
|
|
450
|
+
*/
|
|
451
|
+
getServerPriority(serverId, groupId) {
|
|
452
|
+
// Scoped lookup: check only the specified group
|
|
453
|
+
if (groupId) {
|
|
454
|
+
const group = this.groups.get(groupId);
|
|
455
|
+
if (group?.weights) {
|
|
456
|
+
const weight = group.weights.find((w) => w.serverId === serverId);
|
|
457
|
+
if (weight) {
|
|
458
|
+
return weight.priority;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
// Fallback: check all groups for weight/priority settings
|
|
463
|
+
for (const group of this.groups.values()) {
|
|
464
|
+
if (group.weights) {
|
|
465
|
+
const weight = group.weights.find((w) => w.serverId === serverId);
|
|
466
|
+
if (weight) {
|
|
467
|
+
return weight.priority;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
// Default priority based on order added
|
|
472
|
+
const serverIds = Array.from(this.servers.keys());
|
|
473
|
+
return serverIds.indexOf(serverId);
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Update server metrics
|
|
477
|
+
*/
|
|
478
|
+
updateMetrics(serverId, updates) {
|
|
479
|
+
const metrics = this.metrics.get(serverId);
|
|
480
|
+
if (metrics) {
|
|
481
|
+
Object.assign(metrics, updates);
|
|
482
|
+
this.emit("metricsUpdated", { serverId, metrics: { ...metrics } });
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Mark request started
|
|
487
|
+
*/
|
|
488
|
+
requestStarted(serverId) {
|
|
489
|
+
const metrics = this.metrics.get(serverId);
|
|
490
|
+
if (metrics) {
|
|
491
|
+
metrics.activeRequests++;
|
|
492
|
+
metrics.totalRequests++;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Mark request completed
|
|
497
|
+
*/
|
|
498
|
+
requestCompleted(serverId, duration, success) {
|
|
499
|
+
const metrics = this.metrics.get(serverId);
|
|
500
|
+
if (metrics) {
|
|
501
|
+
metrics.activeRequests = Math.max(0, metrics.activeRequests - 1);
|
|
502
|
+
metrics.completedRequests++;
|
|
503
|
+
// Update average response time using only completed requests
|
|
504
|
+
const totalTime = metrics.averageResponseTime * (metrics.completedRequests - 1) +
|
|
505
|
+
duration;
|
|
506
|
+
metrics.averageResponseTime = totalTime / metrics.completedRequests;
|
|
507
|
+
// Update error rate (simple moving average)
|
|
508
|
+
const alpha = 0.1; // Smoothing factor
|
|
509
|
+
metrics.errorRate =
|
|
510
|
+
metrics.errorRate * (1 - alpha) + (success ? 0 : 1) * alpha;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Get all servers
|
|
515
|
+
*/
|
|
516
|
+
getServers() {
|
|
517
|
+
return Array.from(this.servers.values());
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Get server by ID
|
|
521
|
+
*/
|
|
522
|
+
getServer(serverId) {
|
|
523
|
+
return this.servers.get(serverId);
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Get all groups
|
|
527
|
+
*/
|
|
528
|
+
getGroups() {
|
|
529
|
+
return Array.from(this.groups.values());
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Get group by ID
|
|
533
|
+
*/
|
|
534
|
+
getGroup(groupId) {
|
|
535
|
+
return this.groups.get(groupId);
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Get server metrics
|
|
539
|
+
*/
|
|
540
|
+
getServerMetrics(serverId) {
|
|
541
|
+
return this.metrics.get(serverId);
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Get all metrics
|
|
545
|
+
*/
|
|
546
|
+
getAllMetrics() {
|
|
547
|
+
return new Map(this.metrics);
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Get statistics
|
|
551
|
+
*/
|
|
552
|
+
getStatistics() {
|
|
553
|
+
let healthyServers = 0;
|
|
554
|
+
let totalRequests = 0;
|
|
555
|
+
let activeRequests = 0;
|
|
556
|
+
for (const metrics of this.metrics.values()) {
|
|
557
|
+
if (metrics.isHealthy) {
|
|
558
|
+
healthyServers++;
|
|
559
|
+
}
|
|
560
|
+
totalRequests += metrics.totalRequests;
|
|
561
|
+
activeRequests += metrics.activeRequests;
|
|
562
|
+
}
|
|
563
|
+
const unifiedTools = this.getUnifiedTools();
|
|
564
|
+
const conflictingTools = unifiedTools.filter((t) => t.hasConflict).length;
|
|
565
|
+
return {
|
|
566
|
+
totalServers: this.servers.size,
|
|
567
|
+
healthyServers,
|
|
568
|
+
totalGroups: this.groups.size,
|
|
569
|
+
totalTools: unifiedTools.length,
|
|
570
|
+
conflictingTools,
|
|
571
|
+
totalRequests,
|
|
572
|
+
activeRequests,
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Global multi-server manager instance
|
|
578
|
+
*/
|
|
579
|
+
export const globalMultiServerManager = new MultiServerManager();
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Routing Module - Intelligent tool call routing
|
|
3
|
+
*
|
|
4
|
+
* Provides advanced routing strategies for multi-server MCP environments:
|
|
5
|
+
* - Round-robin distribution
|
|
6
|
+
* - Least-loaded selection
|
|
7
|
+
* - Capability-based routing
|
|
8
|
+
* - Session affinity
|
|
9
|
+
*/
|
|
10
|
+
export type { AffinityRule, CategoryMapping, MCPTool, RoutingDecision, RoutingStrategy, ServerWeight, ToolRouterConfig, ToolRouterEvents, } from "./toolRouter.js";
|
|
11
|
+
export { createToolRouter, DEFAULT_ROUTER_CONFIG, ToolRouter, } from "./toolRouter.js";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Routing Module - Intelligent tool call routing
|
|
3
|
+
*
|
|
4
|
+
* Provides advanced routing strategies for multi-server MCP environments:
|
|
5
|
+
* - Round-robin distribution
|
|
6
|
+
* - Least-loaded selection
|
|
7
|
+
* - Capability-based routing
|
|
8
|
+
* - Session affinity
|
|
9
|
+
*/
|
|
10
|
+
export { createToolRouter, DEFAULT_ROUTER_CONFIG, ToolRouter, } from "./toolRouter.js";
|