dsh-mcp-manager-plus 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/CHANGELOG.md +50 -0
- package/LICENSE +21 -0
- package/README.en.md +271 -0
- package/README.md +235 -0
- package/cordis.patch.yml +8 -0
- package/lib/client.js +1602 -0
- package/lib/import.js +395 -0
- package/lib/index.js +1058 -0
- package/lib/patch.js +525 -0
- package/package.json +48 -0
package/lib/import.js
ADDED
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parsing pasted MCP configuration from other tools into this plugin's own
|
|
3
|
+
* server shape.
|
|
4
|
+
*
|
|
5
|
+
* Every tool in the ecosystem ships the same idea — a named map of servers,
|
|
6
|
+
* each either a local command or a remote URL — under a different wrapper,
|
|
7
|
+
* different key names, and sometimes a different file syntax. This module
|
|
8
|
+
* absorbs that variety so the settings page can accept a paste from any of
|
|
9
|
+
* them without the user reformatting anything.
|
|
10
|
+
*
|
|
11
|
+
* The shapes handled, and their tell-tale keys:
|
|
12
|
+
*
|
|
13
|
+
* | Source | Container | stdio | remote |
|
|
14
|
+
* | ---------------------------------------- | --------------- | --------------------------- | ------------------------------- |
|
|
15
|
+
* | Claude Code, Cursor, Windsurf, Qoder, | `mcpServers` | `command`/`args` | `url`/`headers` |
|
|
16
|
+
* | CodeBuddy, TRAE, Cherry Studio, ZCode | | | |
|
|
17
|
+
* | DeepSeek Harness (this plugin) | `mcpServers` | `command`/`args` | `url`/`headers` |
|
|
18
|
+
* | VS Code | `servers` | `command`/`args` | `type:"http"` + `url` |
|
|
19
|
+
* | Codex | `mcp_servers` | `command`/`args` | `url` |
|
|
20
|
+
* | OpenCode | `mcp` | `type:"local"`, command LIST | `type:"remote"` + `url` |
|
|
21
|
+
* | Continue | `mcpServers` ARRAY with `name` | — |
|
|
22
|
+
* | Pi | `mcpServers` + `settings` | `command`/`args` | `transport:"streamable-http"` |
|
|
23
|
+
*
|
|
24
|
+
* Codex's file is TOML, not JSON, so a TOML document is converted first; the
|
|
25
|
+
* rest arrive as JSON (comments and trailing commas are tolerated, because
|
|
26
|
+
* several tools ship `.jsonc`-style examples).
|
|
27
|
+
*
|
|
28
|
+
* @module dsh-mcp-manager-plus/import
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/** Container keys that hold a name → server map, in the order we prefer them. */
|
|
32
|
+
const MAP_CONTAINERS = ['mcpServers', 'mcp_servers', 'servers', 'mcp', 'mcpServersList'];
|
|
33
|
+
|
|
34
|
+
/** Keys that identify a container rather than a server. */
|
|
35
|
+
const CONTAINER_KEYS = new Set([...MAP_CONTAINERS, 'settings', 'projects', '$schema']);
|
|
36
|
+
|
|
37
|
+
/** Transport words meaning "remote HTTP", across every dialect. */
|
|
38
|
+
const HTTP_TYPES = new Set(['http', 'streamable-http', 'streamablehttp', 'sse', 'remote', 'streamable_http']);
|
|
39
|
+
|
|
40
|
+
/** Transport words meaning "local subprocess". */
|
|
41
|
+
const STDIO_TYPES = new Set(['stdio', 'local', 'command', 'process']);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Strip `//` and `/* *\/` comments plus trailing commas from a JSON-ish
|
|
45
|
+
* document, leaving string contents untouched.
|
|
46
|
+
*
|
|
47
|
+
* Several of these tools document their config with comments, and users paste
|
|
48
|
+
* those examples verbatim, so parsing must tolerate them.
|
|
49
|
+
* @param text - candidate document.
|
|
50
|
+
* @returns the same document with comments and trailing commas removed.
|
|
51
|
+
*/
|
|
52
|
+
export function stripJsonNoise(text) {
|
|
53
|
+
let out = '';
|
|
54
|
+
let inString = false;
|
|
55
|
+
let quote = '';
|
|
56
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
57
|
+
const ch = text[i];
|
|
58
|
+
const next = text[i + 1];
|
|
59
|
+
if (inString) {
|
|
60
|
+
out += ch;
|
|
61
|
+
if (ch === '\\') {
|
|
62
|
+
out += next ?? '';
|
|
63
|
+
i += 1;
|
|
64
|
+
} else if (ch === quote) {
|
|
65
|
+
inString = false;
|
|
66
|
+
}
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (ch === '"' || ch === "'") {
|
|
70
|
+
inString = true;
|
|
71
|
+
quote = ch;
|
|
72
|
+
out += ch;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (ch === '/' && next === '/') {
|
|
76
|
+
while (i < text.length && text[i] !== '\n') i += 1;
|
|
77
|
+
out += '\n';
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (ch === '/' && next === '*') {
|
|
81
|
+
const end = text.indexOf('*/', i + 2);
|
|
82
|
+
i = end === -1 ? text.length : end + 1;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
out += ch;
|
|
86
|
+
}
|
|
87
|
+
// Trailing commas: `,` followed only by whitespace and a closer.
|
|
88
|
+
return out.replace(/,(\s*[}\]])/g, '$1');
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Whether a document looks like TOML rather than JSON.
|
|
93
|
+
*
|
|
94
|
+
* A Codex `config.toml` paste carries `[mcp_servers.name]` tables; JSON never
|
|
95
|
+
* opens a line with `[` followed by a bare key.
|
|
96
|
+
* @param text - candidate document.
|
|
97
|
+
* @returns true when the text is better read as TOML.
|
|
98
|
+
*/
|
|
99
|
+
export function looksLikeToml(text) {
|
|
100
|
+
return /^\s*\[(mcp_servers|mcpServers|servers)\./m.test(text) || /^\s*\[mcp_servers\]/m.test(text);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Parse the small TOML subset these configs use: `[table.path]` sections with
|
|
105
|
+
* `key = value` pairs, where values are strings, numbers, booleans, or arrays
|
|
106
|
+
* of strings.
|
|
107
|
+
*
|
|
108
|
+
* A full TOML parser is deliberately not pulled in: the input is a
|
|
109
|
+
* configuration file a user pasted, the grammar actually used is tiny, and a
|
|
110
|
+
* dependency here would have to be resolvable from the profile.
|
|
111
|
+
* @param text - TOML document.
|
|
112
|
+
* @returns the parsed object.
|
|
113
|
+
* @throws when a line is not part of that subset.
|
|
114
|
+
*/
|
|
115
|
+
export function parseToml(text) {
|
|
116
|
+
const root = {};
|
|
117
|
+
let current = root;
|
|
118
|
+
const lines = text.split(/\r?\n/);
|
|
119
|
+
|
|
120
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
121
|
+
const line = lines[index].replace(/(^|\s)#.*$/, '').trim();
|
|
122
|
+
if (line === '') continue;
|
|
123
|
+
|
|
124
|
+
const table = /^\[\[?([^\]]+)\]\]?$/.exec(line);
|
|
125
|
+
if (table !== null) {
|
|
126
|
+
const path = table[1].split('.').map((part) => part.trim().replace(/^"|"$/g, ''));
|
|
127
|
+
current = root;
|
|
128
|
+
for (const part of path) {
|
|
129
|
+
if (current[part] === undefined || typeof current[part] !== 'object' || current[part] === null) {
|
|
130
|
+
current[part] = {};
|
|
131
|
+
}
|
|
132
|
+
current = current[part];
|
|
133
|
+
}
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const eq = line.indexOf('=');
|
|
138
|
+
if (eq === -1) throw new Error(`TOML 第 ${index + 1} 行无法解析:${line}`);
|
|
139
|
+
const key = line.slice(0, eq).trim().replace(/^"|"$/g, '');
|
|
140
|
+
const raw = line.slice(eq + 1).trim();
|
|
141
|
+
current[key] = parseTomlValue(raw, index + 1);
|
|
142
|
+
}
|
|
143
|
+
return root;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Parse one TOML scalar or array value. */
|
|
147
|
+
function parseTomlValue(raw, lineNumber) {
|
|
148
|
+
if (raw.startsWith('"') || raw.startsWith("'")) {
|
|
149
|
+
const quote = raw[0];
|
|
150
|
+
if (!raw.endsWith(quote) || raw.length < 2) throw new Error(`TOML 第 ${lineNumber} 行字符串未闭合`);
|
|
151
|
+
return raw.slice(1, -1);
|
|
152
|
+
}
|
|
153
|
+
if (raw.startsWith('[')) {
|
|
154
|
+
if (!raw.endsWith(']')) throw new Error(`TOML 第 ${lineNumber} 行数组未闭合`);
|
|
155
|
+
return splitTopLevelList(raw.slice(1, -1)).map((item) => parseTomlValue(item.trim(), lineNumber));
|
|
156
|
+
}
|
|
157
|
+
if (raw === 'true') return true;
|
|
158
|
+
if (raw === 'false') return false;
|
|
159
|
+
const num = Number(raw);
|
|
160
|
+
return Number.isFinite(num) && raw !== '' ? num : raw;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Split an inline TOML/JSON array body on commas that are not inside quotes. */
|
|
164
|
+
function splitTopLevelList(body) {
|
|
165
|
+
const parts = [];
|
|
166
|
+
let depth = 0;
|
|
167
|
+
let inString = false;
|
|
168
|
+
let quote = '';
|
|
169
|
+
let buffer = '';
|
|
170
|
+
for (let i = 0; i < body.length; i += 1) {
|
|
171
|
+
const ch = body[i];
|
|
172
|
+
if (inString) {
|
|
173
|
+
buffer += ch;
|
|
174
|
+
if (ch === '\\') {
|
|
175
|
+
buffer += body[i + 1] ?? '';
|
|
176
|
+
i += 1;
|
|
177
|
+
} else if (ch === quote) inString = false;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (ch === '"' || ch === "'") {
|
|
181
|
+
inString = true;
|
|
182
|
+
quote = ch;
|
|
183
|
+
buffer += ch;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
if (ch === '[' || ch === '{') depth += 1;
|
|
187
|
+
if (ch === ']' || ch === '}') depth -= 1;
|
|
188
|
+
if (ch === ',' && depth === 0) {
|
|
189
|
+
parts.push(buffer);
|
|
190
|
+
buffer = '';
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
buffer += ch;
|
|
194
|
+
}
|
|
195
|
+
if (buffer.trim() !== '') parts.push(buffer);
|
|
196
|
+
return parts;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Decode a pasted document into an object.
|
|
201
|
+
* @param input - the raw text, or an already-decoded object.
|
|
202
|
+
* @returns the decoded object.
|
|
203
|
+
* @throws when the text is neither valid TOML nor valid JSON.
|
|
204
|
+
*/
|
|
205
|
+
export function decodeDocument(input) {
|
|
206
|
+
if (input !== null && typeof input === 'object') return input;
|
|
207
|
+
const text = String(input ?? '').trim();
|
|
208
|
+
if (text === '') throw new Error('配置内容为空');
|
|
209
|
+
|
|
210
|
+
if (looksLikeToml(text)) return parseToml(text);
|
|
211
|
+
|
|
212
|
+
try {
|
|
213
|
+
return JSON.parse(stripJsonNoise(text));
|
|
214
|
+
} catch (jsonError) {
|
|
215
|
+
// A TOML document without a recognisable MCP table still deserves a TOML
|
|
216
|
+
// error rather than a JSON one, since that is what the user pasted.
|
|
217
|
+
if (/^\s*\[/m.test(text)) {
|
|
218
|
+
try {
|
|
219
|
+
return parseToml(text);
|
|
220
|
+
} catch {
|
|
221
|
+
/* fall through to the JSON error below */
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
throw new Error(`配置解析失败:${String(jsonError?.message ?? jsonError)}`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Find the name → server map inside a decoded document.
|
|
230
|
+
*
|
|
231
|
+
* The container key differs per tool, so every known one is tried before
|
|
232
|
+
* falling back to treating the whole document as the map (which is what a
|
|
233
|
+
* snippet copied from a single server's docs usually is).
|
|
234
|
+
* @param decoded - the decoded document.
|
|
235
|
+
* @returns the container object.
|
|
236
|
+
*/
|
|
237
|
+
function findContainer(decoded) {
|
|
238
|
+
for (const key of MAP_CONTAINERS) {
|
|
239
|
+
const value = decoded[key];
|
|
240
|
+
if (value === null || typeof value !== 'object') continue;
|
|
241
|
+
if (Array.isArray(value)) return { container: value, key };
|
|
242
|
+
// `mcp` in OpenCode v2 nests one level further (`mcp.servers`).
|
|
243
|
+
if (key === 'mcp' && value.servers !== null && typeof value.servers === 'object' && !Array.isArray(value.servers)) {
|
|
244
|
+
return { container: value.servers, key };
|
|
245
|
+
}
|
|
246
|
+
return { container: value, key };
|
|
247
|
+
}
|
|
248
|
+
return { container: decoded, key: null };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Whether a value looks like a server entry rather than a nested container.
|
|
253
|
+
* @param value - candidate entry.
|
|
254
|
+
* @returns true when the object carries any transport-bearing key.
|
|
255
|
+
*/
|
|
256
|
+
function isServerEntry(value) {
|
|
257
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;
|
|
258
|
+
return (
|
|
259
|
+
value.command !== undefined ||
|
|
260
|
+
value.url !== undefined ||
|
|
261
|
+
value.serverUrl !== undefined ||
|
|
262
|
+
value.serverName !== undefined ||
|
|
263
|
+
value.type !== undefined ||
|
|
264
|
+
value.transport !== undefined
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Flatten one server entry into the fields this plugin stores.
|
|
270
|
+
*
|
|
271
|
+
* Handles the field-name variance across dialects: OpenCode's command array,
|
|
272
|
+
* Codex's `http_headers`, Pi's `transport`, Windsurf's `serverUrl`, and the
|
|
273
|
+
* `sse` type that maps onto this plugin's streamable-HTTP transport.
|
|
274
|
+
* @param raw - one entry from a pasted document.
|
|
275
|
+
* @returns the normalized `{ serverName, transport, … }` fields.
|
|
276
|
+
*/
|
|
277
|
+
export function normalizeEntry(raw) {
|
|
278
|
+
const type = typeof raw.type === 'string' ? raw.type.toLowerCase() : '';
|
|
279
|
+
const declared = typeof raw.transport === 'string' ? raw.transport.toLowerCase() : '';
|
|
280
|
+
const remote = HTTP_TYPES.has(type) || HTTP_TYPES.has(declared);
|
|
281
|
+
const stdio = STDIO_TYPES.has(type) || STDIO_TYPES.has(declared);
|
|
282
|
+
|
|
283
|
+
// A URL with no command is remote whatever the entry says; a command with no
|
|
284
|
+
// URL is local. Only an entry carrying both is decided by its declared type.
|
|
285
|
+
const url = firstString(raw.url, raw.serverUrl, raw.server_url, raw.endpoint);
|
|
286
|
+
const commandValue = raw.command;
|
|
287
|
+
const commandList = Array.isArray(commandValue) ? commandValue.filter((part) => typeof part === 'string') : null;
|
|
288
|
+
const command =
|
|
289
|
+
typeof commandValue === 'string'
|
|
290
|
+
? commandValue
|
|
291
|
+
: commandList !== null && commandList.length > 0
|
|
292
|
+
? commandList[0]
|
|
293
|
+
: '';
|
|
294
|
+
|
|
295
|
+
const isRemote = remote || (!stdio && command === '' && url !== '');
|
|
296
|
+
|
|
297
|
+
if (isRemote) {
|
|
298
|
+
return {
|
|
299
|
+
transport: 'streamable-http',
|
|
300
|
+
url,
|
|
301
|
+
headers: firstMap(raw.headers, raw.http_headers, raw.httpHeaders, raw.requestHeaders),
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return {
|
|
306
|
+
transport: 'stdio',
|
|
307
|
+
command,
|
|
308
|
+
// An array command carries its own arguments; otherwise take `args`.
|
|
309
|
+
args: commandList !== null && commandList.length > 1
|
|
310
|
+
? commandList.slice(1)
|
|
311
|
+
: Array.isArray(raw.args)
|
|
312
|
+
? raw.args.filter((part) => typeof part === 'string')
|
|
313
|
+
: [],
|
|
314
|
+
env: firstMap(raw.env, raw.environment, raw.envVars),
|
|
315
|
+
cwd: firstString(raw.cwd, raw.workingDirectory, raw.workdir),
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** The first argument that is a string, or ''. */
|
|
320
|
+
function firstString(...values) {
|
|
321
|
+
for (const value of values) {
|
|
322
|
+
if (typeof value === 'string' && value.trim() !== '') return value.trim();
|
|
323
|
+
}
|
|
324
|
+
return '';
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** The first argument that is a plain string→string map, or {}. */
|
|
328
|
+
function firstMap(...values) {
|
|
329
|
+
for (const value of values) {
|
|
330
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) continue;
|
|
331
|
+
const out = {};
|
|
332
|
+
for (const [key, inner] of Object.entries(value)) {
|
|
333
|
+
// Codex and some others allow a non-string here (a number, or a nested
|
|
334
|
+
// object for `env_http_headers`); those are not env vars we can pass on.
|
|
335
|
+
if (typeof inner === 'string') out[key] = inner;
|
|
336
|
+
else if (typeof inner === 'number' || typeof inner === 'boolean') out[key] = String(inner);
|
|
337
|
+
}
|
|
338
|
+
if (Object.keys(out).length > 0) return out;
|
|
339
|
+
}
|
|
340
|
+
return {};
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Parse a pasted configuration from any supported tool.
|
|
345
|
+
*
|
|
346
|
+
* Every dialect ends up as the same list, so the caller never needs to know
|
|
347
|
+
* which tool the paste came from.
|
|
348
|
+
* @param input - the pasted text (or an object).
|
|
349
|
+
* @returns `{ servers, source }`, where `source` names the detected dialect.
|
|
350
|
+
* @throws when nothing that looks like a server can be found.
|
|
351
|
+
*/
|
|
352
|
+
export function parseMcpConfig(input) {
|
|
353
|
+
const decoded = decodeDocument(input);
|
|
354
|
+
|
|
355
|
+
// OpenCode v2 nests the map one level down.
|
|
356
|
+
const { container, key } = findContainer(decoded);
|
|
357
|
+
const entries = [];
|
|
358
|
+
|
|
359
|
+
if (Array.isArray(container)) {
|
|
360
|
+
// Continue's form: an array whose entries carry their own `name`.
|
|
361
|
+
for (const item of container) {
|
|
362
|
+
if (!isServerEntry(item)) continue;
|
|
363
|
+
const name = firstString(item.name, item.serverName, item.id);
|
|
364
|
+
if (name === '') continue;
|
|
365
|
+
entries.push([name, item]);
|
|
366
|
+
}
|
|
367
|
+
} else {
|
|
368
|
+
for (const [name, item] of Object.entries(container)) {
|
|
369
|
+
if (CONTAINER_KEYS.has(name)) continue;
|
|
370
|
+
if (!isServerEntry(item)) continue;
|
|
371
|
+
entries.push([name, item]);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
if (entries.length === 0) throw new Error('未在配置中找到任何 MCP 服务器条目');
|
|
376
|
+
|
|
377
|
+
return {
|
|
378
|
+
source: describeSource(key, decoded),
|
|
379
|
+
servers: entries.map(([name, raw]) => {
|
|
380
|
+
const normalized = normalizeEntry(raw);
|
|
381
|
+
const serverName = firstString(raw.serverName, name);
|
|
382
|
+
return { serverName, raw, ...normalized };
|
|
383
|
+
}),
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** Name the detected dialect, for the message the user sees. */
|
|
388
|
+
function describeSource(key, decoded) {
|
|
389
|
+
if (key === 'servers') return 'VS Code';
|
|
390
|
+
if (key === 'mcp_servers') return 'Codex';
|
|
391
|
+
if (key === 'mcp') return 'OpenCode';
|
|
392
|
+
if (key === null) return '单服务器片段';
|
|
393
|
+
if (Array.isArray(decoded[key])) return 'Continue';
|
|
394
|
+
return '标准 mcpServers';
|
|
395
|
+
}
|