@frontmcp/react 1.8.7 → 1.9.1-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -107,11 +107,22 @@ var import_sdk4 = require("@frontmcp/sdk");
107
107
  var import_sdk5 = require("@frontmcp/sdk");
108
108
 
109
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
+ }
110
119
  var ServerRegistry = class {
111
120
  entries = /* @__PURE__ */ new Map();
112
121
  listeners = /* @__PURE__ */ new Set();
122
+ toolListWatches = /* @__PURE__ */ new Map();
113
123
  version = 0;
114
124
  register(name, server) {
125
+ this.stopToolListWatch(name);
115
126
  this.entries.set(name, {
116
127
  server,
117
128
  client: null,
@@ -125,6 +136,7 @@ var ServerRegistry = class {
125
136
  this.notify();
126
137
  }
127
138
  unregister(name) {
139
+ this.stopToolListWatch(name);
128
140
  this.entries.delete(name);
129
141
  this.notify();
130
142
  }
@@ -163,6 +175,7 @@ var ServerRegistry = class {
163
175
  };
164
176
  this.entries.set(name, connected);
165
177
  this.notify();
178
+ this.watchToolList(name, client);
166
179
  return client;
167
180
  } catch (err) {
168
181
  const error = err instanceof Error ? err : new Error(String(err));
@@ -174,6 +187,32 @@ var ServerRegistry = class {
174
187
  async connectAll() {
175
188
  await Promise.all(this.list().map((name) => this.connect(name)));
176
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
+ }
177
216
  update(name, partial) {
178
217
  const entry = this.entries.get(name);
179
218
  if (entry) {
@@ -182,6 +221,7 @@ var ServerRegistry = class {
182
221
  }
183
222
  }
184
223
  clear() {
224
+ for (const name of [...this.toolListWatches.keys()]) this.stopToolListWatch(name);
185
225
  this.entries.clear();
186
226
  this.notify();
187
227
  }
@@ -194,6 +234,12 @@ var ServerRegistry = class {
194
234
  getVersion() {
195
235
  return this.version;
196
236
  }
237
+ stopToolListWatch(name) {
238
+ const stop = this.toolListWatches.get(name);
239
+ if (!stop) return;
240
+ this.toolListWatches.delete(name);
241
+ stop();
242
+ }
197
243
  notify() {
198
244
  this.version++;
199
245
  this.listeners.forEach((l) => l());
@@ -220,16 +266,16 @@ var DynamicRegistry = class {
220
266
  * suitable for useEffect cleanup.
221
267
  *
222
268
  * Multiple registrations of the same name are ref-counted:
223
- * subsequent registrations update the definition but the tool
224
- * 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.
225
273
  */
226
274
  registerTool(def) {
227
275
  const existing = this.toolRefCounts.get(def.name) ?? 0;
228
276
  this.toolRefCounts.set(def.name, existing + 1);
229
277
  this.tools.set(def.name, def);
230
- if (existing === 0) {
231
- this.notify();
232
- }
278
+ this.notify();
233
279
  let called = false;
234
280
  return () => {
235
281
  if (called) return;
@@ -349,16 +395,6 @@ var DynamicRegistry = class {
349
395
 
350
396
  // libs/react/src/registry/createWrappedServer.ts
351
397
  function patchClientWithDynamic(client, dynamicRegistry) {
352
- if (typeof client.callTool === "function") {
353
- const originalCallTool = client.callTool.bind(client);
354
- client.callTool = async (name, args) => {
355
- const dynamicTool = dynamicRegistry.findTool(name);
356
- if (dynamicTool) {
357
- return dynamicTool.execute(args ?? {});
358
- }
359
- return originalCallTool(name, args);
360
- };
361
- }
362
398
  if (typeof client.readResource === "function") {
363
399
  const originalReadResource = client.readResource.bind(client);
364
400
  client.readResource = async (uri) => {
@@ -402,28 +438,14 @@ function createWrappedServer(base, dynamicRegistry) {
402
438
  return base.ready;
403
439
  },
404
440
  async listTools(options) {
405
- const baseResult = await base.listTools(options);
406
- const dynamicTools = dynamicRegistry.getTools();
407
- if (dynamicTools.length === 0) return baseResult;
408
- const dynamicNames = new Set(dynamicTools.map((t) => t.name));
409
- const baseTools = (baseResult.tools ?? []).filter((t) => !dynamicNames.has(t.name));
410
- const mergedTools = [
411
- ...baseTools,
412
- ...dynamicTools.map((t) => ({
413
- name: t.name,
414
- description: t.description,
415
- inputSchema: t.inputSchema
416
- }))
417
- ];
418
- return { ...baseResult, tools: mergedTools };
441
+ return base.listTools(options);
419
442
  },
420
443
  async callTool(name, args, options) {
421
- const dynamicTool = dynamicRegistry.findTool(name);
422
- if (dynamicTool) {
423
- return dynamicTool.execute(args ?? {});
424
- }
425
444
  return base.callTool(name, args, options);
426
445
  },
446
+ async registerTool(definition) {
447
+ return base.registerTool(definition);
448
+ },
427
449
  async listResources(options) {
428
450
  const baseResult = await base.listResources(options);
429
451
  const dynamicResources = dynamicRegistry.getResources();
@@ -432,9 +454,10 @@ function createWrappedServer(base, dynamicRegistry) {
432
454
  const baseResources = (baseResult.resources ?? []).filter(
433
455
  (r) => !dynamicUris.has(r.uri)
434
456
  );
457
+ const laterPage = options?.cursor !== void 0;
435
458
  const mergedResources = [
436
459
  ...baseResources,
437
- ...dynamicResources.map((r) => ({
460
+ ...(laterPage ? [] : dynamicResources).map((r) => ({
438
461
  uri: r.uri,
439
462
  name: r.name ?? r.uri,
440
463
  description: r.description,
@@ -560,6 +583,109 @@ var FrontMcpContext = getOrCreateContext();
560
583
  // libs/react/src/provider/FrontMcpProvider.tsx
561
584
  var import_react3 = __toESM(require("react"));
562
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
+
563
689
  // libs/react/src/state/useStoreRegistration.ts
564
690
  var import_react2 = require("react");
565
691
  var VALID_NAME_RE = /^[a-zA-Z0-9_-]+$/;
@@ -670,6 +796,7 @@ function FrontMcpProvider({
670
796
  servers,
671
797
  components,
672
798
  stores,
799
+ dynamicToolApps,
673
800
  autoConnect = true,
674
801
  children,
675
802
  onConnected,
@@ -678,6 +805,8 @@ function FrontMcpProvider({
678
805
  const resolvedName = nameProp ?? "default";
679
806
  const mountedRef = (0, import_react3.useRef)(true);
680
807
  const clientRef = (0, import_react3.useRef)(null);
808
+ const onErrorRef = (0, import_react3.useRef)(onError);
809
+ onErrorRef.current = onError;
681
810
  const registry = (0, import_react3.useMemo)(() => {
682
811
  const reg = new ComponentRegistry();
683
812
  if (components) {
@@ -701,16 +830,36 @@ function FrontMcpProvider({
701
830
  const dynamicRegistry = (0, import_react3.useMemo)(() => getDynamicRegistry(resolvedName), [getDynamicRegistry, resolvedName]);
702
831
  useStoreRegistration(stores ?? [], dynamicRegistry);
703
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
+ );
704
848
  (0, import_react3.useEffect)(() => {
849
+ const unbinds = [];
705
850
  serverRegistry.register(resolvedName, wrappedServer);
706
851
  if (servers) {
707
852
  for (const [sName, srv] of Object.entries(servers)) {
708
853
  const srvRegistry = getDynamicRegistry(sName);
709
854
  const wrappedSrv = createWrappedServer(srv, srvRegistry);
710
855
  serverRegistry.register(sName, wrappedSrv);
856
+ unbinds.push(bindDynamicTools(srvRegistry, srv, { onError: reportToolError, app: toolApps[sName] }));
711
857
  }
712
858
  }
713
859
  return () => {
860
+ unbinds.forEach((unbind) => {
861
+ unbind();
862
+ });
714
863
  serverRegistry.unregister(resolvedName);
715
864
  if (servers) {
716
865
  for (const sName of Object.keys(servers)) {
@@ -723,19 +872,18 @@ function FrontMcpProvider({
723
872
  }
724
873
  }
725
874
  };
726
- }, [resolvedName, wrappedServer, servers, getDynamicRegistry]);
875
+ }, [resolvedName, wrappedServer, servers, getDynamicRegistry, reportToolError, toolApps]);
727
876
  (0, import_react3.useEffect)(() => {
728
877
  const refreshServerEntry = (name) => {
729
878
  const entry = serverRegistry.get(name);
730
879
  if (!entry || !entry.client) return;
731
880
  const srv = entry.server;
732
881
  if (!srv) return;
733
- Promise.all([srv.listTools(), srv.listResources()]).then(([toolsResult, resourcesResult]) => {
734
- if (mountedRef.current) {
735
- serverRegistry.update(name, {
736
- tools: toolsResult.tools ?? [],
737
- resources: resourcesResult.resources ?? []
738
- });
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 });
739
887
  }
740
888
  }).catch(() => {
741
889
  });
@@ -778,11 +926,6 @@ function FrontMcpProvider({
778
926
  safeList(() => client.listPrompts(), { prompts: [] })
779
927
  ]);
780
928
  if (mountedRef.current) {
781
- const dynamicTools = dynamicRegistry.getTools().map((t) => ({
782
- name: t.name,
783
- description: t.description,
784
- inputSchema: t.inputSchema
785
- }));
786
929
  const dynamicResources = dynamicRegistry.getResources().map((r) => ({
787
930
  uri: r.uri,
788
931
  name: r.name,
@@ -790,10 +933,6 @@ function FrontMcpProvider({
790
933
  mimeType: r.mimeType
791
934
  }));
792
935
  const baseTools = toolsResult;
793
- const dynamicToolNames = new Set(dynamicTools.map((t) => t.name));
794
- const filteredBaseTools = (Array.isArray(baseTools) ? baseTools : []).filter(
795
- (t) => !dynamicToolNames.has(t.name)
796
- );
797
936
  const baseResources = resourcesResult.resources ?? [];
798
937
  const dynamicResourceUris = new Set(dynamicResources.map((r) => r.uri));
799
938
  const filteredBaseResources = baseResources.filter((r) => !dynamicResourceUris.has(r.uri));
@@ -801,11 +940,12 @@ function FrontMcpProvider({
801
940
  client,
802
941
  status: "connected",
803
942
  error: null,
804
- tools: [...filteredBaseTools, ...dynamicTools],
943
+ tools: Array.isArray(baseTools) ? baseTools : [],
805
944
  resources: [...filteredBaseResources, ...dynamicResources],
806
945
  resourceTemplates: templatesResult.resourceTemplates ?? [],
807
946
  prompts: promptsResult.prompts ?? []
808
947
  });
948
+ serverRegistry.watchToolList(resolvedName, client);
809
949
  onConnected?.(client);
810
950
  }
811
951
  if (servers) {
@@ -1127,12 +1267,16 @@ function stableKey(value) {
1127
1267
  }
1128
1268
  }
1129
1269
  function useDynamicTool(options) {
1130
- const { name, description, enabled = true } = options;
1270
+ const { name, description, app, enabled = true } = options;
1131
1271
  const { getDynamicRegistry } = (0, import_react10.useContext)(FrontMcpContext);
1132
1272
  const dynamicRegistry = getDynamicRegistry(options.server);
1133
1273
  const computedInputSchema = "schema" in options && options.schema ? zodToJsonSchema(options.schema) : options.inputSchema;
1134
1274
  const inputSchemaKey = stableKey(computedInputSchema);
1135
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]);
1136
1280
  const executeRef = (0, import_react10.useRef)(options.execute);
1137
1281
  executeRef.current = options.execute;
1138
1282
  const schemaRef = (0, import_react10.useRef)("schema" in options && options.schema ? options.schema : null);
@@ -1168,10 +1312,13 @@ function useDynamicTool(options) {
1168
1312
  name,
1169
1313
  description,
1170
1314
  inputSchema: resolvedInputSchema,
1171
- execute: stableExecute
1315
+ execute: stableExecute,
1316
+ ...annotations && { annotations },
1317
+ ...availableWhen && { availableWhen },
1318
+ ...app && { app }
1172
1319
  });
1173
1320
  return unregister;
1174
- }, [dynamicRegistry, name, description, resolvedInputSchema, enabled]);
1321
+ }, [dynamicRegistry, name, description, resolvedInputSchema, annotations, availableWhen, app, enabled]);
1175
1322
  }
1176
1323
 
1177
1324
  // libs/react/src/hooks/useDynamicResource.ts
@@ -1385,19 +1532,19 @@ function ToolForm({
1385
1532
  const properties = schema["properties"] ?? {};
1386
1533
  const required = schema["required"] ?? [];
1387
1534
  const [values, setValues] = (0, import_react14.useState)({});
1388
- const initialValue = (prop) => {
1535
+ const initialValue = (key, prop) => {
1389
1536
  const options = prop["enum"];
1390
1537
  if (!options) return "";
1391
1538
  const fallback = prop["default"];
1392
- return typeof fallback === "string" && options.includes(fallback) ? fallback : options[0] ?? "";
1539
+ if (typeof fallback === "string" && options.includes(fallback)) return fallback;
1540
+ return required.includes(key) ? options[0] ?? "" : "";
1393
1541
  };
1394
1542
  const handleSubmit = (0, import_react14.useCallback)(
1395
1543
  (e) => {
1396
1544
  e.preventDefault();
1397
1545
  const args = {};
1398
1546
  for (const [key, prop] of Object.entries(properties)) {
1399
- const seeded = required.includes(key) || prop["default"] !== void 0;
1400
- const raw = values[key] ?? (seeded ? initialValue(prop) : "");
1547
+ const raw = values[key] ?? initialValue(key, prop);
1401
1548
  if (!required.includes(key) && raw === "") continue;
1402
1549
  if (prop["type"] === "number" || prop["type"] === "integer") {
1403
1550
  args[key] = Number(raw);
@@ -1421,7 +1568,7 @@ function ToolForm({
1421
1568
  const isRequired = required.includes(key);
1422
1569
  const enumValues = prop["enum"];
1423
1570
  const fieldType = getFieldType(prop);
1424
- const value = values[key] ?? initialValue(prop);
1571
+ const value = values[key] ?? initialValue(key, prop);
1425
1572
  if (renderField) {
1426
1573
  return import_react14.default.createElement(
1427
1574
  import_react14.default.Fragment,
@@ -1452,6 +1599,7 @@ function ToolForm({
1452
1599
  value,
1453
1600
  onChange: (e) => handleChange(key, e.target.value)
1454
1601
  },
1602
+ ...isRequired ? [] : [import_react14.default.createElement("option", { key: "", value: "" }, "")],
1455
1603
  ...enumValues.map((v) => import_react14.default.createElement("option", { key: v, value: v }, v))
1456
1604
  ) : import_react14.default.createElement("input", {
1457
1605
  id: `field-${key}`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frontmcp/react",
3
- "version": "1.8.7",
3
+ "version": "1.9.1-rc.1",
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.7",
90
- "@frontmcp/sdk": "1.8.7",
91
- "@frontmcp/utils": "1.8.7",
89
+ "@frontmcp/lazy-zod": "1.9.1-rc.1",
90
+ "@frontmcp/sdk": "1.9.1-rc.1",
91
+ "@frontmcp/utils": "1.9.1-rc.1",
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.7",
106
- "@frontmcp/utils": "1.8.7"
105
+ "@frontmcp/sdk": "1.9.1-rc.1",
106
+ "@frontmcp/utils": "1.9.1-rc.1"
107
107
  },
108
108
  "devDependencies": {
109
109
  "typescript": "^5.9.3"
@@ -7,13 +7,14 @@
7
7
  * 4. Optionally auto-connects a client on mount (default: true)
8
8
  * 5. Registers all servers into the shared ServerRegistry singleton
9
9
  * 6. Creates a DynamicRegistry for dynamic tool/resource registration
10
- * 7. Wraps the server to overlay dynamic entries on list/call operations
11
- * 8. All state (status, tools, etc.) lives in the ServerRegistry — context
10
+ * 7. Mirrors dynamic tools into each server as real tools (bindDynamicTools), and keeps the
11
+ * listed tools current from the server's `notifications/tools/list_changed`
12
+ * 8. Wraps the server to overlay dynamic resources on list/read operations
13
+ * 9. All state (status, tools, etc.) lives in the ServerRegistry — context
12
14
  * carries only `name`, `registry`, `dynamicRegistry`, and `connect`.
13
15
  */
14
- import React from 'react';
15
- import type { ComponentType } from 'react';
16
- import type { DirectMcpServer, DirectClient } from '@frontmcp/sdk';
16
+ import React, { type ComponentType } from 'react';
17
+ import type { DirectClient, DirectMcpServer } from '@frontmcp/sdk';
17
18
  import type { StoreAdapter } from '../types';
18
19
  export interface FrontMcpProviderProps {
19
20
  /** Logical name for the primary server (defaults to 'default') */
@@ -25,10 +26,15 @@ export interface FrontMcpProviderProps {
25
26
  components?: Record<string, ComponentType<Record<string, unknown>>>;
26
27
  /** Store adapters to register at the provider level (reduxStore, valtioStore, createStore). */
27
28
  stores?: StoreAdapter[];
29
+ /**
30
+ * Server app that dynamic tools join, by server name (the primary server goes by `name`). Needed
31
+ * only for a server with more than one local app; a tool's own `app` option takes precedence.
32
+ */
33
+ dynamicToolApps?: Record<string, string>;
28
34
  autoConnect?: boolean;
29
35
  children: React.ReactNode;
30
36
  onConnected?: (client: DirectClient) => void;
31
37
  onError?: (error: Error) => void;
32
38
  }
33
- export declare function FrontMcpProvider({ name: nameProp, server, servers, components, stores, autoConnect, children, onConnected, onError, }: FrontMcpProviderProps): React.ReactElement;
39
+ export declare function FrontMcpProvider({ name: nameProp, server, servers, components, stores, dynamicToolApps, autoConnect, children, onConnected, onError, }: FrontMcpProviderProps): React.ReactElement;
34
40
  //# sourceMappingURL=FrontMcpProvider.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"FrontMcpProvider.d.ts","sourceRoot":"","sources":["../../src/provider/FrontMcpProvider.tsx"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAkD,MAAM,OAAO,CAAC;AACvE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAC3C,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAKnE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAK7C,MAAM,WAAW,qBAAqB;IACpC,kEAAkE;IAClE,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,qEAAqE;IACrE,MAAM,EAAE,eAAe,CAAC;IACx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aAAa,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC;IACpE,+FAA+F;IAC/F,MAAM,CAAC,EAAE,YAAY,EAAE,CAAC;IACxB;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACzC,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,WAAW,CAAC,EAAE,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI,CAAC;IAC7C,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CAClC;AAED,wBAAgB,gBAAgB,CAAC,EAC/B,IAAI,EAAE,QAAQ,EACd,MAAM,EACN,OAAO,EACP,UAAU,EACV,MAAM,EACN,eAAe,EACf,WAAkB,EAClB,QAAQ,EACR,WAAW,EACX,OAAO,GACR,EAAE,qBAAqB,GAAG,KAAK,CAAC,YAAY,CA8O5C"}
@@ -2,9 +2,10 @@
2
2
  * DynamicRegistry — React-side registry for tools and resources that
3
3
  * components register on mount and unregister on unmount.
4
4
  *
5
- * Operates as an overlay on the base DirectMcpServer: dynamic entries
6
- * are merged into listTools/listResources and checked first on
7
- * callTool/readResource.
5
+ * Tools are mirrored into the server as real tools by `bindDynamicTools`
6
+ * (the provider binds each server's registry), so they run through the
7
+ * server's flows. Resources stay an overlay on the base DirectMcpServer:
8
+ * they are merged into listResources and checked first on readResource.
8
9
  *
9
10
  * Uses the same listener/version pattern as ServerRegistry for
10
11
  * useSyncExternalStore compatibility.
@@ -24,8 +25,10 @@ export declare class DynamicRegistry {
24
25
  * suitable for useEffect cleanup.
25
26
  *
26
27
  * Multiple registrations of the same name are ref-counted:
27
- * subsequent registrations update the definition but the tool
28
- * is only removed when every registrant has unregistered.
28
+ * subsequent registrations replace the definition (and notify, so
29
+ * the server registration follows the new description, schema or
30
+ * availability) but the tool is only removed when every registrant
31
+ * has unregistered.
29
32
  */
30
33
  registerTool(def: DynamicToolDef): () => void;
31
34
  unregisterTool(name: string): void;
@@ -1 +1 @@
1
- {"version":3,"file":"DynamicRegistry.d.ts","sourceRoot":"","sources":["../../src/registry/DynamicRegistry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;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,SAAS,CAAyC;IAC1D,OAAO,CAAC,aAAa,CAA6B;IAClD,OAAO,CAAC,iBAAiB,CAA6B;IACtD,OAAO,CAAC,SAAS,CAAuB;IACxC,OAAO,CAAC,iBAAiB,CAAoC;IAC7D,OAAO,CAAC,OAAO,CAAK;IAEpB;;;;;;;OAOG;IACH,YAAY,CAAC,GAAG,EAAE,cAAc,GAAG,MAAM,IAAI;IAe7C,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAYlC;;;;;OAKG;IACH,gBAAgB,CAAC,GAAG,EAAE,kBAAkB,GAAG,MAAM,IAAI;IAerD,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAYrC,uFAAuF;IACvF,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,CAAC,SAAS,CAAC,GAAG,IAAI;IAOzE,gFAAgF;IAChF,kBAAkB,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,kBAAkB,CAAC,MAAM,CAAC,GAAG,IAAI;IAWvE,4FAA4F;IAC5F,iBAAiB,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,GAAG,MAAM,IAAI;IAO9D,QAAQ,IAAI,cAAc,EAAE;IAI5B,YAAY,IAAI,kBAAkB,EAAE;IAIpC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG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AAC,MAAM,CAAC,GAAG,IAAI;IAWvE,4FAA4F;IAC5F,iBAAiB,CAAC,QAAQ,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,GAAG,MAAM,IAAI;IAO9D,QAAQ,IAAI,cAAc,EAAE;IAI5B,YAAY,IAAI,kBAAkB,EAAE;IAIpC,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS;IAIlD,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,kBAAkB,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"}
@@ -10,6 +10,8 @@
10
10
  import type { DirectClient, DirectMcpServer } from '@frontmcp/sdk';
11
11
  import type { FrontMcpStatus, PromptInfo, ResourceInfo, ResourceTemplateInfo, ToolInfo } from '../types';
12
12
  type Listener = () => void;
13
+ /** Whether two tool / resource listings are the same, entry for entry. */
14
+ export declare function sameListing(a: readonly unknown[], b: readonly unknown[]): boolean;
13
15
  export interface ServerEntry {
14
16
  server: DirectMcpServer;
15
17
  client: DirectClient | null;
@@ -23,6 +25,7 @@ export interface ServerEntry {
23
25
  export declare class ServerRegistry {
24
26
  private entries;
25
27
  private listeners;
28
+ private toolListWatches;
26
29
  private version;
27
30
  register(name: string, server: DirectMcpServer): void;
28
31
  unregister(name: string): void;
@@ -31,10 +34,18 @@ export declare class ServerRegistry {
31
34
  list(): string[];
32
35
  connect(name: string): Promise<DirectClient>;
33
36
  connectAll(): Promise<void>;
37
+ /**
38
+ * Keep `name`'s tool list current: re-list through `client` now, and whenever the server
39
+ * announces `notifications/tools/list_changed` (a dynamic tool registered or removed, say). The
40
+ * first listing catches a change made before the watch began. Only the newest listing is applied,
41
+ * and only while `client` is still the entry's client.
42
+ */
43
+ watchToolList(name: string, client: DirectClient): void;
34
44
  update(name: string, partial: Partial<ServerEntry>): void;
35
45
  clear(): void;
36
46
  subscribe(listener: Listener): () => void;
37
47
  getVersion(): number;
48
+ private stopToolListWatch;
38
49
  private notify;
39
50
  }
40
51
  /** Page-wide singleton — shared across all components and all entry points. */
@@ -1 +1 @@
1
- {"version":3,"file":"ServerRegistry.d.ts","sourceRoot":"","sources":["../../src/registry/ServerRegistry.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAEnE,OAAO,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,YAAY,EAAE,oBAAoB,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEzG,KAAK,QAAQ,GAAG,MAAM,IAAI,CAAC;AAE3B,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,eAAe,CAAC;IACxB,MAAM,EAAE,YAAY,GAAG,IAAI,CAAC;IAC5B,MAAM,EAAE,cAAc,CAAC;IACvB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;IACpB,KAAK,EAAE,QAAQ,EAAE,CAAC;IAClB,SAAS,EAAE,YAAY,EAAE,CAAC;IAC1B,iBAAiB,EAAE,oBAAoB,EAAE,CAAC;IAC1C,OAAO,EAAE,UAAU,EAAE,CAAC;CACvB;AAED,qBAAa,cAAc;IACzB,OAAO,CAAC,OAAO,CAAkC;IACjD,OAAO,CAAC,SAAS,CAAuB;IACxC,OAAO,CAAC,OAAO,CAAK;IAEpB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,GAAG,IAAI;IAcrD,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAK9B,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,SAAS;IAI1C,GAAG,CAAC,IAAI,EAA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,qBAAa,cAAc;IACzB,OAAO,CAAC,OAAO,CAAkC;IACjD,OAAO,CAAC,SAAS,CAAuB;IACxC,OAAO,CAAC,eAAe,CAAiC;IACxD,OAAO,CAAC,OAAO,CAAK;IAEpB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,GAAG,IAAI;IAerD,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAM9B,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,SAAS;IAI1C,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAI1B,IAAI,IAAI,MAAM,EAAE;IAIV,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC;IAwC5C,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAIjC;;;;;OAKG;IACH,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI;IA0BvD,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,WAAW,CAAC,GAAG,IAAI;IAQzD,KAAK,IAAI,IAAI;IAMb,SAAS,CAAC,QAAQ,EAAE,QAAQ,GAAG,MAAM,IAAI;IAOzC,UAAU,IAAI,MAAM;IAIpB,OAAO,CAAC,iBAAiB;IAOzB,OAAO,CAAC,MAAM;CAIf;AAYD,+EAA+E;AAC/E,eAAO,MAAM,cAAc,gBAAwB,CAAC"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * bindDynamicTools — mirrors a DynamicRegistry's tools into a DirectMcpServer as real server tools.
3
+ *
4
+ * Components register tools in the DynamicRegistry (ref-counted, latest `execute` kept in the
5
+ * registry). This binder registers each one with `server.registerTool()`, so the tool is listed by
6
+ * the server, runs through its `tools:call-tool` flow (hooks, authorities, quota, `availableWhen`),
7
+ * reaches every client (including WebMCP) and is announced with `notifications/tools/list_changed`.
8
+ *
9
+ * Changes are reconciled on a microtask, so the register → unregister → register churn of React
10
+ * StrictMode settles into a single change on the server.
11
+ */
12
+ import type { DirectMcpServer } from '@frontmcp/sdk';
13
+ import type { DynamicRegistry } from './DynamicRegistry';
14
+ export interface BindDynamicToolsOptions {
15
+ /** Called when the server refuses a tool (a name conflict, a name over 64 characters, ...). */
16
+ onError?: (error: Error, toolName: string) => void;
17
+ /**
18
+ * Id of the server app that tools join when they don't name one (`DynamicToolDef.app`). Needed
19
+ * only for a server with more than one local app.
20
+ */
21
+ app?: string;
22
+ }
23
+ /**
24
+ * Keep `server`'s runtime tools in step with `registry`'s tools.
25
+ *
26
+ * @returns A function that stops mirroring and unregisters every mirrored tool
27
+ */
28
+ export declare function bindDynamicTools(registry: DynamicRegistry, server: DirectMcpServer, options?: BindDynamicToolsOptions): () => void;
29
+ //# sourceMappingURL=bindDynamicTools.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bindDynamicTools.d.ts","sourceRoot":"","sources":["../../src/registry/bindDynamicTools.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAkB,eAAe,EAAyB,MAAM,eAAe,CAAC;AAG5F,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEzD,MAAM,WAAW,uBAAuB;IACtC,+FAA+F;IAC/F,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAC;IACnD;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AA2BD;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,eAAe,EACzB,MAAM,EAAE,eAAe,EACvB,OAAO,GAAE,uBAA4B,GACpC,MAAM,IAAI,CAsGZ"}