@juspay/neurolink 3.0.1 → 4.1.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 +66 -6
- package/README.md +318 -27
- package/dist/agent/direct-tools.d.ts +6 -6
- package/dist/chat/client-utils.d.ts +92 -0
- package/dist/chat/client-utils.js +298 -0
- package/dist/chat/index.d.ts +27 -0
- package/dist/chat/index.js +41 -0
- package/dist/chat/session-storage.d.ts +77 -0
- package/dist/chat/session-storage.js +233 -0
- package/dist/chat/session.d.ts +95 -0
- package/dist/chat/session.js +257 -0
- package/dist/chat/sse-handler.d.ts +49 -0
- package/dist/chat/sse-handler.js +266 -0
- package/dist/chat/types.d.ts +73 -0
- package/dist/chat/types.js +5 -0
- package/dist/chat/websocket-chat-handler.d.ts +36 -0
- package/dist/chat/websocket-chat-handler.js +262 -0
- package/dist/cli/commands/config.js +12 -12
- package/dist/cli/commands/mcp.js +3 -4
- package/dist/cli/index.d.ts +0 -7
- package/dist/cli/index.js +247 -28
- package/dist/config/configManager.d.ts +60 -0
- package/dist/config/configManager.js +300 -0
- package/dist/config/types.d.ts +136 -0
- package/dist/config/types.js +43 -0
- package/dist/core/analytics.d.ts +23 -0
- package/dist/core/analytics.js +131 -0
- package/dist/core/constants.d.ts +41 -0
- package/dist/core/constants.js +50 -0
- package/dist/core/defaults.d.ts +18 -0
- package/dist/core/defaults.js +29 -0
- package/dist/core/evaluation-config.d.ts +29 -0
- package/dist/core/evaluation-config.js +144 -0
- package/dist/core/evaluation-providers.d.ts +30 -0
- package/dist/core/evaluation-providers.js +187 -0
- package/dist/core/evaluation.d.ts +117 -0
- package/dist/core/evaluation.js +528 -0
- package/dist/core/factory.js +33 -25
- package/dist/core/types.d.ts +165 -6
- package/dist/core/types.js +3 -4
- package/dist/index.d.ts +9 -4
- package/dist/index.js +25 -4
- package/dist/lib/agent/direct-tools.d.ts +6 -6
- package/dist/lib/chat/client-utils.d.ts +92 -0
- package/dist/lib/chat/client-utils.js +298 -0
- package/dist/lib/chat/index.d.ts +27 -0
- package/dist/lib/chat/index.js +41 -0
- package/dist/lib/chat/session-storage.d.ts +77 -0
- package/dist/lib/chat/session-storage.js +233 -0
- package/dist/lib/chat/session.d.ts +95 -0
- package/dist/lib/chat/session.js +257 -0
- package/dist/lib/chat/sse-handler.d.ts +49 -0
- package/dist/lib/chat/sse-handler.js +266 -0
- package/dist/lib/chat/types.d.ts +73 -0
- package/dist/lib/chat/types.js +5 -0
- package/dist/lib/chat/websocket-chat-handler.d.ts +36 -0
- package/dist/lib/chat/websocket-chat-handler.js +262 -0
- package/dist/lib/config/configManager.d.ts +60 -0
- package/dist/lib/config/configManager.js +300 -0
- package/dist/lib/config/types.d.ts +136 -0
- package/dist/lib/config/types.js +43 -0
- package/dist/lib/core/analytics.d.ts +23 -0
- package/dist/lib/core/analytics.js +131 -0
- package/dist/lib/core/constants.d.ts +41 -0
- package/dist/lib/core/constants.js +50 -0
- package/dist/lib/core/defaults.d.ts +18 -0
- package/dist/lib/core/defaults.js +29 -0
- package/dist/lib/core/evaluation-config.d.ts +29 -0
- package/dist/lib/core/evaluation-config.js +144 -0
- package/dist/lib/core/evaluation-providers.d.ts +30 -0
- package/dist/lib/core/evaluation-providers.js +187 -0
- package/dist/lib/core/evaluation.d.ts +117 -0
- package/dist/lib/core/evaluation.js +528 -0
- package/dist/lib/core/factory.js +33 -26
- package/dist/lib/core/types.d.ts +165 -6
- package/dist/lib/core/types.js +3 -4
- package/dist/lib/index.d.ts +9 -4
- package/dist/lib/index.js +25 -4
- package/dist/lib/mcp/contracts/mcpContract.d.ts +118 -0
- package/dist/lib/mcp/contracts/mcpContract.js +5 -0
- package/dist/lib/mcp/dynamic-chain-executor.d.ts +201 -0
- package/dist/lib/mcp/dynamic-chain-executor.js +489 -0
- package/dist/lib/mcp/dynamic-orchestrator.d.ts +109 -0
- package/dist/lib/mcp/dynamic-orchestrator.js +351 -0
- package/dist/lib/mcp/error-manager.d.ts +254 -0
- package/dist/lib/mcp/error-manager.js +501 -0
- package/dist/lib/mcp/error-recovery.d.ts +158 -0
- package/dist/lib/mcp/error-recovery.js +405 -0
- package/dist/lib/mcp/function-calling.js +11 -3
- package/dist/lib/mcp/health-monitor.d.ts +256 -0
- package/dist/lib/mcp/health-monitor.js +621 -0
- package/dist/lib/mcp/logging.js +5 -0
- package/dist/lib/mcp/neurolink-mcp-client.js +2 -1
- package/dist/lib/mcp/orchestrator.d.ts +136 -5
- package/dist/lib/mcp/orchestrator.js +332 -16
- package/dist/lib/mcp/registry.d.ts +71 -16
- package/dist/lib/mcp/registry.js +104 -6
- package/dist/lib/mcp/semaphore-manager.d.ts +137 -0
- package/dist/lib/mcp/semaphore-manager.js +329 -0
- package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
- package/dist/lib/mcp/servers/ai-providers/ai-workflow-tools.js +5 -4
- package/dist/lib/mcp/session-manager.d.ts +186 -0
- package/dist/lib/mcp/session-manager.js +400 -0
- package/dist/lib/mcp/session-persistence.d.ts +93 -0
- package/dist/lib/mcp/session-persistence.js +298 -0
- package/dist/lib/mcp/tool-integration.js +1 -1
- package/dist/lib/mcp/tool-registry.d.ts +55 -34
- package/dist/lib/mcp/tool-registry.js +111 -97
- package/dist/lib/mcp/transport-manager.d.ts +153 -0
- package/dist/lib/mcp/transport-manager.js +330 -0
- package/dist/lib/mcp/unified-mcp.js +6 -1
- package/dist/lib/mcp/unified-registry.d.ts +54 -5
- package/dist/lib/mcp/unified-registry.js +139 -6
- package/dist/lib/neurolink.d.ts +101 -0
- package/dist/lib/neurolink.js +147 -1
- package/dist/lib/providers/agent-enhanced-provider.d.ts +11 -2
- package/dist/lib/providers/agent-enhanced-provider.js +86 -15
- package/dist/lib/providers/amazonBedrock.d.ts +9 -1
- package/dist/lib/providers/amazonBedrock.js +26 -2
- package/dist/lib/providers/analytics-helper.d.ts +53 -0
- package/dist/lib/providers/analytics-helper.js +151 -0
- package/dist/lib/providers/anthropic.d.ts +11 -1
- package/dist/lib/providers/anthropic.js +29 -4
- package/dist/lib/providers/azureOpenAI.d.ts +3 -1
- package/dist/lib/providers/azureOpenAI.js +28 -4
- package/dist/lib/providers/function-calling-provider.d.ts +9 -1
- package/dist/lib/providers/function-calling-provider.js +14 -1
- package/dist/lib/providers/googleAIStudio.d.ts +15 -1
- package/dist/lib/providers/googleAIStudio.js +32 -2
- package/dist/lib/providers/googleVertexAI.d.ts +9 -1
- package/dist/lib/providers/googleVertexAI.js +31 -2
- package/dist/lib/providers/huggingFace.d.ts +3 -1
- package/dist/lib/providers/huggingFace.js +26 -3
- package/dist/lib/providers/mcp-provider.d.ts +9 -1
- package/dist/lib/providers/mcp-provider.js +12 -0
- package/dist/lib/providers/mistralAI.d.ts +3 -1
- package/dist/lib/providers/mistralAI.js +25 -2
- package/dist/lib/providers/ollama.d.ts +3 -1
- package/dist/lib/providers/ollama.js +27 -4
- package/dist/lib/providers/openAI.d.ts +15 -1
- package/dist/lib/providers/openAI.js +32 -2
- package/dist/lib/proxy/proxy-fetch.js +8 -7
- package/dist/lib/services/streaming/streaming-manager.d.ts +29 -0
- package/dist/lib/services/streaming/streaming-manager.js +244 -0
- package/dist/lib/services/types.d.ts +155 -0
- package/dist/lib/services/types.js +2 -0
- package/dist/lib/services/websocket/websocket-server.d.ts +34 -0
- package/dist/lib/services/websocket/websocket-server.js +304 -0
- package/dist/lib/telemetry/index.d.ts +15 -0
- package/dist/lib/telemetry/index.js +22 -0
- package/dist/lib/telemetry/telemetry-service.d.ts +47 -0
- package/dist/lib/telemetry/telemetry-service.js +259 -0
- package/dist/lib/utils/streaming-utils.d.ts +67 -0
- package/dist/lib/utils/streaming-utils.js +201 -0
- package/dist/mcp/contracts/mcpContract.d.ts +118 -0
- package/dist/mcp/contracts/mcpContract.js +5 -0
- package/dist/mcp/dynamic-chain-executor.d.ts +201 -0
- package/dist/mcp/dynamic-chain-executor.js +489 -0
- package/dist/mcp/dynamic-orchestrator.d.ts +109 -0
- package/dist/mcp/dynamic-orchestrator.js +351 -0
- package/dist/mcp/error-manager.d.ts +254 -0
- package/dist/mcp/error-manager.js +501 -0
- package/dist/mcp/error-recovery.d.ts +158 -0
- package/dist/mcp/error-recovery.js +405 -0
- package/dist/mcp/function-calling.js +11 -3
- package/dist/mcp/health-monitor.d.ts +256 -0
- package/dist/mcp/health-monitor.js +621 -0
- package/dist/mcp/logging.js +5 -0
- package/dist/mcp/neurolink-mcp-client.js +2 -1
- package/dist/mcp/orchestrator.d.ts +136 -5
- package/dist/mcp/orchestrator.js +332 -16
- package/dist/mcp/plugins/core/neurolink-mcp.json +15 -15
- package/dist/mcp/registry.d.ts +71 -16
- package/dist/mcp/registry.js +104 -6
- package/dist/mcp/semaphore-manager.d.ts +137 -0
- package/dist/mcp/semaphore-manager.js +329 -0
- package/dist/mcp/servers/ai-providers/ai-workflow-tools.d.ts +2 -2
- package/dist/mcp/servers/ai-providers/ai-workflow-tools.js +5 -4
- package/dist/mcp/session-manager.d.ts +186 -0
- package/dist/mcp/session-manager.js +400 -0
- package/dist/mcp/session-persistence.d.ts +93 -0
- package/dist/mcp/session-persistence.js +299 -0
- package/dist/mcp/tool-integration.js +1 -1
- package/dist/mcp/tool-registry.d.ts +55 -34
- package/dist/mcp/tool-registry.js +111 -97
- package/dist/mcp/transport-manager.d.ts +153 -0
- package/dist/mcp/transport-manager.js +331 -0
- package/dist/mcp/unified-mcp.js +6 -1
- package/dist/mcp/unified-registry.d.ts +54 -5
- package/dist/mcp/unified-registry.js +139 -6
- package/dist/neurolink.d.ts +101 -0
- package/dist/neurolink.js +147 -1
- package/dist/providers/agent-enhanced-provider.d.ts +11 -2
- package/dist/providers/agent-enhanced-provider.js +86 -15
- package/dist/providers/amazonBedrock.d.ts +9 -1
- package/dist/providers/amazonBedrock.js +26 -2
- package/dist/providers/analytics-helper.d.ts +53 -0
- package/dist/providers/analytics-helper.js +151 -0
- package/dist/providers/anthropic.d.ts +11 -1
- package/dist/providers/anthropic.js +29 -4
- package/dist/providers/azureOpenAI.d.ts +3 -1
- package/dist/providers/azureOpenAI.js +29 -4
- package/dist/providers/function-calling-provider.d.ts +9 -1
- package/dist/providers/function-calling-provider.js +14 -1
- package/dist/providers/googleAIStudio.d.ts +15 -1
- package/dist/providers/googleAIStudio.js +32 -2
- package/dist/providers/googleVertexAI.d.ts +9 -1
- package/dist/providers/googleVertexAI.js +31 -2
- package/dist/providers/huggingFace.d.ts +3 -1
- package/dist/providers/huggingFace.js +26 -3
- package/dist/providers/mcp-provider.d.ts +9 -1
- package/dist/providers/mcp-provider.js +12 -0
- package/dist/providers/mistralAI.d.ts +3 -1
- package/dist/providers/mistralAI.js +25 -2
- package/dist/providers/ollama.d.ts +3 -1
- package/dist/providers/ollama.js +27 -4
- package/dist/providers/openAI.d.ts +15 -1
- package/dist/providers/openAI.js +33 -2
- package/dist/proxy/proxy-fetch.js +8 -7
- package/dist/services/streaming/streaming-manager.d.ts +29 -0
- package/dist/services/streaming/streaming-manager.js +244 -0
- package/dist/services/types.d.ts +155 -0
- package/dist/services/types.js +2 -0
- package/dist/services/websocket/websocket-server.d.ts +34 -0
- package/dist/services/websocket/websocket-server.js +304 -0
- package/dist/telemetry/index.d.ts +15 -0
- package/dist/telemetry/index.js +22 -0
- package/dist/telemetry/telemetry-service.d.ts +47 -0
- package/dist/telemetry/telemetry-service.js +261 -0
- package/dist/utils/streaming-utils.d.ts +67 -0
- package/dist/utils/streaming-utils.js +201 -0
- package/package.json +245 -228
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport Manager for MCP connections
|
|
3
|
+
* Supports stdio, SSE, and HTTP transports with reconnection logic
|
|
4
|
+
*/
|
|
5
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { ErrorManager } from "./error-manager.js";
|
|
8
|
+
export declare const TransportConfigSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
|
|
9
|
+
type: z.ZodLiteral<"stdio">;
|
|
10
|
+
command: z.ZodString;
|
|
11
|
+
args: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
12
|
+
cwd: z.ZodOptional<z.ZodString>;
|
|
13
|
+
env: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
14
|
+
}, "strip", z.ZodTypeAny, {
|
|
15
|
+
type: "stdio";
|
|
16
|
+
command: string;
|
|
17
|
+
args?: string[] | undefined;
|
|
18
|
+
cwd?: string | undefined;
|
|
19
|
+
env?: Record<string, string> | undefined;
|
|
20
|
+
}, {
|
|
21
|
+
type: "stdio";
|
|
22
|
+
command: string;
|
|
23
|
+
args?: string[] | undefined;
|
|
24
|
+
cwd?: string | undefined;
|
|
25
|
+
env?: Record<string, string> | undefined;
|
|
26
|
+
}>, z.ZodObject<{
|
|
27
|
+
type: z.ZodLiteral<"sse">;
|
|
28
|
+
url: z.ZodString;
|
|
29
|
+
headers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
30
|
+
timeout: z.ZodDefault<z.ZodNumber>;
|
|
31
|
+
maxRetryTime: z.ZodDefault<z.ZodNumber>;
|
|
32
|
+
withCredentials: z.ZodDefault<z.ZodBoolean>;
|
|
33
|
+
}, "strip", z.ZodTypeAny, {
|
|
34
|
+
type: "sse";
|
|
35
|
+
timeout: number;
|
|
36
|
+
url: string;
|
|
37
|
+
maxRetryTime: number;
|
|
38
|
+
withCredentials: boolean;
|
|
39
|
+
headers?: Record<string, string> | undefined;
|
|
40
|
+
}, {
|
|
41
|
+
type: "sse";
|
|
42
|
+
url: string;
|
|
43
|
+
timeout?: number | undefined;
|
|
44
|
+
headers?: Record<string, string> | undefined;
|
|
45
|
+
maxRetryTime?: number | undefined;
|
|
46
|
+
withCredentials?: boolean | undefined;
|
|
47
|
+
}>, z.ZodObject<{
|
|
48
|
+
type: z.ZodLiteral<"http">;
|
|
49
|
+
url: z.ZodString;
|
|
50
|
+
headers: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
51
|
+
timeout: z.ZodDefault<z.ZodNumber>;
|
|
52
|
+
}, "strip", z.ZodTypeAny, {
|
|
53
|
+
type: "http";
|
|
54
|
+
timeout: number;
|
|
55
|
+
url: string;
|
|
56
|
+
headers?: Record<string, string> | undefined;
|
|
57
|
+
}, {
|
|
58
|
+
type: "http";
|
|
59
|
+
url: string;
|
|
60
|
+
timeout?: number | undefined;
|
|
61
|
+
headers?: Record<string, string> | undefined;
|
|
62
|
+
}>]>;
|
|
63
|
+
export type TransportConfig = z.infer<typeof TransportConfigSchema>;
|
|
64
|
+
export interface TransportStatus {
|
|
65
|
+
connected: boolean;
|
|
66
|
+
type: "stdio" | "sse" | "http";
|
|
67
|
+
lastConnected?: Date;
|
|
68
|
+
lastError?: Error;
|
|
69
|
+
reconnectAttempts: number;
|
|
70
|
+
}
|
|
71
|
+
export interface TransportManagerOptions {
|
|
72
|
+
autoReconnect?: boolean;
|
|
73
|
+
maxReconnectAttempts?: number;
|
|
74
|
+
reconnectDelay?: number;
|
|
75
|
+
healthCheckInterval?: number;
|
|
76
|
+
jitterEnabled?: boolean;
|
|
77
|
+
maxJitter?: number;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Manages MCP transport connections with automatic reconnection and health monitoring
|
|
81
|
+
*/
|
|
82
|
+
export declare class TransportManager {
|
|
83
|
+
private errorManager;
|
|
84
|
+
private options;
|
|
85
|
+
private client?;
|
|
86
|
+
private config?;
|
|
87
|
+
private status;
|
|
88
|
+
private reconnectTimer?;
|
|
89
|
+
private healthCheckTimer?;
|
|
90
|
+
private reconnectPromise?;
|
|
91
|
+
constructor(errorManager: ErrorManager, options?: TransportManagerOptions);
|
|
92
|
+
/**
|
|
93
|
+
* Connect to MCP server using specified transport
|
|
94
|
+
*/
|
|
95
|
+
connect(config: TransportConfig): Promise<Client>;
|
|
96
|
+
/**
|
|
97
|
+
* Create transport based on configuration
|
|
98
|
+
*/
|
|
99
|
+
createTransport(config: TransportConfig): Promise<any>;
|
|
100
|
+
/**
|
|
101
|
+
* Create stdio transport
|
|
102
|
+
*/
|
|
103
|
+
private createStdioTransport;
|
|
104
|
+
/**
|
|
105
|
+
* Create SSE transport with reconnection support
|
|
106
|
+
*/
|
|
107
|
+
private createSSETransport;
|
|
108
|
+
/**
|
|
109
|
+
* Create HTTP transport
|
|
110
|
+
*/
|
|
111
|
+
private createHTTPTransport;
|
|
112
|
+
/**
|
|
113
|
+
* Set up health monitoring
|
|
114
|
+
*/
|
|
115
|
+
private setupHealthMonitoring;
|
|
116
|
+
/**
|
|
117
|
+
* Handle connection errors
|
|
118
|
+
*/
|
|
119
|
+
private handleConnectionError;
|
|
120
|
+
/**
|
|
121
|
+
* Schedule reconnection attempt
|
|
122
|
+
*/
|
|
123
|
+
private scheduleReconnect;
|
|
124
|
+
/**
|
|
125
|
+
* Calculate reconnect delay with exponential backoff
|
|
126
|
+
*/
|
|
127
|
+
private calculateReconnectDelay;
|
|
128
|
+
/**
|
|
129
|
+
* Reconnect to server
|
|
130
|
+
*/
|
|
131
|
+
reconnect(): Promise<void>;
|
|
132
|
+
private _reconnect;
|
|
133
|
+
/**
|
|
134
|
+
* Disconnect from server
|
|
135
|
+
*/
|
|
136
|
+
disconnect(): Promise<void>;
|
|
137
|
+
/**
|
|
138
|
+
* Get current transport status
|
|
139
|
+
*/
|
|
140
|
+
getStatus(): TransportStatus;
|
|
141
|
+
/**
|
|
142
|
+
* Get connected client
|
|
143
|
+
*/
|
|
144
|
+
getClient(): Client | undefined;
|
|
145
|
+
/**
|
|
146
|
+
* Check if connected
|
|
147
|
+
*/
|
|
148
|
+
isConnected(): boolean;
|
|
149
|
+
/**
|
|
150
|
+
* Reset reconnection attempts
|
|
151
|
+
*/
|
|
152
|
+
resetReconnectAttempts(): void;
|
|
153
|
+
}
|
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport Manager for MCP connections
|
|
3
|
+
* Supports stdio, SSE, and HTTP transports with reconnection logic
|
|
4
|
+
*/
|
|
5
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
6
|
+
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import { ErrorManager } from "./error-manager.js";
|
|
9
|
+
import { ErrorCategory, ErrorSeverity } from "./error-manager.js";
|
|
10
|
+
// Transport configuration schemas
|
|
11
|
+
export const TransportConfigSchema = z.discriminatedUnion("type", [
|
|
12
|
+
z.object({
|
|
13
|
+
type: z.literal("stdio"),
|
|
14
|
+
command: z.string(),
|
|
15
|
+
args: z.array(z.string()).optional(),
|
|
16
|
+
cwd: z.string().optional(),
|
|
17
|
+
env: z.record(z.string()).optional(),
|
|
18
|
+
}),
|
|
19
|
+
z.object({
|
|
20
|
+
type: z.literal("sse"),
|
|
21
|
+
url: z.string().url(),
|
|
22
|
+
headers: z.record(z.string()).optional(),
|
|
23
|
+
timeout: z.number().min(5).default(30),
|
|
24
|
+
maxRetryTime: z.number().default(5000),
|
|
25
|
+
withCredentials: z.boolean().default(false),
|
|
26
|
+
}),
|
|
27
|
+
z.object({
|
|
28
|
+
type: z.literal("http"),
|
|
29
|
+
url: z.string().url(),
|
|
30
|
+
headers: z.record(z.string()).optional(),
|
|
31
|
+
timeout: z.number().min(5).default(30),
|
|
32
|
+
}),
|
|
33
|
+
]);
|
|
34
|
+
/**
|
|
35
|
+
* Manages MCP transport connections with automatic reconnection and health monitoring
|
|
36
|
+
*/
|
|
37
|
+
export class TransportManager {
|
|
38
|
+
errorManager;
|
|
39
|
+
options;
|
|
40
|
+
client;
|
|
41
|
+
config;
|
|
42
|
+
status;
|
|
43
|
+
reconnectTimer;
|
|
44
|
+
healthCheckTimer;
|
|
45
|
+
reconnectPromise;
|
|
46
|
+
constructor(errorManager, options = {}) {
|
|
47
|
+
this.errorManager = errorManager;
|
|
48
|
+
this.options = options;
|
|
49
|
+
// Initialize default options
|
|
50
|
+
this.options = {
|
|
51
|
+
jitterEnabled: true,
|
|
52
|
+
maxJitter: 1000,
|
|
53
|
+
...this.options,
|
|
54
|
+
};
|
|
55
|
+
this.status = {
|
|
56
|
+
connected: false,
|
|
57
|
+
type: "stdio",
|
|
58
|
+
reconnectAttempts: 0,
|
|
59
|
+
};
|
|
60
|
+
// Apply defaults
|
|
61
|
+
this.options = {
|
|
62
|
+
autoReconnect: true,
|
|
63
|
+
maxReconnectAttempts: 5,
|
|
64
|
+
reconnectDelay: 1000,
|
|
65
|
+
healthCheckInterval: 30000,
|
|
66
|
+
...options,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Connect to MCP server using specified transport
|
|
71
|
+
*/
|
|
72
|
+
async connect(config) {
|
|
73
|
+
try {
|
|
74
|
+
// Validate configuration
|
|
75
|
+
const validatedConfig = TransportConfigSchema.parse(config);
|
|
76
|
+
this.config = validatedConfig;
|
|
77
|
+
this.status.type = validatedConfig.type;
|
|
78
|
+
// Disconnect existing client if any
|
|
79
|
+
if (this.client) {
|
|
80
|
+
await this.disconnect();
|
|
81
|
+
}
|
|
82
|
+
// Create transport based on type
|
|
83
|
+
const transport = await this.createTransport(validatedConfig);
|
|
84
|
+
// Create MCP client
|
|
85
|
+
this.client = new Client({
|
|
86
|
+
name: "neurolink-mcp-client",
|
|
87
|
+
version: "1.0.0",
|
|
88
|
+
}, {
|
|
89
|
+
capabilities: {},
|
|
90
|
+
});
|
|
91
|
+
// Connect client
|
|
92
|
+
await this.client.connect(transport);
|
|
93
|
+
// Update status
|
|
94
|
+
this.status.connected = true;
|
|
95
|
+
this.status.lastConnected = new Date();
|
|
96
|
+
this.status.reconnectAttempts = 0;
|
|
97
|
+
// Set up health monitoring if enabled
|
|
98
|
+
if (this.options.healthCheckInterval &&
|
|
99
|
+
this.options.healthCheckInterval > 0) {
|
|
100
|
+
this.setupHealthMonitoring();
|
|
101
|
+
}
|
|
102
|
+
return this.client;
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
this.handleConnectionError(error);
|
|
106
|
+
throw error;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Create transport based on configuration
|
|
111
|
+
*/
|
|
112
|
+
async createTransport(config) {
|
|
113
|
+
switch (config.type) {
|
|
114
|
+
case "stdio":
|
|
115
|
+
return this.createStdioTransport(config);
|
|
116
|
+
case "sse":
|
|
117
|
+
return this.createSSETransport(config);
|
|
118
|
+
case "http":
|
|
119
|
+
return this.createHTTPTransport(config);
|
|
120
|
+
default:
|
|
121
|
+
throw new Error(`Unsupported transport type: ${config.type}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Create stdio transport
|
|
126
|
+
*/
|
|
127
|
+
createStdioTransport(config) {
|
|
128
|
+
// Filter out undefined values from process.env
|
|
129
|
+
const envWithoutUndefined = Object.fromEntries(Object.entries(process.env).filter(([_, value]) => value !== undefined));
|
|
130
|
+
// StdioClientTransport handles process creation internally
|
|
131
|
+
return new StdioClientTransport({
|
|
132
|
+
command: config.command,
|
|
133
|
+
args: config.args ?? [],
|
|
134
|
+
env: { ...envWithoutUndefined, ...config.env },
|
|
135
|
+
cwd: config.cwd,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Create SSE transport with reconnection support
|
|
140
|
+
*/
|
|
141
|
+
async createSSETransport(config) {
|
|
142
|
+
// Dynamically import SSE transport
|
|
143
|
+
const { SSEClientTransport } = await import("@modelcontextprotocol/sdk/client/sse.js");
|
|
144
|
+
// Use ReconnectingEventSource for reliability
|
|
145
|
+
const ReconnectingEventSource = await import("reconnecting-eventsource").then((m) => m.default);
|
|
146
|
+
return new SSEClientTransport(new URL(config.url), {
|
|
147
|
+
requestInit: {
|
|
148
|
+
headers: config.headers,
|
|
149
|
+
...(config.timeout && { timeout: config.timeout }),
|
|
150
|
+
},
|
|
151
|
+
eventSourceInit: {
|
|
152
|
+
eventSource: ReconnectingEventSource,
|
|
153
|
+
max_retry_time: config.maxRetryTime,
|
|
154
|
+
withCredentials: config.withCredentials,
|
|
155
|
+
},
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Create HTTP transport
|
|
160
|
+
*/
|
|
161
|
+
async createHTTPTransport(config) {
|
|
162
|
+
// Dynamically import HTTP transport
|
|
163
|
+
const httpModule = await import("@modelcontextprotocol/sdk/client/streamableHttp.js");
|
|
164
|
+
const HTTPClientTransport = httpModule.default || httpModule.HTTPClientTransport;
|
|
165
|
+
return new HTTPClientTransport(new URL(config.url), {
|
|
166
|
+
requestInit: {
|
|
167
|
+
headers: config.headers,
|
|
168
|
+
timeout: config.timeout * 1000, // Convert seconds to milliseconds
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Set up health monitoring
|
|
174
|
+
*/
|
|
175
|
+
setupHealthMonitoring() {
|
|
176
|
+
if (!this.client) {
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
// Clear any existing health check timer
|
|
180
|
+
if (this.healthCheckTimer) {
|
|
181
|
+
clearInterval(this.healthCheckTimer);
|
|
182
|
+
}
|
|
183
|
+
// Set up periodic health checks
|
|
184
|
+
this.healthCheckTimer = setInterval(async () => {
|
|
185
|
+
if (!this.client) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
try {
|
|
189
|
+
// Simple health check - list tools
|
|
190
|
+
await this.client.request({ method: "tools/list" }, z.object({ tools: z.array(z.any()) }));
|
|
191
|
+
}
|
|
192
|
+
catch (error) {
|
|
193
|
+
// Health check failed, trigger reconnection if enabled
|
|
194
|
+
if (this.options.autoReconnect && this.config) {
|
|
195
|
+
this.handleConnectionError(error);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}, this.options.healthCheckInterval);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Handle connection errors
|
|
202
|
+
*/
|
|
203
|
+
handleConnectionError(error) {
|
|
204
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
205
|
+
this.status.connected = false;
|
|
206
|
+
this.status.lastError = err;
|
|
207
|
+
// Record error
|
|
208
|
+
this.errorManager.recordError(err, {
|
|
209
|
+
category: ErrorCategory.CONNECTION_ERROR,
|
|
210
|
+
severity: ErrorSeverity.HIGH,
|
|
211
|
+
toolName: "transport-manager",
|
|
212
|
+
parameters: {
|
|
213
|
+
transport: this.status.type,
|
|
214
|
+
reconnectAttempts: this.status.reconnectAttempts,
|
|
215
|
+
},
|
|
216
|
+
});
|
|
217
|
+
// Attempt reconnection if enabled
|
|
218
|
+
if (this.options.autoReconnect &&
|
|
219
|
+
this.status.reconnectAttempts < this.options.maxReconnectAttempts) {
|
|
220
|
+
// Schedule reconnection attempt
|
|
221
|
+
this.scheduleReconnect();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Schedule reconnection attempt
|
|
226
|
+
*/
|
|
227
|
+
scheduleReconnect() {
|
|
228
|
+
if (this.reconnectTimer) {
|
|
229
|
+
clearTimeout(this.reconnectTimer);
|
|
230
|
+
}
|
|
231
|
+
// Increment attempts BEFORE calculating delay (uses updated attempt count)
|
|
232
|
+
this.status.reconnectAttempts++;
|
|
233
|
+
const delay = this.calculateReconnectDelay();
|
|
234
|
+
this.reconnectTimer = setTimeout(() => {
|
|
235
|
+
this.reconnect().catch((error) => {
|
|
236
|
+
console.error("Reconnection failed:", error);
|
|
237
|
+
});
|
|
238
|
+
}, delay);
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Calculate reconnect delay with exponential backoff
|
|
242
|
+
*/
|
|
243
|
+
calculateReconnectDelay() {
|
|
244
|
+
const baseDelay = this.options.reconnectDelay || 1000;
|
|
245
|
+
const maxDelay = 30000; // 30 seconds max
|
|
246
|
+
const delay = Math.min(baseDelay * Math.pow(2, this.status.reconnectAttempts), maxDelay);
|
|
247
|
+
// Add configurable jitter to prevent thundering herd
|
|
248
|
+
if (this.options.jitterEnabled === true) {
|
|
249
|
+
const maxJitter = this.options.maxJitter || 1000;
|
|
250
|
+
return delay + Math.random() * maxJitter;
|
|
251
|
+
}
|
|
252
|
+
return delay;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Reconnect to server
|
|
256
|
+
*/
|
|
257
|
+
async reconnect() {
|
|
258
|
+
// Prevent concurrent reconnection attempts
|
|
259
|
+
if (this.reconnectPromise) {
|
|
260
|
+
return this.reconnectPromise;
|
|
261
|
+
}
|
|
262
|
+
this.reconnectPromise = this._reconnect();
|
|
263
|
+
try {
|
|
264
|
+
await this.reconnectPromise;
|
|
265
|
+
}
|
|
266
|
+
finally {
|
|
267
|
+
this.reconnectPromise = undefined;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
async _reconnect() {
|
|
271
|
+
if (!this.config) {
|
|
272
|
+
throw new Error("No configuration available for reconnection");
|
|
273
|
+
}
|
|
274
|
+
try {
|
|
275
|
+
await this.connect(this.config);
|
|
276
|
+
console.log(`Reconnected successfully after ${this.status.reconnectAttempts} attempts`);
|
|
277
|
+
}
|
|
278
|
+
catch (error) {
|
|
279
|
+
if (this.status.reconnectAttempts >= this.options.maxReconnectAttempts) {
|
|
280
|
+
throw new Error(`Max reconnection attempts (${this.options.maxReconnectAttempts}) reached`);
|
|
281
|
+
}
|
|
282
|
+
throw error;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Disconnect from server
|
|
287
|
+
*/
|
|
288
|
+
async disconnect() {
|
|
289
|
+
// Clear reconnection timer
|
|
290
|
+
if (this.reconnectTimer) {
|
|
291
|
+
clearTimeout(this.reconnectTimer);
|
|
292
|
+
this.reconnectTimer = undefined;
|
|
293
|
+
}
|
|
294
|
+
// Stop health monitoring
|
|
295
|
+
if (this.healthCheckTimer) {
|
|
296
|
+
clearInterval(this.healthCheckTimer);
|
|
297
|
+
this.healthCheckTimer = undefined;
|
|
298
|
+
}
|
|
299
|
+
// Close client connection
|
|
300
|
+
if (this.client) {
|
|
301
|
+
await this.client.close();
|
|
302
|
+
this.client = undefined;
|
|
303
|
+
}
|
|
304
|
+
// Update status
|
|
305
|
+
this.status.connected = false;
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Get current transport status
|
|
309
|
+
*/
|
|
310
|
+
getStatus() {
|
|
311
|
+
return { ...this.status };
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Get connected client
|
|
315
|
+
*/
|
|
316
|
+
getClient() {
|
|
317
|
+
return this.client;
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Check if connected
|
|
321
|
+
*/
|
|
322
|
+
isConnected() {
|
|
323
|
+
return this.status.connected && this.client !== undefined;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Reset reconnection attempts
|
|
327
|
+
*/
|
|
328
|
+
resetReconnectAttempts() {
|
|
329
|
+
this.status.reconnectAttempts = 0;
|
|
330
|
+
}
|
|
331
|
+
}
|
package/dist/mcp/unified-mcp.js
CHANGED
|
@@ -139,6 +139,11 @@ export class UnifiedMCPSystem {
|
|
|
139
139
|
const registryStats = await this.registry.getStats();
|
|
140
140
|
const externalStatus = this.externalManager.getStatus();
|
|
141
141
|
const internalToolCount = this.internalServers.reduce((sum, server) => sum + Object.keys(server.tools).length, 0);
|
|
142
|
+
// Calculate total tools from registry
|
|
143
|
+
const registryToolCount = this.registry.getToolCount
|
|
144
|
+
? this.registry.getToolCount()
|
|
145
|
+
: 0;
|
|
146
|
+
const totalTools = internalToolCount + externalStatus.totalTools + registryToolCount;
|
|
142
147
|
return {
|
|
143
148
|
isInitialized: this.isInitialized,
|
|
144
149
|
internalServers: {
|
|
@@ -150,7 +155,7 @@ export class UnifiedMCPSystem {
|
|
|
150
155
|
connected: externalStatus.connected,
|
|
151
156
|
tools: externalStatus.totalTools,
|
|
152
157
|
},
|
|
153
|
-
totalTools
|
|
158
|
+
totalTools,
|
|
154
159
|
registryStats,
|
|
155
160
|
};
|
|
156
161
|
}
|
|
@@ -3,15 +3,21 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import type { DiscoveredMCP, ExecutionContext } from "./contracts/mcp-contract.js";
|
|
5
5
|
import type { DiscoveryOptions } from "./auto-discovery.js";
|
|
6
|
-
import { MCPToolRegistry, type ToolInfo
|
|
6
|
+
import { MCPToolRegistry, type ToolInfo } from "./tool-registry.js";
|
|
7
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
8
|
+
import { ErrorManager } from "./error-manager.js";
|
|
7
9
|
/**
|
|
8
10
|
* Unified registry combining multiple sources
|
|
9
11
|
*/
|
|
10
12
|
export declare class UnifiedMCPRegistry extends MCPToolRegistry {
|
|
13
|
+
private errorManager;
|
|
11
14
|
private autoDiscoveryEnabled;
|
|
12
15
|
private autoDiscoveredServers;
|
|
13
16
|
private manualServers;
|
|
14
17
|
private availableServers;
|
|
18
|
+
private transportManager;
|
|
19
|
+
private activeConnections;
|
|
20
|
+
constructor(errorManager?: ErrorManager);
|
|
15
21
|
/**
|
|
16
22
|
* Initialize with auto-discovery
|
|
17
23
|
*/
|
|
@@ -47,7 +53,7 @@ export declare class UnifiedMCPRegistry extends MCPToolRegistry {
|
|
|
47
53
|
/**
|
|
48
54
|
* Execute a tool through the registry
|
|
49
55
|
*/
|
|
50
|
-
executeTool(toolName: string, args
|
|
56
|
+
executeTool<T = unknown>(toolName: string, args?: unknown, context?: ExecutionContext): Promise<T>;
|
|
51
57
|
/**
|
|
52
58
|
* Lazily activate a server by ID
|
|
53
59
|
*/
|
|
@@ -57,9 +63,17 @@ export declare class UnifiedMCPRegistry extends MCPToolRegistry {
|
|
|
57
63
|
*/
|
|
58
64
|
registerManualServer(id: string, server: any): void;
|
|
59
65
|
/**
|
|
60
|
-
* Get registry statistics
|
|
66
|
+
* Get registry statistics (override parent method)
|
|
61
67
|
*/
|
|
62
|
-
getStats():
|
|
68
|
+
getStats(): Record<string, {
|
|
69
|
+
count: number;
|
|
70
|
+
averageTime: number;
|
|
71
|
+
totalTime: number;
|
|
72
|
+
}>;
|
|
73
|
+
/**
|
|
74
|
+
* Get detailed registry statistics
|
|
75
|
+
*/
|
|
76
|
+
getDetailedStats(): Promise<{
|
|
63
77
|
total: number;
|
|
64
78
|
bySource: Record<string, number>;
|
|
65
79
|
byType: Record<string, number>;
|
|
@@ -72,9 +86,44 @@ export declare class UnifiedMCPRegistry extends MCPToolRegistry {
|
|
|
72
86
|
tools?: number;
|
|
73
87
|
}>;
|
|
74
88
|
/**
|
|
75
|
-
*
|
|
89
|
+
* Add external MCP server programmatically
|
|
90
|
+
*
|
|
91
|
+
* @param serverId - Unique server identifier
|
|
92
|
+
* @param config - Server configuration (stdio, sse, or http)
|
|
93
|
+
*/
|
|
94
|
+
addExternalServer(serverId: string, config: {
|
|
95
|
+
type: "stdio" | "sse" | "http";
|
|
96
|
+
command?: string;
|
|
97
|
+
args?: string[];
|
|
98
|
+
env?: Record<string, string>;
|
|
99
|
+
cwd?: string;
|
|
100
|
+
url?: string;
|
|
101
|
+
headers?: Record<string, string>;
|
|
102
|
+
timeout?: number;
|
|
103
|
+
}): Promise<void>;
|
|
104
|
+
/**
|
|
105
|
+
* Get active connection for a server
|
|
106
|
+
*/
|
|
107
|
+
getConnection(serverId: string): Client | undefined;
|
|
108
|
+
/**
|
|
109
|
+
* Check if server is actively connected
|
|
110
|
+
*/
|
|
111
|
+
isConnected(serverId: string): boolean;
|
|
112
|
+
/**
|
|
113
|
+
* Clear all registries and active connections (synchronous, preserves base API contract)
|
|
114
|
+
*/
|
|
115
|
+
/**
|
|
116
|
+
* Clear registries without closing connections (internal use)
|
|
117
|
+
*/
|
|
118
|
+
private clearRegistriesOnly;
|
|
119
|
+
/**
|
|
120
|
+
* Clear all registries and initiate async connection cleanup
|
|
76
121
|
*/
|
|
77
122
|
clear(): void;
|
|
123
|
+
/**
|
|
124
|
+
* Clear all registries and close active connections asynchronously
|
|
125
|
+
*/
|
|
126
|
+
clearAsync(): Promise<void>;
|
|
78
127
|
}
|
|
79
128
|
/**
|
|
80
129
|
* Default unified registry instance
|