dsh-mcp-ops 0.2.1 → 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 +39 -12
- package/lib/client.js +282 -145
- package/lib/configs.js +21 -8
- package/lib/host-locales.js +2 -20
- package/lib/index.js +7 -1
- package/lib/mount.js +353 -146
- package/lib/oauth-key.js +21 -0
- package/lib/probe.js +38 -43
- package/lib/refs.js +0 -34
- package/lib/service.js +113 -216
- package/lib/sources/cordis-source.js +38 -17
- package/lib/system-ca.js +79 -0
- package/lib/tool-control.js +91 -271
- package/package.json +14 -15
- package/lib/oauth-web.js +0 -119
- package/lib/oauth.js +0 -228
package/lib/tool-control.js
CHANGED
|
@@ -1,160 +1,63 @@
|
|
|
1
|
+
import { normalizeInjectionMode } from "./configs.js";
|
|
1
2
|
import { hostText } from "./host-locales.js";
|
|
2
3
|
const TOOL_PREFIX = "mcp__";
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const SEARCH_SECTION_NAME = "mcp-tool-control";
|
|
4
|
+
const LOADER_TOOL_SUFFIX = "__load_tools";
|
|
5
|
+
const TOOL_ROW_DESCRIPTION_CLIP = 250;
|
|
6
6
|
function isToolDeniedBySwitch(name, isEnabled) {
|
|
7
7
|
if (!name.startsWith(TOOL_PREFIX)) return false;
|
|
8
8
|
return !isEnabled(name);
|
|
9
9
|
}
|
|
10
|
-
const DEFAULT_SEARCH_LIMIT = 8;
|
|
11
|
-
const MAX_SEARCH_LIMIT = 20;
|
|
12
|
-
const DESCRIPTION_CLIP = 220;
|
|
13
|
-
const TOOL_ROW_DESCRIPTION_CLIP = 250;
|
|
14
|
-
const PUBLIC_TOOL_NAME_RE = /^mcp__(.+)__(.+)$/;
|
|
15
10
|
function serverOfToolName(name) {
|
|
16
|
-
|
|
17
|
-
if (match === null) return "";
|
|
11
|
+
if (!name.startsWith(TOOL_PREFIX)) return "";
|
|
18
12
|
const rest = name.slice(TOOL_PREFIX.length);
|
|
19
|
-
const
|
|
20
|
-
return
|
|
13
|
+
const cut = rest.indexOf("__");
|
|
14
|
+
return cut <= 0 ? "" : rest.slice(0, cut);
|
|
21
15
|
}
|
|
22
16
|
function toolOfToolName(name) {
|
|
23
|
-
|
|
24
|
-
if (match === null) return "";
|
|
17
|
+
if (!name.startsWith(TOOL_PREFIX)) return "";
|
|
25
18
|
const rest = name.slice(TOOL_PREFIX.length);
|
|
26
|
-
const
|
|
27
|
-
return
|
|
19
|
+
const cut = rest.indexOf("__");
|
|
20
|
+
return cut <= 0 ? "" : rest.slice(cut + 2);
|
|
28
21
|
}
|
|
29
22
|
function serverOf(name, knownServers) {
|
|
23
|
+
const naive = serverOfToolName(name);
|
|
24
|
+
if (knownServers === void 0) return naive;
|
|
30
25
|
let best = "";
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (name.startsWith(`${TOOL_PREFIX}${candidate}__`)) best = candidate;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return best !== "" ? best : serverOfToolName(name);
|
|
38
|
-
}
|
|
39
|
-
function tokenizeQuery(query) {
|
|
40
|
-
return query.toLowerCase().split(/[^\p{L}\p{N}_]+/u).filter((token) => token !== "");
|
|
41
|
-
}
|
|
42
|
-
function scoreTool(tokens, name, description, options = {}) {
|
|
43
|
-
if (tokens.length === 0) return 0;
|
|
44
|
-
const resolve = options.serverOf ?? ((toolName) => serverOfToolName(toolName));
|
|
45
|
-
const server = resolve(name).toLowerCase();
|
|
46
|
-
const tool = toolOfToolName(name).toLowerCase();
|
|
47
|
-
const hay = (description ?? "").toLowerCase();
|
|
48
|
-
let score = 0;
|
|
49
|
-
for (const token of tokens) {
|
|
50
|
-
if (server.includes(token)) score += 2;
|
|
51
|
-
if (tool.includes(token)) score += 3;
|
|
52
|
-
if (hay.includes(token)) score += 1;
|
|
53
|
-
}
|
|
54
|
-
return score;
|
|
55
|
-
}
|
|
56
|
-
function pickTools(pool, query, limit, options = {}) {
|
|
57
|
-
const tokens = tokenizeQuery(query);
|
|
58
|
-
if (tokens.length === 0) {
|
|
59
|
-
return [...pool].sort(compareName).slice(0, limit);
|
|
60
|
-
}
|
|
61
|
-
return pool.map((tool) => ({ tool, score: scoreTool(tokens, tool.name, tool.description, options) })).filter((entry) => entry.score > 0).sort((a, b) => b.score - a.score || compareName(a.tool, b.tool)).slice(0, limit).map((entry) => entry.tool);
|
|
62
|
-
}
|
|
63
|
-
function compareName(a, b) {
|
|
64
|
-
return a.name < b.name ? -1 : a.name > b.name ? 1 : 0;
|
|
65
|
-
}
|
|
66
|
-
function comparePlainName(a, b) {
|
|
67
|
-
return a < b ? -1 : a > b ? 1 : 0;
|
|
68
|
-
}
|
|
69
|
-
class HotSet {
|
|
70
|
-
/** 名字 → 最近一次 touch 的序号(仅用于保持插入顺序,值不参与比较)。 */
|
|
71
|
-
entries = /* @__PURE__ */ new Map();
|
|
72
|
-
/** 单调递增序号。 */
|
|
73
|
-
clock = 0;
|
|
74
|
-
/** 上限。 */
|
|
75
|
-
limit;
|
|
76
|
-
/**
|
|
77
|
-
* @param limit - 上限(正数)。
|
|
78
|
-
*/
|
|
79
|
-
constructor(limit = HOT_LIMIT) {
|
|
80
|
-
this.limit = limit > 0 ? limit : HOT_LIMIT;
|
|
81
|
-
}
|
|
82
|
-
/** 当前大小。 */
|
|
83
|
-
get size() {
|
|
84
|
-
return this.entries.size;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* 标记一次使用(已存在则刷新新鲜度)。
|
|
88
|
-
* @param name - 工具名。
|
|
89
|
-
*/
|
|
90
|
-
touch(name) {
|
|
91
|
-
this.entries.delete(name);
|
|
92
|
-
this.entries.set(name, this.clock++);
|
|
93
|
-
while (this.entries.size > this.limit) {
|
|
94
|
-
const oldest = this.entries.keys().next().value;
|
|
95
|
-
if (oldest === void 0) break;
|
|
96
|
-
this.entries.delete(oldest);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* 是否在热集里。
|
|
101
|
-
* @param name - 工具名。
|
|
102
|
-
* @returns 在则为 true。
|
|
103
|
-
*/
|
|
104
|
-
has(name) {
|
|
105
|
-
return this.entries.has(name);
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* 移出一个名字。
|
|
109
|
-
* @param name - 工具名。
|
|
110
|
-
*/
|
|
111
|
-
delete(name) {
|
|
112
|
-
this.entries.delete(name);
|
|
113
|
-
}
|
|
114
|
-
/** 清空。 */
|
|
115
|
-
clear() {
|
|
116
|
-
this.entries.clear();
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* 热集里的名字(最旧 → 最新)。
|
|
120
|
-
* @returns 名字数组。
|
|
121
|
-
*/
|
|
122
|
-
names() {
|
|
123
|
-
return [...this.entries.keys()];
|
|
26
|
+
for (const candidate of knownServers) {
|
|
27
|
+
if (candidate.length <= best.length) continue;
|
|
28
|
+
if (name.startsWith(`${TOOL_PREFIX}${candidate}__`)) best = candidate;
|
|
124
29
|
}
|
|
30
|
+
return best === "" ? naive : best;
|
|
125
31
|
}
|
|
126
32
|
function deniedToolNames(tools, input) {
|
|
127
|
-
|
|
128
|
-
const denied = [];
|
|
129
|
-
for (const tool of tools) {
|
|
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);
|
|
133
|
-
}
|
|
134
|
-
return denied.sort(comparePlainName);
|
|
33
|
+
return tools.map((tool) => tool.name).filter((name) => isToolDeniedBySwitch(name, input.isEnabled)).sort(comparePlainName);
|
|
135
34
|
}
|
|
136
35
|
class ToolControl {
|
|
137
36
|
/** host 上下文。 */
|
|
138
37
|
ctx;
|
|
139
|
-
/**
|
|
38
|
+
/** 持久化出口(缺省不落库)。 */
|
|
140
39
|
store;
|
|
141
|
-
/** 热集。 */
|
|
142
|
-
hot;
|
|
143
|
-
/** 热集上限(`list()` 回给 UI)。 */
|
|
144
|
-
hotLimit;
|
|
145
40
|
/** 已知服务器名(`serverOf` 的集合匹配输入;由服务在每次 `sync()` 时刷新)。 */
|
|
146
41
|
serverNames = [];
|
|
42
|
+
/**
|
|
43
|
+
* **本插件实际注册**的服务器名。
|
|
44
|
+
*
|
|
45
|
+
* 官方 `ctx.mcpManager` 也会注册 `mcp__*` 工具;若开关按"所有 mcp__ 前缀"计算,会把官方
|
|
46
|
+
* manager 的 configured 项、组合层手写的 mcp-client 行一并挡掉。这里只认本插件 MountManager
|
|
47
|
+
* 当前存活的注册。
|
|
48
|
+
*/
|
|
49
|
+
ownServers;
|
|
147
50
|
/** 逐工具开关(只存显式值;缺席 = 启用)。 */
|
|
148
51
|
switches = /* @__PURE__ */ new Map();
|
|
149
|
-
/**
|
|
150
|
-
mode = "
|
|
52
|
+
/** 注入模式(= 注册项的 `toolLoading`;缺省与 `configs.DEFAULT_CONFIGS` 对齐为 `eager`)。 */
|
|
53
|
+
mode = "eager";
|
|
151
54
|
/** 是否已 install(幂等)。 */
|
|
152
55
|
installed = false;
|
|
153
|
-
/**
|
|
56
|
+
/** 钩子是否已经报过一次错(避免每轮刷屏)。 */
|
|
154
57
|
warned = false;
|
|
155
58
|
/** 活跃 agent 的可见性掩码(键 = Agent 对象,与官方作用域键是同一个对象)。 */
|
|
156
59
|
masks = /* @__PURE__ */ new Map();
|
|
157
|
-
/** 掩码作用域工厂(缺省 = 不做 agent
|
|
60
|
+
/** 掩码作用域工厂(缺省 = 不做 agent 级掩码)。 */
|
|
158
61
|
maskScope;
|
|
159
62
|
/** 声明了 `inject: ['tools']` 的上下文(掩码作用域建在它下面;异步就绪)。 */
|
|
160
63
|
maskHost;
|
|
@@ -163,75 +66,29 @@ class ToolControl {
|
|
|
163
66
|
/** 防重入:`restrict()` 自身会触发 `tools/change`。 */
|
|
164
67
|
refreshing = false;
|
|
165
68
|
/**
|
|
166
|
-
* @param ctx - host 上下文(需要 `tools`
|
|
167
|
-
* @param options -
|
|
69
|
+
* @param ctx - host 上下文(需要 `tools` 服务)。
|
|
70
|
+
* @param options - 持久化出口与掩码作用域工厂。
|
|
168
71
|
*/
|
|
169
72
|
constructor(ctx, options = {}) {
|
|
170
73
|
this.ctx = ctx;
|
|
171
74
|
if (options.store !== void 0) this.store = options.store;
|
|
172
75
|
if (options.maskScope !== void 0) this.maskScope = options.maskScope;
|
|
173
|
-
this.hotLimit = options.hotLimit ?? HOT_LIMIT;
|
|
174
|
-
this.hot = new HotSet(this.hotLimit);
|
|
175
76
|
if (typeof ctx.plugin !== "function") this.maskHost = ctx;
|
|
176
77
|
}
|
|
177
78
|
/**
|
|
178
|
-
*
|
|
79
|
+
* 注册守卫与掩码钩子(幂等)。
|
|
80
|
+
*
|
|
81
|
+
* 不再注册 `mcp_tool_search`、不再挂 `system-prompt/assemble`:按需加载与提示词目录都由
|
|
82
|
+
* 官方 client 提供。
|
|
179
83
|
*/
|
|
180
84
|
install() {
|
|
181
85
|
if (this.installed) return;
|
|
182
86
|
this.installed = true;
|
|
183
|
-
this.ctx.effect(() => this.ctx.tools.register({
|
|
184
|
-
name: SEARCH_TOOL_NAME,
|
|
185
|
-
description: hostText(this.ctx, "toolSearchDesc"),
|
|
186
|
-
parameters: {
|
|
187
|
-
type: "object",
|
|
188
|
-
properties: {
|
|
189
|
-
query: { type: "string", description: hostText(this.ctx, "toolSearchQuery") },
|
|
190
|
-
limit: { type: "number", description: hostText(this.ctx, "toolSearchLimit") }
|
|
191
|
-
},
|
|
192
|
-
required: ["query"],
|
|
193
|
-
additionalProperties: false
|
|
194
|
-
},
|
|
195
|
-
output: {
|
|
196
|
-
schema: {
|
|
197
|
-
type: "object",
|
|
198
|
-
properties: { text: { type: "string" } },
|
|
199
|
-
required: ["text"],
|
|
200
|
-
additionalProperties: false
|
|
201
|
-
},
|
|
202
|
-
render: (_args, value) => [{
|
|
203
|
-
type: "text",
|
|
204
|
-
text: typeof value?.text === "string" ? value.text : ""
|
|
205
|
-
}]
|
|
206
|
-
},
|
|
207
|
-
execute: async (args) => ({ text: this.search(args) })
|
|
208
|
-
}), "dsh-mcp-ops:tool-search");
|
|
209
|
-
this.ctx.effect(
|
|
210
|
-
() => this.ctx.on("system-prompt/assemble", (assembly, _context, next) => {
|
|
211
|
-
try {
|
|
212
|
-
this.applyAssembly(assembly);
|
|
213
|
-
} catch (error) {
|
|
214
|
-
this.warnOnce(`assemble hook failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
215
|
-
}
|
|
216
|
-
return next();
|
|
217
|
-
}),
|
|
218
|
-
"dsh-mcp-ops:assemble"
|
|
219
|
-
);
|
|
220
|
-
this.ctx.effect(
|
|
221
|
-
() => this.ctx.on("tools/result", (exec) => {
|
|
222
|
-
try {
|
|
223
|
-
const name = exec?.name;
|
|
224
|
-
if (typeof name === "string" && name.startsWith(TOOL_PREFIX)) this.hot.touch(name);
|
|
225
|
-
} catch (error) {
|
|
226
|
-
this.warnOnce(`result hook failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
227
|
-
}
|
|
228
|
-
}),
|
|
229
|
-
"dsh-mcp-ops:hot-tracking"
|
|
230
|
-
);
|
|
231
87
|
this.ctx.effect(
|
|
232
88
|
() => this.ctx.tools.guard((execution) => {
|
|
233
89
|
const name = execution?.name;
|
|
234
90
|
if (typeof name !== "string") return void 0;
|
|
91
|
+
if (!this.isOwnedTool(name)) return void 0;
|
|
235
92
|
if (!isToolDeniedBySwitch(name, (candidate) => this.isEnabled(candidate))) return void 0;
|
|
236
93
|
if (!this.isVisibleTo(execution?.agent, name)) return void 0;
|
|
237
94
|
return hostText(this.ctx, "toolDisabled", { name });
|
|
@@ -284,17 +141,20 @@ class ToolControl {
|
|
|
284
141
|
}
|
|
285
142
|
}
|
|
286
143
|
/**
|
|
287
|
-
*
|
|
144
|
+
* 用文件里的状态刷新内存态(每次 `sync()` 调用)。
|
|
288
145
|
*
|
|
289
146
|
* @param switches - 工具开关记录(键 = 公开工具名)。
|
|
290
|
-
* @param mode -
|
|
147
|
+
* @param mode - 注入模式;历史值(`search` / `full`)在这里归一,无法识别时保留当前值。
|
|
291
148
|
* @param serverNames - 已知服务器名(工具名归属判定用;省略则保留旧值)。
|
|
149
|
+
* @param ownServers - **本插件实际注册**的服务器名(deny 的边界;省略则保留旧值)。
|
|
292
150
|
*/
|
|
293
|
-
load(switches, mode, serverNames) {
|
|
151
|
+
load(switches, mode, serverNames, ownServers) {
|
|
294
152
|
this.switches.clear();
|
|
295
153
|
for (const [name, enabled] of switches) this.switches.set(name, enabled);
|
|
296
|
-
|
|
154
|
+
const normalized = normalizeInjectionMode(mode);
|
|
155
|
+
if (normalized !== void 0) this.mode = normalized;
|
|
297
156
|
if (serverNames !== void 0) this.serverNames = [...serverNames];
|
|
157
|
+
if (ownServers !== void 0) this.ownServers = new Set(ownServers);
|
|
298
158
|
this.refreshMasks();
|
|
299
159
|
}
|
|
300
160
|
/**
|
|
@@ -314,17 +174,21 @@ class ToolControl {
|
|
|
314
174
|
server: this.serverNameOf(tool.name),
|
|
315
175
|
tool: toolOfToolName(tool.name),
|
|
316
176
|
description: (tool.description ?? "").slice(0, TOOL_ROW_DESCRIPTION_CLIP),
|
|
317
|
-
enabled: this.isEnabled(tool.name)
|
|
318
|
-
hot: this.hot.has(tool.name)
|
|
177
|
+
enabled: this.isEnabled(tool.name)
|
|
319
178
|
}));
|
|
320
|
-
return {
|
|
179
|
+
return {
|
|
180
|
+
tools,
|
|
181
|
+
mode: this.mode,
|
|
182
|
+
// restrict() 是唯一执行入口;缺它时配置仍然保存,但不会生效。
|
|
183
|
+
enforcement: typeof this.ctx.tools.restrict === "function" && this.maskScope !== void 0 ? "mask" : "unavailable"
|
|
184
|
+
};
|
|
321
185
|
}
|
|
322
186
|
/**
|
|
323
187
|
* 设置一个工具的开关。
|
|
324
188
|
*
|
|
325
189
|
* @param name - 公开工具名(必须合 `mcp__<server>__<tool>` 文法)。
|
|
326
190
|
* @param enabled - 是否启用。
|
|
327
|
-
* @returns
|
|
191
|
+
* @returns 成功(附带该行)或失败。
|
|
328
192
|
*/
|
|
329
193
|
async set(name, enabled) {
|
|
330
194
|
if (typeof name !== "string" || serverOfToolName(name) === "" || toolOfToolName(name) === "") {
|
|
@@ -333,7 +197,6 @@ class ToolControl {
|
|
|
333
197
|
const next = enabled === true;
|
|
334
198
|
const previous = this.switches.get(name);
|
|
335
199
|
this.switches.set(name, next);
|
|
336
|
-
if (!next) this.hot.delete(name);
|
|
337
200
|
try {
|
|
338
201
|
await this.store?.putSwitch(name, next);
|
|
339
202
|
} catch (error) {
|
|
@@ -345,50 +208,26 @@ class ToolControl {
|
|
|
345
208
|
return { ok: true, value: { name, enabled: next } };
|
|
346
209
|
}
|
|
347
210
|
/**
|
|
348
|
-
*
|
|
211
|
+
* 切换注入模式(= 注册项的 `toolLoading`;服务侧 `sync()` 会因指纹变化差分重建)。
|
|
349
212
|
*
|
|
350
|
-
* @param mode -
|
|
213
|
+
* @param mode - 目标模式;历史值按 {@link normalizeInjectionMode} 归一。
|
|
351
214
|
* @returns 成功(附带模式)或失败。
|
|
352
215
|
*/
|
|
353
216
|
async setMode(mode) {
|
|
354
|
-
|
|
217
|
+
const normalized = normalizeInjectionMode(mode);
|
|
218
|
+
if (normalized === void 0) {
|
|
355
219
|
return failure("MCP_TOOL_INVALID_MODE", `invalid injection mode: ${JSON.stringify(mode ?? null)}`);
|
|
356
220
|
}
|
|
357
221
|
const previous = this.mode;
|
|
358
|
-
this.mode =
|
|
359
|
-
if (previous !== mode) this.hot.clear();
|
|
222
|
+
this.mode = normalized;
|
|
360
223
|
try {
|
|
361
|
-
await this.store?.putMode(
|
|
224
|
+
await this.store?.putMode(normalized);
|
|
362
225
|
} catch (error) {
|
|
363
226
|
this.mode = previous;
|
|
364
227
|
return failure("MCP_STORE_WRITE_FAILED", error instanceof Error ? error.message : String(error));
|
|
365
228
|
}
|
|
366
229
|
this.refreshMasks();
|
|
367
|
-
return { ok: true, value: { mode } };
|
|
368
|
-
}
|
|
369
|
-
/**
|
|
370
|
-
* 执行一次关键词检索(`mcp_tool_search` 的实现)。
|
|
371
|
-
*
|
|
372
|
-
* @param args - 模型参数 `{ query, limit? }`。
|
|
373
|
-
* @returns 给模型看的文本。
|
|
374
|
-
*/
|
|
375
|
-
search(args) {
|
|
376
|
-
const raw = args ?? {};
|
|
377
|
-
const query = typeof raw.query === "string" ? raw.query : "";
|
|
378
|
-
const wanted = Number(raw.limit);
|
|
379
|
-
const limit = Number.isFinite(wanted) ? Math.min(Math.max(Math.trunc(wanted), 1), MAX_SEARCH_LIMIT) : DEFAULT_SEARCH_LIMIT;
|
|
380
|
-
const pool = this.snapshot().filter((tool) => this.isEnabled(tool.name));
|
|
381
|
-
if (pool.length === 0) return hostText(this.ctx, "searchDisabled");
|
|
382
|
-
const picked = pickTools(pool, query, limit, { serverOf: (name) => this.serverNameOf(name) });
|
|
383
|
-
for (const tool of picked) this.hot.touch(tool.name);
|
|
384
|
-
if (picked.length > 0) this.refreshMasks();
|
|
385
|
-
if (picked.length === 0) return hostText(this.ctx, "searchMiss");
|
|
386
|
-
const lines = picked.map((tool) => JSON.stringify({
|
|
387
|
-
name: tool.name,
|
|
388
|
-
description: (tool.description ?? "").slice(0, DESCRIPTION_CLIP),
|
|
389
|
-
parameters: tool.parameters
|
|
390
|
-
}));
|
|
391
|
-
return hostText(this.ctx, "searchHit", { lines: lines.join("\n") });
|
|
230
|
+
return { ok: true, value: { mode: normalized } };
|
|
392
231
|
}
|
|
393
232
|
/**
|
|
394
233
|
* 某个工具是否允许注入(缺席 = 启用)。
|
|
@@ -398,41 +237,6 @@ class ToolControl {
|
|
|
398
237
|
isEnabled(name) {
|
|
399
238
|
return this.switches.get(name) !== false;
|
|
400
239
|
}
|
|
401
|
-
/**
|
|
402
|
-
* 把一个装配按当前模式改写(只追加提示段落)。
|
|
403
|
-
*
|
|
404
|
-
* **不再碰 `tools`**:`0.2.2-rc.1` 起官方用提供者结果覆盖瀑布返回值里的工具声明,在这里改
|
|
405
|
-
* 不会报错、也不会生效;可见性由 {@link refreshMasks} 的 restriction 负责。
|
|
406
|
-
*
|
|
407
|
-
* @param assembly - 装配对象(就地改写 `sections`)。
|
|
408
|
-
*/
|
|
409
|
-
applyAssembly(assembly) {
|
|
410
|
-
if (this.mode !== "search") return;
|
|
411
|
-
const servers = this.enabledServers();
|
|
412
|
-
const sections = (assembly.sections ?? []).filter((section) => section.name !== SEARCH_SECTION_NAME);
|
|
413
|
-
sections.push({
|
|
414
|
-
name: SEARCH_SECTION_NAME,
|
|
415
|
-
text: hostText(this.ctx, "toolControlPrompt", {
|
|
416
|
-
servers: servers.length === 0 ? hostText(this.ctx, "noServers") : servers.join(", ")
|
|
417
|
-
}),
|
|
418
|
-
// 段落是自然语言,不该被 `{{var}}` 插值器严格校验(见文件头)。
|
|
419
|
-
interpolate: false
|
|
420
|
-
});
|
|
421
|
-
assembly.sections = sections;
|
|
422
|
-
}
|
|
423
|
-
/**
|
|
424
|
-
* 当前已启用工具覆盖的服务器名(升序去重)。
|
|
425
|
-
* @returns 服务器名。
|
|
426
|
-
*/
|
|
427
|
-
enabledServers() {
|
|
428
|
-
const servers = /* @__PURE__ */ new Set();
|
|
429
|
-
for (const tool of this.snapshot()) {
|
|
430
|
-
if (!this.isEnabled(tool.name)) continue;
|
|
431
|
-
const server = this.serverNameOf(tool.name);
|
|
432
|
-
if (server !== "") servers.add(server);
|
|
433
|
-
}
|
|
434
|
-
return [...servers].sort();
|
|
435
|
-
}
|
|
436
240
|
/**
|
|
437
241
|
* 归属一个工具名到服务器(优先用已知集合匹配,见 {@link serverOf})。
|
|
438
242
|
* @param name - 公开工具名。
|
|
@@ -441,13 +245,32 @@ class ToolControl {
|
|
|
441
245
|
serverNameOf(name) {
|
|
442
246
|
return serverOf(name, this.serverNames);
|
|
443
247
|
}
|
|
248
|
+
/**
|
|
249
|
+
* 工具名是否属于**本插件自有服务器**。
|
|
250
|
+
*
|
|
251
|
+
* 必须按"最长已知服务器前缀"判定,不能退回 `serverOfToolName()` 的首个 `__` 切分:
|
|
252
|
+
* 官方 manager 的 `foo_extra` 会被切成 `foo`,而 `foo` 恰好是本插件自有服务器时,
|
|
253
|
+
* 它就会混进 deny。已知集合包含文件行、组合层行与 manager 的 configured 项。
|
|
254
|
+
*
|
|
255
|
+
* @param name - 公开工具名。
|
|
256
|
+
* @returns 属于自有服务器时为 true(未配置边界时一律 true,供零依赖单测)。
|
|
257
|
+
*/
|
|
258
|
+
isOwnedTool(name) {
|
|
259
|
+
if (this.ownServers === void 0) return true;
|
|
260
|
+
let best = "";
|
|
261
|
+
for (const candidate of this.serverNames) {
|
|
262
|
+
if (candidate.length <= best.length) continue;
|
|
263
|
+
if (name.startsWith(`${TOOL_PREFIX}${candidate}__`)) best = candidate;
|
|
264
|
+
}
|
|
265
|
+
return best !== "" && this.ownServers.has(best);
|
|
266
|
+
}
|
|
444
267
|
/**
|
|
445
268
|
* 当前已注册的 MCP 工具(按名排序 = 字节稳定)。
|
|
446
269
|
* @returns 工具列表。
|
|
447
270
|
*/
|
|
448
271
|
snapshot() {
|
|
449
272
|
const all = this.ctx.tools.schemas();
|
|
450
|
-
return all.filter((tool) => typeof tool?.name === "string" && tool.name.startsWith(TOOL_PREFIX)).sort(compareName);
|
|
273
|
+
return all.filter((tool) => typeof tool?.name === "string" && tool.name.startsWith(TOOL_PREFIX)).filter((tool) => !tool.name.endsWith(LOADER_TOOL_SUFFIX)).filter((tool) => this.isOwnedTool(tool.name)).sort(compareName);
|
|
451
274
|
}
|
|
452
275
|
/**
|
|
453
276
|
* 为一个新建 agent 建掩码作用域,并立即同步一次可见性。
|
|
@@ -528,6 +351,10 @@ class ToolControl {
|
|
|
528
351
|
mask.lift = () => {
|
|
529
352
|
};
|
|
530
353
|
mask.denied = signature;
|
|
354
|
+
if (process.env.DSH_MCP_OPS_DEBUG !== void 0 && denied.length > 0) {
|
|
355
|
+
process.stderr.write(`dsh-mcp-ops: mask mode=${this.mode} denied=${denied.join(",")} own=${[...this.ownServers ?? []].join(",")}
|
|
356
|
+
`);
|
|
357
|
+
}
|
|
531
358
|
if (denied.length === 0) return;
|
|
532
359
|
mask.lift = mask.scope.ctx.tools.restrict({ deny: denied });
|
|
533
360
|
}
|
|
@@ -552,14 +379,10 @@ class ToolControl {
|
|
|
552
379
|
* @returns 按名升序的工具名。
|
|
553
380
|
*/
|
|
554
381
|
deniedFor() {
|
|
555
|
-
return deniedToolNames(this.snapshot(), {
|
|
556
|
-
mode: this.mode,
|
|
557
|
-
isEnabled: (name) => this.isEnabled(name),
|
|
558
|
-
hot: this.hot.names()
|
|
559
|
-
});
|
|
382
|
+
return deniedToolNames(this.snapshot(), { isEnabled: (name) => this.isEnabled(name) });
|
|
560
383
|
}
|
|
561
384
|
/**
|
|
562
|
-
*
|
|
385
|
+
* 只在调试开关下打印一次钩子异常(钩子抛错会打断整轮装配,绝不能外泄)。
|
|
563
386
|
* @param message - 文本。
|
|
564
387
|
*/
|
|
565
388
|
warnOnce(message) {
|
|
@@ -569,26 +392,23 @@ class ToolControl {
|
|
|
569
392
|
`);
|
|
570
393
|
}
|
|
571
394
|
}
|
|
395
|
+
function compareName(left, right) {
|
|
396
|
+
return comparePlainName(left.name, right.name);
|
|
397
|
+
}
|
|
398
|
+
function comparePlainName(left, right) {
|
|
399
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
400
|
+
}
|
|
572
401
|
function failure(code, message) {
|
|
573
402
|
return { ok: false, error: { code, message } };
|
|
574
403
|
}
|
|
575
404
|
export {
|
|
576
|
-
|
|
577
|
-
DESCRIPTION_CLIP,
|
|
578
|
-
HOT_LIMIT,
|
|
579
|
-
HotSet,
|
|
580
|
-
MAX_SEARCH_LIMIT,
|
|
581
|
-
SEARCH_SECTION_NAME,
|
|
582
|
-
SEARCH_TOOL_NAME,
|
|
405
|
+
LOADER_TOOL_SUFFIX,
|
|
583
406
|
TOOL_PREFIX,
|
|
584
407
|
TOOL_ROW_DESCRIPTION_CLIP,
|
|
585
408
|
ToolControl,
|
|
586
409
|
deniedToolNames,
|
|
587
410
|
isToolDeniedBySwitch,
|
|
588
|
-
pickTools,
|
|
589
|
-
scoreTool,
|
|
590
411
|
serverOf,
|
|
591
412
|
serverOfToolName,
|
|
592
|
-
tokenizeQuery,
|
|
593
413
|
toolOfToolName
|
|
594
414
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-mcp-ops",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.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": "
|
|
45
|
-
"dsh": "
|
|
44
|
+
"node": "^24.22.3 || >=26.13.1",
|
|
45
|
+
"dsh": ">=0.2.3-0 <0.3.0-0"
|
|
46
46
|
},
|
|
47
47
|
"dsh": {
|
|
48
48
|
"manifestVersion": 1,
|
|
@@ -68,18 +68,17 @@
|
|
|
68
68
|
"registry": "https://registry.npmjs.org/"
|
|
69
69
|
},
|
|
70
70
|
"peerDependencies": {
|
|
71
|
-
"@deepseek-ai/cordis": "^4.
|
|
72
|
-
"@deepseek-ai/cordis-plugin-include": "^1.
|
|
73
|
-
"@deepseek-ai/dsh": "^0.2.
|
|
74
|
-
"@deepseek-ai/dsh-atomic-write": "^0.2.
|
|
75
|
-
"@deepseek-ai/dsh-credentials": "^0.2.
|
|
76
|
-
"@deepseek-ai/dsh-mcp-client": "^0.2.
|
|
77
|
-
"@deepseek-ai/dsh-
|
|
78
|
-
"@deepseek-ai/dsh-
|
|
79
|
-
"@deepseek-ai/dsh-
|
|
80
|
-
"@deepseek-ai/dsh-
|
|
81
|
-
"@deepseek-ai/
|
|
82
|
-
"@deepseek-ai/schemastery": "^3.19.0",
|
|
71
|
+
"@deepseek-ai/cordis": "^4.2.0-alpha.1",
|
|
72
|
+
"@deepseek-ai/cordis-plugin-include": "^1.2.0-alpha.1",
|
|
73
|
+
"@deepseek-ai/dsh": "^0.2.3-alpha.2",
|
|
74
|
+
"@deepseek-ai/dsh-atomic-write": "^0.2.3-alpha.2",
|
|
75
|
+
"@deepseek-ai/dsh-credentials": "^0.2.3-alpha.2",
|
|
76
|
+
"@deepseek-ai/dsh-mcp-client": "^0.2.3-alpha.2",
|
|
77
|
+
"@deepseek-ai/dsh-mcp-manager": "^0.2.3-alpha.2",
|
|
78
|
+
"@deepseek-ai/dsh-scope": "^0.2.3-alpha.2",
|
|
79
|
+
"@deepseek-ai/dsh-subprocess": "^0.2.3-alpha.2",
|
|
80
|
+
"@deepseek-ai/dsh-typert-protocol": "^0.2.3-alpha.2",
|
|
81
|
+
"@deepseek-ai/schemastery": "^3.20.0-alpha.1",
|
|
83
82
|
"js-yaml": "^4.3.2",
|
|
84
83
|
"zod": "^4.6.7"
|
|
85
84
|
},
|