@frontmcp/react 1.8.6 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +2 -0
  2. package/ai/index.js +90 -17
  3. package/api/api.types.d.ts +29 -0
  4. package/api/api.types.d.ts.map +1 -1
  5. package/api/index.d.ts +1 -1
  6. package/api/index.d.ts.map +1 -1
  7. package/api/index.js +136 -33
  8. package/api/parseOpenApiSpec.d.ts.map +1 -1
  9. package/api/useApiClient.d.ts +5 -0
  10. package/api/useApiClient.d.ts.map +1 -1
  11. package/components/ToolForm.d.ts +1 -1
  12. package/components/ToolForm.d.ts.map +1 -1
  13. package/esm/ai/index.mjs +93 -20
  14. package/esm/api/index.mjs +136 -33
  15. package/esm/index.mjs +298 -85
  16. package/esm/package.json +6 -6
  17. package/esm/router/index.mjs +22 -4
  18. package/esm/state/index.mjs +113 -98
  19. package/hooks/useDynamicTool.d.ts +19 -3
  20. package/hooks/useDynamicTool.d.ts.map +1 -1
  21. package/hooks/useServer.d.ts +2 -2
  22. package/hooks/useServer.d.ts.map +1 -1
  23. package/index.d.ts +1 -0
  24. package/index.d.ts.map +1 -1
  25. package/index.js +293 -80
  26. package/package.json +6 -6
  27. package/provider/FrontMcpContext.d.ts +2 -1
  28. package/provider/FrontMcpContext.d.ts.map +1 -1
  29. package/provider/FrontMcpProvider.d.ts +12 -6
  30. package/provider/FrontMcpProvider.d.ts.map +1 -1
  31. package/registry/DynamicRegistry.d.ts +12 -6
  32. package/registry/DynamicRegistry.d.ts.map +1 -1
  33. package/registry/ServerRegistry.d.ts +14 -3
  34. package/registry/ServerRegistry.d.ts.map +1 -1
  35. package/registry/bindDynamicTools.d.ts +29 -0
  36. package/registry/bindDynamicTools.d.ts.map +1 -0
  37. package/registry/createWrappedServer.d.ts +6 -5
  38. package/registry/createWrappedServer.d.ts.map +1 -1
  39. package/router/index.js +22 -4
  40. package/router/router.entries.d.ts +9 -5
  41. package/router/router.entries.d.ts.map +1 -1
  42. package/state/index.js +112 -97
  43. package/state/useReduxResource.d.ts.map +1 -1
  44. package/state/useStoreResource.d.ts +6 -0
  45. package/state/useStoreResource.d.ts.map +1 -1
  46. package/types.d.ts +10 -1
  47. package/types.d.ts.map +1 -1
