@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/esm/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"
@@ -1,5 +1,5 @@
1
1
  // libs/react/src/state/useStoreResource.ts
2
- import { useContext, useEffect, useRef, useCallback } from "react";
2
+ import { useContext, useEffect, useRef } from "react";
3
3
 
4
4
  // libs/react/src/provider/FrontMcpContext.ts
5
5
  import { createContext } from "react";
@@ -65,16 +65,16 @@ var DynamicRegistry = class {
65
65
  * suitable for useEffect cleanup.
66
66
  *
67
67
  * Multiple registrations of the same name are ref-counted:
68
- * subsequent registrations update the definition but the tool
69
- * is only removed when every registrant has unregistered.
68
+ * subsequent registrations replace the definition (and notify, so
69
+ * the server registration follows the new description, schema or
70
+ * availability) but the tool is only removed when every registrant
71
+ * has unregistered.
70
72
  */
71
73
  registerTool(def) {
72
74
  const existing = this.toolRefCounts.get(def.name) ?? 0;
73
75
  this.toolRefCounts.set(def.name, existing + 1);
74
76
  this.tools.set(def.name, def);
75
- if (existing === 0) {
76
- this.notify();
77
- }
77
+ this.notify();
78
78
  let called = false;
79
79
  return () => {
80
80
  if (called) return;
@@ -214,117 +214,110 @@ var FrontMcpContext = getOrCreateContext();
214
214
 
215
215
  // libs/react/src/state/useStoreResource.ts
216
216
  var VALID_NAME_RE = /^[a-zA-Z0-9_-]+$/;
217
+ function keysOf(map, kind) {
218
+ if (!map) return "[]";
219
+ const keys = Object.keys(map);
220
+ if (kind === "selector") {
221
+ for (const key of keys) {
222
+ if (!key || !VALID_NAME_RE.test(key)) {
223
+ throw new Error(`useStoreResource: invalid selector key "${key}". Keys must match ${VALID_NAME_RE}.`);
224
+ }
225
+ }
226
+ }
227
+ return JSON.stringify(keys.sort());
228
+ }
229
+ function parseKeys(keys) {
230
+ return JSON.parse(keys);
231
+ }
232
+ function jsonResource(uri, value) {
233
+ return { contents: [{ uri, mimeType: "application/json", text: JSON.stringify(value ?? null) }] };
234
+ }
217
235
  function useStoreResource(options) {
218
- const { name, getState, subscribe, selectors, actions } = options;
236
+ const { name, subscribe } = options;
219
237
  const { getDynamicRegistry } = useContext(FrontMcpContext);
220
238
  const dynamicRegistry = getDynamicRegistry(options.server);
221
239
  if (!name || !VALID_NAME_RE.test(name)) {
222
240
  throw new Error(`useStoreResource: invalid store name "${name}". Names must match ${VALID_NAME_RE}.`);
223
241
  }
224
- const getStateRef = useRef(getState);
225
- getStateRef.current = getState;
226
- const readState = useCallback(
227
- async () => ({
228
- contents: [
229
- {
230
- uri: `state://${name}`,
231
- mimeType: "application/json",
232
- text: JSON.stringify(getStateRef.current() ?? null)
233
- }
234
- ]
235
- }),
236
- [name]
237
- );
238
- useEffect(() => {
239
- const unregister = dynamicRegistry.registerResource({
240
- uri: `state://${name}`,
242
+ const selectorKeys = keysOf(options.selectors, "selector");
243
+ const actionKeys = keysOf(options.actions, "action");
244
+ const getStateRef = useRef(options.getState);
245
+ getStateRef.current = options.getState;
246
+ const selectorsRef = useRef(options.selectors);
247
+ selectorsRef.current = options.selectors;
248
+ const actionsRef = useRef(options.actions);
249
+ actionsRef.current = options.actions;
250
+ const stateUri = `state://${name}`;
251
+ useEffect(
252
+ () => dynamicRegistry.registerResource({
253
+ uri: stateUri,
241
254
  name: `${name}-state`,
242
255
  description: `Full state of ${name} store`,
243
256
  mimeType: "application/json",
244
- read: readState
245
- });
246
- const unsubscribe = subscribe(() => {
247
- dynamicRegistry.updateResourceRead(`state://${name}`, readState);
248
- });
249
- return () => {
250
- unregister();
251
- unsubscribe();
252
- };
253
- }, [dynamicRegistry, name, subscribe, readState]);
257
+ read: async () => jsonResource(stateUri, getStateRef.current())
258
+ }),
259
+ [dynamicRegistry, name, stateUri]
260
+ );
254
261
  useEffect(() => {
255
- if (!selectors) return;
256
- const cleanups = [];
257
- const selectorUris = [];
258
- for (const [key, selector] of Object.entries(selectors)) {
259
- if (!key || !VALID_NAME_RE.test(key)) {
260
- throw new Error(`useStoreResource: invalid selector key "${key}". Keys must match ${VALID_NAME_RE}.`);
261
- }
262
- const uri = `state://${name}/${key}`;
263
- const selectorRef = { current: selector };
264
- const readSelector = async () => ({
265
- contents: [
266
- {
267
- uri,
268
- mimeType: "application/json",
269
- text: JSON.stringify(selectorRef.current(getStateRef.current()) ?? null)
270
- }
271
- ]
262
+ const cleanups = parseKeys(selectorKeys).map((key) => {
263
+ const uri = `${stateUri}/${key}`;
264
+ return dynamicRegistry.registerResource({
265
+ uri,
266
+ name: `${name}-${key}`,
267
+ description: `Selector "${key}" from ${name} store`,
268
+ mimeType: "application/json",
269
+ read: async () => {
270
+ const selector = selectorsRef.current?.[key];
271
+ return jsonResource(uri, selector ? selector(getStateRef.current()) : void 0);
272
+ }
272
273
  });
273
- selectorUris.push({ uri, readSelector });
274
- cleanups.push(
275
- dynamicRegistry.registerResource({
276
- uri,
277
- name: `${name}-${key}`,
278
- description: `Selector "${key}" from ${name} store`,
279
- mimeType: "application/json",
280
- read: readSelector
281
- })
282
- );
283
- }
284
- const unsubscribe = subscribe(() => {
285
- for (const { uri, readSelector } of selectorUris) {
286
- dynamicRegistry.updateResourceRead(uri, readSelector);
287
- }
288
274
  });
289
- cleanups.push(unsubscribe);
290
275
  return () => {
291
276
  cleanups.forEach((fn) => {
292
277
  fn();
293
278
  });
294
279
  };
295
- }, [dynamicRegistry, name, selectors, subscribe]);
280
+ }, [dynamicRegistry, name, stateUri, selectorKeys]);
281
+ useEffect(() => {
282
+ const uris = [stateUri, ...parseKeys(selectorKeys).map((key) => `${stateUri}/${key}`)];
283
+ return subscribe(() => {
284
+ for (const uri of uris) {
285
+ const resource = dynamicRegistry.findResource(uri);
286
+ if (resource) dynamicRegistry.updateResourceRead(uri, resource.read);
287
+ }
288
+ });
289
+ }, [dynamicRegistry, stateUri, selectorKeys, subscribe]);
296
290
  useEffect(() => {
297
- if (!actions) return;
298
- const cleanups = [];
299
- for (const [key, action] of Object.entries(actions)) {
300
- const toolName = `${name}_${key}`;
291
+ const cleanups = parseKeys(actionKeys).map((key) => {
301
292
  const execute = async (args) => {
293
+ const action = actionsRef.current?.[key];
294
+ if (!action) {
295
+ return { isError: true, content: [{ type: "text", text: `Action "${key}" is no longer available` }] };
296
+ }
302
297
  const argsArray = args["args"];
303
298
  const result = await (Array.isArray(argsArray) ? action(...argsArray) : action(args));
304
299
  return {
305
300
  content: [{ type: "text", text: JSON.stringify({ success: true, result }) }]
306
301
  };
307
302
  };
308
- cleanups.push(
309
- dynamicRegistry.registerTool({
310
- name: toolName,
311
- description: `Action "${key}" on ${name} store`,
312
- inputSchema: {
313
- type: "object",
314
- properties: {
315
- args: { type: "array", description: "Arguments to pass to the action" }
316
- }
317
- },
318
- execute
319
- })
320
- );
321
- }
303
+ return dynamicRegistry.registerTool({
304
+ name: `${name}_${key}`,
305
+ description: `Action "${key}" on ${name} store`,
306
+ inputSchema: {
307
+ type: "object",
308
+ properties: {
309
+ args: { type: "array", description: "Arguments to pass to the action" }
310
+ }
311
+ },
312
+ execute
313
+ });
314
+ });
322
315
  return () => {
323
316
  cleanups.forEach((fn) => {
324
317
  fn();
325
318
  });
326
319
  };
327
- }, [dynamicRegistry, name, actions]);
320
+ }, [dynamicRegistry, name, actionKeys]);
328
321
  }
329
322
 
330
323
  // libs/react/src/state/useReduxResource.ts
@@ -342,10 +335,12 @@ function useReduxResource(options) {
342
335
  }
343
336
  return wrapped;
344
337
  }, [actions, store]);
338
+ const getState = useMemo(() => store.getState.bind(store), [store]);
339
+ const subscribe = useMemo(() => store.subscribe.bind(store), [store]);
345
340
  useStoreResource({
346
341
  name,
347
- getState: store.getState.bind(store),
348
- subscribe: store.subscribe.bind(store),
342
+ getState,
343
+ subscribe,
349
344
  selectors,
350
345
  actions: wrappedActions,
351
346
  server
@@ -2,14 +2,29 @@
2
2
  * useDynamicTool — registers an MCP tool on mount, unregisters on unmount.
3
3
  *
4
4
  * Uses useRef for the execute function to avoid stale closures.
5
- * The tool appears in useListTools and can be called by agents.
5
+ * The provider registers the tool with the server as a real tool, so it
6
+ * runs through the server's flows (hooks, authorities, `availableWhen`),
7
+ * appears in useListTools and can be called by agents, WebMCP included.
6
8
  *
7
9
  * Supports both JSON Schema and zod-based schemas. When a zod schema
8
10
  * is provided, input is validated before reaching the execute callback.
9
11
  */
10
12
  import type { z } from '@frontmcp/lazy-zod';
11
13
  import type { CallToolResult } from '@frontmcp/sdk';
12
- export interface UseDynamicToolSchemaOptions<S extends z.ZodObject<z.ZodRawShape>> {
14
+ import type { DynamicToolDef } from '../types';
15
+ /** Options every dynamic tool takes, whichever way its schema is given. */
16
+ interface UseDynamicToolCommonOptions {
17
+ /** MCP behavioral hints, e.g. `{ readOnlyHint: true }`. */
18
+ annotations?: DynamicToolDef['annotations'];
19
+ /** Where the tool is offered, e.g. `{ surface: ['webmcp'] }` for in-browser agents only. */
20
+ availableWhen?: DynamicToolDef['availableWhen'];
21
+ /**
22
+ * Id of the server app the tool joins. Needed only for a server with more than one local app;
23
+ * defaults to the provider's `dynamicToolApps` entry for the server.
24
+ */
25
+ app?: string;
26
+ }
27
+ export interface UseDynamicToolSchemaOptions<S extends z.ZodObject<z.ZodRawShape>> extends UseDynamicToolCommonOptions {
13
28
  name: string;
14
29
  description: string;
15
30
  /** Zod schema for type-safe input validation. */
@@ -22,7 +37,7 @@ export interface UseDynamicToolSchemaOptions<S extends z.ZodObject<z.ZodRawShape
22
37
  /** Target a specific named server from the ServerRegistry. */
23
38
  server?: string;
24
39
  }
25
- export interface UseDynamicToolJsonSchemaOptions {
40
+ export interface UseDynamicToolJsonSchemaOptions extends UseDynamicToolCommonOptions {
26
41
  name: string;
27
42
  description: string;
28
43
  schema?: never;
@@ -36,4 +51,5 @@ export interface UseDynamicToolJsonSchemaOptions {
36
51
  }
37
52
  export type UseDynamicToolOptions<S extends z.ZodObject<z.ZodRawShape> = z.ZodObject<z.ZodRawShape>> = UseDynamicToolSchemaOptions<S> | UseDynamicToolJsonSchemaOptions;
38
53
  export declare function useDynamicTool<S extends z.ZodObject<z.ZodRawShape>>(options: UseDynamicToolOptions<S>): void;
54
+ export {};
39
55
  //# sourceMappingURL=useDynamicTool.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useDynamicTool.d.ts","sourceRoot":"","sources":["../../src/hooks/useDynamicTool.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAOpD,MAAM,WAAW,2BAA2B,CAAC,CAAC,SAAS,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC;IAC/E,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,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