dsh-project-mcp 0.4.0 → 0.4.1
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/lib/index.d.ts +83 -4
- package/lib/index.js +125 -15
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/bridge.ts +114 -7
- package/src/runtime.ts +54 -12
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-project-mcp",
|
|
3
3
|
"description": "Per-project MCP servers for DeepSeek Harness: mounts the MCP servers a project declares into the sessions working in it",
|
|
4
|
-
"version": "0.4.
|
|
4
|
+
"version": "0.4.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
7
7
|
"types": "lib/index.d.ts",
|
package/src/bridge.ts
CHANGED
|
@@ -33,7 +33,11 @@
|
|
|
33
33
|
* it level with the project's live set: the set is re-read on every trigger,
|
|
34
34
|
* the batch is replaced as a whole (released before the replacement is
|
|
35
35
|
* registered), and the re-entry every registration forces on the sync through
|
|
36
|
-
* `tools/change` is owned rather than left to tear the batch down.
|
|
36
|
+
* `tools/change` is owned rather than left to tear the batch down. Because that
|
|
37
|
+
* event is emitted once per entry registered and once per entry disposed, the
|
|
38
|
+
* subscription takes {@link ForwardBatch.note} — which folds a whole burst into
|
|
39
|
+
* one queued pass — and never {@link ForwardBatch.sync}, whose work belongs to
|
|
40
|
+
* the frame that asked for it.
|
|
37
41
|
*
|
|
38
42
|
* The module is import-free of `@deepseek-ai/dsh-tools` on purpose, exactly like
|
|
39
43
|
* {@link ./activation.ts}: the registry, definition and schema surfaces are
|
|
@@ -320,8 +324,30 @@ export interface ForwardBatchOptions {
|
|
|
320
324
|
* @returns the exact releaser that drops the whole batch.
|
|
321
325
|
*/
|
|
322
326
|
readonly register: (names: readonly string[]) => () => void
|
|
327
|
+
/**
|
|
328
|
+
* Report a failure thrown by a pass this batch ran on its own.
|
|
329
|
+
*
|
|
330
|
+
* A pass requested by {@link ForwardBatch.note} runs off the caller's stack:
|
|
331
|
+
* the registry call that announced the change has already returned, and so
|
|
332
|
+
* has every frame that could have propagated the failure. Without this the
|
|
333
|
+
* batch would go quiet — no forwarder, and no trace of why.
|
|
334
|
+
* @param error - what the scheduled pass threw.
|
|
335
|
+
*/
|
|
336
|
+
readonly onError?: (error: unknown) => void
|
|
323
337
|
}
|
|
324
338
|
|
|
339
|
+
/**
|
|
340
|
+
* How many synchronous passes one {@link ForwardBatch.sync} may run.
|
|
341
|
+
*
|
|
342
|
+
* Two is the ordinary ceiling: a pass that replaced the batch is re-entered once
|
|
343
|
+
* by its own announcements and short-circuits on the second round, and a pass
|
|
344
|
+
* that found the set unchanged exits on the first. The extra room is for a set
|
|
345
|
+
* that genuinely moved while the batch was registering — and the ceiling itself
|
|
346
|
+
* is what stops a registry that keeps moving under the batch from holding the
|
|
347
|
+
* event loop for as long as it likes.
|
|
348
|
+
*/
|
|
349
|
+
export const MAX_SYNC_PASSES = 4
|
|
350
|
+
|
|
325
351
|
/**
|
|
326
352
|
* One session's live forwarding batch, kept level with the project's live tools.
|
|
327
353
|
*
|
|
@@ -334,7 +360,8 @@ export interface ForwardBatchOptions {
|
|
|
334
360
|
* which the live first copy refuses as a duplicate. A bridge that steps aside
|
|
335
361
|
* for a duplicate (it must, for the session's own tools) then drops those names
|
|
336
362
|
* silently, and a full pass records `namesIn` as the whole set, so the loss is
|
|
337
|
-
* frozen for every later pass. That is the defect this class exists to
|
|
363
|
+
* frozen for every later pass. That is the first defect this class exists to
|
|
364
|
+
* close.
|
|
338
365
|
*
|
|
339
366
|
* So: a pass requested from inside a pass only marks the batch dirty, and the
|
|
340
367
|
* frame that owns the batch runs one more pass when it is complete — which is
|
|
@@ -342,6 +369,23 @@ export interface ForwardBatchOptions {
|
|
|
342
369
|
* the old batch is released **before** the new one is registered, because one
|
|
343
370
|
* layer holds one entry per name: registering over a live batch would refuse
|
|
344
371
|
* every name it already had.
|
|
372
|
+
*
|
|
373
|
+
* Owning the re-entry is not enough on its own, and the shape of the event is
|
|
374
|
+
* why. Replacing a batch of N entries announces 2·N changes — one per disposal
|
|
375
|
+
* and one per registration — and the event carries no origin, so every session
|
|
376
|
+
* watching the stream runs a pass for every one of them, and every pass begins
|
|
377
|
+
* by projecting the project's whole catalog, which the registry answers by deep
|
|
378
|
+
* cloning each visible definition's JSON schema. The registry per pass is what
|
|
379
|
+
* makes that cost real: one MCP generation swap was measured holding the event
|
|
380
|
+
* loop for the full 54 seconds of a CPU profile (47% of its samples inside this
|
|
381
|
+
* sync, 52% in the collector the projections feed), while the plugin's own
|
|
382
|
+
* frames were 0.46% of it. So {@link note} is the seat a `tools/change`
|
|
383
|
+
* subscription takes, not {@link sync}: a change that arrives during a pass is
|
|
384
|
+
* absorbed by it, and a burst that arrives between passes costs **one** queued
|
|
385
|
+
* pass however many events it carries. A stale forwarder is safe in the window
|
|
386
|
+
* that opens — it resolves the project entry at call time and fails closed when
|
|
387
|
+
* there is none (`forwarder`) — and every read path brings the batch level
|
|
388
|
+
* before it reads, so a reader never waits for the queue.
|
|
345
389
|
*/
|
|
346
390
|
export class ForwardBatch {
|
|
347
391
|
/** The names the live batch was registered for; `undefined` before the first pass. */
|
|
@@ -352,6 +396,8 @@ export class ForwardBatch {
|
|
|
352
396
|
#building = false
|
|
353
397
|
/** Whether a pass asked for while {@link #building} still needs a run. */
|
|
354
398
|
#again = false
|
|
399
|
+
/** Whether a scheduled pass is already waiting for a burst of changes. */
|
|
400
|
+
#queued = false
|
|
355
401
|
|
|
356
402
|
constructor(private readonly options: ForwardBatchOptions) {}
|
|
357
403
|
|
|
@@ -367,11 +413,12 @@ export class ForwardBatch {
|
|
|
367
413
|
}
|
|
368
414
|
|
|
369
415
|
/**
|
|
370
|
-
* Bring the session's layer level with the project's live tool set.
|
|
416
|
+
* Bring the session's layer level with the project's live tool set, now.
|
|
371
417
|
*
|
|
372
418
|
* Cheap when it already is: an unchanged set short-circuits and leaves the
|
|
373
419
|
* live batch — and its generation of definitions — untouched. Re-entrant
|
|
374
|
-
* while registering
|
|
420
|
+
* while registering, and bounded to {@link MAX_SYNC_PASSES} synchronous
|
|
421
|
+
* rounds so no registry state can hold the caller's frame: see the class note.
|
|
375
422
|
* @returns nothing; a failure from the probe or the registry propagates.
|
|
376
423
|
*/
|
|
377
424
|
sync(): void {
|
|
@@ -379,17 +426,47 @@ export class ForwardBatch {
|
|
|
379
426
|
this.#again = true
|
|
380
427
|
return
|
|
381
428
|
}
|
|
429
|
+
// A queued pass would read the very registry this one is about to read, so
|
|
430
|
+
// it is dropped here rather than left to repeat the probe.
|
|
431
|
+
this.#queued = false
|
|
382
432
|
this.#building = true
|
|
383
433
|
try {
|
|
384
|
-
|
|
434
|
+
for (let pass = 0; pass < MAX_SYNC_PASSES; pass += 1) {
|
|
385
435
|
this.#again = false
|
|
386
436
|
this.#syncOnce()
|
|
387
|
-
|
|
437
|
+
if (!this.#again) return
|
|
438
|
+
}
|
|
439
|
+
// Still moving after a full round: hand the rest to the queue. A registry
|
|
440
|
+
// that keeps changing under the batch must not hold the caller's frame.
|
|
441
|
+
this.#schedule(true)
|
|
388
442
|
} finally {
|
|
389
443
|
this.#building = false
|
|
390
444
|
}
|
|
391
445
|
}
|
|
392
446
|
|
|
447
|
+
/**
|
|
448
|
+
* Note that the registry changed, and bring the batch level — off this stack.
|
|
449
|
+
*
|
|
450
|
+
* This is the seat a `tools/change` subscription takes, and the reason it is
|
|
451
|
+
* not {@link sync}: the event is emitted once per entry registered and once
|
|
452
|
+
* per entry disposed, from inside the registry's own insertion and removal,
|
|
453
|
+
* so one MCP generation swap is a burst of thousands. A change that arrives
|
|
454
|
+
* while a pass is running is absorbed by that pass — it re-reads the set it is
|
|
455
|
+
* replacing before it returns — and a burst that arrives between passes costs
|
|
456
|
+
* one pass: the first queues, every later one folds into it.
|
|
457
|
+
*
|
|
458
|
+
* Nothing reads a stale layer for it. Every read path brings the batch level
|
|
459
|
+
* first (`src/runtime.ts`, `mountedSchemas`), and the queued pass has run
|
|
460
|
+
* before any turn that could read the layer at all.
|
|
461
|
+
*/
|
|
462
|
+
note(): void {
|
|
463
|
+
if (this.#building) {
|
|
464
|
+
this.#again = true
|
|
465
|
+
return
|
|
466
|
+
}
|
|
467
|
+
this.#schedule(false)
|
|
468
|
+
}
|
|
469
|
+
|
|
393
470
|
/**
|
|
394
471
|
* Drop the live batch: the session keeps no forwarder, and the next
|
|
395
472
|
* {@link sync} starts from nothing rather than amending a stale set.
|
|
@@ -399,15 +476,45 @@ export class ForwardBatch {
|
|
|
399
476
|
* session is subscribed to that event; a pass re-entered from there must not
|
|
400
477
|
* resurrect the batch it is watching being dropped. So the guard is held for
|
|
401
478
|
* the whole release, and the pass it queues is discarded rather than run.
|
|
479
|
+
*
|
|
480
|
+
* The guard is *held*, not taken: a drop that runs inside a pass — a detach
|
|
481
|
+
* driven from a listener, a scope torn down under a registration — restores
|
|
482
|
+
* the pass's own `building` rather than clearing it. Clearing it there would
|
|
483
|
+
* let the next `tools/change` start a nested pass inside a frame that still
|
|
484
|
+
* owns one, which is exactly the re-entry this class exists to own.
|
|
402
485
|
*/
|
|
403
486
|
drop(): void {
|
|
487
|
+
const building = this.#building
|
|
404
488
|
this.#building = true
|
|
405
489
|
try {
|
|
406
490
|
this.#releaseBatch()
|
|
407
491
|
} finally {
|
|
408
|
-
this.#building =
|
|
492
|
+
this.#building = building
|
|
409
493
|
this.#again = false
|
|
494
|
+
this.#queued = false
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Queue the one deferred pass a burst of changes needs.
|
|
500
|
+
* @param yielding - run it on the timer queue rather than the microtask queue.
|
|
501
|
+
* A pass that overran {@link MAX_SYNC_PASSES} asks for this: a chain of
|
|
502
|
+
* microtasks still starves timers and I/O, a round of timers does not.
|
|
503
|
+
*/
|
|
504
|
+
#schedule(yielding: boolean): void {
|
|
505
|
+
if (this.#queued) return
|
|
506
|
+
this.#queued = true
|
|
507
|
+
const run = (): void => {
|
|
508
|
+
if (!this.#queued) return
|
|
509
|
+
this.#queued = false
|
|
510
|
+
try {
|
|
511
|
+
this.sync()
|
|
512
|
+
} catch (error) {
|
|
513
|
+
this.options.onError?.(error)
|
|
514
|
+
}
|
|
410
515
|
}
|
|
516
|
+
if (yielding) setTimeout(run, 0)
|
|
517
|
+
else queueMicrotask(run)
|
|
411
518
|
}
|
|
412
519
|
|
|
413
520
|
/** One pass: read the project's set, then replace the batch when it moved. */
|
package/src/runtime.ts
CHANGED
|
@@ -2864,9 +2864,14 @@ export class ProjectMcpRuntime {
|
|
|
2864
2864
|
const servers = (): ReadonlySet<string> => this.runtimeNamesOf(state)
|
|
2865
2865
|
const batch = new ForwardBatch({
|
|
2866
2866
|
names: () => {
|
|
2867
|
+
const project = state.project
|
|
2868
|
+
// No project, nothing to probe. An empty key here would read the
|
|
2869
|
+
// *global* view instead — every tool any scope registered — and each
|
|
2870
|
+
// pass would forward names this session's project never published.
|
|
2871
|
+
if (project === undefined) return undefined
|
|
2867
2872
|
try {
|
|
2868
2873
|
return projectToolNames(
|
|
2869
|
-
|
|
2874
|
+
project.key,
|
|
2870
2875
|
(scope) => this.bridgeSchemas(scope),
|
|
2871
2876
|
servers(),
|
|
2872
2877
|
)
|
|
@@ -2879,20 +2884,27 @@ export class ProjectMcpRuntime {
|
|
|
2879
2884
|
return undefined
|
|
2880
2885
|
}
|
|
2881
2886
|
},
|
|
2882
|
-
register: (names) =>
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
|
|
2887
|
-
|
|
2887
|
+
register: (names) => {
|
|
2888
|
+
const project = state.project
|
|
2889
|
+
if (project === undefined) return () => undefined
|
|
2890
|
+
return forwardProjectTools({
|
|
2891
|
+
ctx: context,
|
|
2892
|
+
project: project.key,
|
|
2893
|
+
names,
|
|
2894
|
+
registry,
|
|
2895
|
+
})
|
|
2896
|
+
},
|
|
2897
|
+
onError: (error) => this.reportBridgeFailure(state, error),
|
|
2888
2898
|
})
|
|
2889
2899
|
const bridge: BridgeForwarders = {
|
|
2890
2900
|
batch,
|
|
2891
2901
|
detach: () => undefined,
|
|
2892
2902
|
}
|
|
2893
2903
|
// The listener lives on the session scope and is dropped with it, so a
|
|
2894
|
-
// released session never keeps syncing against a project it left.
|
|
2895
|
-
|
|
2904
|
+
// released session never keeps syncing against a project it left. It only
|
|
2905
|
+
// *notes* the change: the registry emits this once per entry registered and
|
|
2906
|
+
// once per entry disposed, and a pass per event is what held the event loop.
|
|
2907
|
+
const detach = state.scope.ctx.on('tools/change', () => this.noteBridge(state))
|
|
2896
2908
|
bridge.detach = typeof detach === 'function' ? (detach as () => void) : () => undefined
|
|
2897
2909
|
state.bridge = bridge
|
|
2898
2910
|
this.syncBridge(state)
|
|
@@ -2914,12 +2926,42 @@ export class ProjectMcpRuntime {
|
|
|
2914
2926
|
try {
|
|
2915
2927
|
bridge.batch.sync()
|
|
2916
2928
|
} catch (error) {
|
|
2917
|
-
this.
|
|
2918
|
-
`project-mcp: forwarding the tools of ${state.project?.projectRoot ?? 'the project'} into session ${state.agent.id} failed: ${errorText(error)}`,
|
|
2919
|
-
)
|
|
2929
|
+
this.reportBridgeFailure(state, error)
|
|
2920
2930
|
}
|
|
2921
2931
|
}
|
|
2922
2932
|
|
|
2933
|
+
/**
|
|
2934
|
+
* Note one registry change for one session's bridge, off the emitter's stack.
|
|
2935
|
+
*
|
|
2936
|
+
* The event this answers is emitted synchronously from inside the registry's
|
|
2937
|
+
* own insertion and removal — once per entry in, once per entry out — so one
|
|
2938
|
+
* MCP generation swap is a burst of thousands. Running the pass here would
|
|
2939
|
+
* make whoever registered a tool pay for every session watching the project,
|
|
2940
|
+
* and each pass projects the project's whole catalog. The batch folds the
|
|
2941
|
+
* burst into one queued pass instead, which is also why this is not
|
|
2942
|
+
* {@link syncBridge}: the read paths, the mount settle and the scope wiring
|
|
2943
|
+
* all bring the batch level synchronously, before anything reads the layer.
|
|
2944
|
+
* @param state - the session whose bridge noticed the change.
|
|
2945
|
+
*/
|
|
2946
|
+
private noteBridge(state: AgentState): void {
|
|
2947
|
+
state.bridge?.batch.note()
|
|
2948
|
+
}
|
|
2949
|
+
|
|
2950
|
+
/**
|
|
2951
|
+
* Write the one warning line a failed bridge pass is allowed.
|
|
2952
|
+
*
|
|
2953
|
+
* Shared by the synchronous pass and by the one a batch runs on its own: a
|
|
2954
|
+
* scheduled pass has no caller left to propagate its failure to, so the batch
|
|
2955
|
+
* hands it here through `onError` rather than swallowing it.
|
|
2956
|
+
* @param state - the session whose bridge failed.
|
|
2957
|
+
* @param error - what the pass threw.
|
|
2958
|
+
*/
|
|
2959
|
+
private reportBridgeFailure(state: AgentState, error: unknown): void {
|
|
2960
|
+
this.ctx.logger.warn(
|
|
2961
|
+
`project-mcp: forwarding the tools of ${state.project?.projectRoot ?? 'the project'} into session ${state.agent.id} failed: ${errorText(error)}`,
|
|
2962
|
+
)
|
|
2963
|
+
}
|
|
2964
|
+
|
|
2923
2965
|
/** The project scope's visible schemas, as {@link projectToolNames} reads them. */
|
|
2924
2966
|
private bridgeSchemas(scope: object): ReturnType<BridgeRegistryLike['schemas']> {
|
|
2925
2967
|
return this.bridgeRegistry().schemas(scope)
|