@jadchene/mcp-gateway-service 0.11.2 → 0.11.3
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/dist/mcp/http-client.js +30 -1
- package/dist/mcp/sdk-client.js +39 -15
- package/package.json +1 -1
package/dist/mcp/http-client.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/client";
|
|
1
|
+
import { SdkHttpError, StreamableHTTPClientTransport } from "@modelcontextprotocol/client";
|
|
2
2
|
import { SdkMcpClient } from "./sdk-client.js";
|
|
3
3
|
/**
|
|
4
4
|
* Connects to a downstream HTTP MCP service through the official SDK v2.
|
|
@@ -18,6 +18,35 @@ export class StreamableHttpClient extends SdkMcpClient {
|
|
|
18
18
|
}
|
|
19
19
|
}));
|
|
20
20
|
}
|
|
21
|
+
/** 仅对已建立的旧版 HTTP 会话识别失效响应,不将认证或参数错误视为重连信号。 */
|
|
22
|
+
isSessionExpired(error, client) {
|
|
23
|
+
const transport = client?.transport;
|
|
24
|
+
if (!(error instanceof SdkHttpError)
|
|
25
|
+
|| !(transport instanceof StreamableHTTPClientTransport)
|
|
26
|
+
|| !transport.sessionId
|
|
27
|
+
|| client?.getProtocolEra() !== "legacy") {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
if (error.status === 404) {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
if (error.status !== 400 || typeof error.data.text !== "string") {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const response = JSON.parse(error.data.text);
|
|
38
|
+
// 微信开发者工具在旧会话不存在时返回通用 Bad Request,且响应不关联请求 ID。
|
|
39
|
+
return response?.error === "Invalid or missing session ID"
|
|
40
|
+
|| (response?.jsonrpc === "2.0"
|
|
41
|
+
&& response?.id === null
|
|
42
|
+
&& response?.error?.code === -32000
|
|
43
|
+
&& response?.error?.message === "Bad Request")
|
|
44
|
+
|| response?.error?.message === "Session not found";
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
21
50
|
}
|
|
22
51
|
/**
|
|
23
52
|
* Applies static configured headers without allowing them to replace per-request SDK headers.
|
package/dist/mcp/sdk-client.js
CHANGED
|
@@ -90,8 +90,7 @@ export class SdkMcpClient {
|
|
|
90
90
|
* Refreshes server identity and the complete tool catalog.
|
|
91
91
|
*/
|
|
92
92
|
async getMetadata() {
|
|
93
|
-
return this.executeWithRecovery(async () => {
|
|
94
|
-
const client = await this.requireClient();
|
|
93
|
+
return this.executeWithRecovery(async (client) => {
|
|
95
94
|
const tools = await this.requestToolsList(client);
|
|
96
95
|
return {
|
|
97
96
|
protocolVersion: client.getNegotiatedProtocolVersion() ?? null,
|
|
@@ -106,14 +105,16 @@ export class SdkMcpClient {
|
|
|
106
105
|
* Lists the downstream tools using SDK caching and pagination behavior.
|
|
107
106
|
*/
|
|
108
107
|
async listTools() {
|
|
109
|
-
return this.executeWithRecovery(
|
|
108
|
+
return this.executeWithRecovery((client) => this.requestToolsList(client));
|
|
110
109
|
}
|
|
111
110
|
/**
|
|
112
111
|
* Calls a downstream tool while preserving cancellation, custom headers, and MRTR state.
|
|
113
112
|
*/
|
|
114
113
|
async callTool(name, args, context = {}) {
|
|
114
|
+
let client = null;
|
|
115
115
|
try {
|
|
116
|
-
|
|
116
|
+
client = await this.requireClient();
|
|
117
|
+
const callClient = client;
|
|
117
118
|
const params = {
|
|
118
119
|
name,
|
|
119
120
|
arguments: args,
|
|
@@ -124,7 +125,7 @@ export class SdkMcpClient {
|
|
|
124
125
|
};
|
|
125
126
|
const invoke = async (signal) => {
|
|
126
127
|
const callSignal = combineWithTimeout(signal, this.service.callTimeoutMs ?? DEFAULT_CALL_TIMEOUT_MS);
|
|
127
|
-
const result = await
|
|
128
|
+
const result = await callClient.callTool(params, {
|
|
128
129
|
signal: callSignal,
|
|
129
130
|
allowInputRequired: true,
|
|
130
131
|
toolDefinition: this.tools.get(name)
|
|
@@ -132,20 +133,28 @@ export class SdkMcpClient {
|
|
|
132
133
|
return result;
|
|
133
134
|
};
|
|
134
135
|
if (client.getProtocolEra() === "legacy") {
|
|
135
|
-
return this.formElicitationBridge.execute(name, args, context, invoke);
|
|
136
|
+
return await this.formElicitationBridge.execute(name, args, context, invoke);
|
|
136
137
|
}
|
|
137
138
|
const result = await invoke(context.signal ?? new AbortController().signal);
|
|
138
139
|
return result;
|
|
139
140
|
}
|
|
140
141
|
catch (error) {
|
|
141
142
|
const normalized = normalizeError(error);
|
|
143
|
+
if (this.isSessionExpired(normalized, client)) {
|
|
144
|
+
await this.disposeConnection(client);
|
|
145
|
+
throw new Error(`Downstream session expired while calling '${name}'; the gateway did not replay it. The next call will reconnect. ${normalized.message}`, { cause: normalized });
|
|
146
|
+
}
|
|
142
147
|
if (isNonRetryable(normalized) || context.signal?.aborted) {
|
|
143
148
|
throw normalized;
|
|
144
149
|
}
|
|
145
|
-
await this.disposeConnection();
|
|
150
|
+
await this.disposeConnection(client);
|
|
146
151
|
throw new Error(`Downstream tool '${name}' failed; execution status is unknown and the gateway did not replay it. ${normalized.message}`, { cause: normalized });
|
|
147
152
|
}
|
|
148
153
|
}
|
|
154
|
+
/** 判断传输层是否明确报告当前会话已失效,默认传输不作此判断。 */
|
|
155
|
+
isSessionExpired(_error, _client) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
149
158
|
/**
|
|
150
159
|
* Connects one fresh SDK client and transport.
|
|
151
160
|
*/
|
|
@@ -185,6 +194,7 @@ export class SdkMcpClient {
|
|
|
185
194
|
message: error.message
|
|
186
195
|
});
|
|
187
196
|
};
|
|
197
|
+
client.onclose = () => this.clearConnection(client);
|
|
188
198
|
const transport = transportFactory();
|
|
189
199
|
try {
|
|
190
200
|
await client.connect(transport, { timeout: CONNECT_TIMEOUT_MS });
|
|
@@ -225,22 +235,24 @@ export class SdkMcpClient {
|
|
|
225
235
|
return result.tools.map(normalizeToolDefinition);
|
|
226
236
|
}
|
|
227
237
|
/**
|
|
228
|
-
*
|
|
238
|
+
* 重试只读元数据请求的传输或会话错误,保留其他协议及 HTTP 错误。
|
|
229
239
|
*/
|
|
230
240
|
async executeWithRecovery(operation) {
|
|
231
241
|
while (true) {
|
|
232
242
|
if (this.unavailableReason) {
|
|
233
243
|
throw new Error(this.unavailableReason);
|
|
234
244
|
}
|
|
245
|
+
let client = null;
|
|
235
246
|
try {
|
|
236
|
-
|
|
247
|
+
client = await this.requireClient();
|
|
248
|
+
return await operation(client);
|
|
237
249
|
}
|
|
238
250
|
catch (error) {
|
|
239
251
|
const normalized = normalizeError(error);
|
|
240
|
-
if (isNonRetryable(normalized)) {
|
|
252
|
+
if (isNonRetryable(normalized) && !this.isSessionExpired(normalized, client)) {
|
|
241
253
|
throw normalized;
|
|
242
254
|
}
|
|
243
|
-
await this.disposeConnection();
|
|
255
|
+
await this.disposeConnection(client);
|
|
244
256
|
if (this.restartAttempts >= MAX_RESTART_ATTEMPTS) {
|
|
245
257
|
this.unavailableReason = `Service '${this.key}' is unavailable after ${MAX_RESTART_ATTEMPTS} restart attempts. Last error: ${normalized.message}`;
|
|
246
258
|
throw new Error(this.unavailableReason);
|
|
@@ -255,14 +267,26 @@ export class SdkMcpClient {
|
|
|
255
267
|
}
|
|
256
268
|
}
|
|
257
269
|
/**
|
|
258
|
-
*
|
|
270
|
+
* 关闭失败请求所属的连接,避免旧请求误关闭已经建立的新连接。
|
|
259
271
|
*/
|
|
260
|
-
async disposeConnection() {
|
|
261
|
-
this.formElicitationBridge.reset(new Error(`Downstream service '${this.key}' connection was closed.`));
|
|
272
|
+
async disposeConnection(expectedClient) {
|
|
262
273
|
const client = this.client;
|
|
274
|
+
if (expectedClient !== undefined && client !== expectedClient) {
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
if (client) {
|
|
278
|
+
this.clearConnection(client);
|
|
279
|
+
await client.close().catch(() => undefined);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
/** 清理当前连接的派生状态;已替换连接的关闭事件不影响新连接。 */
|
|
283
|
+
clearConnection(client) {
|
|
284
|
+
if (this.client !== client) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
this.formElicitationBridge.reset(new Error(`Downstream service '${this.key}' connection was closed.`));
|
|
263
288
|
this.client = null;
|
|
264
289
|
this.tools.clear();
|
|
265
|
-
await client?.close().catch(() => undefined);
|
|
266
290
|
}
|
|
267
291
|
}
|
|
268
292
|
/**
|
package/package.json
CHANGED