@momeidejiyi/dsh-general-mcp 0.1.8 → 0.1.9
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/lib/index.js +101 -26
- package/package.json +1 -1
- package/src/connect.ts +4 -0
- package/src/index.ts +93 -16
- package/src/sse.ts +40 -3
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// @momeidejiyi/dsh-general-mcp@0.1.
|
|
1
|
+
// @momeidejiyi/dsh-general-mcp@0.1.9 (generated)
|
|
2
2
|
|
|
3
3
|
// packages/general-mcp/src/index.ts
|
|
4
4
|
import { appendFileSync } from "node:fs";
|
|
@@ -325,6 +325,27 @@ async function readSseResult(response, id) {
|
|
|
325
325
|
}
|
|
326
326
|
|
|
327
327
|
// packages/general-mcp/src/sse.ts
|
|
328
|
+
function isLocalHost(hostname) {
|
|
329
|
+
const host = hostname.toLowerCase();
|
|
330
|
+
return host === "localhost" || host === "127.0.0.1" || host === "::1" || host === "[::1]" || host === "0.0.0.0" || /^10\./.test(host) || /^192\.168\./.test(host) || /^172\.(1[6-9]|2\d|3[01])\./.test(host);
|
|
331
|
+
}
|
|
332
|
+
function resolveMessageUrl(endpoint, sseUrl) {
|
|
333
|
+
let target;
|
|
334
|
+
let sse;
|
|
335
|
+
try {
|
|
336
|
+
sse = new URL(sseUrl);
|
|
337
|
+
target = new URL(endpoint, sseUrl);
|
|
338
|
+
} catch {
|
|
339
|
+
return endpoint;
|
|
340
|
+
}
|
|
341
|
+
if (target.origin !== sse.origin && isLocalHost(target.hostname)) {
|
|
342
|
+
target.protocol = sse.protocol;
|
|
343
|
+
target.hostname = sse.hostname;
|
|
344
|
+
target.port = sse.port;
|
|
345
|
+
return target.toString();
|
|
346
|
+
}
|
|
347
|
+
return target.toString();
|
|
348
|
+
}
|
|
328
349
|
var SseClient = class {
|
|
329
350
|
constructor(url, headers = {}, timeoutMs = 6e4) {
|
|
330
351
|
this.url = url;
|
|
@@ -341,6 +362,10 @@ var SseClient = class {
|
|
|
341
362
|
get protocolVersion() {
|
|
342
363
|
return "2024-11-05";
|
|
343
364
|
}
|
|
365
|
+
/** Where requests are POSTed; useful in diagnostics. */
|
|
366
|
+
get messageEndpoint() {
|
|
367
|
+
return this.messageUrl;
|
|
368
|
+
}
|
|
344
369
|
/** Open the stream and wait for the endpoint announcement. */
|
|
345
370
|
connect() {
|
|
346
371
|
if (this.connected !== void 0) return this.connected;
|
|
@@ -351,7 +376,7 @@ var SseClient = class {
|
|
|
351
376
|
if (response.body === null) throw new Error("SSE connect returned no body");
|
|
352
377
|
this.reader = response.body.getReader();
|
|
353
378
|
const announced = new Promise((resolve, reject) => {
|
|
354
|
-
const timer = setTimeout(() => reject(new Error("SSE endpoint event timed out")), this.timeoutMs);
|
|
379
|
+
const timer = setTimeout(() => reject(new Error("SSE endpoint event timed out after " + this.timeoutMs + "ms (GET " + this.url + ")")), this.timeoutMs);
|
|
355
380
|
this.endpointWaiter = { resolve, reject, timer };
|
|
356
381
|
});
|
|
357
382
|
void this.pump();
|
|
@@ -389,11 +414,7 @@ var SseClient = class {
|
|
|
389
414
|
}
|
|
390
415
|
const payload = data.join("\n");
|
|
391
416
|
if (event === "endpoint") {
|
|
392
|
-
|
|
393
|
-
this.messageUrl = new URL(payload, this.url).toString();
|
|
394
|
-
} catch {
|
|
395
|
-
this.messageUrl = payload;
|
|
396
|
-
}
|
|
417
|
+
this.messageUrl = resolveMessageUrl(payload, this.url);
|
|
397
418
|
const waiter = this.endpointWaiter;
|
|
398
419
|
this.endpointWaiter = void 0;
|
|
399
420
|
if (waiter !== void 0) {
|
|
@@ -450,7 +471,7 @@ var SseClient = class {
|
|
|
450
471
|
const reply = new Promise((resolve, reject) => {
|
|
451
472
|
const timer = setTimeout(() => {
|
|
452
473
|
this.pending.delete(id);
|
|
453
|
-
reject(new Error("MCP " + method + " timed out after " + this.timeoutMs + "ms"));
|
|
474
|
+
reject(new Error("MCP " + method + " timed out after " + this.timeoutMs + "ms (POST " + this.messageUrl + ")"));
|
|
454
475
|
}, this.timeoutMs);
|
|
455
476
|
this.pending.set(id, { resolve, reject, timer });
|
|
456
477
|
});
|
|
@@ -530,6 +551,7 @@ async function viaStreamable(url, headers, timeoutMs) {
|
|
|
530
551
|
const info = await client.initialize();
|
|
531
552
|
return {
|
|
532
553
|
transport: "streamable",
|
|
554
|
+
detail: "POST " + url,
|
|
533
555
|
protocolVersion: info?.protocolVersion,
|
|
534
556
|
serverInfo: info?.serverInfo,
|
|
535
557
|
async initialize() {
|
|
@@ -544,6 +566,7 @@ async function viaSse(url, headers, timeoutMs) {
|
|
|
544
566
|
const info = await client.initialize();
|
|
545
567
|
return {
|
|
546
568
|
transport: "sse",
|
|
569
|
+
detail: "GET " + url + " \u2192 POST " + (client.messageEndpoint ?? "(pending)"),
|
|
547
570
|
protocolVersion: info?.protocolVersion,
|
|
548
571
|
serverInfo: info?.serverInfo,
|
|
549
572
|
async initialize() {
|
|
@@ -668,7 +691,10 @@ function state() {
|
|
|
668
691
|
servers: /* @__PURE__ */ new Map(),
|
|
669
692
|
version: 0,
|
|
670
693
|
debug: false,
|
|
671
|
-
timeoutMs: 6e4
|
|
694
|
+
timeoutMs: 6e4,
|
|
695
|
+
retryIntervalMs: 3e4,
|
|
696
|
+
maxRetries: 20,
|
|
697
|
+
pollIntervalMs: 15e3
|
|
672
698
|
};
|
|
673
699
|
}
|
|
674
700
|
return shared.__GENMCP_STATE__;
|
|
@@ -687,7 +713,9 @@ function serverState(name2) {
|
|
|
687
713
|
catalog: [],
|
|
688
714
|
tools: /* @__PURE__ */ new Map(),
|
|
689
715
|
settings: {},
|
|
690
|
-
attempts: 0
|
|
716
|
+
attempts: 0,
|
|
717
|
+
enabled: true,
|
|
718
|
+
timeoutMs: 0
|
|
691
719
|
};
|
|
692
720
|
live.servers.set(name2, entry);
|
|
693
721
|
}
|
|
@@ -718,8 +746,11 @@ function normalizeSettings(raw) {
|
|
|
718
746
|
function normalizeConfig(config) {
|
|
719
747
|
const raw = config ?? {};
|
|
720
748
|
const live = state();
|
|
721
|
-
const
|
|
722
|
-
live.timeoutMs =
|
|
749
|
+
const clamp = (value, fallback, min, max) => typeof value === "number" && Number.isFinite(value) ? Math.min(Math.max(Math.round(value), min), max) : fallback;
|
|
750
|
+
live.timeoutMs = clamp(raw.toolCallTimeoutMs, 6e4, 1e3, 6e5);
|
|
751
|
+
live.retryIntervalMs = clamp(raw.retryIntervalMs, 3e4, 5e3, 6e5);
|
|
752
|
+
live.maxRetries = clamp(raw.maxRetries, 20, 0, 1e3);
|
|
753
|
+
live.pollIntervalMs = clamp(raw.pollIntervalMs, 15e3, 2e3, 6e5);
|
|
723
754
|
live.debug = raw.debug === true;
|
|
724
755
|
const list = Array.isArray(raw.servers) ? raw.servers.map((item) => ({ ...item })) : [{ name: raw.serverName, endpoint: raw.endpoint, headers: raw.headers, tools: raw.tools }];
|
|
725
756
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -763,9 +794,13 @@ function apply(ctx, config) {
|
|
|
763
794
|
if (typeof value === "string") headers[key] = value;
|
|
764
795
|
}
|
|
765
796
|
}
|
|
766
|
-
const
|
|
797
|
+
const enabled = item.enabled !== false;
|
|
798
|
+
const serverTimeout = typeof item.timeoutMs === "number" && Number.isFinite(item.timeoutMs) ? Math.min(Math.max(Math.round(item.timeoutMs), 1e3), 6e5) : 0;
|
|
799
|
+
const changed = entry.endpoint !== (item.endpoint ?? "") || JSON.stringify(entry.headers) !== JSON.stringify(headers) || entry.enabled !== enabled || entry.timeoutMs !== serverTimeout;
|
|
767
800
|
entry.endpoint = item.endpoint ?? "";
|
|
768
801
|
entry.headers = headers;
|
|
802
|
+
entry.enabled = enabled;
|
|
803
|
+
entry.timeoutMs = serverTimeout;
|
|
769
804
|
const nextSettings = normalizeSettings(item.tools);
|
|
770
805
|
if (entry.sync !== void 0 && JSON.stringify(entry.settings) !== JSON.stringify(nextSettings)) entry.stale = true;
|
|
771
806
|
entry.settings = nextSettings;
|
|
@@ -793,12 +828,13 @@ function apply(ctx, config) {
|
|
|
793
828
|
entry.attempts = 0;
|
|
794
829
|
live.version += 1;
|
|
795
830
|
}
|
|
796
|
-
if (entry.endpoint === "") {
|
|
831
|
+
if (entry.endpoint === "" || entry.enabled === false) {
|
|
797
832
|
disposeTools(entry);
|
|
833
|
+
stopRetry(entry);
|
|
798
834
|
entry.connected = false;
|
|
799
835
|
entry.toolCount = 0;
|
|
800
836
|
entry.catalog = [];
|
|
801
|
-
entry.lastError = "";
|
|
837
|
+
entry.lastError = entry.enabled === false ? "" : entry.lastError;
|
|
802
838
|
continue;
|
|
803
839
|
}
|
|
804
840
|
void sync(entry).then((ok) => {
|
|
@@ -841,11 +877,13 @@ function createRuntime(ctx) {
|
|
|
841
877
|
const alive = (entry) => entry.disposed !== true && live.servers.get(entry.name) === entry;
|
|
842
878
|
const sync = (entry) => {
|
|
843
879
|
if (entry.sync !== void 0) return entry.sync;
|
|
880
|
+
if (entry.endpoint === "" || entry.enabled === false) return Promise.resolve(false);
|
|
844
881
|
const run = (async () => {
|
|
845
882
|
const generation = entry.generation ?? 0;
|
|
846
883
|
try {
|
|
847
|
-
if (entry.endpoint === "") return false;
|
|
848
|
-
const
|
|
884
|
+
if (entry.endpoint === "" || entry.enabled === false) return false;
|
|
885
|
+
const timeout = entry.timeoutMs > 0 ? entry.timeoutMs : live.timeoutMs;
|
|
886
|
+
const client = await connectMcp(entry.endpoint, entry.headers, timeout);
|
|
849
887
|
if (!alive(entry) || (entry.generation ?? 0) !== generation) {
|
|
850
888
|
await client.close().catch(() => {
|
|
851
889
|
});
|
|
@@ -910,9 +948,9 @@ function createRuntime(ctx) {
|
|
|
910
948
|
entry.retryTimer = setInterval(() => {
|
|
911
949
|
entry.attempts += 1;
|
|
912
950
|
void sync(entry).then((ok) => {
|
|
913
|
-
if (ok || entry.attempts >=
|
|
951
|
+
if (ok || entry.attempts >= live.maxRetries) stopRetry(entry);
|
|
914
952
|
});
|
|
915
|
-
},
|
|
953
|
+
}, live.retryIntervalMs);
|
|
916
954
|
};
|
|
917
955
|
return { disposeTools, registerTools, sync, scheduleRetry, stopRetry };
|
|
918
956
|
}
|
|
@@ -1007,10 +1045,19 @@ function installRoutes(ctx) {
|
|
|
1007
1045
|
if (since === live.version) return new Response(null, { status: 304, headers: { ...allowed, "cache-control": "no-store" } });
|
|
1008
1046
|
return new Response(JSON.stringify({
|
|
1009
1047
|
version: live.version,
|
|
1048
|
+
timeoutMs: live.timeoutMs,
|
|
1049
|
+
retryIntervalMs: live.retryIntervalMs,
|
|
1050
|
+
maxRetries: live.maxRetries,
|
|
1051
|
+
pollIntervalMs: live.pollIntervalMs,
|
|
1052
|
+
debug: live.debug,
|
|
1010
1053
|
servers: [...live.servers.values()].map((entry) => ({
|
|
1011
1054
|
name: entry.name,
|
|
1012
1055
|
endpoint: entry.endpoint,
|
|
1013
|
-
status: entry.connected ? "connected" : "idle",
|
|
1056
|
+
status: entry.connected ? "connected" : entry.enabled === false ? "disabled" : "idle",
|
|
1057
|
+
enabled: entry.enabled,
|
|
1058
|
+
timeoutMs: entry.timeoutMs,
|
|
1059
|
+
// Never ship header values to the page; names are enough to edit them.
|
|
1060
|
+
headerNames: Object.keys(entry.headers),
|
|
1014
1061
|
lastError: entry.lastError,
|
|
1015
1062
|
toolCount: entry.toolCount,
|
|
1016
1063
|
tools: entry.catalog,
|
|
@@ -1051,6 +1098,7 @@ function installRoutes(ctx) {
|
|
|
1051
1098
|
ok: true,
|
|
1052
1099
|
protocolVersion: probe.protocolVersion ?? null,
|
|
1053
1100
|
transport: probe.transport,
|
|
1101
|
+
detail: probe.detail ?? null,
|
|
1054
1102
|
server: probe.serverInfo?.name ?? null,
|
|
1055
1103
|
version: probe.serverInfo?.version ?? null,
|
|
1056
1104
|
tools: tools.map((tool) => ({ name: tool.name, description: tool.description ?? "", folder: tool._meta?.["dsh/folder"] ?? null }))
|
|
@@ -1102,15 +1150,40 @@ function installRoutes(ctx) {
|
|
|
1102
1150
|
const name2 = String(item.name ?? "general");
|
|
1103
1151
|
if (removed.has(name2)) continue;
|
|
1104
1152
|
const previous = byName.get(name2) ?? {};
|
|
1105
|
-
const
|
|
1106
|
-
const
|
|
1107
|
-
|
|
1108
|
-
|
|
1153
|
+
const hasEndpoint = typeof item.endpoint === "string";
|
|
1154
|
+
const endpoint2 = hasEndpoint ? item.endpoint.trim() : previous.endpoint ?? "";
|
|
1155
|
+
const sameEndpoint = (previous.endpoint ?? "") === endpoint2;
|
|
1156
|
+
const next2 = { ...previous, ...item, name: name2, endpoint: endpoint2 };
|
|
1157
|
+
if (!sameEndpoint) delete next2.tools;
|
|
1158
|
+
if (item.headersPatch !== null && typeof item.headersPatch === "object" && !Array.isArray(item.headersPatch)) {
|
|
1159
|
+
const headers2 = { ...previous.headers ?? {} };
|
|
1160
|
+
for (const [key, value] of Object.entries(item.headersPatch)) {
|
|
1161
|
+
if (value === null) delete headers2[key];
|
|
1162
|
+
else if (typeof value === "string") headers2[key] = value;
|
|
1163
|
+
}
|
|
1164
|
+
next2.headers = headers2;
|
|
1165
|
+
}
|
|
1166
|
+
delete next2.headersPatch;
|
|
1167
|
+
byName.set(name2, next2);
|
|
1109
1168
|
}
|
|
1110
1169
|
for (const name2 of removed) byName.delete(name2);
|
|
1111
1170
|
const servers = [...byName.values()];
|
|
1112
1171
|
const { serverName, endpoint, headers, tools, ...rest } = current;
|
|
1113
|
-
|
|
1172
|
+
const next = { ...rest, servers };
|
|
1173
|
+
const knobs = [
|
|
1174
|
+
["toolCallTimeoutMs", 1e3, 6e5],
|
|
1175
|
+
["retryIntervalMs", 5e3, 6e5],
|
|
1176
|
+
["pollIntervalMs", 2e3, 6e5],
|
|
1177
|
+
["maxRetries", 0, 1e3]
|
|
1178
|
+
];
|
|
1179
|
+
for (const [key, min, max] of knobs) {
|
|
1180
|
+
const value = body[key];
|
|
1181
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
1182
|
+
next[key] = Math.min(Math.max(Math.round(value), min), max);
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
if (typeof body.debug === "boolean") next.debug = body.debug;
|
|
1186
|
+
return next;
|
|
1114
1187
|
});
|
|
1115
1188
|
beacon("config-saved servers=" + incoming.length + " removed=" + removed.size);
|
|
1116
1189
|
send(res, 200, { ok: true });
|
|
@@ -1215,7 +1288,7 @@ function registerStatusTool(ctx) {
|
|
|
1215
1288
|
additionalProperties: false
|
|
1216
1289
|
},
|
|
1217
1290
|
render(_args, value) {
|
|
1218
|
-
const lines = (value.servers ?? []).map((item) => item.name + ": " + (item.connected ? "connected" : "idle") + " tools=" + item.toolCount + (item.lastError === "" ? "" : " error=" + item.lastError));
|
|
1291
|
+
const lines = (value.servers ?? []).map((item) => item.name + ": " + (item.connected ? "connected" : item.enabled === false ? "disabled" : "idle") + " tools=" + item.toolCount + (item.timeoutMs > 0 ? " timeout=" + item.timeoutMs + "ms" : "") + (item.lastError === "" ? "" : " error=" + item.lastError));
|
|
1219
1292
|
return [{ type: "text", text: "general-mcp" + (lines.length === 0 ? " (no gateways)" : "\n" + lines.join("\n")) }];
|
|
1220
1293
|
}
|
|
1221
1294
|
},
|
|
@@ -1224,6 +1297,8 @@ function registerStatusTool(ctx) {
|
|
|
1224
1297
|
name: entry.name,
|
|
1225
1298
|
endpoint: entry.endpoint,
|
|
1226
1299
|
connected: entry.connected,
|
|
1300
|
+
enabled: entry.enabled,
|
|
1301
|
+
timeoutMs: entry.timeoutMs,
|
|
1227
1302
|
toolCount: entry.toolCount,
|
|
1228
1303
|
lastError: entry.lastError,
|
|
1229
1304
|
disabled: entry.settings.disabled ?? [],
|
package/package.json
CHANGED
package/src/connect.ts
CHANGED
|
@@ -4,6 +4,8 @@ import { SseClient } from './sse.ts'
|
|
|
4
4
|
|
|
5
5
|
export interface McpConnection {
|
|
6
6
|
transport: 'streamable' | 'sse'
|
|
7
|
+
/** Human-readable transport detail, e.g. the SSE message endpoint. */
|
|
8
|
+
detail?: string
|
|
7
9
|
protocolVersion: string | undefined
|
|
8
10
|
serverInfo: unknown
|
|
9
11
|
initialize(): Promise<void>
|
|
@@ -21,6 +23,7 @@ async function viaStreamable(url: string, headers: Record<string, string>, timeo
|
|
|
21
23
|
const info: any = await client.initialize()
|
|
22
24
|
return {
|
|
23
25
|
transport: 'streamable',
|
|
26
|
+
detail: 'POST ' + url,
|
|
24
27
|
protocolVersion: info?.protocolVersion,
|
|
25
28
|
serverInfo: info?.serverInfo,
|
|
26
29
|
async initialize() {},
|
|
@@ -35,6 +38,7 @@ async function viaSse(url: string, headers: Record<string, string>, timeoutMs: n
|
|
|
35
38
|
const info: any = await client.initialize()
|
|
36
39
|
return {
|
|
37
40
|
transport: 'sse',
|
|
41
|
+
detail: 'GET ' + url + ' → POST ' + (client.messageEndpoint ?? '(pending)'),
|
|
38
42
|
protocolVersion: info?.protocolVersion,
|
|
39
43
|
serverInfo: info?.serverInfo,
|
|
40
44
|
async initialize() {},
|
package/src/index.ts
CHANGED
|
@@ -23,6 +23,10 @@ export interface ServerConfig {
|
|
|
23
23
|
endpoint?: string
|
|
24
24
|
headers?: Record<string, string>
|
|
25
25
|
tools?: ToolSettings
|
|
26
|
+
/** false keeps the gateway configured but disconnected. */
|
|
27
|
+
enabled?: boolean
|
|
28
|
+
/** Per-gateway override of the global request timeout. */
|
|
29
|
+
timeoutMs?: number
|
|
26
30
|
}
|
|
27
31
|
|
|
28
32
|
export interface Config {
|
|
@@ -31,6 +35,11 @@ export interface Config {
|
|
|
31
35
|
endpoint?: string
|
|
32
36
|
headers?: Record<string, string>
|
|
33
37
|
toolCallTimeoutMs?: number
|
|
38
|
+
/** Reconnect cadence for a gateway that failed to connect. */
|
|
39
|
+
retryIntervalMs?: number
|
|
40
|
+
maxRetries?: number
|
|
41
|
+
/** How often the settings page re-reads the catalogue. */
|
|
42
|
+
pollIntervalMs?: number
|
|
34
43
|
debug?: boolean
|
|
35
44
|
tools?: ToolSettings
|
|
36
45
|
}
|
|
@@ -52,6 +61,8 @@ interface ServerState {
|
|
|
52
61
|
disposed?: boolean
|
|
53
62
|
generation?: number
|
|
54
63
|
client?: McpConnection
|
|
64
|
+
enabled: boolean
|
|
65
|
+
timeoutMs: number
|
|
55
66
|
}
|
|
56
67
|
|
|
57
68
|
interface RuntimeState {
|
|
@@ -60,6 +71,9 @@ interface RuntimeState {
|
|
|
60
71
|
version: number
|
|
61
72
|
debug: boolean
|
|
62
73
|
timeoutMs: number
|
|
74
|
+
retryIntervalMs: number
|
|
75
|
+
maxRetries: number
|
|
76
|
+
pollIntervalMs: number
|
|
63
77
|
}
|
|
64
78
|
|
|
65
79
|
const NAME_PATTERN = /^[A-Za-z0-9_-]{1,32}$/
|
|
@@ -76,6 +90,9 @@ function state(): RuntimeState {
|
|
|
76
90
|
version: 0,
|
|
77
91
|
debug: false,
|
|
78
92
|
timeoutMs: 60_000,
|
|
93
|
+
retryIntervalMs: 30_000,
|
|
94
|
+
maxRetries: 20,
|
|
95
|
+
pollIntervalMs: 15_000,
|
|
79
96
|
}
|
|
80
97
|
}
|
|
81
98
|
return shared.__GENMCP_STATE__
|
|
@@ -96,6 +113,8 @@ function serverState(name: string): ServerState {
|
|
|
96
113
|
tools: new Map(),
|
|
97
114
|
settings: {},
|
|
98
115
|
attempts: 0,
|
|
116
|
+
enabled: true,
|
|
117
|
+
timeoutMs: 0,
|
|
99
118
|
}
|
|
100
119
|
live.servers.set(name, entry)
|
|
101
120
|
}
|
|
@@ -129,10 +148,14 @@ function normalizeSettings(raw: any): ToolSettings {
|
|
|
129
148
|
function normalizeConfig(config: Config | undefined): ServerConfig[] {
|
|
130
149
|
const raw = config ?? {}
|
|
131
150
|
const live = state()
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
151
|
+
const clamp = (value: unknown, fallback: number, min: number, max: number): number =>
|
|
152
|
+
typeof value === 'number' && Number.isFinite(value)
|
|
153
|
+
? Math.min(Math.max(Math.round(value), min), max)
|
|
154
|
+
: fallback
|
|
155
|
+
live.timeoutMs = clamp(raw.toolCallTimeoutMs, 60_000, 1_000, 600_000)
|
|
156
|
+
live.retryIntervalMs = clamp(raw.retryIntervalMs, 30_000, 5_000, 600_000)
|
|
157
|
+
live.maxRetries = clamp(raw.maxRetries, 20, 0, 1_000)
|
|
158
|
+
live.pollIntervalMs = clamp(raw.pollIntervalMs, 15_000, 2_000, 600_000)
|
|
136
159
|
live.debug = raw.debug === true
|
|
137
160
|
|
|
138
161
|
// An explicit empty array means "no gateways"; only a missing key falls back
|
|
@@ -186,10 +209,18 @@ export function apply(ctx: any, config?: Config): void {
|
|
|
186
209
|
if (typeof value === 'string') headers[key] = value
|
|
187
210
|
}
|
|
188
211
|
}
|
|
212
|
+
const enabled = item.enabled !== false
|
|
213
|
+
const serverTimeout = typeof item.timeoutMs === 'number' && Number.isFinite(item.timeoutMs)
|
|
214
|
+
? Math.min(Math.max(Math.round(item.timeoutMs), 1_000), 600_000)
|
|
215
|
+
: 0
|
|
189
216
|
const changed = entry.endpoint !== (item.endpoint ?? '') ||
|
|
190
|
-
JSON.stringify(entry.headers) !== JSON.stringify(headers)
|
|
217
|
+
JSON.stringify(entry.headers) !== JSON.stringify(headers) ||
|
|
218
|
+
entry.enabled !== enabled ||
|
|
219
|
+
entry.timeoutMs !== serverTimeout
|
|
191
220
|
entry.endpoint = item.endpoint ?? ''
|
|
192
221
|
entry.headers = headers
|
|
222
|
+
entry.enabled = enabled
|
|
223
|
+
entry.timeoutMs = serverTimeout
|
|
193
224
|
const nextSettings = normalizeSettings(item.tools)
|
|
194
225
|
// Display settings live outside the connection; a sync already in flight
|
|
195
226
|
// read the old ones, so ask for one more pass once it settles.
|
|
@@ -220,12 +251,13 @@ export function apply(ctx: any, config?: Config): void {
|
|
|
220
251
|
entry.attempts = 0
|
|
221
252
|
live.version += 1
|
|
222
253
|
}
|
|
223
|
-
if (entry.endpoint === '') {
|
|
254
|
+
if (entry.endpoint === '' || entry.enabled === false) {
|
|
224
255
|
disposeTools(entry)
|
|
256
|
+
stopRetry(entry)
|
|
225
257
|
entry.connected = false
|
|
226
258
|
entry.toolCount = 0
|
|
227
259
|
entry.catalog = []
|
|
228
|
-
entry.lastError = ''
|
|
260
|
+
entry.lastError = entry.enabled === false ? '' : entry.lastError
|
|
229
261
|
continue
|
|
230
262
|
}
|
|
231
263
|
void sync(entry).then(ok => {
|
|
@@ -272,11 +304,15 @@ function createRuntime(ctx: any) {
|
|
|
272
304
|
|
|
273
305
|
const sync = (entry: ServerState): Promise<boolean> => {
|
|
274
306
|
if (entry.sync !== undefined) return entry.sync
|
|
307
|
+
// Must be checked before the promise is stored: an early return would run
|
|
308
|
+
// the finally block before "entry.sync = run", leaving it permanently set.
|
|
309
|
+
if (entry.endpoint === '' || entry.enabled === false) return Promise.resolve(false)
|
|
275
310
|
const run = (async () => {
|
|
276
311
|
const generation = entry.generation ?? 0
|
|
277
312
|
try {
|
|
278
|
-
if (entry.endpoint === '') return false
|
|
279
|
-
const
|
|
313
|
+
if (entry.endpoint === '' || entry.enabled === false) return false
|
|
314
|
+
const timeout = entry.timeoutMs > 0 ? entry.timeoutMs : live.timeoutMs
|
|
315
|
+
const client = await connectMcp(entry.endpoint, entry.headers, timeout)
|
|
280
316
|
if (!alive(entry) || (entry.generation ?? 0) !== generation) {
|
|
281
317
|
await client.close().catch(() => {})
|
|
282
318
|
return false
|
|
@@ -339,9 +375,9 @@ function createRuntime(ctx: any) {
|
|
|
339
375
|
entry.retryTimer = setInterval(() => {
|
|
340
376
|
entry.attempts += 1
|
|
341
377
|
void sync(entry).then(ok => {
|
|
342
|
-
if (ok || entry.attempts >=
|
|
378
|
+
if (ok || entry.attempts >= live.maxRetries) stopRetry(entry)
|
|
343
379
|
})
|
|
344
|
-
},
|
|
380
|
+
}, live.retryIntervalMs)
|
|
345
381
|
}
|
|
346
382
|
|
|
347
383
|
return { disposeTools, registerTools, sync, scheduleRetry, stopRetry }
|
|
@@ -442,10 +478,19 @@ function installRoutes(ctx: any): void {
|
|
|
442
478
|
if (since === live.version) return new Response(null, { status: 304, headers: { ...allowed, 'cache-control': 'no-store' } })
|
|
443
479
|
return new Response(JSON.stringify({
|
|
444
480
|
version: live.version,
|
|
481
|
+
timeoutMs: live.timeoutMs,
|
|
482
|
+
retryIntervalMs: live.retryIntervalMs,
|
|
483
|
+
maxRetries: live.maxRetries,
|
|
484
|
+
pollIntervalMs: live.pollIntervalMs,
|
|
485
|
+
debug: live.debug,
|
|
445
486
|
servers: [...live.servers.values()].map(entry => ({
|
|
446
487
|
name: entry.name,
|
|
447
488
|
endpoint: entry.endpoint,
|
|
448
|
-
status: entry.connected ? 'connected' : 'idle',
|
|
489
|
+
status: entry.connected ? 'connected' : entry.enabled === false ? 'disabled' : 'idle',
|
|
490
|
+
enabled: entry.enabled,
|
|
491
|
+
timeoutMs: entry.timeoutMs,
|
|
492
|
+
// Never ship header values to the page; names are enough to edit them.
|
|
493
|
+
headerNames: Object.keys(entry.headers),
|
|
449
494
|
lastError: entry.lastError,
|
|
450
495
|
toolCount: entry.toolCount,
|
|
451
496
|
tools: entry.catalog,
|
|
@@ -480,6 +525,7 @@ function installRoutes(ctx: any): void {
|
|
|
480
525
|
ok: true,
|
|
481
526
|
protocolVersion: probe.protocolVersion ?? null,
|
|
482
527
|
transport: probe.transport,
|
|
528
|
+
detail: probe.detail ?? null,
|
|
483
529
|
server: (probe.serverInfo as any)?.name ?? null,
|
|
484
530
|
version: (probe.serverInfo as any)?.version ?? null,
|
|
485
531
|
tools: tools.map(tool => ({ name: tool.name, description: tool.description ?? '', folder: (tool._meta as any)?.['dsh/folder'] ?? null })),
|
|
@@ -532,15 +578,43 @@ function installRoutes(ctx: any): void {
|
|
|
532
578
|
const name = String(item.name ?? 'general')
|
|
533
579
|
if (removed.has(name)) continue
|
|
534
580
|
const previous = byName.get(name) ?? {}
|
|
535
|
-
|
|
536
|
-
|
|
581
|
+
// A partial payload (only enabled/timeout/headers) must keep the
|
|
582
|
+
// stored endpoint and tool settings.
|
|
583
|
+
const hasEndpoint = typeof item.endpoint === 'string'
|
|
584
|
+
const endpoint = hasEndpoint ? item.endpoint.trim() : (previous.endpoint ?? '')
|
|
585
|
+
const sameEndpoint = (previous.endpoint ?? '') === endpoint
|
|
586
|
+
const next: any = { ...previous, ...item, name, endpoint }
|
|
537
587
|
if (!sameEndpoint) delete next.tools
|
|
588
|
+
if (item.headersPatch !== null && typeof item.headersPatch === 'object' && !Array.isArray(item.headersPatch)) {
|
|
589
|
+
const headers: Record<string, string> = { ...(previous.headers ?? {}) }
|
|
590
|
+
for (const [key, value] of Object.entries(item.headersPatch as Record<string, unknown>)) {
|
|
591
|
+
if (value === null) delete headers[key]
|
|
592
|
+
else if (typeof value === 'string') headers[key] = value
|
|
593
|
+
}
|
|
594
|
+
next.headers = headers
|
|
595
|
+
}
|
|
596
|
+
delete next.headersPatch
|
|
538
597
|
byName.set(name, next)
|
|
539
598
|
}
|
|
540
599
|
for (const name of removed) byName.delete(name)
|
|
541
600
|
const servers = [...byName.values()]
|
|
542
601
|
const { serverName, endpoint, headers, tools, ...rest } = current
|
|
543
|
-
|
|
602
|
+
const next: any = { ...rest, servers }
|
|
603
|
+
// Display/behaviour knobs live next to the gateway list.
|
|
604
|
+
const knobs: Array<[string, number, number]> = [
|
|
605
|
+
['toolCallTimeoutMs', 1_000, 600_000],
|
|
606
|
+
['retryIntervalMs', 5_000, 600_000],
|
|
607
|
+
['pollIntervalMs', 2_000, 600_000],
|
|
608
|
+
['maxRetries', 0, 1_000],
|
|
609
|
+
]
|
|
610
|
+
for (const [key, min, max] of knobs) {
|
|
611
|
+
const value = body[key]
|
|
612
|
+
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
613
|
+
next[key] = Math.min(Math.max(Math.round(value), min), max)
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
if (typeof body.debug === 'boolean') next.debug = body.debug
|
|
617
|
+
return next
|
|
544
618
|
})
|
|
545
619
|
beacon('config-saved servers=' + (incoming as any[]).length + ' removed=' + removed.size)
|
|
546
620
|
send(res, 200, { ok: true })
|
|
@@ -647,7 +721,8 @@ function registerStatusTool(ctx: any): void {
|
|
|
647
721
|
},
|
|
648
722
|
render(_args: unknown, value: any) {
|
|
649
723
|
const lines = (value.servers ?? []).map((item: any) =>
|
|
650
|
-
item.name + ': ' + (item.connected ? 'connected' : '
|
|
724
|
+
item.name + ': ' + (item.connected ? 'connected' : item.enabled === false ? 'disabled' : 'idle') +
|
|
725
|
+
' tools=' + item.toolCount + (item.timeoutMs > 0 ? ' timeout=' + item.timeoutMs + 'ms' : '') +
|
|
651
726
|
(item.lastError === '' ? '' : ' error=' + item.lastError))
|
|
652
727
|
return [{ type: 'text', text: 'general-mcp' + (lines.length === 0 ? ' (no gateways)' : '\n' + lines.join('\n')) }]
|
|
653
728
|
},
|
|
@@ -657,6 +732,8 @@ function registerStatusTool(ctx: any): void {
|
|
|
657
732
|
name: entry.name,
|
|
658
733
|
endpoint: entry.endpoint,
|
|
659
734
|
connected: entry.connected,
|
|
735
|
+
enabled: entry.enabled,
|
|
736
|
+
timeoutMs: entry.timeoutMs,
|
|
660
737
|
toolCount: entry.toolCount,
|
|
661
738
|
lastError: entry.lastError,
|
|
662
739
|
disabled: entry.settings.disabled ?? [],
|
package/src/sse.ts
CHANGED
|
@@ -27,6 +27,38 @@ interface Pending {
|
|
|
27
27
|
timer: any
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
/** Hosts that only make sense on the server itself. */
|
|
31
|
+
function isLocalHost(hostname: string): boolean {
|
|
32
|
+
const host = hostname.toLowerCase()
|
|
33
|
+
return host === 'localhost' || host === '127.0.0.1' || host === '::1' || host === '[::1]' ||
|
|
34
|
+
host === '0.0.0.0' || /^10\./.test(host) || /^192\.168\./.test(host) ||
|
|
35
|
+
/^172\.(1[6-9]|2\d|3[01])\./.test(host)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Spring AI builds the message endpoint from the server's own request URL, so
|
|
40
|
+
* behind a NAT/port-forward it often announces http://localhost:PORT/... That
|
|
41
|
+
* address is unreachable from the client; keep the path and take the origin
|
|
42
|
+
* from the SSE URL we actually connected to.
|
|
43
|
+
*/
|
|
44
|
+
export function resolveMessageUrl(endpoint: string, sseUrl: string): string {
|
|
45
|
+
let target: URL
|
|
46
|
+
let sse: URL
|
|
47
|
+
try {
|
|
48
|
+
sse = new URL(sseUrl)
|
|
49
|
+
target = new URL(endpoint, sseUrl)
|
|
50
|
+
} catch {
|
|
51
|
+
return endpoint
|
|
52
|
+
}
|
|
53
|
+
if (target.origin !== sse.origin && isLocalHost(target.hostname)) {
|
|
54
|
+
target.protocol = sse.protocol
|
|
55
|
+
target.hostname = sse.hostname
|
|
56
|
+
target.port = sse.port
|
|
57
|
+
return target.toString()
|
|
58
|
+
}
|
|
59
|
+
return target.toString()
|
|
60
|
+
}
|
|
61
|
+
|
|
30
62
|
export class SseClient {
|
|
31
63
|
private messageUrl: string | undefined
|
|
32
64
|
private pending = new Map<number, Pending>()
|
|
@@ -46,6 +78,11 @@ export class SseClient {
|
|
|
46
78
|
return '2024-11-05'
|
|
47
79
|
}
|
|
48
80
|
|
|
81
|
+
/** Where requests are POSTed; useful in diagnostics. */
|
|
82
|
+
get messageEndpoint(): string | undefined {
|
|
83
|
+
return this.messageUrl
|
|
84
|
+
}
|
|
85
|
+
|
|
49
86
|
/** Open the stream and wait for the endpoint announcement. */
|
|
50
87
|
connect(): Promise<void> {
|
|
51
88
|
if (this.connected !== undefined) return this.connected
|
|
@@ -56,7 +93,7 @@ export class SseClient {
|
|
|
56
93
|
if (response.body === null) throw new Error('SSE connect returned no body')
|
|
57
94
|
this.reader = response.body.getReader()
|
|
58
95
|
const announced = new Promise<void>((resolve, reject) => {
|
|
59
|
-
const timer = setTimeout(() => reject(new Error('SSE endpoint event timed out')), this.timeoutMs)
|
|
96
|
+
const timer = setTimeout(() => reject(new Error('SSE endpoint event timed out after ' + this.timeoutMs + 'ms (GET ' + this.url + ')')), this.timeoutMs)
|
|
60
97
|
this.endpointWaiter = { resolve, reject, timer }
|
|
61
98
|
})
|
|
62
99
|
void this.pump()
|
|
@@ -99,7 +136,7 @@ export class SseClient {
|
|
|
99
136
|
}
|
|
100
137
|
const payload = data.join('\n')
|
|
101
138
|
if (event === 'endpoint') {
|
|
102
|
-
|
|
139
|
+
this.messageUrl = resolveMessageUrl(payload, this.url)
|
|
103
140
|
const waiter = this.endpointWaiter
|
|
104
141
|
this.endpointWaiter = undefined
|
|
105
142
|
if (waiter !== undefined) { clearTimeout(waiter.timer); waiter.resolve(undefined) }
|
|
@@ -145,7 +182,7 @@ export class SseClient {
|
|
|
145
182
|
const reply = new Promise<any>((resolve, reject) => {
|
|
146
183
|
const timer = setTimeout(() => {
|
|
147
184
|
this.pending.delete(id)
|
|
148
|
-
reject(new Error('MCP ' + method + ' timed out after ' + this.timeoutMs + 'ms'))
|
|
185
|
+
reject(new Error('MCP ' + method + ' timed out after ' + this.timeoutMs + 'ms (POST ' + this.messageUrl + ')'))
|
|
149
186
|
}, this.timeoutMs)
|
|
150
187
|
this.pending.set(id, { resolve, reject, timer })
|
|
151
188
|
})
|