dsh-mcp-ops 0.1.5 → 0.2.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/lib/service.js CHANGED
@@ -51,6 +51,7 @@ import { Remote, TypertRemoteService } from "@deepseek-ai/dsh-typert-protocol";
51
51
  import { existsSync, mkdirSync, readFileSync, renameSync } from "node:fs";
52
52
  import { dirname, join } from "node:path";
53
53
  import { withFileLock } from "@deepseek-ai/dsh-atomic-write";
54
+ import { createScope } from "@deepseek-ai/dsh-scope";
54
55
  import { normalizeEntry, serverEntries } from "./files/mcp-json.js";
55
56
  import { defaultWriteTarget } from "./files/locations.js";
56
57
  import { effectiveRow } from "./models.js";
@@ -204,7 +205,11 @@ class McpOpsService extends (_a = TypertRemoteService, _list_dec = [Remote], _re
204
205
  this.hostCtx = ctx;
205
206
  this.config = config;
206
207
  this.mounts = new MountManager(ctx);
207
- this.toolControl = new ToolControl(ctx, { store: this.toolStore() });
208
+ this.toolControl = new ToolControl(ctx, {
209
+ store: this.toolStore(),
210
+ // 可见性掩码:官方 createScope 以 Agent 对象为作用域键,与 agent 自己的可见集同域。
211
+ maskScope: (scopeCtx, agent) => createScope(scopeCtx, agent, {})
212
+ });
208
213
  this.toolControl.install();
209
214
  this.ready = this.init().catch((error) => {
210
215
  this.initError = error instanceof Error ? error.message : String(error);
@@ -63,6 +63,9 @@ function pickTools(pool, query, limit, options = {}) {
63
63
  function compareName(a, b) {
64
64
  return a.name < b.name ? -1 : a.name > b.name ? 1 : 0;
65
65
  }
66
+ function comparePlainName(a, b) {
67
+ return a < b ? -1 : a > b ? 1 : 0;
68
+ }
66
69
  class HotSet {
67
70
  /** 名字 → 最近一次 touch 的序号(仅用于保持插入顺序,值不参与比较)。 */
68
71
  entries = /* @__PURE__ */ new Map();
@@ -120,24 +123,15 @@ class HotSet {
120
123
  return [...this.entries.keys()];
121
124
  }
122
125
  }
123
- function filterAssemblyTools(tools, input) {
124
- const resolve = input.serverOf ?? ((name) => serverOfToolName(name));
126
+ function deniedToolNames(tools, input) {
125
127
  const hot = new Set(input.hot);
126
- const others = [];
127
- const mcp = [];
128
- const servers = /* @__PURE__ */ new Set();
128
+ const denied = [];
129
129
  for (const tool of tools) {
130
- if (!tool.name.startsWith(TOOL_PREFIX)) {
131
- others.push(tool);
132
- continue;
133
- }
134
- if (!input.isEnabled(tool.name)) continue;
135
- const server = resolve(tool.name);
136
- if (server !== "") servers.add(server);
137
- if (input.mode === "full" || hot.has(tool.name)) mcp.push(tool);
130
+ if (typeof tool?.name !== "string" || !tool.name.startsWith(TOOL_PREFIX)) continue;
131
+ const visible = input.isEnabled(tool.name) && (input.mode === "full" || hot.has(tool.name));
132
+ if (!visible) denied.push(tool.name);
138
133
  }
139
- mcp.sort(compareName);
140
- return { tools: [...others, ...mcp], servers: [...servers].sort() };
134
+ return denied.sort(comparePlainName);
141
135
  }
142
136
  class ToolControl {
143
137
  /** host 上下文。 */
@@ -158,15 +152,27 @@ class ToolControl {
158
152
  installed = false;
159
153
  /** 装配钩子是否已经报过一次错(避免每轮刷屏)。 */
160
154
  warned = false;
155
+ /** 活跃 agent 的可见性掩码(键 = Agent 对象,与官方作用域键是同一个对象)。 */
156
+ masks = /* @__PURE__ */ new Map();
157
+ /** 掩码作用域工厂(缺省 = 不做 agent 级掩码,供零依赖单测)。 */
158
+ maskScope;
159
+ /** 声明了 `inject: ['tools']` 的上下文(掩码作用域建在它下面;异步就绪)。 */
160
+ maskHost;
161
+ /** host 就绪前收到 `agent/created` 的 agent(就绪后补挂)。 */
162
+ pending = /* @__PURE__ */ new Set();
163
+ /** 防重入:`restrict()` 自身会触发 `tools/change`。 */
164
+ refreshing = false;
161
165
  /**
162
166
  * @param ctx - host 上下文(需要 `tools` 服务;`system-prompt/assemble` 事件是可选的)。
163
- * @param options - 持久化出口与热集上限。
167
+ * @param options - 持久化出口、热集上限与掩码作用域工厂。
164
168
  */
165
169
  constructor(ctx, options = {}) {
166
170
  this.ctx = ctx;
167
171
  if (options.store !== void 0) this.store = options.store;
172
+ if (options.maskScope !== void 0) this.maskScope = options.maskScope;
168
173
  this.hotLimit = options.hotLimit ?? HOT_LIMIT;
169
174
  this.hot = new HotSet(this.hotLimit);
175
+ if (typeof ctx.plugin !== "function") this.maskHost = ctx;
170
176
  }
171
177
  /**
172
178
  * 注册检索工具与两个钩子(幂等)。
@@ -227,10 +233,55 @@ class ToolControl {
227
233
  const name = execution?.name;
228
234
  if (typeof name !== "string") return void 0;
229
235
  if (!isToolDeniedBySwitch(name, (candidate) => this.isEnabled(candidate))) return void 0;
236
+ if (!this.isVisibleTo(execution?.agent, name)) return void 0;
230
237
  return hostText(this.ctx, "toolDisabled", { name });
231
238
  }),
232
239
  "dsh-mcp-ops:disabled-guard"
233
240
  );
241
+ this.ctx.effect(() => this.ctx.on("agent/created", (payload) => {
242
+ try {
243
+ this.attachAgent(payload?.agent);
244
+ } catch (error) {
245
+ this.warnOnce(`agent mask failed: ${error instanceof Error ? error.message : String(error)}`);
246
+ }
247
+ }), "dsh-mcp-ops:agent-created");
248
+ this.ctx.effect(() => this.ctx.on("agent/disposed", (payload) => {
249
+ try {
250
+ this.detachAgent(payload?.agent);
251
+ } catch (error) {
252
+ this.warnOnce(`agent unmask failed: ${error instanceof Error ? error.message : String(error)}`);
253
+ }
254
+ }), "dsh-mcp-ops:agent-disposed");
255
+ this.ctx.effect(
256
+ () => this.ctx.on("tools/change", () => {
257
+ try {
258
+ this.refreshMasks();
259
+ } catch (error) {
260
+ this.warnOnce(`mask refresh failed: ${error instanceof Error ? error.message : String(error)}`);
261
+ }
262
+ }),
263
+ "dsh-mcp-ops:masks"
264
+ );
265
+ this.ctx.effect(() => () => {
266
+ this.disposeMasks();
267
+ }, "dsh-mcp-ops:masks-dispose");
268
+ const mountable = typeof this.ctx.plugin === "function";
269
+ if (this.maskScope !== void 0 && mountable) {
270
+ this.ctx.effect(() => {
271
+ const fiber = this.ctx.plugin(Object.assign((inner) => {
272
+ this.maskHost = inner;
273
+ const waiting = [...this.pending];
274
+ this.pending.clear();
275
+ for (const key of waiting) this.attachAgent(key);
276
+ return () => {
277
+ this.maskHost = void 0;
278
+ };
279
+ }, { inject: ["tools"] }));
280
+ return () => {
281
+ void fiber?.dispose?.();
282
+ };
283
+ }, "dsh-mcp-ops:mask-host");
284
+ }
234
285
  }
235
286
  /**
236
287
  * 用域表内容刷新内存态(每次 `sync()` 调用)。
@@ -244,6 +295,7 @@ class ToolControl {
244
295
  for (const [name, enabled] of switches) this.switches.set(name, enabled);
245
296
  if (mode === "search" || mode === "full") this.mode = mode;
246
297
  if (serverNames !== void 0) this.serverNames = [...serverNames];
298
+ this.refreshMasks();
247
299
  }
248
300
  /**
249
301
  * 取当前注入模式。
@@ -289,6 +341,7 @@ class ToolControl {
289
341
  else this.switches.set(name, previous);
290
342
  return failure("MCP_STORE_WRITE_FAILED", error instanceof Error ? error.message : String(error));
291
343
  }
344
+ this.refreshMasks();
292
345
  return { ok: true, value: { name, enabled: next } };
293
346
  }
294
347
  /**
@@ -310,6 +363,7 @@ class ToolControl {
310
363
  this.mode = previous;
311
364
  return failure("MCP_STORE_WRITE_FAILED", error instanceof Error ? error.message : String(error));
312
365
  }
366
+ this.refreshMasks();
313
367
  return { ok: true, value: { mode } };
314
368
  }
315
369
  /**
@@ -327,6 +381,7 @@ class ToolControl {
327
381
  if (pool.length === 0) return hostText(this.ctx, "searchDisabled");
328
382
  const picked = pickTools(pool, query, limit, { serverOf: (name) => this.serverNameOf(name) });
329
383
  for (const tool of picked) this.hot.touch(tool.name);
384
+ if (picked.length > 0) this.refreshMasks();
330
385
  if (picked.length === 0) return hostText(this.ctx, "searchMiss");
331
386
  const lines = picked.map((tool) => JSON.stringify({
332
387
  name: tool.name,
@@ -344,18 +399,14 @@ class ToolControl {
344
399
  return this.switches.get(name) !== false;
345
400
  }
346
401
  /**
347
- * 把一个装配按当前模式改写(过滤 + 排序 + 追加提示段落)。
402
+ * 把一个装配按当前模式改写(只追加提示段落)。
403
+ *
404
+ * **不再碰 `tools`**:`0.2.2-rc.1` 起官方用提供者结果覆盖瀑布返回值里的工具声明,在这里改
405
+ * 不会报错、也不会生效;可见性由 {@link refreshMasks} 的 restriction 负责。
348
406
  *
349
- * @param assembly - 装配对象(就地改写 `tools` / `sections`)。
407
+ * @param assembly - 装配对象(就地改写 `sections`)。
350
408
  */
351
409
  applyAssembly(assembly) {
352
- const filtered = filterAssemblyTools(assembly.tools ?? [], {
353
- mode: this.mode,
354
- isEnabled: (name) => this.isEnabled(name),
355
- hot: this.hot.names(),
356
- serverOf: (name) => this.serverNameOf(name)
357
- });
358
- assembly.tools = filtered.tools;
359
410
  if (this.mode !== "search") return;
360
411
  const servers = this.enabledServers();
361
412
  const sections = (assembly.sections ?? []).filter((section) => section.name !== SEARCH_SECTION_NAME);
@@ -398,6 +449,115 @@ class ToolControl {
398
449
  const all = this.ctx.tools.schemas();
399
450
  return all.filter((tool) => typeof tool?.name === "string" && tool.name.startsWith(TOOL_PREFIX)).sort(compareName);
400
451
  }
452
+ /**
453
+ * 为一个新建 agent 建掩码作用域,并立即同步一次可见性。
454
+ *
455
+ * `agent/created` 的载荷里是 Agent 对象本身,官方把它同时用作作用域键(`schemas(agent)` /
456
+ * 执行侧的 `scopeTarget(..., exec.agent)` 都按它分域),所以 `createScope(ctx, agent)` 建出来的
457
+ * 作用域与 agent 自己的可见集同键。
458
+ *
459
+ * @param agent - `agent/created` 载荷里的 Agent。
460
+ */
461
+ attachAgent(agent) {
462
+ if (this.maskScope === void 0) return;
463
+ if (agent === null || typeof agent !== "object" && typeof agent !== "function") return;
464
+ const key = agent;
465
+ if (this.maskHost === void 0) {
466
+ this.pending.add(key);
467
+ return;
468
+ }
469
+ if (this.masks.has(key)) {
470
+ this.syncMask(key);
471
+ return;
472
+ }
473
+ const scope = this.maskScope(this.maskHost, key);
474
+ this.masks.set(key, { scope, lift: () => {
475
+ }, denied: "" });
476
+ if (this.refreshing) this.syncMask(key);
477
+ else this.refreshMasks();
478
+ }
479
+ /**
480
+ * 撤掉一个 agent 的掩码(`agent/disposed`)。
481
+ * @param agent - `agent/disposed` 载荷里的 Agent。
482
+ */
483
+ detachAgent(agent) {
484
+ if (agent === null || typeof agent !== "object") return;
485
+ const key = agent;
486
+ const mask = this.masks.get(key);
487
+ if (mask === void 0) return;
488
+ this.masks.delete(key);
489
+ try {
490
+ mask.lift();
491
+ } catch {
492
+ }
493
+ void Promise.resolve(mask.scope.dispose()).catch(() => void 0);
494
+ }
495
+ /** 卸载时撤掉全部掩码(掩码作用域挂在全局 ctx 上,不会随本插件的 fiber 自动回收)。 */
496
+ disposeMasks() {
497
+ this.pending.clear();
498
+ for (const key of [...this.masks.keys()]) this.detachAgent(key);
499
+ }
500
+ /**
501
+ * 重算所有活跃 agent 的掩码。
502
+ *
503
+ * `restrict()` 自身会触发 `tools/change`,所以用 {@link refreshing} 防自激;名单没变就跳过重建。
504
+ */
505
+ refreshMasks() {
506
+ if (this.refreshing || this.masks.size === 0) return;
507
+ this.refreshing = true;
508
+ try {
509
+ for (const key of [...this.masks.keys()]) this.syncMask(key);
510
+ } finally {
511
+ this.refreshing = false;
512
+ }
513
+ }
514
+ /**
515
+ * 重算单个 agent 的 deny 名单;与上次相同就不动(避免每次 `tools/change` 都重建 restriction)。
516
+ * @param agent - 作用域键。
517
+ */
518
+ syncMask(agent) {
519
+ const mask = this.masks.get(agent);
520
+ if (mask === void 0) return;
521
+ const denied = this.deniedFor();
522
+ const signature = denied.join("\0");
523
+ if (signature === mask.denied) return;
524
+ try {
525
+ mask.lift();
526
+ } catch {
527
+ }
528
+ mask.lift = () => {
529
+ };
530
+ mask.denied = signature;
531
+ if (denied.length === 0) return;
532
+ mask.lift = mask.scope.ctx.tools.restrict({ deny: denied });
533
+ }
534
+ /**
535
+ * 某个工具此刻是否对给定 agent 可见(restriction 之后的口径)。
536
+ * @param agent - 作用域键(缺省 = 全局视图)。
537
+ * @param name - 公开工具名。
538
+ * @returns 可见为 true。
539
+ */
540
+ isVisibleTo(agent, name) {
541
+ const scope = agent !== null && typeof agent === "object" ? agent : void 0;
542
+ const schemas = this.ctx.tools.schemas(scope);
543
+ return schemas.some((tool) => tool?.name === name);
544
+ }
545
+ /**
546
+ * 当前应当被剔除的 MCP 工具名。
547
+ *
548
+ * 名单取自**全局注册表**({@link snapshot})而不是 `schemas(agent)`:作用域视图已经把上一次的
549
+ * restriction 算进去了,拿它反推会得到"没有要挡的"→ 撤掉 restriction → 工具又全冒出来(自噬)。
550
+ * 同时 `restrict()` 只接受"当前存在的全局工具名",全局视图正好满足这个前置条件。
551
+ *
552
+ * @returns 按名升序的工具名。
553
+ */
554
+ deniedFor() {
555
+ return deniedToolNames(this.snapshot(), {
556
+ mode: this.mode,
557
+ isEnabled: (name) => this.isEnabled(name),
558
+ hot: this.hot.names()
559
+ });
560
+ }
401
561
  /**
402
562
  * 只在调试开关下打印一次装配钩子的异常(钩子抛错会打断整轮提示词装配,绝不能外泄)。
403
563
  * @param message - 文本。
@@ -423,7 +583,7 @@ export {
423
583
  TOOL_PREFIX,
424
584
  TOOL_ROW_DESCRIPTION_CLIP,
425
585
  ToolControl,
426
- filterAssemblyTools,
586
+ deniedToolNames,
427
587
  isToolDeniedBySwitch,
428
588
  pickTools,
429
589
  scoreTool,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mcp-ops",
3
- "version": "0.1.5",
3
+ "version": "0.2.0",
4
4
  "description": "MCP 服务管理:增删改查与启停、连接测试、工具按需检索与全量注入、进程级与服务级环境变量(含密钥);MCP service management: CRUD, connection tests, tool injection, environment variables and secrets",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -41,8 +41,8 @@
41
41
  "plugin"
42
42
  ],
43
43
  "engines": {
44
- "node": "^22.19.0 || >=24.0.0",
45
- "dsh": "^0.2.0-rc.2"
44
+ "node": ">=24.9.0",
45
+ "dsh": "^0.2.2-rc.1"
46
46
  },
47
47
  "dsh": {
48
48
  "manifestVersion": 1,
@@ -68,20 +68,20 @@
68
68
  "registry": "https://registry.npmjs.org/"
69
69
  },
70
70
  "peerDependencies": {
71
- "@deepseek-ai/cordis": "^4.0.4 || ^4.0.5-alpha.1",
72
- "@deepseek-ai/cordis-plugin-include": "^1.0.9 || ^1.0.10-alpha.1",
73
- "@deepseek-ai/dsh": "^0.2.0-rc.2",
74
- "@deepseek-ai/dsh-atomic-write": "^0.2.0-rc.2",
75
- "@deepseek-ai/dsh-credentials": "^0.2.0-rc.2",
76
- "@deepseek-ai/dsh-mcp-client": "^0.2.0-rc.2",
77
- "@deepseek-ai/dsh-scope": "^0.2.0-rc.2",
78
- "@deepseek-ai/dsh-subprocess": "^0.2.0-rc.2",
79
- "@deepseek-ai/dsh-system-prompt": "^0.2.0-rc.2",
80
- "@deepseek-ai/dsh-tools": "^0.2.0-rc.2",
81
- "@deepseek-ai/dsh-typert-protocol": "^0.2.0-rc.2",
82
- "@deepseek-ai/schemastery": "^3.18.4 || ^3.18.5-alpha.1",
83
- "js-yaml": "^4.1.0",
84
- "zod": "^4.4.3"
71
+ "@deepseek-ai/cordis": "^4.1.0",
72
+ "@deepseek-ai/cordis-plugin-include": "^1.1.0",
73
+ "@deepseek-ai/dsh": "^0.2.2-rc.1",
74
+ "@deepseek-ai/dsh-atomic-write": "^0.2.2-rc.1",
75
+ "@deepseek-ai/dsh-credentials": "^0.2.2-rc.1",
76
+ "@deepseek-ai/dsh-mcp-client": "^0.2.2-rc.1",
77
+ "@deepseek-ai/dsh-scope": "^0.2.2-rc.1",
78
+ "@deepseek-ai/dsh-subprocess": "^0.2.2-rc.1",
79
+ "@deepseek-ai/dsh-system-prompt": "^0.2.2-rc.1",
80
+ "@deepseek-ai/dsh-tools": "^0.2.2-rc.1",
81
+ "@deepseek-ai/dsh-typert-protocol": "^0.2.2-rc.1",
82
+ "@deepseek-ai/schemastery": "^3.19.0",
83
+ "js-yaml": "^4.3.2",
84
+ "zod": "^4.6.7"
85
85
  },
86
86
  "scripts": {
87
87
  "build": "node scripts/build-host.mjs && node scripts/build-client.mjs && node scripts/check-client-bundle.mjs",