dsh-mcp-ops 0.2.2 → 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 +20 -10
- package/lib/client.js +281 -145
- package/lib/configs.js +21 -8
- package/lib/host-locales.js +2 -20
- package/lib/mount.js +31 -42
- package/lib/oauth-key.js +21 -0
- package/lib/probe.js +20 -6
- package/lib/refs.js +0 -42
- package/lib/service.js +71 -243
- package/lib/sources/cordis-source.js +3 -4
- package/lib/tool-control.js +56 -276
- package/package.json +15 -17
- package/lib/oauth-store.js +0 -81
- package/lib/oauth-web.js +0 -120
- package/lib/oauth.js +0 -256
package/lib/tool-control.js
CHANGED
|
@@ -1,167 +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 = [];
|
|
147
42
|
/**
|
|
148
|
-
*
|
|
43
|
+
* **本插件实际注册**的服务器名。
|
|
149
44
|
*
|
|
150
|
-
* 官方 `ctx.mcpManager` 也会注册 `mcp__*`
|
|
151
|
-
*
|
|
45
|
+
* 官方 `ctx.mcpManager` 也会注册 `mcp__*` 工具;若开关按"所有 mcp__ 前缀"计算,会把官方
|
|
46
|
+
* manager 的 configured 项、组合层手写的 mcp-client 行一并挡掉。这里只认本插件 MountManager
|
|
47
|
+
* 当前存活的注册。
|
|
152
48
|
*/
|
|
153
49
|
ownServers;
|
|
154
50
|
/** 逐工具开关(只存显式值;缺席 = 启用)。 */
|
|
155
51
|
switches = /* @__PURE__ */ new Map();
|
|
156
|
-
/**
|
|
157
|
-
mode = "
|
|
52
|
+
/** 注入模式(= 注册项的 `toolLoading`;缺省与 `configs.DEFAULT_CONFIGS` 对齐为 `eager`)。 */
|
|
53
|
+
mode = "eager";
|
|
158
54
|
/** 是否已 install(幂等)。 */
|
|
159
55
|
installed = false;
|
|
160
|
-
/**
|
|
56
|
+
/** 钩子是否已经报过一次错(避免每轮刷屏)。 */
|
|
161
57
|
warned = false;
|
|
162
58
|
/** 活跃 agent 的可见性掩码(键 = Agent 对象,与官方作用域键是同一个对象)。 */
|
|
163
59
|
masks = /* @__PURE__ */ new Map();
|
|
164
|
-
/** 掩码作用域工厂(缺省 = 不做 agent
|
|
60
|
+
/** 掩码作用域工厂(缺省 = 不做 agent 级掩码)。 */
|
|
165
61
|
maskScope;
|
|
166
62
|
/** 声明了 `inject: ['tools']` 的上下文(掩码作用域建在它下面;异步就绪)。 */
|
|
167
63
|
maskHost;
|
|
@@ -170,71 +66,24 @@ class ToolControl {
|
|
|
170
66
|
/** 防重入:`restrict()` 自身会触发 `tools/change`。 */
|
|
171
67
|
refreshing = false;
|
|
172
68
|
/**
|
|
173
|
-
* @param ctx - host 上下文(需要 `tools`
|
|
174
|
-
* @param options -
|
|
69
|
+
* @param ctx - host 上下文(需要 `tools` 服务)。
|
|
70
|
+
* @param options - 持久化出口与掩码作用域工厂。
|
|
175
71
|
*/
|
|
176
72
|
constructor(ctx, options = {}) {
|
|
177
73
|
this.ctx = ctx;
|
|
178
74
|
if (options.store !== void 0) this.store = options.store;
|
|
179
75
|
if (options.maskScope !== void 0) this.maskScope = options.maskScope;
|
|
180
|
-
this.hotLimit = options.hotLimit ?? HOT_LIMIT;
|
|
181
|
-
this.hot = new HotSet(this.hotLimit);
|
|
182
76
|
if (typeof ctx.plugin !== "function") this.maskHost = ctx;
|
|
183
77
|
}
|
|
184
78
|
/**
|
|
185
|
-
*
|
|
79
|
+
* 注册守卫与掩码钩子(幂等)。
|
|
80
|
+
*
|
|
81
|
+
* 不再注册 `mcp_tool_search`、不再挂 `system-prompt/assemble`:按需加载与提示词目录都由
|
|
82
|
+
* 官方 client 提供。
|
|
186
83
|
*/
|
|
187
84
|
install() {
|
|
188
85
|
if (this.installed) return;
|
|
189
86
|
this.installed = true;
|
|
190
|
-
this.ctx.effect(() => this.ctx.tools.register({
|
|
191
|
-
name: SEARCH_TOOL_NAME,
|
|
192
|
-
description: hostText(this.ctx, "toolSearchDesc"),
|
|
193
|
-
parameters: {
|
|
194
|
-
type: "object",
|
|
195
|
-
properties: {
|
|
196
|
-
query: { type: "string", description: hostText(this.ctx, "toolSearchQuery") },
|
|
197
|
-
limit: { type: "number", description: hostText(this.ctx, "toolSearchLimit") }
|
|
198
|
-
},
|
|
199
|
-
required: ["query"],
|
|
200
|
-
additionalProperties: false
|
|
201
|
-
},
|
|
202
|
-
output: {
|
|
203
|
-
schema: {
|
|
204
|
-
type: "object",
|
|
205
|
-
properties: { text: { type: "string" } },
|
|
206
|
-
required: ["text"],
|
|
207
|
-
additionalProperties: false
|
|
208
|
-
},
|
|
209
|
-
render: (_args, value) => [{
|
|
210
|
-
type: "text",
|
|
211
|
-
text: typeof value?.text === "string" ? value.text : ""
|
|
212
|
-
}]
|
|
213
|
-
},
|
|
214
|
-
execute: async (args) => ({ text: this.search(args) })
|
|
215
|
-
}), "dsh-mcp-ops:tool-search");
|
|
216
|
-
this.ctx.effect(
|
|
217
|
-
() => this.ctx.on("system-prompt/assemble", (assembly, _context, next) => {
|
|
218
|
-
try {
|
|
219
|
-
this.applyAssembly(assembly);
|
|
220
|
-
} catch (error) {
|
|
221
|
-
this.warnOnce(`assemble hook failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
222
|
-
}
|
|
223
|
-
return next();
|
|
224
|
-
}),
|
|
225
|
-
"dsh-mcp-ops:assemble"
|
|
226
|
-
);
|
|
227
|
-
this.ctx.effect(
|
|
228
|
-
() => this.ctx.on("tools/result", (exec) => {
|
|
229
|
-
try {
|
|
230
|
-
const name = exec?.name;
|
|
231
|
-
if (typeof name === "string" && name.startsWith(TOOL_PREFIX)) this.hot.touch(name);
|
|
232
|
-
} catch (error) {
|
|
233
|
-
this.warnOnce(`result hook failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
234
|
-
}
|
|
235
|
-
}),
|
|
236
|
-
"dsh-mcp-ops:hot-tracking"
|
|
237
|
-
);
|
|
238
87
|
this.ctx.effect(
|
|
239
88
|
() => this.ctx.tools.guard((execution) => {
|
|
240
89
|
const name = execution?.name;
|
|
@@ -292,17 +141,18 @@ class ToolControl {
|
|
|
292
141
|
}
|
|
293
142
|
}
|
|
294
143
|
/**
|
|
295
|
-
*
|
|
144
|
+
* 用文件里的状态刷新内存态(每次 `sync()` 调用)。
|
|
296
145
|
*
|
|
297
146
|
* @param switches - 工具开关记录(键 = 公开工具名)。
|
|
298
|
-
* @param mode -
|
|
147
|
+
* @param mode - 注入模式;历史值(`search` / `full`)在这里归一,无法识别时保留当前值。
|
|
299
148
|
* @param serverNames - 已知服务器名(工具名归属判定用;省略则保留旧值)。
|
|
300
149
|
* @param ownServers - **本插件实际注册**的服务器名(deny 的边界;省略则保留旧值)。
|
|
301
150
|
*/
|
|
302
151
|
load(switches, mode, serverNames, ownServers) {
|
|
303
152
|
this.switches.clear();
|
|
304
153
|
for (const [name, enabled] of switches) this.switches.set(name, enabled);
|
|
305
|
-
|
|
154
|
+
const normalized = normalizeInjectionMode(mode);
|
|
155
|
+
if (normalized !== void 0) this.mode = normalized;
|
|
306
156
|
if (serverNames !== void 0) this.serverNames = [...serverNames];
|
|
307
157
|
if (ownServers !== void 0) this.ownServers = new Set(ownServers);
|
|
308
158
|
this.refreshMasks();
|
|
@@ -324,15 +174,12 @@ class ToolControl {
|
|
|
324
174
|
server: this.serverNameOf(tool.name),
|
|
325
175
|
tool: toolOfToolName(tool.name),
|
|
326
176
|
description: (tool.description ?? "").slice(0, TOOL_ROW_DESCRIPTION_CLIP),
|
|
327
|
-
enabled: this.isEnabled(tool.name)
|
|
328
|
-
hot: this.hot.has(tool.name)
|
|
177
|
+
enabled: this.isEnabled(tool.name)
|
|
329
178
|
}));
|
|
330
179
|
return {
|
|
331
180
|
tools,
|
|
332
181
|
mode: this.mode,
|
|
333
|
-
|
|
334
|
-
hotLimit: this.hotLimit,
|
|
335
|
-
// rc.2 基线的 restrict() 是唯一执行入口;缺它时配置仍然保存,但不会生效。
|
|
182
|
+
// restrict() 是唯一执行入口;缺它时配置仍然保存,但不会生效。
|
|
336
183
|
enforcement: typeof this.ctx.tools.restrict === "function" && this.maskScope !== void 0 ? "mask" : "unavailable"
|
|
337
184
|
};
|
|
338
185
|
}
|
|
@@ -341,7 +188,7 @@ class ToolControl {
|
|
|
341
188
|
*
|
|
342
189
|
* @param name - 公开工具名(必须合 `mcp__<server>__<tool>` 文法)。
|
|
343
190
|
* @param enabled - 是否启用。
|
|
344
|
-
* @returns
|
|
191
|
+
* @returns 成功(附带该行)或失败。
|
|
345
192
|
*/
|
|
346
193
|
async set(name, enabled) {
|
|
347
194
|
if (typeof name !== "string" || serverOfToolName(name) === "" || toolOfToolName(name) === "") {
|
|
@@ -350,7 +197,6 @@ class ToolControl {
|
|
|
350
197
|
const next = enabled === true;
|
|
351
198
|
const previous = this.switches.get(name);
|
|
352
199
|
this.switches.set(name, next);
|
|
353
|
-
if (!next) this.hot.delete(name);
|
|
354
200
|
try {
|
|
355
201
|
await this.store?.putSwitch(name, next);
|
|
356
202
|
} catch (error) {
|
|
@@ -362,50 +208,26 @@ class ToolControl {
|
|
|
362
208
|
return { ok: true, value: { name, enabled: next } };
|
|
363
209
|
}
|
|
364
210
|
/**
|
|
365
|
-
*
|
|
211
|
+
* 切换注入模式(= 注册项的 `toolLoading`;服务侧 `sync()` 会因指纹变化差分重建)。
|
|
366
212
|
*
|
|
367
|
-
* @param mode -
|
|
213
|
+
* @param mode - 目标模式;历史值按 {@link normalizeInjectionMode} 归一。
|
|
368
214
|
* @returns 成功(附带模式)或失败。
|
|
369
215
|
*/
|
|
370
216
|
async setMode(mode) {
|
|
371
|
-
|
|
217
|
+
const normalized = normalizeInjectionMode(mode);
|
|
218
|
+
if (normalized === void 0) {
|
|
372
219
|
return failure("MCP_TOOL_INVALID_MODE", `invalid injection mode: ${JSON.stringify(mode ?? null)}`);
|
|
373
220
|
}
|
|
374
221
|
const previous = this.mode;
|
|
375
|
-
this.mode =
|
|
376
|
-
if (previous !== mode) this.hot.clear();
|
|
222
|
+
this.mode = normalized;
|
|
377
223
|
try {
|
|
378
|
-
await this.store?.putMode(
|
|
224
|
+
await this.store?.putMode(normalized);
|
|
379
225
|
} catch (error) {
|
|
380
226
|
this.mode = previous;
|
|
381
227
|
return failure("MCP_STORE_WRITE_FAILED", error instanceof Error ? error.message : String(error));
|
|
382
228
|
}
|
|
383
229
|
this.refreshMasks();
|
|
384
|
-
return { ok: true, value: { mode } };
|
|
385
|
-
}
|
|
386
|
-
/**
|
|
387
|
-
* 执行一次关键词检索(`mcp_tool_search` 的实现)。
|
|
388
|
-
*
|
|
389
|
-
* @param args - 模型参数 `{ query, limit? }`。
|
|
390
|
-
* @returns 给模型看的文本。
|
|
391
|
-
*/
|
|
392
|
-
search(args) {
|
|
393
|
-
const raw = args ?? {};
|
|
394
|
-
const query = typeof raw.query === "string" ? raw.query : "";
|
|
395
|
-
const wanted = Number(raw.limit);
|
|
396
|
-
const limit = Number.isFinite(wanted) ? Math.min(Math.max(Math.trunc(wanted), 1), MAX_SEARCH_LIMIT) : DEFAULT_SEARCH_LIMIT;
|
|
397
|
-
const pool = this.snapshot().filter((tool) => this.isEnabled(tool.name));
|
|
398
|
-
if (pool.length === 0) return hostText(this.ctx, "searchDisabled");
|
|
399
|
-
const picked = pickTools(pool, query, limit, { serverOf: (name) => this.serverNameOf(name) });
|
|
400
|
-
for (const tool of picked) this.hot.touch(tool.name);
|
|
401
|
-
if (picked.length > 0) this.refreshMasks();
|
|
402
|
-
if (picked.length === 0) return hostText(this.ctx, "searchMiss");
|
|
403
|
-
const lines = picked.map((tool) => JSON.stringify({
|
|
404
|
-
name: tool.name,
|
|
405
|
-
description: (tool.description ?? "").slice(0, DESCRIPTION_CLIP),
|
|
406
|
-
parameters: tool.parameters
|
|
407
|
-
}));
|
|
408
|
-
return hostText(this.ctx, "searchHit", { lines: lines.join("\n") });
|
|
230
|
+
return { ok: true, value: { mode: normalized } };
|
|
409
231
|
}
|
|
410
232
|
/**
|
|
411
233
|
* 某个工具是否允许注入(缺席 = 启用)。
|
|
@@ -415,41 +237,6 @@ class ToolControl {
|
|
|
415
237
|
isEnabled(name) {
|
|
416
238
|
return this.switches.get(name) !== false;
|
|
417
239
|
}
|
|
418
|
-
/**
|
|
419
|
-
* 把一个装配按当前模式改写(只追加提示段落)。
|
|
420
|
-
*
|
|
421
|
-
* **不再碰 `tools`**:`0.2.2-rc.1` 起官方用提供者结果覆盖瀑布返回值里的工具声明,在这里改
|
|
422
|
-
* 不会报错、也不会生效;可见性由 {@link refreshMasks} 的 restriction 负责。
|
|
423
|
-
*
|
|
424
|
-
* @param assembly - 装配对象(就地改写 `sections`)。
|
|
425
|
-
*/
|
|
426
|
-
applyAssembly(assembly) {
|
|
427
|
-
if (this.mode !== "search") return;
|
|
428
|
-
const servers = this.enabledServers();
|
|
429
|
-
const sections = (assembly.sections ?? []).filter((section) => section.name !== SEARCH_SECTION_NAME);
|
|
430
|
-
sections.push({
|
|
431
|
-
name: SEARCH_SECTION_NAME,
|
|
432
|
-
text: hostText(this.ctx, "toolControlPrompt", {
|
|
433
|
-
servers: servers.length === 0 ? hostText(this.ctx, "noServers") : servers.join(", ")
|
|
434
|
-
}),
|
|
435
|
-
// 段落是自然语言,不该被 `{{var}}` 插值器严格校验(见文件头)。
|
|
436
|
-
interpolate: false
|
|
437
|
-
});
|
|
438
|
-
assembly.sections = sections;
|
|
439
|
-
}
|
|
440
|
-
/**
|
|
441
|
-
* 当前已启用工具覆盖的服务器名(升序去重)。
|
|
442
|
-
* @returns 服务器名。
|
|
443
|
-
*/
|
|
444
|
-
enabledServers() {
|
|
445
|
-
const servers = /* @__PURE__ */ new Set();
|
|
446
|
-
for (const tool of this.snapshot()) {
|
|
447
|
-
if (!this.isEnabled(tool.name)) continue;
|
|
448
|
-
const server = this.serverNameOf(tool.name);
|
|
449
|
-
if (server !== "") servers.add(server);
|
|
450
|
-
}
|
|
451
|
-
return [...servers].sort();
|
|
452
|
-
}
|
|
453
240
|
/**
|
|
454
241
|
* 归属一个工具名到服务器(优先用已知集合匹配,见 {@link serverOf})。
|
|
455
242
|
* @param name - 公开工具名。
|
|
@@ -463,7 +250,7 @@ class ToolControl {
|
|
|
463
250
|
*
|
|
464
251
|
* 必须按"最长已知服务器前缀"判定,不能退回 `serverOfToolName()` 的首个 `__` 切分:
|
|
465
252
|
* 官方 manager 的 `foo_extra` 会被切成 `foo`,而 `foo` 恰好是本插件自有服务器时,
|
|
466
|
-
* 它就会混进 deny
|
|
253
|
+
* 它就会混进 deny。已知集合包含文件行、组合层行与 manager 的 configured 项。
|
|
467
254
|
*
|
|
468
255
|
* @param name - 公开工具名。
|
|
469
256
|
* @returns 属于自有服务器时为 true(未配置边界时一律 true,供零依赖单测)。
|
|
@@ -483,7 +270,7 @@ class ToolControl {
|
|
|
483
270
|
*/
|
|
484
271
|
snapshot() {
|
|
485
272
|
const all = this.ctx.tools.schemas();
|
|
486
|
-
return all.filter((tool) => typeof tool?.name === "string" && tool.name.startsWith(TOOL_PREFIX)).filter((tool) => this.isOwnedTool(tool.name)).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);
|
|
487
274
|
}
|
|
488
275
|
/**
|
|
489
276
|
* 为一个新建 agent 建掩码作用域,并立即同步一次可见性。
|
|
@@ -592,14 +379,10 @@ class ToolControl {
|
|
|
592
379
|
* @returns 按名升序的工具名。
|
|
593
380
|
*/
|
|
594
381
|
deniedFor() {
|
|
595
|
-
return deniedToolNames(this.snapshot(), {
|
|
596
|
-
mode: this.mode,
|
|
597
|
-
isEnabled: (name) => this.isEnabled(name),
|
|
598
|
-
hot: this.hot.names()
|
|
599
|
-
});
|
|
382
|
+
return deniedToolNames(this.snapshot(), { isEnabled: (name) => this.isEnabled(name) });
|
|
600
383
|
}
|
|
601
384
|
/**
|
|
602
|
-
*
|
|
385
|
+
* 只在调试开关下打印一次钩子异常(钩子抛错会打断整轮装配,绝不能外泄)。
|
|
603
386
|
* @param message - 文本。
|
|
604
387
|
*/
|
|
605
388
|
warnOnce(message) {
|
|
@@ -609,26 +392,23 @@ class ToolControl {
|
|
|
609
392
|
`);
|
|
610
393
|
}
|
|
611
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
|
+
}
|
|
612
401
|
function failure(code, message) {
|
|
613
402
|
return { ok: false, error: { code, message } };
|
|
614
403
|
}
|
|
615
404
|
export {
|
|
616
|
-
|
|
617
|
-
DESCRIPTION_CLIP,
|
|
618
|
-
HOT_LIMIT,
|
|
619
|
-
HotSet,
|
|
620
|
-
MAX_SEARCH_LIMIT,
|
|
621
|
-
SEARCH_SECTION_NAME,
|
|
622
|
-
SEARCH_TOOL_NAME,
|
|
405
|
+
LOADER_TOOL_SUFFIX,
|
|
623
406
|
TOOL_PREFIX,
|
|
624
407
|
TOOL_ROW_DESCRIPTION_CLIP,
|
|
625
408
|
ToolControl,
|
|
626
409
|
deniedToolNames,
|
|
627
410
|
isToolDeniedBySwitch,
|
|
628
|
-
pickTools,
|
|
629
|
-
scoreTool,
|
|
630
411
|
serverOf,
|
|
631
412
|
serverOfToolName,
|
|
632
|
-
tokenizeQuery,
|
|
633
413
|
toolOfToolName
|
|
634
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,21 +68,19 @@
|
|
|
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-mcp-manager": "^0.2.
|
|
78
|
-
"@deepseek-ai/dsh-scope": "^0.2.
|
|
79
|
-
"@deepseek-ai/dsh-subprocess": "^0.2.
|
|
80
|
-
"@deepseek-ai/dsh-
|
|
81
|
-
"@deepseek-ai/
|
|
82
|
-
"@deepseek-ai/dsh-typert-protocol": "^0.2.2-rc.2",
|
|
83
|
-
"@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",
|
|
84
82
|
"js-yaml": "^4.3.2",
|
|
85
|
-
"zod": "^4.6.
|
|
83
|
+
"zod": "^4.6.7"
|
|
86
84
|
},
|
|
87
85
|
"scripts": {
|
|
88
86
|
"build": "node scripts/build-host.mjs && node scripts/build-client.mjs && node scripts/check-client-bundle.mjs",
|
package/lib/oauth-store.js
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
discoverMetadata,
|
|
3
|
-
isTokenFresh,
|
|
4
|
-
parseClient,
|
|
5
|
-
parseToken,
|
|
6
|
-
refreshAccessToken,
|
|
7
|
-
serializeToken
|
|
8
|
-
} from "./oauth.js";
|
|
9
|
-
import { oauthAccessRef, oauthRef } from "./refs.js";
|
|
10
|
-
async function readToken(credentials, serverName) {
|
|
11
|
-
try {
|
|
12
|
-
const resolved = await credentials.resolve(oauthRef(serverName));
|
|
13
|
-
return parseToken(resolved?.value);
|
|
14
|
-
} catch {
|
|
15
|
-
return void 0;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
async function writeToken(credentials, serverName, token) {
|
|
19
|
-
const text = serializeToken(token);
|
|
20
|
-
const recordRef = oauthRef(serverName);
|
|
21
|
-
const currentRecord = await credentials.resolve(recordRef);
|
|
22
|
-
if (currentRecord?.value !== text) await credentials.set(recordRef, text);
|
|
23
|
-
const ref = oauthAccessRef(serverName);
|
|
24
|
-
const current = await credentials.resolve(ref);
|
|
25
|
-
if (current?.value !== token.accessToken) await credentials.set(ref, token.accessToken);
|
|
26
|
-
}
|
|
27
|
-
async function ensureFreshToken(credentials, serverName, url, options = {}) {
|
|
28
|
-
const stored = await readToken(credentials, serverName);
|
|
29
|
-
if (stored === void 0) return { refreshed: false, error: "no stored token" };
|
|
30
|
-
if (isTokenFresh(stored)) {
|
|
31
|
-
const ref = oauthAccessRef(serverName);
|
|
32
|
-
const current = await credentials.resolve(ref);
|
|
33
|
-
if (current?.value !== stored.accessToken) await credentials.set(ref, stored.accessToken);
|
|
34
|
-
return { token: stored, refreshed: false, accessRef: ref };
|
|
35
|
-
}
|
|
36
|
-
if (stored.refreshToken === void 0 || stored.refreshToken === "") {
|
|
37
|
-
return { refreshed: false, error: "access token expired and no refresh_token is stored" };
|
|
38
|
-
}
|
|
39
|
-
try {
|
|
40
|
-
const metadata = await discoverMetadata(url, options.fetchImpl);
|
|
41
|
-
let clientId = "dsh-mcp-ops";
|
|
42
|
-
let clientSecret;
|
|
43
|
-
try {
|
|
44
|
-
const cached = await credentials.resolve(oauthRef(serverName, true));
|
|
45
|
-
const client = parseClient(cached?.value);
|
|
46
|
-
if (client !== void 0) {
|
|
47
|
-
clientId = client.clientId;
|
|
48
|
-
clientSecret = client.clientSecret;
|
|
49
|
-
}
|
|
50
|
-
} catch {
|
|
51
|
-
}
|
|
52
|
-
const refreshed = await refreshAccessToken({
|
|
53
|
-
tokenEndpoint: metadata.tokenEndpoint,
|
|
54
|
-
refreshToken: stored.refreshToken,
|
|
55
|
-
clientId,
|
|
56
|
-
...clientSecret === void 0 ? {} : { clientSecret },
|
|
57
|
-
...options.fetchImpl === void 0 ? {} : { fetchImpl: options.fetchImpl }
|
|
58
|
-
});
|
|
59
|
-
await writeToken(credentials, serverName, refreshed);
|
|
60
|
-
return { token: refreshed, refreshed: true, accessRef: oauthAccessRef(serverName) };
|
|
61
|
-
} catch (error) {
|
|
62
|
-
return {
|
|
63
|
-
refreshed: false,
|
|
64
|
-
error: `token refresh failed: ${error instanceof Error ? error.message : String(error)}`
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
async function clearToken(credentials, serverName) {
|
|
69
|
-
for (const ref of [oauthRef(serverName), oauthAccessRef(serverName), oauthRef(serverName, true)]) {
|
|
70
|
-
try {
|
|
71
|
-
await credentials.unset(ref);
|
|
72
|
-
} catch {
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
export {
|
|
77
|
-
clearToken,
|
|
78
|
-
ensureFreshToken,
|
|
79
|
-
readToken,
|
|
80
|
-
writeToken
|
|
81
|
-
};
|