@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/README.md +2 -0
- package/ai/index.js +51 -5
- 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 +107 -24
- 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.map +1 -1
- package/esm/ai/index.mjs +51 -5
- package/esm/api/index.mjs +107 -24
- package/esm/index.mjs +208 -60
- package/esm/package.json +6 -6
- package/esm/state/index.mjs +84 -89
- package/hooks/useDynamicTool.d.ts +19 -3
- package/hooks/useDynamicTool.d.ts.map +1 -1
- package/index.js +207 -59
- package/package.json +6 -6
- package/provider/FrontMcpProvider.d.ts +12 -6
- package/provider/FrontMcpProvider.d.ts.map +1 -1
- package/registry/DynamicRegistry.d.ts +8 -5
- package/registry/DynamicRegistry.d.ts.map +1 -1
- package/registry/ServerRegistry.d.ts +11 -0
- 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/state/index.js +83 -88
- 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/api/index.mjs
CHANGED
|
@@ -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
|
|
69
|
-
*
|
|
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
|
-
|
|
76
|
-
this.notify();
|
|
77
|
-
}
|
|
77
|
+
this.notify();
|
|
78
78
|
let called = false;
|
|
79
79
|
return () => {
|
|
80
80
|
if (called) return;
|
|
@@ -252,29 +252,91 @@ function interpolatePath(path, params) {
|
|
|
252
252
|
return value != null ? encodeURIComponent(String(value)) : `{${key}}`;
|
|
253
253
|
});
|
|
254
254
|
}
|
|
255
|
+
function paramText(value) {
|
|
256
|
+
return typeof value === "object" && value !== null ? JSON.stringify(value) : String(value);
|
|
257
|
+
}
|
|
258
|
+
function isRecord(value) {
|
|
259
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
260
|
+
}
|
|
261
|
+
var QUERY_DELIMITERS = { spaceDelimited: "%20", pipeDelimited: "%7C" };
|
|
262
|
+
function serializedItems(value, explode, encode) {
|
|
263
|
+
if (Array.isArray(value)) return value.map((item) => encode(paramText(item)));
|
|
264
|
+
return Object.entries(value).flatMap(
|
|
265
|
+
([key, item]) => explode ? [`${encode(key)}=${encode(paramText(item))}`] : [encode(key), encode(paramText(item))]
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
function queryPairs(param, value) {
|
|
269
|
+
const style = param.style ?? "form";
|
|
270
|
+
const explode = param.explode ?? style === "form";
|
|
271
|
+
const name = encodeURIComponent(param.name);
|
|
272
|
+
if (style === "deepObject" && isRecord(value)) {
|
|
273
|
+
return Object.entries(value).map(
|
|
274
|
+
([key, item]) => `${encodeURIComponent(`${param.name}[${key}]`)}=${encodeURIComponent(paramText(item))}`
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
if (!Array.isArray(value) && !isRecord(value)) return [`${name}=${encodeURIComponent(paramText(value))}`];
|
|
278
|
+
const items = serializedItems(value, explode, encodeURIComponent);
|
|
279
|
+
if (!explode) return [`${name}=${items.join(QUERY_DELIMITERS[style] ?? ",")}`];
|
|
280
|
+
return Array.isArray(value) ? items.map((item) => `${name}=${item}`) : items;
|
|
281
|
+
}
|
|
282
|
+
function buildRequestUrl(baseUrl, op, args) {
|
|
283
|
+
const pairs = [];
|
|
284
|
+
for (const param of op.parameters ?? []) {
|
|
285
|
+
if (param.in !== "query") continue;
|
|
286
|
+
const value = args[param.name];
|
|
287
|
+
if (value === void 0 || value === null) continue;
|
|
288
|
+
pairs.push(...queryPairs(param, value));
|
|
289
|
+
}
|
|
290
|
+
const url = baseUrl + interpolatePath(op.path, args);
|
|
291
|
+
return pairs.length > 0 ? `${url}${url.includes("?") ? "&" : "?"}${pairs.join("&")}` : url;
|
|
292
|
+
}
|
|
293
|
+
function headerText(param, value) {
|
|
294
|
+
if (!Array.isArray(value) && !isRecord(value)) return paramText(value);
|
|
295
|
+
return serializedItems(value, param.explode ?? false, (text) => text).join(",");
|
|
296
|
+
}
|
|
297
|
+
function headerParams(op, args) {
|
|
298
|
+
const headers = {};
|
|
299
|
+
for (const param of op.parameters ?? []) {
|
|
300
|
+
if (param.in !== "header") continue;
|
|
301
|
+
const value = args[param.name];
|
|
302
|
+
if (value !== void 0 && value !== null) headers[param.name] = headerText(param, value);
|
|
303
|
+
}
|
|
304
|
+
return headers;
|
|
305
|
+
}
|
|
306
|
+
function registrationKey(operations) {
|
|
307
|
+
return JSON.stringify(
|
|
308
|
+
operations.map((op) => [op.operationId, op.description, op.method, op.path, op.inputSchema, op.parameters ?? null])
|
|
309
|
+
);
|
|
310
|
+
}
|
|
255
311
|
function useApiClient(options) {
|
|
256
312
|
const { baseUrl, operations, headers, prefix = "api", client, fetch: customFetch } = options;
|
|
257
313
|
const { getDynamicRegistry } = useContext(FrontMcpContext);
|
|
258
314
|
const dynamicRegistry = getDynamicRegistry(options.server);
|
|
315
|
+
const baseUrlRef = useRef(baseUrl);
|
|
259
316
|
const headersRef = useRef(headers);
|
|
260
|
-
|
|
317
|
+
const operationsRef = useRef(operations);
|
|
261
318
|
const clientRef = useRef(client ?? createFetchClient(customFetch));
|
|
262
|
-
clientRef.current = client ?? createFetchClient(customFetch);
|
|
263
319
|
useEffect(() => {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
320
|
+
baseUrlRef.current = baseUrl;
|
|
321
|
+
headersRef.current = headers;
|
|
322
|
+
operationsRef.current = operations;
|
|
323
|
+
clientRef.current = client ?? createFetchClient(customFetch);
|
|
324
|
+
}, [baseUrl, headers, operations, client, customFetch]);
|
|
325
|
+
const key = registrationKey(operations);
|
|
326
|
+
useEffect(() => {
|
|
327
|
+
const cleanups = operationsRef.current.map((registered) => {
|
|
267
328
|
const execute = async (args) => {
|
|
329
|
+
const op = operationsRef.current.find((o) => o.operationId === registered.operationId) ?? registered;
|
|
268
330
|
const resolvedHeaders = {
|
|
269
331
|
"Content-Type": "application/json",
|
|
270
|
-
...typeof headersRef.current === "function" ? headersRef.current() : headersRef.current ?? {}
|
|
332
|
+
...typeof headersRef.current === "function" ? headersRef.current() : headersRef.current ?? {},
|
|
333
|
+
...headerParams(op, args)
|
|
271
334
|
};
|
|
272
|
-
const url = baseUrl + interpolatePath(op.path, args);
|
|
273
335
|
const body = args["body"];
|
|
274
336
|
const method = op.method;
|
|
275
337
|
const requestConfig = {
|
|
276
338
|
method,
|
|
277
|
-
url,
|
|
339
|
+
url: buildRequestUrl(baseUrlRef.current, op, args),
|
|
278
340
|
headers: resolvedHeaders
|
|
279
341
|
};
|
|
280
342
|
if (body !== void 0 && method !== "GET" && method !== "HEAD") {
|
|
@@ -295,25 +357,37 @@ function useApiClient(options) {
|
|
|
295
357
|
isError: response.status >= 400
|
|
296
358
|
};
|
|
297
359
|
};
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
);
|
|
306
|
-
}
|
|
360
|
+
return dynamicRegistry.registerTool({
|
|
361
|
+
name: `${prefix}_${registered.operationId}`,
|
|
362
|
+
description: registered.description,
|
|
363
|
+
inputSchema: registered.inputSchema,
|
|
364
|
+
execute
|
|
365
|
+
});
|
|
366
|
+
});
|
|
307
367
|
return () => {
|
|
308
368
|
cleanups.forEach((fn) => {
|
|
309
369
|
fn();
|
|
310
370
|
});
|
|
311
371
|
};
|
|
312
|
-
}, [dynamicRegistry,
|
|
372
|
+
}, [dynamicRegistry, prefix, key]);
|
|
313
373
|
}
|
|
314
374
|
|
|
315
375
|
// libs/react/src/api/parseOpenApiSpec.ts
|
|
316
376
|
var HTTP_METHODS = ["get", "post", "put", "delete", "patch", "options", "head"];
|
|
377
|
+
var PARAMETER_LOCATIONS = ["path", "query", "header", "cookie"];
|
|
378
|
+
var PARAMETER_STYLES = [
|
|
379
|
+
"form",
|
|
380
|
+
"spaceDelimited",
|
|
381
|
+
"pipeDelimited",
|
|
382
|
+
"deepObject",
|
|
383
|
+
"simple"
|
|
384
|
+
];
|
|
385
|
+
function toApiParameter(param) {
|
|
386
|
+
const parameter = { name: param.name, in: param.in };
|
|
387
|
+
if (PARAMETER_STYLES.includes(param.style)) parameter.style = param.style;
|
|
388
|
+
if (typeof param.explode === "boolean") parameter.explode = param.explode;
|
|
389
|
+
return parameter;
|
|
390
|
+
}
|
|
317
391
|
function parseOpenApiSpec(spec) {
|
|
318
392
|
const paths = spec["paths"];
|
|
319
393
|
if (!paths) return [];
|
|
@@ -353,6 +427,7 @@ function parseOpenApiSpec(spec) {
|
|
|
353
427
|
}
|
|
354
428
|
}
|
|
355
429
|
const propertyLocations = /* @__PURE__ */ new Map();
|
|
430
|
+
const parameters = [];
|
|
356
431
|
for (const param of paramMap.values()) {
|
|
357
432
|
if (param.name === "__proto__" || param.name === "constructor" || param.name === "prototype") continue;
|
|
358
433
|
const existingIn = propertyLocations.get(param.name);
|
|
@@ -367,6 +442,7 @@ function parseOpenApiSpec(spec) {
|
|
|
367
442
|
description: param.description
|
|
368
443
|
};
|
|
369
444
|
if (param.required) required.push(param.name);
|
|
445
|
+
if (PARAMETER_LOCATIONS.includes(param.in)) parameters.push(toApiParameter(param));
|
|
370
446
|
}
|
|
371
447
|
const requestBody = operation["requestBody"];
|
|
372
448
|
if (requestBody) {
|
|
@@ -387,7 +463,14 @@ function parseOpenApiSpec(spec) {
|
|
|
387
463
|
if (required.length > 0) {
|
|
388
464
|
inputSchema["required"] = required;
|
|
389
465
|
}
|
|
390
|
-
operations.push({
|
|
466
|
+
operations.push({
|
|
467
|
+
operationId,
|
|
468
|
+
description,
|
|
469
|
+
method: method.toUpperCase(),
|
|
470
|
+
path,
|
|
471
|
+
inputSchema,
|
|
472
|
+
...parameters.length > 0 ? { parameters } : {}
|
|
473
|
+
});
|
|
391
474
|
}
|
|
392
475
|
}
|
|
393
476
|
return operations;
|
package/esm/index.mjs
CHANGED
|
@@ -30,11 +30,22 @@ import { z as z2 } from "@frontmcp/sdk";
|
|
|
30
30
|
import { ToolContext, ResourceContext, PromptContext, ExecutionContextBase } from "@frontmcp/sdk";
|
|
31
31
|
|
|
32
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
|
+
}
|
|
33
42
|
var ServerRegistry = class {
|
|
34
43
|
entries = /* @__PURE__ */ new Map();
|
|
35
44
|
listeners = /* @__PURE__ */ new Set();
|
|
45
|
+
toolListWatches = /* @__PURE__ */ new Map();
|
|
36
46
|
version = 0;
|
|
37
47
|
register(name, server) {
|
|
48
|
+
this.stopToolListWatch(name);
|
|
38
49
|
this.entries.set(name, {
|
|
39
50
|
server,
|
|
40
51
|
client: null,
|
|
@@ -48,6 +59,7 @@ var ServerRegistry = class {
|
|
|
48
59
|
this.notify();
|
|
49
60
|
}
|
|
50
61
|
unregister(name) {
|
|
62
|
+
this.stopToolListWatch(name);
|
|
51
63
|
this.entries.delete(name);
|
|
52
64
|
this.notify();
|
|
53
65
|
}
|
|
@@ -86,6 +98,7 @@ var ServerRegistry = class {
|
|
|
86
98
|
};
|
|
87
99
|
this.entries.set(name, connected);
|
|
88
100
|
this.notify();
|
|
101
|
+
this.watchToolList(name, client);
|
|
89
102
|
return client;
|
|
90
103
|
} catch (err) {
|
|
91
104
|
const error = err instanceof Error ? err : new Error(String(err));
|
|
@@ -97,6 +110,32 @@ var ServerRegistry = class {
|
|
|
97
110
|
async connectAll() {
|
|
98
111
|
await Promise.all(this.list().map((name) => this.connect(name)));
|
|
99
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
|
+
}
|
|
100
139
|
update(name, partial) {
|
|
101
140
|
const entry = this.entries.get(name);
|
|
102
141
|
if (entry) {
|
|
@@ -105,6 +144,7 @@ var ServerRegistry = class {
|
|
|
105
144
|
}
|
|
106
145
|
}
|
|
107
146
|
clear() {
|
|
147
|
+
for (const name of [...this.toolListWatches.keys()]) this.stopToolListWatch(name);
|
|
108
148
|
this.entries.clear();
|
|
109
149
|
this.notify();
|
|
110
150
|
}
|
|
@@ -117,6 +157,12 @@ var ServerRegistry = class {
|
|
|
117
157
|
getVersion() {
|
|
118
158
|
return this.version;
|
|
119
159
|
}
|
|
160
|
+
stopToolListWatch(name) {
|
|
161
|
+
const stop = this.toolListWatches.get(name);
|
|
162
|
+
if (!stop) return;
|
|
163
|
+
this.toolListWatches.delete(name);
|
|
164
|
+
stop();
|
|
165
|
+
}
|
|
120
166
|
notify() {
|
|
121
167
|
this.version++;
|
|
122
168
|
this.listeners.forEach((l) => l());
|
|
@@ -143,16 +189,16 @@ var DynamicRegistry = class {
|
|
|
143
189
|
* suitable for useEffect cleanup.
|
|
144
190
|
*
|
|
145
191
|
* Multiple registrations of the same name are ref-counted:
|
|
146
|
-
* subsequent registrations
|
|
147
|
-
*
|
|
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.
|
|
148
196
|
*/
|
|
149
197
|
registerTool(def) {
|
|
150
198
|
const existing = this.toolRefCounts.get(def.name) ?? 0;
|
|
151
199
|
this.toolRefCounts.set(def.name, existing + 1);
|
|
152
200
|
this.tools.set(def.name, def);
|
|
153
|
-
|
|
154
|
-
this.notify();
|
|
155
|
-
}
|
|
201
|
+
this.notify();
|
|
156
202
|
let called = false;
|
|
157
203
|
return () => {
|
|
158
204
|
if (called) return;
|
|
@@ -272,16 +318,6 @@ var DynamicRegistry = class {
|
|
|
272
318
|
|
|
273
319
|
// libs/react/src/registry/createWrappedServer.ts
|
|
274
320
|
function patchClientWithDynamic(client, dynamicRegistry) {
|
|
275
|
-
if (typeof client.callTool === "function") {
|
|
276
|
-
const originalCallTool = client.callTool.bind(client);
|
|
277
|
-
client.callTool = async (name, args) => {
|
|
278
|
-
const dynamicTool = dynamicRegistry.findTool(name);
|
|
279
|
-
if (dynamicTool) {
|
|
280
|
-
return dynamicTool.execute(args ?? {});
|
|
281
|
-
}
|
|
282
|
-
return originalCallTool(name, args);
|
|
283
|
-
};
|
|
284
|
-
}
|
|
285
321
|
if (typeof client.readResource === "function") {
|
|
286
322
|
const originalReadResource = client.readResource.bind(client);
|
|
287
323
|
client.readResource = async (uri) => {
|
|
@@ -325,28 +361,14 @@ function createWrappedServer(base, dynamicRegistry) {
|
|
|
325
361
|
return base.ready;
|
|
326
362
|
},
|
|
327
363
|
async listTools(options) {
|
|
328
|
-
|
|
329
|
-
const dynamicTools = dynamicRegistry.getTools();
|
|
330
|
-
if (dynamicTools.length === 0) return baseResult;
|
|
331
|
-
const dynamicNames = new Set(dynamicTools.map((t) => t.name));
|
|
332
|
-
const baseTools = (baseResult.tools ?? []).filter((t) => !dynamicNames.has(t.name));
|
|
333
|
-
const mergedTools = [
|
|
334
|
-
...baseTools,
|
|
335
|
-
...dynamicTools.map((t) => ({
|
|
336
|
-
name: t.name,
|
|
337
|
-
description: t.description,
|
|
338
|
-
inputSchema: t.inputSchema
|
|
339
|
-
}))
|
|
340
|
-
];
|
|
341
|
-
return { ...baseResult, tools: mergedTools };
|
|
364
|
+
return base.listTools(options);
|
|
342
365
|
},
|
|
343
366
|
async callTool(name, args, options) {
|
|
344
|
-
const dynamicTool = dynamicRegistry.findTool(name);
|
|
345
|
-
if (dynamicTool) {
|
|
346
|
-
return dynamicTool.execute(args ?? {});
|
|
347
|
-
}
|
|
348
367
|
return base.callTool(name, args, options);
|
|
349
368
|
},
|
|
369
|
+
async registerTool(definition) {
|
|
370
|
+
return base.registerTool(definition);
|
|
371
|
+
},
|
|
350
372
|
async listResources(options) {
|
|
351
373
|
const baseResult = await base.listResources(options);
|
|
352
374
|
const dynamicResources = dynamicRegistry.getResources();
|
|
@@ -355,9 +377,10 @@ function createWrappedServer(base, dynamicRegistry) {
|
|
|
355
377
|
const baseResources = (baseResult.resources ?? []).filter(
|
|
356
378
|
(r) => !dynamicUris.has(r.uri)
|
|
357
379
|
);
|
|
380
|
+
const laterPage = options?.cursor !== void 0;
|
|
358
381
|
const mergedResources = [
|
|
359
382
|
...baseResources,
|
|
360
|
-
...dynamicResources.map((r) => ({
|
|
383
|
+
...(laterPage ? [] : dynamicResources).map((r) => ({
|
|
361
384
|
uri: r.uri,
|
|
362
385
|
name: r.name ?? r.uri,
|
|
363
386
|
description: r.description,
|
|
@@ -481,7 +504,110 @@ function getOrCreateContext() {
|
|
|
481
504
|
var FrontMcpContext = getOrCreateContext();
|
|
482
505
|
|
|
483
506
|
// libs/react/src/provider/FrontMcpProvider.tsx
|
|
484
|
-
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
|
+
}
|
|
485
611
|
|
|
486
612
|
// libs/react/src/state/useStoreRegistration.ts
|
|
487
613
|
import { useEffect } from "react";
|
|
@@ -593,6 +719,7 @@ function FrontMcpProvider({
|
|
|
593
719
|
servers,
|
|
594
720
|
components,
|
|
595
721
|
stores,
|
|
722
|
+
dynamicToolApps,
|
|
596
723
|
autoConnect = true,
|
|
597
724
|
children,
|
|
598
725
|
onConnected,
|
|
@@ -601,6 +728,8 @@ function FrontMcpProvider({
|
|
|
601
728
|
const resolvedName = nameProp ?? "default";
|
|
602
729
|
const mountedRef = useRef(true);
|
|
603
730
|
const clientRef = useRef(null);
|
|
731
|
+
const onErrorRef = useRef(onError);
|
|
732
|
+
onErrorRef.current = onError;
|
|
604
733
|
const registry = useMemo(() => {
|
|
605
734
|
const reg = new ComponentRegistry();
|
|
606
735
|
if (components) {
|
|
@@ -624,16 +753,36 @@ function FrontMcpProvider({
|
|
|
624
753
|
const dynamicRegistry = useMemo(() => getDynamicRegistry(resolvedName), [getDynamicRegistry, resolvedName]);
|
|
625
754
|
useStoreRegistration(stores ?? [], dynamicRegistry);
|
|
626
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
|
+
);
|
|
627
771
|
useEffect2(() => {
|
|
772
|
+
const unbinds = [];
|
|
628
773
|
serverRegistry.register(resolvedName, wrappedServer);
|
|
629
774
|
if (servers) {
|
|
630
775
|
for (const [sName, srv] of Object.entries(servers)) {
|
|
631
776
|
const srvRegistry = getDynamicRegistry(sName);
|
|
632
777
|
const wrappedSrv = createWrappedServer(srv, srvRegistry);
|
|
633
778
|
serverRegistry.register(sName, wrappedSrv);
|
|
779
|
+
unbinds.push(bindDynamicTools(srvRegistry, srv, { onError: reportToolError, app: toolApps[sName] }));
|
|
634
780
|
}
|
|
635
781
|
}
|
|
636
782
|
return () => {
|
|
783
|
+
unbinds.forEach((unbind) => {
|
|
784
|
+
unbind();
|
|
785
|
+
});
|
|
637
786
|
serverRegistry.unregister(resolvedName);
|
|
638
787
|
if (servers) {
|
|
639
788
|
for (const sName of Object.keys(servers)) {
|
|
@@ -646,19 +795,18 @@ function FrontMcpProvider({
|
|
|
646
795
|
}
|
|
647
796
|
}
|
|
648
797
|
};
|
|
649
|
-
}, [resolvedName, wrappedServer, servers, getDynamicRegistry]);
|
|
798
|
+
}, [resolvedName, wrappedServer, servers, getDynamicRegistry, reportToolError, toolApps]);
|
|
650
799
|
useEffect2(() => {
|
|
651
800
|
const refreshServerEntry = (name) => {
|
|
652
801
|
const entry = serverRegistry.get(name);
|
|
653
802
|
if (!entry || !entry.client) return;
|
|
654
803
|
const srv = entry.server;
|
|
655
804
|
if (!srv) return;
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
});
|
|
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 });
|
|
662
810
|
}
|
|
663
811
|
}).catch(() => {
|
|
664
812
|
});
|
|
@@ -701,11 +849,6 @@ function FrontMcpProvider({
|
|
|
701
849
|
safeList(() => client.listPrompts(), { prompts: [] })
|
|
702
850
|
]);
|
|
703
851
|
if (mountedRef.current) {
|
|
704
|
-
const dynamicTools = dynamicRegistry.getTools().map((t) => ({
|
|
705
|
-
name: t.name,
|
|
706
|
-
description: t.description,
|
|
707
|
-
inputSchema: t.inputSchema
|
|
708
|
-
}));
|
|
709
852
|
const dynamicResources = dynamicRegistry.getResources().map((r) => ({
|
|
710
853
|
uri: r.uri,
|
|
711
854
|
name: r.name,
|
|
@@ -713,10 +856,6 @@ function FrontMcpProvider({
|
|
|
713
856
|
mimeType: r.mimeType
|
|
714
857
|
}));
|
|
715
858
|
const baseTools = toolsResult;
|
|
716
|
-
const dynamicToolNames = new Set(dynamicTools.map((t) => t.name));
|
|
717
|
-
const filteredBaseTools = (Array.isArray(baseTools) ? baseTools : []).filter(
|
|
718
|
-
(t) => !dynamicToolNames.has(t.name)
|
|
719
|
-
);
|
|
720
859
|
const baseResources = resourcesResult.resources ?? [];
|
|
721
860
|
const dynamicResourceUris = new Set(dynamicResources.map((r) => r.uri));
|
|
722
861
|
const filteredBaseResources = baseResources.filter((r) => !dynamicResourceUris.has(r.uri));
|
|
@@ -724,11 +863,12 @@ function FrontMcpProvider({
|
|
|
724
863
|
client,
|
|
725
864
|
status: "connected",
|
|
726
865
|
error: null,
|
|
727
|
-
tools: [
|
|
866
|
+
tools: Array.isArray(baseTools) ? baseTools : [],
|
|
728
867
|
resources: [...filteredBaseResources, ...dynamicResources],
|
|
729
868
|
resourceTemplates: templatesResult.resourceTemplates ?? [],
|
|
730
869
|
prompts: promptsResult.prompts ?? []
|
|
731
870
|
});
|
|
871
|
+
serverRegistry.watchToolList(resolvedName, client);
|
|
732
872
|
onConnected?.(client);
|
|
733
873
|
}
|
|
734
874
|
if (servers) {
|
|
@@ -1050,12 +1190,16 @@ function stableKey(value) {
|
|
|
1050
1190
|
}
|
|
1051
1191
|
}
|
|
1052
1192
|
function useDynamicTool(options) {
|
|
1053
|
-
const { name, description, enabled = true } = options;
|
|
1193
|
+
const { name, description, app, enabled = true } = options;
|
|
1054
1194
|
const { getDynamicRegistry } = useContext3(FrontMcpContext);
|
|
1055
1195
|
const dynamicRegistry = getDynamicRegistry(options.server);
|
|
1056
1196
|
const computedInputSchema = "schema" in options && options.schema ? zodToJsonSchema(options.schema) : options.inputSchema;
|
|
1057
1197
|
const inputSchemaKey = stableKey(computedInputSchema);
|
|
1058
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]);
|
|
1059
1203
|
const executeRef = useRef3(options.execute);
|
|
1060
1204
|
executeRef.current = options.execute;
|
|
1061
1205
|
const schemaRef = useRef3("schema" in options && options.schema ? options.schema : null);
|
|
@@ -1091,10 +1235,13 @@ function useDynamicTool(options) {
|
|
|
1091
1235
|
name,
|
|
1092
1236
|
description,
|
|
1093
1237
|
inputSchema: resolvedInputSchema,
|
|
1094
|
-
execute: stableExecute
|
|
1238
|
+
execute: stableExecute,
|
|
1239
|
+
...annotations && { annotations },
|
|
1240
|
+
...availableWhen && { availableWhen },
|
|
1241
|
+
...app && { app }
|
|
1095
1242
|
});
|
|
1096
1243
|
return unregister;
|
|
1097
|
-
}, [dynamicRegistry, name, description, resolvedInputSchema, enabled]);
|
|
1244
|
+
}, [dynamicRegistry, name, description, resolvedInputSchema, annotations, availableWhen, app, enabled]);
|
|
1098
1245
|
}
|
|
1099
1246
|
|
|
1100
1247
|
// libs/react/src/hooks/useDynamicResource.ts
|
|
@@ -1308,19 +1455,19 @@ function ToolForm({
|
|
|
1308
1455
|
const properties = schema["properties"] ?? {};
|
|
1309
1456
|
const required = schema["required"] ?? [];
|
|
1310
1457
|
const [values, setValues] = useState5({});
|
|
1311
|
-
const initialValue = (prop) => {
|
|
1458
|
+
const initialValue = (key, prop) => {
|
|
1312
1459
|
const options = prop["enum"];
|
|
1313
1460
|
if (!options) return "";
|
|
1314
1461
|
const fallback = prop["default"];
|
|
1315
|
-
|
|
1462
|
+
if (typeof fallback === "string" && options.includes(fallback)) return fallback;
|
|
1463
|
+
return required.includes(key) ? options[0] ?? "" : "";
|
|
1316
1464
|
};
|
|
1317
1465
|
const handleSubmit = useCallback8(
|
|
1318
1466
|
(e) => {
|
|
1319
1467
|
e.preventDefault();
|
|
1320
1468
|
const args = {};
|
|
1321
1469
|
for (const [key, prop] of Object.entries(properties)) {
|
|
1322
|
-
const
|
|
1323
|
-
const raw = values[key] ?? (seeded ? initialValue(prop) : "");
|
|
1470
|
+
const raw = values[key] ?? initialValue(key, prop);
|
|
1324
1471
|
if (!required.includes(key) && raw === "") continue;
|
|
1325
1472
|
if (prop["type"] === "number" || prop["type"] === "integer") {
|
|
1326
1473
|
args[key] = Number(raw);
|
|
@@ -1344,7 +1491,7 @@ function ToolForm({
|
|
|
1344
1491
|
const isRequired = required.includes(key);
|
|
1345
1492
|
const enumValues = prop["enum"];
|
|
1346
1493
|
const fieldType = getFieldType(prop);
|
|
1347
|
-
const value = values[key] ?? initialValue(prop);
|
|
1494
|
+
const value = values[key] ?? initialValue(key, prop);
|
|
1348
1495
|
if (renderField) {
|
|
1349
1496
|
return React3.createElement(
|
|
1350
1497
|
React3.Fragment,
|
|
@@ -1375,6 +1522,7 @@ function ToolForm({
|
|
|
1375
1522
|
value,
|
|
1376
1523
|
onChange: (e) => handleChange(key, e.target.value)
|
|
1377
1524
|
},
|
|
1525
|
+
...isRequired ? [] : [React3.createElement("option", { key: "", value: "" }, "")],
|
|
1378
1526
|
...enumValues.map((v) => React3.createElement("option", { key: v, value: v }, v))
|
|
1379
1527
|
) : React3.createElement("input", {
|
|
1380
1528
|
id: `field-${key}`,
|