dsh-mcp-manager-plus 0.4.5 → 0.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 +71 -0
- package/README.en.md +128 -6
- package/README.md +102 -7
- package/lib/client.js +278 -38
- package/lib/index.js +504 -3
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -24,8 +24,17 @@
|
|
|
24
24
|
* @module dsh-mcp-manager-plus
|
|
25
25
|
*/
|
|
26
26
|
|
|
27
|
-
import {
|
|
28
|
-
|
|
27
|
+
import {
|
|
28
|
+
accessSync,
|
|
29
|
+
constants,
|
|
30
|
+
existsSync,
|
|
31
|
+
mkdirSync,
|
|
32
|
+
readFileSync,
|
|
33
|
+
renameSync,
|
|
34
|
+
statSync,
|
|
35
|
+
writeFileSync,
|
|
36
|
+
} from 'node:fs';
|
|
37
|
+
import { dirname, isAbsolute, join } from 'node:path';
|
|
29
38
|
import { fileURLToPath } from 'node:url';
|
|
30
39
|
import { homedir } from 'node:os';
|
|
31
40
|
import {
|
|
@@ -55,6 +64,14 @@ const CACHE_TTL_MS = 300;
|
|
|
55
64
|
const SECRET_KEY_RE = /KEY|PASSWORD|SECRET|TOKEN/i;
|
|
56
65
|
/** The mask the browser receives and may echo back unchanged. */
|
|
57
66
|
const SECRET_MASK = '••••••••';
|
|
67
|
+
/** Package id; also the directory name under the profile's `plugin-data`. */
|
|
68
|
+
const PLUGIN_ID = 'dsh-mcp-manager-plus';
|
|
69
|
+
/** File name of the durable project-binding table. */
|
|
70
|
+
const BINDINGS_FILE = 'bindings.json';
|
|
71
|
+
/** How long a binding read may be reused, in ms. */
|
|
72
|
+
const BINDINGS_TTL_MS = 1000;
|
|
73
|
+
/** The bridge's namespace prefix on every MCP tool name. */
|
|
74
|
+
const MCP_TOOL_PREFIX = 'mcp__';
|
|
58
75
|
/** Config keys this plugin models explicitly; every other key passes through. */
|
|
59
76
|
const STRUCTURED_KEYS = new Set([
|
|
60
77
|
'serverName',
|
|
@@ -119,6 +136,10 @@ const MSGS = {
|
|
|
119
136
|
renamedFrom: '(由 {name} 重命名)',
|
|
120
137
|
yamlDegraded:
|
|
121
138
|
'解析到的 js-yaml({source})不支持 !!js 自定义类型,已改用兼容解析;面板功能不受影响',
|
|
139
|
+
bindSaved: '项目绑定已保存:{name}',
|
|
140
|
+
bindCleared: '已解除项目绑定:{name},改为全局可用',
|
|
141
|
+
bindUnknownServer: '未找到服务器 {name}',
|
|
142
|
+
bindPathsAbsolute: '项目路径必须是绝对路径:{path}',
|
|
122
143
|
},
|
|
123
144
|
en: {
|
|
124
145
|
loopbackOnly: 'Only local (loopback) requests are allowed',
|
|
@@ -153,6 +174,10 @@ const MSGS = {
|
|
|
153
174
|
renamedFrom: ' (renamed from {name})',
|
|
154
175
|
yamlDegraded:
|
|
155
176
|
'The js-yaml in use ({source}) does not support the !!js custom type; a compatible parser is active and the panel stays fully usable',
|
|
177
|
+
bindSaved: 'Project binding saved: {name}',
|
|
178
|
+
bindCleared: 'Project binding removed: {name}; it is available everywhere again',
|
|
179
|
+
bindUnknownServer: 'Server not found: {name}',
|
|
180
|
+
bindPathsAbsolute: 'A project path must be absolute: {path}',
|
|
156
181
|
},
|
|
157
182
|
};
|
|
158
183
|
|
|
@@ -303,6 +328,401 @@ function unmaskMap(next, previous) {
|
|
|
303
328
|
return restored;
|
|
304
329
|
}
|
|
305
330
|
|
|
331
|
+
// ── project bindings ────────────────────────────────────────────────────────
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Where the durable project-binding table lives.
|
|
335
|
+
*
|
|
336
|
+
* Binding state is deliberately NOT the profile patch layer. That layer
|
|
337
|
+
* composes the running tree, so writing a binding into it would remount every
|
|
338
|
+
* MCP connection on every binding edit; bindings are ordinary plugin data,
|
|
339
|
+
* stored beside the patch file, and they take effect in the running process the
|
|
340
|
+
* moment they are written.
|
|
341
|
+
*
|
|
342
|
+
* `DSH_MCP_MANAGER_BINDINGS` overrides the location (tests, unusual installs).
|
|
343
|
+
* @param ctx - host context.
|
|
344
|
+
* @returns an absolute path.
|
|
345
|
+
*/
|
|
346
|
+
function resolveBindingsPath(ctx) {
|
|
347
|
+
const override = process.env.DSH_MCP_MANAGER_BINDINGS;
|
|
348
|
+
if (override !== undefined && override.trim() !== '') return override;
|
|
349
|
+
const { profileDir } = resolvePatchPath(ctx);
|
|
350
|
+
return join(profileDir, 'plugin-data', PLUGIN_ID, BINDINGS_FILE);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* The comparison key for a directory: separators normalized, trailing
|
|
355
|
+
* separators dropped, and case-folded on Windows, whose filesystem is
|
|
356
|
+
* case-insensitive. A leading `~` expands to the home directory.
|
|
357
|
+
* @param value - a path.
|
|
358
|
+
* @returns the key, or `''` for an empty path.
|
|
359
|
+
*/
|
|
360
|
+
function pathKey(value) {
|
|
361
|
+
let text = String(value ?? '').trim();
|
|
362
|
+
if (text === '') return '';
|
|
363
|
+
if (text === '~' || text.startsWith('~/') || text.startsWith('~\\')) {
|
|
364
|
+
text = join(homedir(), text.slice(1));
|
|
365
|
+
}
|
|
366
|
+
const trimmed = text.replace(/[\\/]+$/u, '').replace(/\\/gu, '/');
|
|
367
|
+
if (trimmed === '') return '/';
|
|
368
|
+
return process.platform === 'win32' ? trimmed.toLowerCase() : trimmed;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Whether a session directory is the bound project itself or lives inside it.
|
|
373
|
+
* @param candidate - the session's effective directory.
|
|
374
|
+
* @param root - a bound project path.
|
|
375
|
+
* @returns true when the directory is covered by the binding.
|
|
376
|
+
*/
|
|
377
|
+
function isWithin(candidate, root) {
|
|
378
|
+
const a = pathKey(candidate);
|
|
379
|
+
const b = pathKey(root);
|
|
380
|
+
if (a === '' || b === '') return false;
|
|
381
|
+
if (a === b) return true;
|
|
382
|
+
return a.startsWith(b.endsWith('/') ? b : `${b}/`);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Accept a submitted project path. A relative path is refused rather than
|
|
387
|
+
* quietly stored: it could never match a session directory, so the binding
|
|
388
|
+
* would look configured while doing nothing.
|
|
389
|
+
* @param value - the submitted path.
|
|
390
|
+
* @returns the trimmed path, or null when it is not absolute.
|
|
391
|
+
*/
|
|
392
|
+
function normalizeBindingPath(value) {
|
|
393
|
+
const text = String(value ?? '').trim();
|
|
394
|
+
if (text === '') return null;
|
|
395
|
+
if (text === '~' || text.startsWith('~/') || text.startsWith('~\\')) return text;
|
|
396
|
+
return isAbsolute(text) ? text : null;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Normalize a parsed binding table. Both the canonical shape
|
|
401
|
+
* (`{ version, servers: { name: { paths: [...] } } }`) and a hand-written
|
|
402
|
+
* shorthand (`{ name: ["C:\\dir"] }`) are accepted, and anything unusable is
|
|
403
|
+
* dropped rather than throwing.
|
|
404
|
+
* @param parsed - the decoded JSON value.
|
|
405
|
+
* @returns `{ version: 1, servers }`.
|
|
406
|
+
*/
|
|
407
|
+
function normalizeBindings(parsed) {
|
|
408
|
+
const servers = {};
|
|
409
|
+
if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
410
|
+
return { version: 1, servers };
|
|
411
|
+
}
|
|
412
|
+
const source =
|
|
413
|
+
parsed.servers !== null && typeof parsed.servers === 'object' ? parsed.servers : parsed;
|
|
414
|
+
for (const [name, entry] of Object.entries(source)) {
|
|
415
|
+
if (!/^[A-Za-z0-9_-]{1,32}$/u.test(name)) continue;
|
|
416
|
+
const raw = Array.isArray(entry)
|
|
417
|
+
? entry
|
|
418
|
+
: entry !== null && typeof entry === 'object' && Array.isArray(entry.paths)
|
|
419
|
+
? entry.paths
|
|
420
|
+
: undefined;
|
|
421
|
+
if (raw === undefined) continue;
|
|
422
|
+
const paths = [];
|
|
423
|
+
for (const item of raw) {
|
|
424
|
+
const accepted = typeof item === 'string' ? normalizeBindingPath(item) : null;
|
|
425
|
+
if (accepted === null) continue;
|
|
426
|
+
if (paths.some((existing) => pathKey(existing) === pathKey(accepted))) continue;
|
|
427
|
+
paths.push(accepted);
|
|
428
|
+
}
|
|
429
|
+
if (paths.length > 0) servers[name] = { paths };
|
|
430
|
+
}
|
|
431
|
+
return { version: 1, servers };
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/** Last binding snapshot; invalidated on write and refreshed when the file changes. */
|
|
435
|
+
let bindingsCache = null;
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Read the binding table, tolerating a missing or damaged file.
|
|
439
|
+
*
|
|
440
|
+
* A damaged table reads as "no bindings" — the pre-feature behaviour — so a bad
|
|
441
|
+
* edit can never make a user's MCP servers unusable everywhere.
|
|
442
|
+
* @param ctx - host context.
|
|
443
|
+
* @returns `{ version: 1, servers }`.
|
|
444
|
+
*/
|
|
445
|
+
function readBindings(ctx) {
|
|
446
|
+
const path = resolveBindingsPath(ctx);
|
|
447
|
+
const now = Date.now();
|
|
448
|
+
let stamp = -1;
|
|
449
|
+
try {
|
|
450
|
+
stamp = statSync(path).mtimeMs;
|
|
451
|
+
} catch {
|
|
452
|
+
stamp = -1;
|
|
453
|
+
}
|
|
454
|
+
if (
|
|
455
|
+
bindingsCache !== null &&
|
|
456
|
+
bindingsCache.path === path &&
|
|
457
|
+
bindingsCache.stamp === stamp &&
|
|
458
|
+
now - bindingsCache.at < BINDINGS_TTL_MS
|
|
459
|
+
) {
|
|
460
|
+
return bindingsCache.value;
|
|
461
|
+
}
|
|
462
|
+
let value = { version: 1, servers: {} };
|
|
463
|
+
if (stamp !== -1) {
|
|
464
|
+
try {
|
|
465
|
+
value = normalizeBindings(JSON.parse(readFileSync(path, 'utf8')));
|
|
466
|
+
} catch {
|
|
467
|
+
value = { version: 1, servers: {} };
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
bindingsCache = { path, stamp, at: now, value };
|
|
471
|
+
return value;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Replace the binding table atomically, with sorted keys so the file diffs
|
|
476
|
+
* cleanly.
|
|
477
|
+
* @param ctx - host context.
|
|
478
|
+
* @param servers - the `servers` map to persist.
|
|
479
|
+
* @returns the absolute path written.
|
|
480
|
+
*/
|
|
481
|
+
function writeBindings(ctx, servers) {
|
|
482
|
+
const path = resolveBindingsPath(ctx);
|
|
483
|
+
const sorted = {};
|
|
484
|
+
for (const name of Object.keys(servers).sort()) sorted[name] = servers[name];
|
|
485
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
486
|
+
const temp = `${path}.tmp-${process.pid}-${Date.now()}`;
|
|
487
|
+
writeFileSync(temp, `${JSON.stringify({ version: 1, servers: sorted }, null, 2)}\n`);
|
|
488
|
+
renameSync(temp, path);
|
|
489
|
+
bindingsCache = null;
|
|
490
|
+
return path;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* The active binding for one server.
|
|
495
|
+
* @param table - a table from {@link readBindings}.
|
|
496
|
+
* @param serverName - the server namespace.
|
|
497
|
+
* @returns the binding, or null when the server is global.
|
|
498
|
+
*/
|
|
499
|
+
function bindingOf(table, serverName) {
|
|
500
|
+
const entry = table.servers[serverName];
|
|
501
|
+
if (entry === undefined || !Array.isArray(entry.paths) || entry.paths.length === 0) return null;
|
|
502
|
+
return entry;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* The server namespace a bridge tool belongs to. A `serverName` may itself
|
|
507
|
+
* contain `_`, so the separating `__` is the LAST one — the bridge's own naming
|
|
508
|
+
* contract.
|
|
509
|
+
* @param name - a tool name.
|
|
510
|
+
* @returns the server name, or undefined when this is not an MCP tool.
|
|
511
|
+
*/
|
|
512
|
+
function serverOfToolName(name) {
|
|
513
|
+
if (typeof name !== 'string' || !name.startsWith(MCP_TOOL_PREFIX)) return undefined;
|
|
514
|
+
const rest = name.slice(MCP_TOOL_PREFIX.length);
|
|
515
|
+
const index = rest.lastIndexOf('__');
|
|
516
|
+
if (index <= 0) return undefined;
|
|
517
|
+
return rest.slice(0, index);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/** Every registered tool name a server currently contributes. */
|
|
521
|
+
function toolNamesOfServer(ctx, serverName) {
|
|
522
|
+
const prefix = `${MCP_TOOL_PREFIX}${serverName}__`;
|
|
523
|
+
const names = [];
|
|
524
|
+
for (const schema of schemasOf(ctx)) {
|
|
525
|
+
const name = schema?.name;
|
|
526
|
+
if (typeof name === 'string' && name.startsWith(prefix)) names.push(name);
|
|
527
|
+
}
|
|
528
|
+
return names;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* The directory a session runs in.
|
|
533
|
+
*
|
|
534
|
+
* `workingDirectory` is the live owner of the effective directory and is
|
|
535
|
+
* preferred; the session header's original `cwd` is the fallback, and the
|
|
536
|
+
* deployment default is the last resort. An undeterminable directory leaves the
|
|
537
|
+
* caller unenforced, so an unknown directory never locks a user out of their
|
|
538
|
+
* own servers.
|
|
539
|
+
* @param ctx - host context.
|
|
540
|
+
* @param agent - the agent on whose behalf a call runs.
|
|
541
|
+
* @returns an absolute directory, or undefined when none can be read.
|
|
542
|
+
*/
|
|
543
|
+
function cwdOfAgent(ctx, agent) {
|
|
544
|
+
const session = agent !== null && typeof agent === 'object' ? agent.session : undefined;
|
|
545
|
+
let wd;
|
|
546
|
+
try {
|
|
547
|
+
wd = ctx.get('workingDirectory');
|
|
548
|
+
} catch {
|
|
549
|
+
wd = undefined;
|
|
550
|
+
}
|
|
551
|
+
if (wd !== undefined && session !== undefined && typeof wd.get === 'function') {
|
|
552
|
+
try {
|
|
553
|
+
const dir = wd.get(session);
|
|
554
|
+
if (typeof dir === 'string' && dir !== '') return dir;
|
|
555
|
+
} catch {
|
|
556
|
+
// Fall through to the session header.
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
const header = session !== undefined ? session.header : undefined;
|
|
560
|
+
if (header !== undefined && typeof header.cwd === 'string' && header.cwd !== '') return header.cwd;
|
|
561
|
+
if (wd !== undefined && typeof wd.defaultDirectory === 'string' && wd.defaultDirectory !== '') {
|
|
562
|
+
return wd.defaultDirectory;
|
|
563
|
+
}
|
|
564
|
+
return undefined;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/** The denial a guard returns: the model learns why, and where to change it. */
|
|
568
|
+
function bindingDenial(toolName, serverName, cwd, paths) {
|
|
569
|
+
return (
|
|
570
|
+
`MCP 项目绑定:工具 ${toolName} 在当前项目不可用(当前目录 ${cwd})。` +
|
|
571
|
+
`服务器 "${serverName}" 只绑定到:${paths.join('、')}。可在「设置 → MCP 管理 → 项目绑定」中修改。` +
|
|
572
|
+
` / Project binding: "${serverName}" is bound to ${paths.join(', ')}, so ${toolName} is unavailable in ${cwd}.`
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/** The agent-scoped restriction last applied to each live agent. */
|
|
577
|
+
const agentBindings = new WeakMap();
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* Bring one agent's tool visibility in line with the binding table.
|
|
581
|
+
*
|
|
582
|
+
* A restricted server's tools are hidden from that agent with an agent-scoped
|
|
583
|
+
* `tools.restrict()` — the only context in which that call is legal. The work is
|
|
584
|
+
* skipped when nothing changed, so calling it on every step is cheap. Nothing
|
|
585
|
+
* here may throw: a failure inside an `agent/pre-step` listener would fail the
|
|
586
|
+
* step.
|
|
587
|
+
* @param ctx - host context.
|
|
588
|
+
* @param agent - the agent to bring up to date.
|
|
589
|
+
*/
|
|
590
|
+
function syncAgentBindings(ctx, agent) {
|
|
591
|
+
try {
|
|
592
|
+
if (agent === null || typeof agent !== 'object') return;
|
|
593
|
+
const agentCtx = agent.ctx;
|
|
594
|
+
if (agentCtx === undefined || agentCtx === null) return;
|
|
595
|
+
let scoped;
|
|
596
|
+
try {
|
|
597
|
+
scoped = agentCtx.tools;
|
|
598
|
+
} catch {
|
|
599
|
+
scoped = undefined;
|
|
600
|
+
}
|
|
601
|
+
if (scoped === undefined && typeof agentCtx.get === 'function') {
|
|
602
|
+
try {
|
|
603
|
+
scoped = agentCtx.get('tools');
|
|
604
|
+
} catch {
|
|
605
|
+
scoped = undefined;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
if (scoped === undefined || typeof scoped.restrict !== 'function') return;
|
|
609
|
+
|
|
610
|
+
const cwd = cwdOfAgent(ctx, agent);
|
|
611
|
+
const known = cwd !== undefined && cwd !== '';
|
|
612
|
+
const table = readBindings(ctx);
|
|
613
|
+
const deny = [];
|
|
614
|
+
for (const serverName of Object.keys(table.servers)) {
|
|
615
|
+
const binding = bindingOf(table, serverName);
|
|
616
|
+
if (binding === null) continue;
|
|
617
|
+
// Fail open, exactly like the per-call guard: when this session's
|
|
618
|
+
// directory cannot be determined we hide nothing, rather than locking a
|
|
619
|
+
// user out of a project the plugin failed to recognize. Hiding the tools
|
|
620
|
+
// while the guard still allows the call would also be self-contradictory.
|
|
621
|
+
if (!known) continue;
|
|
622
|
+
if (binding.paths.some((root) => isWithin(cwd, root))) continue;
|
|
623
|
+
for (const name of toolNamesOfServer(ctx, serverName)) deny.push(name);
|
|
624
|
+
}
|
|
625
|
+
deny.sort();
|
|
626
|
+
|
|
627
|
+
const state = agentBindings.get(agent);
|
|
628
|
+
const signature = deny.join('\n');
|
|
629
|
+
if (state !== undefined && state.signature === signature) return;
|
|
630
|
+
if (state !== undefined) {
|
|
631
|
+
for (const dispose of state.disposers) {
|
|
632
|
+
try {
|
|
633
|
+
dispose();
|
|
634
|
+
} catch {
|
|
635
|
+
// A disposer that fails must not stop the new restriction.
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
const disposers = [];
|
|
640
|
+
if (deny.length > 0) {
|
|
641
|
+
try {
|
|
642
|
+
disposers.push(scoped.restrict({ deny }));
|
|
643
|
+
} catch {
|
|
644
|
+
// An empty filter, an unknown name, or a registration that vanished
|
|
645
|
+
// between the listing and the call: the per-call guard still enforces
|
|
646
|
+
// the binding, so losing the schema filter is not a hole.
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
agentBindings.set(agent, { signature, disposers });
|
|
650
|
+
} catch {
|
|
651
|
+
// Never let enforcement break a step.
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/** Bring every live agent up to date, after a binding edit. */
|
|
656
|
+
function syncAllAgents(ctx) {
|
|
657
|
+
let agents;
|
|
658
|
+
try {
|
|
659
|
+
agents = ctx.get('agents');
|
|
660
|
+
} catch {
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
if (agents === undefined || typeof agents.list !== 'function') return;
|
|
664
|
+
let list;
|
|
665
|
+
try {
|
|
666
|
+
list = agents.list();
|
|
667
|
+
} catch {
|
|
668
|
+
return;
|
|
669
|
+
}
|
|
670
|
+
if (!Array.isArray(list)) return;
|
|
671
|
+
for (const agent of list) syncAgentBindings(ctx, agent);
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
/**
|
|
675
|
+
* Install project-binding enforcement.
|
|
676
|
+
*
|
|
677
|
+
* Two independent layers, because they cover different things:
|
|
678
|
+
* - a global monotonic `tools.guard`, denying a bound server's tool call from a
|
|
679
|
+
* session outside its projects. This is the hard rule, and it also covers
|
|
680
|
+
* sessions that already existed, a directory changed mid-session, and the
|
|
681
|
+
* moment before a scope restriction is re-applied;
|
|
682
|
+
* - agent-scoped `tools.restrict`, applied at `agent/created` and re-checked at
|
|
683
|
+
* every `agent/pre-step`, which keeps the tools out of the model's schema list
|
|
684
|
+
* in the first place.
|
|
685
|
+
* @param ctx - host context.
|
|
686
|
+
*/
|
|
687
|
+
function applyProjectBindings(ctx) {
|
|
688
|
+
let tools;
|
|
689
|
+
try {
|
|
690
|
+
tools = ctx.get('tools');
|
|
691
|
+
} catch {
|
|
692
|
+
tools = undefined;
|
|
693
|
+
}
|
|
694
|
+
if (tools !== undefined && typeof tools.guard === 'function') {
|
|
695
|
+
ctx.effect(
|
|
696
|
+
() =>
|
|
697
|
+
tools.guard((exec) => {
|
|
698
|
+
const serverName = serverOfToolName(exec?.name);
|
|
699
|
+
if (serverName === undefined) return undefined;
|
|
700
|
+
const binding = bindingOf(readBindings(ctx), serverName);
|
|
701
|
+
if (binding === null) return undefined;
|
|
702
|
+
const cwd = cwdOfAgent(ctx, exec.agent);
|
|
703
|
+
if (cwd === undefined || cwd === '') return undefined;
|
|
704
|
+
if (binding.paths.some((root) => isWithin(cwd, root))) return undefined;
|
|
705
|
+
return bindingDenial(exec.name, serverName, cwd, binding.paths);
|
|
706
|
+
}),
|
|
707
|
+
'dsh-mcp-manager-plus: project binding guard',
|
|
708
|
+
);
|
|
709
|
+
}
|
|
710
|
+
if (typeof ctx.on !== 'function') return;
|
|
711
|
+
ctx.effect(() => {
|
|
712
|
+
const offCreated = ctx.on('agent/created', (payload) => {
|
|
713
|
+
syncAgentBindings(ctx, payload?.agent);
|
|
714
|
+
});
|
|
715
|
+
const offStep = ctx.on('agent/pre-step', (payload, next) => {
|
|
716
|
+
syncAgentBindings(ctx, payload?.agent);
|
|
717
|
+
return typeof next === 'function' ? next() : undefined;
|
|
718
|
+
});
|
|
719
|
+
return () => {
|
|
720
|
+
if (typeof offCreated === 'function') offCreated();
|
|
721
|
+
if (typeof offStep === 'function') offStep();
|
|
722
|
+
};
|
|
723
|
+
}, 'dsh-mcp-manager-plus: project binding scope');
|
|
724
|
+
}
|
|
725
|
+
|
|
306
726
|
// ── live-state reads ────────────────────────────────────────────────────────
|
|
307
727
|
|
|
308
728
|
/** Cordis fiber states, mirrored as the readable phase the browser shows. */
|
|
@@ -418,6 +838,12 @@ function buildInventory(ctx, document, lang = 'zh') {
|
|
|
418
838
|
const issues = [...document.issues];
|
|
419
839
|
const servers = [];
|
|
420
840
|
const claimed = new Set();
|
|
841
|
+
const table = readBindings(ctx);
|
|
842
|
+
/** The binding projection the page renders for one server. */
|
|
843
|
+
const bindingsOf = (name) => {
|
|
844
|
+
const binding = bindingOf(table, name);
|
|
845
|
+
return { paths: binding === null ? [] : [...binding.paths], restricted: binding !== null };
|
|
846
|
+
};
|
|
421
847
|
|
|
422
848
|
for (const row of document.rows) {
|
|
423
849
|
const config = row.config;
|
|
@@ -446,6 +872,7 @@ function buildInventory(ctx, document, lang = 'zh') {
|
|
|
446
872
|
cwd: typeof config.cwd === 'string' ? config.cwd : '',
|
|
447
873
|
url: typeof config.url === 'string' ? config.url : '',
|
|
448
874
|
headers: maskMap(config.headers),
|
|
875
|
+
binding: bindingsOf(serverName),
|
|
449
876
|
live: {
|
|
450
877
|
mounted: entry !== undefined,
|
|
451
878
|
phase: phaseOf(entry, disabled),
|
|
@@ -488,6 +915,7 @@ function buildInventory(ctx, document, lang = 'zh') {
|
|
|
488
915
|
cwd: typeof config.cwd === 'string' ? config.cwd : '',
|
|
489
916
|
url: typeof config.url === 'string' ? config.url : '',
|
|
490
917
|
headers: maskMap(config.headers),
|
|
918
|
+
binding: bindingsOf(serverName),
|
|
491
919
|
live: {
|
|
492
920
|
mounted: entry.fiber !== undefined,
|
|
493
921
|
phase: phaseOf(entry, off),
|
|
@@ -519,6 +947,7 @@ function buildInventory(ctx, document, lang = 'zh') {
|
|
|
519
947
|
cwd: '',
|
|
520
948
|
url: '',
|
|
521
949
|
headers: {},
|
|
950
|
+
binding: { paths: [], restricted: false },
|
|
522
951
|
live: { mounted: true, phase: 'active', toolCount: tools.length, tools },
|
|
523
952
|
});
|
|
524
953
|
}
|
|
@@ -816,6 +1245,65 @@ async function restartServer(ctx, input, lang = 'zh') {
|
|
|
816
1245
|
return tr(lang, 'restarted', { name: server.serverName });
|
|
817
1246
|
}
|
|
818
1247
|
|
|
1248
|
+
/**
|
|
1249
|
+
* Bind one server to a set of project directories, or clear its binding.
|
|
1250
|
+
*
|
|
1251
|
+
* An empty path list clears the binding and returns the server to "available in
|
|
1252
|
+
* every project". The server must already exist in the inventory: binding a
|
|
1253
|
+
* name nothing serves would look configured and never do anything.
|
|
1254
|
+
*
|
|
1255
|
+
* The write lands in the binding table, not the patch layer, so it takes effect
|
|
1256
|
+
* in the running process immediately — no loader generation is involved. Live
|
|
1257
|
+
* agents are re-synced here as well, so an open session sees the change on its
|
|
1258
|
+
* next step.
|
|
1259
|
+
* @param ctx - host context.
|
|
1260
|
+
* @param input - `{ serverName, paths }`.
|
|
1261
|
+
* @param lang - response language for error messages.
|
|
1262
|
+
* @returns `{ action, serverName, paths, message }`.
|
|
1263
|
+
*/
|
|
1264
|
+
async function bindServer(ctx, input, lang = 'zh') {
|
|
1265
|
+
const serverName = typeof input.serverName === 'string' ? input.serverName.trim() : '';
|
|
1266
|
+
const invalid = validateServerName(serverName, lang);
|
|
1267
|
+
if (invalid !== null) throw new Error(invalid);
|
|
1268
|
+
|
|
1269
|
+
const { patchPath } = resolvePatchPath(ctx);
|
|
1270
|
+
const { yaml, schema } = await loadYaml(ctx);
|
|
1271
|
+
const document = readDocument(readPatchText(patchPath), yaml, schema);
|
|
1272
|
+
const known = buildInventory(ctx, document, lang).servers.some(
|
|
1273
|
+
(server) => server.serverName === serverName,
|
|
1274
|
+
);
|
|
1275
|
+
if (!known) throw new Error(tr(lang, 'bindUnknownServer', { name: serverName }));
|
|
1276
|
+
|
|
1277
|
+
const submitted = Array.isArray(input.paths) ? input.paths : [];
|
|
1278
|
+
const paths = [];
|
|
1279
|
+
for (const item of submitted) {
|
|
1280
|
+
if (typeof item !== 'string' || item.trim() === '') continue;
|
|
1281
|
+
const accepted = normalizeBindingPath(item);
|
|
1282
|
+
if (accepted === null) {
|
|
1283
|
+
throw new Error(tr(lang, 'bindPathsAbsolute', { path: item.trim() }));
|
|
1284
|
+
}
|
|
1285
|
+
if (paths.some((existing) => pathKey(existing) === pathKey(accepted))) continue;
|
|
1286
|
+
paths.push(accepted);
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
const table = readBindings(ctx);
|
|
1290
|
+
const servers = { ...table.servers };
|
|
1291
|
+
if (paths.length === 0) delete servers[serverName];
|
|
1292
|
+
else servers[serverName] = { paths };
|
|
1293
|
+
writeBindings(ctx, servers);
|
|
1294
|
+
syncAllAgents(ctx);
|
|
1295
|
+
|
|
1296
|
+
return {
|
|
1297
|
+
action: paths.length === 0 ? 'cleared' : 'bound',
|
|
1298
|
+
serverName,
|
|
1299
|
+
paths,
|
|
1300
|
+
message:
|
|
1301
|
+
paths.length === 0
|
|
1302
|
+
? tr(lang, 'bindCleared', { name: serverName })
|
|
1303
|
+
: tr(lang, 'bindSaved', { name: serverName }),
|
|
1304
|
+
};
|
|
1305
|
+
}
|
|
1306
|
+
|
|
819
1307
|
/**
|
|
820
1308
|
* Import servers from a pasted configuration document. The document may come
|
|
821
1309
|
* from any of the tools this module's parser understands — Claude Code, Cursor,
|
|
@@ -975,7 +1463,11 @@ function sendJson(res, status, value) {
|
|
|
975
1463
|
}
|
|
976
1464
|
|
|
977
1465
|
export const name = 'dsh-mcp-manager-plus';
|
|
978
|
-
|
|
1466
|
+
// `tools` is a hard dependency, not a `ctx.get` probe: the project-binding
|
|
1467
|
+
// guard and the agent-scoped restrictions are the whole of this plugin's
|
|
1468
|
+
// enforcement, and a declared service parks the plugin until its provider
|
|
1469
|
+
// exists rather than silently applying with no enforcer.
|
|
1470
|
+
export const inject = ['webServer', 'tools'];
|
|
979
1471
|
|
|
980
1472
|
/**
|
|
981
1473
|
* Register the management API on the harness web server.
|
|
@@ -1010,6 +1502,7 @@ export function apply(ctx) {
|
|
|
1010
1502
|
}
|
|
1011
1503
|
const value = {
|
|
1012
1504
|
patchPath,
|
|
1505
|
+
bindingsPath: resolveBindingsPath(ctx),
|
|
1013
1506
|
profileDir,
|
|
1014
1507
|
writable,
|
|
1015
1508
|
servers: inventory.servers,
|
|
@@ -1062,6 +1555,12 @@ export function apply(ctx) {
|
|
|
1062
1555
|
sendJson(res, 200, { ok: true, message, state: await listServers(lang) });
|
|
1063
1556
|
return;
|
|
1064
1557
|
}
|
|
1558
|
+
if (route === '/bind' && req.method === 'POST') {
|
|
1559
|
+
const result = await bindServer(ctx, await readJsonBody(req, lang), lang);
|
|
1560
|
+
cache = null;
|
|
1561
|
+
sendJson(res, 200, { ok: true, ...result, state: await listServers(lang) });
|
|
1562
|
+
return;
|
|
1563
|
+
}
|
|
1065
1564
|
if (route === '/import' && req.method === 'POST') {
|
|
1066
1565
|
const result = await importServers(ctx, await readJsonBody(req, lang), lang);
|
|
1067
1566
|
cache = null;
|
|
@@ -1083,6 +1582,8 @@ export function apply(ctx) {
|
|
|
1083
1582
|
}),
|
|
1084
1583
|
'dsh-mcp-manager-plus: management API',
|
|
1085
1584
|
);
|
|
1585
|
+
|
|
1586
|
+
applyProjectBindings(ctx);
|
|
1086
1587
|
}
|
|
1087
1588
|
|
|
1088
1589
|
export default { name, inject, apply };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-mcp-manager-plus",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "MCP manager settings page for DeepSeek Harness: list, enable, configure and add MCP servers from the browser, written durably to the profile's user patch layer.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"icon": "./icon.svg",
|