dsh-plugin-tool-management 0.21.0 → 0.21.2
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 +108 -0
- package/README.md +47 -10
- package/README_EN.md +45 -10
- package/docs/images/1-EN.png +0 -0
- package/docs/images/1.png +0 -0
- package/docs/images/2-EN.png +0 -0
- package/docs/images/2.png +0 -0
- package/docs/images/3-EN.png +0 -0
- package/docs/images/3.png +0 -0
- package/docs/images/4-EN.png +0 -0
- package/docs/images/4.png +0 -0
- package/docs/images/5-EN.png +0 -0
- package/docs/images/5.png +0 -0
- package/docs/images/6-EN.png +0 -0
- package/docs/images/6.png +0 -0
- package/docs/images/7-EN.png +0 -0
- package/docs/images/7.png +0 -0
- package/docs/images/8-EN.png +0 -0
- package/docs/images/8.png +0 -0
- package/docs/images/icon.png +0 -0
- package/docs/images/icon.svg +1 -0
- package/docs/update.md +37 -35
- package/lib/boot/reconcile.js +157 -0
- package/lib/client.js +7166 -3084
- package/lib/compat/patch-dialect.js +14 -1
- package/lib/compat/probe.js +25 -21
- package/lib/context-inject-frame.js +62 -40
- package/lib/context-inject-select.js +59 -4
- package/lib/context-inject-surface.js +49 -3
- package/lib/context-inject.js +145 -22
- package/lib/diagnostics.js +77 -0
- package/lib/http-api.js +39 -23
- package/lib/http-fence.js +40 -5
- package/lib/imports/unzip.js +48 -0
- package/lib/imports/upload.js +8 -36
- package/lib/index.js +2933 -169
- package/lib/mcp/disabled-tools.js +96 -0
- package/lib/mcp/manager.js +559 -128
- package/lib/mcp/restart-marker.js +110 -0
- package/lib/mcp/state-section.js +132 -43
- package/lib/mcp/tool-visibility.js +348 -56
- package/lib/memories/archive-engine.js +39 -14
- package/lib/memories/discovery.js +30 -10
- package/lib/memories/index-io.js +217 -18
- package/lib/memories/projection.js +20 -3
- package/lib/memories/prompt-layer.js +37 -0
- package/lib/memories/search.js +86 -0
- package/lib/memories/service.js +327 -91
- package/lib/memories/snapshot.js +66 -19
- package/lib/op-registry.js +49 -4
- package/lib/ops/candidates.js +11 -3
- package/lib/ops/compat.js +46 -4
- package/lib/ops/memory.js +78 -9
- package/lib/ops/prompts.js +49 -5
- package/lib/ops/quick-prompts.js +24 -2
- package/lib/ops/scene-records.js +7 -0
- package/lib/ops/scene-sync.js +23 -4
- package/lib/ops/scope.js +1093 -0
- package/lib/ops/sessions.js +5 -2
- package/lib/ops/snapshot.js +55 -20
- package/lib/ops/state-doctor.js +39 -4
- package/lib/ops/trash.js +12 -1
- package/lib/paths.js +50 -3
- package/lib/prompts/preset-id.js +4 -0
- package/lib/prompts/project-instructions.js +162 -0
- package/lib/prompts/quick-service.js +5 -4
- package/lib/prompts/service.js +20 -9
- package/lib/request-gate.js +6 -4
- package/lib/scene-linkage.js +12 -0
- package/lib/scene-prompt-sync.js +1 -1
- package/lib/scenes/candidates.js +8 -1
- package/lib/scope/intent-store.js +105 -0
- package/lib/scope/intent.js +376 -0
- package/lib/scope/resolve.js +190 -0
- package/lib/scope/scope-overrides.js +894 -0
- package/lib/scope/store.js +262 -0
- package/lib/scope/workspace-key.js +64 -0
- package/lib/sessions/history.js +3 -2
- package/lib/settings-sidecars.js +82 -19
- package/lib/sidebar-settings.js +13 -0
- package/lib/skills/catalog.js +206 -33
- package/lib/skills/core.js +1 -1
- package/lib/skills/frontmatter.js +121 -0
- package/lib/skills/service.js +145 -20
- package/lib/stale-write.js +74 -0
- package/lib/subagents/catalog.js +47 -13
- package/lib/subagents/persona-parse.js +26 -5
- package/lib/subagents/persona-write.js +11 -7
- package/lib/subagents/service.js +3 -2
- package/lib/subagents/tools.js +78 -8
- package/lib/token-config.js +25 -5
- package/lib/tool-table-switch.js +141 -49
- package/lib/tools/deps.js +151 -5
- package/lib/tools/mcp.js +189 -39
- package/lib/tools/memory.js +103 -18
- package/lib/tools/prompt.js +61 -9
- package/lib/tools/remove.js +46 -21
- package/lib/tools/scene.js +120 -26
- package/lib/tools/skills.js +191 -30
- package/lib/tools/subagent.js +52 -20
- package/lib/tools/table.js +48 -11
- package/lib/trash-retention.js +3 -2
- package/lib/update-check.js +218 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +19 -14
package/lib/mcp/manager.js
CHANGED
|
@@ -18,15 +18,18 @@
|
|
|
18
18
|
// 其余 14 项(ensurePaths / withWriteLock / readPatch / pluginInventory / memoriesService 等)
|
|
19
19
|
// 由 deps 显式传入。
|
|
20
20
|
import { clearRuntimeNote, noteRuntime } from '../compat/runtime-notes.js';
|
|
21
|
-
import { mkdir } from 'node:fs/promises';
|
|
21
|
+
import { chmod, mkdir } from 'node:fs/promises';
|
|
22
22
|
import { join } from 'node:path';
|
|
23
23
|
import { MCP_CLIENT_MODULE, MCP_TOOL_PREFIX, ambiguousServerNames, looksLikeMcpPrefixDrift, serverNameCandidates, splitMcpToolName } from '../host-names.js';
|
|
24
24
|
import { hubPath } from '../hub.js';
|
|
25
|
+
import { stampConflict, stampOfText } from '../stale-write.js';
|
|
26
|
+
import { cloneDisabledTools, emptyDisabledTools, isUnsafeKey, } from './disabled-tools.js';
|
|
25
27
|
import { planOverrideCompaction } from './override-blocks.js';
|
|
26
28
|
import { createMcpStateCatalog } from './state-section.js';
|
|
27
29
|
import { createToolVisibility } from './tool-visibility.js';
|
|
28
30
|
import { appendBlock, buildDisableBlock, buildInsertBlock, insertEntryIds, parseRows, removeEntryAll, removeMarked, spliceRanges, splitLines, } from './patch-yaml.js';
|
|
29
31
|
import { describeMaskedOutcome, maskSecretValue, maskUrlQuery, maskedKeysIn, resolveMaskedKv, resolveMaskedUrl } from './secret-guard.js';
|
|
32
|
+
import { clearMcpRestartMarker, writeMcpRestartMarker } from './restart-marker.js';
|
|
30
33
|
/**
|
|
31
34
|
* 归一化「已知工具」侧车的一行值。
|
|
32
35
|
*
|
|
@@ -96,21 +99,43 @@ export function countEnabledTools(known, disabled) {
|
|
|
96
99
|
* 也让 `createMcpManager` 不再需要为它保留一个闭包内定义。
|
|
97
100
|
*/
|
|
98
101
|
export function isToolDisabledIn(map, fullName) {
|
|
99
|
-
const
|
|
102
|
+
const name = String(fullName == null ? '' : fullName);
|
|
103
|
+
if (!name.startsWith(MCP_TOOL_PREFIX))
|
|
104
|
+
return false;
|
|
105
|
+
const rest = name.slice(MCP_TOOL_PREFIX.length);
|
|
106
|
+
let matchedAny = false;
|
|
107
|
+
// `mcp__a__b__c` 在 `a` 与 `a__b` 同时是停用表键时有两种归属。只靠名字无法判定,
|
|
108
|
+
// 守卫按 fail-closed 取“任一候选命中即拒”:宁可误伤另一台的同名工具,也不放行一个
|
|
109
|
+
// 真的被停用的工具(2026-09-30 审查 G14)。
|
|
110
|
+
for (const [server, list] of Object.entries(map || {})) {
|
|
111
|
+
if (isUnsafeKey(server) || !Array.isArray(list))
|
|
112
|
+
continue;
|
|
113
|
+
if (rest.length <= server.length + 2)
|
|
114
|
+
continue;
|
|
115
|
+
if (!rest.startsWith(server + '__'))
|
|
116
|
+
continue;
|
|
117
|
+
matchedAny = true;
|
|
118
|
+
const tool = rest.slice(server.length + 2);
|
|
119
|
+
if (tool && (list.indexOf('*') >= 0 || list.indexOf(tool) >= 0))
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
if (matchedAny)
|
|
123
|
+
return false;
|
|
124
|
+
// 一个候选都没命中 → 与显示侧同一套兜底(最后一个 `__`)。猜出来的 server 不在表里,
|
|
125
|
+
// 查表结果与“没有候选命中”一致。
|
|
126
|
+
const parts = splitMcpToolName(name, []);
|
|
100
127
|
if (parts === null)
|
|
101
128
|
return false;
|
|
102
129
|
const list = map[parts.server];
|
|
103
130
|
if (!list)
|
|
104
131
|
return false;
|
|
105
|
-
|
|
106
|
-
return true;
|
|
107
|
-
return list.indexOf(parts.tool) >= 0;
|
|
132
|
+
return list.indexOf('*') >= 0 || list.indexOf(parts.tool) >= 0;
|
|
108
133
|
}
|
|
109
134
|
/** `mcpm-parse-patch` 的输入上限:粘贴框不是文件上传口,几十 KB 之外基本都是误贴。 */
|
|
110
135
|
const PATCH_PARSE_MAX_CHARS = 200_000;
|
|
111
136
|
export function createMcpManager(deps) {
|
|
112
137
|
// 外部能力一次解构成局部名:块内代码是逐字搬来的,保持原样最不容易出错。
|
|
113
|
-
const { readJsonFile, writeJsonFile, ensurePaths, withWriteLock, message, tools, pluginInventory, settings, readPatch, writePatch, memoriesService, wait, } = deps;
|
|
138
|
+
const { readJsonFile, readJsonFileStrict, writeJsonFile, ensurePaths, withWriteLock, message, tools, pluginInventory, settings, readPatch, writePatch, memoriesService, wait, } = deps;
|
|
114
139
|
// MCP 状态段(注入通道的数据源)由本域自己建:它要的 rows 就是本域的 mcpmRowsWithNotes(false),
|
|
115
140
|
// 让 index.ts 建再传进来会形成 index ↔ manager 的循环引用。
|
|
116
141
|
// `listToolVisible` 是唯一从外面进来的:它要看**这个 agent** 手里有没有 `mcp_manager_list`
|
|
@@ -118,6 +143,11 @@ export function createMcpManager(deps) {
|
|
|
118
143
|
const mcpStateCatalog = createMcpStateCatalog({
|
|
119
144
|
rows: () => mcpmRowsWithNotes(false),
|
|
120
145
|
...(deps.listToolVisible === undefined ? {} : { listToolVisible: deps.listToolVisible }),
|
|
146
|
+
// 会话层:本会话隐藏的服务器整条不进段(理由见 state-section.ts)。逐行问谓词 ——
|
|
147
|
+
// 行数组本身就是服务器全集,不需要另建一份。
|
|
148
|
+
...(deps.sessionHiddenServer === undefined ? {} : { sessionServerHidden: deps.sessionHiddenServer }),
|
|
149
|
+
// 逐工具会话判定(0.21.2):一台服务器在本会话里的工具全关掉时,段里标 muted 而不是可用。
|
|
150
|
+
...(deps.sessionToolVerdictFor === undefined ? {} : { sessionToolVerdictFor: deps.sessionToolVerdictFor }),
|
|
121
151
|
});
|
|
122
152
|
// ---------- duplicate loader-id guard ----------
|
|
123
153
|
// Two rows with the same loader id make the plugin composition fail to
|
|
@@ -284,27 +314,38 @@ export function createMcpManager(deps) {
|
|
|
284
314
|
/** MCP 侧车绝对路径(都在 hub 内)。 */
|
|
285
315
|
const mcpSidecar = (name) => hubPath(name);
|
|
286
316
|
let notesCache = null;
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
const raw = await readJsonFile(mcpSidecar(MCP_NOTES_FILE));
|
|
291
|
-
const out = {};
|
|
317
|
+
/** 备注侧车的形状归一化(读路径与写路径共用;写路径的 raw 来自 strict read)。 */
|
|
318
|
+
function normalizeNotes(raw) {
|
|
319
|
+
const out = Object.create(null);
|
|
292
320
|
if (raw && typeof raw === 'object') {
|
|
293
|
-
for (const key of Object.
|
|
294
|
-
|
|
321
|
+
for (const [key, value] of Object.entries(raw)) {
|
|
322
|
+
if (isUnsafeKey(key))
|
|
323
|
+
continue;
|
|
295
324
|
if (typeof value === 'string' && value.trim())
|
|
296
325
|
out[key] = value;
|
|
297
326
|
}
|
|
298
327
|
}
|
|
328
|
+
return out;
|
|
329
|
+
}
|
|
330
|
+
async function readNotes(force = false) {
|
|
331
|
+
if (notesCache && !force && Date.now() - notesCache.at < SIDECAR_TTL_MS)
|
|
332
|
+
return notesCache.value;
|
|
333
|
+
const raw = await readJsonFile(mcpSidecar(MCP_NOTES_FILE));
|
|
334
|
+
const out = normalizeNotes(raw);
|
|
299
335
|
notesCache = { at: Date.now(), value: out };
|
|
300
336
|
return out;
|
|
301
337
|
}
|
|
302
338
|
/**
|
|
303
339
|
* 备注侧车的读-改-写。**必须在调用方已持有写锁时调用** —— 自己再去拿一次锁会自锁。
|
|
304
340
|
* 空 note = 删掉该条目(与 `mcpm-note` 的语义一致);强制重读,外部改过的侧车是合并而不是覆盖。
|
|
341
|
+
* 读坏时拒绝写入:`readNotes` 的 lenient 读会把坏文件当空表,照旧写就是整份备注丢失。
|
|
305
342
|
*/
|
|
306
343
|
async function writeNoteUnderLock(id, note) {
|
|
307
|
-
const
|
|
344
|
+
const read = await readJsonFileStrict(mcpSidecar(MCP_NOTES_FILE));
|
|
345
|
+
if (read.status === 'corrupt' || read.status === 'unreadable') {
|
|
346
|
+
return { ok: false, error: `MCP 备注文件${read.status === 'corrupt' ? '损坏' : '不可读'},已拒绝保存:${read.error}` };
|
|
347
|
+
}
|
|
348
|
+
const map = Object.assign(Object.create(null), normalizeNotes(read.status === 'ok' ? read.value : null));
|
|
308
349
|
if (note)
|
|
309
350
|
map[id] = note;
|
|
310
351
|
else
|
|
@@ -341,45 +382,51 @@ export function createMcpManager(deps) {
|
|
|
341
382
|
return fallback;
|
|
342
383
|
return Math.min(max, Math.max(min, Math.round(n)));
|
|
343
384
|
}
|
|
385
|
+
function normalizePluginSettings(raw) {
|
|
386
|
+
return {
|
|
387
|
+
pollIntervalMs: clampInt(raw && raw.pollIntervalMs, POLL_INTERVAL_MIN_MS, POLL_INTERVAL_MAX_MS, SETTINGS_DEFAULTS.pollIntervalMs),
|
|
388
|
+
// 0 = keep descriptions in full (the UI default); > 0 truncates.
|
|
389
|
+
toolDescriptionMaxLength: clampInt(raw && raw.toolDescriptionMaxLength, 0, 2000, SETTINGS_DEFAULTS.toolDescriptionMaxLength),
|
|
390
|
+
// 模型写规则需确认(D2):程序化强制,只读设置供 tools/pre-execute 判定。
|
|
391
|
+
requireConfirmForModelRuleWrite: (raw && typeof raw.requireConfirmForModelRuleWrite === 'boolean')
|
|
392
|
+
? raw.requireConfirmForModelRuleWrite
|
|
393
|
+
: SETTINGS_DEFAULTS.requireConfirmForModelRuleWrite,
|
|
394
|
+
// 子代理运行花真 token:默认确认,设置可关(设计 §3.2)。
|
|
395
|
+
requireConfirmForModelSubagentRun: (raw && typeof raw.requireConfirmForModelSubagentRun === 'boolean')
|
|
396
|
+
? raw.requireConfirmForModelSubagentRun
|
|
397
|
+
: SETTINGS_DEFAULTS.requireConfirmForModelSubagentRun,
|
|
398
|
+
};
|
|
399
|
+
}
|
|
344
400
|
async function readPluginSettings(force = false) {
|
|
345
401
|
if (pluginSettingsCache && !force && Date.now() - pluginSettingsCache.at < SIDECAR_TTL_MS)
|
|
346
402
|
return pluginSettingsCache.value;
|
|
347
|
-
|
|
403
|
+
// 读路径不调 ensurePaths:hubPath 走 resolveDshHome,与补丁路径推导无关。
|
|
348
404
|
const raw = await readJsonFile(mcpSidecar(MCP_SETTINGS_FILE));
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
pollIntervalMs: clampInt(raw && raw.pollIntervalMs, POLL_INTERVAL_MIN_MS, POLL_INTERVAL_MAX_MS, SETTINGS_DEFAULTS.pollIntervalMs),
|
|
353
|
-
// 0 = keep descriptions in full (the UI default); > 0 truncates.
|
|
354
|
-
toolDescriptionMaxLength: clampInt(raw && raw.toolDescriptionMaxLength, 0, 2000, SETTINGS_DEFAULTS.toolDescriptionMaxLength),
|
|
355
|
-
// 模型写规则需确认(D2):程序化强制,只读设置供 tools/pre-execute 判定。
|
|
356
|
-
requireConfirmForModelRuleWrite: (raw && typeof raw.requireConfirmForModelRuleWrite === 'boolean')
|
|
357
|
-
? raw.requireConfirmForModelRuleWrite
|
|
358
|
-
: SETTINGS_DEFAULTS.requireConfirmForModelRuleWrite,
|
|
359
|
-
// 子代理运行花真 token:默认确认,设置可关(设计 §3.2)。
|
|
360
|
-
requireConfirmForModelSubagentRun: (raw && typeof raw.requireConfirmForModelSubagentRun === 'boolean')
|
|
361
|
-
? raw.requireConfirmForModelSubagentRun
|
|
362
|
-
: SETTINGS_DEFAULTS.requireConfirmForModelSubagentRun,
|
|
363
|
-
},
|
|
364
|
-
};
|
|
365
|
-
return pluginSettingsCache.value;
|
|
405
|
+
const value = normalizePluginSettings(raw);
|
|
406
|
+
pluginSettingsCache = { at: Date.now(), value };
|
|
407
|
+
return value;
|
|
366
408
|
}
|
|
367
409
|
async function mcpmSettings(args) {
|
|
368
|
-
const current = await readPluginSettings();
|
|
369
410
|
if (!args || args.set !== true)
|
|
370
|
-
return { ok: true, settings:
|
|
371
|
-
|
|
372
|
-
pollIntervalMs: clampInt(args.pollIntervalMs, POLL_INTERVAL_MIN_MS, POLL_INTERVAL_MAX_MS, current.pollIntervalMs),
|
|
373
|
-
toolDescriptionMaxLength: clampInt(args.toolDescriptionMaxLength, 0, 2000, current.toolDescriptionMaxLength),
|
|
374
|
-
requireConfirmForModelRuleWrite: (args && typeof args.requireConfirmForModelRuleWrite === 'boolean')
|
|
375
|
-
? args.requireConfirmForModelRuleWrite
|
|
376
|
-
: current.requireConfirmForModelRuleWrite,
|
|
377
|
-
requireConfirmForModelSubagentRun: (args && typeof args.requireConfirmForModelSubagentRun === 'boolean')
|
|
378
|
-
? args.requireConfirmForModelSubagentRun
|
|
379
|
-
: current.requireConfirmForModelSubagentRun,
|
|
380
|
-
};
|
|
381
|
-
const p = await ensurePaths();
|
|
411
|
+
return { ok: true, settings: await readPluginSettings() };
|
|
412
|
+
await ensurePaths();
|
|
382
413
|
return withWriteLock(async () => {
|
|
414
|
+
// 写锁内 strict read:读坏时不能拿默认值当基线,否则一次保存就把原设置洗掉。
|
|
415
|
+
const read = await readJsonFileStrict(mcpSidecar(MCP_SETTINGS_FILE));
|
|
416
|
+
if (read.status === 'corrupt' || read.status === 'unreadable') {
|
|
417
|
+
return { ok: false, error: `MCP 设置文件${read.status === 'corrupt' ? '损坏' : '不可读'},已拒绝保存:${read.error}` };
|
|
418
|
+
}
|
|
419
|
+
const current = normalizePluginSettings(read.status === 'ok' ? read.value : null);
|
|
420
|
+
const next = {
|
|
421
|
+
pollIntervalMs: clampInt(args.pollIntervalMs, POLL_INTERVAL_MIN_MS, POLL_INTERVAL_MAX_MS, current.pollIntervalMs),
|
|
422
|
+
toolDescriptionMaxLength: clampInt(args.toolDescriptionMaxLength, 0, 2000, current.toolDescriptionMaxLength),
|
|
423
|
+
requireConfirmForModelRuleWrite: (args && typeof args.requireConfirmForModelRuleWrite === 'boolean')
|
|
424
|
+
? args.requireConfirmForModelRuleWrite
|
|
425
|
+
: current.requireConfirmForModelRuleWrite,
|
|
426
|
+
requireConfirmForModelSubagentRun: (args && typeof args.requireConfirmForModelSubagentRun === 'boolean')
|
|
427
|
+
? args.requireConfirmForModelSubagentRun
|
|
428
|
+
: current.requireConfirmForModelSubagentRun,
|
|
429
|
+
};
|
|
383
430
|
try {
|
|
384
431
|
await writeJsonFile(mcpSidecar(MCP_SETTINGS_FILE), next);
|
|
385
432
|
}
|
|
@@ -397,9 +444,11 @@ export function createMcpManager(deps) {
|
|
|
397
444
|
// (isToolDisabled / disabledToolSummary)—— 于是块交回两个句柄,语义与原样一致:
|
|
398
445
|
// setDisabledToolsCache(x) ≡ `disabledToolsCache = { at: Date.now(), value: x }`,
|
|
399
446
|
// disabledToolsSnapshot() ≡ `disabledToolsCache ? disabledToolsCache.value : {}`。
|
|
400
|
-
const { readDisabledTools, readKnownMcpTools, writeKnownMcpTools, mutateKnownMcpTools, mergeLiveToolsInto, mcpmToolEnabled, applyToolRestrictions, scheduleToolRestrictions, attachAgent, detachAgent, setDisabledToolsCache, disabledToolsSnapshot, disposeVisibility, } = createToolVisibility({
|
|
401
|
-
readJsonFile, writeJsonFile, ensurePaths, withWriteLock, message, tools,
|
|
447
|
+
const { readDisabledTools, readDisabledToolsResult, disabledToolsState, disabledToolsIssue, ensureDisabledToolsForensics, resetDisabledTools, readKnownMcpTools, writeKnownMcpTools, mutateKnownMcpTools, mergeLiveToolsInto, mcpmToolEnabled, applyToolRestrictions, scheduleToolRestrictions, attachAgent, detachAgent, setDisabledToolsCache, disabledToolsSnapshot, disposeVisibility, } = createToolVisibility({
|
|
448
|
+
readJsonFile, readJsonFileStrict, writeJsonFile, ensurePaths, withWriteLock, message, tools,
|
|
402
449
|
pluginHiddenTools: deps.pluginHiddenTools, carrierHiddenTools: deps.carrierHiddenTools,
|
|
450
|
+
sessionMcpOverlay: deps.sessionMcpOverlay,
|
|
451
|
+
sessionToolTableOverlay: deps.sessionToolTableOverlay,
|
|
403
452
|
normalizeKnownTools, mcpSidecar, SIDECAR_TTL_MS, MCP_DISABLED_TOOLS_FILE, MCP_KNOWN_TOOLS_FILE,
|
|
404
453
|
});
|
|
405
454
|
function truncateText(value, limit) {
|
|
@@ -407,21 +456,44 @@ export function createMcpManager(deps) {
|
|
|
407
456
|
return value;
|
|
408
457
|
return value.slice(0, Math.max(1, limit - 1)).replace(/\s+$/, '') + '…';
|
|
409
458
|
}
|
|
459
|
+
/**
|
|
460
|
+
* 某台服务器**当前真实注册**的工具 schema(未投影的原始对象)。
|
|
461
|
+
*
|
|
462
|
+
* 与 `mcpmList` 那一遍全表扫描同一条取数路径(`tools.schemas()` 的插件级不受限视图),
|
|
463
|
+
* 只是这里按一台筛。`schemas()` 抛错**不由本函数吞**:`mcpmTools` 要把它翻成 ok:false,
|
|
464
|
+
* 「刷新工具」按"这次没取到"处理。
|
|
465
|
+
*/
|
|
466
|
+
async function liveSchemasOf(serverName) {
|
|
467
|
+
const prefix = MCP_TOOL_PREFIX + serverName + '__';
|
|
468
|
+
const schemas = await tools.schemas();
|
|
469
|
+
return (schemas || []).filter((s) => String(s && s.name || '').startsWith(prefix));
|
|
470
|
+
}
|
|
471
|
+
/** live schema → 「已知工具」侧车那一行的形状(名字 + 描述;按名字排序,与侧车格式一致)。 */
|
|
472
|
+
async function liveToolsOf(serverName) {
|
|
473
|
+
const out = [];
|
|
474
|
+
const seen = new Set();
|
|
475
|
+
for (const s of await liveSchemasOf(serverName)) {
|
|
476
|
+
const rawName = String(s && s.name || '').slice(MCP_TOOL_PREFIX.length + serverName.length + 2);
|
|
477
|
+
if (!rawName || seen.has(rawName))
|
|
478
|
+
continue;
|
|
479
|
+
seen.add(rawName);
|
|
480
|
+
const desc = typeof s.description === 'string' ? s.description : '';
|
|
481
|
+
out.push({ name: rawName, ...(desc ? { description: desc } : {}) });
|
|
482
|
+
}
|
|
483
|
+
return out.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
484
|
+
}
|
|
410
485
|
// ---------- secret masking (UI view only) ----------
|
|
411
|
-
//
|
|
412
|
-
//
|
|
413
|
-
// are redacted because MCP credentials often ride there.
|
|
414
|
-
const SENSITIVE_KEY_RE = /(token|secret|password|passwd|auth|credential|api[_-]?key|access[_-]?key|private[_-]?key|cookie|session|signature|bearer)/i;
|
|
486
|
+
// `$VAR` / `!!js` references are indirections rather than secrets, so they stay readable.
|
|
487
|
+
// URL query strings are redacted because MCP credentials often ride there.
|
|
415
488
|
// 单值打码(`maskSecretValue`)与 URL 打码(`maskUrlQuery`)都挪到 ./secret-guard.js ——
|
|
416
489
|
// 那里是这两条形态的唯一口径,使用方现在有三个(本文件的列表视图 + index.ts 的确认卡
|
|
417
490
|
// + ops/snapshot.ts 的整机迁移导出)。此处不再自建一份。
|
|
418
|
-
function maskValueMap(map
|
|
491
|
+
function maskValueMap(map) {
|
|
419
492
|
if (!map || typeof map !== 'object')
|
|
420
493
|
return map;
|
|
421
494
|
const out = {};
|
|
422
|
-
for (const key of Object.keys(map))
|
|
423
|
-
out[key] =
|
|
424
|
-
}
|
|
495
|
+
for (const key of Object.keys(map))
|
|
496
|
+
out[key] = maskSecretValue(map[key]);
|
|
425
497
|
return out;
|
|
426
498
|
}
|
|
427
499
|
// `maskUrlQuery` 已挪到 ./secret-guard.js:那里是两条打码形态的唯一口径,而使用方
|
|
@@ -436,8 +508,11 @@ export function createMcpManager(deps) {
|
|
|
436
508
|
const view = Object.assign({}, row, { notes: notes[row.id] || '' });
|
|
437
509
|
if (!reveal) {
|
|
438
510
|
view.url = maskUrlQuery(row.url);
|
|
439
|
-
|
|
440
|
-
|
|
511
|
+
// 默认列表对**所有** env / headers 值打码:键名不可靠 —— `DATABASE_URL`、`OPENAI_KEY`
|
|
512
|
+
// 这类名字看起来不像密钥,却经常直接携带凭据。`$VAR` / `!!js` 是间接引用,
|
|
513
|
+
// 由 maskSecretValue 原样保留;编辑弹窗里的打码值由 resolveMaskedKv 从旧配置回填。
|
|
514
|
+
view.headers = maskValueMap(row.headers);
|
|
515
|
+
view.env = maskValueMap(row.env);
|
|
441
516
|
}
|
|
442
517
|
return view;
|
|
443
518
|
});
|
|
@@ -466,24 +541,25 @@ export function createMcpManager(deps) {
|
|
|
466
541
|
const serverName = String(args && args.serverName || '').trim();
|
|
467
542
|
if (!serverName)
|
|
468
543
|
return { ok: false, error: 'serverName 不能为空' };
|
|
469
|
-
const prefix = MCP_TOOL_PREFIX + serverName + '__';
|
|
470
544
|
let schemas = [];
|
|
471
545
|
try {
|
|
472
|
-
schemas = await
|
|
546
|
+
schemas = await liveSchemasOf(serverName);
|
|
473
547
|
}
|
|
474
548
|
catch (e) {
|
|
475
549
|
return { ok: false, error: message(e) };
|
|
476
550
|
}
|
|
477
551
|
const descriptionLimit = (await readPluginSettings()).toolDescriptionMaxLength;
|
|
478
|
-
const
|
|
479
|
-
|
|
552
|
+
const disabledRead = await readDisabledToolsResult();
|
|
553
|
+
// 停用表不可读 = 所有 mcp__* 都被执行侧拒绝(fail-closed)。这里必须报 `false`
|
|
554
|
+
// 而不是让空表把每一行都算成"开着" —— 那正是本次修复要消掉的那句谎话。
|
|
555
|
+
const disabledUnknown = disabledRead.status === 'corrupt' || disabledRead.status === 'unreadable';
|
|
556
|
+
const disabledHere = new Set(disabledUnknown ? ['*'] : (disabledRead.status === 'ok' ? (disabledRead.value[serverName] || []) : []));
|
|
480
557
|
const wildcardHere = disabledHere.has('*');
|
|
481
558
|
const toolsList = [];
|
|
482
559
|
for (const s of schemas) {
|
|
483
|
-
const
|
|
484
|
-
if (!
|
|
560
|
+
const rawName = String(s && s.name || '').slice(MCP_TOOL_PREFIX.length + serverName.length + 2);
|
|
561
|
+
if (!rawName)
|
|
485
562
|
continue;
|
|
486
|
-
const rawName = fullName.slice(prefix.length);
|
|
487
563
|
const params = [];
|
|
488
564
|
const props = s.parameters && typeof s.parameters === 'object' ? s.parameters.properties : null;
|
|
489
565
|
const requiredSet = new Set();
|
|
@@ -539,14 +615,24 @@ export function createMcpManager(deps) {
|
|
|
539
615
|
});
|
|
540
616
|
listed.add(known.name);
|
|
541
617
|
}
|
|
542
|
-
return {
|
|
618
|
+
return {
|
|
619
|
+
ok: true,
|
|
620
|
+
tools: toolsList,
|
|
621
|
+
disabledState: disabledUnknown ? 'unknown' : 'ok',
|
|
622
|
+
disabledIssue: disabledUnknown ? disabledRead.status : null,
|
|
623
|
+
};
|
|
543
624
|
}
|
|
544
625
|
/**
|
|
545
626
|
* 临时启动服务器以枚举工具(v0.8.1):从未运行过的服务器在 schemas() 与「已知工具」
|
|
546
627
|
* 缓存里都没有工具名,场景档案勾选器对它一无所知。这里**临时**把它启用(写补丁),
|
|
547
628
|
* 轮询等它的工具注册(npx 冷启动要下载,上限 TOOLS_REFRESH_TIMEOUT_MS),拿到名单后就
|
|
548
|
-
* **恢复原启停状态**——
|
|
549
|
-
*
|
|
629
|
+
* **恢复原启停状态**—— 用户环境的服务器开关不受影响。
|
|
630
|
+
*
|
|
631
|
+
* 2026-10-19 起它同时是**「刷新工具」的本义**:服务器此刻正在跑,就用 live 名单**整体替换**
|
|
632
|
+
* 缓存里那一台的旧名单 —— 服务器升级改名(dhole 的 `smart_fetch` → `web_read`)之后旧名字
|
|
633
|
+
* 必须消失。原先这里只要缓存非空就直接返回,按钮等于死的;而轮询路径的合并又是只进不出
|
|
634
|
+
* 的并集,于是旧工具永远挂着「上次运行时」的标签赖在详情页里。服务器没在跑时仍退回缓存
|
|
635
|
+
* (没有 live 可看,临时启动一台停着的服务器不是"刷新"该有的代价)。
|
|
550
636
|
*/
|
|
551
637
|
// npx 首次启动要下载包,慢的那次可能要好几秒;给 30 秒上限,超了就如实报"没等到"。
|
|
552
638
|
const TOOLS_REFRESH_TIMEOUT_MS = 30_000;
|
|
@@ -557,6 +643,24 @@ export function createMcpManager(deps) {
|
|
|
557
643
|
const existing = await mcpmTools({ serverName });
|
|
558
644
|
if (existing && existing.ok === false)
|
|
559
645
|
return existing;
|
|
646
|
+
// 服务器**此刻正在跑**:live 名单整体替换缓存里那一台(旧名字随之移除),再把最新清单
|
|
647
|
+
// 返回给界面。这是"刷新工具"该做的事 —— 见上面的函数文档。
|
|
648
|
+
let live = [];
|
|
649
|
+
try {
|
|
650
|
+
live = await liveToolsOf(serverName);
|
|
651
|
+
}
|
|
652
|
+
catch { /* 取不到 live 就当没在跑,走下面的退路 */ }
|
|
653
|
+
if (live.length) {
|
|
654
|
+
try {
|
|
655
|
+
// 读-改-写整段在写锁内(审查 P2-15):轮询路径也在回写同一份侧车,锁外两步会互相覆盖。
|
|
656
|
+
// 合并口径与轮询路径同源(`mergeLiveToolsInto`:live 优先、旧的兜底描述)。
|
|
657
|
+
await mutateKnownMcpTools((cache) => mergeLiveToolsInto(cache, { [serverName]: live }));
|
|
658
|
+
}
|
|
659
|
+
catch { /* 缓存写失败不影响本次返回 */ }
|
|
660
|
+
void mcpStateCatalog.refresh().catch(() => { });
|
|
661
|
+
return await mcpmTools({ serverName });
|
|
662
|
+
}
|
|
663
|
+
// 没在跑:缓存里已有名单就不动它(临时启动一台停着的服务器不是"刷新"该有的代价)。
|
|
560
664
|
if (existing && (existing.tools || []).length)
|
|
561
665
|
return existing;
|
|
562
666
|
const list = await mcpmListView();
|
|
@@ -597,26 +701,31 @@ export function createMcpManager(deps) {
|
|
|
597
701
|
const t = await mcpmTools({ serverName });
|
|
598
702
|
if (t && t.ok && (t.tools || []).length) {
|
|
599
703
|
// 记进「已知工具」缓存:以后未运行时场景档案也能列出(名字 + 这次的描述)。
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
704
|
+
// 只记 **live 那一份**:`mcpmTools` 的返回里混着缓存中的旧名字(服务器升级改名后
|
|
705
|
+
// 的死者),拿它回写等于把死名字又写回去 —— 2026-10-19 用户实测的"刷新了还是旧
|
|
706
|
+
// 工具"正是这条。合并口径与轮询路径同源(live 见到即整体替换那一台)。
|
|
707
|
+
let known = [];
|
|
708
|
+
try {
|
|
709
|
+
known = await liveToolsOf(serverName);
|
|
710
|
+
}
|
|
711
|
+
catch { /* 取不到 live 描述:沿用上面那份清单 */ }
|
|
603
712
|
if (known.length) {
|
|
604
713
|
try {
|
|
605
714
|
// 读-改-写整段在写锁内(审查 P2-15):轮询路径也在回写同一份侧车,
|
|
606
|
-
//
|
|
607
|
-
await mutateKnownMcpTools((cache) => {
|
|
608
|
-
const byName = new Map((cache[serverName] || []).map((item) => [item.name, item]));
|
|
609
|
-
for (const item of known) {
|
|
610
|
-
const old = byName.get(item.name);
|
|
611
|
-
byName.set(item.name, old && old.description && !item.description ? old : { ...old, ...item });
|
|
612
|
-
}
|
|
613
|
-
cache[serverName] = [...byName.values()].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
614
|
-
return true;
|
|
615
|
-
});
|
|
715
|
+
// 锁外两步会互相覆盖。
|
|
716
|
+
await mutateKnownMcpTools((cache) => mergeLiveToolsInto(cache, { [serverName]: known }));
|
|
616
717
|
}
|
|
617
718
|
catch { /* 缓存写失败不影响本次返回 */ }
|
|
719
|
+
// 缓存已整体替换 → 重新取一次再返回:这次清单里不再带旧名字(`t` 里还混着)。
|
|
720
|
+
try {
|
|
721
|
+
result = await mcpmTools({ serverName });
|
|
722
|
+
}
|
|
723
|
+
catch {
|
|
724
|
+
result = t;
|
|
725
|
+
}
|
|
618
726
|
}
|
|
619
|
-
|
|
727
|
+
else
|
|
728
|
+
result = t;
|
|
620
729
|
break;
|
|
621
730
|
}
|
|
622
731
|
if (Date.now() >= deadline) {
|
|
@@ -675,10 +784,89 @@ export function createMcpManager(deps) {
|
|
|
675
784
|
}
|
|
676
785
|
return out;
|
|
677
786
|
}
|
|
787
|
+
/**
|
|
788
|
+
* 补丁层启停值的合并视图(0.21.2):只读补丁、不枚举工具 schema。
|
|
789
|
+
*
|
|
790
|
+
* 同名服务器在两个层级各有一份时,会话覆盖只能整名隐藏 —— 所以只要有一份是开着的,
|
|
791
|
+
* 全局就按「开着」算(会话层关掉它才有意义);两份都关着才是「全局关着」。
|
|
792
|
+
*/
|
|
793
|
+
async function configuredServerStates() {
|
|
794
|
+
const p = await ensurePaths();
|
|
795
|
+
const merged = new Map();
|
|
796
|
+
const patchIds = new Set();
|
|
797
|
+
for (const level of ['project', 'global']) {
|
|
798
|
+
const abs = level === 'project' ? p.projectPatch : p.globalPatch;
|
|
799
|
+
let content = '';
|
|
800
|
+
try {
|
|
801
|
+
content = await readPatch(abs);
|
|
802
|
+
}
|
|
803
|
+
catch {
|
|
804
|
+
continue;
|
|
805
|
+
}
|
|
806
|
+
for (const r of parseRows(content).rows) {
|
|
807
|
+
const row = normalizeRow(r, level, abs);
|
|
808
|
+
const n = String(row.serverName || '');
|
|
809
|
+
if (!n)
|
|
810
|
+
continue;
|
|
811
|
+
patchIds.add(String(row.id));
|
|
812
|
+
const disabled = row.disabled === true;
|
|
813
|
+
const prev = merged.get(n);
|
|
814
|
+
merged.set(n, prev === undefined ? disabled : prev && disabled);
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
// loader 里有、补丁里没有的条目(临时挂载 / 手写 loader 配置):`mcpm-list` 会列出它们,
|
|
818
|
+
// 面板的会话开关也能点到 —— 全局快照必须认识,否则「关掉本会话」会被判成与全局同值而清掉。
|
|
819
|
+
if (pluginInventory) {
|
|
820
|
+
try {
|
|
821
|
+
const res = await pluginInventory.list();
|
|
822
|
+
for (const e of res.entries) {
|
|
823
|
+
if (e.moduleName !== MCP_CLIENT_MODULE)
|
|
824
|
+
continue;
|
|
825
|
+
if (patchIds.has(bareEntryId(e.entryId)))
|
|
826
|
+
continue;
|
|
827
|
+
const name = String(e.entryId);
|
|
828
|
+
const prev = merged.get(name);
|
|
829
|
+
merged.set(name, prev === undefined ? e.enabled !== true : prev && e.enabled !== true);
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
catch { /* loader 清单读不到 → 只按补丁那几台算 */ }
|
|
833
|
+
}
|
|
834
|
+
return [...merged].map(([serverName, disabled]) => ({ serverName, disabled }));
|
|
835
|
+
}
|
|
836
|
+
/** 补丁行(只读补丁,不枚举 schema)—— 对账与场景联动的 id → serverName 映射。 */
|
|
837
|
+
async function patchServerRows() {
|
|
838
|
+
const p = await ensurePaths();
|
|
839
|
+
const out = [];
|
|
840
|
+
for (const level of ['project', 'global']) {
|
|
841
|
+
const abs = level === 'project' ? p.projectPatch : p.globalPatch;
|
|
842
|
+
let content = '';
|
|
843
|
+
try {
|
|
844
|
+
content = await readPatch(abs);
|
|
845
|
+
}
|
|
846
|
+
catch {
|
|
847
|
+
continue;
|
|
848
|
+
}
|
|
849
|
+
for (const r of parseRows(content).rows) {
|
|
850
|
+
const row = normalizeRow(r, level, abs);
|
|
851
|
+
const serverName = String(row.serverName || '');
|
|
852
|
+
if (!serverName)
|
|
853
|
+
continue;
|
|
854
|
+
out.push({ id: String(row.id), serverName, level, disabled: row.disabled === true });
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
return out;
|
|
858
|
+
}
|
|
678
859
|
async function mcpmList() {
|
|
679
860
|
const p = await ensurePaths();
|
|
861
|
+
// 先取四态:后面那段本就在 try 里吞错(schema / 缓存读失败不影响列表),而停用表
|
|
862
|
+
// 读不出来时**不能**让同一个 catch 把状态也一起吞掉 —— 那会退回"工具全部启用"。
|
|
863
|
+
const disabledRead = await readDisabledToolsResult();
|
|
864
|
+
const disabledUnknown = disabledRead.status === 'corrupt' || disabledRead.status === 'unreadable';
|
|
865
|
+
const disabledIssue = disabledUnknown ? disabledRead.status : null;
|
|
680
866
|
const rows = [];
|
|
681
867
|
const errors = [];
|
|
868
|
+
// 每层补丁的内容指纹:编辑弹窗带着它回来,`mcpm-edit` 核对后才写(并发编辑判据)。
|
|
869
|
+
const patchStamps = {};
|
|
682
870
|
for (const level of ['project', 'global']) {
|
|
683
871
|
const abs = level === 'project' ? p.projectPatch : p.globalPatch;
|
|
684
872
|
let content = '';
|
|
@@ -689,6 +877,7 @@ export function createMcpManager(deps) {
|
|
|
689
877
|
errors.push(level + ': ' + message(e));
|
|
690
878
|
continue;
|
|
691
879
|
}
|
|
880
|
+
patchStamps[level] = stampOfText(content);
|
|
692
881
|
const { rows: fileRows } = parseRows(content);
|
|
693
882
|
for (const r of fileRows)
|
|
694
883
|
rows.push(normalizeRow(r, level, abs));
|
|
@@ -712,11 +901,15 @@ export function createMcpManager(deps) {
|
|
|
712
901
|
// 档案挑工具、详情页清单用(那里"上次见过哪些工具"正是需要的,且已标 `stale`)。
|
|
713
902
|
const liveToolCounts = {};
|
|
714
903
|
const liveEnabledToolCounts = {};
|
|
904
|
+
const liveToolNames = {};
|
|
905
|
+
const liveEnabledToolNames = {};
|
|
715
906
|
const knownToolCounts = {};
|
|
716
907
|
try {
|
|
717
908
|
const schemas = await tools.schemas();
|
|
718
909
|
const knownCache = await readKnownMcpTools();
|
|
719
|
-
|
|
910
|
+
// unknown → 按"整台停用"扣减:真实可调用数为 0(执行守卫会拒绝每一个 mcp__*),
|
|
911
|
+
// 但工具**名字**仍如实列出(它们只是此刻调不到,不是不存在)。
|
|
912
|
+
const disabledMap = disabledUnknown ? emptyDisabledTools() : (disabledRead.status === 'ok' ? disabledRead.value : emptyDisabledTools());
|
|
720
913
|
const liveNames = {};
|
|
721
914
|
const liveTools = {};
|
|
722
915
|
const seen = new Set();
|
|
@@ -838,21 +1031,27 @@ export function createMcpManager(deps) {
|
|
|
838
1031
|
// 可用数 = 已知工具里未被停用表扣减的(`*` = 整台停用 → 0)。场景档案收窄与
|
|
839
1032
|
// 手动逐工具开关写的是同一张停用表,段与页面的「N 个工具」都必须按它扣减,
|
|
840
1033
|
// 否则收窄后模型看到的数字比它能调的工具多,页面与详情页也会互相矛盾。
|
|
841
|
-
enabledToolCounts[server] = countEnabledTools(set, disabledMap[server] || []);
|
|
1034
|
+
enabledToolCounts[server] = disabledUnknown ? 0 : countEnabledTools(set, disabledMap[server] || []);
|
|
842
1035
|
}
|
|
843
1036
|
// 真实来源的两个数:只数 `liveNames`(schema 里真正注册了的),再按停用表扣减。
|
|
1037
|
+
// 同时留一份**工具名**(0.21.2):注入段要按会话逐工具开关在名字上扣减 / 补回。
|
|
844
1038
|
for (const [server, list] of Object.entries(liveNames)) {
|
|
845
1039
|
const set = new Set(list);
|
|
1040
|
+
const off = disabledMap[server] || [];
|
|
846
1041
|
liveToolCounts[server] = set.size;
|
|
847
|
-
|
|
1042
|
+
// unknown 时"能调到几个"的答案是 0:守卫会把它们全部拒绝,界面与注入段
|
|
1043
|
+
// 都不能报成可用(这是本轮修复的核心口径,见 tool-visibility 的 fail-closed)。
|
|
1044
|
+
liveEnabledToolCounts[server] = disabledUnknown ? 0 : countEnabledTools(set, off);
|
|
1045
|
+
liveToolNames[server] = list.slice();
|
|
1046
|
+
liveEnabledToolNames[server] = disabledUnknown || off.indexOf('*') >= 0 ? [] : list.filter((t) => off.indexOf(t) < 0);
|
|
848
1047
|
}
|
|
849
1048
|
// 「曾经连上过」的证据:缓存里这台 server 见过几个工具。注意读的是**回写之前**的
|
|
850
1049
|
// `knownCache` —— 回写会把这次 live 见到的并进去,而这次 live 见到的本来就已经算在
|
|
851
1050
|
// liveToolCounts 里了,不需要它来充当"曾经"。
|
|
852
1051
|
for (const [server, list] of Object.entries(knownCache))
|
|
853
1052
|
knownToolCounts[server] = list.length;
|
|
854
|
-
//
|
|
855
|
-
//
|
|
1053
|
+
// 回写「已知工具」:这次 live 见到几台就整体替换这几台(服务器升级改名后旧名字随之
|
|
1054
|
+
// 消失,2026-10-19 用户实测);没见到的服务器原样保留上一份 —— 那是它没跑时的兜底。
|
|
856
1055
|
// 先在本地这份上跑一遍只为**判断要不要写盘**;真正落盘的那次合并发生在锁内、对象是
|
|
857
1056
|
// 刚读出来的新鲜表(审查 P2-15:临时启动路径也在写同一份侧车,锁外两步会互相覆盖)。
|
|
858
1057
|
const cacheChanged = mergeLiveToolsInto(knownCache, liveTools);
|
|
@@ -892,7 +1091,13 @@ export function createMcpManager(deps) {
|
|
|
892
1091
|
// 描述的是"这台 server 有过哪些工具"。两者混淆过一次,代价是注入段谎报可用。
|
|
893
1092
|
row.liveToolCount = liveToolCounts[row.serverName] || 0;
|
|
894
1093
|
row.liveEnabledToolCount = liveEnabledToolCounts[row.serverName] || 0;
|
|
1094
|
+
// 工具名同样总是覆盖:会话逐工具判定按名字算,用上一轮的旧名单会串会话。
|
|
1095
|
+
row.liveTools = liveToolNames[row.serverName] || [];
|
|
1096
|
+
row.liveEnabledTools = liveEnabledToolNames[row.serverName] || [];
|
|
895
1097
|
row.knownToolCount = knownToolCounts[row.serverName] || 0;
|
|
1098
|
+
// 状态段(注入通道)按行渲染,顶层字段到不了它手里 —— unknown 逐行带上。
|
|
1099
|
+
if (disabledUnknown)
|
|
1100
|
+
row.disabledState = 'unknown';
|
|
896
1101
|
}
|
|
897
1102
|
// 列表顺序(用户要求 2026-09-17):**全局在前、应用级在后**,各级按服务器名排,
|
|
898
1103
|
// 当前在跑的 loader 行垫底。放在服务端而不是页面里:同一个顺序也是场景档案勾选器
|
|
@@ -926,11 +1131,26 @@ export function createMcpManager(deps) {
|
|
|
926
1131
|
return {
|
|
927
1132
|
ok: true,
|
|
928
1133
|
rows,
|
|
1134
|
+
// 停用表状态与列表分属两条读路径:后者失败不影响前者,前者 unknown 也不隐藏列表。
|
|
1135
|
+
disabledState: disabledUnknown ? 'unknown' : 'ok',
|
|
1136
|
+
disabledIssue,
|
|
929
1137
|
paths: { project: p.projectPatch, global: p.globalPatch, home: p.home, profile: p.profileName },
|
|
1138
|
+
patchStamps,
|
|
930
1139
|
errors,
|
|
931
1140
|
warnings,
|
|
932
1141
|
};
|
|
933
1142
|
}
|
|
1143
|
+
/**
|
|
1144
|
+
* 重置停用表(恢复入口):只在 corrupt / unreadable 时可用。
|
|
1145
|
+
* 实现与取证全在 tool-visibility(写锁、先取证后覆盖、清状态都在那一处);这里只负责
|
|
1146
|
+
* 成功后的状态段刷新 —— 段里那些行必须立刻从"全部 muted"回到真实状态。
|
|
1147
|
+
*/
|
|
1148
|
+
async function mcpmDisabledReset(_args) {
|
|
1149
|
+
const result = await resetDisabledTools();
|
|
1150
|
+
if (result.ok)
|
|
1151
|
+
void mcpStateCatalog.refresh().catch(() => { });
|
|
1152
|
+
return result;
|
|
1153
|
+
}
|
|
934
1154
|
async function mcpmAdd(args) {
|
|
935
1155
|
const p = await ensurePaths();
|
|
936
1156
|
const serverName = String(args.serverName || '').trim();
|
|
@@ -1057,13 +1277,26 @@ export function createMcpManager(deps) {
|
|
|
1057
1277
|
// 已有发射路径的键 —— 其余未知键的保真需要值保持式序列化(裸布尔/数字经
|
|
1058
1278
|
// unquote/yq 往返会漂成字符串),不在此处理,宁可如实丢弃也不写错类型。
|
|
1059
1279
|
let curRaw = null;
|
|
1280
|
+
let oldContent = null;
|
|
1060
1281
|
try {
|
|
1061
|
-
|
|
1282
|
+
oldContent = await readPatch(oldAbs);
|
|
1283
|
+
}
|
|
1284
|
+
catch { /* 旧文件读不了时按建模字段重建,与原行为一致 */ }
|
|
1285
|
+
if (oldContent !== null) {
|
|
1286
|
+
// 并发编辑:界面读到的补丁层指纹对不上就拒绝,别把另一个窗口的改动整份覆盖掉。
|
|
1287
|
+
// 不传 expectedStamp = 旧行为(脚本 / 模型工具不受影响)。
|
|
1288
|
+
const conflict = stampConflict(stampOfText(oldContent), args && args.expectedStamp);
|
|
1289
|
+
if (conflict)
|
|
1290
|
+
return conflict;
|
|
1291
|
+
curRaw = parseRows(oldContent).rows.find((r) => r.id === id);
|
|
1062
1292
|
if (curRaw && curRaw.config && curRaw.config.toolCallTimeoutMs != null) {
|
|
1063
1293
|
row.toolCallTimeoutMs = curRaw.config.toolCallTimeoutMs;
|
|
1064
1294
|
}
|
|
1065
1295
|
}
|
|
1066
|
-
|
|
1296
|
+
else if (args && args.expectedStamp !== undefined) {
|
|
1297
|
+
// 带了指纹却读不到旧文件:无从校验,按失败处理,不静默放行。
|
|
1298
|
+
return { ok: false, error: '读取补丁失败,无法校验是否被并发修改:' + oldAbs };
|
|
1299
|
+
}
|
|
1067
1300
|
// 打码值**不能入库**:编辑弹窗的密钥框预填的就是打码值(界面拿到的行本身就是打码的),
|
|
1068
1301
|
// 所以这里必须拿旧真值顶替,否则"改个别的字段再保存"就把真密钥覆盖成 `••••••`
|
|
1069
1302
|
// —— 本机 2026-09-18 的 `TAVILY_API_KEY` 就是这样丢的。旧文件读不出来时 `curRaw`
|
|
@@ -1097,34 +1330,55 @@ export function createMcpManager(deps) {
|
|
|
1097
1330
|
}
|
|
1098
1331
|
const block = buildInsertBlock(row);
|
|
1099
1332
|
return withWriteLock(async () => {
|
|
1100
|
-
//
|
|
1101
|
-
//
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1333
|
+
// 改名要同时动三处:补丁本体、逐工具停用表、运行时场景快照。三者不在一个文件里,
|
|
1334
|
+
// 没有事务;顺序固定为「补丁先落 → 侧车后落」,侧车失败就把补丁回滚。反过来的旧顺序
|
|
1335
|
+
// (先写停用表 / 快照、再写补丁)在补丁失败时会留下“侧车已改名、补丁还是旧名”的半态。
|
|
1336
|
+
const renaming = serverName !== cur.serverName;
|
|
1337
|
+
// 侧车迁移先在内存里算好,不落盘。停用表新名下可能已有条目:合并去重,不能覆盖。
|
|
1338
|
+
let toolMapBefore = null;
|
|
1339
|
+
let toolMapNext = null;
|
|
1340
|
+
if (renaming) {
|
|
1341
|
+
const read = await readDisabledToolsResult(true);
|
|
1342
|
+
if (read.status === 'corrupt' || read.status === 'unreadable') {
|
|
1343
|
+
// 全局 unknown 仍然拦住所有 mcp__*;这里只如实说明“旧名字下的停用项没迁移”。
|
|
1344
|
+
guardNotes.push('MCP 停用表不可读,旧服务器名下的逐工具停用项未迁移');
|
|
1345
|
+
}
|
|
1346
|
+
else {
|
|
1347
|
+
const map = cloneDisabledTools(read.status === 'ok' ? read.value : emptyDisabledTools());
|
|
1348
|
+
if (map[cur.serverName]) {
|
|
1349
|
+
toolMapBefore = cloneDisabledTools(map);
|
|
1350
|
+
const merged = Array.from(new Set([...(map[serverName] || []), ...(map[cur.serverName] || [])]));
|
|
1351
|
+
map[serverName] = merged;
|
|
1352
|
+
delete map[cur.serverName];
|
|
1353
|
+
toolMapNext = map;
|
|
1109
1354
|
}
|
|
1110
|
-
catch (e) { /* non-fatal */ }
|
|
1111
|
-
setDisabledToolsCache(toolMap);
|
|
1112
1355
|
}
|
|
1113
|
-
|
|
1114
|
-
|
|
1356
|
+
}
|
|
1357
|
+
// 运行时快照的 mcp 停用表同样按 serverName 键 —— 改名不跟着改,退出场景整体回写时
|
|
1358
|
+
// 旧键成死键、新名工具的停用状态丢失(回到全启用)。同样先算、后写。
|
|
1359
|
+
let sceneNext = null;
|
|
1360
|
+
if (renaming) {
|
|
1115
1361
|
try {
|
|
1116
1362
|
const slice = await memoriesService.readArchiveSlice();
|
|
1117
1363
|
const mode = slice && slice.mode;
|
|
1118
1364
|
const mcpMap = mode && mode.snapshot && mode.snapshot.mcp;
|
|
1119
1365
|
if (mcpMap && Object.prototype.hasOwnProperty.call(mcpMap, cur.serverName)) {
|
|
1120
1366
|
const next = {};
|
|
1121
|
-
for (const [k, v] of Object.entries(mcpMap))
|
|
1122
|
-
|
|
1123
|
-
|
|
1367
|
+
for (const [k, v] of Object.entries(mcpMap)) {
|
|
1368
|
+
if (k !== cur.serverName)
|
|
1369
|
+
next[k] = v;
|
|
1370
|
+
}
|
|
1371
|
+
next[serverName] = Array.from(new Set([
|
|
1372
|
+
...(Array.isArray(mcpMap[serverName]) ? mcpMap[serverName] : []),
|
|
1373
|
+
...(Array.isArray(mcpMap[cur.serverName]) ? mcpMap[cur.serverName] : []),
|
|
1374
|
+
]));
|
|
1375
|
+
sceneNext = { ...mode, snapshot: { ...mode.snapshot, mcp: next } };
|
|
1124
1376
|
}
|
|
1125
1377
|
}
|
|
1126
1378
|
catch { /* 快照同步失败不阻断改名本身;残留与修复前一致 */ }
|
|
1127
1379
|
}
|
|
1380
|
+
// 补丁是事实源:先写它,并留下能把两份文件都还原的回滚闭包。
|
|
1381
|
+
let patchRollback = null;
|
|
1128
1382
|
if (oldAbs !== newAbs) {
|
|
1129
1383
|
// Level migration: remove from the old file, insert into the new one.
|
|
1130
1384
|
// Not atomic, so keep the old content and restore it if the second
|
|
@@ -1138,8 +1392,18 @@ export function createMcpManager(deps) {
|
|
|
1138
1392
|
catch (e) {
|
|
1139
1393
|
return { ok: false, error: '写入失败: ' + message(e) };
|
|
1140
1394
|
}
|
|
1141
|
-
let
|
|
1142
|
-
|
|
1395
|
+
let beforeNew;
|
|
1396
|
+
try {
|
|
1397
|
+
beforeNew = await readPatch(newAbs);
|
|
1398
|
+
}
|
|
1399
|
+
catch (e) {
|
|
1400
|
+
try {
|
|
1401
|
+
await writePatch(oldAbs, origOld);
|
|
1402
|
+
}
|
|
1403
|
+
catch (e2) { /* best effort */ }
|
|
1404
|
+
return { ok: false, error: '读取目标补丁失败(已回滚): ' + message(e) };
|
|
1405
|
+
}
|
|
1406
|
+
let c2 = beforeNew;
|
|
1143
1407
|
c2 = appendBlock(c2, block);
|
|
1144
1408
|
if (cur.disabled)
|
|
1145
1409
|
c2 = appendBlock(c2, buildDisableBlock(id, true));
|
|
@@ -1161,18 +1425,97 @@ export function createMcpManager(deps) {
|
|
|
1161
1425
|
catch (e2) { /* best effort */ }
|
|
1162
1426
|
return { ok: false, error: '写入失败(已回滚): ' + message(e) };
|
|
1163
1427
|
}
|
|
1428
|
+
patchRollback = async () => {
|
|
1429
|
+
const errors = [];
|
|
1430
|
+
try {
|
|
1431
|
+
await writePatch(newAbs, beforeNew);
|
|
1432
|
+
}
|
|
1433
|
+
catch (e) {
|
|
1434
|
+
errors.push('恢复目标补丁失败:' + message(e));
|
|
1435
|
+
}
|
|
1436
|
+
try {
|
|
1437
|
+
await writePatch(oldAbs, origOld);
|
|
1438
|
+
}
|
|
1439
|
+
catch (e) {
|
|
1440
|
+
errors.push('恢复原补丁失败:' + message(e));
|
|
1441
|
+
}
|
|
1442
|
+
if (errors.length)
|
|
1443
|
+
throw new Error(errors.join(';'));
|
|
1444
|
+
};
|
|
1164
1445
|
}
|
|
1165
1446
|
else {
|
|
1166
|
-
let
|
|
1167
|
-
|
|
1168
|
-
|
|
1447
|
+
let before;
|
|
1448
|
+
try {
|
|
1449
|
+
before = await readPatch(newAbs);
|
|
1450
|
+
}
|
|
1451
|
+
catch (e) {
|
|
1452
|
+
return { ok: false, error: '读取补丁失败: ' + message(e) };
|
|
1453
|
+
}
|
|
1454
|
+
let c = removeEntryAll(before, id);
|
|
1169
1455
|
c = appendBlock(c, block);
|
|
1170
1456
|
if (cur.disabled)
|
|
1171
1457
|
c = appendBlock(c, buildDisableBlock(id, true));
|
|
1172
1458
|
const guard = duplicateGuard(before, c);
|
|
1173
1459
|
if (guard)
|
|
1174
1460
|
return guard;
|
|
1175
|
-
|
|
1461
|
+
try {
|
|
1462
|
+
await writePatch(newAbs, c);
|
|
1463
|
+
}
|
|
1464
|
+
catch (e) {
|
|
1465
|
+
return { ok: false, error: '写入失败: ' + message(e) };
|
|
1466
|
+
}
|
|
1467
|
+
patchRollback = async () => { await writePatch(newAbs, before); };
|
|
1468
|
+
}
|
|
1469
|
+
const rollbackPatch = async () => {
|
|
1470
|
+
if (patchRollback === null)
|
|
1471
|
+
return null;
|
|
1472
|
+
try {
|
|
1473
|
+
await patchRollback();
|
|
1474
|
+
return null;
|
|
1475
|
+
}
|
|
1476
|
+
catch (e) {
|
|
1477
|
+
return message(e);
|
|
1478
|
+
}
|
|
1479
|
+
};
|
|
1480
|
+
// 停用表迁移:写失败 → 回滚补丁;此时没有别的侧车已被改动。
|
|
1481
|
+
if (toolMapNext) {
|
|
1482
|
+
try {
|
|
1483
|
+
await writeJsonFile(mcpSidecar(MCP_DISABLED_TOOLS_FILE), toolMapNext);
|
|
1484
|
+
}
|
|
1485
|
+
catch (e) {
|
|
1486
|
+
const patchError = await rollbackPatch();
|
|
1487
|
+
return {
|
|
1488
|
+
ok: false,
|
|
1489
|
+
error: '逐工具停用项迁移失败(改名已回滚):' + message(e)
|
|
1490
|
+
+ (patchError ? ';补丁回滚也失败:' + patchError : ''),
|
|
1491
|
+
};
|
|
1492
|
+
}
|
|
1493
|
+
setDisabledToolsCache(toolMapNext);
|
|
1494
|
+
}
|
|
1495
|
+
// 运行时快照迁移最后写:失败时把停用表与补丁一起还原。
|
|
1496
|
+
if (sceneNext) {
|
|
1497
|
+
try {
|
|
1498
|
+
await memoriesService.patchIndex({ mode: sceneNext });
|
|
1499
|
+
}
|
|
1500
|
+
catch (e) {
|
|
1501
|
+
const errors = [];
|
|
1502
|
+
if (toolMapNext && toolMapBefore) {
|
|
1503
|
+
try {
|
|
1504
|
+
await writeJsonFile(mcpSidecar(MCP_DISABLED_TOOLS_FILE), toolMapBefore);
|
|
1505
|
+
setDisabledToolsCache(toolMapBefore);
|
|
1506
|
+
}
|
|
1507
|
+
catch (e2) {
|
|
1508
|
+
errors.push('停用表恢复失败:' + message(e2));
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
const patchError = await rollbackPatch();
|
|
1512
|
+
return {
|
|
1513
|
+
ok: false,
|
|
1514
|
+
error: '运行时快照迁移失败(改名已回滚):' + message(e)
|
|
1515
|
+
+ (errors.length ? ';' + errors.join(';') : '')
|
|
1516
|
+
+ (patchError ? ';补丁回滚也失败:' + patchError : ''),
|
|
1517
|
+
};
|
|
1518
|
+
}
|
|
1176
1519
|
}
|
|
1177
1520
|
return { ok: true, ...(guardNotes.length ? { warning: guardNotes.join(';') } : {}) };
|
|
1178
1521
|
});
|
|
@@ -1224,15 +1567,21 @@ export function createMcpManager(deps) {
|
|
|
1224
1567
|
const levelFilter = args.level === 'global' ? 'global' : args.level === 'project' ? 'project' : null;
|
|
1225
1568
|
return withWriteLock(async () => {
|
|
1226
1569
|
const changed = [];
|
|
1570
|
+
// 静默 `continue` 会让"两层都切了"变成半真半假:一层补丁不可读时用户以为全部生效,
|
|
1571
|
+
// 实际只改了一层。失败按层级收集,最后按"全失败 / 部分失败 / 全成功"分三档回执。
|
|
1572
|
+
const failures = [];
|
|
1573
|
+
let attempted = 0;
|
|
1227
1574
|
for (const level of ['project', 'global']) {
|
|
1228
1575
|
if (levelFilter && levelFilter !== level)
|
|
1229
1576
|
continue;
|
|
1577
|
+
attempted++;
|
|
1230
1578
|
const abs = level === 'project' ? p.projectPatch : p.globalPatch;
|
|
1231
1579
|
let c = '';
|
|
1232
1580
|
try {
|
|
1233
1581
|
c = await readPatch(abs);
|
|
1234
1582
|
}
|
|
1235
1583
|
catch (e) {
|
|
1584
|
+
failures.push(level + ' 层补丁读取失败:' + message(e));
|
|
1236
1585
|
continue;
|
|
1237
1586
|
}
|
|
1238
1587
|
const { rows } = parseRows(c);
|
|
@@ -1252,9 +1601,20 @@ export function createMcpManager(deps) {
|
|
|
1252
1601
|
c = appendBlock(c, buildDisableBlock(row.id, true));
|
|
1253
1602
|
}
|
|
1254
1603
|
}
|
|
1255
|
-
|
|
1604
|
+
try {
|
|
1605
|
+
await writePatch(abs, c);
|
|
1606
|
+
}
|
|
1607
|
+
catch (e) {
|
|
1608
|
+
failures.push(level + ' 层补丁写入失败:' + message(e));
|
|
1609
|
+
continue;
|
|
1610
|
+
}
|
|
1256
1611
|
}
|
|
1257
|
-
|
|
1612
|
+
if (failures.length === 0)
|
|
1613
|
+
return { ok: true, enabled, changed };
|
|
1614
|
+
const detail = failures.join(';');
|
|
1615
|
+
if (failures.length >= attempted)
|
|
1616
|
+
return { ok: false, error: '所有补丁都未能改写:' + detail, changed };
|
|
1617
|
+
return { ok: true, enabled, changed, partial: true, warning: '部分层级未能改写:' + detail };
|
|
1258
1618
|
});
|
|
1259
1619
|
}
|
|
1260
1620
|
/** 一份补丁内容里某个 id 的**生效**启停值(insert 行自带的 flag 与顶层覆盖块合并后)。 */
|
|
@@ -1291,6 +1651,11 @@ export function createMcpManager(deps) {
|
|
|
1291
1651
|
// pre-restart state is captured and restored.
|
|
1292
1652
|
const wasDisabled = await isRowDisabled(id, level);
|
|
1293
1653
|
const warnings = [];
|
|
1654
|
+
// 0.21.2:先落"重启进行中"标记。它存在的唯一理由是给**崩溃后**的收尾留线索 ——
|
|
1655
|
+
// 补丁里阶段 1 留下的 disabled:true 与「用户主动停用」完全同形,没有这个标记就再也
|
|
1656
|
+
// 分不出来(进程在 ~11s 等待窗口里被杀 → 服务器永久停在停用态、无任何界面能解释)。
|
|
1657
|
+
// best-effort:写失败只 warn,重启照常进行。
|
|
1658
|
+
await writeMcpRestartMarker({ server: id, level, phase: 1, pid: process.pid, bootId: deps.bootId() });
|
|
1294
1659
|
// Phase 1 write (short lock): force-disable. Stale disable overrides are
|
|
1295
1660
|
// cleared first so duplicate blocks never accumulate.
|
|
1296
1661
|
await withWriteLock(async () => {
|
|
@@ -1315,6 +1680,9 @@ export function createMcpManager(deps) {
|
|
|
1315
1680
|
await wait(1000);
|
|
1316
1681
|
}
|
|
1317
1682
|
finally {
|
|
1683
|
+
// 阶段 2 也标一下:收尾方(启动对账)据此知道"写回走到一半",恢复时要重新读有效状态
|
|
1684
|
+
// 而不是照抄快照。与阶段 1 同一个理由:这段时间里被杀同样看不出状态。
|
|
1685
|
+
await writeMcpRestartMarker({ server: id, level, phase: 2, pid: process.pid, bootId: deps.bootId() });
|
|
1318
1686
|
// Phase 2 write (short lock): restore the pre-restart state. The polling
|
|
1319
1687
|
// waits deliberately run OUTSIDE the write lock — holding the global
|
|
1320
1688
|
// write lock for up to ~11s stalled every other write op.
|
|
@@ -1356,6 +1724,11 @@ export function createMcpManager(deps) {
|
|
|
1356
1724
|
}
|
|
1357
1725
|
else
|
|
1358
1726
|
await wait(1500);
|
|
1727
|
+
// 走完了(含恢复失败那条 catch 之后)—— 清理"重启进行中"标记。
|
|
1728
|
+
// **恢复失败也清**:那时补丁里的停用态要么是用户显式选择(护栏那一支)、要么已经是
|
|
1729
|
+
// 恢复不动的既成事实,留着标记只会让下一次启动为一个无法自动处理的现场反复报警。
|
|
1730
|
+
// 真正的收尾信号是上面那条 warning(已经回给界面了)。
|
|
1731
|
+
await clearMcpRestartMarker();
|
|
1359
1732
|
return warnings.length ? { ok: true, warning: warnings.join(';') } : { ok: true };
|
|
1360
1733
|
}
|
|
1361
1734
|
async function mcpmRemove(args) {
|
|
@@ -1366,6 +1739,7 @@ export function createMcpManager(deps) {
|
|
|
1366
1739
|
const abs = level === 'global' ? p.globalPatch : p.projectPatch;
|
|
1367
1740
|
const removedScenes = [];
|
|
1368
1741
|
const skippedLockedScenes = [];
|
|
1742
|
+
const cleanupWarnings = [];
|
|
1369
1743
|
await withWriteLock(async () => {
|
|
1370
1744
|
// serverName 在锁内取:场景档案(mcp / mcpNotes)与两张工具侧车表都按它键,
|
|
1371
1745
|
// 补丁条目 id 是另一套;锁外读会和并发写竞态。
|
|
@@ -1441,14 +1815,20 @@ export function createMcpManager(deps) {
|
|
|
1441
1815
|
try {
|
|
1442
1816
|
// 停用表与「已知工具」也按 serverName 键:整台没了,键留着只会喂出错的口径
|
|
1443
1817
|
// (补集里多出一台永远凑不上的服务器的旧工具名)。
|
|
1444
|
-
const
|
|
1445
|
-
if (
|
|
1446
|
-
|
|
1447
|
-
await writeJsonFile(mcpSidecar(MCP_DISABLED_TOOLS_FILE), toolMap);
|
|
1448
|
-
setDisabledToolsCache(toolMap);
|
|
1449
|
-
await applyToolRestrictions();
|
|
1818
|
+
const read = await readDisabledToolsResult(true);
|
|
1819
|
+
if (read.status === 'corrupt' || read.status === 'unreadable') {
|
|
1820
|
+
cleanupWarnings.push('MCP 停用表不可读,未清理该服务器名下的逐工具停用项');
|
|
1450
1821
|
}
|
|
1451
|
-
|
|
1822
|
+
else {
|
|
1823
|
+
const toolMap = cloneDisabledTools(read.status === 'ok' ? read.value : emptyDisabledTools());
|
|
1824
|
+
if (Object.prototype.hasOwnProperty.call(toolMap, serverName)) {
|
|
1825
|
+
delete toolMap[serverName];
|
|
1826
|
+
await writeJsonFile(mcpSidecar(MCP_DISABLED_TOOLS_FILE), toolMap);
|
|
1827
|
+
setDisabledToolsCache(toolMap);
|
|
1828
|
+
await applyToolRestrictions();
|
|
1829
|
+
}
|
|
1830
|
+
}
|
|
1831
|
+
const knownMap = Object.assign(Object.create(null), await readKnownMcpTools(true));
|
|
1452
1832
|
if (Object.prototype.hasOwnProperty.call(knownMap, serverName)) {
|
|
1453
1833
|
delete knownMap[serverName];
|
|
1454
1834
|
await writeKnownMcpTools(knownMap);
|
|
@@ -1465,7 +1845,7 @@ export function createMcpManager(deps) {
|
|
|
1465
1845
|
// 等 loader 把条目摘掉。等待放在**写锁外**(同 mcpm-restart:锁内等待会卡住其他写)。
|
|
1466
1846
|
// 宿主清单偶尔停在上一帧,等不到**不算失败** —— 补丁条目确实已经删了,宿主侧收敛有
|
|
1467
1847
|
// 自己的节奏;但要说出来,否则用户对着残留的 include:* 行只会以为插件没删干净。
|
|
1468
|
-
const warnings = [];
|
|
1848
|
+
const warnings = [...cleanupWarnings];
|
|
1469
1849
|
if (pluginInventory) {
|
|
1470
1850
|
const gone = await waitFor(async () => (await liveEntry(id)) === null, LOADER_STATE_WAIT_MS, LOADER_STATE_POLL_MS);
|
|
1471
1851
|
if (!gone)
|
|
@@ -1574,9 +1954,21 @@ export function createMcpManager(deps) {
|
|
|
1574
1954
|
if (outDir)
|
|
1575
1955
|
await mkdir(outDir, { recursive: true });
|
|
1576
1956
|
await writeJsonFile(abs, payload);
|
|
1957
|
+
// 导出文件含明文 env / headers 凭据:POSIX 上收窄到 0600。Windows 的 chmod 基本
|
|
1958
|
+
// 是 no-op(ACL 由目录继承),跳过即可;收窄失败也不抹掉"已经写出去"这个事实。
|
|
1959
|
+
if (process.platform !== 'win32') {
|
|
1960
|
+
try {
|
|
1961
|
+
await chmod(abs, 0o600);
|
|
1962
|
+
}
|
|
1963
|
+
catch { /* 文件已落盘,权限收紧失败不改变结果 */ }
|
|
1964
|
+
}
|
|
1577
1965
|
savedTo = abs;
|
|
1578
1966
|
}
|
|
1579
|
-
catch (e) {
|
|
1967
|
+
catch (e) {
|
|
1968
|
+
// 落盘失败必须让调用方看得见:只返回 ok:true + savedTo:null 的隐式契约让
|
|
1969
|
+
// "没写出去"和"写好了"在只看 ok 的调用方眼里完全一样(审查 N4)。
|
|
1970
|
+
return { ok: false, error: '落盘失败:' + message(e), json, savedTo: null };
|
|
1971
|
+
}
|
|
1580
1972
|
return { ok: true, json, savedTo };
|
|
1581
1973
|
}
|
|
1582
1974
|
async function mcpmImport(args) {
|
|
@@ -1780,10 +2172,21 @@ export function createMcpManager(deps) {
|
|
|
1780
2172
|
* 先确保目录、再在写锁下落盘、然后更新 TTL 缓存、最后重排工具可见性。
|
|
1781
2173
|
* 顺序有意义:缓存必须在落盘成功之后才更新(失败时不能留下"已生效"的假象)。
|
|
1782
2174
|
*/
|
|
1783
|
-
async function writeDisabledTools(entries) {
|
|
2175
|
+
async function writeDisabledTools(entries, options = {}) {
|
|
2176
|
+
const reason = options.reason ?? 'scene-apply';
|
|
1784
2177
|
await deps.ensurePaths();
|
|
1785
2178
|
await deps.withWriteLock(async () => {
|
|
1786
|
-
await
|
|
2179
|
+
const read = await readDisabledToolsResult(true);
|
|
2180
|
+
if (read.status === 'corrupt' || read.status === 'unreadable') {
|
|
2181
|
+
if (reason !== 'scene-restore') {
|
|
2182
|
+
throw new Error(`MCP 停用表${read.status === 'corrupt' ? '损坏' : '不可读'},已拒绝场景写入:${read.error}`);
|
|
2183
|
+
}
|
|
2184
|
+
// scene-restore 写的是进入场景前拍下的快照;这份快照本身可信,但**落点**现在
|
|
2185
|
+
// 已经损坏 / 不可读。覆盖之前必须先留住原文件 —— 否则一次回滚就把现场洗掉,
|
|
2186
|
+
// 用户再也查不出为什么会变成这样。取证失败则拒绝回滚(宁可场景退不出去)。
|
|
2187
|
+
await ensureDisabledToolsForensics();
|
|
2188
|
+
}
|
|
2189
|
+
await writeJsonFile(mcpSidecar(MCP_DISABLED_TOOLS_FILE), cloneDisabledTools(entries));
|
|
1787
2190
|
});
|
|
1788
2191
|
setDisabledToolsCache(entries);
|
|
1789
2192
|
scheduleToolRestrictions();
|
|
@@ -1794,16 +2197,20 @@ export function createMcpManager(deps) {
|
|
|
1794
2197
|
*/
|
|
1795
2198
|
async function updateNotes(mutate) {
|
|
1796
2199
|
await deps.withWriteLock(async () => {
|
|
1797
|
-
const
|
|
2200
|
+
const read = await readJsonFileStrict(mcpSidecar(MCP_NOTES_FILE));
|
|
2201
|
+
if (read.status === 'corrupt' || read.status === 'unreadable') {
|
|
2202
|
+
throw new Error(`MCP 备注文件${read.status === 'corrupt' ? '损坏' : '不可读'},已拒绝写入:${read.error}`);
|
|
2203
|
+
}
|
|
2204
|
+
const map = Object.assign({}, normalizeNotes(read.status === 'ok' ? read.value : null));
|
|
1798
2205
|
mutate(map);
|
|
1799
2206
|
await deps.ensurePaths();
|
|
1800
2207
|
await writeJsonFile(mcpSidecar(MCP_NOTES_FILE), map);
|
|
1801
2208
|
notesCache = { at: Date.now(), value: map };
|
|
1802
2209
|
});
|
|
1803
2210
|
}
|
|
1804
|
-
/**
|
|
2211
|
+
/** 启动预热:先完成首次读(建立 ok / unknown),再应用可见性。读失败不抛给调用方。 */
|
|
1805
2212
|
async function warmUp() {
|
|
1806
|
-
await
|
|
2213
|
+
await readDisabledToolsResult();
|
|
1807
2214
|
await applyToolRestrictions();
|
|
1808
2215
|
}
|
|
1809
2216
|
/** 卸载清理(那六行随可见性段搬进 tool-visibility.ts,这里只留出口接线)。 */
|
|
@@ -1827,20 +2234,32 @@ export function createMcpManager(deps) {
|
|
|
1827
2234
|
'mcpm-restart': mcpmRestart,
|
|
1828
2235
|
'mcpm-remove': mcpmRemove,
|
|
1829
2236
|
'mcpm-compact': mcpmCompact,
|
|
2237
|
+
'mcpm-disabled-reset': mcpmDisabledReset,
|
|
1830
2238
|
'mcpm-export': mcpmExport,
|
|
1831
2239
|
'mcpm-import': mcpmImport,
|
|
1832
2240
|
'mcpm-parse-patch': mcpmParsePatch,
|
|
1833
2241
|
},
|
|
1834
2242
|
readDisabledTools,
|
|
2243
|
+
readDisabledToolsResult,
|
|
2244
|
+
disabledToolsState,
|
|
2245
|
+
disabledToolsIssue,
|
|
1835
2246
|
readKnownMcpTools,
|
|
1836
2247
|
readNotes,
|
|
1837
2248
|
writeDisabledTools,
|
|
1838
2249
|
updateNotes,
|
|
1839
2250
|
mcpmListView,
|
|
1840
2251
|
configuredServerNames,
|
|
2252
|
+
configuredServerStates,
|
|
2253
|
+
patchServerRows,
|
|
1841
2254
|
mcpmRowsWithNotes,
|
|
1842
2255
|
readPluginSettings,
|
|
1843
|
-
isToolDisabled: (name) =>
|
|
2256
|
+
isToolDisabled: (name) => {
|
|
2257
|
+
// 防御性兜底:unknown 时 mcp__* 一律视为停用。守卫还会先看 disabledToolsState;
|
|
2258
|
+
// 这里再兜一层,避免以后有人只读 snapshot 就又回到 fail-open。
|
|
2259
|
+
if (disabledToolsState() === 'unknown')
|
|
2260
|
+
return String(name).startsWith(MCP_TOOL_PREFIX);
|
|
2261
|
+
return isToolDisabledIn(disabledToolsSnapshot(), String(name));
|
|
2262
|
+
},
|
|
1844
2263
|
/** 停用表的两个口径(读 TTL 缓存;功能总览用):`*` 通配的台数 + 单独停用的工具数。 */
|
|
1845
2264
|
disabledToolSummary: () => {
|
|
1846
2265
|
const map = disabledToolsSnapshot();
|
|
@@ -1853,12 +2272,24 @@ export function createMcpManager(deps) {
|
|
|
1853
2272
|
servers += 1;
|
|
1854
2273
|
tools += list.filter((name) => name !== '*').length;
|
|
1855
2274
|
}
|
|
1856
|
-
|
|
2275
|
+
// unknown 时两个计数都只是"缓存里剩的东西",不能当事实报;调用方据此换一句话。
|
|
2276
|
+
return { servers, tools, state: disabledToolsState(), issue: disabledToolsIssue() };
|
|
1857
2277
|
},
|
|
1858
2278
|
warmUp,
|
|
1859
2279
|
scheduleToolRestrictions,
|
|
1860
2280
|
attachAgent,
|
|
1861
2281
|
detachAgent,
|
|
1862
2282
|
dispose,
|
|
2283
|
+
// 0.21.2:重启中断收尾用的两个读口。isRowDisabled 每次重新读盘(不走缓存)——
|
|
2284
|
+
// 收尾要判断「阶段 1 的强制停用现在还是不是生效值」,缓存值会把"用户刚改过"读成"没人动"。
|
|
2285
|
+
isRowDisabledPublic: isRowDisabled,
|
|
2286
|
+
effectiveDisabledIn: (content, id) => {
|
|
2287
|
+
try {
|
|
2288
|
+
return effectiveDisabledIn(content, id);
|
|
2289
|
+
}
|
|
2290
|
+
catch {
|
|
2291
|
+
return null;
|
|
2292
|
+
}
|
|
2293
|
+
},
|
|
1863
2294
|
};
|
|
1864
2295
|
}
|