@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.
- package/README.md +2 -0
- package/ai/index.js +90 -17
- package/api/api.types.d.ts +29 -0
- package/api/api.types.d.ts.map +1 -1
- package/api/index.d.ts +1 -1
- package/api/index.d.ts.map +1 -1
- package/api/index.js +136 -33
- package/api/parseOpenApiSpec.d.ts.map +1 -1
- package/api/useApiClient.d.ts +5 -0
- package/api/useApiClient.d.ts.map +1 -1
- package/components/ToolForm.d.ts +1 -1
- package/components/ToolForm.d.ts.map +1 -1
- package/esm/ai/index.mjs +93 -20
- package/esm/api/index.mjs +136 -33
- package/esm/index.mjs +298 -85
- package/esm/package.json +6 -6
- package/esm/router/index.mjs +22 -4
- package/esm/state/index.mjs +113 -98
- package/hooks/useDynamicTool.d.ts +19 -3
- package/hooks/useDynamicTool.d.ts.map +1 -1
- package/hooks/useServer.d.ts +2 -2
- package/hooks/useServer.d.ts.map +1 -1
- package/index.d.ts +1 -0
- package/index.d.ts.map +1 -1
- package/index.js +293 -80
- package/package.json +6 -6
- package/provider/FrontMcpContext.d.ts +2 -1
- package/provider/FrontMcpContext.d.ts.map +1 -1
- package/provider/FrontMcpProvider.d.ts +12 -6
- package/provider/FrontMcpProvider.d.ts.map +1 -1
- package/registry/DynamicRegistry.d.ts +12 -6
- package/registry/DynamicRegistry.d.ts.map +1 -1
- package/registry/ServerRegistry.d.ts +14 -3
- package/registry/ServerRegistry.d.ts.map +1 -1
- package/registry/bindDynamicTools.d.ts +29 -0
- package/registry/bindDynamicTools.d.ts.map +1 -0
- package/registry/createWrappedServer.d.ts +6 -5
- package/registry/createWrappedServer.d.ts.map +1 -1
- package/router/index.js +22 -4
- package/router/router.entries.d.ts +9 -5
- package/router/router.entries.d.ts.map +1 -1
- package/state/index.js +112 -97
- package/state/useReduxResource.d.ts.map +1 -1
- package/state/useStoreResource.d.ts +6 -0
- package/state/useStoreResource.d.ts.map +1 -1
- package/types.d.ts +10 -1
- package/types.d.ts.map +1 -1
package/esm/index.mjs
CHANGED
|
@@ -26,14 +26,26 @@ import {
|
|
|
26
26
|
Plugin,
|
|
27
27
|
FrontMcpPlugin
|
|
28
28
|
} from "@frontmcp/sdk";
|
|
29
|
+
import { z as z2 } from "@frontmcp/sdk";
|
|
29
30
|
import { ToolContext, ResourceContext, PromptContext, ExecutionContextBase } from "@frontmcp/sdk";
|
|
30
31
|
|
|
31
32
|
// libs/react/src/registry/ServerRegistry.ts
|
|
33
|
+
function sameListing(a, b) {
|
|
34
|
+
if (a === b) return true;
|
|
35
|
+
if (a.length !== b.length) return false;
|
|
36
|
+
try {
|
|
37
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
38
|
+
} catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
32
42
|
var ServerRegistry = class {
|
|
33
43
|
entries = /* @__PURE__ */ new Map();
|
|
34
44
|
listeners = /* @__PURE__ */ new Set();
|
|
45
|
+
toolListWatches = /* @__PURE__ */ new Map();
|
|
35
46
|
version = 0;
|
|
36
47
|
register(name, server) {
|
|
48
|
+
this.stopToolListWatch(name);
|
|
37
49
|
this.entries.set(name, {
|
|
38
50
|
server,
|
|
39
51
|
client: null,
|
|
@@ -47,6 +59,7 @@ var ServerRegistry = class {
|
|
|
47
59
|
this.notify();
|
|
48
60
|
}
|
|
49
61
|
unregister(name) {
|
|
62
|
+
this.stopToolListWatch(name);
|
|
50
63
|
this.entries.delete(name);
|
|
51
64
|
this.notify();
|
|
52
65
|
}
|
|
@@ -85,6 +98,7 @@ var ServerRegistry = class {
|
|
|
85
98
|
};
|
|
86
99
|
this.entries.set(name, connected);
|
|
87
100
|
this.notify();
|
|
101
|
+
this.watchToolList(name, client);
|
|
88
102
|
return client;
|
|
89
103
|
} catch (err) {
|
|
90
104
|
const error = err instanceof Error ? err : new Error(String(err));
|
|
@@ -96,6 +110,32 @@ var ServerRegistry = class {
|
|
|
96
110
|
async connectAll() {
|
|
97
111
|
await Promise.all(this.list().map((name) => this.connect(name)));
|
|
98
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Keep `name`'s tool list current: re-list through `client` now, and whenever the server
|
|
115
|
+
* announces `notifications/tools/list_changed` (a dynamic tool registered or removed, say). The
|
|
116
|
+
* first listing catches a change made before the watch began. Only the newest listing is applied,
|
|
117
|
+
* and only while `client` is still the entry's client.
|
|
118
|
+
*/
|
|
119
|
+
watchToolList(name, client) {
|
|
120
|
+
this.stopToolListWatch(name);
|
|
121
|
+
if (typeof client.onNotification !== "function") return;
|
|
122
|
+
let latest = 0;
|
|
123
|
+
const relist = () => {
|
|
124
|
+
const listing = ++latest;
|
|
125
|
+
client.listTools().then((tools) => {
|
|
126
|
+
const entry = this.entries.get(name);
|
|
127
|
+
if (listing !== latest || !entry || entry.client !== client) return;
|
|
128
|
+
if (sameListing(entry.tools, tools)) return;
|
|
129
|
+
this.update(name, { tools });
|
|
130
|
+
}).catch(() => {
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
const stop = client.onNotification((notification) => {
|
|
134
|
+
if (notification.method === "notifications/tools/list_changed") relist();
|
|
135
|
+
});
|
|
136
|
+
this.toolListWatches.set(name, stop);
|
|
137
|
+
relist();
|
|
138
|
+
}
|
|
99
139
|
update(name, partial) {
|
|
100
140
|
const entry = this.entries.get(name);
|
|
101
141
|
if (entry) {
|
|
@@ -104,6 +144,7 @@ var ServerRegistry = class {
|
|
|
104
144
|
}
|
|
105
145
|
}
|
|
106
146
|
clear() {
|
|
147
|
+
for (const name of [...this.toolListWatches.keys()]) this.stopToolListWatch(name);
|
|
107
148
|
this.entries.clear();
|
|
108
149
|
this.notify();
|
|
109
150
|
}
|
|
@@ -116,12 +157,23 @@ var ServerRegistry = class {
|
|
|
116
157
|
getVersion() {
|
|
117
158
|
return this.version;
|
|
118
159
|
}
|
|
160
|
+
stopToolListWatch(name) {
|
|
161
|
+
const stop = this.toolListWatches.get(name);
|
|
162
|
+
if (!stop) return;
|
|
163
|
+
this.toolListWatches.delete(name);
|
|
164
|
+
stop();
|
|
165
|
+
}
|
|
119
166
|
notify() {
|
|
120
167
|
this.version++;
|
|
121
168
|
this.listeners.forEach((l) => l());
|
|
122
169
|
}
|
|
123
170
|
};
|
|
124
|
-
var
|
|
171
|
+
var REGISTRY_KEY = /* @__PURE__ */ Symbol.for("@frontmcp/react/serverRegistry");
|
|
172
|
+
function getOrCreateRegistry() {
|
|
173
|
+
const host = globalThis;
|
|
174
|
+
return host[REGISTRY_KEY] ??= new ServerRegistry();
|
|
175
|
+
}
|
|
176
|
+
var serverRegistry = getOrCreateRegistry();
|
|
125
177
|
|
|
126
178
|
// libs/react/src/registry/DynamicRegistry.ts
|
|
127
179
|
var DynamicRegistry = class {
|
|
@@ -130,22 +182,23 @@ var DynamicRegistry = class {
|
|
|
130
182
|
toolRefCounts = /* @__PURE__ */ new Map();
|
|
131
183
|
resourceRefCounts = /* @__PURE__ */ new Map();
|
|
132
184
|
listeners = /* @__PURE__ */ new Set();
|
|
185
|
+
resourceListeners = /* @__PURE__ */ new Set();
|
|
133
186
|
version = 0;
|
|
134
187
|
/**
|
|
135
188
|
* Register a dynamic tool. Returns an unregister function
|
|
136
189
|
* suitable for useEffect cleanup.
|
|
137
190
|
*
|
|
138
191
|
* Multiple registrations of the same name are ref-counted:
|
|
139
|
-
* subsequent registrations
|
|
140
|
-
*
|
|
192
|
+
* subsequent registrations replace the definition (and notify, so
|
|
193
|
+
* the server registration follows the new description, schema or
|
|
194
|
+
* availability) but the tool is only removed when every registrant
|
|
195
|
+
* has unregistered.
|
|
141
196
|
*/
|
|
142
197
|
registerTool(def) {
|
|
143
198
|
const existing = this.toolRefCounts.get(def.name) ?? 0;
|
|
144
199
|
this.toolRefCounts.set(def.name, existing + 1);
|
|
145
200
|
this.tools.set(def.name, def);
|
|
146
|
-
|
|
147
|
-
this.notify();
|
|
148
|
-
}
|
|
201
|
+
this.notify();
|
|
149
202
|
let called = false;
|
|
150
203
|
return () => {
|
|
151
204
|
if (called) return;
|
|
@@ -208,8 +261,18 @@ var DynamicRegistry = class {
|
|
|
208
261
|
if (existing) {
|
|
209
262
|
existing.read = read;
|
|
210
263
|
this.notify();
|
|
264
|
+
this.resourceListeners.forEach((l) => {
|
|
265
|
+
l(uri);
|
|
266
|
+
});
|
|
211
267
|
}
|
|
212
268
|
}
|
|
269
|
+
/** Listen for changes to a dynamic resource's content (the in-page `resources/updated`). */
|
|
270
|
+
onResourceUpdated(listener) {
|
|
271
|
+
this.resourceListeners.add(listener);
|
|
272
|
+
return () => {
|
|
273
|
+
this.resourceListeners.delete(listener);
|
|
274
|
+
};
|
|
275
|
+
}
|
|
213
276
|
getTools() {
|
|
214
277
|
return [...this.tools.values()];
|
|
215
278
|
}
|
|
@@ -255,16 +318,6 @@ var DynamicRegistry = class {
|
|
|
255
318
|
|
|
256
319
|
// libs/react/src/registry/createWrappedServer.ts
|
|
257
320
|
function patchClientWithDynamic(client, dynamicRegistry) {
|
|
258
|
-
if (typeof client.callTool === "function") {
|
|
259
|
-
const originalCallTool = client.callTool.bind(client);
|
|
260
|
-
client.callTool = async (name, args) => {
|
|
261
|
-
const dynamicTool = dynamicRegistry.findTool(name);
|
|
262
|
-
if (dynamicTool) {
|
|
263
|
-
return dynamicTool.execute(args ?? {});
|
|
264
|
-
}
|
|
265
|
-
return originalCallTool(name, args);
|
|
266
|
-
};
|
|
267
|
-
}
|
|
268
321
|
if (typeof client.readResource === "function") {
|
|
269
322
|
const originalReadResource = client.readResource.bind(client);
|
|
270
323
|
client.readResource = async (uri) => {
|
|
@@ -275,6 +328,31 @@ function patchClientWithDynamic(client, dynamicRegistry) {
|
|
|
275
328
|
return originalReadResource(uri);
|
|
276
329
|
};
|
|
277
330
|
}
|
|
331
|
+
if (typeof client.subscribeResource === "function") {
|
|
332
|
+
const originalSubscribe = client.subscribeResource.bind(client);
|
|
333
|
+
client.subscribeResource = async (uri) => {
|
|
334
|
+
if (dynamicRegistry.hasResource(uri)) return;
|
|
335
|
+
return originalSubscribe(uri);
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
if (typeof client.unsubscribeResource === "function") {
|
|
339
|
+
const originalUnsubscribe = client.unsubscribeResource.bind(client);
|
|
340
|
+
client.unsubscribeResource = async (uri) => {
|
|
341
|
+
if (dynamicRegistry.hasResource(uri)) return;
|
|
342
|
+
return originalUnsubscribe(uri);
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
if (typeof client.onResourceUpdated === "function") {
|
|
346
|
+
const originalOnUpdated = client.onResourceUpdated.bind(client);
|
|
347
|
+
client.onResourceUpdated = (handler) => {
|
|
348
|
+
const offServer = originalOnUpdated(handler);
|
|
349
|
+
const offDynamic = dynamicRegistry.onResourceUpdated(handler);
|
|
350
|
+
return () => {
|
|
351
|
+
offServer();
|
|
352
|
+
offDynamic();
|
|
353
|
+
};
|
|
354
|
+
};
|
|
355
|
+
}
|
|
278
356
|
return client;
|
|
279
357
|
}
|
|
280
358
|
function createWrappedServer(base, dynamicRegistry) {
|
|
@@ -283,28 +361,14 @@ function createWrappedServer(base, dynamicRegistry) {
|
|
|
283
361
|
return base.ready;
|
|
284
362
|
},
|
|
285
363
|
async listTools(options) {
|
|
286
|
-
|
|
287
|
-
const dynamicTools = dynamicRegistry.getTools();
|
|
288
|
-
if (dynamicTools.length === 0) return baseResult;
|
|
289
|
-
const dynamicNames = new Set(dynamicTools.map((t) => t.name));
|
|
290
|
-
const baseTools = (baseResult.tools ?? []).filter((t) => !dynamicNames.has(t.name));
|
|
291
|
-
const mergedTools = [
|
|
292
|
-
...baseTools,
|
|
293
|
-
...dynamicTools.map((t) => ({
|
|
294
|
-
name: t.name,
|
|
295
|
-
description: t.description,
|
|
296
|
-
inputSchema: t.inputSchema
|
|
297
|
-
}))
|
|
298
|
-
];
|
|
299
|
-
return { ...baseResult, tools: mergedTools };
|
|
364
|
+
return base.listTools(options);
|
|
300
365
|
},
|
|
301
366
|
async callTool(name, args, options) {
|
|
302
|
-
const dynamicTool = dynamicRegistry.findTool(name);
|
|
303
|
-
if (dynamicTool) {
|
|
304
|
-
return dynamicTool.execute(args ?? {});
|
|
305
|
-
}
|
|
306
367
|
return base.callTool(name, args, options);
|
|
307
368
|
},
|
|
369
|
+
async registerTool(definition) {
|
|
370
|
+
return base.registerTool(definition);
|
|
371
|
+
},
|
|
308
372
|
async listResources(options) {
|
|
309
373
|
const baseResult = await base.listResources(options);
|
|
310
374
|
const dynamicResources = dynamicRegistry.getResources();
|
|
@@ -313,9 +377,10 @@ function createWrappedServer(base, dynamicRegistry) {
|
|
|
313
377
|
const baseResources = (baseResult.resources ?? []).filter(
|
|
314
378
|
(r) => !dynamicUris.has(r.uri)
|
|
315
379
|
);
|
|
380
|
+
const laterPage = options?.cursor !== void 0;
|
|
316
381
|
const mergedResources = [
|
|
317
382
|
...baseResources,
|
|
318
|
-
...dynamicResources.map((r) => ({
|
|
383
|
+
...(laterPage ? [] : dynamicResources).map((r) => ({
|
|
319
384
|
uri: r.uri,
|
|
320
385
|
name: r.name ?? r.uri,
|
|
321
386
|
description: r.description,
|
|
@@ -419,18 +484,130 @@ function extractName(uri) {
|
|
|
419
484
|
}
|
|
420
485
|
|
|
421
486
|
// libs/react/src/provider/FrontMcpContext.ts
|
|
422
|
-
var
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
487
|
+
var CONTEXT_KEY = /* @__PURE__ */ Symbol.for("@frontmcp/react/FrontMcpContext");
|
|
488
|
+
function getOrCreateContext() {
|
|
489
|
+
const host = globalThis;
|
|
490
|
+
const existing = host[CONTEXT_KEY];
|
|
491
|
+
if (existing) return existing;
|
|
492
|
+
const defaultDynamicRegistry = new DynamicRegistry();
|
|
493
|
+
const created = createContext({
|
|
494
|
+
name: "default",
|
|
495
|
+
registry: new ComponentRegistry(),
|
|
496
|
+
dynamicRegistry: defaultDynamicRegistry,
|
|
497
|
+
getDynamicRegistry: () => defaultDynamicRegistry,
|
|
498
|
+
connect: async () => {
|
|
499
|
+
}
|
|
500
|
+
});
|
|
501
|
+
host[CONTEXT_KEY] = created;
|
|
502
|
+
return created;
|
|
503
|
+
}
|
|
504
|
+
var FrontMcpContext = getOrCreateContext();
|
|
431
505
|
|
|
432
506
|
// libs/react/src/provider/FrontMcpProvider.tsx
|
|
433
|
-
import React, { useCallback, useEffect as useEffect2,
|
|
507
|
+
import React, { useCallback, useEffect as useEffect2, useMemo, useRef } from "react";
|
|
508
|
+
|
|
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
|
+
}
|
|
434
611
|
|
|
435
612
|
// libs/react/src/state/useStoreRegistration.ts
|
|
436
613
|
import { useEffect } from "react";
|
|
@@ -542,6 +719,7 @@ function FrontMcpProvider({
|
|
|
542
719
|
servers,
|
|
543
720
|
components,
|
|
544
721
|
stores,
|
|
722
|
+
dynamicToolApps,
|
|
545
723
|
autoConnect = true,
|
|
546
724
|
children,
|
|
547
725
|
onConnected,
|
|
@@ -550,6 +728,8 @@ function FrontMcpProvider({
|
|
|
550
728
|
const resolvedName = nameProp ?? "default";
|
|
551
729
|
const mountedRef = useRef(true);
|
|
552
730
|
const clientRef = useRef(null);
|
|
731
|
+
const onErrorRef = useRef(onError);
|
|
732
|
+
onErrorRef.current = onError;
|
|
553
733
|
const registry = useMemo(() => {
|
|
554
734
|
const reg = new ComponentRegistry();
|
|
555
735
|
if (components) {
|
|
@@ -573,16 +753,36 @@ function FrontMcpProvider({
|
|
|
573
753
|
const dynamicRegistry = useMemo(() => getDynamicRegistry(resolvedName), [getDynamicRegistry, resolvedName]);
|
|
574
754
|
useStoreRegistration(stores ?? [], dynamicRegistry);
|
|
575
755
|
const wrappedServer = useMemo(() => createWrappedServer(server, dynamicRegistry), [server, dynamicRegistry]);
|
|
756
|
+
const reportToolError = useCallback((error, toolName) => {
|
|
757
|
+
const report = onErrorRef.current;
|
|
758
|
+
if (report) report(new Error(`Dynamic tool "${toolName}" was not registered: ${error.message}`));
|
|
759
|
+
else console.warn(`[frontmcp] dynamic tool "${toolName}" was not registered: ${error.message}`);
|
|
760
|
+
}, []);
|
|
761
|
+
const dynamicToolAppsKey = JSON.stringify(dynamicToolApps ?? {});
|
|
762
|
+
const toolApps = useMemo(
|
|
763
|
+
() => JSON.parse(dynamicToolAppsKey),
|
|
764
|
+
[dynamicToolAppsKey]
|
|
765
|
+
);
|
|
766
|
+
const primaryToolApp = toolApps[resolvedName];
|
|
767
|
+
useEffect2(
|
|
768
|
+
() => bindDynamicTools(dynamicRegistry, server, { onError: reportToolError, app: primaryToolApp }),
|
|
769
|
+
[dynamicRegistry, server, reportToolError, primaryToolApp]
|
|
770
|
+
);
|
|
576
771
|
useEffect2(() => {
|
|
772
|
+
const unbinds = [];
|
|
577
773
|
serverRegistry.register(resolvedName, wrappedServer);
|
|
578
774
|
if (servers) {
|
|
579
775
|
for (const [sName, srv] of Object.entries(servers)) {
|
|
580
776
|
const srvRegistry = getDynamicRegistry(sName);
|
|
581
777
|
const wrappedSrv = createWrappedServer(srv, srvRegistry);
|
|
582
778
|
serverRegistry.register(sName, wrappedSrv);
|
|
779
|
+
unbinds.push(bindDynamicTools(srvRegistry, srv, { onError: reportToolError, app: toolApps[sName] }));
|
|
583
780
|
}
|
|
584
781
|
}
|
|
585
782
|
return () => {
|
|
783
|
+
unbinds.forEach((unbind) => {
|
|
784
|
+
unbind();
|
|
785
|
+
});
|
|
586
786
|
serverRegistry.unregister(resolvedName);
|
|
587
787
|
if (servers) {
|
|
588
788
|
for (const sName of Object.keys(servers)) {
|
|
@@ -595,19 +795,18 @@ function FrontMcpProvider({
|
|
|
595
795
|
}
|
|
596
796
|
}
|
|
597
797
|
};
|
|
598
|
-
}, [resolvedName, wrappedServer, servers, getDynamicRegistry]);
|
|
798
|
+
}, [resolvedName, wrappedServer, servers, getDynamicRegistry, reportToolError, toolApps]);
|
|
599
799
|
useEffect2(() => {
|
|
600
800
|
const refreshServerEntry = (name) => {
|
|
601
801
|
const entry = serverRegistry.get(name);
|
|
602
802
|
if (!entry || !entry.client) return;
|
|
603
803
|
const srv = entry.server;
|
|
604
804
|
if (!srv) return;
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
});
|
|
805
|
+
srv.listResources().then((resourcesResult) => {
|
|
806
|
+
const resources = resourcesResult.resources ?? [];
|
|
807
|
+
const current = serverRegistry.get(name);
|
|
808
|
+
if (mountedRef.current && current && !sameListing(current.resources, resources)) {
|
|
809
|
+
serverRegistry.update(name, { resources });
|
|
611
810
|
}
|
|
612
811
|
}).catch(() => {
|
|
613
812
|
});
|
|
@@ -650,11 +849,6 @@ function FrontMcpProvider({
|
|
|
650
849
|
safeList(() => client.listPrompts(), { prompts: [] })
|
|
651
850
|
]);
|
|
652
851
|
if (mountedRef.current) {
|
|
653
|
-
const dynamicTools = dynamicRegistry.getTools().map((t) => ({
|
|
654
|
-
name: t.name,
|
|
655
|
-
description: t.description,
|
|
656
|
-
inputSchema: t.inputSchema
|
|
657
|
-
}));
|
|
658
852
|
const dynamicResources = dynamicRegistry.getResources().map((r) => ({
|
|
659
853
|
uri: r.uri,
|
|
660
854
|
name: r.name,
|
|
@@ -662,10 +856,6 @@ function FrontMcpProvider({
|
|
|
662
856
|
mimeType: r.mimeType
|
|
663
857
|
}));
|
|
664
858
|
const baseTools = toolsResult;
|
|
665
|
-
const dynamicToolNames = new Set(dynamicTools.map((t) => t.name));
|
|
666
|
-
const filteredBaseTools = (Array.isArray(baseTools) ? baseTools : []).filter(
|
|
667
|
-
(t) => !dynamicToolNames.has(t.name)
|
|
668
|
-
);
|
|
669
859
|
const baseResources = resourcesResult.resources ?? [];
|
|
670
860
|
const dynamicResourceUris = new Set(dynamicResources.map((r) => r.uri));
|
|
671
861
|
const filteredBaseResources = baseResources.filter((r) => !dynamicResourceUris.has(r.uri));
|
|
@@ -673,11 +863,12 @@ function FrontMcpProvider({
|
|
|
673
863
|
client,
|
|
674
864
|
status: "connected",
|
|
675
865
|
error: null,
|
|
676
|
-
tools: [
|
|
866
|
+
tools: Array.isArray(baseTools) ? baseTools : [],
|
|
677
867
|
resources: [...filteredBaseResources, ...dynamicResources],
|
|
678
868
|
resourceTemplates: templatesResult.resourceTemplates ?? [],
|
|
679
869
|
prompts: promptsResult.prompts ?? []
|
|
680
870
|
});
|
|
871
|
+
serverRegistry.watchToolList(resolvedName, client);
|
|
681
872
|
onConnected?.(client);
|
|
682
873
|
}
|
|
683
874
|
if (servers) {
|
|
@@ -718,21 +909,23 @@ function FrontMcpProvider({
|
|
|
718
909
|
}
|
|
719
910
|
|
|
720
911
|
// libs/react/src/hooks/useResolvedServer.ts
|
|
721
|
-
import { useContext } from "react";
|
|
912
|
+
import { useContext as useContext2 } from "react";
|
|
722
913
|
|
|
723
914
|
// libs/react/src/hooks/useServer.ts
|
|
724
|
-
import {
|
|
915
|
+
import { useCallback as useCallback2, useContext, useSyncExternalStore } from "react";
|
|
725
916
|
function useServer(name) {
|
|
917
|
+
const ctx = useContext(FrontMcpContext);
|
|
918
|
+
const serverName = name ?? ctx.name;
|
|
726
919
|
const subscribe = useCallback2((cb) => serverRegistry.subscribe(cb), []);
|
|
727
920
|
const getSnapshot = useCallback2(() => {
|
|
728
|
-
return serverRegistry.get(
|
|
729
|
-
}, [
|
|
921
|
+
return serverRegistry.get(serverName);
|
|
922
|
+
}, [serverName]);
|
|
730
923
|
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
731
924
|
}
|
|
732
925
|
|
|
733
926
|
// libs/react/src/hooks/useResolvedServer.ts
|
|
734
927
|
function useResolvedServer(serverName) {
|
|
735
|
-
const ctx =
|
|
928
|
+
const ctx = useContext2(FrontMcpContext);
|
|
736
929
|
const resolvedName = serverName ?? ctx.name;
|
|
737
930
|
const entry = useServer(resolvedName);
|
|
738
931
|
return { entry, name: resolvedName, registry: ctx.registry, connect: ctx.connect };
|
|
@@ -980,7 +1173,7 @@ function useStoreResource(uri, options) {
|
|
|
980
1173
|
}
|
|
981
1174
|
|
|
982
1175
|
// libs/react/src/hooks/useDynamicTool.ts
|
|
983
|
-
import { useContext as
|
|
1176
|
+
import { useContext as useContext3, useEffect as useEffect6, useMemo as useMemo2, useRef as useRef3 } from "react";
|
|
984
1177
|
|
|
985
1178
|
// libs/react/src/utils/zodToJsonSchema.ts
|
|
986
1179
|
import { forceMaterialize, toJSONSchema } from "@frontmcp/lazy-zod";
|
|
@@ -989,16 +1182,24 @@ function zodToJsonSchema(schema) {
|
|
|
989
1182
|
}
|
|
990
1183
|
|
|
991
1184
|
// libs/react/src/hooks/useDynamicTool.ts
|
|
1185
|
+
function stableKey(value) {
|
|
1186
|
+
try {
|
|
1187
|
+
return JSON.stringify(value) ?? "";
|
|
1188
|
+
} catch {
|
|
1189
|
+
return String(Math.random());
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
992
1192
|
function useDynamicTool(options) {
|
|
993
|
-
const { name, description, enabled = true } = options;
|
|
994
|
-
const { getDynamicRegistry } =
|
|
1193
|
+
const { name, description, app, enabled = true } = options;
|
|
1194
|
+
const { getDynamicRegistry } = useContext3(FrontMcpContext);
|
|
995
1195
|
const dynamicRegistry = getDynamicRegistry(options.server);
|
|
996
|
-
const
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1196
|
+
const computedInputSchema = "schema" in options && options.schema ? zodToJsonSchema(options.schema) : options.inputSchema;
|
|
1197
|
+
const inputSchemaKey = stableKey(computedInputSchema);
|
|
1198
|
+
const resolvedInputSchema = useMemo2(() => computedInputSchema, [inputSchemaKey]);
|
|
1199
|
+
const annotationsKey = stableKey(options.annotations ?? null);
|
|
1200
|
+
const annotations = useMemo2(() => options.annotations, [annotationsKey]);
|
|
1201
|
+
const availableWhenKey = stableKey(options.availableWhen ?? null);
|
|
1202
|
+
const availableWhen = useMemo2(() => options.availableWhen, [availableWhenKey]);
|
|
1002
1203
|
const executeRef = useRef3(options.execute);
|
|
1003
1204
|
executeRef.current = options.execute;
|
|
1004
1205
|
const schemaRef = useRef3("schema" in options && options.schema ? options.schema : null);
|
|
@@ -1034,17 +1235,20 @@ function useDynamicTool(options) {
|
|
|
1034
1235
|
name,
|
|
1035
1236
|
description,
|
|
1036
1237
|
inputSchema: resolvedInputSchema,
|
|
1037
|
-
execute: stableExecute
|
|
1238
|
+
execute: stableExecute,
|
|
1239
|
+
...annotations && { annotations },
|
|
1240
|
+
...availableWhen && { availableWhen },
|
|
1241
|
+
...app && { app }
|
|
1038
1242
|
});
|
|
1039
1243
|
return unregister;
|
|
1040
|
-
}, [dynamicRegistry, name, description, resolvedInputSchema, enabled]);
|
|
1244
|
+
}, [dynamicRegistry, name, description, resolvedInputSchema, annotations, availableWhen, app, enabled]);
|
|
1041
1245
|
}
|
|
1042
1246
|
|
|
1043
1247
|
// libs/react/src/hooks/useDynamicResource.ts
|
|
1044
|
-
import { useContext as
|
|
1248
|
+
import { useContext as useContext4, useEffect as useEffect7, useRef as useRef4 } from "react";
|
|
1045
1249
|
function useDynamicResource(options) {
|
|
1046
1250
|
const { uri, name, description, mimeType, read, enabled = true } = options;
|
|
1047
|
-
const { getDynamicRegistry } =
|
|
1251
|
+
const { getDynamicRegistry } = useContext4(FrontMcpContext);
|
|
1048
1252
|
const dynamicRegistry = getDynamicRegistry(options.server);
|
|
1049
1253
|
const readRef = useRef4(read);
|
|
1050
1254
|
readRef.current = read;
|
|
@@ -1240,7 +1444,7 @@ function readDomBySelector(selector) {
|
|
|
1240
1444
|
}
|
|
1241
1445
|
|
|
1242
1446
|
// libs/react/src/components/ToolForm.tsx
|
|
1243
|
-
import React3, {
|
|
1447
|
+
import React3, { useCallback as useCallback8, useState as useState5 } from "react";
|
|
1244
1448
|
function ToolForm({
|
|
1245
1449
|
tool: tool2,
|
|
1246
1450
|
onSubmit,
|
|
@@ -1251,12 +1455,19 @@ function ToolForm({
|
|
|
1251
1455
|
const properties = schema["properties"] ?? {};
|
|
1252
1456
|
const required = schema["required"] ?? [];
|
|
1253
1457
|
const [values, setValues] = useState5({});
|
|
1458
|
+
const initialValue = (key, prop) => {
|
|
1459
|
+
const options = prop["enum"];
|
|
1460
|
+
if (!options) return "";
|
|
1461
|
+
const fallback = prop["default"];
|
|
1462
|
+
if (typeof fallback === "string" && options.includes(fallback)) return fallback;
|
|
1463
|
+
return required.includes(key) ? options[0] ?? "" : "";
|
|
1464
|
+
};
|
|
1254
1465
|
const handleSubmit = useCallback8(
|
|
1255
1466
|
(e) => {
|
|
1256
1467
|
e.preventDefault();
|
|
1257
1468
|
const args = {};
|
|
1258
1469
|
for (const [key, prop] of Object.entries(properties)) {
|
|
1259
|
-
const raw = values[key] ??
|
|
1470
|
+
const raw = values[key] ?? initialValue(key, prop);
|
|
1260
1471
|
if (!required.includes(key) && raw === "") continue;
|
|
1261
1472
|
if (prop["type"] === "number" || prop["type"] === "integer") {
|
|
1262
1473
|
args[key] = Number(raw);
|
|
@@ -1280,7 +1491,7 @@ function ToolForm({
|
|
|
1280
1491
|
const isRequired = required.includes(key);
|
|
1281
1492
|
const enumValues = prop["enum"];
|
|
1282
1493
|
const fieldType = getFieldType(prop);
|
|
1283
|
-
const value = values[key] ??
|
|
1494
|
+
const value = values[key] ?? initialValue(key, prop);
|
|
1284
1495
|
if (renderField) {
|
|
1285
1496
|
return React3.createElement(
|
|
1286
1497
|
React3.Fragment,
|
|
@@ -1308,9 +1519,10 @@ function ToolForm({
|
|
|
1308
1519
|
"select",
|
|
1309
1520
|
{
|
|
1310
1521
|
id: `field-${key}`,
|
|
1311
|
-
value
|
|
1522
|
+
value,
|
|
1312
1523
|
onChange: (e) => handleChange(key, e.target.value)
|
|
1313
1524
|
},
|
|
1525
|
+
...isRequired ? [] : [React3.createElement("option", { key: "", value: "" }, "")],
|
|
1314
1526
|
...enumValues.map((v) => React3.createElement("option", { key: v, value: v }, v))
|
|
1315
1527
|
) : React3.createElement("input", {
|
|
1316
1528
|
id: `field-${key}`,
|
|
@@ -1810,5 +2022,6 @@ export {
|
|
|
1810
2022
|
useResolvedServer,
|
|
1811
2023
|
useServer,
|
|
1812
2024
|
useStoreResource,
|
|
1813
|
-
valtioStore
|
|
2025
|
+
valtioStore,
|
|
2026
|
+
z2 as z
|
|
1814
2027
|
};
|
package/esm/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontmcp/react",
|
|
3
|
-
"version": "1.
|
|
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.
|
|
90
|
-
"@frontmcp/sdk": "1.
|
|
91
|
-
"@frontmcp/utils": "1.
|
|
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.
|
|
106
|
-
"@frontmcp/utils": "1.
|
|
105
|
+
"@frontmcp/sdk": "1.9.0",
|
|
106
|
+
"@frontmcp/utils": "1.9.0"
|
|
107
107
|
},
|
|
108
108
|
"devDependencies": {
|
|
109
109
|
"typescript": "^5.9.3"
|