dsh-mcp-skills-controller 0.2.0 → 0.4.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 +13 -11
- package/client.js +340 -40
- package/index.js +374 -18
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +4 -4
- package/src/frozen-registry.js +171 -151
- package/src/masks.js +182 -37
- package/src/mcp-catalog.js +39 -15
- package/src/mcp-effective.js +55 -0
- package/src/merge.js +405 -127
- package/src/rpc.js +67 -11
- package/src/session-gc.js +62 -0
- package/src/views.js +70 -24
package/src/mcp-catalog.js
CHANGED
|
@@ -32,7 +32,7 @@ const FALLBACK_PARSE = /^mcp__(.+?)__(.+)$/
|
|
|
32
32
|
* @param ctx 插件上下文(经 `ctx.get` 机会式获取 `configEditor` 与 `tools`)。
|
|
33
33
|
* @param log 日志对象(可为 console)。
|
|
34
34
|
* @param extraServers 额外要识别的服务器名(服务器暂时没有工具时也能列出)。
|
|
35
|
-
* @returns {{ serverCatalog: () => Array<{ name: string, transport: string, target: string, tools: string[], toolNames: string[], connected: boolean }> }}
|
|
35
|
+
* @returns {{ serverCatalog: () => Array<{ name: string, transport: string, target: string, tools: string[], toolNames: string[], toolDetails: Array<{ name: string, rawName: string, description: string }>, connected: boolean }> }}
|
|
36
36
|
*/
|
|
37
37
|
export function createMcpCatalog(ctx, log, extraServers = []) {
|
|
38
38
|
/**
|
|
@@ -79,33 +79,51 @@ export function createMcpCatalog(ctx, log, extraServers = []) {
|
|
|
79
79
|
return found
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
/**
|
|
83
|
+
* 读一次全局工具视图:名字 + 描述。
|
|
84
|
+
*
|
|
85
|
+
* 目录、掩码与设置页共用这一次读数;工具服务缺失或读取失败时返回空集合,
|
|
86
|
+
* 目录退化为「只有已配置服务器」。
|
|
87
|
+
* @returns {{ names: string[], descriptions: Map<string, string> }}
|
|
88
|
+
*/
|
|
89
|
+
const readGlobalTools = () => {
|
|
90
|
+
const tools = ctx.get('tools')
|
|
91
|
+
if (tools === undefined) return { names: [], descriptions: new Map() }
|
|
92
|
+
try {
|
|
93
|
+
const schemas = tools.schemas()
|
|
94
|
+
const names = []
|
|
95
|
+
const descriptions = new Map()
|
|
96
|
+
for (const schema of schemas) {
|
|
97
|
+
names.push(schema.name)
|
|
98
|
+
descriptions.set(schema.name, typeof schema.description === 'string' ? schema.description : '')
|
|
99
|
+
}
|
|
100
|
+
return { names, descriptions }
|
|
101
|
+
} catch (error) {
|
|
102
|
+
log?.warn?.(`mcp-skills-controller: cannot read global tool schemas: ${String(error?.message ?? error)}`)
|
|
103
|
+
return { names: [], descriptions: new Map() }
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
82
107
|
/**
|
|
83
108
|
* 从全局工具视图里按服务器归组 MCP 工具。
|
|
84
109
|
*
|
|
85
110
|
* 优先用 configEditor 给的名字做前缀匹配(服务器名本身可能含 `__`),
|
|
86
111
|
* 再用 `mcp__<server>__<tool>` 兜底解析没匹配上的名字。
|
|
87
112
|
* @param configured 调用方已读的 configEditor 目录(复用,避免重复读 entries)。
|
|
113
|
+
* @param toolView 调用方已读的全局工具视图(名字与描述)。
|
|
88
114
|
* @returns Map<serverName, 公开工具名列表>
|
|
89
115
|
*/
|
|
90
|
-
const toolsByServer = (configured) => {
|
|
116
|
+
const toolsByServer = (configured, toolView) => {
|
|
91
117
|
/** @type {Map<string, string[]>} */
|
|
92
118
|
const grouped = new Map()
|
|
93
|
-
|
|
94
|
-
if (tools === undefined) return grouped
|
|
95
|
-
let names = []
|
|
96
|
-
try {
|
|
97
|
-
names = tools.schemas().map((schema) => schema.name)
|
|
98
|
-
} catch (error) {
|
|
99
|
-
log?.warn?.(`mcp-skills-controller: cannot read global tool schemas: ${String(error?.message ?? error)}`)
|
|
100
|
-
return grouped
|
|
101
|
-
}
|
|
119
|
+
if (toolView.names.length === 0) return grouped
|
|
102
120
|
const known = [...(configured ?? configuredServers()).keys()].sort((left, right) => right.length - left.length)
|
|
103
121
|
const push = (server, toolName) => {
|
|
104
122
|
const list = grouped.get(server)
|
|
105
123
|
if (list === undefined) grouped.set(server, [toolName])
|
|
106
124
|
else list.push(toolName)
|
|
107
125
|
}
|
|
108
|
-
for (const toolName of names) {
|
|
126
|
+
for (const toolName of toolView.names) {
|
|
109
127
|
if (!toolName.startsWith(PUBLIC_PREFIX)) continue
|
|
110
128
|
const matched = known.find((server) => toolName.startsWith(`${PUBLIC_PREFIX}${server}__`))
|
|
111
129
|
if (matched !== undefined) {
|
|
@@ -125,7 +143,8 @@ export function createMcpCatalog(ctx, log, extraServers = []) {
|
|
|
125
143
|
*/
|
|
126
144
|
const serverCatalog = () => {
|
|
127
145
|
const configured = configuredServers()
|
|
128
|
-
const
|
|
146
|
+
const toolView = readGlobalTools()
|
|
147
|
+
const grouped = toolsByServer(configured, toolView)
|
|
129
148
|
const names = new Set([...configured.keys(), ...grouped.keys()])
|
|
130
149
|
return [...names].sort((left, right) => left.localeCompare(right)).map((serverName) => {
|
|
131
150
|
const row = configured.get(serverName)
|
|
@@ -134,10 +153,15 @@ export function createMcpCatalog(ctx, log, extraServers = []) {
|
|
|
134
153
|
name: serverName,
|
|
135
154
|
transport: row?.transport ?? 'unknown',
|
|
136
155
|
target: row?.target ?? '',
|
|
137
|
-
// tools 是掩码要用的公开全名(mcp__<server>__<tool>);toolNames
|
|
138
|
-
//
|
|
156
|
+
// tools 是掩码要用的公开全名(mcp__<server>__<tool>);toolNames 与
|
|
157
|
+
// toolDetails 是设置页展开区给人看的裸工具名与描述,不参与决策。
|
|
139
158
|
tools,
|
|
140
159
|
toolNames: tools.map((toolName) => stripServerPrefix(toolName, serverName)),
|
|
160
|
+
toolDetails: tools.map((toolName) => ({
|
|
161
|
+
name: toolName,
|
|
162
|
+
rawName: stripServerPrefix(toolName, serverName),
|
|
163
|
+
description: toolView.descriptions.get(toolName) ?? '',
|
|
164
|
+
})),
|
|
141
165
|
connected: grouped.has(serverName),
|
|
142
166
|
}
|
|
143
167
|
})
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP 两层决策的纯函数:服务器节点(enabled)+ 其下的工具例外(tools)。
|
|
3
|
+
*
|
|
4
|
+
* 生效顺序(后者覆盖前者):默认开 → 全局服务器 → 全局工具 → 会话服务器 →
|
|
5
|
+
* 会话工具。工具未记录时**跟随服务器生效值**(含会话服务器覆盖),这是覆盖式
|
|
6
|
+
* 语义与「服务器关 + 单工具开」的实现点。
|
|
7
|
+
*
|
|
8
|
+
* 本模块不读写文件、不做 IO,所有函数对畸形输入返回安全默认值,绝不抛错。
|
|
9
|
+
*
|
|
10
|
+
* @module mcp-effective
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { effectiveOn } from './merge.js'
|
|
14
|
+
|
|
15
|
+
/** 布尔或 undefined;其余视为未记录。 */
|
|
16
|
+
function booleanOrUndefined(value) {
|
|
17
|
+
return value === true || value === false ? value : undefined
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** 全局层:服务器是否开启(未记录 = 开)。 */
|
|
21
|
+
export function globalServerOn(state, serverName) {
|
|
22
|
+
return state?.mcp?.defaults?.[serverName]?.enabled !== false
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** 全局层:工具是否开启;未记录时跟随所属服务器的全局值。 */
|
|
26
|
+
export function globalToolOn(state, serverName, rawName) {
|
|
27
|
+
const recorded = booleanOrUndefined(state?.mcp?.defaults?.[serverName]?.tools?.[rawName]?.enabled)
|
|
28
|
+
if (recorded !== undefined) return recorded
|
|
29
|
+
return globalServerOn(state, serverName)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** 全局工具层三态:显式开 / 显式关 / 未记录(follow,交给服务器)。 */
|
|
33
|
+
export function globalToolMode(state, serverName, rawName) {
|
|
34
|
+
const recorded = booleanOrUndefined(state?.mcp?.defaults?.[serverName]?.tools?.[rawName]?.enabled)
|
|
35
|
+
return recorded === true ? 'on' : recorded === false ? 'off' : 'follow'
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** 会话层:服务器是否开启(会话覆盖压过全局)。 */
|
|
39
|
+
export function resolveServer(state, sessionId, serverName) {
|
|
40
|
+
return effectiveOn(state, 'mcp', sessionId, serverName) === true
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 会话层:工具是否开启。
|
|
45
|
+
* @param serverEffective 已算好的服务器生效值(可能是冻结值,不能在这里重读 state)。
|
|
46
|
+
*/
|
|
47
|
+
export function resolveTool(state, sessionId, serverName, rawName, serverEffective) {
|
|
48
|
+
const session = booleanOrUndefined(
|
|
49
|
+
state?.mcp?.sessions?.[sessionId]?.[serverName]?.tools?.[rawName]?.enabled,
|
|
50
|
+
)
|
|
51
|
+
if (session !== undefined) return session
|
|
52
|
+
const recorded = booleanOrUndefined(state?.mcp?.defaults?.[serverName]?.tools?.[rawName]?.enabled)
|
|
53
|
+
if (recorded !== undefined) return recorded
|
|
54
|
+
return serverEffective === true
|
|
55
|
+
}
|