dsh-capability-switch 0.0.0-stage → 1.5.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/CHANGELOG.md +273 -0
- package/LICENSE +21 -0
- package/README.md +239 -2
- package/cordis.patch.yml +9 -0
- package/docs/screenshots/mcp.png +0 -0
- package/docs/screenshots/overview.png +0 -0
- package/icon.svg +5 -0
- package/lib/client.js +840 -0
- package/lib/imports.js +120 -0
- package/lib/index.js +320 -0
- package/lib/mcp.js +306 -0
- package/lib/skills.js +526 -0
- package/lib/update.js +193 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +61 -4
package/lib/mcp.js
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { promises as fs } from 'node:fs'
|
|
2
|
+
import { basename, dirname, join } from 'node:path'
|
|
3
|
+
import { randomUUID } from 'node:crypto'
|
|
4
|
+
import { isMap, isSeq, parseDocument } from 'yaml'
|
|
5
|
+
import { groupMcpImports, scanMcpApps } from './imports.js'
|
|
6
|
+
import { dshHome } from './skills.js'
|
|
7
|
+
|
|
8
|
+
const NAME = /^[A-Za-z0-9_-]{1,32}$/
|
|
9
|
+
|
|
10
|
+
function manager(ctx) {
|
|
11
|
+
const service = ctx.get('mcpManager')
|
|
12
|
+
if (!service) throw new Error('当前 DSH 配置未启用 MCP Manager')
|
|
13
|
+
return service
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function editor(ctx) {
|
|
17
|
+
const service = ctx.get('configEditor')
|
|
18
|
+
if (!service) throw new Error('当前 DSH 配置不支持在线修改')
|
|
19
|
+
return service
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function pluginRows(ctx) {
|
|
23
|
+
const service = ctx.get('pluginManager')
|
|
24
|
+
if (!service) return []
|
|
25
|
+
try { return await service.listPlugins() } catch { return [] }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function pluginRowFor(entry, rows) {
|
|
29
|
+
return rows.find(row => row.patchId === entry.options.id
|
|
30
|
+
|| (typeof row.entryId === 'string' && row.entryId.endsWith(':' + entry.options.id)))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function standaloneEntries(ctx) {
|
|
34
|
+
const service = ctx.get('configEditor')
|
|
35
|
+
if (!service) return []
|
|
36
|
+
const rows = await pluginRows(ctx)
|
|
37
|
+
return service.entries()
|
|
38
|
+
.filter(entry => entry.options?.name === '@deepseek-ai/dsh-mcp-client')
|
|
39
|
+
.map(entry => ({ entry, pluginRow: pluginRowFor(entry, rows) }))
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function standaloneMcp(ctx) {
|
|
43
|
+
const result = []
|
|
44
|
+
for (const { entry, pluginRow } of await standaloneEntries(ctx)) {
|
|
45
|
+
const config = entry.options.config || {}
|
|
46
|
+
const enabled = pluginRow?.enabled ?? entry.options.disabled !== true
|
|
47
|
+
const manageable = pluginRow === undefined || pluginRow.readOnlyReason === undefined
|
|
48
|
+
result.push({
|
|
49
|
+
transport: config.transport || 'stdio', command: String(config.command || ''),
|
|
50
|
+
args: Array.isArray(config.args) ? config.args.map(String) : [],
|
|
51
|
+
env: config.env || {}, cwd: String(config.cwd || ''), url: String(config.url || ''),
|
|
52
|
+
headers: config.headers || {}, name: String(config.serverName || entry.options.id),
|
|
53
|
+
enabled, status: { state: enabled ? 'unknown' : 'disabled' }, source: 'standalone',
|
|
54
|
+
writable: manageable,
|
|
55
|
+
canToggle: pluginRow !== undefined && manageable,
|
|
56
|
+
canDelete: manageable, ...(pluginRow?.entryId === undefined ? {} : { pluginEntryId: pluginRow.entryId }), entryId: entry.options.id,
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
return result
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export async function listMcp(ctx) {
|
|
63
|
+
const service = manager(ctx)
|
|
64
|
+
const entries = service.config.servers.get()
|
|
65
|
+
const rows = new Map()
|
|
66
|
+
for (const view of service.list()) {
|
|
67
|
+
const configured = Object.hasOwn(entries, view.name)
|
|
68
|
+
if (!view.configured && !configured) continue
|
|
69
|
+
rows.set(view.name, {
|
|
70
|
+
...(configured ? entries[view.name] : {}), name: view.name,
|
|
71
|
+
enabled: configured ? entries[view.name].enabled : true,
|
|
72
|
+
status: view.status, source: configured ? 'manager' : 'registered', writable: configured,
|
|
73
|
+
canToggle: configured, canDelete: configured,
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
for (const [name, entry] of Object.entries(entries)) {
|
|
77
|
+
if (!rows.has(name)) rows.set(name, {
|
|
78
|
+
...entry, name, status: { state: entry.enabled ? 'connecting' : 'disabled' },
|
|
79
|
+
source: 'manager', writable: true, canToggle: true, canDelete: true,
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
for (const row of await standaloneMcp(ctx)) if (!rows.has(row.name)) rows.set(row.name, row)
|
|
83
|
+
return [...rows.values()]
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async function edit(ctx, update) {
|
|
87
|
+
const service = manager(ctx)
|
|
88
|
+
const entry = service.ctx?.fiber?.entry
|
|
89
|
+
if (!entry) throw new Error('无法定位 MCP Manager 的配置项')
|
|
90
|
+
return await editor(ctx).edit(entry, current => update({ ...(current || {}), servers: { ...(current?.servers || {}) } }))
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function cleanMap(value, label) {
|
|
94
|
+
if (value == null) return {}
|
|
95
|
+
if (typeof value !== 'object' || Array.isArray(value)) throw new Error(label + '必须是 JSON 对象')
|
|
96
|
+
return Object.fromEntries(Object.entries(value).map(([key, val]) => {
|
|
97
|
+
if (!key.trim() || typeof val !== 'string') throw new Error(label + '的键和值必须是字符串')
|
|
98
|
+
return [key, val]
|
|
99
|
+
}))
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function normalizeServer(input, previous = {}) {
|
|
103
|
+
const name = String(input.name || '').trim()
|
|
104
|
+
if (!NAME.test(name)) throw new Error('MCP 名称需为 1–32 位字母、数字、连字符或下划线')
|
|
105
|
+
const requestedTransport = input.transport
|
|
106
|
+
const transport = requestedTransport === 'sse' ? 'streamable-http' : requestedTransport
|
|
107
|
+
if (transport !== 'stdio' && transport !== 'streamable-http') throw new Error('不支持的 MCP 传输类型')
|
|
108
|
+
const shared = { ...previous, name: undefined, transport, enabled: input.enabled !== false }
|
|
109
|
+
delete shared.name
|
|
110
|
+
if (transport === 'stdio') {
|
|
111
|
+
const command = String(input.command || '').trim()
|
|
112
|
+
if (!command) throw new Error('stdio MCP 必须填写启动命令')
|
|
113
|
+
return { ...shared, command, args: Array.isArray(input.args) ? input.args.map(String) : [], env: cleanMap(input.env, '环境变量'), cwd: String(input.cwd || ''), url: '', headers: {} }
|
|
114
|
+
}
|
|
115
|
+
const url = String(input.url || '').trim()
|
|
116
|
+
try { const parsed = new URL(url); if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') throw new Error() } catch { throw new Error('HTTP MCP 必须填写有效的 http(s) URL') }
|
|
117
|
+
return { ...shared, url, headers: cleanMap(input.headers, '请求头'), command: '', args: [], env: {}, cwd: '' }
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export async function saveMcp(ctx, input) {
|
|
121
|
+
const service = manager(ctx)
|
|
122
|
+
const name = String(input.name || '').trim()
|
|
123
|
+
const configured = service.config.servers.get()
|
|
124
|
+
const previous = configured[name]
|
|
125
|
+
if (input.originalName && input.originalName !== name) throw new Error('编辑时不能修改 MCP 名称')
|
|
126
|
+
if (!input.originalName && previous) throw new Error('同名 MCP 已存在')
|
|
127
|
+
const standaloneEntriesList = await standaloneEntries(ctx)
|
|
128
|
+
const standaloneName = candidate => String(candidate.entry.options.config?.serverName || candidate.entry.options.id)
|
|
129
|
+
const standalone = input.originalName && previous === undefined
|
|
130
|
+
? standaloneEntriesList.find(candidate => standaloneName(candidate) === name)
|
|
131
|
+
: undefined
|
|
132
|
+
if (!input.originalName && standaloneEntriesList.some(candidate => standaloneName(candidate) === name)) {
|
|
133
|
+
throw new Error('同名 MCP 已存在')
|
|
134
|
+
}
|
|
135
|
+
if (standalone) {
|
|
136
|
+
if (standalone.pluginRow?.readOnlyReason !== undefined) throw new Error('该独立 MCP 的插件条目不可管理')
|
|
137
|
+
const server = normalizeServer(input, standalone.entry.options.config || {})
|
|
138
|
+
const { enabled, ...config } = server
|
|
139
|
+
await editor(ctx).edit(standalone.entry, current => ({
|
|
140
|
+
...(current || {}),
|
|
141
|
+
...config,
|
|
142
|
+
serverName: name,
|
|
143
|
+
}))
|
|
144
|
+
return { name, enabled: enabled !== false, standalone: true }
|
|
145
|
+
}
|
|
146
|
+
if (input.originalName && previous === undefined) throw new Error('MCP 不存在或已被移除')
|
|
147
|
+
const server = normalizeServer(input, previous || {})
|
|
148
|
+
await edit(ctx, current => { current.servers[name] = server; return current })
|
|
149
|
+
return { name, enabled: server.enabled }
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function importId(server) {
|
|
153
|
+
return server.name + ':' + server.fingerprint
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export async function listMcpImports(ctx, baseHome) {
|
|
157
|
+
const existing = new Set((await listMcp(ctx)).map(row => row.name))
|
|
158
|
+
const scanned = await scanMcpApps(baseHome)
|
|
159
|
+
const servers = groupMcpImports(scanned.servers).map(server => ({
|
|
160
|
+
id: importId(server), name: server.name, transport: server.transport, command: server.command,
|
|
161
|
+
args: server.args, cwd: server.cwd, url: server.url, sources: server.sources,
|
|
162
|
+
conflict: server.conflict, exists: existing.has(server.name),
|
|
163
|
+
}))
|
|
164
|
+
return { servers, errors: scanned.sources.filter(source => source.error) }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export async function importMcp(ctx, selections, baseHome, enable = false) {
|
|
168
|
+
if (!Array.isArray(selections)) throw new Error('selections 必须是预览条目 ID 数组')
|
|
169
|
+
const requested = new Set(selections.filter(id => typeof id === 'string'))
|
|
170
|
+
if (!requested.size) return { imported: [], skipped: [], errors: [] }
|
|
171
|
+
const scanned = await scanMcpApps(baseHome)
|
|
172
|
+
const groups = groupMcpImports(scanned.servers)
|
|
173
|
+
const existing = new Set((await listMcp(ctx)).map(row => row.name))
|
|
174
|
+
const selected = groups.filter(server => requested.has(importId(server)))
|
|
175
|
+
if (selected.length !== requested.size) throw new Error('来源配置已在预览后变化,请重新扫描并确认')
|
|
176
|
+
if (new Set(selected.map(server => server.name)).size !== selected.length) throw new Error('同名配置冲突时只能选择一个来源')
|
|
177
|
+
const imported = []
|
|
178
|
+
const skipped = selected.filter(server => existing.has(server.name)).map(server => server.name)
|
|
179
|
+
const candidates = selected.filter(server => !existing.has(server.name))
|
|
180
|
+
.map(server => [importId(server), normalizeServer({ ...server, enabled: enable === true })])
|
|
181
|
+
const normalized = new Map(candidates)
|
|
182
|
+
await edit(ctx, current => {
|
|
183
|
+
for (const server of selected) {
|
|
184
|
+
if (existing.has(server.name) || current.servers[server.name]) continue
|
|
185
|
+
const normalizedServer = normalized.get(importId(server))
|
|
186
|
+
if (!normalizedServer) continue
|
|
187
|
+
current.servers[server.name] = normalizedServer
|
|
188
|
+
imported.push(server.name)
|
|
189
|
+
}
|
|
190
|
+
return current
|
|
191
|
+
})
|
|
192
|
+
return { imported, skipped, errors: scanned.sources.filter(source => source.error) }
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async function standaloneTarget(ctx, name) {
|
|
196
|
+
const entry = (await standaloneEntries(ctx)).find(candidate => {
|
|
197
|
+
const config = candidate.entry.options.config || {}
|
|
198
|
+
return String(config.serverName || candidate.entry.options.id) === name
|
|
199
|
+
})
|
|
200
|
+
if (!entry) throw new Error('MCP 不存在或已被移除')
|
|
201
|
+
return entry
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function removePatchEntry(text, id) {
|
|
205
|
+
const document = parseDocument(text, {
|
|
206
|
+
customTags: [{ tag: 'tag:yaml.org,2002:js', resolve: value => value }],
|
|
207
|
+
})
|
|
208
|
+
if (document.errors[0] !== undefined) throw document.errors[0]
|
|
209
|
+
if (!isSeq(document.contents)) throw new Error('Profile patch must be a YAML sequence')
|
|
210
|
+
let removed = false
|
|
211
|
+
const rows = document.contents.items
|
|
212
|
+
for (let index = rows.length - 1; index >= 0; index -= 1) {
|
|
213
|
+
const row = rows[index]
|
|
214
|
+
if (!isMap(row)) continue
|
|
215
|
+
const insert = row.get('insert')
|
|
216
|
+
if (isSeq(insert)) {
|
|
217
|
+
for (let child = insert.items.length - 1; child >= 0; child -= 1) {
|
|
218
|
+
const item = insert.items[child]
|
|
219
|
+
if (isMap(item) && item.get('id') === id) {
|
|
220
|
+
insert.items.splice(child, 1)
|
|
221
|
+
removed = true
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
if (insert.items.length === 0) rows.splice(index, 1)
|
|
225
|
+
} else if (row.get('id') === id) {
|
|
226
|
+
rows.splice(index, 1)
|
|
227
|
+
removed = true
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return { text: String(document), removed }
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export async function toggleMcp(ctx, name, enabled) {
|
|
234
|
+
if (!NAME.test(name)) throw new Error('无效的 MCP 名称')
|
|
235
|
+
const configured = manager(ctx).config.servers.get()
|
|
236
|
+
if (Object.hasOwn(configured, name)) {
|
|
237
|
+
await edit(ctx, current => {
|
|
238
|
+
if (!current.servers[name]) throw new Error('MCP 不存在或已被移除')
|
|
239
|
+
current.servers[name] = { ...current.servers[name], enabled: enabled === true }
|
|
240
|
+
return current
|
|
241
|
+
})
|
|
242
|
+
return { name, enabled: enabled === true }
|
|
243
|
+
}
|
|
244
|
+
const target = await standaloneTarget(ctx, name)
|
|
245
|
+
if (!target.pluginRow?.entryId || target.pluginRow.readOnlyReason !== undefined) {
|
|
246
|
+
throw new Error('该独立 MCP 的插件条目不可管理')
|
|
247
|
+
}
|
|
248
|
+
const service = ctx.get('pluginManager')
|
|
249
|
+
if (!service) throw new Error('当前 DSH 配置未启用插件管理器')
|
|
250
|
+
const result = await service.setPluginEnabled(target.pluginRow.entryId, enabled === true)
|
|
251
|
+
if (result.application === 'failed' || result.application === 'cancelled') {
|
|
252
|
+
throw new Error(result.error?.diagnostic || result.error?.code || 'MCP 启停失败')
|
|
253
|
+
}
|
|
254
|
+
return { name, enabled: enabled === true, standalone: true, restartRequired: result.application === 'restart-required' }
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
export async function deleteMcp(ctx, name, confirm) {
|
|
258
|
+
if (confirm !== true) throw new Error('删除 MCP 需要明确确认')
|
|
259
|
+
const configured = manager(ctx).config.servers.get()
|
|
260
|
+
if (Object.hasOwn(configured, name)) {
|
|
261
|
+
await edit(ctx, current => {
|
|
262
|
+
if (!current.servers[name]) throw new Error('MCP 不存在或已被移除')
|
|
263
|
+
delete current.servers[name]
|
|
264
|
+
return current
|
|
265
|
+
})
|
|
266
|
+
return { name, deleted: true }
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const target = await standaloneTarget(ctx, name)
|
|
270
|
+
if (target.pluginRow?.readOnlyReason !== undefined) throw new Error('该独立 MCP 的插件条目不可管理')
|
|
271
|
+
const { entry } = target
|
|
272
|
+
const service = editor(ctx)
|
|
273
|
+
if (typeof service.documentPath !== 'string' || !service.documentPath) throw new Error('无法定位当前 profile 配置')
|
|
274
|
+
const before = await fs.readFile(service.documentPath, 'utf8')
|
|
275
|
+
const { text, removed } = removePatchEntry(before, entry.options.id)
|
|
276
|
+
if (!removed) throw new Error('无法在 profile 配置中定位该独立 MCP')
|
|
277
|
+
const backupDir = join(dshHome(), 'capability-switch', 'backups')
|
|
278
|
+
await fs.mkdir(backupDir, { recursive: true })
|
|
279
|
+
const backup = join(backupDir, `${Date.now()}-${randomUUID()}-${basename(service.documentPath)}`)
|
|
280
|
+
await fs.copyFile(service.documentPath, backup)
|
|
281
|
+
const mode = (await fs.stat(service.documentPath)).mode
|
|
282
|
+
const temporary = join(dirname(service.documentPath), `.${basename(service.documentPath)}.${randomUUID()}.tmp`)
|
|
283
|
+
await fs.writeFile(temporary, text, { mode })
|
|
284
|
+
await fs.rename(temporary, service.documentPath)
|
|
285
|
+
|
|
286
|
+
let liveRemoved = false
|
|
287
|
+
let liveError
|
|
288
|
+
try {
|
|
289
|
+
if (entry.parent?.remove) entry.parent.remove(entry.options.id)
|
|
290
|
+
else ctx.get('loader')?.remove(entry.options.id)
|
|
291
|
+
await ctx.get('loader')?.await()
|
|
292
|
+
liveRemoved = true
|
|
293
|
+
} catch (error) {
|
|
294
|
+
liveError = error.message
|
|
295
|
+
}
|
|
296
|
+
return { name, deleted: true, standalone: true, liveRemoved, restartRequired: !liveRemoved, backup, ...(liveError ? { liveError } : {}) }
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export async function testMcp(ctx, name) {
|
|
300
|
+
return await manager(ctx).probe(name)
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export async function restartMcp(ctx, name) {
|
|
304
|
+
await manager(ctx).restart(name)
|
|
305
|
+
return { name, restarted: true }
|
|
306
|
+
}
|