package/index.js CHANGED
@@ -37,7 +37,7 @@ __export(index_exports, {
37
37
  ComponentRegistry: () => ComponentRegistry,
38
38
  DynamicRegistry: () => DynamicRegistry,
39
39
  DynamicRenderer: () => DynamicRenderer,
40
- ExecutionContextBase: () => import_sdk4.ExecutionContextBase,
40
+ ExecutionContextBase: () => import_sdk5.ExecutionContextBase,
41
41
  FrontMcp: () => import_sdk3.FrontMcp,
42
42
  FrontMcpAdapter: () => import_sdk3.FrontMcpAdapter,
43
43
  FrontMcpApp: () => import_sdk3.FrontMcpApp,
@@ -51,15 +51,15 @@ __export(index_exports, {
51
51
  OutputDisplay: () => OutputDisplay,
52
52
  Plugin: () => import_sdk3.Plugin,
53
53
  Prompt: () => import_sdk3.Prompt,
54
- PromptContext: () => import_sdk4.PromptContext,
54
+ PromptContext: () => import_sdk5.PromptContext,
55
55
  PromptForm: () => PromptForm,
56
56
  Resource: () => import_sdk3.Resource,
57
- ResourceContext: () => import_sdk4.ResourceContext,
57
+ ResourceContext: () => import_sdk5.ResourceContext,
58
58
  ResourceTemplate: () => import_sdk3.ResourceTemplate,
59
59
  ResourceViewer: () => ResourceViewer,
60
60
  ServerRegistry: () => ServerRegistry,
61
61
  Tool: () => import_sdk3.Tool,
62
- ToolContext: () => import_sdk4.ToolContext,
62
+ ToolContext: () => import_sdk5.ToolContext,
63
63
  ToolForm: () => ToolForm,
64
64
  clearCreateCache: () => import_sdk.clearCreateCache,
65
65
  connect: () => import_sdk2.connect,
@@ -96,20 +96,33 @@ __export(index_exports, {
96
96
  useResolvedServer: () => useResolvedServer,
97
97
  useServer: () => useServer,
98
98
  useStoreResource: () => useStoreResource,
99
- valtioStore: () => valtioStore
99
+ valtioStore: () => valtioStore,
100
+ z: () => import_sdk4.z
100
101
  });
101
102
  module.exports = __toCommonJS(index_exports);
102
103
  var import_sdk = require("@frontmcp/sdk");
103
104
  var import_sdk2 = require("@frontmcp/sdk");
104
105
  var import_sdk3 = require("@frontmcp/sdk");
105
106
  var import_sdk4 = require("@frontmcp/sdk");
107
+ var import_sdk5 = require("@frontmcp/sdk");
106
108
 
107
109
  // libs/react/src/registry/ServerRegistry.ts
110
+ function sameListing(a, b) {
111
+ if (a === b) return true;
112
+ if (a.length !== b.length) return false;
113
+ try {
114
+ return JSON.stringify(a) === JSON.stringify(b);
115
+ } catch {
116
+ return false;
117
+ }
118
+ }
108
119
  var ServerRegistry = class {
109
120
  entries = /* @__PURE__ */ new Map();
110
121
  listeners = /* @__PURE__ */ new Set();
122
+ toolListWatches = /* @__PURE__ */ new Map();
111
123
  version = 0;
112
124
  register(name, server) {
125
+ this.stopToolListWatch(name);
113
126
  this.entries.set(name, {
114
127
  server,
115
128
  client: null,
@@ -123,6 +136,7 @@ var ServerRegistry = class {
123
136
  this.notify();
124
137
  }
125
138
  unregister(name) {
139
+ this.stopToolListWatch(name);
126
140
  this.entries.delete(name);
127
141
  this.notify();
128
142
  }
@@ -161,6 +175,7 @@ var ServerRegistry = class {
161
175
  };
162
176
  this.entries.set(name, connected);
163
177
  this.notify();
178
+ this.watchToolList(name, client);
164
179
  return client;
165
180
  } catch (err) {
166
181
  const error = err instanceof Error ? err : new Error(String(err));
@@ -172,6 +187,32 @@ var ServerRegistry = class {
172
187
  async connectAll() {
173
188
  await Promise.all(this.list().map((name) => this.connect(name)));
174
189
  }
190
+ /**
191
+ * Keep `name`'s tool list current: re-list through `client` now, and whenever the server
192
+ * announces `notifications/tools/list_changed` (a dynamic tool registered or removed, say). The
193
+ * first listing catches a change made before the watch began. Only the newest listing is applied,
194
+ * and only while `client` is still the entry's client.
195
+ */
196
+ watchToolList(name, client) {
197
+ this.stopToolListWatch(name);
198
+ if (typeof client.onNotification !== "function") return;
199
+ let latest = 0;
200
+ const relist = () => {
201
+ const listing = ++latest;
202
+ client.listTools().then((tools) => {
203
+ const entry = this.entries.get(name);
204
+ if (listing !== latest || !entry || entry.client !== client) return;
205
+ if (sameListing(entry.tools, tools)) return;
206
+ this.update(name, { tools });
207
+ }).catch(() => {
208
+ });
209
+ };
210
+ const stop = client.onNotification((notification) => {
211
+ if (notification.method === "notifications/tools/list_changed") relist();
212
+ });
213
+ this.toolListWatches.set(name, stop);
214
+ relist();
215
+ }
175
216
  update(name, partial) {
176
217
  const entry = this.entries.get(name);
177
218
  if (entry) {
@@ -180,6 +221,7 @@ var ServerRegistry = class {
180
221
  }
181
222
  }
182
223
  clear() {
224
+ for (const name of [...this.toolListWatches.keys()]) this.stopToolListWatch(name);
183
225
  this.entries.clear();
184
226
  this.notify();
185
227
  }
@@ -192,12 +234,23 @@ var ServerRegistry = class {
192
234
  getVersion() {
193
235
  return this.version;
194
236
  }
237
+ stopToolListWatch(name) {
238
+ const stop = this.toolListWatches.get(name);
239
+ if (!stop) return;
240
+ this.toolListWatches.delete(name);
241
+ stop();
242
+ }
195
243
  notify() {
196
244
  this.version++;
197
245
  this.listeners.forEach((l) => l());
198
246
  }
199
247
  };
200
- var serverRegistry = new ServerRegistry();
248
+ var REGISTRY_KEY = /* @__PURE__ */ Symbol.for("@frontmcp/react/serverRegistry");
249
+ function getOrCreateRegistry() {
250
+ const host = globalThis;
251
+ return host[REGISTRY_KEY] ??= new ServerRegistry();
252
+ }
253
+ var serverRegistry = getOrCreateRegistry();
201
254
 
202
255
  // libs/react/src/registry/DynamicRegistry.ts
203
256
  var DynamicRegistry = class {
@@ -206,22 +259,23 @@ var DynamicRegistry = class {
206
259
  toolRefCounts = /* @__PURE__ */ new Map();
207
260
  resourceRefCounts = /* @__PURE__ */ new Map();
208
261
  listeners = /* @__PURE__ */ new Set();
262
+ resourceListeners = /* @__PURE__ */ new Set();
209
263
  version = 0;
210
264
  /**
211
265
  * Register a dynamic tool. Returns an unregister function
212
266
  * suitable for useEffect cleanup.
213
267
  *
214
268
  * Multiple registrations of the same name are ref-counted:
215
- * subsequent registrations update the definition but the tool
216
- * is only removed when every registrant has unregistered.
269
+ * subsequent registrations replace the definition (and notify, so
270
+ * the server registration follows the new description, schema or
271
+ * availability) but the tool is only removed when every registrant
272
+ * has unregistered.
217
273
  */
218
274
  registerTool(def) {
219
275
  const existing = this.toolRefCounts.get(def.name) ?? 0;
220
276
  this.toolRefCounts.set(def.name, existing + 1);
221
277
  this.tools.set(def.name, def);
222
- if (existing === 0) {
223
- this.notify();
224
- }
278
+ this.notify();
225
279
  let called = false;
226
280
  return () => {
227
281
  if (called) return;
@@ -284,8 +338,18 @@ var DynamicRegistry = class {
284
338
  if (existing) {
285
339
  existing.read = read;
286
340
  this.notify();
341
+ this.resourceListeners.forEach((l) => {
342
+ l(uri);
343
+ });
287
344
  }
288
345
  }
346
+ /** Listen for changes to a dynamic resource's content (the in-page `resources/updated`). */
347
+ onResourceUpdated(listener) {
348
+ this.resourceListeners.add(listener);
349
+ return () => {
350
+ this.resourceListeners.delete(listener);
351
+ };
352
+ }
289
353
  getTools() {
290
354
  return [...this.tools.values()];
291
355
  }
@@ -331,16 +395,6 @@ var DynamicRegistry = class {
331
395
 
332
396
  // libs/react/src/registry/createWrappedServer.ts
333
397
  function patchClientWithDynamic(client, dynamicRegistry) {
334
- if (typeof client.callTool === "function") {
335
- const originalCallTool = client.callTool.bind(client);
336
- client.callTool = async (name, args) => {
337
- const dynamicTool = dynamicRegistry.findTool(name);
338
- if (dynamicTool) {
339
- return dynamicTool.execute(args ?? {});
340
- }
341
- return originalCallTool(name, args);
342
- };
343
- }
344
398
  if (typeof client.readResource === "function") {
345
399
  const originalReadResource = client.readResource.bind(client);
346
400
  client.readResource = async (uri) => {
@@ -351,6 +405,31 @@ function patchClientWithDynamic(client, dynamicRegistry) {
351
405
  return originalReadResource(uri);
352
406
  };
353
407
  }
408
+ if (typeof client.subscribeResource === "function") {
409
+ const originalSubscribe = client.subscribeResource.bind(client);
410
+ client.subscribeResource = async (uri) => {
411
+ if (dynamicRegistry.hasResource(uri)) return;
412
+ return originalSubscribe(uri);
413
+ };
414
+ }
415
+ if (typeof client.unsubscribeResource === "function") {
416
+ const originalUnsubscribe = client.unsubscribeResource.bind(client);
417
+ client.unsubscribeResource = async (uri) => {
418
+ if (dynamicRegistry.hasResource(uri)) return;
419
+ return originalUnsubscribe(uri);
420
+ };
421
+ }
422
+ if (typeof client.onResourceUpdated === "function") {
423
+ const originalOnUpdated = client.onResourceUpdated.bind(client);
424
+ client.onResourceUpdated = (handler) => {
425
+ const offServer = originalOnUpdated(handler);
426
+ const offDynamic = dynamicRegistry.onResourceUpdated(handler);
427
+ return () => {
428
+ offServer();
429
+ offDynamic();
430
+ };
431
+ };
432
+ }
354
433
  return client;
355
434
  }
356
435
  function createWrappedServer(base, dynamicRegistry) {
@@ -359,28 +438,14 @@ function createWrappedServer(base, dynamicRegistry) {
359
438
  return base.ready;
360
439
  },
361
440
  async listTools(options) {
362
- const baseResult = await base.listTools(options);
363
- const dynamicTools = dynamicRegistry.getTools();
364
- if (dynamicTools.length === 0) return baseResult;
365
- const dynamicNames = new Set(dynamicTools.map((t) => t.name));
366
- const baseTools = (baseResult.tools ?? []).filter((t) => !dynamicNames.has(t.name));
367
- const mergedTools = [
368
- ...baseTools,
369
- ...dynamicTools.map((t) => ({
370
- name: t.name,
371
- description: t.description,
372
- inputSchema: t.inputSchema
373
- }))
374
- ];
375
- return { ...baseResult, tools: mergedTools };
441
+ return base.listTools(options);
376
442
  },
377
443
  async callTool(name, args, options) {
378
- const dynamicTool = dynamicRegistry.findTool(name);
379
- if (dynamicTool) {
380
- return dynamicTool.execute(args ?? {});
381
- }
382
444
  return base.callTool(name, args, options);
383
445
  },
446
+ async registerTool(definition) {
447
+ return base.registerTool(definition);
448
+ },
384
449
  async listResources(options) {
385
450
  const baseResult = await base.listResources(options);
386
451
  const dynamicResources = dynamicRegistry.getResources();
@@ -389,9 +454,10 @@ function createWrappedServer(base, dynamicRegistry) {
389
454
  const baseResources = (baseResult.resources ?? []).filter(
390
455
  (r) => !dynamicUris.has(r.uri)
391
456
  );
457
+ const laterPage = options?.cursor !== void 0;
392
458
  const mergedResources = [
393
459
  ...baseResources,
394
- ...dynamicResources.map((r) => ({
460
+ ...(laterPage ? [] : dynamicResources).map((r) => ({
395
461
  uri: r.uri,
396
462
  name: r.name ?? r.uri,
397
463
  description: r.description,
@@ -495,19 +561,131 @@ function extractName(uri) {
495
561
  }
496
562
 
497
563
  // libs/react/src/provider/FrontMcpContext.ts
498
- var defaultDynamicRegistry = new DynamicRegistry();
499
- var FrontMcpContext = (0, import_react.createContext)({
500
- name: "default",
501
- registry: new ComponentRegistry(),
502
- dynamicRegistry: defaultDynamicRegistry,
503
- getDynamicRegistry: () => defaultDynamicRegistry,
504
- connect: async () => {
505
- }
506
- });
564
+ var CONTEXT_KEY = /* @__PURE__ */ Symbol.for("@frontmcp/react/FrontMcpContext");
565
+ function getOrCreateContext() {
566
+ const host = globalThis;
567
+ const existing = host[CONTEXT_KEY];
568
+ if (existing) return existing;
569
+ const defaultDynamicRegistry = new DynamicRegistry();
570
+ const created = (0, import_react.createContext)({
571
+ name: "default",
572
+ registry: new ComponentRegistry(),
573
+ dynamicRegistry: defaultDynamicRegistry,
574
+ getDynamicRegistry: () => defaultDynamicRegistry,
575
+ connect: async () => {
576
+ }
577
+ });
578
+ host[CONTEXT_KEY] = created;
579
+ return created;
580
+ }
581
+ var FrontMcpContext = getOrCreateContext();
507
582
 
508
583
  // libs/react/src/provider/FrontMcpProvider.tsx
509
584
  var import_react3 = __toESM(require("react"));
510
585
 
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
+
511
689
  // libs/react/src/state/useStoreRegistration.ts
512
690
  var import_react2 = require("react");
513
691
  var VALID_NAME_RE = /^[a-zA-Z0-9_-]+$/;
@@ -618,6 +796,7 @@ function FrontMcpProvider({
618
796
  servers,
619
797
  components,
620
798
  stores,
799
+ dynamicToolApps,
621
800
  autoConnect = true,
622
801
  children,
623
802
  onConnected,
@@ -626,6 +805,8 @@ function FrontMcpProvider({
626
805
  const resolvedName = nameProp ?? "default";
627
806
  const mountedRef = (0, import_react3.useRef)(true);
628
807
  const clientRef = (0, import_react3.useRef)(null);
808
+ const onErrorRef = (0, import_react3.useRef)(onError);
809
+ onErrorRef.current = onError;
629
810
  const registry = (0, import_react3.useMemo)(() => {
630
811
  const reg = new ComponentRegistry();
631
812
  if (components) {
@@ -649,16 +830,36 @@ function FrontMcpProvider({
649
830
  const dynamicRegistry = (0, import_react3.useMemo)(() => getDynamicRegistry(resolvedName), [getDynamicRegistry, resolvedName]);
650
831
  useStoreRegistration(stores ?? [], dynamicRegistry);
651
832
  const wrappedServer = (0, import_react3.useMemo)(() => createWrappedServer(server, dynamicRegistry), [server, dynamicRegistry]);
833
+ const reportToolError = (0, import_react3.useCallback)((error, toolName) => {
834
+ const report = onErrorRef.current;
835
+ if (report) report(new Error(`Dynamic tool "${toolName}" was not registered: ${error.message}`));
836
+ else console.warn(`[frontmcp] dynamic tool "${toolName}" was not registered: ${error.message}`);
837
+ }, []);
838
+ const dynamicToolAppsKey = JSON.stringify(dynamicToolApps ?? {});
839
+ const toolApps = (0, import_react3.useMemo)(
840
+ () => JSON.parse(dynamicToolAppsKey),
841
+ [dynamicToolAppsKey]
842
+ );
843
+ const primaryToolApp = toolApps[resolvedName];
844
+ (0, import_react3.useEffect)(
845
+ () => bindDynamicTools(dynamicRegistry, server, { onError: reportToolError, app: primaryToolApp }),
846
+ [dynamicRegistry, server, reportToolError, primaryToolApp]
847
+ );
652
848
  (0, import_react3.useEffect)(() => {
849
+ const unbinds = [];
653
850
  serverRegistry.register(resolvedName, wrappedServer);
654
851
  if (servers) {
655
852
  for (const [sName, srv] of Object.entries(servers)) {
656
853
  const srvRegistry = getDynamicRegistry(sName);
657
854
  const wrappedSrv = createWrappedServer(srv, srvRegistry);
658
855
  serverRegistry.register(sName, wrappedSrv);
856
+ unbinds.push(bindDynamicTools(srvRegistry, srv, { onError: reportToolError, app: toolApps[sName] }));
659
857
  }
660
858
  }
661
859
  return () => {
860
+ unbinds.forEach((unbind) => {
861
+ unbind();
862
+ });
662
863
  serverRegistry.unregister(resolvedName);
663
864
  if (servers) {
664
865
  for (const sName of Object.keys(servers)) {
@@ -671,19 +872,18 @@ function FrontMcpProvider({
671
872
  }
672
873
  }
673
874
  };
674
- }, [resolvedName, wrappedServer, servers, getDynamicRegistry]);
875
+ }, [resolvedName, wrappedServer, servers, getDynamicRegistry, reportToolError, toolApps]);
675
876
  (0, import_react3.useEffect)(() => {
676
877
  const refreshServerEntry = (name) => {
677
878
  const entry = serverRegistry.get(name);
678
879
  if (!entry || !entry.client) return;
679
880
  const srv = entry.server;
680
881
  if (!srv) return;
681
- Promise.all([srv.listTools(), srv.listResources()]).then(([toolsResult, resourcesResult]) => {
682
- if (mountedRef.current) {
683
- serverRegistry.update(name, {
684
- tools: toolsResult.tools ?? [],
685
- resources: resourcesResult.resources ?? []
686
- });
882
+ srv.listResources().then((resourcesResult) => {
883
+ const resources = resourcesResult.resources ?? [];
884
+ const current = serverRegistry.get(name);
885
+ if (mountedRef.current && current && !sameListing(current.resources, resources)) {
886
+ serverRegistry.update(name, { resources });
687
887
  }
688
888
  }).catch(() => {
689
889
  });
@@ -726,11 +926,6 @@ function FrontMcpProvider({
726
926
  safeList(() => client.listPrompts(), { prompts: [] })
727
927
  ]);
728
928
  if (mountedRef.current) {
729
- const dynamicTools = dynamicRegistry.getTools().map((t) => ({
730
- name: t.name,
731
- description: t.description,
732
- inputSchema: t.inputSchema
733
- }));
734
929
  const dynamicResources = dynamicRegistry.getResources().map((r) => ({
735
930
  uri: r.uri,
736
931
  name: r.name,
@@ -738,10 +933,6 @@ function FrontMcpProvider({
738
933
  mimeType: r.mimeType
739
934
  }));
740
935
  const baseTools = toolsResult;
741
- const dynamicToolNames = new Set(dynamicTools.map((t) => t.name));
742
- const filteredBaseTools = (Array.isArray(baseTools) ? baseTools : []).filter(
743
- (t) => !dynamicToolNames.has(t.name)
744
- );
745
936
  const baseResources = resourcesResult.resources ?? [];
746
937
  const dynamicResourceUris = new Set(dynamicResources.map((r) => r.uri));
747
938
  const filteredBaseResources = baseResources.filter((r) => !dynamicResourceUris.has(r.uri));
@@ -749,11 +940,12 @@ function FrontMcpProvider({
749
940
  client,
750
941
  status: "connected",
751
942
  error: null,
752
- tools: [...filteredBaseTools, ...dynamicTools],
943
+ tools: Array.isArray(baseTools) ? baseTools : [],
753
944
  resources: [...filteredBaseResources, ...dynamicResources],
754
945
  resourceTemplates: templatesResult.resourceTemplates ?? [],
755
946
  prompts: promptsResult.prompts ?? []
756
947
  });
948
+ serverRegistry.watchToolList(resolvedName, client);
757
949
  onConnected?.(client);
758
950
  }
759
951
  if (servers) {
@@ -799,10 +991,12 @@ var import_react5 = require("react");
799
991
  // libs/react/src/hooks/useServer.ts
800
992
  var import_react4 = require("react");
801
993
  function useServer(name) {
994
+ const ctx = (0, import_react4.useContext)(FrontMcpContext);
995
+ const serverName = name ?? ctx.name;
802
996
  const subscribe = (0, import_react4.useCallback)((cb) => serverRegistry.subscribe(cb), []);
803
997
  const getSnapshot = (0, import_react4.useCallback)(() => {
804
- return serverRegistry.get(name ?? "default");
805
- }, [name]);
998
+ return serverRegistry.get(serverName);
999
+ }, [serverName]);
806
1000
  return (0, import_react4.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
807
1001
  }
808
1002
 
@@ -1065,16 +1259,24 @@ function zodToJsonSchema(schema) {
1065
1259
  }
1066
1260
 
1067
1261
  // libs/react/src/hooks/useDynamicTool.ts
1262
+ function stableKey(value) {
1263
+ try {
1264
+ return JSON.stringify(value) ?? "";
1265
+ } catch {
1266
+ return String(Math.random());
1267
+ }
1268
+ }
1068
1269
  function useDynamicTool(options) {
1069
- const { name, description, enabled = true } = options;
1270
+ const { name, description, app, enabled = true } = options;
1070
1271
  const { getDynamicRegistry } = (0, import_react10.useContext)(FrontMcpContext);
1071
1272
  const dynamicRegistry = getDynamicRegistry(options.server);
1072
- const resolvedInputSchema = (0, import_react10.useMemo)(() => {
1073
- if ("schema" in options && options.schema) {
1074
- return zodToJsonSchema(options.schema);
1075
- }
1076
- return options.inputSchema;
1077
- }, ["schema" in options ? options.schema : void 0, "inputSchema" in options ? options.inputSchema : void 0]);
1273
+ const computedInputSchema = "schema" in options && options.schema ? zodToJsonSchema(options.schema) : options.inputSchema;
1274
+ const inputSchemaKey = stableKey(computedInputSchema);
1275
+ const resolvedInputSchema = (0, import_react10.useMemo)(() => computedInputSchema, [inputSchemaKey]);
1276
+ const annotationsKey = stableKey(options.annotations ?? null);
1277
+ const annotations = (0, import_react10.useMemo)(() => options.annotations, [annotationsKey]);
1278
+ const availableWhenKey = stableKey(options.availableWhen ?? null);
1279
+ const availableWhen = (0, import_react10.useMemo)(() => options.availableWhen, [availableWhenKey]);
1078
1280
  const executeRef = (0, import_react10.useRef)(options.execute);
1079
1281
  executeRef.current = options.execute;
1080
1282
  const schemaRef = (0, import_react10.useRef)("schema" in options && options.schema ? options.schema : null);
@@ -1110,10 +1312,13 @@ function useDynamicTool(options) {
1110
1312
  name,
1111
1313
  description,
1112
1314
  inputSchema: resolvedInputSchema,
1113
- execute: stableExecute
1315
+ execute: stableExecute,
1316
+ ...annotations && { annotations },
1317
+ ...availableWhen && { availableWhen },
1318
+ ...app && { app }
1114
1319
  });
1115
1320
  return unregister;
1116
- }, [dynamicRegistry, name, description, resolvedInputSchema, enabled]);
1321
+ }, [dynamicRegistry, name, description, resolvedInputSchema, annotations, availableWhen, app, enabled]);
1117
1322
  }
1118
1323
 
1119
1324
  // libs/react/src/hooks/useDynamicResource.ts
@@ -1327,12 +1532,19 @@ function ToolForm({
1327
1532
  const properties = schema["properties"] ?? {};
1328
1533
  const required = schema["required"] ?? [];
1329
1534
  const [values, setValues] = (0, import_react14.useState)({});
1535
+ const initialValue = (key, prop) => {
1536
+ const options = prop["enum"];
1537
+ if (!options) return "";
1538
+ const fallback = prop["default"];
1539
+ if (typeof fallback === "string" && options.includes(fallback)) return fallback;
1540
+ return required.includes(key) ? options[0] ?? "" : "";
1541
+ };
1330
1542
  const handleSubmit = (0, import_react14.useCallback)(
1331
1543
  (e) => {
1332
1544
  e.preventDefault();
1333
1545
  const args = {};
1334
1546
  for (const [key, prop] of Object.entries(properties)) {
1335
- const raw = values[key] ?? "";
1547
+ const raw = values[key] ?? initialValue(key, prop);
1336
1548
  if (!required.includes(key) && raw === "") continue;
1337
1549
  if (prop["type"] === "number" || prop["type"] === "integer") {
1338
1550
  args[key] = Number(raw);
@@ -1356,7 +1568,7 @@ function ToolForm({
1356
1568
  const isRequired = required.includes(key);
1357
1569
  const enumValues = prop["enum"];
1358
1570
  const fieldType = getFieldType(prop);
1359
- const value = values[key] ?? "";
1571
+ const value = values[key] ?? initialValue(key, prop);
1360
1572
  if (renderField) {
1361
1573
  return import_react14.default.createElement(
1362
1574
  import_react14.default.Fragment,
@@ -1384,9 +1596,10 @@ function ToolForm({
1384
1596
  "select",
1385
1597
  {
1386
1598
  id: `field-${key}`,
1387
- value: value || enumValues[0] || "",
1599
+ value,
1388
1600
  onChange: (e) => handleChange(key, e.target.value)
1389
1601
  },
1602
+ ...isRequired ? [] : [import_react14.default.createElement("option", { key: "", value: "" }, "")],
1390
1603
  ...enumValues.map((v) => import_react14.default.createElement("option", { key: v, value: v }, v))
1391
1604
  ) : import_react14.default.createElement("input", {
1392
1605
  id: `field-${key}`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frontmcp/react",
3
- "version": "1.8.6",
3
+ "version": "1.9.0",
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.8.6",
90
- "@frontmcp/sdk": "1.8.6",
91
- "@frontmcp/utils": "1.8.6",
89
+ "@frontmcp/lazy-zod": "1.9.0",
90
+ "@frontmcp/sdk": "1.9.0",
91
+ "@frontmcp/utils": "1.9.0",
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.8.6",
106
- "@frontmcp/utils": "1.8.6"
105
+ "@frontmcp/sdk": "1.9.0",
106
+ "@frontmcp/utils": "1.9.0"
107
107
  },
108
108
  "devDependencies": {
109
109
  "typescript": "^5.9.3"
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * React context for FrontMCP.
3
3
  */
4
+ import { type Context } from 'react';
4
5
  import type { FrontMcpContextValue } from '../types';
5
- export declare const FrontMcpContext: import("react").Context<FrontMcpContextValue>;
6
+ export declare const FrontMcpContext: Context<FrontMcpContextValue>;
6
7
  //# sourceMappingURL=FrontMcpContext.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"FrontMcpContext.d.ts","sourceRoot":"","sources":["../../src/provider/FrontMcpContext.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAMrD,eAAO,MAAM,eAAe,+CAM1B,CAAC"}
1
+ {"version":3,"file":"FrontMcpContext.d.ts","sourceRoot":"","sources":["../../src/provider/FrontMcpContext.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAiB,KAAK,OAAO,EAAE,MAAM,OAAO,CAAC;AAIpD,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AA2BrD,eAAO,MAAM,eAAe,+BAAuB,CAAC"}