@frontmcp/react 1.9.1-rc.3 → 1.9.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/ai/index.js CHANGED
@@ -87,7 +87,8 @@ function extractName(uri) {
87
87
  var DynamicRegistry = class {
88
88
  tools = /* @__PURE__ */ new Map();
89
89
  resources = /* @__PURE__ */ new Map();
90
- toolRefCounts = /* @__PURE__ */ new Map();
90
+ /** Every live registration of a tool name, oldest first; the newest one is the definition in use. */
91
+ toolRegistrations = /* @__PURE__ */ new Map();
91
92
  resourceRefCounts = /* @__PURE__ */ new Map();
92
93
  listeners = /* @__PURE__ */ new Set();
93
94
  resourceListeners = /* @__PURE__ */ new Set();
@@ -100,29 +101,41 @@ var DynamicRegistry = class {
100
101
  * subsequent registrations replace the definition (and notify, so
101
102
  * the server registration follows the new description, schema or
102
103
  * availability) but the tool is only removed when every registrant
103
- * has unregistered.
104
+ * has unregistered. When the registrant whose definition is in use
105
+ * unregisters, the newest remaining registration takes over, so a
106
+ * call never reaches an unmounted component's `execute`.
104
107
  */
105
108
  registerTool(def) {
106
- const existing = this.toolRefCounts.get(def.name) ?? 0;
107
- this.toolRefCounts.set(def.name, existing + 1);
109
+ const registrations = this.toolRegistrations.get(def.name) ?? [];
110
+ registrations.push(def);
111
+ this.toolRegistrations.set(def.name, registrations);
108
112
  this.tools.set(def.name, def);
109
113
  this.notify();
110
114
  let called = false;
111
115
  return () => {
112
116
  if (called) return;
113
117
  called = true;
114
- this.unregisterTool(def.name);
118
+ this.removeToolRegistration(def);
115
119
  };
116
120
  }
121
+ /** Drop the oldest registration of `name` (registrants should call the function `registerTool` returned). */
117
122
  unregisterTool(name) {
118
- const count = this.toolRefCounts.get(name);
119
- if (count == null) return;
120
- if (count <= 1) {
121
- this.toolRefCounts.delete(name);
122
- this.tools.delete(name);
123
+ const oldest = this.toolRegistrations.get(name)?.[0];
124
+ if (oldest) this.removeToolRegistration(oldest);
125
+ }
126
+ removeToolRegistration(def) {
127
+ const registrations = this.toolRegistrations.get(def.name);
128
+ const index = registrations?.lastIndexOf(def) ?? -1;
129
+ if (!registrations || index === -1) return;
130
+ registrations.splice(index, 1);
131
+ const newest = registrations[registrations.length - 1];
132
+ if (!newest) {
133
+ this.toolRegistrations.delete(def.name);
134
+ this.tools.delete(def.name);
135
+ this.notify();
136
+ } else if (this.tools.get(def.name) !== newest) {
137
+ this.tools.set(def.name, newest);
123
138
  this.notify();
124
- } else {
125
- this.toolRefCounts.set(name, count - 1);
126
139
  }
127
140
  }
128
141
  /**
@@ -212,7 +225,7 @@ var DynamicRegistry = class {
212
225
  if (this.tools.size === 0 && this.resources.size === 0) return;
213
226
  this.tools.clear();
214
227
  this.resources.clear();
215
- this.toolRefCounts.clear();
228
+ this.toolRegistrations.clear();
216
229
  this.resourceRefCounts.clear();
217
230
  this.notify();
218
231
  }
package/api/index.js CHANGED
@@ -83,7 +83,8 @@ function extractName(uri) {
83
83
  var DynamicRegistry = class {
84
84
  tools = /* @__PURE__ */ new Map();
85
85
  resources = /* @__PURE__ */ new Map();
86
- toolRefCounts = /* @__PURE__ */ new Map();
86
+ /** Every live registration of a tool name, oldest first; the newest one is the definition in use. */
87
+ toolRegistrations = /* @__PURE__ */ new Map();
87
88
  resourceRefCounts = /* @__PURE__ */ new Map();
88
89
  listeners = /* @__PURE__ */ new Set();
89
90
  resourceListeners = /* @__PURE__ */ new Set();
@@ -96,29 +97,41 @@ var DynamicRegistry = class {
96
97
  * subsequent registrations replace the definition (and notify, so
97
98
  * the server registration follows the new description, schema or
98
99
  * availability) but the tool is only removed when every registrant
99
- * has unregistered.
100
+ * has unregistered. When the registrant whose definition is in use
101
+ * unregisters, the newest remaining registration takes over, so a
102
+ * call never reaches an unmounted component's `execute`.
100
103
  */
101
104
  registerTool(def) {
102
- const existing = this.toolRefCounts.get(def.name) ?? 0;
103
- this.toolRefCounts.set(def.name, existing + 1);
105
+ const registrations = this.toolRegistrations.get(def.name) ?? [];
106
+ registrations.push(def);
107
+ this.toolRegistrations.set(def.name, registrations);
104
108
  this.tools.set(def.name, def);
105
109
  this.notify();
106
110
  let called = false;
107
111
  return () => {
108
112
  if (called) return;
109
113
  called = true;
110
- this.unregisterTool(def.name);
114
+ this.removeToolRegistration(def);
111
115
  };
112
116
  }
117
+ /** Drop the oldest registration of `name` (registrants should call the function `registerTool` returned). */
113
118
  unregisterTool(name) {
114
- const count = this.toolRefCounts.get(name);
115
- if (count == null) return;
116
- if (count <= 1) {
117
- this.toolRefCounts.delete(name);
118
- this.tools.delete(name);
119
+ const oldest = this.toolRegistrations.get(name)?.[0];
120
+ if (oldest) this.removeToolRegistration(oldest);
121
+ }
122
+ removeToolRegistration(def) {
123
+ const registrations = this.toolRegistrations.get(def.name);
124
+ const index = registrations?.lastIndexOf(def) ?? -1;
125
+ if (!registrations || index === -1) return;
126
+ registrations.splice(index, 1);
127
+ const newest = registrations[registrations.length - 1];
128
+ if (!newest) {
129
+ this.toolRegistrations.delete(def.name);
130
+ this.tools.delete(def.name);
131
+ this.notify();
132
+ } else if (this.tools.get(def.name) !== newest) {
133
+ this.tools.set(def.name, newest);
119
134
  this.notify();
120
- } else {
121
- this.toolRefCounts.set(name, count - 1);
122
135
  }
123
136
  }
124
137
  /**
@@ -208,7 +221,7 @@ var DynamicRegistry = class {
208
221
  if (this.tools.size === 0 && this.resources.size === 0) return;
209
222
  this.tools.clear();
210
223
  this.resources.clear();
211
- this.toolRefCounts.clear();
224
+ this.toolRegistrations.clear();
212
225
  this.resourceRefCounts.clear();
213
226
  this.notify();
214
227
  }
package/esm/ai/index.mjs CHANGED
@@ -59,7 +59,8 @@ function extractName(uri) {
59
59
  var DynamicRegistry = class {
60
60
  tools = /* @__PURE__ */ new Map();
61
61
  resources = /* @__PURE__ */ new Map();
62
- toolRefCounts = /* @__PURE__ */ new Map();
62
+ /** Every live registration of a tool name, oldest first; the newest one is the definition in use. */
63
+ toolRegistrations = /* @__PURE__ */ new Map();
63
64
  resourceRefCounts = /* @__PURE__ */ new Map();
64
65
  listeners = /* @__PURE__ */ new Set();
65
66
  resourceListeners = /* @__PURE__ */ new Set();
@@ -72,29 +73,41 @@ var DynamicRegistry = class {
72
73
  * subsequent registrations replace the definition (and notify, so
73
74
  * the server registration follows the new description, schema or
74
75
  * availability) but the tool is only removed when every registrant
75
- * has unregistered.
76
+ * has unregistered. When the registrant whose definition is in use
77
+ * unregisters, the newest remaining registration takes over, so a
78
+ * call never reaches an unmounted component's `execute`.
76
79
  */
77
80
  registerTool(def) {
78
- const existing = this.toolRefCounts.get(def.name) ?? 0;
79
- this.toolRefCounts.set(def.name, existing + 1);
81
+ const registrations = this.toolRegistrations.get(def.name) ?? [];
82
+ registrations.push(def);
83
+ this.toolRegistrations.set(def.name, registrations);
80
84
  this.tools.set(def.name, def);
81
85
  this.notify();
82
86
  let called = false;
83
87
  return () => {
84
88
  if (called) return;
85
89
  called = true;
86
- this.unregisterTool(def.name);
90
+ this.removeToolRegistration(def);
87
91
  };
88
92
  }
93
+ /** Drop the oldest registration of `name` (registrants should call the function `registerTool` returned). */
89
94
  unregisterTool(name) {
90
- const count = this.toolRefCounts.get(name);
91
- if (count == null) return;
92
- if (count <= 1) {
93
- this.toolRefCounts.delete(name);
94
- this.tools.delete(name);
95
+ const oldest = this.toolRegistrations.get(name)?.[0];
96
+ if (oldest) this.removeToolRegistration(oldest);
97
+ }
98
+ removeToolRegistration(def) {
99
+ const registrations = this.toolRegistrations.get(def.name);
100
+ const index = registrations?.lastIndexOf(def) ?? -1;
101
+ if (!registrations || index === -1) return;
102
+ registrations.splice(index, 1);
103
+ const newest = registrations[registrations.length - 1];
104
+ if (!newest) {
105
+ this.toolRegistrations.delete(def.name);
106
+ this.tools.delete(def.name);
107
+ this.notify();
108
+ } else if (this.tools.get(def.name) !== newest) {
109
+ this.tools.set(def.name, newest);
95
110
  this.notify();
96
- } else {
97
- this.toolRefCounts.set(name, count - 1);
98
111
  }
99
112
  }
100
113
  /**
@@ -184,7 +197,7 @@ var DynamicRegistry = class {
184
197
  if (this.tools.size === 0 && this.resources.size === 0) return;
185
198
  this.tools.clear();
186
199
  this.resources.clear();
187
- this.toolRefCounts.clear();
200
+ this.toolRegistrations.clear();
188
201
  this.resourceRefCounts.clear();
189
202
  this.notify();
190
203
  }
package/esm/api/index.mjs CHANGED
@@ -55,7 +55,8 @@ function extractName(uri) {
55
55
  var DynamicRegistry = class {
56
56
  tools = /* @__PURE__ */ new Map();
57
57
  resources = /* @__PURE__ */ new Map();
58
- toolRefCounts = /* @__PURE__ */ new Map();
58
+ /** Every live registration of a tool name, oldest first; the newest one is the definition in use. */
59
+ toolRegistrations = /* @__PURE__ */ new Map();
59
60
  resourceRefCounts = /* @__PURE__ */ new Map();
60
61
  listeners = /* @__PURE__ */ new Set();
61
62
  resourceListeners = /* @__PURE__ */ new Set();
@@ -68,29 +69,41 @@ var DynamicRegistry = class {
68
69
  * subsequent registrations replace the definition (and notify, so
69
70
  * the server registration follows the new description, schema or
70
71
  * availability) but the tool is only removed when every registrant
71
- * has unregistered.
72
+ * has unregistered. When the registrant whose definition is in use
73
+ * unregisters, the newest remaining registration takes over, so a
74
+ * call never reaches an unmounted component's `execute`.
72
75
  */
73
76
  registerTool(def) {
74
- const existing = this.toolRefCounts.get(def.name) ?? 0;
75
- this.toolRefCounts.set(def.name, existing + 1);
77
+ const registrations = this.toolRegistrations.get(def.name) ?? [];
78
+ registrations.push(def);
79
+ this.toolRegistrations.set(def.name, registrations);
76
80
  this.tools.set(def.name, def);
77
81
  this.notify();
78
82
  let called = false;
79
83
  return () => {
80
84
  if (called) return;
81
85
  called = true;
82
- this.unregisterTool(def.name);
86
+ this.removeToolRegistration(def);
83
87
  };
84
88
  }
89
+ /** Drop the oldest registration of `name` (registrants should call the function `registerTool` returned). */
85
90
  unregisterTool(name) {
86
- const count = this.toolRefCounts.get(name);
87
- if (count == null) return;
88
- if (count <= 1) {
89
- this.toolRefCounts.delete(name);
90
- this.tools.delete(name);
91
+ const oldest = this.toolRegistrations.get(name)?.[0];
92
+ if (oldest) this.removeToolRegistration(oldest);
93
+ }
94
+ removeToolRegistration(def) {
95
+ const registrations = this.toolRegistrations.get(def.name);
96
+ const index = registrations?.lastIndexOf(def) ?? -1;
97
+ if (!registrations || index === -1) return;
98
+ registrations.splice(index, 1);
99
+ const newest = registrations[registrations.length - 1];
100
+ if (!newest) {
101
+ this.toolRegistrations.delete(def.name);
102
+ this.tools.delete(def.name);
103
+ this.notify();
104
+ } else if (this.tools.get(def.name) !== newest) {
105
+ this.tools.set(def.name, newest);
91
106
  this.notify();
92
- } else {
93
- this.toolRefCounts.set(name, count - 1);
94
107
  }
95
108
  }
96
109
  /**
@@ -180,7 +193,7 @@ var DynamicRegistry = class {
180
193
  if (this.tools.size === 0 && this.resources.size === 0) return;
181
194
  this.tools.clear();
182
195
  this.resources.clear();
183
- this.toolRefCounts.clear();
196
+ this.toolRegistrations.clear();
184
197
  this.resourceRefCounts.clear();
185
198
  this.notify();
186
199
  }
package/esm/index.mjs CHANGED
@@ -179,7 +179,8 @@ var serverRegistry = getOrCreateRegistry();
179
179
  var DynamicRegistry = class {
180
180
  tools = /* @__PURE__ */ new Map();
181
181
  resources = /* @__PURE__ */ new Map();
182
- toolRefCounts = /* @__PURE__ */ new Map();
182
+ /** Every live registration of a tool name, oldest first; the newest one is the definition in use. */
183
+ toolRegistrations = /* @__PURE__ */ new Map();
183
184
  resourceRefCounts = /* @__PURE__ */ new Map();
184
185
  listeners = /* @__PURE__ */ new Set();
185
186
  resourceListeners = /* @__PURE__ */ new Set();
@@ -192,29 +193,41 @@ var DynamicRegistry = class {
192
193
  * subsequent registrations replace the definition (and notify, so
193
194
  * the server registration follows the new description, schema or
194
195
  * availability) but the tool is only removed when every registrant
195
- * has unregistered.
196
+ * has unregistered. When the registrant whose definition is in use
197
+ * unregisters, the newest remaining registration takes over, so a
198
+ * call never reaches an unmounted component's `execute`.
196
199
  */
197
200
  registerTool(def) {
198
- const existing = this.toolRefCounts.get(def.name) ?? 0;
199
- this.toolRefCounts.set(def.name, existing + 1);
201
+ const registrations = this.toolRegistrations.get(def.name) ?? [];
202
+ registrations.push(def);
203
+ this.toolRegistrations.set(def.name, registrations);
200
204
  this.tools.set(def.name, def);
201
205
  this.notify();
202
206
  let called = false;
203
207
  return () => {
204
208
  if (called) return;
205
209
  called = true;
206
- this.unregisterTool(def.name);
210
+ this.removeToolRegistration(def);
207
211
  };
208
212
  }
213
+ /** Drop the oldest registration of `name` (registrants should call the function `registerTool` returned). */
209
214
  unregisterTool(name) {
210
- const count = this.toolRefCounts.get(name);
211
- if (count == null) return;
212
- if (count <= 1) {
213
- this.toolRefCounts.delete(name);
214
- this.tools.delete(name);
215
+ const oldest = this.toolRegistrations.get(name)?.[0];
216
+ if (oldest) this.removeToolRegistration(oldest);
217
+ }
218
+ removeToolRegistration(def) {
219
+ const registrations = this.toolRegistrations.get(def.name);
220
+ const index = registrations?.lastIndexOf(def) ?? -1;
221
+ if (!registrations || index === -1) return;
222
+ registrations.splice(index, 1);
223
+ const newest = registrations[registrations.length - 1];
224
+ if (!newest) {
225
+ this.toolRegistrations.delete(def.name);
226
+ this.tools.delete(def.name);
227
+ this.notify();
228
+ } else if (this.tools.get(def.name) !== newest) {
229
+ this.tools.set(def.name, newest);
215
230
  this.notify();
216
- } else {
217
- this.toolRefCounts.set(name, count - 1);
218
231
  }
219
232
  }
220
233
  /**
@@ -304,7 +317,7 @@ var DynamicRegistry = class {
304
317
  if (this.tools.size === 0 && this.resources.size === 0) return;
305
318
  this.tools.clear();
306
319
  this.resources.clear();
307
- this.toolRefCounts.clear();
320
+ this.toolRegistrations.clear();
308
321
  this.resourceRefCounts.clear();
309
322
  this.notify();
310
323
  }
@@ -316,6 +329,109 @@ var DynamicRegistry = class {
316
329
  }
317
330
  };
318
331
 
332
+ // libs/react/src/registry/bindDynamicTools.ts
333
+ function fingerprintOf(def) {
334
+ try {
335
+ return JSON.stringify([
336
+ def.description,
337
+ def.inputSchema,
338
+ def.annotations ?? null,
339
+ def.availableWhen ?? null,
340
+ def.app ?? null
341
+ ]);
342
+ } catch {
343
+ return `unserializable:${def.name}`;
344
+ }
345
+ }
346
+ function unavailable(name) {
347
+ return { isError: true, content: [{ type: "text", text: `Tool "${name}" is no longer available` }] };
348
+ }
349
+ function bindDynamicTools(registry, server, options = {}) {
350
+ const report = (error, toolName) => {
351
+ const err = error instanceof Error ? error : new Error(String(error));
352
+ if (options.onError) options.onError(err, toolName);
353
+ else console.warn(`[frontmcp] dynamic tool "${toolName}" was not registered: ${err.message}`);
354
+ };
355
+ if (typeof server.registerTool !== "function") {
356
+ return () => void 0;
357
+ }
358
+ const mirrored = /* @__PURE__ */ new Map();
359
+ const refused = /* @__PURE__ */ new Map();
360
+ let disposed = false;
361
+ let scheduled = false;
362
+ let reconciling = Promise.resolve();
363
+ const register = async (def, fingerprint) => {
364
+ const { name } = def;
365
+ const definition = {
366
+ name,
367
+ description: def.description,
368
+ inputSchema: def.inputSchema,
369
+ annotations: def.annotations,
370
+ availableWhen: def.availableWhen,
371
+ app: def.app ?? options.app,
372
+ // The registry holds the latest execute (components swap closures without re-registering)
373
+ execute: (args) => {
374
+ const current = registry.findTool(name);
375
+ return current ? current.execute(args) : unavailable(name);
376
+ }
377
+ };
378
+ try {
379
+ const unregister = await server.registerTool(definition);
380
+ if (disposed) {
381
+ unregister();
382
+ return;
383
+ }
384
+ mirrored.set(name, { fingerprint, unregister });
385
+ refused.delete(name);
386
+ } catch (error) {
387
+ refused.set(name, def);
388
+ report(error, name);
389
+ }
390
+ };
391
+ const reconcile = async () => {
392
+ if (disposed) return;
393
+ const wanted = new Map(registry.getTools().map((def) => [def.name, def]));
394
+ for (const [name, entry] of mirrored) {
395
+ const def = wanted.get(name);
396
+ if (!def || fingerprintOf(def) !== entry.fingerprint) {
397
+ entry.unregister();
398
+ mirrored.delete(name);
399
+ }
400
+ }
401
+ for (const name of refused.keys()) {
402
+ if (!wanted.has(name)) refused.delete(name);
403
+ }
404
+ const pending = [];
405
+ for (const [name, def] of wanted) {
406
+ if (mirrored.has(name)) continue;
407
+ if (refused.get(name) === def) continue;
408
+ pending.push(register(def, fingerprintOf(def)));
409
+ }
410
+ await Promise.all(pending);
411
+ };
412
+ const schedule = () => {
413
+ for (const name of refused.keys()) {
414
+ if (!registry.hasTool(name)) refused.delete(name);
415
+ }
416
+ if (scheduled) return;
417
+ scheduled = true;
418
+ queueMicrotask(() => {
419
+ scheduled = false;
420
+ reconciling = reconciling.then(reconcile, reconcile);
421
+ });
422
+ };
423
+ const unsubscribe = registry.subscribe(schedule);
424
+ schedule();
425
+ return () => {
426
+ if (disposed) return;
427
+ disposed = true;
428
+ unsubscribe();
429
+ for (const entry of mirrored.values()) entry.unregister();
430
+ mirrored.clear();
431
+ refused.clear();
432
+ };
433
+ }
434
+
319
435
  // libs/react/src/registry/createWrappedServer.ts
320
436
  function patchClientWithDynamic(client, dynamicRegistry) {
321
437
  if (typeof client.readResource === "function") {
@@ -506,109 +622,6 @@ var FrontMcpContext = getOrCreateContext();
506
622
  // libs/react/src/provider/FrontMcpProvider.tsx
507
623
  import React, { useCallback, useEffect as useEffect3, useMemo, useRef } from "react";
508
624
 
509
- // libs/react/src/registry/bindDynamicTools.ts
510
- function fingerprintOf(def) {
511
- try {
512
- return JSON.stringify([
513
- def.description,
514
- def.inputSchema,
515
- def.annotations ?? null,
516
- def.availableWhen ?? null,
517
- def.app ?? null
518
- ]);
519
- } catch {
520
- return `unserializable:${def.name}`;
521
- }
522
- }
523
- function unavailable(name) {
524
- return { isError: true, content: [{ type: "text", text: `Tool "${name}" is no longer available` }] };
525
- }
526
- function bindDynamicTools(registry, server, options = {}) {
527
- const report = (error, toolName) => {
528
- const err = error instanceof Error ? error : new Error(String(error));
529
- if (options.onError) options.onError(err, toolName);
530
- else console.warn(`[frontmcp] dynamic tool "${toolName}" was not registered: ${err.message}`);
531
- };
532
- if (typeof server.registerTool !== "function") {
533
- return () => void 0;
534
- }
535
- const mirrored = /* @__PURE__ */ new Map();
536
- const refused = /* @__PURE__ */ new Map();
537
- let disposed = false;
538
- let scheduled = false;
539
- let reconciling = Promise.resolve();
540
- const register = async (def, fingerprint) => {
541
- const { name } = def;
542
- const definition = {
543
- name,
544
- description: def.description,
545
- inputSchema: def.inputSchema,
546
- annotations: def.annotations,
547
- availableWhen: def.availableWhen,
548
- app: def.app ?? options.app,
549
- // The registry holds the latest execute (components swap closures without re-registering)
550
- execute: (args) => {
551
- const current = registry.findTool(name);
552
- return current ? current.execute(args) : unavailable(name);
553
- }
554
- };
555
- try {
556
- const unregister = await server.registerTool(definition);
557
- if (disposed) {
558
- unregister();
559
- return;
560
- }
561
- mirrored.set(name, { fingerprint, unregister });
562
- refused.delete(name);
563
- } catch (error) {
564
- refused.set(name, def);
565
- report(error, name);
566
- }
567
- };
568
- const reconcile = async () => {
569
- if (disposed) return;
570
- const wanted = new Map(registry.getTools().map((def) => [def.name, def]));
571
- for (const [name, entry] of mirrored) {
572
- const def = wanted.get(name);
573
- if (!def || fingerprintOf(def) !== entry.fingerprint) {
574
- entry.unregister();
575
- mirrored.delete(name);
576
- }
577
- }
578
- for (const name of refused.keys()) {
579
- if (!wanted.has(name)) refused.delete(name);
580
- }
581
- const pending = [];
582
- for (const [name, def] of wanted) {
583
- if (mirrored.has(name)) continue;
584
- if (refused.get(name) === def) continue;
585
- pending.push(register(def, fingerprintOf(def)));
586
- }
587
- await Promise.all(pending);
588
- };
589
- const schedule = () => {
590
- for (const name of refused.keys()) {
591
- if (!registry.hasTool(name)) refused.delete(name);
592
- }
593
- if (scheduled) return;
594
- scheduled = true;
595
- queueMicrotask(() => {
596
- scheduled = false;
597
- reconciling = reconciling.then(reconcile, reconcile);
598
- });
599
- };
600
- const unsubscribe = registry.subscribe(schedule);
601
- schedule();
602
- return () => {
603
- if (disposed) return;
604
- disposed = true;
605
- unsubscribe();
606
- for (const entry of mirrored.values()) entry.unregister();
607
- mirrored.clear();
608
- refused.clear();
609
- };
610
- }
611
-
612
625
  // libs/react/src/state/useStoreRegistration.ts
613
626
  import { useEffect } from "react";
614
627
  var VALID_NAME_RE = /^[a-zA-Z0-9_-]+$/;
@@ -2000,6 +2013,7 @@ export {
2000
2013
  Tool,
2001
2014
  ToolContext,
2002
2015
  ToolForm,
2016
+ bindDynamicTools,
2003
2017
  clearCreateCache,
2004
2018
  connect,
2005
2019
  connectClaude,
package/esm/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frontmcp/react",
3
- "version": "1.9.1-rc.3",
3
+ "version": "1.9.2",
4
4
  "description": "React hooks, components, and AI SDK integration for FrontMCP",
5
5
  "author": "AgentFront <info@agentfront.dev>",
6
6
  "homepage": "https://docs.agentfront.dev",
@@ -86,9 +86,9 @@
86
86
  "node": ">=24.0.0"
87
87
  },
88
88
  "peerDependencies": {
89
- "@frontmcp/lazy-zod": "1.9.1-rc.3",
90
- "@frontmcp/sdk": "1.9.1-rc.3",
91
- "@frontmcp/utils": "1.9.1-rc.3",
89
+ "@frontmcp/lazy-zod": "1.9.2",
90
+ "@frontmcp/sdk": "1.9.2",
91
+ "@frontmcp/utils": "1.9.2",
92
92
  "react": "^18.0.0 || ^19.0.0",
93
93
  "react-dom": "^18.0.0 || ^19.0.0",
94
94
  "react-router-dom": "^7.0.0"
@@ -102,8 +102,8 @@
102
102
  }
103
103
  },
104
104
  "dependencies": {
105
- "@frontmcp/sdk": "1.9.1-rc.3",
106
- "@frontmcp/utils": "1.9.1-rc.3"
105
+ "@frontmcp/sdk": "1.9.2",
106
+ "@frontmcp/utils": "1.9.2"
107
107
  },
108
108
  "devDependencies": {
109
109
  "typescript": "^5.9.3"
@@ -55,7 +55,8 @@ function extractName(uri) {
55
55
  var DynamicRegistry = class {
56
56
  tools = /* @__PURE__ */ new Map();
57
57
  resources = /* @__PURE__ */ new Map();
58
- toolRefCounts = /* @__PURE__ */ new Map();
58
+ /** Every live registration of a tool name, oldest first; the newest one is the definition in use. */
59
+ toolRegistrations = /* @__PURE__ */ new Map();
59
60
  resourceRefCounts = /* @__PURE__ */ new Map();
60
61
  listeners = /* @__PURE__ */ new Set();
61
62
  resourceListeners = /* @__PURE__ */ new Set();
@@ -68,29 +69,41 @@ var DynamicRegistry = class {
68
69
  * subsequent registrations replace the definition (and notify, so
69
70
  * the server registration follows the new description, schema or
70
71
  * availability) but the tool is only removed when every registrant
71
- * has unregistered.
72
+ * has unregistered. When the registrant whose definition is in use
73
+ * unregisters, the newest remaining registration takes over, so a
74
+ * call never reaches an unmounted component's `execute`.
72
75
  */
73
76
  registerTool(def) {
74
- const existing = this.toolRefCounts.get(def.name) ?? 0;
75
- this.toolRefCounts.set(def.name, existing + 1);
77
+ const registrations = this.toolRegistrations.get(def.name) ?? [];
78
+ registrations.push(def);
79
+ this.toolRegistrations.set(def.name, registrations);
76
80
  this.tools.set(def.name, def);
77
81
  this.notify();
78
82
  let called = false;
79
83
  return () => {
80
84
  if (called) return;
81
85
  called = true;
82
- this.unregisterTool(def.name);
86
+ this.removeToolRegistration(def);
83
87
  };
84
88
  }
89
+ /** Drop the oldest registration of `name` (registrants should call the function `registerTool` returned). */
85
90
  unregisterTool(name) {
86
- const count = this.toolRefCounts.get(name);
87
- if (count == null) return;
88
- if (count <= 1) {
89
- this.toolRefCounts.delete(name);
90
- this.tools.delete(name);
91
+ const oldest = this.toolRegistrations.get(name)?.[0];
92
+ if (oldest) this.removeToolRegistration(oldest);
93
+ }
94
+ removeToolRegistration(def) {
95
+ const registrations = this.toolRegistrations.get(def.name);
96
+ const index = registrations?.lastIndexOf(def) ?? -1;
97
+ if (!registrations || index === -1) return;
98
+ registrations.splice(index, 1);
99
+ const newest = registrations[registrations.length - 1];
100
+ if (!newest) {
101
+ this.toolRegistrations.delete(def.name);
102
+ this.tools.delete(def.name);
103
+ this.notify();
104
+ } else if (this.tools.get(def.name) !== newest) {
105
+ this.tools.set(def.name, newest);
91
106
  this.notify();
92
- } else {
93
- this.toolRefCounts.set(name, count - 1);
94
107
  }
95
108
  }
96
109
  /**
@@ -180,7 +193,7 @@ var DynamicRegistry = class {
180
193
  if (this.tools.size === 0 && this.resources.size === 0) return;
181
194
  this.tools.clear();
182
195
  this.resources.clear();
183
- this.toolRefCounts.clear();
196
+ this.toolRegistrations.clear();
184
197
  this.resourceRefCounts.clear();
185
198
  this.notify();
186
199
  }
package/index.d.ts CHANGED
@@ -20,8 +20,8 @@ export { ToolContext, ResourceContext, PromptContext, ExecutionContextBase } fro
20
20
  export type { GetPromptResult, ReadResourceResult, CallToolResult, ListToolsResult, ListResourcesResult, ListResourceTemplatesResult, ListPromptsResult, TextContent, ImageContent, PromptMessage, } from '@frontmcp/sdk';
21
21
  export type { FrontMcpContextValue, ResolvedServer, FrontMcpStatus, ToolInfo, ResourceInfo, ResourceTemplateInfo, PromptInfo, ToolState, ResourceState, PromptState, UseCallToolOptions, UseCallToolReturn, ComponentNode, FieldRenderProps, DynamicToolDef, DynamicResourceDef, StoreAdapter, McpColumnDef, } from './types';
22
22
  export { ServerRegistry, serverRegistry } from './registry';
23
- export { DynamicRegistry } from './registry';
24
- export type { ServerEntry } from './registry';
23
+ export { DynamicRegistry, bindDynamicTools } from './registry';
24
+ export type { ServerEntry, BindDynamicToolsOptions } from './registry';
25
25
  export { FrontMcpContext, FrontMcpProvider } from './provider';
26
26
  export type { FrontMcpProviderProps } from './provider';
27
27
  export { useFrontMcp, useServer, useResolvedServer, useCallTool, useReadResource, useGetPrompt, useListTools, useListResources, useListPrompts, useStoreResource, useDynamicTool, useDynamicResource, useComponentTree, } from './hooks';
package/index.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAOH,OAAO,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AACzD,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACzG,YAA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AAM,YAAY,CAAC;AAGvE,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAC/D,YAAY,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAGxD,OAAO,EACL,WAAW,EACX,SAAS,EACT,iBAAiB,EACjB,WAAW,EACX,eAAe,EACf,YAAY,EACZ,YAAY,EACZ,gBAAgB,EAChB,cAAc,EACd,gBAAgB,EAChB,cAAc,EACd,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,SAAS,CAAC;AACjB,YAAY,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,sBAAsB,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAC;AACvH,YAAY,EACV,qBAAqB,EACrB,2BAA2B,EAC3B,+BAA+B,EAC/B,yBAAyB,EACzB,uBAAuB,GACxB,MAAM,SAAS,CAAC;AAGjB,OAAO,EACL,iBAAiB,EACjB,eAAe,EACf,WAAW,EACX,iBAAiB,EACjB,QAAQ,EACR,UAAU,EACV,cAAc,EACd,aAAa,EACb,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,OAAO,GACR,MAAM,cAAc,CAAC;AACtB,YAAY,EACV,sBAAsB,EACtB,oBAAoB,EACpB,iBAAiB,EACjB,aAAa,EACb,eAAe,EACf,mBAAmB,EACnB,eAAe,EACf,kBAAkB,EAClB,iBAAiB,EACjB,gBAAgB,EAChB,sBAAsB,EACtB,mBAAmB,EACnB,oBAAoB,GACrB,MAAM,cAAc,CAAC;AAGtB,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAGxE,YAAY,EAAE,UAAU,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC"}
package/index.js CHANGED
@@ -61,6 +61,7 @@ __export(index_exports, {
61
61
  Tool: () => import_sdk3.Tool,
62
62
  ToolContext: () => import_sdk5.ToolContext,
63
63
  ToolForm: () => ToolForm,
64
+ bindDynamicTools: () => bindDynamicTools,
64
65
  clearCreateCache: () => import_sdk.clearCreateCache,
65
66
  connect: () => import_sdk2.connect,
66
67
  connectClaude: () => import_sdk2.connectClaude,
@@ -256,7 +257,8 @@ var serverRegistry = getOrCreateRegistry();
256
257
  var DynamicRegistry = class {
257
258
  tools = /* @__PURE__ */ new Map();
258
259
  resources = /* @__PURE__ */ new Map();
259
- toolRefCounts = /* @__PURE__ */ new Map();
260
+ /** Every live registration of a tool name, oldest first; the newest one is the definition in use. */
261
+ toolRegistrations = /* @__PURE__ */ new Map();
260
262
  resourceRefCounts = /* @__PURE__ */ new Map();
261
263
  listeners = /* @__PURE__ */ new Set();
262
264
  resourceListeners = /* @__PURE__ */ new Set();
@@ -269,29 +271,41 @@ var DynamicRegistry = class {
269
271
  * subsequent registrations replace the definition (and notify, so
270
272
  * the server registration follows the new description, schema or
271
273
  * availability) but the tool is only removed when every registrant
272
- * has unregistered.
274
+ * has unregistered. When the registrant whose definition is in use
275
+ * unregisters, the newest remaining registration takes over, so a
276
+ * call never reaches an unmounted component's `execute`.
273
277
  */
274
278
  registerTool(def) {
275
- const existing = this.toolRefCounts.get(def.name) ?? 0;
276
- this.toolRefCounts.set(def.name, existing + 1);
279
+ const registrations = this.toolRegistrations.get(def.name) ?? [];
280
+ registrations.push(def);
281
+ this.toolRegistrations.set(def.name, registrations);
277
282
  this.tools.set(def.name, def);
278
283
  this.notify();
279
284
  let called = false;
280
285
  return () => {
281
286
  if (called) return;
282
287
  called = true;
283
- this.unregisterTool(def.name);
288
+ this.removeToolRegistration(def);
284
289
  };
285
290
  }
291
+ /** Drop the oldest registration of `name` (registrants should call the function `registerTool` returned). */
286
292
  unregisterTool(name) {
287
- const count = this.toolRefCounts.get(name);
288
- if (count == null) return;
289
- if (count <= 1) {
290
- this.toolRefCounts.delete(name);
291
- this.tools.delete(name);
293
+ const oldest = this.toolRegistrations.get(name)?.[0];
294
+ if (oldest) this.removeToolRegistration(oldest);
295
+ }
296
+ removeToolRegistration(def) {
297
+ const registrations = this.toolRegistrations.get(def.name);
298
+ const index = registrations?.lastIndexOf(def) ?? -1;
299
+ if (!registrations || index === -1) return;
300
+ registrations.splice(index, 1);
301
+ const newest = registrations[registrations.length - 1];
302
+ if (!newest) {
303
+ this.toolRegistrations.delete(def.name);
304
+ this.tools.delete(def.name);
305
+ this.notify();
306
+ } else if (this.tools.get(def.name) !== newest) {
307
+ this.tools.set(def.name, newest);
292
308
  this.notify();
293
- } else {
294
- this.toolRefCounts.set(name, count - 1);
295
309
  }
296
310
  }
297
311
  /**
@@ -381,7 +395,7 @@ var DynamicRegistry = class {
381
395
  if (this.tools.size === 0 && this.resources.size === 0) return;
382
396
  this.tools.clear();
383
397
  this.resources.clear();
384
- this.toolRefCounts.clear();
398
+ this.toolRegistrations.clear();
385
399
  this.resourceRefCounts.clear();
386
400
  this.notify();
387
401
  }
@@ -393,6 +407,109 @@ var DynamicRegistry = class {
393
407
  }
394
408
  };
395
409
 
410
+ // libs/react/src/registry/bindDynamicTools.ts
411
+ function fingerprintOf(def) {
412
+ try {
413
+ return JSON.stringify([
414
+ def.description,
415
+ def.inputSchema,
416
+ def.annotations ?? null,
417
+ def.availableWhen ?? null,
418
+ def.app ?? null
419
+ ]);
420
+ } catch {
421
+ return `unserializable:${def.name}`;
422
+ }
423
+ }
424
+ function unavailable(name) {
425
+ return { isError: true, content: [{ type: "text", text: `Tool "${name}" is no longer available` }] };
426
+ }
427
+ function bindDynamicTools(registry, server, options = {}) {
428
+ const report = (error, toolName) => {
429
+ const err = error instanceof Error ? error : new Error(String(error));
430
+ if (options.onError) options.onError(err, toolName);
431
+ else console.warn(`[frontmcp] dynamic tool "${toolName}" was not registered: ${err.message}`);
432
+ };
433
+ if (typeof server.registerTool !== "function") {
434
+ return () => void 0;
435
+ }
436
+ const mirrored = /* @__PURE__ */ new Map();
437
+ const refused = /* @__PURE__ */ new Map();
438
+ let disposed = false;
439
+ let scheduled = false;
440
+ let reconciling = Promise.resolve();
441
+ const register = async (def, fingerprint) => {
442
+ const { name } = def;
443
+ const definition = {
444
+ name,
445
+ description: def.description,
446
+ inputSchema: def.inputSchema,
447
+ annotations: def.annotations,
448
+ availableWhen: def.availableWhen,
449
+ app: def.app ?? options.app,
450
+ // The registry holds the latest execute (components swap closures without re-registering)
451
+ execute: (args) => {
452
+ const current = registry.findTool(name);
453
+ return current ? current.execute(args) : unavailable(name);
454
+ }
455
+ };
456
+ try {
457
+ const unregister = await server.registerTool(definition);
458
+ if (disposed) {
459
+ unregister();
460
+ return;
461
+ }
462
+ mirrored.set(name, { fingerprint, unregister });
463
+ refused.delete(name);
464
+ } catch (error) {
465
+ refused.set(name, def);
466
+ report(error, name);
467
+ }
468
+ };
469
+ const reconcile = async () => {
470
+ if (disposed) return;
471
+ const wanted = new Map(registry.getTools().map((def) => [def.name, def]));
472
+ for (const [name, entry] of mirrored) {
473
+ const def = wanted.get(name);
474
+ if (!def || fingerprintOf(def) !== entry.fingerprint) {
475
+ entry.unregister();
476
+ mirrored.delete(name);
477
+ }
478
+ }
479
+ for (const name of refused.keys()) {
480
+ if (!wanted.has(name)) refused.delete(name);
481
+ }
482
+ const pending = [];
483
+ for (const [name, def] of wanted) {
484
+ if (mirrored.has(name)) continue;
485
+ if (refused.get(name) === def) continue;
486
+ pending.push(register(def, fingerprintOf(def)));
487
+ }
488
+ await Promise.all(pending);
489
+ };
490
+ const schedule = () => {
491
+ for (const name of refused.keys()) {
492
+ if (!registry.hasTool(name)) refused.delete(name);
493
+ }
494
+ if (scheduled) return;
495
+ scheduled = true;
496
+ queueMicrotask(() => {
497
+ scheduled = false;
498
+ reconciling = reconciling.then(reconcile, reconcile);
499
+ });
500
+ };
501
+ const unsubscribe = registry.subscribe(schedule);
502
+ schedule();
503
+ return () => {
504
+ if (disposed) return;
505
+ disposed = true;
506
+ unsubscribe();
507
+ for (const entry of mirrored.values()) entry.unregister();
508
+ mirrored.clear();
509
+ refused.clear();
510
+ };
511
+ }
512
+
396
513
  // libs/react/src/registry/createWrappedServer.ts
397
514
  function patchClientWithDynamic(client, dynamicRegistry) {
398
515
  if (typeof client.readResource === "function") {
@@ -583,109 +700,6 @@ var FrontMcpContext = getOrCreateContext();
583
700
  // libs/react/src/provider/FrontMcpProvider.tsx
584
701
  var import_react4 = __toESM(require("react"));
585
702
 
586
- // libs/react/src/registry/bindDynamicTools.ts
587
- function fingerprintOf(def) {
588
- try {
589
- return JSON.stringify([
590
- def.description,
591
- def.inputSchema,
592
- def.annotations ?? null,
593
- def.availableWhen ?? null,
594
- def.app ?? null
595
- ]);
596
- } catch {
597
- return `unserializable:${def.name}`;
598
- }
599
- }
600
- function unavailable(name) {
601
- return { isError: true, content: [{ type: "text", text: `Tool "${name}" is no longer available` }] };
602
- }
603
- function bindDynamicTools(registry, server, options = {}) {
604
- const report = (error, toolName) => {
605
- const err = error instanceof Error ? error : new Error(String(error));
606
- if (options.onError) options.onError(err, toolName);
607
- else console.warn(`[frontmcp] dynamic tool "${toolName}" was not registered: ${err.message}`);
608
- };
609
- if (typeof server.registerTool !== "function") {
610
- return () => void 0;
611
- }
612
- const mirrored = /* @__PURE__ */ new Map();
613
- const refused = /* @__PURE__ */ new Map();
614
- let disposed = false;
615
- let scheduled = false;
616
- let reconciling = Promise.resolve();
617
- const register = async (def, fingerprint) => {
618
- const { name } = def;
619
- const definition = {
620
- name,
621
- description: def.description,
622
- inputSchema: def.inputSchema,
623
- annotations: def.annotations,
624
- availableWhen: def.availableWhen,
625
- app: def.app ?? options.app,
626
- // The registry holds the latest execute (components swap closures without re-registering)
627
- execute: (args) => {
628
- const current = registry.findTool(name);
629
- return current ? current.execute(args) : unavailable(name);
630
- }
631
- };
632
- try {
633
- const unregister = await server.registerTool(definition);
634
- if (disposed) {
635
- unregister();
636
- return;
637
- }
638
- mirrored.set(name, { fingerprint, unregister });
639
- refused.delete(name);
640
- } catch (error) {
641
- refused.set(name, def);
642
- report(error, name);
643
- }
644
- };
645
- const reconcile = async () => {
646
- if (disposed) return;
647
- const wanted = new Map(registry.getTools().map((def) => [def.name, def]));
648
- for (const [name, entry] of mirrored) {
649
- const def = wanted.get(name);
650
- if (!def || fingerprintOf(def) !== entry.fingerprint) {
651
- entry.unregister();
652
- mirrored.delete(name);
653
- }
654
- }
655
- for (const name of refused.keys()) {
656
- if (!wanted.has(name)) refused.delete(name);
657
- }
658
- const pending = [];
659
- for (const [name, def] of wanted) {
660
- if (mirrored.has(name)) continue;
661
- if (refused.get(name) === def) continue;
662
- pending.push(register(def, fingerprintOf(def)));
663
- }
664
- await Promise.all(pending);
665
- };
666
- const schedule = () => {
667
- for (const name of refused.keys()) {
668
- if (!registry.hasTool(name)) refused.delete(name);
669
- }
670
- if (scheduled) return;
671
- scheduled = true;
672
- queueMicrotask(() => {
673
- scheduled = false;
674
- reconciling = reconciling.then(reconcile, reconcile);
675
- });
676
- };
677
- const unsubscribe = registry.subscribe(schedule);
678
- schedule();
679
- return () => {
680
- if (disposed) return;
681
- disposed = true;
682
- unsubscribe();
683
- for (const entry of mirrored.values()) entry.unregister();
684
- mirrored.clear();
685
- refused.clear();
686
- };
687
- }
688
-
689
703
  // libs/react/src/state/useStoreRegistration.ts
690
704
  var import_react2 = require("react");
691
705
  var VALID_NAME_RE = /^[a-zA-Z0-9_-]+$/;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frontmcp/react",
3
- "version": "1.9.1-rc.3",
3
+ "version": "1.9.2",
4
4
  "description": "React hooks, components, and AI SDK integration for FrontMCP",
5
5
  "author": "AgentFront <info@agentfront.dev>",
6
6
  "homepage": "https://docs.agentfront.dev",
@@ -86,9 +86,9 @@
86
86
  "node": ">=24.0.0"
87
87
  },
88
88
  "peerDependencies": {
89
- "@frontmcp/lazy-zod": "1.9.1-rc.3",
90
- "@frontmcp/sdk": "1.9.1-rc.3",
91
- "@frontmcp/utils": "1.9.1-rc.3",
89
+ "@frontmcp/lazy-zod": "1.9.2",
90
+ "@frontmcp/sdk": "1.9.2",
91
+ "@frontmcp/utils": "1.9.2",
92
92
  "react": "^18.0.0 || ^19.0.0",
93
93
  "react-dom": "^18.0.0 || ^19.0.0",
94
94
  "react-router-dom": "^7.0.0"
@@ -102,8 +102,8 @@
102
102
  }
103
103
  },
104
104
  "dependencies": {
105
- "@frontmcp/sdk": "1.9.1-rc.3",
106
- "@frontmcp/utils": "1.9.1-rc.3"
105
+ "@frontmcp/sdk": "1.9.2",
106
+ "@frontmcp/utils": "1.9.2"
107
107
  },
108
108
  "devDependencies": {
109
109
  "typescript": "^5.9.3"
@@ -15,7 +15,8 @@ type Listener = () => void;
15
15
  export declare class DynamicRegistry {
16
16
  private tools;
17
17
  private resources;
18
- private toolRefCounts;
18
+ /** Every live registration of a tool name, oldest first; the newest one is the definition in use. */
19
+ private toolRegistrations;
19
20
  private resourceRefCounts;
20
21
  private listeners;
21
22
  private resourceListeners;
@@ -28,10 +29,14 @@ export declare class DynamicRegistry {
28
29
  * subsequent registrations replace the definition (and notify, so
29
30
  * the server registration follows the new description, schema or
30
31
  * availability) but the tool is only removed when every registrant
31
- * has unregistered.
32
+ * has unregistered. When the registrant whose definition is in use
33
+ * unregisters, the newest remaining registration takes over, so a
34
+ * call never reaches an unmounted component's `execute`.
32
35
  */
33
36
  registerTool(def: DynamicToolDef): () => void;
37
+ /** Drop the oldest registration of `name` (registrants should call the function `registerTool` returned). */
34
38
  unregisterTool(name: string): void;
39
+ private removeToolRegistration;
35
40
  /**
36
41
  * Register a dynamic resource. Returns an unregister function
37
42
  * suitable for useEffect cleanup.
@@ -1 +1 @@
1
- {"version":3,"file":"DynamicRegistry.d.ts","sourceRoot":"","sources":["../../src/registry/DynamicRegistry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAEnE,KAAK,QAAQ,GAAG,MAAM,IAAI,CAAC;AAE3B,qBAAa,eAAe;IAC1B,OAAO,CAAC,KAAK,CAAqC;IAClD,OAAO,CAAC,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AAkB,GAAG,SAAS;IAIzD,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAI9B,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAIjC,SAAS,CAAC,QAAQ,EAAE,QAAQ,GAAG,MAAM,IAAI;IAOzC,UAAU,IAAI,MAAM;IAIpB,KAAK,IAAI,IAAI;IASb,OAAO,CAAC,MAAM;CAMf"}
@@ -1,5 +1,7 @@
1
1
  export { ServerRegistry, serverRegistry } from './ServerRegistry';
2
2
  export type { ServerEntry } from './ServerRegistry';
3
3
  export { DynamicRegistry } from './DynamicRegistry';
4
+ export { bindDynamicTools } from './bindDynamicTools';
5
+ export type { BindDynamicToolsOptions } from './bindDynamicTools';
4
6
  export { createWrappedServer } from './createWrappedServer';
5
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/registry/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClE,YAAY,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/registry/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClE,YAAY,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,YAAY,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAClE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC"}
package/state/index.js CHANGED
@@ -86,7 +86,8 @@ function extractName(uri) {
86
86
  var DynamicRegistry = class {
87
87
  tools = /* @__PURE__ */ new Map();
88
88
  resources = /* @__PURE__ */ new Map();
89
- toolRefCounts = /* @__PURE__ */ new Map();
89
+ /** Every live registration of a tool name, oldest first; the newest one is the definition in use. */
90
+ toolRegistrations = /* @__PURE__ */ new Map();
90
91
  resourceRefCounts = /* @__PURE__ */ new Map();
91
92
  listeners = /* @__PURE__ */ new Set();
92
93
  resourceListeners = /* @__PURE__ */ new Set();
@@ -99,29 +100,41 @@ var DynamicRegistry = class {
99
100
  * subsequent registrations replace the definition (and notify, so
100
101
  * the server registration follows the new description, schema or
101
102
  * availability) but the tool is only removed when every registrant
102
- * has unregistered.
103
+ * has unregistered. When the registrant whose definition is in use
104
+ * unregisters, the newest remaining registration takes over, so a
105
+ * call never reaches an unmounted component's `execute`.
103
106
  */
104
107
  registerTool(def) {
105
- const existing = this.toolRefCounts.get(def.name) ?? 0;
106
- this.toolRefCounts.set(def.name, existing + 1);
108
+ const registrations = this.toolRegistrations.get(def.name) ?? [];
109
+ registrations.push(def);
110
+ this.toolRegistrations.set(def.name, registrations);
107
111
  this.tools.set(def.name, def);
108
112
  this.notify();
109
113
  let called = false;
110
114
  return () => {
111
115
  if (called) return;
112
116
  called = true;
113
- this.unregisterTool(def.name);
117
+ this.removeToolRegistration(def);
114
118
  };
115
119
  }
120
+ /** Drop the oldest registration of `name` (registrants should call the function `registerTool` returned). */
116
121
  unregisterTool(name) {
117
- const count = this.toolRefCounts.get(name);
118
- if (count == null) return;
119
- if (count <= 1) {
120
- this.toolRefCounts.delete(name);
121
- this.tools.delete(name);
122
+ const oldest = this.toolRegistrations.get(name)?.[0];
123
+ if (oldest) this.removeToolRegistration(oldest);
124
+ }
125
+ removeToolRegistration(def) {
126
+ const registrations = this.toolRegistrations.get(def.name);
127
+ const index = registrations?.lastIndexOf(def) ?? -1;
128
+ if (!registrations || index === -1) return;
129
+ registrations.splice(index, 1);
130
+ const newest = registrations[registrations.length - 1];
131
+ if (!newest) {
132
+ this.toolRegistrations.delete(def.name);
133
+ this.tools.delete(def.name);
134
+ this.notify();
135
+ } else if (this.tools.get(def.name) !== newest) {
136
+ this.tools.set(def.name, newest);
122
137
  this.notify();
123
- } else {
124
- this.toolRefCounts.set(name, count - 1);
125
138
  }
126
139
  }
127
140
  /**
@@ -211,7 +224,7 @@ var DynamicRegistry = class {
211
224
  if (this.tools.size === 0 && this.resources.size === 0) return;
212
225
  this.tools.clear();
213
226
  this.resources.clear();
214
- this.toolRefCounts.clear();
227
+ this.toolRegistrations.clear();
215
228
  this.resourceRefCounts.clear();
216
229
  this.notify();
217
230
  }