@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/README.md
CHANGED
|
@@ -169,6 +169,8 @@ useDynamicTool({
|
|
|
169
169
|
|
|
170
170
|
Also supports raw JSON Schema via `inputSchema` for backward compatibility.
|
|
171
171
|
|
|
172
|
+
The provider registers each dynamic tool with the server as a real tool (`server.registerTool()`), so it runs through the server's flows (plugin hooks, authorities, `availableWhen`) and every client sees it — including in-browser agents through `@frontmcp/plugin-webmcp`. A name a server tool already has is reported through the provider's `onError` instead of shadowing that tool. On a server with several apps, say which app dynamic tools join with the provider's `dynamicToolApps` (by server name) or a tool's own `app` option.
|
|
173
|
+
|
|
172
174
|
## State Management
|
|
173
175
|
|
|
174
176
|
`@frontmcp/react/state` exposes your application state as MCP resources and actions as tools.
|
package/ai/index.js
CHANGED
|
@@ -90,22 +90,23 @@ var DynamicRegistry = class {
|
|
|
90
90
|
toolRefCounts = /* @__PURE__ */ new Map();
|
|
91
91
|
resourceRefCounts = /* @__PURE__ */ new Map();
|
|
92
92
|
listeners = /* @__PURE__ */ new Set();
|
|
93
|
+
resourceListeners = /* @__PURE__ */ new Set();
|
|
93
94
|
version = 0;
|
|
94
95
|
/**
|
|
95
96
|
* Register a dynamic tool. Returns an unregister function
|
|
96
97
|
* suitable for useEffect cleanup.
|
|
97
98
|
*
|
|
98
99
|
* Multiple registrations of the same name are ref-counted:
|
|
99
|
-
* subsequent registrations
|
|
100
|
-
*
|
|
100
|
+
* subsequent registrations replace the definition (and notify, so
|
|
101
|
+
* the server registration follows the new description, schema or
|
|
102
|
+
* availability) but the tool is only removed when every registrant
|
|
103
|
+
* has unregistered.
|
|
101
104
|
*/
|
|
102
105
|
registerTool(def) {
|
|
103
106
|
const existing = this.toolRefCounts.get(def.name) ?? 0;
|
|
104
107
|
this.toolRefCounts.set(def.name, existing + 1);
|
|
105
108
|
this.tools.set(def.name, def);
|
|
106
|
-
|
|
107
|
-
this.notify();
|
|
108
|
-
}
|
|
109
|
+
this.notify();
|
|
109
110
|
let called = false;
|
|
110
111
|
return () => {
|
|
111
112
|
if (called) return;
|
|
@@ -168,8 +169,18 @@ var DynamicRegistry = class {
|
|
|
168
169
|
if (existing) {
|
|
169
170
|
existing.read = read;
|
|
170
171
|
this.notify();
|
|
172
|
+
this.resourceListeners.forEach((l) => {
|
|
173
|
+
l(uri);
|
|
174
|
+
});
|
|
171
175
|
}
|
|
172
176
|
}
|
|
177
|
+
/** Listen for changes to a dynamic resource's content (the in-page `resources/updated`). */
|
|
178
|
+
onResourceUpdated(listener) {
|
|
179
|
+
this.resourceListeners.add(listener);
|
|
180
|
+
return () => {
|
|
181
|
+
this.resourceListeners.delete(listener);
|
|
182
|
+
};
|
|
183
|
+
}
|
|
173
184
|
getTools() {
|
|
174
185
|
return [...this.tools.values()];
|
|
175
186
|
}
|
|
@@ -214,25 +225,45 @@ var DynamicRegistry = class {
|
|
|
214
225
|
};
|
|
215
226
|
|
|
216
227
|
// libs/react/src/provider/FrontMcpContext.ts
|
|
217
|
-
var
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
228
|
+
var CONTEXT_KEY = /* @__PURE__ */ Symbol.for("@frontmcp/react/FrontMcpContext");
|
|
229
|
+
function getOrCreateContext() {
|
|
230
|
+
const host = globalThis;
|
|
231
|
+
const existing = host[CONTEXT_KEY];
|
|
232
|
+
if (existing) return existing;
|
|
233
|
+
const defaultDynamicRegistry = new DynamicRegistry();
|
|
234
|
+
const created = (0, import_react.createContext)({
|
|
235
|
+
name: "default",
|
|
236
|
+
registry: new ComponentRegistry(),
|
|
237
|
+
dynamicRegistry: defaultDynamicRegistry,
|
|
238
|
+
getDynamicRegistry: () => defaultDynamicRegistry,
|
|
239
|
+
connect: async () => {
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
host[CONTEXT_KEY] = created;
|
|
243
|
+
return created;
|
|
244
|
+
}
|
|
245
|
+
var FrontMcpContext = getOrCreateContext();
|
|
226
246
|
|
|
227
247
|
// libs/react/src/hooks/useServer.ts
|
|
228
248
|
var import_react2 = require("react");
|
|
229
249
|
|
|
230
250
|
// libs/react/src/registry/ServerRegistry.ts
|
|
251
|
+
function sameListing(a, b) {
|
|
252
|
+
if (a === b) return true;
|
|
253
|
+
if (a.length !== b.length) return false;
|
|
254
|
+
try {
|
|
255
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
256
|
+
} catch {
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
231
260
|
var ServerRegistry = class {
|
|
232
261
|
entries = /* @__PURE__ */ new Map();
|
|
233
262
|
listeners = /* @__PURE__ */ new Set();
|
|
263
|
+
toolListWatches = /* @__PURE__ */ new Map();
|
|
234
264
|
version = 0;
|
|
235
265
|
register(name, server) {
|
|
266
|
+
this.stopToolListWatch(name);
|
|
236
267
|
this.entries.set(name, {
|
|
237
268
|
server,
|
|
238
269
|
client: null,
|
|
@@ -246,6 +277,7 @@ var ServerRegistry = class {
|
|
|
246
277
|
this.notify();
|
|
247
278
|
}
|
|
248
279
|
unregister(name) {
|
|
280
|
+
this.stopToolListWatch(name);
|
|
249
281
|
this.entries.delete(name);
|
|
250
282
|
this.notify();
|
|
251
283
|
}
|
|
@@ -284,6 +316,7 @@ var ServerRegistry = class {
|
|
|
284
316
|
};
|
|
285
317
|
this.entries.set(name, connected);
|
|
286
318
|
this.notify();
|
|
319
|
+
this.watchToolList(name, client);
|
|
287
320
|
return client;
|
|
288
321
|
} catch (err) {
|
|
289
322
|
const error = err instanceof Error ? err : new Error(String(err));
|
|
@@ -295,6 +328,32 @@ var ServerRegistry = class {
|
|
|
295
328
|
async connectAll() {
|
|
296
329
|
await Promise.all(this.list().map((name) => this.connect(name)));
|
|
297
330
|
}
|
|
331
|
+
/**
|
|
332
|
+
* Keep `name`'s tool list current: re-list through `client` now, and whenever the server
|
|
333
|
+
* announces `notifications/tools/list_changed` (a dynamic tool registered or removed, say). The
|
|
334
|
+
* first listing catches a change made before the watch began. Only the newest listing is applied,
|
|
335
|
+
* and only while `client` is still the entry's client.
|
|
336
|
+
*/
|
|
337
|
+
watchToolList(name, client) {
|
|
338
|
+
this.stopToolListWatch(name);
|
|
339
|
+
if (typeof client.onNotification !== "function") return;
|
|
340
|
+
let latest = 0;
|
|
341
|
+
const relist = () => {
|
|
342
|
+
const listing = ++latest;
|
|
343
|
+
client.listTools().then((tools) => {
|
|
344
|
+
const entry = this.entries.get(name);
|
|
345
|
+
if (listing !== latest || !entry || entry.client !== client) return;
|
|
346
|
+
if (sameListing(entry.tools, tools)) return;
|
|
347
|
+
this.update(name, { tools });
|
|
348
|
+
}).catch(() => {
|
|
349
|
+
});
|
|
350
|
+
};
|
|
351
|
+
const stop = client.onNotification((notification) => {
|
|
352
|
+
if (notification.method === "notifications/tools/list_changed") relist();
|
|
353
|
+
});
|
|
354
|
+
this.toolListWatches.set(name, stop);
|
|
355
|
+
relist();
|
|
356
|
+
}
|
|
298
357
|
update(name, partial) {
|
|
299
358
|
const entry = this.entries.get(name);
|
|
300
359
|
if (entry) {
|
|
@@ -303,6 +362,7 @@ var ServerRegistry = class {
|
|
|
303
362
|
}
|
|
304
363
|
}
|
|
305
364
|
clear() {
|
|
365
|
+
for (const name of [...this.toolListWatches.keys()]) this.stopToolListWatch(name);
|
|
306
366
|
this.entries.clear();
|
|
307
367
|
this.notify();
|
|
308
368
|
}
|
|
@@ -315,19 +375,32 @@ var ServerRegistry = class {
|
|
|
315
375
|
getVersion() {
|
|
316
376
|
return this.version;
|
|
317
377
|
}
|
|
378
|
+
stopToolListWatch(name) {
|
|
379
|
+
const stop = this.toolListWatches.get(name);
|
|
380
|
+
if (!stop) return;
|
|
381
|
+
this.toolListWatches.delete(name);
|
|
382
|
+
stop();
|
|
383
|
+
}
|
|
318
384
|
notify() {
|
|
319
385
|
this.version++;
|
|
320
386
|
this.listeners.forEach((l) => l());
|
|
321
387
|
}
|
|
322
388
|
};
|
|
323
|
-
var
|
|
389
|
+
var REGISTRY_KEY = /* @__PURE__ */ Symbol.for("@frontmcp/react/serverRegistry");
|
|
390
|
+
function getOrCreateRegistry() {
|
|
391
|
+
const host = globalThis;
|
|
392
|
+
return host[REGISTRY_KEY] ??= new ServerRegistry();
|
|
393
|
+
}
|
|
394
|
+
var serverRegistry = getOrCreateRegistry();
|
|
324
395
|
|
|
325
396
|
// libs/react/src/hooks/useServer.ts
|
|
326
397
|
function useServer(name) {
|
|
398
|
+
const ctx = (0, import_react2.useContext)(FrontMcpContext);
|
|
399
|
+
const serverName = name ?? ctx.name;
|
|
327
400
|
const subscribe = (0, import_react2.useCallback)((cb) => serverRegistry.subscribe(cb), []);
|
|
328
401
|
const getSnapshot = (0, import_react2.useCallback)(() => {
|
|
329
|
-
return serverRegistry.get(
|
|
330
|
-
}, [
|
|
402
|
+
return serverRegistry.get(serverName);
|
|
403
|
+
}, [serverName]);
|
|
331
404
|
return (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
|
|
332
405
|
}
|
|
333
406
|
|
package/api/api.types.d.ts
CHANGED
|
@@ -1,6 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Types for API client integration.
|
|
3
3
|
*/
|
|
4
|
+
/** Where an operation parameter goes in the request (the OpenAPI `in` field). */
|
|
5
|
+
export type ApiParameterLocation = 'path' | 'query' | 'header' | 'cookie';
|
|
6
|
+
/** How an array or object argument is written into the request (the OpenAPI `style` field). */
|
|
7
|
+
export type ApiParameterStyle = 'form' | 'spaceDelimited' | 'pipeDelimited' | 'deepObject' | 'simple';
|
|
8
|
+
/** An operation parameter: the argument `name`, where the request carries it, and how. */
|
|
9
|
+
export interface ApiParameter {
|
|
10
|
+
/** Argument name, as it appears in `inputSchema.properties`. */
|
|
11
|
+
name: string;
|
|
12
|
+
/**
|
|
13
|
+
* `'query'` arguments are sent in the query string and `'header'` arguments as request headers.
|
|
14
|
+
* `'path'` arguments fill the `{name}` placeholders of `path` (as every placeholder is filled);
|
|
15
|
+
* `'cookie'` arguments are not sent.
|
|
16
|
+
*/
|
|
17
|
+
in: ApiParameterLocation;
|
|
18
|
+
/**
|
|
19
|
+
* How an array or object value is serialized, as in OpenAPI. A query argument defaults to
|
|
20
|
+
* `'form'` (an array repeats the name, an object sends each property as its own parameter) and
|
|
21
|
+
* may be `'spaceDelimited'`, `'pipeDelimited'` or `'deepObject'` (`name[key]=value`). A header
|
|
22
|
+
* argument is always `'simple'`: its items joined with commas.
|
|
23
|
+
*/
|
|
24
|
+
style?: ApiParameterStyle;
|
|
25
|
+
/** The OpenAPI `explode` flag: `true` by default for `'form'`, `false` for the other styles. */
|
|
26
|
+
explode?: boolean;
|
|
27
|
+
}
|
|
4
28
|
export interface ApiOperation {
|
|
5
29
|
/** Unique operation identifier (becomes part of the tool name). */
|
|
6
30
|
operationId: string;
|
|
@@ -12,6 +36,11 @@ export interface ApiOperation {
|
|
|
12
36
|
path: string;
|
|
13
37
|
/** JSON Schema for the operation's input. */
|
|
14
38
|
inputSchema: Record<string, unknown>;
|
|
39
|
+
/**
|
|
40
|
+
* Where each argument goes. `parseOpenApiSpec` fills this from the spec's `parameters`. An
|
|
41
|
+
* argument without an entry here is sent only when it is a `{param}` of `path` or the `body`.
|
|
42
|
+
*/
|
|
43
|
+
parameters?: ApiParameter[];
|
|
15
44
|
}
|
|
16
45
|
/** Configuration for a single HTTP request. */
|
|
17
46
|
export interface HttpRequestConfig {
|
package/api/api.types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.types.d.ts","sourceRoot":"","sources":["../../src/api/api.types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,WAAW,YAAY;IAC3B,mEAAmE;IACnE,WAAW,EAAE,MAAM,CAAC;IACpB,6CAA6C;IAC7C,WAAW,EAAE,MAAM,CAAC;IACpB,mDAAmD;IACnD,MAAM,EAAE,MAAM,CAAC;IACf,mDAAmD;IACnD,IAAI,EAAE,MAAM,CAAC;IACb,6CAA6C;IAC7C,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"api.types.d.ts","sourceRoot":"","sources":["../../src/api/api.types.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,iFAAiF;AACjF,MAAM,MAAM,oBAAoB,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAE1E,+FAA+F;AAC/F,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,gBAAgB,GAAG,eAAe,GAAG,YAAY,GAAG,QAAQ,CAAC;AAEtG,0FAA0F;AAC1F,MAAM,WAAW,YAAY;IAC3B,gEAAgE;IAChE,IAAI,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,EAAE,EAAE,oBAAoB,CAAC;IACzB;;;;;OAKG;IACH,KAAK,CAAC,EAAE,iBAAiB,CAAC;IAC1B,gGAAgG;IAChG,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED,MAAM,WAAW,YAAY;IAC3B,mEAAmE;IACnE,WAAW,EAAE,MAAM,CAAC;IACpB,6CAA6C;IAC7C,WAAW,EAAE,MAAM,CAAC;IACpB,mDAAmD;IACnD,MAAM,EAAE,MAAM,CAAC;IACf,mDAAmD;IACnD,IAAI,EAAE,MAAM,CAAC;IACb,6CAA6C;IAC7C,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrC;;;OAGG;IACH,UAAU,CAAC,EAAE,YAAY,EAAE,CAAC;CAC7B;AAED,+CAA+C;AAC/C,MAAM,WAAW,iBAAiB;IAChC,yDAAyD;IACzD,MAAM,EAAE,MAAM,CAAC;IACf,0BAA0B;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,uBAAuB;IACvB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,uDAAuD;IACvD,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED,0DAA0D;AAC1D,MAAM,WAAW,YAAY;IAC3B,wBAAwB;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,mCAAmC;IACnC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,4BAA4B;IAC5B,IAAI,EAAE,OAAO,CAAC;CACf;AAED;;;;;GAKG;AACH,MAAM,WAAW,UAAU;IACzB,OAAO,CAAC,MAAM,EAAE,iBAAiB,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;CAC3D;AAED,MAAM,WAAW,gBAAgB;IAC/B,qCAAqC;IACrC,OAAO,EAAE,MAAM,CAAC;IAChB,2CAA2C;IAC3C,UAAU,EAAE,YAAY,EAAE,CAAC;IAC3B,iDAAiD;IACjD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,MAAM,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IAClE,yCAAyC;IACzC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,iFAAiF;IACjF,MAAM,CAAC,EAAE,UAAU,CAAC;IACpB,4DAA4D;IAC5D,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;IAChC,sCAAsC;IACtC,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB"}
|
package/api/index.d.ts
CHANGED
|
@@ -8,5 +8,5 @@
|
|
|
8
8
|
export { useApiClient } from './useApiClient';
|
|
9
9
|
export { parseOpenApiSpec } from './parseOpenApiSpec';
|
|
10
10
|
export { createFetchClient } from './createFetchClient';
|
|
11
|
-
export type { ApiOperation, ApiClientOptions, HttpClient, HttpRequestConfig, HttpResponse } from './api.types';
|
|
11
|
+
export type { ApiOperation, ApiParameter, ApiParameterLocation, ApiParameterStyle, ApiClientOptions, HttpClient, HttpRequestConfig, HttpResponse, } from './api.types';
|
|
12
12
|
//# sourceMappingURL=index.d.ts.map
|
package/api/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/api/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,YAAY,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/api/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,YAAY,EACV,YAAY,EACZ,YAAY,EACZ,oBAAoB,EACpB,iBAAiB,EACjB,gBAAgB,EAChB,UAAU,EACV,iBAAiB,EACjB,YAAY,GACb,MAAM,aAAa,CAAC"}
|
package/api/index.js
CHANGED
|
@@ -86,22 +86,23 @@ var DynamicRegistry = class {
|
|
|
86
86
|
toolRefCounts = /* @__PURE__ */ new Map();
|
|
87
87
|
resourceRefCounts = /* @__PURE__ */ new Map();
|
|
88
88
|
listeners = /* @__PURE__ */ new Set();
|
|
89
|
+
resourceListeners = /* @__PURE__ */ new Set();
|
|
89
90
|
version = 0;
|
|
90
91
|
/**
|
|
91
92
|
* Register a dynamic tool. Returns an unregister function
|
|
92
93
|
* suitable for useEffect cleanup.
|
|
93
94
|
*
|
|
94
95
|
* Multiple registrations of the same name are ref-counted:
|
|
95
|
-
* subsequent registrations
|
|
96
|
-
*
|
|
96
|
+
* subsequent registrations replace the definition (and notify, so
|
|
97
|
+
* the server registration follows the new description, schema or
|
|
98
|
+
* availability) but the tool is only removed when every registrant
|
|
99
|
+
* has unregistered.
|
|
97
100
|
*/
|
|
98
101
|
registerTool(def) {
|
|
99
102
|
const existing = this.toolRefCounts.get(def.name) ?? 0;
|
|
100
103
|
this.toolRefCounts.set(def.name, existing + 1);
|
|
101
104
|
this.tools.set(def.name, def);
|
|
102
|
-
|
|
103
|
-
this.notify();
|
|
104
|
-
}
|
|
105
|
+
this.notify();
|
|
105
106
|
let called = false;
|
|
106
107
|
return () => {
|
|
107
108
|
if (called) return;
|
|
@@ -164,8 +165,18 @@ var DynamicRegistry = class {
|
|
|
164
165
|
if (existing) {
|
|
165
166
|
existing.read = read;
|
|
166
167
|
this.notify();
|
|
168
|
+
this.resourceListeners.forEach((l) => {
|
|
169
|
+
l(uri);
|
|
170
|
+
});
|
|
167
171
|
}
|
|
168
172
|
}
|
|
173
|
+
/** Listen for changes to a dynamic resource's content (the in-page `resources/updated`). */
|
|
174
|
+
onResourceUpdated(listener) {
|
|
175
|
+
this.resourceListeners.add(listener);
|
|
176
|
+
return () => {
|
|
177
|
+
this.resourceListeners.delete(listener);
|
|
178
|
+
};
|
|
179
|
+
}
|
|
169
180
|
getTools() {
|
|
170
181
|
return [...this.tools.values()];
|
|
171
182
|
}
|
|
@@ -210,15 +221,24 @@ var DynamicRegistry = class {
|
|
|
210
221
|
};
|
|
211
222
|
|
|
212
223
|
// libs/react/src/provider/FrontMcpContext.ts
|
|
213
|
-
var
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
224
|
+
var CONTEXT_KEY = /* @__PURE__ */ Symbol.for("@frontmcp/react/FrontMcpContext");
|
|
225
|
+
function getOrCreateContext() {
|
|
226
|
+
const host = globalThis;
|
|
227
|
+
const existing = host[CONTEXT_KEY];
|
|
228
|
+
if (existing) return existing;
|
|
229
|
+
const defaultDynamicRegistry = new DynamicRegistry();
|
|
230
|
+
const created = (0, import_react.createContext)({
|
|
231
|
+
name: "default",
|
|
232
|
+
registry: new ComponentRegistry(),
|
|
233
|
+
dynamicRegistry: defaultDynamicRegistry,
|
|
234
|
+
getDynamicRegistry: () => defaultDynamicRegistry,
|
|
235
|
+
connect: async () => {
|
|
236
|
+
}
|
|
237
|
+
});
|
|
238
|
+
host[CONTEXT_KEY] = created;
|
|
239
|
+
return created;
|
|
240
|
+
}
|
|
241
|
+
var FrontMcpContext = getOrCreateContext();
|
|
222
242
|
|
|
223
243
|
// libs/react/src/api/createFetchClient.ts
|
|
224
244
|
function createFetchClient(fetchFn) {
|
|
@@ -260,29 +280,91 @@ function interpolatePath(path, params) {
|
|
|
260
280
|
return value != null ? encodeURIComponent(String(value)) : `{${key}}`;
|
|
261
281
|
});
|
|
262
282
|
}
|
|
283
|
+
function paramText(value) {
|
|
284
|
+
return typeof value === "object" && value !== null ? JSON.stringify(value) : String(value);
|
|
285
|
+
}
|
|
286
|
+
function isRecord(value) {
|
|
287
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
288
|
+
}
|
|
289
|
+
var QUERY_DELIMITERS = { spaceDelimited: "%20", pipeDelimited: "%7C" };
|
|
290
|
+
function serializedItems(value, explode, encode) {
|
|
291
|
+
if (Array.isArray(value)) return value.map((item) => encode(paramText(item)));
|
|
292
|
+
return Object.entries(value).flatMap(
|
|
293
|
+
([key, item]) => explode ? [`${encode(key)}=${encode(paramText(item))}`] : [encode(key), encode(paramText(item))]
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
function queryPairs(param, value) {
|
|
297
|
+
const style = param.style ?? "form";
|
|
298
|
+
const explode = param.explode ?? style === "form";
|
|
299
|
+
const name = encodeURIComponent(param.name);
|
|
300
|
+
if (style === "deepObject" && isRecord(value)) {
|
|
301
|
+
return Object.entries(value).map(
|
|
302
|
+
([key, item]) => `${encodeURIComponent(`${param.name}[${key}]`)}=${encodeURIComponent(paramText(item))}`
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
if (!Array.isArray(value) && !isRecord(value)) return [`${name}=${encodeURIComponent(paramText(value))}`];
|
|
306
|
+
const items = serializedItems(value, explode, encodeURIComponent);
|
|
307
|
+
if (!explode) return [`${name}=${items.join(QUERY_DELIMITERS[style] ?? ",")}`];
|
|
308
|
+
return Array.isArray(value) ? items.map((item) => `${name}=${item}`) : items;
|
|
309
|
+
}
|
|
310
|
+
function buildRequestUrl(baseUrl, op, args) {
|
|
311
|
+
const pairs = [];
|
|
312
|
+
for (const param of op.parameters ?? []) {
|
|
313
|
+
if (param.in !== "query") continue;
|
|
314
|
+
const value = args[param.name];
|
|
315
|
+
if (value === void 0 || value === null) continue;
|
|
316
|
+
pairs.push(...queryPairs(param, value));
|
|
317
|
+
}
|
|
318
|
+
const url = baseUrl + interpolatePath(op.path, args);
|
|
319
|
+
return pairs.length > 0 ? `${url}${url.includes("?") ? "&" : "?"}${pairs.join("&")}` : url;
|
|
320
|
+
}
|
|
321
|
+
function headerText(param, value) {
|
|
322
|
+
if (!Array.isArray(value) && !isRecord(value)) return paramText(value);
|
|
323
|
+
return serializedItems(value, param.explode ?? false, (text) => text).join(",");
|
|
324
|
+
}
|
|
325
|
+
function headerParams(op, args) {
|
|
326
|
+
const headers = {};
|
|
327
|
+
for (const param of op.parameters ?? []) {
|
|
328
|
+
if (param.in !== "header") continue;
|
|
329
|
+
const value = args[param.name];
|
|
330
|
+
if (value !== void 0 && value !== null) headers[param.name] = headerText(param, value);
|
|
331
|
+
}
|
|
332
|
+
return headers;
|
|
333
|
+
}
|
|
334
|
+
function registrationKey(operations) {
|
|
335
|
+
return JSON.stringify(
|
|
336
|
+
operations.map((op) => [op.operationId, op.description, op.method, op.path, op.inputSchema, op.parameters ?? null])
|
|
337
|
+
);
|
|
338
|
+
}
|
|
263
339
|
function useApiClient(options) {
|
|
264
340
|
const { baseUrl, operations, headers, prefix = "api", client, fetch: customFetch } = options;
|
|
265
341
|
const { getDynamicRegistry } = (0, import_react2.useContext)(FrontMcpContext);
|
|
266
342
|
const dynamicRegistry = getDynamicRegistry(options.server);
|
|
343
|
+
const baseUrlRef = (0, import_react2.useRef)(baseUrl);
|
|
267
344
|
const headersRef = (0, import_react2.useRef)(headers);
|
|
268
|
-
|
|
345
|
+
const operationsRef = (0, import_react2.useRef)(operations);
|
|
269
346
|
const clientRef = (0, import_react2.useRef)(client ?? createFetchClient(customFetch));
|
|
270
|
-
clientRef.current = client ?? createFetchClient(customFetch);
|
|
271
347
|
(0, import_react2.useEffect)(() => {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
348
|
+
baseUrlRef.current = baseUrl;
|
|
349
|
+
headersRef.current = headers;
|
|
350
|
+
operationsRef.current = operations;
|
|
351
|
+
clientRef.current = client ?? createFetchClient(customFetch);
|
|
352
|
+
}, [baseUrl, headers, operations, client, customFetch]);
|
|
353
|
+
const key = registrationKey(operations);
|
|
354
|
+
(0, import_react2.useEffect)(() => {
|
|
355
|
+
const cleanups = operationsRef.current.map((registered) => {
|
|
275
356
|
const execute = async (args) => {
|
|
357
|
+
const op = operationsRef.current.find((o) => o.operationId === registered.operationId) ?? registered;
|
|
276
358
|
const resolvedHeaders = {
|
|
277
359
|
"Content-Type": "application/json",
|
|
278
|
-
...typeof headersRef.current === "function" ? headersRef.current() : headersRef.current ?? {}
|
|
360
|
+
...typeof headersRef.current === "function" ? headersRef.current() : headersRef.current ?? {},
|
|
361
|
+
...headerParams(op, args)
|
|
279
362
|
};
|
|
280
|
-
const url = baseUrl + interpolatePath(op.path, args);
|
|
281
363
|
const body = args["body"];
|
|
282
364
|
const method = op.method;
|
|
283
365
|
const requestConfig = {
|
|
284
366
|
method,
|
|
285
|
-
url,
|
|
367
|
+
url: buildRequestUrl(baseUrlRef.current, op, args),
|
|
286
368
|
headers: resolvedHeaders
|
|
287
369
|
};
|
|
288
370
|
if (body !== void 0 && method !== "GET" && method !== "HEAD") {
|
|
@@ -303,25 +385,37 @@ function useApiClient(options) {
|
|
|
303
385
|
isError: response.status >= 400
|
|
304
386
|
};
|
|
305
387
|
};
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
);
|
|
314
|
-
}
|
|
388
|
+
return dynamicRegistry.registerTool({
|
|
389
|
+
name: `${prefix}_${registered.operationId}`,
|
|
390
|
+
description: registered.description,
|
|
391
|
+
inputSchema: registered.inputSchema,
|
|
392
|
+
execute
|
|
393
|
+
});
|
|
394
|
+
});
|
|
315
395
|
return () => {
|
|
316
396
|
cleanups.forEach((fn) => {
|
|
317
397
|
fn();
|
|
318
398
|
});
|
|
319
399
|
};
|
|
320
|
-
}, [dynamicRegistry,
|
|
400
|
+
}, [dynamicRegistry, prefix, key]);
|
|
321
401
|
}
|
|
322
402
|
|
|
323
403
|
// libs/react/src/api/parseOpenApiSpec.ts
|
|
324
404
|
var HTTP_METHODS = ["get", "post", "put", "delete", "patch", "options", "head"];
|
|
405
|
+
var PARAMETER_LOCATIONS = ["path", "query", "header", "cookie"];
|
|
406
|
+
var PARAMETER_STYLES = [
|
|
407
|
+
"form",
|
|
408
|
+
"spaceDelimited",
|
|
409
|
+
"pipeDelimited",
|
|
410
|
+
"deepObject",
|
|
411
|
+
"simple"
|
|
412
|
+
];
|
|
413
|
+
function toApiParameter(param) {
|
|
414
|
+
const parameter = { name: param.name, in: param.in };
|
|
415
|
+
if (PARAMETER_STYLES.includes(param.style)) parameter.style = param.style;
|
|
416
|
+
if (typeof param.explode === "boolean") parameter.explode = param.explode;
|
|
417
|
+
return parameter;
|
|
418
|
+
}
|
|
325
419
|
function parseOpenApiSpec(spec) {
|
|
326
420
|
const paths = spec["paths"];
|
|
327
421
|
if (!paths) return [];
|
|
@@ -361,6 +455,7 @@ function parseOpenApiSpec(spec) {
|
|
|
361
455
|
}
|
|
362
456
|
}
|
|
363
457
|
const propertyLocations = /* @__PURE__ */ new Map();
|
|
458
|
+
const parameters = [];
|
|
364
459
|
for (const param of paramMap.values()) {
|
|
365
460
|
if (param.name === "__proto__" || param.name === "constructor" || param.name === "prototype") continue;
|
|
366
461
|
const existingIn = propertyLocations.get(param.name);
|
|
@@ -375,6 +470,7 @@ function parseOpenApiSpec(spec) {
|
|
|
375
470
|
description: param.description
|
|
376
471
|
};
|
|
377
472
|
if (param.required) required.push(param.name);
|
|
473
|
+
if (PARAMETER_LOCATIONS.includes(param.in)) parameters.push(toApiParameter(param));
|
|
378
474
|
}
|
|
379
475
|
const requestBody = operation["requestBody"];
|
|
380
476
|
if (requestBody) {
|
|
@@ -395,7 +491,14 @@ function parseOpenApiSpec(spec) {
|
|
|
395
491
|
if (required.length > 0) {
|
|
396
492
|
inputSchema["required"] = required;
|
|
397
493
|
}
|
|
398
|
-
operations.push({
|
|
494
|
+
operations.push({
|
|
495
|
+
operationId,
|
|
496
|
+
description,
|
|
497
|
+
method: method.toUpperCase(),
|
|
498
|
+
path,
|
|
499
|
+
inputSchema,
|
|
500
|
+
...parameters.length > 0 ? { parameters } : {}
|
|
501
|
+
});
|
|
399
502
|
}
|
|
400
503
|
}
|
|
401
504
|
return operations;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parseOpenApiSpec.d.ts","sourceRoot":"","sources":["../../src/api/parseOpenApiSpec.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,YAAY,
|
|
1
|
+
{"version":3,"file":"parseOpenApiSpec.d.ts","sourceRoot":"","sources":["../../src/api/parseOpenApiSpec.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAyD,MAAM,aAAa,CAAC;AA8BvG,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,YAAY,EAAE,CA4G9E"}
|
package/api/useApiClient.d.ts
CHANGED
|
@@ -3,6 +3,11 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Each operation becomes a dynamic tool that makes an HTTP request
|
|
5
5
|
* using an injected HttpClient, a custom fetch, or globalThis.fetch.
|
|
6
|
+
*
|
|
7
|
+
* Options may be written inline: the tools are registered again only when the
|
|
8
|
+
* `prefix`, the target server, or what an operation declares (its id, description,
|
|
9
|
+
* method, path, input schema or parameters) changes. `baseUrl`, `headers`,
|
|
10
|
+
* `client` and `fetch` are read when a tool runs, from the latest committed render.
|
|
6
11
|
*/
|
|
7
12
|
import type { ApiClientOptions } from './api.types';
|
|
8
13
|
export declare function useApiClient(options: ApiClientOptions): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useApiClient.d.ts","sourceRoot":"","sources":["../../src/api/useApiClient.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"useApiClient.d.ts","sourceRoot":"","sources":["../../src/api/useApiClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAOH,OAAO,KAAK,EACV,gBAAgB,EAMjB,MAAM,aAAa,CAAC;AAyFrB,wBAAgB,YAAY,CAAC,OAAO,EAAE,gBAAgB,GAAG,IAAI,CA0E5D"}
|
package/components/ToolForm.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* Otherwise uses basic unstyled `<input>` / `<select>` / `<textarea>`.
|
|
6
6
|
*/
|
|
7
7
|
import React from 'react';
|
|
8
|
-
import type {
|
|
8
|
+
import type { FieldRenderProps, ToolInfo } from '../types';
|
|
9
9
|
export interface ToolFormProps {
|
|
10
10
|
tool: ToolInfo;
|
|
11
11
|
onSubmit: (args: Record<string, unknown>) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ToolForm.d.ts","sourceRoot":"","sources":["../../src/components/ToolForm.tsx"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAgC,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"ToolForm.d.ts","sourceRoot":"","sources":["../../src/components/ToolForm.tsx"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAgC,MAAM,OAAO,CAAC;AAErD,OAAO,KAAK,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAE3D,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,QAAQ,CAAC;IACf,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC;IAClD,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,KAAK,CAAC,SAAS,CAAC;IAC3D,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,wBAAgB,QAAQ,CAAC,EACvB,IAAI,EACJ,QAAQ,EACR,WAAW,EACX,WAAyB,GAC1B,EAAE,aAAa,GAAG,KAAK,CAAC,YAAY,CAoGpC"}
|