@alfe.ai/mcp-bundler 0.3.1 → 0.3.2
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/dist/index.cjs +61 -0
- package/dist/index.d.cts +32 -0
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +32 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +61 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -335,6 +335,7 @@ async function defaultConnect(server, ctx) {
|
|
|
335
335
|
//#region src/bundler.ts
|
|
336
336
|
const DEFAULT_IDLE_TTL_MS = 600 * 1e3;
|
|
337
337
|
const DEFAULT_IDLE_SWEEP_INTERVAL_MS = 60 * 1e3;
|
|
338
|
+
const DEFAULT_RETRY_SWEEP_INTERVAL_MS = 60 * 1e3;
|
|
338
339
|
/** First text content of an error result, for host error reporting. */
|
|
339
340
|
function extractErrorText(result) {
|
|
340
341
|
for (const item of result.content) if (item.type === "text" && typeof item.text === "string") return item.text.slice(0, 500);
|
|
@@ -355,6 +356,9 @@ var McpBundler = class {
|
|
|
355
356
|
idleTtlMs;
|
|
356
357
|
idleSweepIntervalMs;
|
|
357
358
|
idleSweepTimer;
|
|
359
|
+
retrySweepIntervalMs;
|
|
360
|
+
retrySweepTimer;
|
|
361
|
+
retrySweepInFlight = false;
|
|
358
362
|
deps;
|
|
359
363
|
onToolError;
|
|
360
364
|
onServerCrash;
|
|
@@ -365,11 +369,13 @@ var McpBundler = class {
|
|
|
365
369
|
this.logger = opts.logger;
|
|
366
370
|
this.idleTtlMs = opts.idleTtlMs ?? DEFAULT_IDLE_TTL_MS;
|
|
367
371
|
this.idleSweepIntervalMs = opts.idleSweepIntervalMs ?? DEFAULT_IDLE_SWEEP_INTERVAL_MS;
|
|
372
|
+
this.retrySweepIntervalMs = opts.retrySweepIntervalMs ?? DEFAULT_RETRY_SWEEP_INTERVAL_MS;
|
|
368
373
|
this.deps = deps ?? { connect: defaultConnect };
|
|
369
374
|
this.onToolError = opts.onToolError;
|
|
370
375
|
this.onServerCrash = opts.onServerCrash;
|
|
371
376
|
this.onServerStderr = opts.onServerStderr;
|
|
372
377
|
if (this.idleTtlMs > 0) this.startIdleSweep();
|
|
378
|
+
if (this.retrySweepIntervalMs > 0) this.startRetrySweep();
|
|
373
379
|
}
|
|
374
380
|
/** Construct a Connection with the host hooks bound to its server name. */
|
|
375
381
|
buildConnection(name, config) {
|
|
@@ -486,6 +492,35 @@ var McpBundler = class {
|
|
|
486
492
|
}
|
|
487
493
|
}));
|
|
488
494
|
}
|
|
495
|
+
/**
|
|
496
|
+
* Re-attempt every server that is NOT connected but HAS a recorded connect
|
|
497
|
+
* failure (`connected: false && lastError`). This is the self-heal path: a
|
|
498
|
+
* server that failed its first warm — because its backing credential /
|
|
499
|
+
* account / network wasn't resolvable yet — gets reconnected once that
|
|
500
|
+
* dependency appears, WITHOUT needing an operator to restart the daemon.
|
|
501
|
+
*
|
|
502
|
+
* Convergence: this only calls `ensureConnected()`, which honours each
|
|
503
|
+
* Connection's own exponential reconnect backoff (`reconnectBlockedUntilMs`,
|
|
504
|
+
* 500ms → 30s cap). A server that keeps `exit(1)`-ing fast-fails while in
|
|
505
|
+
* backoff, so repeated sweeps are cheap and never become a tight crash-loop
|
|
506
|
+
* — the backoff widens with each failure. Servers that connect on their
|
|
507
|
+
* first warm, and lazily-added servers that never warmed (no `lastError`),
|
|
508
|
+
* are left alone.
|
|
509
|
+
*
|
|
510
|
+
* Returns the statuses of the servers it attempted (empty if none needed a
|
|
511
|
+
* retry). Never throws — per-server failures are reflected in the status.
|
|
512
|
+
*/
|
|
513
|
+
async retryFailed() {
|
|
514
|
+
if (this.disposed) return [];
|
|
515
|
+
const targets = Array.from(this.connections.values()).filter((conn) => !conn.isConnected() && conn.lastErrorMessage() !== void 0);
|
|
516
|
+
if (targets.length === 0) return [];
|
|
517
|
+
return (await Promise.allSettled(targets.map(async (conn) => {
|
|
518
|
+
try {
|
|
519
|
+
await conn.ensureConnected();
|
|
520
|
+
} catch {}
|
|
521
|
+
return this.statusOf(conn);
|
|
522
|
+
}))).flatMap((r) => r.status === "fulfilled" ? [r.value] : []);
|
|
523
|
+
}
|
|
489
524
|
/** Build the live status descriptor for one connection. */
|
|
490
525
|
statusOf(conn) {
|
|
491
526
|
const status = {
|
|
@@ -615,6 +650,10 @@ var McpBundler = class {
|
|
|
615
650
|
clearInterval(this.idleSweepTimer);
|
|
616
651
|
this.idleSweepTimer = void 0;
|
|
617
652
|
}
|
|
653
|
+
if (this.retrySweepTimer) {
|
|
654
|
+
clearInterval(this.retrySweepTimer);
|
|
655
|
+
this.retrySweepTimer = void 0;
|
|
656
|
+
}
|
|
618
657
|
await Promise.allSettled(Array.from(this.connections.values()).map((c) => c.close()));
|
|
619
658
|
this.connections.clear();
|
|
620
659
|
}
|
|
@@ -626,6 +665,28 @@ var McpBundler = class {
|
|
|
626
665
|
}, this.idleSweepIntervalMs);
|
|
627
666
|
if (typeof this.idleSweepTimer === "object" && "unref" in this.idleSweepTimer) this.idleSweepTimer.unref();
|
|
628
667
|
}
|
|
668
|
+
startRetrySweep() {
|
|
669
|
+
this.retrySweepTimer = setInterval(() => {
|
|
670
|
+
if (this.retrySweepInFlight) return;
|
|
671
|
+
this.retrySweepInFlight = true;
|
|
672
|
+
this.retryFailed().then((attempted) => {
|
|
673
|
+
if (attempted.length === 0) return;
|
|
674
|
+
const healed = attempted.filter((s) => s.connected);
|
|
675
|
+
const stillDown = attempted.filter((s) => !s.connected);
|
|
676
|
+
this.logger?.debug("[mcp-bundler] retry sweep", {
|
|
677
|
+
attempted: attempted.length,
|
|
678
|
+
healed: healed.length,
|
|
679
|
+
stillDown: stillDown.length
|
|
680
|
+
});
|
|
681
|
+
for (const s of healed) this.logger?.info(`[mcp-bundler] server "${s.name}" self-healed on retry sweep`, { toolCount: s.toolCount });
|
|
682
|
+
}).catch((err) => {
|
|
683
|
+
this.logger?.warn("[mcp-bundler] retry sweep error", { err: err instanceof Error ? err.message : String(err) });
|
|
684
|
+
}).finally(() => {
|
|
685
|
+
this.retrySweepInFlight = false;
|
|
686
|
+
});
|
|
687
|
+
}, this.retrySweepIntervalMs);
|
|
688
|
+
if (typeof this.retrySweepTimer === "object" && "unref" in this.retrySweepTimer) this.retrySweepTimer.unref();
|
|
689
|
+
}
|
|
629
690
|
async sweepIdle() {
|
|
630
691
|
if (this.idleTtlMs <= 0) return;
|
|
631
692
|
const targets = [];
|
package/dist/index.d.cts
CHANGED
|
@@ -92,6 +92,15 @@ interface BundlerOptions {
|
|
|
92
92
|
idleTtlMs?: number;
|
|
93
93
|
/** Sweep interval for idle reaping (ms). Default: 60_000 (1 min). */
|
|
94
94
|
idleSweepIntervalMs?: number;
|
|
95
|
+
/**
|
|
96
|
+
* Sweep interval for re-attempting servers that failed to connect
|
|
97
|
+
* (`connected: false && lastError`), so a server whose backing
|
|
98
|
+
* credential/account/network appears AFTER the first warm self-heals without
|
|
99
|
+
* an operator restart (ms). 0 disables. Default: 60_000 (1 min). The
|
|
100
|
+
* fine-grained backoff still lives on each Connection — this is the coarse
|
|
101
|
+
* outer bound that keeps re-checking.
|
|
102
|
+
*/
|
|
103
|
+
retrySweepIntervalMs?: number;
|
|
95
104
|
/**
|
|
96
105
|
* Host hook fired when a tool call fails — thrown or `isError` result.
|
|
97
106
|
* Invoked best-effort (exceptions swallowed); must not block.
|
|
@@ -259,6 +268,9 @@ declare class McpBundler {
|
|
|
259
268
|
private readonly idleTtlMs;
|
|
260
269
|
private readonly idleSweepIntervalMs;
|
|
261
270
|
private idleSweepTimer;
|
|
271
|
+
private readonly retrySweepIntervalMs;
|
|
272
|
+
private retrySweepTimer;
|
|
273
|
+
private retrySweepInFlight;
|
|
262
274
|
private readonly deps;
|
|
263
275
|
private readonly onToolError;
|
|
264
276
|
private readonly onServerCrash;
|
|
@@ -296,6 +308,25 @@ declare class McpBundler {
|
|
|
296
308
|
* swallowed per-server (logged), so one bad server doesn't fail the batch.
|
|
297
309
|
*/
|
|
298
310
|
warmup(): Promise<void>;
|
|
311
|
+
/**
|
|
312
|
+
* Re-attempt every server that is NOT connected but HAS a recorded connect
|
|
313
|
+
* failure (`connected: false && lastError`). This is the self-heal path: a
|
|
314
|
+
* server that failed its first warm — because its backing credential /
|
|
315
|
+
* account / network wasn't resolvable yet — gets reconnected once that
|
|
316
|
+
* dependency appears, WITHOUT needing an operator to restart the daemon.
|
|
317
|
+
*
|
|
318
|
+
* Convergence: this only calls `ensureConnected()`, which honours each
|
|
319
|
+
* Connection's own exponential reconnect backoff (`reconnectBlockedUntilMs`,
|
|
320
|
+
* 500ms → 30s cap). A server that keeps `exit(1)`-ing fast-fails while in
|
|
321
|
+
* backoff, so repeated sweeps are cheap and never become a tight crash-loop
|
|
322
|
+
* — the backoff widens with each failure. Servers that connect on their
|
|
323
|
+
* first warm, and lazily-added servers that never warmed (no `lastError`),
|
|
324
|
+
* are left alone.
|
|
325
|
+
*
|
|
326
|
+
* Returns the statuses of the servers it attempted (empty if none needed a
|
|
327
|
+
* retry). Never throws — per-server failures are reflected in the status.
|
|
328
|
+
*/
|
|
329
|
+
retryFailed(): Promise<McpServerStatus[]>;
|
|
299
330
|
/** Build the live status descriptor for one connection. */
|
|
300
331
|
private statusOf;
|
|
301
332
|
/** Live status for every known server. Synchronous snapshot, no I/O. */
|
|
@@ -331,6 +362,7 @@ declare class McpBundler {
|
|
|
331
362
|
*/
|
|
332
363
|
dispose(): Promise<void>;
|
|
333
364
|
private startIdleSweep;
|
|
365
|
+
private startRetrySweep;
|
|
334
366
|
private sweepIdle;
|
|
335
367
|
}
|
|
336
368
|
//# sourceMappingURL=bundler.d.ts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/types.ts","../src/connection.ts","../src/bundler.ts","../src/tool-naming.ts","../src/store.ts","../src/manager.ts","../src/pattern-a-validator.ts"],"mappings":";;AAIA;;;AAAkD,KAAtC,eAAA,GAAkB,iBAAoB,GAAA,kBAAA;AAAkB,UAEnD,iBAAA,CAFmD;EAEnD,OAAA,EAAA,MAAA;EAOA,IAAA,CAAA,EAAA,MAAA,EAAA;EAOL,GAAA,CAAA,EAXJ,MAWI,CAAA,MAAgB,EAAA,MAAA,CAAA;EAKX,GAAA,CAAA,EAAA,MAAA;AAejB;AAaiB,UAxCA,kBAAA,CAwCe;EAaf,GAAA,EAAA,MAAM;EAAA,SAAA,CAAA,EAAA,KAAA,GAAA,iBAAA;SACO,CAAA,EAnDlB,MAmDkB,CAAA,MAAA,EAAA,MAAA,CAAA;qBACD,CAAA,EAAA,MAAA;;AAEC,KAlDlB,gBAAA,GAkDkB,OAAA,GAAA,KAAA,GAAA,iBAAA;;AAG9B;AAMA;AAkBiB,UAxEA,iBAAA,CAwEc;EAAA;UACpB,EAAA,MAAA;;
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/types.ts","../src/connection.ts","../src/bundler.ts","../src/tool-naming.ts","../src/store.ts","../src/manager.ts","../src/pattern-a-validator.ts"],"mappings":";;AAIA;;;AAAkD,KAAtC,eAAA,GAAkB,iBAAoB,GAAA,kBAAA;AAAkB,UAEnD,iBAAA,CAFmD;EAEnD,OAAA,EAAA,MAAA;EAOA,IAAA,CAAA,EAAA,MAAA,EAAA;EAOL,GAAA,CAAA,EAXJ,MAWI,CAAA,MAAgB,EAAA,MAAA,CAAA;EAKX,GAAA,CAAA,EAAA,MAAA;AAejB;AAaiB,UAxCA,kBAAA,CAwCe;EAaf,GAAA,EAAA,MAAM;EAAA,SAAA,CAAA,EAAA,KAAA,GAAA,iBAAA;SACO,CAAA,EAnDlB,MAmDkB,CAAA,MAAA,EAAA,MAAA,CAAA;qBACD,CAAA,EAAA,MAAA;;AAEC,KAlDlB,gBAAA,GAkDkB,OAAA,GAAA,KAAA,GAAA,iBAAA;;AAG9B;AAMA;AAkBiB,UAxEA,iBAAA,CAwEc;EAAA;U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IpJH,eJoJG,CAAA,EInJX,iBJmJW,EAAA;;;;;AAgE2D,iBI/JzD,cAAA,CJ+JyD,KAAA,EAAA,SI9JvD,eJ8JuD,EAAA,EAAA,OAAA,EI7J9D,eJ6J8D,CAAA,EAAA,IAAA;;;;;;AC3SzE;AAIgB,iBG0JA,iBAAA,CH1JuB,UAAA,EG0JO,iBH1JP,CAAA,EG0J2B,eH1J3B;AAcvC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -92,6 +92,15 @@ interface BundlerOptions {
|
|
|
92
92
|
idleTtlMs?: number;
|
|
93
93
|
/** Sweep interval for idle reaping (ms). Default: 60_000 (1 min). */
|
|
94
94
|
idleSweepIntervalMs?: number;
|
|
95
|
+
/**
|
|
96
|
+
* Sweep interval for re-attempting servers that failed to connect
|
|
97
|
+
* (`connected: false && lastError`), so a server whose backing
|
|
98
|
+
* credential/account/network appears AFTER the first warm self-heals without
|
|
99
|
+
* an operator restart (ms). 0 disables. Default: 60_000 (1 min). The
|
|
100
|
+
* fine-grained backoff still lives on each Connection — this is the coarse
|
|
101
|
+
* outer bound that keeps re-checking.
|
|
102
|
+
*/
|
|
103
|
+
retrySweepIntervalMs?: number;
|
|
95
104
|
/**
|
|
96
105
|
* Host hook fired when a tool call fails — thrown or `isError` result.
|
|
97
106
|
* Invoked best-effort (exceptions swallowed); must not block.
|
|
@@ -259,6 +268,9 @@ declare class McpBundler {
|
|
|
259
268
|
private readonly idleTtlMs;
|
|
260
269
|
private readonly idleSweepIntervalMs;
|
|
261
270
|
private idleSweepTimer;
|
|
271
|
+
private readonly retrySweepIntervalMs;
|
|
272
|
+
private retrySweepTimer;
|
|
273
|
+
private retrySweepInFlight;
|
|
262
274
|
private readonly deps;
|
|
263
275
|
private readonly onToolError;
|
|
264
276
|
private readonly onServerCrash;
|
|
@@ -296,6 +308,25 @@ declare class McpBundler {
|
|
|
296
308
|
* swallowed per-server (logged), so one bad server doesn't fail the batch.
|
|
297
309
|
*/
|
|
298
310
|
warmup(): Promise<void>;
|
|
311
|
+
/**
|
|
312
|
+
* Re-attempt every server that is NOT connected but HAS a recorded connect
|
|
313
|
+
* failure (`connected: false && lastError`). This is the self-heal path: a
|
|
314
|
+
* server that failed its first warm — because its backing credential /
|
|
315
|
+
* account / network wasn't resolvable yet — gets reconnected once that
|
|
316
|
+
* dependency appears, WITHOUT needing an operator to restart the daemon.
|
|
317
|
+
*
|
|
318
|
+
* Convergence: this only calls `ensureConnected()`, which honours each
|
|
319
|
+
* Connection's own exponential reconnect backoff (`reconnectBlockedUntilMs`,
|
|
320
|
+
* 500ms → 30s cap). A server that keeps `exit(1)`-ing fast-fails while in
|
|
321
|
+
* backoff, so repeated sweeps are cheap and never become a tight crash-loop
|
|
322
|
+
* — the backoff widens with each failure. Servers that connect on their
|
|
323
|
+
* first warm, and lazily-added servers that never warmed (no `lastError`),
|
|
324
|
+
* are left alone.
|
|
325
|
+
*
|
|
326
|
+
* Returns the statuses of the servers it attempted (empty if none needed a
|
|
327
|
+
* retry). Never throws — per-server failures are reflected in the status.
|
|
328
|
+
*/
|
|
329
|
+
retryFailed(): Promise<McpServerStatus[]>;
|
|
299
330
|
/** Build the live status descriptor for one connection. */
|
|
300
331
|
private statusOf;
|
|
301
332
|
/** Live status for every known server. Synchronous snapshot, no I/O. */
|
|
@@ -331,6 +362,7 @@ declare class McpBundler {
|
|
|
331
362
|
*/
|
|
332
363
|
dispose(): Promise<void>;
|
|
333
364
|
private startIdleSweep;
|
|
365
|
+
private startRetrySweep;
|
|
334
366
|
private sweepIdle;
|
|
335
367
|
}
|
|
336
368
|
//# sourceMappingURL=bundler.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/types.ts","../src/connection.ts","../src/bundler.ts","../src/tool-naming.ts","../src/store.ts","../src/manager.ts","../src/pattern-a-validator.ts"],"mappings":";;AAIA;;;AAAkD,KAAtC,eAAA,GAAkB,iBAAoB,GAAA,kBAAA;AAAkB,UAEnD,iBAAA,CAFmD;EAEnD,OAAA,EAAA,MAAA;EAOA,IAAA,CAAA,EAAA,MAAA,EAAA;EAOL,GAAA,CAAA,EAXJ,MAWI,CAAA,MAAgB,EAAA,MAAA,CAAA;EAKX,GAAA,CAAA,EAAA,MAAA;AAejB;AAaiB,UAxCA,kBAAA,CAwCe;EAaf,GAAA,EAAA,MAAM;EAAA,SAAA,CAAA,EAAA,KAAA,GAAA,iBAAA;SACO,CAAA,EAnDlB,MAmDkB,CAAA,MAAA,EAAA,MAAA,CAAA;qBACD,CAAA,EAAA,MAAA;;AAEC,KAlDlB,gBAAA,GAkDkB,OAAA,GAAA,KAAA,GAAA,iBAAA;;AAG9B;AAMA;AAkBiB,UAxEA,iBAAA,CAwEc;EAAA;UACpB,EAAA,MAAA;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/types.ts","../src/connection.ts","../src/bundler.ts","../src/tool-naming.ts","../src/store.ts","../src/manager.ts","../src/pattern-a-validator.ts"],"mappings":";;AAIA;;;AAAkD,KAAtC,eAAA,GAAkB,iBAAoB,GAAA,kBAAA;AAAkB,UAEnD,iBAAA,CAFmD;EAEnD,OAAA,EAAA,MAAA;EAOA,IAAA,CAAA,EAAA,MAAA,EAAA;EAOL,GAAA,CAAA,EAXJ,MAWI,CAAA,MAAgB,EAAA,MAAA,CAAA;EAKX,GAAA,CAAA,EAAA,MAAA;AAejB;AAaiB,UAxCA,kBAAA,CAwCe;EAaf,GAAA,EAAA,MAAM;EAAA,SAAA,CAAA,EAAA,KAAA,GAAA,iBAAA;SACO,CAAA,EAnDlB,MAmDkB,CAAA,MAAA,EAAA,MAAA,CAAA;qBACD,CAAA,EAAA,MAAA;;AAEC,KAlDlB,gBAAA,GAkDkB,OAAA,GAAA,KAAA,GAAA,iBAAA;;AAG9B;AAMA;AAkBiB,UAxEA,iBAAA,CAwEc;EAAA;UACpB,EAAA,MAAA;;EAkB4B,MAAA,EAAA,MAA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;;AAgE2D,iBI/JzD,cAAA,CJ+JyD,KAAA,EAAA,SI9JvD,eJ8JuD,EAAA,EAAA,OAAA,EI7J9D,eJ6J8D,CAAA,EAAA,IAAA;;;;;;AC3SzE;AAIgB,iBG0JA,iBAAA,CH1JuB,UAAA,EG0JO,iBH1JP,CAAA,EG0J2B,eH1J3B;AAcvC"}
|
package/dist/index.js
CHANGED
|
@@ -334,6 +334,7 @@ async function defaultConnect(server, ctx) {
|
|
|
334
334
|
//#region src/bundler.ts
|
|
335
335
|
const DEFAULT_IDLE_TTL_MS = 600 * 1e3;
|
|
336
336
|
const DEFAULT_IDLE_SWEEP_INTERVAL_MS = 60 * 1e3;
|
|
337
|
+
const DEFAULT_RETRY_SWEEP_INTERVAL_MS = 60 * 1e3;
|
|
337
338
|
/** First text content of an error result, for host error reporting. */
|
|
338
339
|
function extractErrorText(result) {
|
|
339
340
|
for (const item of result.content) if (item.type === "text" && typeof item.text === "string") return item.text.slice(0, 500);
|
|
@@ -354,6 +355,9 @@ var McpBundler = class {
|
|
|
354
355
|
idleTtlMs;
|
|
355
356
|
idleSweepIntervalMs;
|
|
356
357
|
idleSweepTimer;
|
|
358
|
+
retrySweepIntervalMs;
|
|
359
|
+
retrySweepTimer;
|
|
360
|
+
retrySweepInFlight = false;
|
|
357
361
|
deps;
|
|
358
362
|
onToolError;
|
|
359
363
|
onServerCrash;
|
|
@@ -364,11 +368,13 @@ var McpBundler = class {
|
|
|
364
368
|
this.logger = opts.logger;
|
|
365
369
|
this.idleTtlMs = opts.idleTtlMs ?? DEFAULT_IDLE_TTL_MS;
|
|
366
370
|
this.idleSweepIntervalMs = opts.idleSweepIntervalMs ?? DEFAULT_IDLE_SWEEP_INTERVAL_MS;
|
|
371
|
+
this.retrySweepIntervalMs = opts.retrySweepIntervalMs ?? DEFAULT_RETRY_SWEEP_INTERVAL_MS;
|
|
367
372
|
this.deps = deps ?? { connect: defaultConnect };
|
|
368
373
|
this.onToolError = opts.onToolError;
|
|
369
374
|
this.onServerCrash = opts.onServerCrash;
|
|
370
375
|
this.onServerStderr = opts.onServerStderr;
|
|
371
376
|
if (this.idleTtlMs > 0) this.startIdleSweep();
|
|
377
|
+
if (this.retrySweepIntervalMs > 0) this.startRetrySweep();
|
|
372
378
|
}
|
|
373
379
|
/** Construct a Connection with the host hooks bound to its server name. */
|
|
374
380
|
buildConnection(name, config) {
|
|
@@ -485,6 +491,35 @@ var McpBundler = class {
|
|
|
485
491
|
}
|
|
486
492
|
}));
|
|
487
493
|
}
|
|
494
|
+
/**
|
|
495
|
+
* Re-attempt every server that is NOT connected but HAS a recorded connect
|
|
496
|
+
* failure (`connected: false && lastError`). This is the self-heal path: a
|
|
497
|
+
* server that failed its first warm — because its backing credential /
|
|
498
|
+
* account / network wasn't resolvable yet — gets reconnected once that
|
|
499
|
+
* dependency appears, WITHOUT needing an operator to restart the daemon.
|
|
500
|
+
*
|
|
501
|
+
* Convergence: this only calls `ensureConnected()`, which honours each
|
|
502
|
+
* Connection's own exponential reconnect backoff (`reconnectBlockedUntilMs`,
|
|
503
|
+
* 500ms → 30s cap). A server that keeps `exit(1)`-ing fast-fails while in
|
|
504
|
+
* backoff, so repeated sweeps are cheap and never become a tight crash-loop
|
|
505
|
+
* — the backoff widens with each failure. Servers that connect on their
|
|
506
|
+
* first warm, and lazily-added servers that never warmed (no `lastError`),
|
|
507
|
+
* are left alone.
|
|
508
|
+
*
|
|
509
|
+
* Returns the statuses of the servers it attempted (empty if none needed a
|
|
510
|
+
* retry). Never throws — per-server failures are reflected in the status.
|
|
511
|
+
*/
|
|
512
|
+
async retryFailed() {
|
|
513
|
+
if (this.disposed) return [];
|
|
514
|
+
const targets = Array.from(this.connections.values()).filter((conn) => !conn.isConnected() && conn.lastErrorMessage() !== void 0);
|
|
515
|
+
if (targets.length === 0) return [];
|
|
516
|
+
return (await Promise.allSettled(targets.map(async (conn) => {
|
|
517
|
+
try {
|
|
518
|
+
await conn.ensureConnected();
|
|
519
|
+
} catch {}
|
|
520
|
+
return this.statusOf(conn);
|
|
521
|
+
}))).flatMap((r) => r.status === "fulfilled" ? [r.value] : []);
|
|
522
|
+
}
|
|
488
523
|
/** Build the live status descriptor for one connection. */
|
|
489
524
|
statusOf(conn) {
|
|
490
525
|
const status = {
|
|
@@ -614,6 +649,10 @@ var McpBundler = class {
|
|
|
614
649
|
clearInterval(this.idleSweepTimer);
|
|
615
650
|
this.idleSweepTimer = void 0;
|
|
616
651
|
}
|
|
652
|
+
if (this.retrySweepTimer) {
|
|
653
|
+
clearInterval(this.retrySweepTimer);
|
|
654
|
+
this.retrySweepTimer = void 0;
|
|
655
|
+
}
|
|
617
656
|
await Promise.allSettled(Array.from(this.connections.values()).map((c) => c.close()));
|
|
618
657
|
this.connections.clear();
|
|
619
658
|
}
|
|
@@ -625,6 +664,28 @@ var McpBundler = class {
|
|
|
625
664
|
}, this.idleSweepIntervalMs);
|
|
626
665
|
if (typeof this.idleSweepTimer === "object" && "unref" in this.idleSweepTimer) this.idleSweepTimer.unref();
|
|
627
666
|
}
|
|
667
|
+
startRetrySweep() {
|
|
668
|
+
this.retrySweepTimer = setInterval(() => {
|
|
669
|
+
if (this.retrySweepInFlight) return;
|
|
670
|
+
this.retrySweepInFlight = true;
|
|
671
|
+
this.retryFailed().then((attempted) => {
|
|
672
|
+
if (attempted.length === 0) return;
|
|
673
|
+
const healed = attempted.filter((s) => s.connected);
|
|
674
|
+
const stillDown = attempted.filter((s) => !s.connected);
|
|
675
|
+
this.logger?.debug("[mcp-bundler] retry sweep", {
|
|
676
|
+
attempted: attempted.length,
|
|
677
|
+
healed: healed.length,
|
|
678
|
+
stillDown: stillDown.length
|
|
679
|
+
});
|
|
680
|
+
for (const s of healed) this.logger?.info(`[mcp-bundler] server "${s.name}" self-healed on retry sweep`, { toolCount: s.toolCount });
|
|
681
|
+
}).catch((err) => {
|
|
682
|
+
this.logger?.warn("[mcp-bundler] retry sweep error", { err: err instanceof Error ? err.message : String(err) });
|
|
683
|
+
}).finally(() => {
|
|
684
|
+
this.retrySweepInFlight = false;
|
|
685
|
+
});
|
|
686
|
+
}, this.retrySweepIntervalMs);
|
|
687
|
+
if (typeof this.retrySweepTimer === "object" && "unref" in this.retrySweepTimer) this.retrySweepTimer.unref();
|
|
688
|
+
}
|
|
628
689
|
async sweepIdle() {
|
|
629
690
|
if (this.idleTtlMs <= 0) return;
|
|
630
691
|
const targets = [];
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["errMsg"],"sources":["../src/tool-naming.ts","../src/connection.ts","../src/bundler.ts","../src/store.ts","../src/manager.ts","../src/pattern-a-validator.ts"],"sourcesContent":["/**\n * Tool name sanitization and collision handling.\n *\n * OpenClaw constraint: tool names must match `[A-Za-z0-9_-]` and be ≤64 chars.\n * Pattern mirrored from `openclaw/src/agents/pi-bundle-mcp-names.ts`.\n *\n * Strategy: prefix every tool with its server name (`{server}__{tool}`),\n * sanitize disallowed chars to `_`, truncate, then suffix-disambiguate\n * (`-2`, `-3`, ...) on collision.\n */\n\nconst DISALLOWED = /[^A-Za-z0-9_-]/g;\nconst MAX_LEN = 64;\nconst SEPARATOR = '__';\n\nexport function sanitizeNameSegment(value: string): string {\n return value.replace(DISALLOWED, '_');\n}\n\nexport function buildNamespacedToolName(server: string, tool: string): string {\n const base = `${sanitizeNameSegment(server)}${SEPARATOR}${sanitizeNameSegment(tool)}`;\n if (base.length <= MAX_LEN) return base;\n // Truncate from the tool side first to keep the server prefix intact.\n const reservedForServer = sanitizeNameSegment(server).length + SEPARATOR.length;\n const toolBudget = Math.max(1, MAX_LEN - reservedForServer);\n return `${sanitizeNameSegment(server)}${SEPARATOR}${sanitizeNameSegment(tool).slice(0, toolBudget)}`;\n}\n\n/**\n * Disambiguate a candidate name against an existing set by appending `-2`, `-3`, etc.\n * Mutates nothing; returns the chosen name. Caller is responsible for inserting it\n * into the set.\n */\nexport function disambiguateAgainst(candidate: string, taken: ReadonlySet<string>): string {\n if (!taken.has(candidate)) return candidate;\n for (let i = 2; i < 1000; i += 1) {\n const suffix = `-${i.toString()}`;\n const room = MAX_LEN - suffix.length;\n const trimmed = candidate.length > room ? candidate.slice(0, room) : candidate;\n const next = `${trimmed}${suffix}`;\n if (!taken.has(next)) return next;\n }\n // Pathological: 998 collisions. Fall back to a deterministic-ish hash.\n return `${candidate.slice(0, MAX_LEN - 6)}-x${(taken.size % 1000).toString().padStart(3, '0')}`;\n}\n","import type { Logger, McpServerConfig, McpToolDescriptor, McpToolCallResult, StdioServerConfig } from './types.js';\nimport { buildNamespacedToolName } from './tool-naming.js';\n\n/** Env keys OpenClaw rejects from stdio MCP env blocks. Filter them out before spawning. */\nexport const STDIO_ENV_DENYLIST = new Set([\n 'NODE_OPTIONS',\n 'PYTHONSTARTUP',\n 'PYTHONPATH',\n 'PERL5OPT',\n 'RUBYOPT',\n 'SHELLOPTS',\n 'PS4',\n]);\n\nexport function sanitizeStdioEnv(env: Record<string, string> | undefined): Record<string, string> {\n if (!env) return {};\n const safe: Record<string, string> = {};\n for (const [k, v] of Object.entries(env)) {\n if (STDIO_ENV_DENYLIST.has(k)) continue;\n safe[k] = v;\n }\n return safe;\n}\n\n/** Per-connect context the Connection threads into the connect factory. */\nexport interface ConnectContext {\n /** Server name (store key) — for logging/attribution. */\n serverName: string;\n /**\n * When set, stdio children are spawned with `stderr: 'pipe'` and each stderr\n * line is delivered here. When absent, stderr stays `inherit` (host fd).\n */\n onStderrLine?: (line: string) => void;\n}\n\nexport interface ConnectionDeps {\n /**\n * Factory for an MCP Client connected to the given config. Injected so tests\n * can mock without spawning real processes. In production this wraps\n * `@modelcontextprotocol/sdk/client`. The context arg is optional so\n * existing test mocks keep working.\n */\n connect: (server: McpServerConfig, ctx?: ConnectContext) => Promise<McpClientHandle>;\n}\n\n/**\n * Minimal interface our connection layer needs from an MCP client. Mirrors\n * the @modelcontextprotocol/sdk Client surface but kept narrow so we can\n * mock cleanly in tests.\n */\nexport interface McpClientHandle {\n listTools(): Promise<{ name: string; description?: string; inputSchema: Record<string, unknown> }[]>;\n callTool(name: string, args: unknown, opts?: { signal?: AbortSignal }): Promise<McpToolCallResult>;\n close(): Promise<void>;\n /**\n * Register a callback fired when the underlying transport closes or errors\n * unexpectedly (e.g. the child process crashed). Lets the Connection drop\n * its dead client so the next call re-spawns. Optional so test mocks can\n * omit it.\n */\n onClose?(handler: () => void): void;\n}\n\n/**\n * One Connection per MCP server. Owns lifecycle (lazy-spawn, close, refresh-lock).\n * Refresh-lock pattern adapted from AIWerk `index.ts:219-250` — prevents\n * reconnect + `notifications/tools/list_changed` race.\n */\nexport class Connection {\n readonly name: string;\n readonly config: McpServerConfig;\n private readonly deps: ConnectionDeps;\n private readonly logger: Logger | undefined;\n\n private client: McpClientHandle | undefined;\n private tools: McpToolDescriptor[] = [];\n private connectInFlight: Promise<void> | undefined;\n private refreshInFlight = false;\n private refreshQueued = false;\n private lastUsedAt = Date.now();\n\n /** Set while `close()` runs so the transport's onclose isn't treated as a crash. */\n private closing = false;\n /** Consecutive failed connect attempts — drives reconnect backoff. */\n private consecutiveFailures = 0;\n /** Message from the most recent failed connect attempt; cleared on success. */\n private lastError: string | undefined;\n /** Epoch ms before which re-connect attempts fast-fail (crash-loop guard). */\n private reconnectBlockedUntilMs = 0;\n\n private static readonly RECONNECT_BACKOFF_BASE_MS = 500;\n private static readonly RECONNECT_BACKOFF_MAX_MS = 30_000;\n\n private readonly onUnexpectedClose: (() => void) | undefined;\n private readonly onStderrLine: ((line: string) => void) | undefined;\n\n constructor(params: {\n name: string;\n config: McpServerConfig;\n deps: ConnectionDeps;\n logger?: Logger;\n /** Fired when the transport closes unexpectedly (crash), after internal cleanup. */\n onUnexpectedClose?: () => void;\n /** Threaded to the connect factory — pipes stdio child stderr when set. */\n onStderrLine?: (line: string) => void;\n }) {\n this.name = params.name;\n this.config = params.config;\n this.deps = params.deps;\n this.logger = params.logger;\n this.onUnexpectedClose = params.onUnexpectedClose;\n this.onStderrLine = params.onStderrLine;\n }\n\n /** Returns the most recent known tool list. May be empty if the server hasn't connected yet. */\n snapshotTools(): McpToolDescriptor[] {\n return this.tools;\n }\n\n /** Whether an MCP child process / remote connection has been established. */\n isConnected(): boolean {\n return this.client !== undefined;\n }\n\n /** Tools currently advertised; 0 until the server connects + discovers. */\n toolCount(): number {\n return this.tools.length;\n }\n\n /** Consecutive failed connect attempts; 0 when healthy. */\n failureCount(): number {\n return this.consecutiveFailures;\n }\n\n /** Message from the most recent failed connect attempt, if any. */\n lastErrorMessage(): string | undefined {\n return this.lastError;\n }\n\n /** Idle timestamp for reaping. */\n idleSinceMs(): number {\n return Date.now() - this.lastUsedAt;\n }\n\n /**\n * Lazy connect + tool discovery. Safe to call concurrently; in-flight\n * connects coalesce.\n */\n async ensureConnected(): Promise<void> {\n if (this.client) return;\n if (this.connectInFlight) return this.connectInFlight;\n if (Date.now() < this.reconnectBlockedUntilMs) {\n throw new Error(\n `server \"${this.name}\" is in reconnect backoff after ${this.consecutiveFailures.toString()} failed attempt(s)`,\n );\n }\n this.connectInFlight = this.connectAndDiscover().finally(() => {\n this.connectInFlight = undefined;\n });\n return this.connectInFlight;\n }\n\n private async connectAndDiscover(): Promise<void> {\n const safeConfig = 'command' in this.config\n ? ({ ...this.config, env: sanitizeStdioEnv(this.config.env) } satisfies StdioServerConfig)\n : this.config;\n this.logger?.debug(`[mcp-bundler] connecting server \"${this.name}\"`);\n // `client` is undefined until connect() resolves; keep it here so the\n // catch can close a half-open handle. Widening the try to cover connect()\n // itself is load-bearing: a spawn/URL/auth failure throws HERE (before\n // listTools), and that is the COMMON failure mode for a freshly-added\n // server. Leaving it outside the try meant such failures never recorded\n // `lastError`, never incremented `consecutiveFailures`, and never armed\n // the reconnect backoff — so a bad server had no diagnostics and no\n // crash-loop guard.\n let client: McpClientHandle | undefined;\n try {\n client = await this.deps.connect(safeConfig, {\n serverName: this.name,\n onStderrLine: this.onStderrLine,\n });\n const advertised = await client.listTools();\n const connected = client;\n this.client = connected;\n this.closing = false;\n // Drop the dead client on an unexpected transport close so the next\n // call re-spawns instead of calling into a corpse. Bind the callback to\n // THIS handle so a late close from an already-replaced client can't wipe\n // out a freshly respawned one.\n connected.onClose?.(() => { this.handleUnexpectedClose(connected); });\n this.tools = advertised.map((t) => ({\n prefixed: buildNamespacedToolName(this.name, t.name),\n server: this.name,\n original: t.name,\n label: (t.description ?? t.name).slice(0, 80),\n description: t.description ?? '',\n parameters: t.inputSchema,\n }));\n this.lastUsedAt = Date.now();\n this.consecutiveFailures = 0;\n this.lastError = undefined;\n this.reconnectBlockedUntilMs = 0;\n this.logger?.info(`[mcp-bundler] server \"${this.name}\" connected, ${this.tools.length.toString()} tool(s)`);\n } catch (err) {\n if (client) await client.close().catch(() => undefined);\n this.consecutiveFailures += 1;\n this.lastError = err instanceof Error ? err.message : String(err);\n const backoff = Math.min(\n Connection.RECONNECT_BACKOFF_BASE_MS * 2 ** (this.consecutiveFailures - 1),\n Connection.RECONNECT_BACKOFF_MAX_MS,\n );\n this.reconnectBlockedUntilMs = Date.now() + backoff;\n throw err;\n }\n }\n\n /**\n * Handle an unexpected transport close (crash / network drop). Clears the\n * dead client + tools so the next `ensureConnected` re-spawns. No-op if we\n * initiated the close ourselves (idle reap / reconcile removal).\n */\n private handleUnexpectedClose(handle: McpClientHandle): void {\n // Ignore if we initiated the close, or if this callback belongs to a\n // client we've already replaced (stale late-fire after a respawn).\n if (this.closing || this.client !== handle) return;\n this.logger?.warn(`[mcp-bundler] server \"${this.name}\" connection closed unexpectedly; will re-spawn on next use`);\n this.client = undefined;\n this.tools = [];\n try {\n this.onUnexpectedClose?.();\n } catch {\n /* host hook must never break the connection lifecycle */\n }\n }\n\n /**\n * Re-discover tools. Used on reconnect or `tools/list_changed` notification.\n * Refresh-lock collapses concurrent refreshes; if one is in flight, the next\n * is queued (max 1 queued, since N>1 queued provides no extra freshness).\n */\n async refresh(): Promise<void> {\n if (!this.client) return this.ensureConnected();\n if (this.refreshInFlight) {\n this.refreshQueued = true;\n return;\n }\n this.refreshInFlight = true;\n try {\n const advertised = await this.client.listTools();\n this.tools = advertised.map((t) => ({\n prefixed: buildNamespacedToolName(this.name, t.name),\n server: this.name,\n original: t.name,\n label: (t.description ?? t.name).slice(0, 80),\n description: t.description ?? '',\n parameters: t.inputSchema,\n }));\n this.logger?.debug(`[mcp-bundler] server \"${this.name}\" refreshed, ${this.tools.length.toString()} tool(s)`);\n } finally {\n this.refreshInFlight = false;\n if (this.refreshQueued) {\n this.refreshQueued = false;\n // Trigger one more refresh; do not await so caller isn't blocked on cascading refreshes.\n void this.refresh().catch((err: unknown) => {\n this.logger?.warn(`[mcp-bundler] queued refresh for \"${this.name}\" failed`, { err: err instanceof Error ? err.message : String(err) });\n });\n }\n }\n }\n\n async callTool(originalName: string, args: unknown, signal?: AbortSignal): Promise<McpToolCallResult> {\n await this.ensureConnected();\n if (!this.client) throw new Error(`server \"${this.name}\" failed to connect`);\n this.lastUsedAt = Date.now();\n return this.client.callTool(originalName, args, signal ? { signal } : undefined);\n }\n\n /**\n * Close the underlying transport. Idempotent. If a connect is in flight\n * (warmup racing with reconcile-removal), wait for it to settle and then\n * close the client it produced — otherwise the child process is orphaned.\n */\n async close(): Promise<void> {\n if (this.connectInFlight) {\n await this.connectInFlight.catch(() => undefined);\n }\n // Mark this close as intentional so the transport's onclose callback\n // doesn't trip the unexpected-close re-spawn path.\n this.closing = true;\n const c = this.client;\n this.client = undefined;\n this.tools = [];\n if (c) await c.close().catch((err: unknown) => {\n this.logger?.warn(`[mcp-bundler] close error for \"${this.name}\"`, { err: err instanceof Error ? err.message : String(err) });\n });\n }\n\n /**\n * Stable hash of the config for diff detection in `reconcile`.\n * Two configs with the same hash are equivalent (no restart needed).\n */\n configFingerprint(): string {\n return JSON.stringify(this.config);\n }\n}\n\n/**\n * Build the production `connect` factory using the official MCP SDK.\n * Kept in a separate function so tests can substitute a mock without\n * pulling the SDK into the test bundle.\n */\nexport async function defaultConnect(server: McpServerConfig, ctx?: ConnectContext): Promise<McpClientHandle> {\n const { Client } = await import('@modelcontextprotocol/sdk/client/index.js');\n const client = new Client({ name: 'alfe-mcp-bundler', version: '0.0.0' }, {});\n\n if ('command' in server) {\n const stdio = server;\n const { StdioClientTransport } = await import('@modelcontextprotocol/sdk/client/stdio.js');\n const onStderrLine = ctx?.onStderrLine;\n const transport = new StdioClientTransport({\n command: stdio.command,\n args: stdio.args ?? [],\n env: { ...sanitizeStdioEnv(stdio.env) } as Record<string, string>,\n cwd: stdio.cwd,\n // Default is 'inherit' (child stderr lands on the HOST's fd 2, invisible\n // to any output monitoring). Pipe only when the host consumes it — an\n // unread pipe would back-pressure the child's stderr writes.\n ...(onStderrLine ? { stderr: 'pipe' as const } : {}),\n });\n if (onStderrLine) {\n // The SDK exposes the PassThrough immediately when stderr:'pipe', so\n // attaching before connect() captures early startup output too.\n let carry = '';\n transport.stderr?.on('data', (chunk: Buffer) => {\n const parts = (carry + chunk.toString()).split('\\n');\n carry = parts.pop() ?? '';\n for (const line of parts) {\n if (line.trim() === '') continue;\n try {\n onStderrLine(line);\n } catch {\n /* host hook must never break the transport */\n }\n }\n });\n }\n await client.connect(transport);\n } else {\n const remote = server;\n if (remote.transport === 'streamable-http') {\n const { StreamableHTTPClientTransport } = await import('@modelcontextprotocol/sdk/client/streamableHttp.js');\n const transport = new StreamableHTTPClientTransport(new URL(remote.url), {\n requestInit: { headers: remote.headers ?? {} },\n });\n await client.connect(transport);\n } else {\n // SSE is deprecated in newer MCP SDK in favor of streamable-http, but\n // some servers still only support SSE — keep transport for back-compat.\n /* eslint-disable @typescript-eslint/no-deprecated */\n const { SSEClientTransport } = await import('@modelcontextprotocol/sdk/client/sse.js');\n const transport = new SSEClientTransport(new URL(remote.url), {\n requestInit: { headers: remote.headers ?? {} },\n });\n /* eslint-enable @typescript-eslint/no-deprecated */\n await client.connect(transport);\n }\n }\n\n let closeHandler: (() => void) | undefined;\n let closed = false;\n const fireClose = () => {\n if (closed) return;\n closed = true;\n closeHandler?.();\n };\n // The high-level SDK Client proxies its transport's lifecycle callbacks.\n client.onclose = fireClose;\n client.onerror = fireClose;\n\n return {\n async listTools() {\n const result = await client.listTools();\n return result.tools.map((t) => ({\n name: t.name,\n description: t.description,\n inputSchema: t.inputSchema as Record<string, unknown>,\n }));\n },\n async callTool(name, args, opts) {\n return (await client.callTool({ name, arguments: args as Record<string, unknown> | undefined }, undefined, opts)) as McpToolCallResult;\n },\n async close() {\n closed = true; // suppress the onClose callback for an intentional close\n await client.close();\n },\n onClose(handler: () => void) {\n closeHandler = handler;\n },\n };\n}\n","import { Connection, defaultConnect, type ConnectionDeps } from './connection.js';\nimport { disambiguateAgainst } from './tool-naming.js';\nimport type {\n BundlerOptions,\n Logger,\n McpServerConfig,\n McpServerStatus,\n McpToolCallResult,\n McpToolDescriptor,\n McpToolErrorInfo,\n ReconcileDiff,\n} from './types.js';\n\nconst DEFAULT_IDLE_TTL_MS = 10 * 60 * 1000;\nconst DEFAULT_IDLE_SWEEP_INTERVAL_MS = 60 * 1000;\n\n/** First text content of an error result, for host error reporting. */\nfunction extractErrorText(result: McpToolCallResult): string {\n for (const item of result.content) {\n if (item.type === 'text' && typeof item.text === 'string') return item.text.slice(0, 500);\n }\n return '(no error text)';\n}\n\n/**\n * Provider-agnostic MCP server bundler. Holds N MCP server connections,\n * exposes a unified namespaced tool catalog, and routes calls to the right\n * server.\n *\n * Designed to be embedded in any host (OpenClaw plugin, AI proxy, Lambda).\n * Public surface is intentionally synchronous where the host needs sync\n * (snapshot, listTools), async only where I/O is unavoidable.\n */\nexport class McpBundler {\n private readonly logger: Logger | undefined;\n private readonly connections = new Map<string, Connection>();\n private readonly idleTtlMs: number;\n private readonly idleSweepIntervalMs: number;\n private idleSweepTimer: ReturnType<typeof setInterval> | undefined;\n private readonly deps: ConnectionDeps;\n private readonly onToolError: ((info: McpToolErrorInfo) => void) | undefined;\n private readonly onServerCrash: ((server: string) => void) | undefined;\n private readonly onServerStderr: ((server: string, line: string) => void) | undefined;\n private disposed = false;\n // Serialize reconcile() so concurrent callers (multiple plugin tool factory\n // ticks within the same ms) don't interleave and orphan Connections, leaking\n // child processes. Acquired via a chain-of-promises latch.\n private reconcileLatch: Promise<unknown> = Promise.resolve();\n\n constructor(opts: BundlerOptions = {}, deps?: ConnectionDeps) {\n this.logger = opts.logger;\n this.idleTtlMs = opts.idleTtlMs ?? DEFAULT_IDLE_TTL_MS;\n this.idleSweepIntervalMs = opts.idleSweepIntervalMs ?? DEFAULT_IDLE_SWEEP_INTERVAL_MS;\n this.deps = deps ?? { connect: defaultConnect };\n this.onToolError = opts.onToolError;\n this.onServerCrash = opts.onServerCrash;\n this.onServerStderr = opts.onServerStderr;\n if (this.idleTtlMs > 0) this.startIdleSweep();\n }\n\n /** Construct a Connection with the host hooks bound to its server name. */\n private buildConnection(name: string, config: McpServerConfig): Connection {\n const crash = this.onServerCrash;\n const stderr = this.onServerStderr;\n return new Connection({\n name,\n config,\n deps: this.deps,\n logger: this.logger,\n ...(crash ? { onUnexpectedClose: () => { crash(name); } } : {}),\n ...(stderr ? { onStderrLine: (line: string) => { stderr(name, line); } } : {}),\n });\n }\n\n /** Fire the host's tool-error hook; exceptions must never affect the call path. */\n private reportToolError(info: McpToolErrorInfo): void {\n try {\n this.onToolError?.(info);\n } catch {\n /* host hook must never break tool routing */\n }\n }\n\n /**\n * Diff `desired` against current connections, spawn newcomers, dispose\n * removals, hot-restart on config change. Pull-based — call whenever the\n * host's config snapshot may have changed. Cheap if no diff.\n *\n * Lazy: newly-added servers are NOT eagerly connected; they connect on the\n * first `callTool()` (or first `listTools()` after `forceDiscover()`).\n * This avoids paying spawn cost for servers the agent never uses.\n */\n async reconcile(desired: Record<string, McpServerConfig>): Promise<ReconcileDiff> {\n if (this.disposed) throw new Error('McpBundler: disposed');\n // Serialize reconciles. Caller awaits its slot; in-flight reconciles run\n // in declaration order. Errors don't poison the latch — `.catch` swallows\n // for chaining, the actual error rejects the awaited slot.\n const slot = this.reconcileLatch.then(async () => this.doReconcile(desired));\n this.reconcileLatch = slot.catch(() => undefined);\n return slot;\n }\n\n private async doReconcile(desired: Record<string, McpServerConfig>): Promise<ReconcileDiff> {\n if (this.disposed) throw new Error('McpBundler: disposed');\n const desiredNames = new Set(Object.keys(desired));\n const currentNames = new Set(this.connections.keys());\n\n const added: string[] = [];\n const removed: string[] = [];\n const changed: string[] = [];\n const unchanged: string[] = [];\n\n // Removals: dispose connections no longer in desired set.\n for (const name of currentNames) {\n if (!desiredNames.has(name)) {\n const conn = this.connections.get(name);\n this.connections.delete(name);\n if (conn) await conn.close();\n removed.push(name);\n }\n }\n\n // Additions and changes.\n for (const [name, config] of Object.entries(desired)) {\n const existing = this.connections.get(name);\n if (!existing) {\n this.connections.set(name, this.buildConnection(name, config));\n added.push(name);\n continue;\n }\n const nextFingerprint = JSON.stringify(config);\n if (existing.configFingerprint() !== nextFingerprint) {\n // Config changed — close old, replace with fresh (lazy reconnect).\n await existing.close();\n this.connections.set(name, this.buildConnection(name, config));\n changed.push(name);\n } else {\n unchanged.push(name);\n }\n }\n\n if (added.length || removed.length || changed.length) {\n this.logger?.info('[mcp-bundler] reconciled', {\n added: added.length,\n removed: removed.length,\n changed: changed.length,\n unchanged: unchanged.length,\n });\n }\n return { added, removed, changed, unchanged };\n }\n\n /**\n * Synchronous snapshot of all currently-known tools across connected servers.\n * Servers that have not connected yet contribute nothing. Intended for use\n * inside OpenClaw's plugin tool factory which must be sync.\n *\n * Tool names are namespaced and disambiguated (suffix `-2`, `-3` on collision)\n * so cross-server name clashes never produce duplicate registrations.\n */\n listTools(): McpToolDescriptor[] {\n const seen = new Set<string>();\n const out: McpToolDescriptor[] = [];\n for (const conn of this.connections.values()) {\n for (const tool of conn.snapshotTools()) {\n const finalName = disambiguateAgainst(tool.prefixed, seen);\n seen.add(finalName);\n out.push(finalName === tool.prefixed ? tool : { ...tool, prefixed: finalName });\n }\n }\n return out;\n }\n\n /**\n * Eagerly connect to every configured server and discover tools. Used by\n * hosts that want a hot list rather than the lazy default. Errors are\n * swallowed per-server (logged), so one bad server doesn't fail the batch.\n */\n async warmup(): Promise<void> {\n if (this.disposed) return;\n await Promise.allSettled(\n Array.from(this.connections.values()).map(async (conn) => {\n try {\n await conn.ensureConnected();\n } catch (err) {\n this.logger?.warn(`[mcp-bundler] warmup failed for \"${conn.name}\"`, {\n err: err instanceof Error ? err.message : String(err),\n });\n }\n }),\n );\n }\n\n /** Build the live status descriptor for one connection. */\n private statusOf(conn: Connection): McpServerStatus {\n const status: McpServerStatus = {\n name: conn.name,\n connected: conn.isConnected(),\n toolCount: conn.toolCount(),\n consecutiveFailures: conn.failureCount(),\n };\n const err = conn.lastErrorMessage();\n if (err !== undefined) status.lastError = err;\n return status;\n }\n\n /** Live status for every known server. Synchronous snapshot, no I/O. */\n statuses(): McpServerStatus[] {\n return Array.from(this.connections.values()).map((conn) => this.statusOf(conn));\n }\n\n /**\n * Eagerly connect ONE server and return its resulting status. Unlike\n * `warmup()` — which fans out over all servers and swallows failures with\n * no return value — this surfaces the outcome so a caller (e.g. the daemon\n * confirming an `alfe mcp add`) can report \"connected, N tools\" or the exact\n * connect error back to the agent.\n *\n * Never throws: a connect failure (or a warm timeout) is reflected in the\n * returned status (`connected: false`, `lastError` set). Returns `undefined`\n * only when the named server isn't present in the bundler.\n */\n async warmServer(name: string, timeoutMs?: number): Promise<McpServerStatus | undefined> {\n if (this.disposed) return undefined;\n const conn = this.connections.get(name);\n if (!conn) return undefined;\n const connect = conn.ensureConnected();\n // If the timeout wins the race below, `connect` stays pending and could\n // reject later — swallow that so it never becomes an unhandled rejection.\n // The connection records its own `lastError`, which `statusOf` reads.\n void connect.catch(() => undefined);\n try {\n if (timeoutMs !== undefined && timeoutMs > 0) {\n await this.raceTimeout(connect, timeoutMs, name);\n } else {\n await connect;\n }\n } catch (err) {\n const status = this.statusOf(conn);\n // A warm timeout is ours, not the connection's — make sure the status\n // carries a message even if the underlying connect hasn't failed yet.\n status.lastError ??= err instanceof Error ? err.message : String(err);\n return status;\n }\n return this.statusOf(conn);\n }\n\n /** Race a promise against a timeout, clearing the timer either way. */\n private async raceTimeout(p: Promise<void>, timeoutMs: number, name: string): Promise<void> {\n let timer: ReturnType<typeof setTimeout> | undefined;\n const timeout = new Promise<never>((_, reject) => {\n timer = setTimeout(() => {\n reject(new Error(`server \"${name}\" warm timed out after ${timeoutMs.toString()}ms`));\n }, timeoutMs);\n if (typeof timer === 'object' && 'unref' in timer) {\n (timer as { unref: () => void }).unref();\n }\n });\n try {\n await Promise.race([p, timeout]);\n } finally {\n // The Promise executor runs synchronously, so `timer` is always set here.\n clearTimeout(timer);\n }\n }\n\n /**\n * Invoke a tool by its namespaced name. Routes to the originating server.\n * Errors are returned as `{ isError: true, content: [...] }` so a failing\n * tool doesn't crash the host.\n */\n async callTool(prefixed: string, args: unknown, signal?: AbortSignal): Promise<McpToolCallResult> {\n if (this.disposed) {\n return { isError: true, content: [{ type: 'text', text: 'mcp-bundler disposed' }] };\n }\n const route = this.routeToolName(prefixed);\n if (!route) {\n return {\n isError: true,\n content: [{ type: 'text', text: `unknown tool: ${prefixed}` }],\n };\n }\n try {\n const result = await route.connection.callTool(route.original, args, signal);\n if (result.isError) {\n this.reportToolError({\n server: route.connection.name,\n tool: route.original,\n prefixed,\n kind: 'result-error',\n message: extractErrorText(result),\n });\n }\n return result;\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err);\n this.logger?.error(`[mcp-bundler] tool call failed for \"${prefixed}\"`, { err: msg });\n this.reportToolError({\n server: route.connection.name,\n tool: route.original,\n prefixed,\n kind: 'thrown',\n message: msg,\n });\n return {\n isError: true,\n content: [{ type: 'text', text: `tool ${prefixed} failed: ${msg}` }],\n };\n }\n }\n\n /**\n * Resolve a namespaced tool name back to its server connection and original\n * tool name. Returns undefined if the tool is not currently advertised.\n */\n private routeToolName(prefixed: string): { connection: Connection; original: string } | undefined {\n for (const conn of this.connections.values()) {\n for (const tool of conn.snapshotTools()) {\n if (tool.prefixed === prefixed) return { connection: conn, original: tool.original };\n }\n }\n return undefined;\n }\n\n /**\n * Tear down all connections and stop background tasks. Idempotent.\n * Call from `registerRuntimeLifecycle({ cleanup })` in the host plugin.\n */\n async dispose(): Promise<void> {\n if (this.disposed) return;\n this.disposed = true;\n if (this.idleSweepTimer) {\n clearInterval(this.idleSweepTimer);\n this.idleSweepTimer = undefined;\n }\n await Promise.allSettled(Array.from(this.connections.values()).map((c) => c.close()));\n this.connections.clear();\n }\n\n private startIdleSweep(): void {\n this.idleSweepTimer = setInterval(() => {\n void this.sweepIdle().catch((err: unknown) => {\n this.logger?.warn('[mcp-bundler] idle sweep error', {\n err: err instanceof Error ? err.message : String(err),\n });\n });\n }, this.idleSweepIntervalMs);\n // Don't keep the host process alive just for the sweep.\n if (typeof this.idleSweepTimer === 'object' && 'unref' in this.idleSweepTimer) {\n (this.idleSweepTimer as { unref: () => void }).unref();\n }\n }\n\n private async sweepIdle(): Promise<void> {\n if (this.idleTtlMs <= 0) return;\n const targets: Connection[] = [];\n for (const conn of this.connections.values()) {\n if (conn.isConnected() && conn.idleSinceMs() > this.idleTtlMs) {\n targets.push(conn);\n }\n }\n if (targets.length === 0) return;\n this.logger?.debug(`[mcp-bundler] reaping ${targets.length.toString()} idle server(s)`);\n await Promise.allSettled(targets.map((c) => c.close()));\n }\n}\n","import {\n closeSync,\n existsSync,\n mkdirSync,\n openSync,\n readFileSync,\n renameSync,\n statSync,\n unlinkSync,\n watch,\n writeFileSync,\n type FSWatcher,\n} from 'node:fs';\nimport { dirname, join } from 'node:path';\nimport { homedir } from 'node:os';\nimport type { Logger, McpServerConfig, StdioServerConfig, RemoteServerConfig, McpTransportKind } from './types.js';\n\n/**\n * Where a server entry came from. Used by `removeServersByOwner` so an\n * integration uninstall can drop only its own entries without touching\n * `cli`-owned (e.g. `alfe-platform`) or `manual`-owned (user-added) ones.\n */\nexport type ServerOwner = 'cli' | `integration:${string}` | 'manual';\n\ninterface StoredServerCommon {\n /** Where the entry came from — controls bulk-removal semantics. */\n owner: ServerOwner;\n /** ISO timestamp of first registration; preserved across updates. */\n addedAt: string;\n /** Optional semver of the providing package (e.g. `@alfe.ai/mcp-server` for `alfe-platform`). Used for drift detection on CLI upgrade. */\n version?: string;\n}\n\nexport type StoredServerEntry =\n | (StoredServerCommon & { transport: 'stdio' } & StdioServerConfig)\n | (StoredServerCommon & { transport: 'sse' | 'streamable-http' } & RemoteServerConfig);\n\nexport interface StoreSchema {\n servers: Record<string, StoredServerEntry>;\n config: {\n sessionIdleTtlMs?: number;\n };\n /**\n * Server names this manager has written into `openclaw.json#mcp.servers.*`.\n * Used to compute the mirror-write diff without re-reading openclaw.json\n * (which would be a second source of truth). Foreign keys not listed here\n * are preserved across mirror writes.\n */\n _ownedOpenclawKeys: string[];\n}\n\nconst DEFAULT_STORE_DIR = join(homedir(), '.alfe', 'mcp');\nconst DEFAULT_STORE_PATH = join(DEFAULT_STORE_DIR, 'servers.json');\n\n/** Inter-process lock tunings — exported as constants so tests can override. */\nconst LOCK_WAIT_MS = 5_000;\nconst LOCK_RETRY_INTERVAL_MS = 25;\nconst LOCK_STALE_MS = 10_000;\n\nexport interface StoreOptions {\n /** Absolute path to the store file. Defaults to `~/.alfe/mcp/servers.json`. */\n path?: string;\n logger?: Logger;\n}\n\n/**\n * On-disk source of truth for the bundler's configured servers.\n *\n * Mutations go through `update()` (read-modify-write with atomic\n * temp+rename) so a concurrent writer (e.g. two `alfe mcp add` invocations\n * racing) can't lose data — the second writer reads the first's state.\n *\n * Schema is owner-tagged so `removeServersByOwner` can implement\n * integration uninstall without touching CLI-owned or manual entries.\n */\nexport class Store {\n private readonly storePath: string;\n private readonly logger?: Logger;\n private watcher?: FSWatcher;\n private watcherListeners = new Set<() => void>();\n private rewatchTimer?: NodeJS.Timeout;\n\n constructor(opts: StoreOptions = {}) {\n this.storePath = opts.path ?? DEFAULT_STORE_PATH;\n this.logger = opts.logger;\n }\n\n get path(): string {\n return this.storePath;\n }\n\n read(): StoreSchema {\n if (!existsSync(this.storePath)) return cloneEmpty();\n try {\n const raw = readFileSync(this.storePath, 'utf8');\n const parsed: unknown = JSON.parse(raw);\n return normalize(parsed);\n } catch (err) {\n this.logger?.warn('[mcp-bundler/store] failed to read store; returning empty', {\n err: errMsg(err),\n path: this.storePath,\n });\n return cloneEmpty();\n }\n }\n\n /**\n * Read-modify-write with atomic temp+rename, guarded by an\n * inter-process lock file. Caller passes a pure function that\n * produces the next state; this serialises the mutation to disk in\n * one rename, which is atomic on POSIX and on Windows when the\n * target path is on the same volume.\n *\n * The lock guards the read-then-rename window so two processes\n * (e.g. two `alfe mcp add` shells, or the CLI racing the daemon)\n * can't drop each other's writes. The lock file is at\n * `<storePath>.lock`; stale locks (older than `LOCK_STALE_MS`) are\n * stolen so a crashed writer doesn't wedge the store.\n *\n * Pure-function shape (instead of a `read()` then `write(next)`\n * pair) intentionally — it keeps the read-modify-write contract\n * local to each caller so two updates back-to-back never see each\n * other's partial state.\n */\n update(fn: (cur: StoreSchema) => StoreSchema): StoreSchema {\n mkdirSync(dirname(this.storePath), { recursive: true });\n const release = this.acquireLock();\n try {\n const cur = this.read();\n const next = fn(cur);\n const tempPath = `${this.storePath}.${String(process.pid)}.${String(Date.now())}.tmp`;\n writeFileSync(tempPath, JSON.stringify(next, null, 2), { encoding: 'utf8', mode: 0o600 });\n try {\n renameSync(tempPath, this.storePath);\n } catch (err) {\n try {\n unlinkSync(tempPath);\n } catch {\n // Best-effort cleanup; the temp file's name has the pid + timestamp\n // so an orphan won't collide with future writers.\n }\n throw err;\n }\n return next;\n } finally {\n release();\n }\n }\n\n /**\n * Acquire an inter-process file lock by atomically creating a\n * sentinel via `openSync(lockPath, 'wx')`. Spins with bounded\n * backoff up to `LOCK_WAIT_MS`. If the lock file is older than\n * `LOCK_STALE_MS` it's assumed orphaned (writer crashed mid-update)\n * and stolen — the write window is sub-second in practice, so\n * holding the lock for >5s means something went wrong.\n *\n * Returns the release function. Single-process callers are\n * unaffected — re-entering the same process spins briefly while\n * the prior call's `finally` runs.\n */\n private acquireLock(): () => void {\n const lockPath = `${this.storePath}.lock`;\n const deadline = Date.now() + LOCK_WAIT_MS;\n let fd = -1;\n for (;;) {\n try {\n fd = openSync(lockPath, 'wx', 0o600);\n break;\n } catch (err) {\n const code = (err as NodeJS.ErrnoException).code;\n if (code !== 'EEXIST') throw err;\n if (this.lockIsStale(lockPath)) {\n try {\n unlinkSync(lockPath);\n } catch {\n // Another process may have just released it — fall through and retry.\n }\n continue;\n }\n if (Date.now() >= deadline) {\n throw new Error(\n `Store.update: timed out waiting for ${lockPath} (held by another writer or stale lock)`,\n );\n }\n // Synchronous spin — the lock window is sub-second under\n // normal load; busy-waiting briefly is simpler than wiring\n // async/await through every `update()` caller.\n const sleepUntil = Date.now() + LOCK_RETRY_INTERVAL_MS;\n while (Date.now() < sleepUntil) { /* spin */ }\n }\n }\n const held = fd;\n return () => {\n try {\n closeSync(held);\n } catch {\n // ignore — the unlink is what releases the lock for the next writer.\n }\n try {\n unlinkSync(lockPath);\n } catch {\n // ignore — already unlinked or stolen by a stale-lock breaker.\n }\n };\n }\n\n private lockIsStale(lockPath: string): boolean {\n try {\n const st = statSync(lockPath);\n return Date.now() - st.mtimeMs > LOCK_STALE_MS;\n } catch {\n return false;\n }\n }\n\n /**\n * Watch the store file for external changes (e.g. another `alfe mcp add`\n * shelling out from a separate process). Returns an unsubscribe fn.\n *\n * Coalesces bursts via a 50 ms debounce — editors and atomic-rename\n * writers commonly fire multiple events per logical save.\n */\n watch(cb: () => void): () => void {\n this.watcherListeners.add(cb);\n this.ensureWatcher();\n return () => {\n this.watcherListeners.delete(cb);\n if (this.watcherListeners.size === 0) this.disposeWatcher();\n };\n }\n\n dispose(): void {\n this.watcherListeners.clear();\n this.disposeWatcher();\n }\n\n private ensureWatcher(): void {\n if (this.watcher) return;\n mkdirSync(dirname(this.storePath), { recursive: true });\n // Some platforms / atomic renames make a per-file watch flaky after\n // a rename; watching the parent directory and filtering by basename\n // is more robust.\n const dir = dirname(this.storePath);\n const basename = this.storePath.slice(dir.length + 1);\n let pending: NodeJS.Timeout | undefined;\n const fire = (): void => {\n pending = undefined;\n for (const cb of this.watcherListeners) {\n try {\n cb();\n } catch (err) {\n this.logger?.warn('[mcp-bundler/store] watcher listener threw', { err: errMsg(err) });\n }\n }\n };\n try {\n this.watcher = watch(dir, (_event, fn) => {\n if (fn !== basename) return;\n if (pending) clearTimeout(pending);\n pending = setTimeout(fire, 50);\n });\n this.watcher.on('error', (err) => {\n this.logger?.warn('[mcp-bundler/store] watcher error; retrying in 1s', { err: errMsg(err) });\n this.disposeWatcher();\n if (!this.rewatchTimer && this.watcherListeners.size > 0) {\n this.rewatchTimer = setTimeout(() => {\n this.rewatchTimer = undefined;\n this.ensureWatcher();\n }, 1000);\n this.rewatchTimer.unref();\n }\n });\n } catch (err) {\n this.logger?.warn('[mcp-bundler/store] failed to start watcher', { err: errMsg(err) });\n }\n }\n\n private disposeWatcher(): void {\n if (this.watcher) {\n try {\n this.watcher.close();\n } catch {\n // close throws on already-disposed watchers; ignore.\n }\n this.watcher = undefined;\n }\n if (this.rewatchTimer) {\n clearTimeout(this.rewatchTimer);\n this.rewatchTimer = undefined;\n }\n }\n}\n\nexport function defaultStorePath(): string {\n return DEFAULT_STORE_PATH;\n}\n\n/** Pull the runtime config (transport + transport-specific fields) out of a stored entry. */\nexport function toServerConfig(entry: StoredServerEntry): McpServerConfig {\n if (entry.transport === 'stdio') {\n const { command, args, env, cwd } = entry;\n const cfg: StdioServerConfig = { command };\n if (args) cfg.args = args;\n if (env) cfg.env = env;\n if (cwd) cfg.cwd = cwd;\n return cfg;\n }\n const { url, transport, headers, connectionTimeoutMs } = entry;\n const cfg: RemoteServerConfig = { url, transport };\n if (headers) cfg.headers = headers;\n if (connectionTimeoutMs !== undefined) cfg.connectionTimeoutMs = connectionTimeoutMs;\n return cfg;\n}\n\n/** Build a stored entry from a runtime config + ownership metadata. */\nexport function toStoredEntry(\n config: McpServerConfig,\n meta: { owner: ServerOwner; transport?: McpTransportKind; version?: string; addedAt?: string },\n): StoredServerEntry {\n const addedAt = meta.addedAt ?? new Date().toISOString();\n if ('command' in config) {\n return {\n transport: 'stdio',\n owner: meta.owner,\n addedAt,\n ...(meta.version !== undefined ? { version: meta.version } : {}),\n ...config,\n };\n }\n const transport = meta.transport ?? config.transport ?? 'sse';\n if (transport === 'stdio') {\n throw new Error('toStoredEntry: transport=stdio specified but config is remote-shaped');\n }\n return {\n transport,\n owner: meta.owner,\n addedAt,\n ...(meta.version !== undefined ? { version: meta.version } : {}),\n ...config,\n };\n}\n\nfunction cloneEmpty(): StoreSchema {\n return { servers: {}, config: {}, _ownedOpenclawKeys: [] };\n}\n\nfunction normalize(raw: unknown): StoreSchema {\n if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return cloneEmpty();\n const r = raw as Partial<StoreSchema>;\n return {\n servers: r.servers && typeof r.servers === 'object' ? r.servers : {},\n config: r.config && typeof r.config === 'object' ? r.config : {},\n _ownedOpenclawKeys: Array.isArray(r._ownedOpenclawKeys) ? r._ownedOpenclawKeys.slice() : [],\n };\n}\n\nfunction errMsg(err: unknown): string {\n return err instanceof Error ? err.message : String(err);\n}\n","import type { McpBundler } from './bundler.js';\nimport { Store, toServerConfig, toStoredEntry, type ServerOwner, type StoredServerEntry } from './store.js';\nimport type { Logger, McpServerConfig, McpServerStatus, McpTransportKind } from './types.js';\n\nexport interface ManagerOptions {\n /** Pre-constructed store. If omitted, one is built with default options. */\n store?: Store;\n logger?: Logger;\n}\n\nexport interface AddServerOptions {\n /** Required — flat-namespace key under the bundler store. */\n id: string;\n /** Marks ownership for bulk removal. Defaults to `manual`. */\n owner?: ServerOwner;\n /** Semver of the providing package; used for CLI version-drift detection. */\n version?: string;\n /** Explicit transport hint for remote configs. Defaults to inferring from `config`. */\n transport?: McpTransportKind;\n}\n\n/**\n * Bundler manager — owns the `~/.alfe/mcp/servers.json` store and surfaces a\n * small CRUD API the CLI and integration applier both call into.\n *\n * Single source of truth: every consumer (daemon-hosted bundler, CLI `alfe mcp\n * list`, integration uninstall) reads from this store. Openclaw.json is no\n * longer kept in sync — the daemon hosts the bundler children and the\n * openclaw plugin reaches them via IPC, so the openclaw.json mirror became\n * dead weight and an active source of duplicate spawning on claude-cli /\n * codex-cli backends.\n *\n * Call `loadIntoBundler(bundler)` once at daemon startup to wire the store\n * into a live `McpBundler` — subsequent store mutations (including those\n * landed by other processes via the file watcher) re-reconcile automatically.\n */\nexport class Manager {\n private readonly store: Store;\n private readonly logger?: Logger;\n private bundler?: McpBundler;\n private changeListeners = new Set<() => void>();\n private storeUnsubscribe?: () => void;\n\n constructor(opts: ManagerOptions = {}) {\n this.store = opts.store ?? new Store({ logger: opts.logger });\n this.logger = opts.logger;\n }\n\n /** Direct accessor — handy for tests and the CLI's `alfe mcp list`. */\n getStore(): Store {\n return this.store;\n }\n\n /**\n * Register or overwrite a server entry. Mutation lands in the store\n * synchronously; if a bundler has been attached via `loadIntoBundler`,\n * it gets re-reconciled in the background (errors logged, never\n * thrown — the store is the source of truth, the bundler is derived).\n */\n async addServer(config: McpServerConfig, opts: AddServerOptions): Promise<void> {\n if (!opts.id) throw new Error('Manager.addServer: id is required');\n const owner = opts.owner ?? 'manual';\n this.store.update((cur) => {\n const previousAddedAt = lookupAddedAt(cur.servers, opts.id);\n const entry = toStoredEntry(config, {\n owner,\n transport: opts.transport,\n version: opts.version,\n addedAt: previousAddedAt,\n });\n return {\n ...cur,\n servers: { ...cur.servers, [opts.id]: entry },\n };\n });\n this.scheduleBundlerReconcile();\n this.fireChange();\n return Promise.resolve();\n }\n\n /**\n * Remove a single server entry. No-op if the id isn't in the store.\n * Refuses to remove an entry whose owner doesn't match `expectedOwner`\n * when supplied — the CLI uses this to guard `alfe mcp remove` from\n * accidentally clobbering integration- or cli-owned entries.\n */\n removeServer(id: string, opts: { expectedOwner?: ServerOwner } = {}): Promise<boolean> {\n const current = this.store.read();\n const existing = lookupEntry(current.servers, id);\n if (!existing) return Promise.resolve(false);\n if (opts.expectedOwner && existing.owner !== opts.expectedOwner) {\n return Promise.reject(\n new Error(\n `Manager.removeServer: server \"${id}\" is owned by \"${existing.owner}\", not \"${opts.expectedOwner}\"`,\n ),\n );\n }\n this.store.update((cur) => ({\n ...cur,\n servers: Object.fromEntries(Object.entries(cur.servers).filter(([k]) => k !== id)),\n }));\n this.scheduleBundlerReconcile();\n this.fireChange();\n return Promise.resolve(true);\n }\n\n /** Drop every entry whose owner matches — used by integration uninstall. */\n async removeServersByOwner(owner: ServerOwner): Promise<string[]> {\n const removed: string[] = [];\n this.store.update((cur) => {\n const next: Record<string, StoredServerEntry> = {};\n for (const [id, entry] of Object.entries(cur.servers)) {\n if (entry.owner === owner) {\n removed.push(id);\n } else {\n next[id] = entry;\n }\n }\n if (removed.length === 0) return cur;\n return { ...cur, servers: next };\n });\n if (removed.length > 0) {\n this.scheduleBundlerReconcile();\n this.fireChange();\n }\n return Promise.resolve(removed);\n }\n\n /** Read-only snapshot for `alfe mcp list` and similar UIs. */\n listServers(): { id: string; entry: StoredServerEntry }[] {\n const snap = this.store.read();\n return Object.entries(snap.servers).map(([id, entry]) => ({ id, entry }));\n }\n\n /**\n * Live connection status per server from the attached bundler. Empty when\n * no bundler is attached (e.g. a CLI-only manager). Lets callers show\n * whether each registered server actually connected and how many tools it\n * advertises, instead of only the stored config.\n */\n serverStatuses(): McpServerStatus[] {\n return this.bundler ? this.bundler.statuses() : [];\n }\n\n /**\n * Reconcile the bundler against the current store (so a just-added entry\n * has a connection object) then eagerly connect ONE server and return its\n * status. Used by the daemon to CONFIRM an `alfe mcp add` actually connected\n * before replying to the agent — turning the fire-and-forget warm into a\n * result the caller can report (\"connected, N tools\" or the real error).\n *\n * Returns `null` when no bundler is attached or the id isn't present; never\n * throws (a connect failure is carried in the returned status).\n */\n async warmServer(id: string, timeoutMs?: number): Promise<McpServerStatus | null> {\n if (!this.bundler) return null;\n await this.reconcileBundler();\n return (await this.bundler.warmServer(id, timeoutMs)) ?? null;\n }\n\n /**\n * Push the current store contents into a bundler instance (which owns\n * connections / tools). Wires up a store watcher so external mutations\n * (e.g. another shell running `alfe mcp add`) re-reconcile.\n */\n async loadIntoBundler(bundler: McpBundler): Promise<void> {\n this.bundler = bundler;\n await this.reconcileBundler();\n this.storeUnsubscribe ??= this.store.watch(() => {\n void this.reconcileBundler().catch((err: unknown) => {\n this.logger?.warn('[mcp-bundler/manager] watcher reconcile failed', { err: errMsg(err) });\n });\n });\n }\n\n /** Subscribe to store mutations. Returns an unsubscribe fn. */\n onChange(cb: () => void): () => void {\n this.changeListeners.add(cb);\n return () => {\n this.changeListeners.delete(cb);\n };\n }\n\n /**\n * Detach from the bundler and stop watching the store. Safe to call\n * multiple times. Does not dispose the underlying `Store` so the\n * shared instance survives multi-manager environments (rare).\n */\n async dispose(): Promise<void> {\n if (this.storeUnsubscribe) {\n this.storeUnsubscribe();\n this.storeUnsubscribe = undefined;\n }\n this.store.dispose();\n this.changeListeners.clear();\n this.bundler = undefined;\n return Promise.resolve();\n }\n\n private scheduleBundlerReconcile(): void {\n if (!this.bundler) return;\n void this.reconcileBundler().catch((err: unknown) => {\n this.logger?.warn('[mcp-bundler/manager] bundler reconcile failed', { err: errMsg(err) });\n });\n }\n\n private async reconcileBundler(): Promise<void> {\n if (!this.bundler) return;\n const snap = this.store.read();\n const servers: Record<string, McpServerConfig> = {};\n for (const [id, entry] of Object.entries(snap.servers)) {\n servers[id] = toServerConfig(entry);\n }\n await this.bundler.reconcile(servers);\n }\n\n private fireChange(): void {\n for (const cb of this.changeListeners) {\n try {\n cb();\n } catch (err) {\n this.logger?.warn('[mcp-bundler/manager] onChange listener threw', { err: errMsg(err) });\n }\n }\n }\n}\n\nfunction errMsg(err: unknown): string {\n return err instanceof Error ? err.message : String(err);\n}\n\n/**\n * Indexed access on `Record<string, T>` returns `T` (not `T | undefined`)\n * unless `noUncheckedIndexedAccess` is set in tsconfig. These helpers\n * make the optional-ness explicit so the lint rules that hate\n * always-truthy conditionals stop firing on real lookups.\n */\nfunction lookupEntry(\n servers: Record<string, StoredServerEntry>,\n id: string,\n): StoredServerEntry | undefined {\n return Object.hasOwn(servers, id) ? servers[id] : undefined;\n}\n\nfunction lookupAddedAt(servers: Record<string, StoredServerEntry>, id: string): string | undefined {\n const entry = lookupEntry(servers, id);\n return entry ? entry.addedAt : undefined;\n}\n","/**\n * Pattern A validator — locks the \"explicit selector arg\" contract for\n * credential-touching tools across the openclaw-* plugin family.\n *\n * Background\n * ----------\n * Pattern A says every credential-touching tool on a multi-account-capable\n * provider MUST take a required selector arg in its JSON Schema so the LLM\n * picks the account deliberately. The reference implementation lives in\n * `@alfe.ai/openclaw-google` (`google_run_command` / `google_disconnect_\n * account` both require `email`). PR 7 of channels-and-credential-driven-\n * integrations sweeps the same shape across notion / xero / myob.\n *\n * Why a validator\n * ---------------\n * The contract is easy to break by accident — a new tool gets added, the\n * selector arg gets forgotten, the LLM silently dispatches to whichever\n * account was loaded first. There's no runtime safety net once the plugin\n * is published. This validator runs in plugin build steps (or CI) and\n * fails the build if a tool that declares itself credential-touching\n * doesn't carry the selector.\n *\n * Scope of this file\n * ------------------\n * Pure functions — no I/O. Plugin packages call into these helpers from\n * their own build scripts (e.g. `pnpm build` or a dedicated lint task) and\n * pass in their `ToolDef[]` or `McpToolDescriptor[]` collection. The\n * validator does NOT know how to read manifests or files — it only checks\n * the shape of the in-memory descriptor list. Wiring is up to each plugin.\n *\n * Consumers\n * ---------\n * - `@alfe.ai/myob-mcp` — direct MCP, explicit TypeBox schemas.\n * - `@alfe.ai/notion-mcp` — proxy MCP, injects selector at runtime.\n * - `@alfe.ai/xero-mcp` — proxy MCP, injects selector at runtime.\n * - `@alfe.ai/openclaw-google` — already compliant; can opt-in for regression.\n *\n * Future\n * ------\n * When PR 7-deferred lands (atlassian / github / microsoft openclaw\n * packages), they hook into the same validator. The atlassian / github\n * cases will likely run the validator over the proxied child server's\n * `listTools()` response after schema-injection, to confirm the injection\n * actually landed on every tool.\n */\n\nimport type { McpToolDescriptor } from \"./types.js\";\n\n// ── Public types ────────────────────────────────────────────\n\n/**\n * A tool descriptor that the validator can inspect. Plugins can pass\n * either {@link McpToolDescriptor} (for proxy plugins surfacing child\n * tools) or a leaner local shape (for direct-MCP plugins). Both come\n * down to a tool name and a JSON Schema parameter object.\n */\nexport interface ValidatableTool {\n /** Tool name as the LLM sees it (post-namespacing). */\n name: string;\n /** JSON Schema for tool parameters (the `inputSchema`). */\n parameters: Record<string, unknown>;\n}\n\nexport interface PatternAOptions {\n /**\n * The required selector property name. Provider-specific (Google uses\n * `email`; Notion will use `workspaceId`; Xero will use `xeroTenantId`;\n * MYOB will use `myobBusinessId`). Plugins MAY support more than one\n * acceptable name — pass an array.\n */\n selector: string | readonly string[];\n /**\n * Tool names exempt from the selector requirement — typically the\n * `list_accounts` discovery tool and any pure-utility tools that don't\n * touch credentials. Match is exact (post-namespacing).\n */\n exempt?: readonly string[];\n}\n\nexport interface PatternAViolation {\n tool: string;\n reason:\n | \"missing-selector-property\"\n | \"selector-not-required\"\n | \"selector-property-not-string\";\n detail: string;\n}\n\n// ── Core check ───────────────────────────────────────────────\n\n/**\n * Validate that every non-exempt tool's JSON Schema declares the selector\n * property AND lists it in `required`. Returns the full set of violations\n * so the caller can report them all in one pass — failing fast on the\n * first one tends to hide cascading bugs in real plugins.\n *\n * The check is intentionally schema-shape-only — it does NOT execute the\n * tool, call the LLM, or talk to the cloud. It's a fast structural pass\n * suitable for build-time use.\n */\nexport function checkPatternA(\n tools: readonly ValidatableTool[],\n options: PatternAOptions,\n): PatternAViolation[] {\n const selectorNames = typeof options.selector === \"string\"\n ? [options.selector]\n : options.selector;\n const exempt = new Set(options.exempt ?? []);\n const violations: PatternAViolation[] = [];\n\n for (const tool of tools) {\n if (exempt.has(tool.name)) continue;\n\n const schema = tool.parameters;\n const properties = (schema as { properties?: Record<string, unknown> }).properties ?? {};\n const required = (schema as { required?: unknown[] }).required ?? [];\n\n const matched = selectorNames.find((name) => name in properties);\n\n if (!matched) {\n violations.push({\n tool: tool.name,\n reason: \"missing-selector-property\",\n detail: `expected one of [${selectorNames.join(\", \")}] in inputSchema.properties`,\n });\n continue;\n }\n\n if (!required.includes(matched)) {\n violations.push({\n tool: tool.name,\n reason: \"selector-not-required\",\n detail: `selector \"${matched}\" present in properties but missing from inputSchema.required`,\n });\n continue;\n }\n\n const propSchema = properties[matched];\n const type = (propSchema as { type?: unknown }).type;\n if (type !== \"string\") {\n violations.push({\n tool: tool.name,\n reason: \"selector-property-not-string\",\n detail: `selector \"${matched}\" must be JSON Schema type=string (found ${JSON.stringify(type)})`,\n });\n }\n }\n\n return violations;\n}\n\n/**\n * Thin wrapper that throws an Error listing every violation if any are\n * present. Convenient for build scripts that want a single guard call.\n */\nexport function assertPatternA(\n tools: readonly ValidatableTool[],\n options: PatternAOptions,\n): void {\n const violations = checkPatternA(tools, options);\n if (violations.length === 0) return;\n const lines = violations.map((v) => ` - [${v.reason}] ${v.tool}: ${v.detail}`);\n throw new Error(\n `Pattern A validation failed for ${String(violations.length)} tool(s):\\n${lines.join(\"\\n\")}`,\n );\n}\n\n/**\n * Cast an {@link McpToolDescriptor} (proxy-plugin shape) to the leaner\n * {@link ValidatableTool} the validator accepts. Useful for proxy\n * plugins that already maintain a `cachedTools: McpToolDescriptor[]`\n * collection.\n */\nexport function fromMcpDescriptor(descriptor: McpToolDescriptor): ValidatableTool {\n return { name: descriptor.prefixed, parameters: descriptor.parameters };\n}\n"],"mappings":";;;;;;;;;;;;;;AAWA,MAAM,aAAa;AACnB,MAAM,UAAU;AAChB,MAAM,YAAY;AAElB,SAAgB,oBAAoB,OAAuB;AACzD,QAAO,MAAM,QAAQ,YAAY,IAAI;;AAGvC,SAAgB,wBAAwB,QAAgB,MAAsB;CAC5E,MAAM,OAAO,GAAG,oBAAoB,OAAO,GAAG,YAAY,oBAAoB,KAAK;AACnF,KAAI,KAAK,UAAU,QAAS,QAAO;CAEnC,MAAM,oBAAoB,oBAAoB,OAAO,CAAC,SAAS;CAC/D,MAAM,aAAa,KAA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AAK;EACpB,MAAM,aAAc,OAAoD,cAAc,EAAE;EACxF,MAAM,WAAY,OAAoC,YAAY,EAAE;EAEpE,MAAM,UAAU,cAAc,MAAM,SAAS,QAAQ,WAAW;AAEhE,MAAI,CAAC,SAAS;AACZ,cAAW,KAAK;IACd,MAAM,KAAK;IACX,QAAQ;IACR,QAAQ,oBAAoB,cAAc,KAAK,KAAK,CAAC;IACtD,CAAC;AACF;;AAGF,MAAI,CAAC,SAAS,SAAS,QAAQ,EAAE;AAC/B,cAAW,KAAK;IACd,MAAM,KAAK;IACX,QAAQ;IACR,QAAQ,aAAa,QAAQ;IAC9B,CAAC;AACF;;EAIF,MAAM,OADa,WAAW,SACkB;AAChD,MAAI,SAAS,SACX,YAAW,KAAK;GACd,MAAM,KAAK;GACX,QAAQ;GACR,QAAQ,aAAa,QAAQ,2CAA2C,KAAK,UAAU,KAAK,CAAC;GAC9F,CAAC;;AAIN,QAAO;;;;;;AAOT,SAAgB,eACd,OACA,SACM;CACN,MAAM,aAAa,cAAc,OAAO,QAAQ;AAChD,KAAI,WAAW,WAAW,EAAG;CAC7B,MAAM,QAAQ,WAAW,KAAK,MAAM,QAAQ,EAAE,OAAO,IAAI,EAAE,KAAK,IAAI,EAAE,SAAS;AAC/E,OAAM,IAAI,MACR,mCAAmC,OAAO,WAAW,OAAO,CAAC,aAAa,MAAM,KAAK,KAAK,GAC3F;;;;;;;;AASH,SAAgB,kBAAkB,YAAgD;AAChF,QAAO;EAAE,MAAM,WAAW;EAAU,YAAY,WAAW;EAAY"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["errMsg"],"sources":["../src/tool-naming.ts","../src/connection.ts","../src/bundler.ts","../src/store.ts","../src/manager.ts","../src/pattern-a-validator.ts"],"sourcesContent":["/**\n * Tool name sanitization and collision handling.\n *\n * OpenClaw constraint: tool names must match `[A-Za-z0-9_-]` and be ≤64 chars.\n * Pattern mirrored from `openclaw/src/agents/pi-bundle-mcp-names.ts`.\n *\n * Strategy: prefix every tool with its server name (`{server}__{tool}`),\n * sanitize disallowed chars to `_`, truncate, then suffix-disambiguate\n * (`-2`, `-3`, ...) on collision.\n */\n\nconst DISALLOWED = /[^A-Za-z0-9_-]/g;\nconst MAX_LEN = 64;\nconst SEPARATOR = '__';\n\nexport function sanitizeNameSegment(value: string): string {\n return value.replace(DISALLOWED, '_');\n}\n\nexport function buildNamespacedToolName(server: string, tool: string): string {\n const base = `${sanitizeNameSegment(server)}${SEPARATOR}${sanitizeNameSegment(tool)}`;\n if (base.length <= MAX_LEN) return base;\n // Truncate from the tool side first to keep the server prefix intact.\n const reservedForServer = sanitizeNameSegment(server).length + SEPARATOR.length;\n const toolBudget = Math.max(1, MAX_LEN - reservedForServer);\n return `${sanitizeNameSegment(server)}${SEPARATOR}${sanitizeNameSegment(tool).slice(0, toolBudget)}`;\n}\n\n/**\n * Disambiguate a candidate name against an existing set by appending `-2`, `-3`, etc.\n * Mutates nothing; returns the chosen name. Caller is responsible for inserting it\n * into the set.\n */\nexport function disambiguateAgainst(candidate: string, taken: ReadonlySet<string>): string {\n if (!taken.has(candidate)) return candidate;\n for (let i = 2; i < 1000; i += 1) {\n const suffix = `-${i.toString()}`;\n const room = MAX_LEN - suffix.length;\n const trimmed = candidate.length > room ? candidate.slice(0, room) : candidate;\n const next = `${trimmed}${suffix}`;\n if (!taken.has(next)) return next;\n }\n // Pathological: 998 collisions. Fall back to a deterministic-ish hash.\n return `${candidate.slice(0, MAX_LEN - 6)}-x${(taken.size % 1000).toString().padStart(3, '0')}`;\n}\n","import type { Logger, McpServerConfig, McpToolDescriptor, McpToolCallResult, StdioServerConfig } from './types.js';\nimport { buildNamespacedToolName } from './tool-naming.js';\n\n/** Env keys OpenClaw rejects from stdio MCP env blocks. Filter them out before spawning. */\nexport const STDIO_ENV_DENYLIST = new Set([\n 'NODE_OPTIONS',\n 'PYTHONSTARTUP',\n 'PYTHONPATH',\n 'PERL5OPT',\n 'RUBYOPT',\n 'SHELLOPTS',\n 'PS4',\n]);\n\nexport function sanitizeStdioEnv(env: Record<string, string> | undefined): Record<string, string> {\n if (!env) return {};\n const safe: Record<string, string> = {};\n for (const [k, v] of Object.entries(env)) {\n if (STDIO_ENV_DENYLIST.has(k)) continue;\n safe[k] = v;\n }\n return safe;\n}\n\n/** Per-connect context the Connection threads into the connect factory. */\nexport interface ConnectContext {\n /** Server name (store key) — for logging/attribution. */\n serverName: string;\n /**\n * When set, stdio children are spawned with `stderr: 'pipe'` and each stderr\n * line is delivered here. When absent, stderr stays `inherit` (host fd).\n */\n onStderrLine?: (line: string) => void;\n}\n\nexport interface ConnectionDeps {\n /**\n * Factory for an MCP Client connected to the given config. Injected so tests\n * can mock without spawning real processes. In production this wraps\n * `@modelcontextprotocol/sdk/client`. The context arg is optional so\n * existing test mocks keep working.\n */\n connect: (server: McpServerConfig, ctx?: ConnectContext) => Promise<McpClientHandle>;\n}\n\n/**\n * Minimal interface our connection layer needs from an MCP client. Mirrors\n * the @modelcontextprotocol/sdk Client surface but kept narrow so we can\n * mock cleanly in tests.\n */\nexport interface McpClientHandle {\n listTools(): Promise<{ name: string; description?: string; inputSchema: Record<string, unknown> }[]>;\n callTool(name: string, args: unknown, opts?: { signal?: AbortSignal }): Promise<McpToolCallResult>;\n close(): Promise<void>;\n /**\n * Register a callback fired when the underlying transport closes or errors\n * unexpectedly (e.g. the child process crashed). Lets the Connection drop\n * its dead client so the next call re-spawns. Optional so test mocks can\n * omit it.\n */\n onClose?(handler: () => void): void;\n}\n\n/**\n * One Connection per MCP server. Owns lifecycle (lazy-spawn, close, refresh-lock).\n * Refresh-lock pattern adapted from AIWerk `index.ts:219-250` — prevents\n * reconnect + `notifications/tools/list_changed` race.\n */\nexport class Connection {\n readonly name: string;\n readonly config: McpServerConfig;\n private readonly deps: ConnectionDeps;\n private readonly logger: Logger | undefined;\n\n private client: McpClientHandle | undefined;\n private tools: McpToolDescriptor[] = [];\n private connectInFlight: Promise<void> | undefined;\n private refreshInFlight = false;\n private refreshQueued = false;\n private lastUsedAt = Date.now();\n\n /** Set while `close()` runs so the transport's onclose isn't treated as a crash. */\n private closing = false;\n /** Consecutive failed connect attempts — drives reconnect backoff. */\n private consecutiveFailures = 0;\n /** Message from the most recent failed connect attempt; cleared on success. */\n private lastError: string | undefined;\n /** Epoch ms before which re-connect attempts fast-fail (crash-loop guard). */\n private reconnectBlockedUntilMs = 0;\n\n private static readonly RECONNECT_BACKOFF_BASE_MS = 500;\n private static readonly RECONNECT_BACKOFF_MAX_MS = 30_000;\n\n private readonly onUnexpectedClose: (() => void) | undefined;\n private readonly onStderrLine: ((line: string) => void) | undefined;\n\n constructor(params: {\n name: string;\n config: McpServerConfig;\n deps: ConnectionDeps;\n logger?: Logger;\n /** Fired when the transport closes unexpectedly (crash), after internal cleanup. */\n onUnexpectedClose?: () => void;\n /** Threaded to the connect factory — pipes stdio child stderr when set. */\n onStderrLine?: (line: string) => void;\n }) {\n this.name = params.name;\n this.config = params.config;\n this.deps = params.deps;\n this.logger = params.logger;\n this.onUnexpectedClose = params.onUnexpectedClose;\n this.onStderrLine = params.onStderrLine;\n }\n\n /** Returns the most recent known tool list. May be empty if the server hasn't connected yet. */\n snapshotTools(): McpToolDescriptor[] {\n return this.tools;\n }\n\n /** Whether an MCP child process / remote connection has been established. */\n isConnected(): boolean {\n return this.client !== undefined;\n }\n\n /** Tools currently advertised; 0 until the server connects + discovers. */\n toolCount(): number {\n return this.tools.length;\n }\n\n /** Consecutive failed connect attempts; 0 when healthy. */\n failureCount(): number {\n return this.consecutiveFailures;\n }\n\n /** Message from the most recent failed connect attempt, if any. */\n lastErrorMessage(): string | undefined {\n return this.lastError;\n }\n\n /** Idle timestamp for reaping. */\n idleSinceMs(): number {\n return Date.now() - this.lastUsedAt;\n }\n\n /**\n * Lazy connect + tool discovery. Safe to call concurrently; in-flight\n * connects coalesce.\n */\n async ensureConnected(): Promise<void> {\n if (this.client) return;\n if (this.connectInFlight) return this.connectInFlight;\n if (Date.now() < this.reconnectBlockedUntilMs) {\n throw new Error(\n `server \"${this.name}\" is in reconnect backoff after ${this.consecutiveFailures.toString()} failed attempt(s)`,\n );\n }\n this.connectInFlight = this.connectAndDiscover().finally(() => {\n this.connectInFlight = undefined;\n });\n return this.connectInFlight;\n }\n\n private async connectAndDiscover(): Promise<void> {\n const safeConfig = 'command' in this.config\n ? ({ ...this.config, env: sanitizeStdioEnv(this.config.env) } satisfies StdioServerConfig)\n : this.config;\n this.logger?.debug(`[mcp-bundler] connecting server \"${this.name}\"`);\n // `client` is undefined until connect() resolves; keep it here so the\n // catch can close a half-open handle. Widening the try to cover connect()\n // itself is load-bearing: a spawn/URL/auth failure throws HERE (before\n // listTools), and that is the COMMON failure mode for a freshly-added\n // server. Leaving it outside the try meant such failures never recorded\n // `lastError`, never incremented `consecutiveFailures`, and never armed\n // the reconnect backoff — so a bad server had no diagnostics and no\n // crash-loop guard.\n let client: McpClientHandle | undefined;\n try {\n client = await this.deps.connect(safeConfig, {\n serverName: this.name,\n onStderrLine: this.onStderrLine,\n });\n const advertised = await client.listTools();\n const connected = client;\n this.client = connected;\n this.closing = false;\n // Drop the dead client on an unexpected transport close so the next\n // call re-spawns instead of calling into a corpse. Bind the callback to\n // THIS handle so a late close from an already-replaced client can't wipe\n // out a freshly respawned one.\n connected.onClose?.(() => { this.handleUnexpectedClose(connected); });\n this.tools = advertised.map((t) => ({\n prefixed: buildNamespacedToolName(this.name, t.name),\n server: this.name,\n original: t.name,\n label: (t.description ?? t.name).slice(0, 80),\n description: t.description ?? '',\n parameters: t.inputSchema,\n }));\n this.lastUsedAt = Date.now();\n this.consecutiveFailures = 0;\n this.lastError = undefined;\n this.reconnectBlockedUntilMs = 0;\n this.logger?.info(`[mcp-bundler] server \"${this.name}\" connected, ${this.tools.length.toString()} tool(s)`);\n } catch (err) {\n if (client) await client.close().catch(() => undefined);\n this.consecutiveFailures += 1;\n this.lastError = err instanceof Error ? err.message : String(err);\n const backoff = Math.min(\n Connection.RECONNECT_BACKOFF_BASE_MS * 2 ** (this.consecutiveFailures - 1),\n Connection.RECONNECT_BACKOFF_MAX_MS,\n );\n this.reconnectBlockedUntilMs = Date.now() + backoff;\n throw err;\n }\n }\n\n /**\n * Handle an unexpected transport close (crash / network drop). Clears the\n * dead client + tools so the next `ensureConnected` re-spawns. No-op if we\n * initiated the close ourselves (idle reap / reconcile removal).\n */\n private handleUnexpectedClose(handle: McpClientHandle): void {\n // Ignore if we initiated the close, or if this callback belongs to a\n // client we've already replaced (stale late-fire after a respawn).\n if (this.closing || this.client !== handle) return;\n this.logger?.warn(`[mcp-bundler] server \"${this.name}\" connection closed unexpectedly; will re-spawn on next use`);\n this.client = undefined;\n this.tools = [];\n try {\n this.onUnexpectedClose?.();\n } catch {\n /* host hook must never break the connection lifecycle */\n }\n }\n\n /**\n * Re-discover tools. Used on reconnect or `tools/list_changed` notification.\n * Refresh-lock collapses concurrent refreshes; if one is in flight, the next\n * is queued (max 1 queued, since N>1 queued provides no extra freshness).\n */\n async refresh(): Promise<void> {\n if (!this.client) return this.ensureConnected();\n if (this.refreshInFlight) {\n this.refreshQueued = true;\n return;\n }\n this.refreshInFlight = true;\n try {\n const advertised = await this.client.listTools();\n this.tools = advertised.map((t) => ({\n prefixed: buildNamespacedToolName(this.name, t.name),\n server: this.name,\n original: t.name,\n label: (t.description ?? t.name).slice(0, 80),\n description: t.description ?? '',\n parameters: t.inputSchema,\n }));\n this.logger?.debug(`[mcp-bundler] server \"${this.name}\" refreshed, ${this.tools.length.toString()} tool(s)`);\n } finally {\n this.refreshInFlight = false;\n if (this.refreshQueued) {\n this.refreshQueued = false;\n // Trigger one more refresh; do not await so caller isn't blocked on cascading refreshes.\n void this.refresh().catch((err: unknown) => {\n this.logger?.warn(`[mcp-bundler] queued refresh for \"${this.name}\" failed`, { err: err instanceof Error ? err.message : String(err) });\n });\n }\n }\n }\n\n async callTool(originalName: string, args: unknown, signal?: AbortSignal): Promise<McpToolCallResult> {\n await this.ensureConnected();\n if (!this.client) throw new Error(`server \"${this.name}\" failed to connect`);\n this.lastUsedAt = Date.now();\n return this.client.callTool(originalName, args, signal ? { signal } : undefined);\n }\n\n /**\n * Close the underlying transport. Idempotent. If a connect is in flight\n * (warmup racing with reconcile-removal), wait for it to settle and then\n * close the client it produced — otherwise the child process is orphaned.\n */\n async close(): Promise<void> {\n if (this.connectInFlight) {\n await this.connectInFlight.catch(() => undefined);\n }\n // Mark this close as intentional so the transport's onclose callback\n // doesn't trip the unexpected-close re-spawn path.\n this.closing = true;\n const c = this.client;\n this.client = undefined;\n this.tools = [];\n if (c) await c.close().catch((err: unknown) => {\n this.logger?.warn(`[mcp-bundler] close error for \"${this.name}\"`, { err: err instanceof Error ? err.message : String(err) });\n });\n }\n\n /**\n * Stable hash of the config for diff detection in `reconcile`.\n * Two configs with the same hash are equivalent (no restart needed).\n */\n configFingerprint(): string {\n return JSON.stringify(this.config);\n }\n}\n\n/**\n * Build the production `connect` factory using the official MCP SDK.\n * Kept in a separate function so tests can substitute a mock without\n * pulling the SDK into the test bundle.\n */\nexport async function defaultConnect(server: McpServerConfig, ctx?: ConnectContext): Promise<McpClientHandle> {\n const { Client } = await import('@modelcontextprotocol/sdk/client/index.js');\n const client = new Client({ name: 'alfe-mcp-bundler', version: '0.0.0' }, {});\n\n if ('command' in server) {\n const stdio = server;\n const { StdioClientTransport } = await import('@modelcontextprotocol/sdk/client/stdio.js');\n const onStderrLine = ctx?.onStderrLine;\n const transport = new StdioClientTransport({\n command: stdio.command,\n args: stdio.args ?? [],\n env: { ...sanitizeStdioEnv(stdio.env) } as Record<string, string>,\n cwd: stdio.cwd,\n // Default is 'inherit' (child stderr lands on the HOST's fd 2, invisible\n // to any output monitoring). Pipe only when the host consumes it — an\n // unread pipe would back-pressure the child's stderr writes.\n ...(onStderrLine ? { stderr: 'pipe' as const } : {}),\n });\n if (onStderrLine) {\n // The SDK exposes the PassThrough immediately when stderr:'pipe', so\n // attaching before connect() captures early startup output too.\n let carry = '';\n transport.stderr?.on('data', (chunk: Buffer) => {\n const parts = (carry + chunk.toString()).split('\\n');\n carry = parts.pop() ?? '';\n for (const line of parts) {\n if (line.trim() === '') continue;\n try {\n onStderrLine(line);\n } catch {\n /* host hook must never break the transport */\n }\n }\n });\n }\n await client.connect(transport);\n } else {\n const remote = server;\n if (remote.transport === 'streamable-http') {\n const { StreamableHTTPClientTransport } = await import('@modelcontextprotocol/sdk/client/streamableHttp.js');\n const transport = new StreamableHTTPClientTransport(new URL(remote.url), {\n requestInit: { headers: remote.headers ?? {} },\n });\n await client.connect(transport);\n } else {\n // SSE is deprecated in newer MCP SDK in favor of streamable-http, but\n // some servers still only support SSE — keep transport for back-compat.\n /* eslint-disable @typescript-eslint/no-deprecated */\n const { SSEClientTransport } = await import('@modelcontextprotocol/sdk/client/sse.js');\n const transport = new SSEClientTransport(new URL(remote.url), {\n requestInit: { headers: remote.headers ?? {} },\n });\n /* eslint-enable @typescript-eslint/no-deprecated */\n await client.connect(transport);\n }\n }\n\n let closeHandler: (() => void) | undefined;\n let closed = false;\n const fireClose = () => {\n if (closed) return;\n closed = true;\n closeHandler?.();\n };\n // The high-level SDK Client proxies its transport's lifecycle callbacks.\n client.onclose = fireClose;\n client.onerror = fireClose;\n\n return {\n async listTools() {\n const result = await client.listTools();\n return result.tools.map((t) => ({\n name: t.name,\n description: t.description,\n inputSchema: t.inputSchema as Record<string, unknown>,\n }));\n },\n async callTool(name, args, opts) {\n return (await client.callTool({ name, arguments: args as Record<string, unknown> | undefined }, undefined, opts)) as McpToolCallResult;\n },\n async close() {\n closed = true; // suppress the onClose callback for an intentional close\n await client.close();\n },\n onClose(handler: () => void) {\n closeHandler = handler;\n },\n };\n}\n","import { Connection, defaultConnect, type ConnectionDeps } from './connection.js';\nimport { disambiguateAgainst } from './tool-naming.js';\nimport type {\n BundlerOptions,\n Logger,\n McpServerConfig,\n McpServerStatus,\n McpToolCallResult,\n McpToolDescriptor,\n McpToolErrorInfo,\n ReconcileDiff,\n} from './types.js';\n\nconst DEFAULT_IDLE_TTL_MS = 10 * 60 * 1000;\nconst DEFAULT_IDLE_SWEEP_INTERVAL_MS = 60 * 1000;\nconst DEFAULT_RETRY_SWEEP_INTERVAL_MS = 60 * 1000;\n\n/** First text content of an error result, for host error reporting. */\nfunction extractErrorText(result: McpToolCallResult): string {\n for (const item of result.content) {\n if (item.type === 'text' && typeof item.text === 'string') return item.text.slice(0, 500);\n }\n return '(no error text)';\n}\n\n/**\n * Provider-agnostic MCP server bundler. Holds N MCP server connections,\n * exposes a unified namespaced tool catalog, and routes calls to the right\n * server.\n *\n * Designed to be embedded in any host (OpenClaw plugin, AI proxy, Lambda).\n * Public surface is intentionally synchronous where the host needs sync\n * (snapshot, listTools), async only where I/O is unavoidable.\n */\nexport class McpBundler {\n private readonly logger: Logger | undefined;\n private readonly connections = new Map<string, Connection>();\n private readonly idleTtlMs: number;\n private readonly idleSweepIntervalMs: number;\n private idleSweepTimer: ReturnType<typeof setInterval> | undefined;\n private readonly retrySweepIntervalMs: number;\n private retrySweepTimer: ReturnType<typeof setInterval> | undefined;\n private retrySweepInFlight = false;\n private readonly deps: ConnectionDeps;\n private readonly onToolError: ((info: McpToolErrorInfo) => void) | undefined;\n private readonly onServerCrash: ((server: string) => void) | undefined;\n private readonly onServerStderr: ((server: string, line: string) => void) | undefined;\n private disposed = false;\n // Serialize reconcile() so concurrent callers (multiple plugin tool factory\n // ticks within the same ms) don't interleave and orphan Connections, leaking\n // child processes. Acquired via a chain-of-promises latch.\n private reconcileLatch: Promise<unknown> = Promise.resolve();\n\n constructor(opts: BundlerOptions = {}, deps?: ConnectionDeps) {\n this.logger = opts.logger;\n this.idleTtlMs = opts.idleTtlMs ?? DEFAULT_IDLE_TTL_MS;\n this.idleSweepIntervalMs = opts.idleSweepIntervalMs ?? DEFAULT_IDLE_SWEEP_INTERVAL_MS;\n this.retrySweepIntervalMs = opts.retrySweepIntervalMs ?? DEFAULT_RETRY_SWEEP_INTERVAL_MS;\n this.deps = deps ?? { connect: defaultConnect };\n this.onToolError = opts.onToolError;\n this.onServerCrash = opts.onServerCrash;\n this.onServerStderr = opts.onServerStderr;\n if (this.idleTtlMs > 0) this.startIdleSweep();\n if (this.retrySweepIntervalMs > 0) this.startRetrySweep();\n }\n\n /** Construct a Connection with the host hooks bound to its server name. */\n private buildConnection(name: string, config: McpServerConfig): Connection {\n const crash = this.onServerCrash;\n const stderr = this.onServerStderr;\n return new Connection({\n name,\n config,\n deps: this.deps,\n logger: this.logger,\n ...(crash ? { onUnexpectedClose: () => { crash(name); } } : {}),\n ...(stderr ? { onStderrLine: (line: string) => { stderr(name, line); } } : {}),\n });\n }\n\n /** Fire the host's tool-error hook; exceptions must never affect the call path. */\n private reportToolError(info: McpToolErrorInfo): void {\n try {\n this.onToolError?.(info);\n } catch {\n /* host hook must never break tool routing */\n }\n }\n\n /**\n * Diff `desired` against current connections, spawn newcomers, dispose\n * removals, hot-restart on config change. Pull-based — call whenever the\n * host's config snapshot may have changed. Cheap if no diff.\n *\n * Lazy: newly-added servers are NOT eagerly connected; they connect on the\n * first `callTool()` (or first `listTools()` after `forceDiscover()`).\n * This avoids paying spawn cost for servers the agent never uses.\n */\n async reconcile(desired: Record<string, McpServerConfig>): Promise<ReconcileDiff> {\n if (this.disposed) throw new Error('McpBundler: disposed');\n // Serialize reconciles. Caller awaits its slot; in-flight reconciles run\n // in declaration order. Errors don't poison the latch — `.catch` swallows\n // for chaining, the actual error rejects the awaited slot.\n const slot = this.reconcileLatch.then(async () => this.doReconcile(desired));\n this.reconcileLatch = slot.catch(() => undefined);\n return slot;\n }\n\n private async doReconcile(desired: Record<string, McpServerConfig>): Promise<ReconcileDiff> {\n if (this.disposed) throw new Error('McpBundler: disposed');\n const desiredNames = new Set(Object.keys(desired));\n const currentNames = new Set(this.connections.keys());\n\n const added: string[] = [];\n const removed: string[] = [];\n const changed: string[] = [];\n const unchanged: string[] = [];\n\n // Removals: dispose connections no longer in desired set.\n for (const name of currentNames) {\n if (!desiredNames.has(name)) {\n const conn = this.connections.get(name);\n this.connections.delete(name);\n if (conn) await conn.close();\n removed.push(name);\n }\n }\n\n // Additions and changes.\n for (const [name, config] of Object.entries(desired)) {\n const existing = this.connections.get(name);\n if (!existing) {\n this.connections.set(name, this.buildConnection(name, config));\n added.push(name);\n continue;\n }\n const nextFingerprint = JSON.stringify(config);\n if (existing.configFingerprint() !== nextFingerprint) {\n // Config changed — close old, replace with fresh (lazy reconnect).\n await existing.close();\n this.connections.set(name, this.buildConnection(name, config));\n changed.push(name);\n } else {\n unchanged.push(name);\n }\n }\n\n if (added.length || removed.length || changed.length) {\n this.logger?.info('[mcp-bundler] reconciled', {\n added: added.length,\n removed: removed.length,\n changed: changed.length,\n unchanged: unchanged.length,\n });\n }\n return { added, removed, changed, unchanged };\n }\n\n /**\n * Synchronous snapshot of all currently-known tools across connected servers.\n * Servers that have not connected yet contribute nothing. Intended for use\n * inside OpenClaw's plugin tool factory which must be sync.\n *\n * Tool names are namespaced and disambiguated (suffix `-2`, `-3` on collision)\n * so cross-server name clashes never produce duplicate registrations.\n */\n listTools(): McpToolDescriptor[] {\n const seen = new Set<string>();\n const out: McpToolDescriptor[] = [];\n for (const conn of this.connections.values()) {\n for (const tool of conn.snapshotTools()) {\n const finalName = disambiguateAgainst(tool.prefixed, seen);\n seen.add(finalName);\n out.push(finalName === tool.prefixed ? tool : { ...tool, prefixed: finalName });\n }\n }\n return out;\n }\n\n /**\n * Eagerly connect to every configured server and discover tools. Used by\n * hosts that want a hot list rather than the lazy default. Errors are\n * swallowed per-server (logged), so one bad server doesn't fail the batch.\n */\n async warmup(): Promise<void> {\n if (this.disposed) return;\n await Promise.allSettled(\n Array.from(this.connections.values()).map(async (conn) => {\n try {\n await conn.ensureConnected();\n } catch (err) {\n this.logger?.warn(`[mcp-bundler] warmup failed for \"${conn.name}\"`, {\n err: err instanceof Error ? err.message : String(err),\n });\n }\n }),\n );\n }\n\n /**\n * Re-attempt every server that is NOT connected but HAS a recorded connect\n * failure (`connected: false && lastError`). This is the self-heal path: a\n * server that failed its first warm — because its backing credential /\n * account / network wasn't resolvable yet — gets reconnected once that\n * dependency appears, WITHOUT needing an operator to restart the daemon.\n *\n * Convergence: this only calls `ensureConnected()`, which honours each\n * Connection's own exponential reconnect backoff (`reconnectBlockedUntilMs`,\n * 500ms → 30s cap). A server that keeps `exit(1)`-ing fast-fails while in\n * backoff, so repeated sweeps are cheap and never become a tight crash-loop\n * — the backoff widens with each failure. Servers that connect on their\n * first warm, and lazily-added servers that never warmed (no `lastError`),\n * are left alone.\n *\n * Returns the statuses of the servers it attempted (empty if none needed a\n * retry). Never throws — per-server failures are reflected in the status.\n */\n async retryFailed(): Promise<McpServerStatus[]> {\n if (this.disposed) return [];\n const targets = Array.from(this.connections.values()).filter(\n (conn) => !conn.isConnected() && conn.lastErrorMessage() !== undefined,\n );\n if (targets.length === 0) return [];\n const results = await Promise.allSettled(\n targets.map(async (conn) => {\n try {\n await conn.ensureConnected();\n } catch {\n /* failure (or still-in-backoff) is recorded on the connection */\n }\n return this.statusOf(conn);\n }),\n );\n return results.flatMap((r) => (r.status === 'fulfilled' ? [r.value] : []));\n }\n\n /** Build the live status descriptor for one connection. */\n private statusOf(conn: Connection): McpServerStatus {\n const status: McpServerStatus = {\n name: conn.name,\n connected: conn.isConnected(),\n toolCount: conn.toolCount(),\n consecutiveFailures: conn.failureCount(),\n };\n const err = conn.lastErrorMessage();\n if (err !== undefined) status.lastError = err;\n return status;\n }\n\n /** Live status for every known server. Synchronous snapshot, no I/O. */\n statuses(): McpServerStatus[] {\n return Array.from(this.connections.values()).map((conn) => this.statusOf(conn));\n }\n\n /**\n * Eagerly connect ONE server and return its resulting status. Unlike\n * `warmup()` — which fans out over all servers and swallows failures with\n * no return value — this surfaces the outcome so a caller (e.g. the daemon\n * confirming an `alfe mcp add`) can report \"connected, N tools\" or the exact\n * connect error back to the agent.\n *\n * Never throws: a connect failure (or a warm timeout) is reflected in the\n * returned status (`connected: false`, `lastError` set). Returns `undefined`\n * only when the named server isn't present in the bundler.\n */\n async warmServer(name: string, timeoutMs?: number): Promise<McpServerStatus | undefined> {\n if (this.disposed) return undefined;\n const conn = this.connections.get(name);\n if (!conn) return undefined;\n const connect = conn.ensureConnected();\n // If the timeout wins the race below, `connect` stays pending and could\n // reject later — swallow that so it never becomes an unhandled rejection.\n // The connection records its own `lastError`, which `statusOf` reads.\n void connect.catch(() => undefined);\n try {\n if (timeoutMs !== undefined && timeoutMs > 0) {\n await this.raceTimeout(connect, timeoutMs, name);\n } else {\n await connect;\n }\n } catch (err) {\n const status = this.statusOf(conn);\n // A warm timeout is ours, not the connection's — make sure the status\n // carries a message even if the underlying connect hasn't failed yet.\n status.lastError ??= err instanceof Error ? err.message : String(err);\n return status;\n }\n return this.statusOf(conn);\n }\n\n /** Race a promise against a timeout, clearing the timer either way. */\n private async raceTimeout(p: Promise<void>, timeoutMs: number, name: string): Promise<void> {\n let timer: ReturnType<typeof setTimeout> | undefined;\n const timeout = new Promise<never>((_, reject) => {\n timer = setTimeout(() => {\n reject(new Error(`server \"${name}\" warm timed out after ${timeoutMs.toString()}ms`));\n }, timeoutMs);\n if (typeof timer === 'object' && 'unref' in timer) {\n (timer as { unref: () => void }).unref();\n }\n });\n try {\n await Promise.race([p, timeout]);\n } finally {\n // The Promise executor runs synchronously, so `timer` is always set here.\n clearTimeout(timer);\n }\n }\n\n /**\n * Invoke a tool by its namespaced name. Routes to the originating server.\n * Errors are returned as `{ isError: true, content: [...] }` so a failing\n * tool doesn't crash the host.\n */\n async callTool(prefixed: string, args: unknown, signal?: AbortSignal): Promise<McpToolCallResult> {\n if (this.disposed) {\n return { isError: true, content: [{ type: 'text', text: 'mcp-bundler disposed' }] };\n }\n const route = this.routeToolName(prefixed);\n if (!route) {\n return {\n isError: true,\n content: [{ type: 'text', text: `unknown tool: ${prefixed}` }],\n };\n }\n try {\n const result = await route.connection.callTool(route.original, args, signal);\n if (result.isError) {\n this.reportToolError({\n server: route.connection.name,\n tool: route.original,\n prefixed,\n kind: 'result-error',\n message: extractErrorText(result),\n });\n }\n return result;\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err);\n this.logger?.error(`[mcp-bundler] tool call failed for \"${prefixed}\"`, { err: msg });\n this.reportToolError({\n server: route.connection.name,\n tool: route.original,\n prefixed,\n kind: 'thrown',\n message: msg,\n });\n return {\n isError: true,\n content: [{ type: 'text', text: `tool ${prefixed} failed: ${msg}` }],\n };\n }\n }\n\n /**\n * Resolve a namespaced tool name back to its server connection and original\n * tool name. Returns undefined if the tool is not currently advertised.\n */\n private routeToolName(prefixed: string): { connection: Connection; original: string } | undefined {\n for (const conn of this.connections.values()) {\n for (const tool of conn.snapshotTools()) {\n if (tool.prefixed === prefixed) return { connection: conn, original: tool.original };\n }\n }\n return undefined;\n }\n\n /**\n * Tear down all connections and stop background tasks. Idempotent.\n * Call from `registerRuntimeLifecycle({ cleanup })` in the host plugin.\n */\n async dispose(): Promise<void> {\n if (this.disposed) return;\n this.disposed = true;\n if (this.idleSweepTimer) {\n clearInterval(this.idleSweepTimer);\n this.idleSweepTimer = undefined;\n }\n if (this.retrySweepTimer) {\n clearInterval(this.retrySweepTimer);\n this.retrySweepTimer = undefined;\n }\n await Promise.allSettled(Array.from(this.connections.values()).map((c) => c.close()));\n this.connections.clear();\n }\n\n private startIdleSweep(): void {\n this.idleSweepTimer = setInterval(() => {\n void this.sweepIdle().catch((err: unknown) => {\n this.logger?.warn('[mcp-bundler] idle sweep error', {\n err: err instanceof Error ? err.message : String(err),\n });\n });\n }, this.idleSweepIntervalMs);\n // Don't keep the host process alive just for the sweep.\n if (typeof this.idleSweepTimer === 'object' && 'unref' in this.idleSweepTimer) {\n (this.idleSweepTimer as { unref: () => void }).unref();\n }\n }\n\n private startRetrySweep(): void {\n this.retrySweepTimer = setInterval(() => {\n // Guard against overlap: a slow retry (spawn/handshake) must not stack up\n // if the previous sweep is still resolving.\n if (this.retrySweepInFlight) return;\n this.retrySweepInFlight = true;\n void this.retryFailed()\n .then((attempted) => {\n if (attempted.length === 0) return;\n const healed = attempted.filter((s) => s.connected);\n const stillDown = attempted.filter((s) => !s.connected);\n this.logger?.debug('[mcp-bundler] retry sweep', {\n attempted: attempted.length,\n healed: healed.length,\n stillDown: stillDown.length,\n });\n for (const s of healed) {\n this.logger?.info(`[mcp-bundler] server \"${s.name}\" self-healed on retry sweep`, {\n toolCount: s.toolCount,\n });\n }\n })\n .catch((err: unknown) => {\n this.logger?.warn('[mcp-bundler] retry sweep error', {\n err: err instanceof Error ? err.message : String(err),\n });\n })\n .finally(() => {\n this.retrySweepInFlight = false;\n });\n }, this.retrySweepIntervalMs);\n // Don't keep the host process alive just for the sweep.\n if (typeof this.retrySweepTimer === 'object' && 'unref' in this.retrySweepTimer) {\n (this.retrySweepTimer as { unref: () => void }).unref();\n }\n }\n\n private async sweepIdle(): Promise<void> {\n if (this.idleTtlMs <= 0) return;\n const targets: Connection[] = [];\n for (const conn of this.connections.values()) {\n if (conn.isConnected() && conn.idleSinceMs() > this.idleTtlMs) {\n targets.push(conn);\n }\n }\n if (targets.length === 0) return;\n this.logger?.debug(`[mcp-bundler] reaping ${targets.length.toString()} idle server(s)`);\n await Promise.allSettled(targets.map((c) => c.close()));\n }\n}\n","import {\n closeSync,\n existsSync,\n mkdirSync,\n openSync,\n readFileSync,\n renameSync,\n statSync,\n unlinkSync,\n watch,\n writeFileSync,\n type FSWatcher,\n} from 'node:fs';\nimport { dirname, join } from 'node:path';\nimport { homedir } from 'node:os';\nimport type { Logger, McpServerConfig, StdioServerConfig, RemoteServerConfig, McpTransportKind } from './types.js';\n\n/**\n * Where a server entry came from. Used by `removeServersByOwner` so an\n * integration uninstall can drop only its own entries without touching\n * `cli`-owned (e.g. `alfe-platform`) or `manual`-owned (user-added) ones.\n */\nexport type ServerOwner = 'cli' | `integration:${string}` | 'manual';\n\ninterface StoredServerCommon {\n /** Where the entry came from — controls bulk-removal semantics. */\n owner: ServerOwner;\n /** ISO timestamp of first registration; preserved across updates. */\n addedAt: string;\n /** Optional semver of the providing package (e.g. `@alfe.ai/mcp-server` for `alfe-platform`). Used for drift detection on CLI upgrade. */\n version?: string;\n}\n\nexport type StoredServerEntry =\n | (StoredServerCommon & { transport: 'stdio' } & StdioServerConfig)\n | (StoredServerCommon & { transport: 'sse' | 'streamable-http' } & RemoteServerConfig);\n\nexport interface StoreSchema {\n servers: Record<string, StoredServerEntry>;\n config: {\n sessionIdleTtlMs?: number;\n };\n /**\n * Server names this manager has written into `openclaw.json#mcp.servers.*`.\n * Used to compute the mirror-write diff without re-reading openclaw.json\n * (which would be a second source of truth). Foreign keys not listed here\n * are preserved across mirror writes.\n */\n _ownedOpenclawKeys: string[];\n}\n\nconst DEFAULT_STORE_DIR = join(homedir(), '.alfe', 'mcp');\nconst DEFAULT_STORE_PATH = join(DEFAULT_STORE_DIR, 'servers.json');\n\n/** Inter-process lock tunings — exported as constants so tests can override. */\nconst LOCK_WAIT_MS = 5_000;\nconst LOCK_RETRY_INTERVAL_MS = 25;\nconst LOCK_STALE_MS = 10_000;\n\nexport interface StoreOptions {\n /** Absolute path to the store file. Defaults to `~/.alfe/mcp/servers.json`. */\n path?: string;\n logger?: Logger;\n}\n\n/**\n * On-disk source of truth for the bundler's configured servers.\n *\n * Mutations go through `update()` (read-modify-write with atomic\n * temp+rename) so a concurrent writer (e.g. two `alfe mcp add` invocations\n * racing) can't lose data — the second writer reads the first's state.\n *\n * Schema is owner-tagged so `removeServersByOwner` can implement\n * integration uninstall without touching CLI-owned or manual entries.\n */\nexport class Store {\n private readonly storePath: string;\n private readonly logger?: Logger;\n private watcher?: FSWatcher;\n private watcherListeners = new Set<() => void>();\n private rewatchTimer?: NodeJS.Timeout;\n\n constructor(opts: StoreOptions = {}) {\n this.storePath = opts.path ?? DEFAULT_STORE_PATH;\n this.logger = opts.logger;\n }\n\n get path(): string {\n return this.storePath;\n }\n\n read(): StoreSchema {\n if (!existsSync(this.storePath)) return cloneEmpty();\n try {\n const raw = readFileSync(this.storePath, 'utf8');\n const parsed: unknown = JSON.parse(raw);\n return normalize(parsed);\n } catch (err) {\n this.logger?.warn('[mcp-bundler/store] failed to read store; returning empty', {\n err: errMsg(err),\n path: this.storePath,\n });\n return cloneEmpty();\n }\n }\n\n /**\n * Read-modify-write with atomic temp+rename, guarded by an\n * inter-process lock file. Caller passes a pure function that\n * produces the next state; this serialises the mutation to disk in\n * one rename, which is atomic on POSIX and on Windows when the\n * target path is on the same volume.\n *\n * The lock guards the read-then-rename window so two processes\n * (e.g. two `alfe mcp add` shells, or the CLI racing the daemon)\n * can't drop each other's writes. The lock file is at\n * `<storePath>.lock`; stale locks (older than `LOCK_STALE_MS`) are\n * stolen so a crashed writer doesn't wedge the store.\n *\n * Pure-function shape (instead of a `read()` then `write(next)`\n * pair) intentionally — it keeps the read-modify-write contract\n * local to each caller so two updates back-to-back never see each\n * other's partial state.\n */\n update(fn: (cur: StoreSchema) => StoreSchema): StoreSchema {\n mkdirSync(dirname(this.storePath), { recursive: true });\n const release = this.acquireLock();\n try {\n const cur = this.read();\n const next = fn(cur);\n const tempPath = `${this.storePath}.${String(process.pid)}.${String(Date.now())}.tmp`;\n writeFileSync(tempPath, JSON.stringify(next, null, 2), { encoding: 'utf8', mode: 0o600 });\n try {\n renameSync(tempPath, this.storePath);\n } catch (err) {\n try {\n unlinkSync(tempPath);\n } catch {\n // Best-effort cleanup; the temp file's name has the pid + timestamp\n // so an orphan won't collide with future writers.\n }\n throw err;\n }\n return next;\n } finally {\n release();\n }\n }\n\n /**\n * Acquire an inter-process file lock by atomically creating a\n * sentinel via `openSync(lockPath, 'wx')`. Spins with bounded\n * backoff up to `LOCK_WAIT_MS`. If the lock file is older than\n * `LOCK_STALE_MS` it's assumed orphaned (writer crashed mid-update)\n * and stolen — the write window is sub-second in practice, so\n * holding the lock for >5s means something went wrong.\n *\n * Returns the release function. Single-process callers are\n * unaffected — re-entering the same process spins briefly while\n * the prior call's `finally` runs.\n */\n private acquireLock(): () => void {\n const lockPath = `${this.storePath}.lock`;\n const deadline = Date.now() + LOCK_WAIT_MS;\n let fd = -1;\n for (;;) {\n try {\n fd = openSync(lockPath, 'wx', 0o600);\n break;\n } catch (err) {\n const code = (err as NodeJS.ErrnoException).code;\n if (code !== 'EEXIST') throw err;\n if (this.lockIsStale(lockPath)) {\n try {\n unlinkSync(lockPath);\n } catch {\n // Another process may have just released it — fall through and retry.\n }\n continue;\n }\n if (Date.now() >= deadline) {\n throw new Error(\n `Store.update: timed out waiting for ${lockPath} (held by another writer or stale lock)`,\n );\n }\n // Synchronous spin — the lock window is sub-second under\n // normal load; busy-waiting briefly is simpler than wiring\n // async/await through every `update()` caller.\n const sleepUntil = Date.now() + LOCK_RETRY_INTERVAL_MS;\n while (Date.now() < sleepUntil) { /* spin */ }\n }\n }\n const held = fd;\n return () => {\n try {\n closeSync(held);\n } catch {\n // ignore — the unlink is what releases the lock for the next writer.\n }\n try {\n unlinkSync(lockPath);\n } catch {\n // ignore — already unlinked or stolen by a stale-lock breaker.\n }\n };\n }\n\n private lockIsStale(lockPath: string): boolean {\n try {\n const st = statSync(lockPath);\n return Date.now() - st.mtimeMs > LOCK_STALE_MS;\n } catch {\n return false;\n }\n }\n\n /**\n * Watch the store file for external changes (e.g. another `alfe mcp add`\n * shelling out from a separate process). Returns an unsubscribe fn.\n *\n * Coalesces bursts via a 50 ms debounce — editors and atomic-rename\n * writers commonly fire multiple events per logical save.\n */\n watch(cb: () => void): () => void {\n this.watcherListeners.add(cb);\n this.ensureWatcher();\n return () => {\n this.watcherListeners.delete(cb);\n if (this.watcherListeners.size === 0) this.disposeWatcher();\n };\n }\n\n dispose(): void {\n this.watcherListeners.clear();\n this.disposeWatcher();\n }\n\n private ensureWatcher(): void {\n if (this.watcher) return;\n mkdirSync(dirname(this.storePath), { recursive: true });\n // Some platforms / atomic renames make a per-file watch flaky after\n // a rename; watching the parent directory and filtering by basename\n // is more robust.\n const dir = dirname(this.storePath);\n const basename = this.storePath.slice(dir.length + 1);\n let pending: NodeJS.Timeout | undefined;\n const fire = (): void => {\n pending = undefined;\n for (const cb of this.watcherListeners) {\n try {\n cb();\n } catch (err) {\n this.logger?.warn('[mcp-bundler/store] watcher listener threw', { err: errMsg(err) });\n }\n }\n };\n try {\n this.watcher = watch(dir, (_event, fn) => {\n if (fn !== basename) return;\n if (pending) clearTimeout(pending);\n pending = setTimeout(fire, 50);\n });\n this.watcher.on('error', (err) => {\n this.logger?.warn('[mcp-bundler/store] watcher error; retrying in 1s', { err: errMsg(err) });\n this.disposeWatcher();\n if (!this.rewatchTimer && this.watcherListeners.size > 0) {\n this.rewatchTimer = setTimeout(() => {\n this.rewatchTimer = undefined;\n this.ensureWatcher();\n }, 1000);\n this.rewatchTimer.unref();\n }\n });\n } catch (err) {\n this.logger?.warn('[mcp-bundler/store] failed to start watcher', { err: errMsg(err) });\n }\n }\n\n private disposeWatcher(): void {\n if (this.watcher) {\n try {\n this.watcher.close();\n } catch {\n // close throws on already-disposed watchers; ignore.\n }\n this.watcher = undefined;\n }\n if (this.rewatchTimer) {\n clearTimeout(this.rewatchTimer);\n this.rewatchTimer = undefined;\n }\n }\n}\n\nexport function defaultStorePath(): string {\n return DEFAULT_STORE_PATH;\n}\n\n/** Pull the runtime config (transport + transport-specific fields) out of a stored entry. */\nexport function toServerConfig(entry: StoredServerEntry): McpServerConfig {\n if (entry.transport === 'stdio') {\n const { command, args, env, cwd } = entry;\n const cfg: StdioServerConfig = { command };\n if (args) cfg.args = args;\n if (env) cfg.env = env;\n if (cwd) cfg.cwd = cwd;\n return cfg;\n }\n const { url, transport, headers, connectionTimeoutMs } = entry;\n const cfg: RemoteServerConfig = { url, transport };\n if (headers) cfg.headers = headers;\n if (connectionTimeoutMs !== undefined) cfg.connectionTimeoutMs = connectionTimeoutMs;\n return cfg;\n}\n\n/** Build a stored entry from a runtime config + ownership metadata. */\nexport function toStoredEntry(\n config: McpServerConfig,\n meta: { owner: ServerOwner; transport?: McpTransportKind; version?: string; addedAt?: string },\n): StoredServerEntry {\n const addedAt = meta.addedAt ?? new Date().toISOString();\n if ('command' in config) {\n return {\n transport: 'stdio',\n owner: meta.owner,\n addedAt,\n ...(meta.version !== undefined ? { version: meta.version } : {}),\n ...config,\n };\n }\n const transport = meta.transport ?? config.transport ?? 'sse';\n if (transport === 'stdio') {\n throw new Error('toStoredEntry: transport=stdio specified but config is remote-shaped');\n }\n return {\n transport,\n owner: meta.owner,\n addedAt,\n ...(meta.version !== undefined ? { version: meta.version } : {}),\n ...config,\n };\n}\n\nfunction cloneEmpty(): StoreSchema {\n return { servers: {}, config: {}, _ownedOpenclawKeys: [] };\n}\n\nfunction normalize(raw: unknown): StoreSchema {\n if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return cloneEmpty();\n const r = raw as Partial<StoreSchema>;\n return {\n servers: r.servers && typeof r.servers === 'object' ? r.servers : {},\n config: r.config && typeof r.config === 'object' ? r.config : {},\n _ownedOpenclawKeys: Array.isArray(r._ownedOpenclawKeys) ? r._ownedOpenclawKeys.slice() : [],\n };\n}\n\nfunction errMsg(err: unknown): string {\n return err instanceof Error ? err.message : String(err);\n}\n","import type { McpBundler } from './bundler.js';\nimport { Store, toServerConfig, toStoredEntry, type ServerOwner, type StoredServerEntry } from './store.js';\nimport type { Logger, McpServerConfig, McpServerStatus, McpTransportKind } from './types.js';\n\nexport interface ManagerOptions {\n /** Pre-constructed store. If omitted, one is built with default options. */\n store?: Store;\n logger?: Logger;\n}\n\nexport interface AddServerOptions {\n /** Required — flat-namespace key under the bundler store. */\n id: string;\n /** Marks ownership for bulk removal. Defaults to `manual`. */\n owner?: ServerOwner;\n /** Semver of the providing package; used for CLI version-drift detection. */\n version?: string;\n /** Explicit transport hint for remote configs. Defaults to inferring from `config`. */\n transport?: McpTransportKind;\n}\n\n/**\n * Bundler manager — owns the `~/.alfe/mcp/servers.json` store and surfaces a\n * small CRUD API the CLI and integration applier both call into.\n *\n * Single source of truth: every consumer (daemon-hosted bundler, CLI `alfe mcp\n * list`, integration uninstall) reads from this store. Openclaw.json is no\n * longer kept in sync — the daemon hosts the bundler children and the\n * openclaw plugin reaches them via IPC, so the openclaw.json mirror became\n * dead weight and an active source of duplicate spawning on claude-cli /\n * codex-cli backends.\n *\n * Call `loadIntoBundler(bundler)` once at daemon startup to wire the store\n * into a live `McpBundler` — subsequent store mutations (including those\n * landed by other processes via the file watcher) re-reconcile automatically.\n */\nexport class Manager {\n private readonly store: Store;\n private readonly logger?: Logger;\n private bundler?: McpBundler;\n private changeListeners = new Set<() => void>();\n private storeUnsubscribe?: () => void;\n\n constructor(opts: ManagerOptions = {}) {\n this.store = opts.store ?? new Store({ logger: opts.logger });\n this.logger = opts.logger;\n }\n\n /** Direct accessor — handy for tests and the CLI's `alfe mcp list`. */\n getStore(): Store {\n return this.store;\n }\n\n /**\n * Register or overwrite a server entry. Mutation lands in the store\n * synchronously; if a bundler has been attached via `loadIntoBundler`,\n * it gets re-reconciled in the background (errors logged, never\n * thrown — the store is the source of truth, the bundler is derived).\n */\n async addServer(config: McpServerConfig, opts: AddServerOptions): Promise<void> {\n if (!opts.id) throw new Error('Manager.addServer: id is required');\n const owner = opts.owner ?? 'manual';\n this.store.update((cur) => {\n const previousAddedAt = lookupAddedAt(cur.servers, opts.id);\n const entry = toStoredEntry(config, {\n owner,\n transport: opts.transport,\n version: opts.version,\n addedAt: previousAddedAt,\n });\n return {\n ...cur,\n servers: { ...cur.servers, [opts.id]: entry },\n };\n });\n this.scheduleBundlerReconcile();\n this.fireChange();\n return Promise.resolve();\n }\n\n /**\n * Remove a single server entry. No-op if the id isn't in the store.\n * Refuses to remove an entry whose owner doesn't match `expectedOwner`\n * when supplied — the CLI uses this to guard `alfe mcp remove` from\n * accidentally clobbering integration- or cli-owned entries.\n */\n removeServer(id: string, opts: { expectedOwner?: ServerOwner } = {}): Promise<boolean> {\n const current = this.store.read();\n const existing = lookupEntry(current.servers, id);\n if (!existing) return Promise.resolve(false);\n if (opts.expectedOwner && existing.owner !== opts.expectedOwner) {\n return Promise.reject(\n new Error(\n `Manager.removeServer: server \"${id}\" is owned by \"${existing.owner}\", not \"${opts.expectedOwner}\"`,\n ),\n );\n }\n this.store.update((cur) => ({\n ...cur,\n servers: Object.fromEntries(Object.entries(cur.servers).filter(([k]) => k !== id)),\n }));\n this.scheduleBundlerReconcile();\n this.fireChange();\n return Promise.resolve(true);\n }\n\n /** Drop every entry whose owner matches — used by integration uninstall. */\n async removeServersByOwner(owner: ServerOwner): Promise<string[]> {\n const removed: string[] = [];\n this.store.update((cur) => {\n const next: Record<string, StoredServerEntry> = {};\n for (const [id, entry] of Object.entries(cur.servers)) {\n if (entry.owner === owner) {\n removed.push(id);\n } else {\n next[id] = entry;\n }\n }\n if (removed.length === 0) return cur;\n return { ...cur, servers: next };\n });\n if (removed.length > 0) {\n this.scheduleBundlerReconcile();\n this.fireChange();\n }\n return Promise.resolve(removed);\n }\n\n /** Read-only snapshot for `alfe mcp list` and similar UIs. */\n listServers(): { id: string; entry: StoredServerEntry }[] {\n const snap = this.store.read();\n return Object.entries(snap.servers).map(([id, entry]) => ({ id, entry }));\n }\n\n /**\n * Live connection status per server from the attached bundler. Empty when\n * no bundler is attached (e.g. a CLI-only manager). Lets callers show\n * whether each registered server actually connected and how many tools it\n * advertises, instead of only the stored config.\n */\n serverStatuses(): McpServerStatus[] {\n return this.bundler ? this.bundler.statuses() : [];\n }\n\n /**\n * Reconcile the bundler against the current store (so a just-added entry\n * has a connection object) then eagerly connect ONE server and return its\n * status. Used by the daemon to CONFIRM an `alfe mcp add` actually connected\n * before replying to the agent — turning the fire-and-forget warm into a\n * result the caller can report (\"connected, N tools\" or the real error).\n *\n * Returns `null` when no bundler is attached or the id isn't present; never\n * throws (a connect failure is carried in the returned status).\n */\n async warmServer(id: string, timeoutMs?: number): Promise<McpServerStatus | null> {\n if (!this.bundler) return null;\n await this.reconcileBundler();\n return (await this.bundler.warmServer(id, timeoutMs)) ?? null;\n }\n\n /**\n * Push the current store contents into a bundler instance (which owns\n * connections / tools). Wires up a store watcher so external mutations\n * (e.g. another shell running `alfe mcp add`) re-reconcile.\n */\n async loadIntoBundler(bundler: McpBundler): Promise<void> {\n this.bundler = bundler;\n await this.reconcileBundler();\n this.storeUnsubscribe ??= this.store.watch(() => {\n void this.reconcileBundler().catch((err: unknown) => {\n this.logger?.warn('[mcp-bundler/manager] watcher reconcile failed', { err: errMsg(err) });\n });\n });\n }\n\n /** Subscribe to store mutations. Returns an unsubscribe fn. */\n onChange(cb: () => void): () => void {\n this.changeListeners.add(cb);\n return () => {\n this.changeListeners.delete(cb);\n };\n }\n\n /**\n * Detach from the bundler and stop watching the store. Safe to call\n * multiple times. Does not dispose the underlying `Store` so the\n * shared instance survives multi-manager environments (rare).\n */\n async dispose(): Promise<void> {\n if (this.storeUnsubscribe) {\n this.storeUnsubscribe();\n this.storeUnsubscribe = undefined;\n }\n this.store.dispose();\n this.changeListeners.clear();\n this.bundler = undefined;\n return Promise.resolve();\n }\n\n private scheduleBundlerReconcile(): void {\n if (!this.bundler) return;\n void this.reconcileBundler().catch((err: unknown) => {\n this.logger?.warn('[mcp-bundler/manager] bundler reconcile failed', { err: errMsg(err) });\n });\n }\n\n private async reconcileBundler(): Promise<void> {\n if (!this.bundler) return;\n const snap = this.store.read();\n const servers: Record<string, McpServerConfig> = {};\n for (const [id, entry] of Object.entries(snap.servers)) {\n servers[id] = toServerConfig(entry);\n }\n await this.bundler.reconcile(servers);\n }\n\n private fireChange(): void {\n for (const cb of this.changeListeners) {\n try {\n cb();\n } catch (err) {\n this.logger?.warn('[mcp-bundler/manager] onChange listener threw', { err: errMsg(err) });\n }\n }\n }\n}\n\nfunction errMsg(err: unknown): string {\n return err instanceof Error ? err.message : String(err);\n}\n\n/**\n * Indexed access on `Record<string, T>` returns `T` (not `T | undefined`)\n * unless `noUncheckedIndexedAccess` is set in tsconfig. These helpers\n * make the optional-ness explicit so the lint rules that hate\n * always-truthy conditionals stop firing on real lookups.\n */\nfunction lookupEntry(\n servers: Record<string, StoredServerEntry>,\n id: string,\n): StoredServerEntry | undefined {\n return Object.hasOwn(servers, id) ? servers[id] : undefined;\n}\n\nfunction lookupAddedAt(servers: Record<string, StoredServerEntry>, id: string): string | undefined {\n const entry = lookupEntry(servers, id);\n return entry ? entry.addedAt : undefined;\n}\n","/**\n * Pattern A validator — locks the \"explicit selector arg\" contract for\n * credential-touching tools across the openclaw-* plugin family.\n *\n * Background\n * ----------\n * Pattern A says every credential-touching tool on a multi-account-capable\n * provider MUST take a required selector arg in its JSON Schema so the LLM\n * picks the account deliberately. The reference implementation lives in\n * `@alfe.ai/openclaw-google` (`google_run_command` / `google_disconnect_\n * account` both require `email`). PR 7 of channels-and-credential-driven-\n * integrations sweeps the same shape across notion / xero / myob.\n *\n * Why a validator\n * ---------------\n * The contract is easy to break by accident — a new tool gets added, the\n * selector arg gets forgotten, the LLM silently dispatches to whichever\n * account was loaded first. There's no runtime safety net once the plugin\n * is published. This validator runs in plugin build steps (or CI) and\n * fails the build if a tool that declares itself credential-touching\n * doesn't carry the selector.\n *\n * Scope of this file\n * ------------------\n * Pure functions — no I/O. Plugin packages call into these helpers from\n * their own build scripts (e.g. `pnpm build` or a dedicated lint task) and\n * pass in their `ToolDef[]` or `McpToolDescriptor[]` collection. The\n * validator does NOT know how to read manifests or files — it only checks\n * the shape of the in-memory descriptor list. Wiring is up to each plugin.\n *\n * Consumers\n * ---------\n * - `@alfe.ai/myob-mcp` — direct MCP, explicit TypeBox schemas.\n * - `@alfe.ai/notion-mcp` — proxy MCP, injects selector at runtime.\n * - `@alfe.ai/xero-mcp` — proxy MCP, injects selector at runtime.\n * - `@alfe.ai/openclaw-google` — already compliant; can opt-in for regression.\n *\n * Future\n * ------\n * When PR 7-deferred lands (atlassian / github / microsoft openclaw\n * packages), they hook into the same validator. The atlassian / github\n * cases will likely run the validator over the proxied child server's\n * `listTools()` response after schema-injection, to confirm the injection\n * actually landed on every tool.\n */\n\nimport type { McpToolDescriptor } from \"./types.js\";\n\n// ── Public types ────────────────────────────────────────────\n\n/**\n * A tool descriptor that the validator can inspect. Plugins can pass\n * either {@link McpToolDescriptor} (for proxy plugins surfacing child\n * tools) or a leaner local shape (for direct-MCP plugins). Both come\n * down to a tool name and a JSON Schema parameter object.\n */\nexport interface ValidatableTool {\n /** Tool name as the LLM sees it (post-namespacing). */\n name: string;\n /** JSON Schema for tool parameters (the `inputSchema`). */\n parameters: Record<string, unknown>;\n}\n\nexport interface PatternAOptions {\n /**\n * The required selector property name. Provider-specific (Google uses\n * `email`; Notion will use `workspaceId`; Xero will use `xeroTenantId`;\n * MYOB will use `myobBusinessId`). Plugins MAY support more than one\n * acceptable name — pass an array.\n */\n selector: string | readonly string[];\n /**\n * Tool names exempt from the selector requirement — typically the\n * `list_accounts` discovery tool and any pure-utility tools that don't\n * touch credentials. Match is exact (post-namespacing).\n */\n exempt?: readonly string[];\n}\n\nexport interface PatternAViolation {\n tool: string;\n reason:\n | \"missing-selector-property\"\n | \"selector-not-required\"\n | \"selector-property-not-string\";\n detail: string;\n}\n\n// ── Core check ───────────────────────────────────────────────\n\n/**\n * Validate that every non-exempt tool's JSON Schema declares the selector\n * property AND lists it in `required`. Returns the full set of violations\n * so the caller can report them all in one pass — failing fast on the\n * first one tends to hide cascading bugs in real plugins.\n *\n * The check is intentionally schema-shape-only — it does NOT execute the\n * tool, call the LLM, or talk to the cloud. It's a fast structural pass\n * suitable for build-time use.\n */\nexport function checkPatternA(\n tools: readonly ValidatableTool[],\n options: PatternAOptions,\n): PatternAViolation[] {\n const selectorNames = typeof options.selector === \"string\"\n ? [options.selector]\n : options.selector;\n const exempt = new Set(options.exempt ?? []);\n const violations: PatternAViolation[] = [];\n\n for (const tool of tools) {\n if (exempt.has(tool.name)) continue;\n\n const schema = tool.parameters;\n const properties = (schema as { properties?: Record<string, unknown> }).properties ?? {};\n const required = (schema as { required?: unknown[] }).required ?? [];\n\n const matched = selectorNames.find((name) => name in properties);\n\n if (!matched) {\n violations.push({\n tool: tool.name,\n reason: \"missing-selector-property\",\n detail: `expected one of [${selectorNames.join(\", \")}] in inputSchema.properties`,\n });\n continue;\n }\n\n if (!required.includes(matched)) {\n violations.push({\n tool: tool.name,\n reason: \"selector-not-required\",\n detail: `selector \"${matched}\" present in properties but missing from inputSchema.required`,\n });\n continue;\n }\n\n const propSchema = properties[matched];\n const type = (propSchema as { type?: unknown }).type;\n if (type !== \"string\") {\n violations.push({\n tool: tool.name,\n reason: \"selector-property-not-string\",\n detail: `selector \"${matched}\" must be JSON Schema type=string (found ${JSON.stringify(type)})`,\n });\n }\n }\n\n return violations;\n}\n\n/**\n * Thin wrapper that throws an Error listing every violation if any are\n * present. Convenient for build scripts that want a single guard call.\n */\nexport function assertPatternA(\n tools: readonly ValidatableTool[],\n options: PatternAOptions,\n): void {\n const violations = checkPatternA(tools, options);\n if (violations.length === 0) return;\n const lines = violations.map((v) => ` - [${v.reason}] ${v.tool}: ${v.detail}`);\n throw new Error(\n `Pattern A validation failed for ${String(violations.length)} tool(s):\\n${lines.join(\"\\n\")}`,\n );\n}\n\n/**\n * Cast an {@link McpToolDescriptor} (proxy-plugin shape) to the leaner\n * {@link ValidatableTool} the validator accepts. Useful for proxy\n * plugins that already maintain a `cachedTools: McpToolDescriptor[]`\n * collection.\n */\nexport function fromMcpDescriptor(descriptor: McpToolDescriptor): ValidatableTool {\n return { name: descriptor.prefixed, parameters: descriptor.parameters };\n}\n"],"mappings":";;;;;;;;;;;;;;AAWA,MAAM,aAAa;AACnB,MAAM,UAAU;AAChB,MAAM,YAAY;AAElB,SAAgB,oBAAoB,OAAuB;AACzD,QAAO,MAAM,QAAQ,YAAY,IAAI;;AAGvC,SAAgB,wBAAwB,QAAgB,MAAsB;CAC5E,MAAM,OAAO,GAAG,oBAAoB,OAAO,GAAG,YAAY,oBAAoB,KAAK;AACnF,KAAI,KAAK,UAAU,QAAS,QAAO;CAEnC,MAAM,oBAAoB,oBAAoB,OAAO,CAAC,SAAS;CAC/D,MAAM,aAAa,KAAK,IAAI,GAAG,UAAU,kBAAkB;AAC3D,QAAO,GAAG,oBAAoB,OAAO,GAAG,Y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|
"description": "Provider-agnostic MCP server bundler — connects to N MCP servers (stdio/SSE/streamable-http), aggregates their tools with namespacing, exposes a unified callable surface",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|