dsh-mcp-status 0.2.0 → 0.3.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/README.md +48 -20
- package/client.js +298 -31
- package/index.js +123 -8
- package/lib/status.js +151 -9
- package/package.json +4 -2
- package/test/status.test.mjs +277 -1
package/index.js
CHANGED
|
@@ -32,6 +32,7 @@ import {
|
|
|
32
32
|
resolveStatus,
|
|
33
33
|
sectionOrder,
|
|
34
34
|
serverNameFromTool,
|
|
35
|
+
summarizeServers,
|
|
35
36
|
} from './lib/status.js';
|
|
36
37
|
import { probeServer } from './lib/probe.js';
|
|
37
38
|
|
|
@@ -98,6 +99,26 @@ function findRowBlock(lines, id) {
|
|
|
98
99
|
return undefined;
|
|
99
100
|
}
|
|
100
101
|
|
|
102
|
+
/**
|
|
103
|
+
* 补丁文件里这一行的 disabled 状态。
|
|
104
|
+
* @returns true / false;返回 undefined 表示这些补丁文件里都没有这一行。
|
|
105
|
+
*/
|
|
106
|
+
async function patchRowDisabled(ctx, id) {
|
|
107
|
+
for (const file of candidatePatchFiles(ctx)) {
|
|
108
|
+
let text;
|
|
109
|
+
try {
|
|
110
|
+
text = await readFile(file.path, 'utf8');
|
|
111
|
+
} catch {
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
const lines = text.split(/\r?\n/);
|
|
115
|
+
const block = findRowBlock(lines, id);
|
|
116
|
+
if (block === undefined) continue;
|
|
117
|
+
return block.disabledIndex !== undefined;
|
|
118
|
+
}
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
|
|
101
122
|
/** 原子写:同目录临时文件 + rename,保留原文件权限位。 */
|
|
102
123
|
async function writeAtomic(file, content) {
|
|
103
124
|
let mode;
|
|
@@ -297,9 +318,11 @@ async function collect(ctx, settings, { withSources = true } = {}) {
|
|
|
297
318
|
const summary = summarize(config);
|
|
298
319
|
const reach = reachable[index];
|
|
299
320
|
const probe = storedProbe(entry.options.id);
|
|
321
|
+
const connection = connectionOf(entry);
|
|
300
322
|
const status = resolveStatus({
|
|
301
323
|
disabled: Boolean(entry.disabled),
|
|
302
324
|
fiberState: entry.fiber?.state ?? null,
|
|
325
|
+
connection,
|
|
303
326
|
toolCount: count === undefined ? null : count,
|
|
304
327
|
reachable: reach === null ? null : reach.ok,
|
|
305
328
|
reachableAt: reach === null ? null : reach.at,
|
|
@@ -326,6 +349,7 @@ async function collect(ctx, settings, { withSources = true } = {}) {
|
|
|
326
349
|
evidence: status.evidence,
|
|
327
350
|
disabled: Boolean(entry.disabled),
|
|
328
351
|
fiberState: entry.fiber?.state ?? null,
|
|
352
|
+
connection: connection ?? null,
|
|
329
353
|
toolCount: count === undefined ? null : count,
|
|
330
354
|
reachable: reach === null ? null : reach.ok,
|
|
331
355
|
reachDetail: reach === null ? null : reach.detail,
|
|
@@ -366,6 +390,35 @@ function errorOf(entry) {
|
|
|
366
390
|
return message === '' ? 'MCP 客户端加载失败' : message;
|
|
367
391
|
}
|
|
368
392
|
|
|
393
|
+
/**
|
|
394
|
+
* 读宿主 `dsh-mcp-client` 自己的连接状态——它是唯一权威的「到底连上没有」。
|
|
395
|
+
*
|
|
396
|
+
* `connectionStatusOf(fiber)` 返回一个 status cell,`current()` 给出
|
|
397
|
+
* connecting / connected / reconnecting(attempt,maxAttempts) / failed(reason) /
|
|
398
|
+
* closed / waiting-credentials。拿不到(旧宿主、条目还没 import)就返回 undefined,
|
|
399
|
+
* 由 resolveStatus 退回观测值推导。宿主状态存在时,我们自己的探测只是旁证。
|
|
400
|
+
*/
|
|
401
|
+
function connectionOf(entry) {
|
|
402
|
+
const read = entry?.moduleNamespace?.connectionStatusOf;
|
|
403
|
+
if (typeof read !== 'function' || entry.fiber === undefined) return undefined;
|
|
404
|
+
try {
|
|
405
|
+
const cell = read(entry.fiber);
|
|
406
|
+
const status = typeof cell?.current === 'function' ? cell.current() : undefined;
|
|
407
|
+
if (status === undefined || status === null || typeof status.state !== 'string') return undefined;
|
|
408
|
+
return {
|
|
409
|
+
state: status.state,
|
|
410
|
+
reason: status.reason ?? null,
|
|
411
|
+
attempt: status.attempt ?? null,
|
|
412
|
+
maxAttempts: status.maxAttempts ?? null,
|
|
413
|
+
lastError: typeof status.lastError === 'string'
|
|
414
|
+
? status.lastError
|
|
415
|
+
: status.lastError?.message ?? null,
|
|
416
|
+
};
|
|
417
|
+
} catch {
|
|
418
|
+
return undefined;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
|
|
369
422
|
/**
|
|
370
423
|
* 状态缓存:后台按 refreshMs 刷新,给 system prompt 与 guard 用;
|
|
371
424
|
* 面板请求 `refresh(true)` 时当场重采,保持它原有的新鲜度。
|
|
@@ -423,7 +476,34 @@ async function waitForState(ctx, config, tracker, id, accept, timeoutMs) {
|
|
|
423
476
|
return last;
|
|
424
477
|
}
|
|
425
478
|
|
|
426
|
-
/**
|
|
479
|
+
/**
|
|
480
|
+
* 重连 = 真的把这一行重新挂载:dispose 旧 fiber,再走一次冷启动(`init`)。
|
|
481
|
+
*
|
|
482
|
+
* HMR 的 `reconcileProfile` 只在「源文件真的变了」时才重挂条目,配置没变时什么都不做
|
|
483
|
+
* ——所以不能拿它当重试(只会白等 startupWaitMs);而 `dsh-mcp-client` 放弃重连后自己的
|
|
484
|
+
* 提示就是「reload the plugin or restart the Host」,这里做的正是前者,且只影响这一行。
|
|
485
|
+
*/
|
|
486
|
+
async function restartEntry(ctx, id) {
|
|
487
|
+
const loader = ctx.get('loader');
|
|
488
|
+
const entry = loader === undefined || typeof loader.entries !== 'function'
|
|
489
|
+
? undefined
|
|
490
|
+
: [...loader.entries()].find((item) => item?.options?.id === id);
|
|
491
|
+
if (entry === undefined) throw new Error(`未找到 MCP 服务器条目:${id}`);
|
|
492
|
+
// 用户自己在补丁文件里停用的行,重试不该把它强行拉起来。
|
|
493
|
+
if (await patchRowDisabled(ctx, id)) {
|
|
494
|
+
throw new Error('这一行在补丁文件里是停用状态:先打开开关再重试');
|
|
495
|
+
}
|
|
496
|
+
// 宿主没有可复用的 init/dispose 契约时返回 false,让调用方退回 HMR 重排。
|
|
497
|
+
if (typeof entry.init !== 'function' || entry.fiber === undefined) return false;
|
|
498
|
+
await entry.fiber.dispose();
|
|
499
|
+
// 加载器会把「不是它自己卸载的」条目标记成 disabled,避免立刻自动重挂;
|
|
500
|
+
// 这里要的恰恰是重挂,所以先把标记清掉,否则条目会一直显示为已停用。
|
|
501
|
+
if (entry.options !== undefined) entry.options.disabled = false;
|
|
502
|
+
await entry.init();
|
|
503
|
+
return true;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** 兜底:补丁监听没接上时用它重排一次(只在源文件变化时才真的重挂)。 */
|
|
427
507
|
async function forceReload(ctx, id) {
|
|
428
508
|
const hmr = ctx.get('hmr');
|
|
429
509
|
if (hmr === undefined || typeof hmr.reconcileProfile !== 'function') return false;
|
|
@@ -477,20 +557,32 @@ async function reconnect(ctx, config, tracker, id) {
|
|
|
477
557
|
const before = await tracker.refresh(true);
|
|
478
558
|
const row = before.servers.find((server) => server.id === id);
|
|
479
559
|
if (row === undefined) throw new Error(`未找到 MCP 服务器条目:${id}`);
|
|
560
|
+
// 重新挂载就是一次新连接:旧的测试结论不再适用。
|
|
480
561
|
probeResults.delete(id);
|
|
481
562
|
reachCache.clear();
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
let
|
|
485
|
-
|
|
563
|
+
|
|
564
|
+
const accept = (server) => server.state === 'connected';
|
|
565
|
+
let attempts = 0;
|
|
566
|
+
let current;
|
|
567
|
+
let restarted = false;
|
|
568
|
+
while (attempts <= RETRY_ROUNDS) {
|
|
486
569
|
attempts += 1;
|
|
487
|
-
|
|
488
|
-
|
|
570
|
+
// restartEntry 只在「真的没法重挂」时返回 false;它抛错是用户该看到的原因
|
|
571
|
+
// (行在补丁文件里被停用、条目找不到、dispose/init 失败),直接冒到路由。
|
|
572
|
+
restarted = await restartEntry(ctx, id);
|
|
573
|
+
if (!restarted) restarted = await forceReload(ctx, id);
|
|
574
|
+
if (!restarted) break;
|
|
575
|
+
// 重挂本身很快(握手几百毫秒),先用短窗口等;同一次请求里第二轮才等满 startupWaitMs。
|
|
576
|
+
const window = attempts === 1 ? Math.min(3000, config.startupWaitMs) : config.startupWaitMs;
|
|
577
|
+
current = await waitForState(ctx, config, tracker, id, accept, window);
|
|
578
|
+
if (current !== undefined && accept(current)) break;
|
|
489
579
|
}
|
|
580
|
+
|
|
490
581
|
return {
|
|
491
582
|
state: current?.state ?? 'unknown',
|
|
492
|
-
settled: current
|
|
583
|
+
settled: current !== undefined && accept(current),
|
|
493
584
|
attempts,
|
|
585
|
+
restarted,
|
|
494
586
|
server: current ?? null,
|
|
495
587
|
};
|
|
496
588
|
}
|
|
@@ -678,6 +770,29 @@ export function apply(ctx, rawConfig) {
|
|
|
678
770
|
}
|
|
679
771
|
},
|
|
680
772
|
}),
|
|
773
|
+
// 侧栏角标用的轻量摘要:读缓存、不强制重采、不探活。
|
|
774
|
+
//
|
|
775
|
+
// 角标每 5 s 问一次,而 /servers 每次都要当场重采(探活 + 工具表扫描)。让角标改用这条
|
|
776
|
+
// 路由,后台 refreshMs 循环负责续期,用户没打开面板时不会因为一个 6 px 的点而打风暴。
|
|
777
|
+
ctx.webServer.register({
|
|
778
|
+
kind: 'exact',
|
|
779
|
+
path: `${ROUTE_BASE}/summary`,
|
|
780
|
+
handler: async (request, response) => {
|
|
781
|
+
const rejection = gate(request, { method: 'GET', write: false });
|
|
782
|
+
if (rejection !== undefined) return sendJson(response, rejection.status, { ok: false, error: rejection.error });
|
|
783
|
+
try {
|
|
784
|
+
// 非强制:新鲜就返回缓存,过期才采一次(仍然受 refreshMs 节流)。
|
|
785
|
+
const snapshot = await tracker.refresh(false);
|
|
786
|
+
return sendJson(response, 200, {
|
|
787
|
+
ok: true,
|
|
788
|
+
...summarizeServers(snapshot.servers),
|
|
789
|
+
checkedAt: snapshot.checkedAt,
|
|
790
|
+
});
|
|
791
|
+
} catch (error) {
|
|
792
|
+
return sendJson(response, 500, { ok: false, error: error?.message ?? String(error) });
|
|
793
|
+
}
|
|
794
|
+
},
|
|
795
|
+
}),
|
|
681
796
|
ctx.webServer.register({
|
|
682
797
|
kind: 'exact',
|
|
683
798
|
path: `${ROUTE_BASE}/toggle`,
|
package/lib/status.js
CHANGED
|
@@ -55,29 +55,45 @@ export function normalizeConfig(raw) {
|
|
|
55
55
|
return config;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
/** 把宿主的连接状态压成一条 evidence(`3/10` 这类计数放在 detail 里)。 */
|
|
59
|
+
function connectionEvidence(connection) {
|
|
60
|
+
const detail = connection.state === 'reconnecting'
|
|
61
|
+
? `${connection.attempt ?? '?'}/${connection.maxAttempts ?? '?'}`
|
|
62
|
+
: connection.state === 'failed' ? connection.reason ?? null : null;
|
|
63
|
+
return { signal: 'client', value: connection.state, at: null, detail };
|
|
64
|
+
}
|
|
65
|
+
|
|
58
66
|
/**
|
|
59
67
|
* 判定一行 MCP 服务器的状态。
|
|
60
68
|
*
|
|
61
|
-
*
|
|
69
|
+
* 拿得到宿主的连接状态(`connection`,来自 `dsh-mcp-client` 自己的状态机)时,它有权
|
|
70
|
+
* 决定「连着 / 正在重连 / 已放弃 / 已关闭」——我们的探测只是旁证,**绝不能**把宿主已经
|
|
71
|
+
* 放弃的连接说成「已连接」(那正是假绿 bug 的形态)。拿不到时(旧宿主、刚启动)
|
|
72
|
+
* 退回纯观测值推导。
|
|
73
|
+
*
|
|
74
|
+
* 优先级:
|
|
62
75
|
* 1. 停用 → disabled(用户的意愿,不是故障)
|
|
63
|
-
* 2. fiber 失败 → failed
|
|
64
|
-
* 3.
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
76
|
+
* 2. fiber 失败 → failed;fiber 未就绪 → pending
|
|
77
|
+
* 3. 宿主 connecting → pending;reconnecting → reconnecting;
|
|
78
|
+
* failed(gave-up) → failed;closed / waiting-credentials → failed
|
|
79
|
+
* 4. 宿主 connected 再复核旁证:「测试连接」失败或端口拒绝 → unreachable;
|
|
80
|
+
* 0 工具 → idle;否则 connected
|
|
81
|
+
* 5. 无宿主状态(旧路径):「测试连接」失败且(端口不通 或 结论够新)→ unreachable;
|
|
82
|
+
* 端口拒绝 → unreachable;测试成功 → connected;工具表读不到 → connected;
|
|
83
|
+
* 有工具 → connected;没有工具 → idle
|
|
70
84
|
*
|
|
71
85
|
* `idle` 刻意与 `unreachable` 区分:工具数为 0 有可能只是这行挂在 per-agent 作用域
|
|
72
86
|
* (全局注册表看不到),把它当断线会误拦、误报。
|
|
73
87
|
*
|
|
74
88
|
* @param {object} input 观测值
|
|
89
|
+
* @param {object} [input.connection] 宿主连接状态 {state, reason, attempt, maxAttempts, lastError}
|
|
75
90
|
* @returns {{state: string, reason: string, evidence: Array<object>}}
|
|
76
91
|
*/
|
|
77
92
|
export function resolveStatus(input) {
|
|
78
93
|
const {
|
|
79
94
|
disabled = false,
|
|
80
95
|
fiberState = null,
|
|
96
|
+
connection, // { state, reason?, attempt?, maxAttempts? } | undefined
|
|
81
97
|
toolCount, // number | null | undefined(undefined/null = 读不到注册表)
|
|
82
98
|
reachable = null, // boolean | null(stdio 行恒为 null)
|
|
83
99
|
reachableAt = null,
|
|
@@ -86,6 +102,9 @@ export function resolveStatus(input) {
|
|
|
86
102
|
now = Date.now(),
|
|
87
103
|
probeAssertMs = DEFAULT_CONFIG.probeAssertMs,
|
|
88
104
|
} = input;
|
|
105
|
+
const hostState = connection !== undefined && connection !== null && typeof connection.state === 'string'
|
|
106
|
+
? connection.state
|
|
107
|
+
: undefined;
|
|
89
108
|
|
|
90
109
|
const evidence = [];
|
|
91
110
|
const fiberValue = fiberState === FIBER_ACTIVE
|
|
@@ -99,11 +118,45 @@ export function resolveStatus(input) {
|
|
|
99
118
|
if (probe !== undefined) {
|
|
100
119
|
evidence.push({ signal: 'probe', value: probe.ok ? 'ok' : 'failed', at: probe.at, detail: probe.error ?? null });
|
|
101
120
|
}
|
|
121
|
+
if (hostState !== undefined) evidence.push(connectionEvidence(connection));
|
|
102
122
|
|
|
103
123
|
if (disabled) return { state: 'disabled', reason: 'disabled', evidence };
|
|
104
124
|
if (fiberState === FIBER_FAILED) return { state: 'failed', reason: 'fiber-failed', evidence };
|
|
105
125
|
if (fiberState !== FIBER_ACTIVE) return { state: 'pending', reason: 'pending', evidence };
|
|
106
126
|
|
|
127
|
+
if (hostState !== undefined) {
|
|
128
|
+
switch (hostState) {
|
|
129
|
+
case 'connecting':
|
|
130
|
+
return { state: 'pending', reason: 'client-connecting', evidence };
|
|
131
|
+
case 'reconnecting':
|
|
132
|
+
return { state: 'reconnecting', reason: 'client-reconnecting', evidence };
|
|
133
|
+
case 'waiting-credentials':
|
|
134
|
+
return { state: 'failed', reason: 'waiting-credentials', evidence };
|
|
135
|
+
case 'closed':
|
|
136
|
+
return { state: 'failed', reason: 'client-closed', evidence };
|
|
137
|
+
case 'failed':
|
|
138
|
+
return {
|
|
139
|
+
state: 'failed',
|
|
140
|
+
reason: connection.reason === 'gave-up' ? 'gave-up' : 'client-failed',
|
|
141
|
+
evidence,
|
|
142
|
+
};
|
|
143
|
+
case 'connected':
|
|
144
|
+
break;
|
|
145
|
+
default:
|
|
146
|
+
// 未知取值:宿主比我们新,退回观测值推导,不硬判。
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
if (hostState === 'connected') {
|
|
150
|
+
// 宿主说连着,但「测试连接」失败 / 端口拒绝仍然算不可达:那是更具体的证据。
|
|
151
|
+
const probeDisagrees = probe !== undefined && probe.ok === false;
|
|
152
|
+
const asserted = probeDisagrees && (reachable !== true || now - (probe.at ?? 0) < probeAssertMs);
|
|
153
|
+
if (asserted) return { state: 'unreachable', reason: 'probe-failed', evidence };
|
|
154
|
+
if (reachable === false) return { state: 'unreachable', reason: 'tcp-refused', evidence };
|
|
155
|
+
if (toolCount === 0) return { state: 'idle', reason: 'no-tools', evidence };
|
|
156
|
+
return { state: 'connected', reason: 'connected', evidence };
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
107
160
|
const probeFailed = probe !== undefined && probe.ok === false;
|
|
108
161
|
const probeAsserted = probeFailed && (reachable !== true || now - (probe.at ?? 0) < probeAssertMs);
|
|
109
162
|
if (probeAsserted) return { state: 'unreachable', reason: 'probe-failed', evidence };
|
|
@@ -141,10 +194,18 @@ export function decideGuard({ toolName, row, snapshotAt, now = Date.now(), confi
|
|
|
141
194
|
if (config.guardUnreachable === false) return undefined;
|
|
142
195
|
if (row === undefined || row === null) return undefined;
|
|
143
196
|
// 只拦「有证据的不可用」:pending/connecting 只是还没起来,可能下一秒就连上。
|
|
144
|
-
|
|
197
|
+
const guarded = row.state === 'unreachable' || row.state === 'failed' || row.state === 'reconnecting';
|
|
198
|
+
if (!guarded) return undefined;
|
|
145
199
|
if (now - snapshotAt > config.refreshMs * 2) return undefined;
|
|
146
200
|
|
|
201
|
+
const connection = row.connection ?? undefined;
|
|
147
202
|
const detail = row.probe?.error
|
|
203
|
+
?? (connection?.state === 'reconnecting'
|
|
204
|
+
? `the MCP client is reconnecting (attempt ${connection.attempt ?? '?'} of ${connection.maxAttempts ?? '?'})`
|
|
205
|
+
: null)
|
|
206
|
+
?? (row.reason === 'gave-up' && connection !== undefined
|
|
207
|
+
? `the MCP client gave up after ${connection.maxAttempts ?? '?'} failed reconnect attempts`
|
|
208
|
+
: null)
|
|
148
209
|
?? (row.reachDetail === 'timeout' ? `TCP connect to ${row.target} timed out` : null)
|
|
149
210
|
?? (row.reachDetail === 'refused' ? `TCP connect to ${row.target} refused` : null)
|
|
150
211
|
?? (row.state === 'failed' ? 'the MCP client failed to load' : 'not connected');
|
|
@@ -164,6 +225,7 @@ export function buildHealthText(snapshot, { now = Date.now(), maxRows = 6, maxCh
|
|
|
164
225
|
? ''
|
|
165
226
|
: `, checked ${Math.max(0, Math.round((now - row.checkedAt) / 1000))}s ago`;
|
|
166
227
|
const detail = row.probe?.error
|
|
228
|
+
?? (row.reason === 'gave-up' ? 'reconnect gave up — tools unregistered' : null)
|
|
167
229
|
?? (row.reachDetail === 'refused' ? 'TCP connect refused' : row.reachDetail === 'timeout' ? 'TCP connect timed out' : row.state);
|
|
168
230
|
return `- ${row.serverName} (${row.target || 'unknown target'}): ${row.state} — ${detail}${age}.`;
|
|
169
231
|
});
|
|
@@ -176,6 +238,86 @@ export function buildHealthText(snapshot, { now = Date.now(), maxRows = 6, maxCh
|
|
|
176
238
|
return text.length > maxChars ? `${text.slice(0, maxChars - 1)}…` : text;
|
|
177
239
|
}
|
|
178
240
|
|
|
241
|
+
/**
|
|
242
|
+
* 侧栏角标的音调:ok(全好)/ idle(还没起来)/ warn(正在自救)/ bad(要人管)/ disabled / empty。
|
|
243
|
+
* 只是渲染用的一档分组,不是第二个状态判定——每个 row 的 state 仍然来自 {@link resolveStatus}。
|
|
244
|
+
*/
|
|
245
|
+
const ATTENTION_RANK = { failed: 3, unreachable: 3, reconnecting: 2 };
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* 把一份快照压成侧栏要的那几个数:这是**聚合**,不是判定。
|
|
249
|
+
*
|
|
250
|
+
* 每个 row 的 state 已经由 {@link resolveStatus} 定好,这里只回答「最值得看的那一档是什么、
|
|
251
|
+
* 有几台要人管、它们叫什么」。所以 A3 的边界原样保留:`idle`/`pending` 不算异常、不进 attention,
|
|
252
|
+
* 只有 failed/unreachable(bad)与 reconnecting(warn)会点亮角标。
|
|
253
|
+
*
|
|
254
|
+
* @param {object[]} servers 快照里的行(DTO 子集:id、serverName、state、reason)
|
|
255
|
+
* @param {object} [options]
|
|
256
|
+
* @param {number} [options.maxNames] 最多带出几个名字(多出来的只给计数,避免侧栏提示过长)
|
|
257
|
+
* @returns {{worst: string, counts: object, attention: object[], names: string[], more: number, signature: string}}
|
|
258
|
+
*/
|
|
259
|
+
export function summarizeServers(servers, { maxNames = 3 } = {}) {
|
|
260
|
+
const rows = Array.isArray(servers) ? servers : [];
|
|
261
|
+
const counts = {
|
|
262
|
+
total: rows.length,
|
|
263
|
+
connected: 0,
|
|
264
|
+
idle: 0,
|
|
265
|
+
pending: 0,
|
|
266
|
+
reconnecting: 0,
|
|
267
|
+
unhealthy: 0,
|
|
268
|
+
disabled: 0,
|
|
269
|
+
unknown: 0,
|
|
270
|
+
};
|
|
271
|
+
const attention = [];
|
|
272
|
+
let worst = 'empty';
|
|
273
|
+
|
|
274
|
+
for (const row of rows) {
|
|
275
|
+
const state = row?.state;
|
|
276
|
+
if (state === 'connected') counts.connected += 1;
|
|
277
|
+
else if (state === 'idle') counts.idle += 1;
|
|
278
|
+
else if (state === 'pending') counts.pending += 1;
|
|
279
|
+
else if (state === 'disabled') counts.disabled += 1;
|
|
280
|
+
else if (state === 'reconnecting') {
|
|
281
|
+
counts.reconnecting += 1;
|
|
282
|
+
attention.push(row);
|
|
283
|
+
} else if (state === 'failed' || state === 'unreachable') {
|
|
284
|
+
counts.unhealthy += 1;
|
|
285
|
+
attention.push(row);
|
|
286
|
+
} else {
|
|
287
|
+
// 未知状态不猜:既不点亮角标,也不假装连上了。
|
|
288
|
+
counts.unknown += 1;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const rowRank = ATTENTION_RANK[state];
|
|
292
|
+
if (rowRank !== undefined) {
|
|
293
|
+
const currentRank = worst === 'bad' ? 3 : worst === 'warn' ? 2 : 0;
|
|
294
|
+
if (rowRank > currentRank) worst = rowRank === 3 ? 'bad' : 'warn';
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (worst === 'empty' && rows.length > 0) {
|
|
299
|
+
if (counts.connected > 0) worst = 'ok';
|
|
300
|
+
else if (counts.idle > 0 || counts.pending > 0) worst = 'idle';
|
|
301
|
+
else if (counts.disabled > 0) worst = 'disabled';
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const shown = attention.slice(0, Math.max(1, maxNames));
|
|
305
|
+
return {
|
|
306
|
+
worst,
|
|
307
|
+
counts,
|
|
308
|
+
attention: shown.map((row) => ({
|
|
309
|
+
id: row?.id,
|
|
310
|
+
name: row?.serverName ?? row?.id ?? 'MCP',
|
|
311
|
+
state: row?.state,
|
|
312
|
+
reason: row?.reason,
|
|
313
|
+
})),
|
|
314
|
+
names: shown.map((row) => row?.serverName ?? row?.id ?? 'MCP'),
|
|
315
|
+
more: Math.max(0, attention.length - shown.length),
|
|
316
|
+
/** 客户端拿它判断「这份快照和上一份是不是同一回事」。 */
|
|
317
|
+
signature: rows.map((row) => `${row?.id}:${row?.state}:${row?.reason}`).join('|'),
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
179
321
|
/** 设置 section 顺序:优先问注册表,问不到就用一个稳的常量(不因为版本差异抛错)。 */
|
|
180
322
|
export function sectionOrder(registry, name = 'MCP_SERVERS', fallback = 50) {
|
|
181
323
|
try {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-mcp-status",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "DSH Web GUI 的 MCP 服务器面板:在侧边栏查看每个 MCP 服务器的连接状态、工具数量与配置,并可直接开关、重试连接。",
|
|
6
6
|
"license": "MIT",
|
|
@@ -56,6 +56,8 @@
|
|
|
56
56
|
}
|
|
57
57
|
},
|
|
58
58
|
"scripts": {
|
|
59
|
-
"test": "node test/status.test.mjs && node lib/probe.test.mjs"
|
|
59
|
+
"test": "node test/status.test.mjs && node lib/probe.test.mjs",
|
|
60
|
+
"check": "node --check index.js && node --check client.js && node --check lib/status.js && node --check lib/probe.js && npm test",
|
|
61
|
+
"release": "node scripts/release.mjs"
|
|
60
62
|
}
|
|
61
63
|
}
|