@frontmcp/react 1.11.3 → 1.12.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/index.mjs +150 -19
- package/esm/package.json +6 -6
- package/esm/router/index.mjs +450 -11
- package/events/createRemoteEventForwarder.d.ts +36 -0
- package/events/createRemoteEventForwarder.d.ts.map +1 -0
- package/events/index.d.ts +3 -0
- package/events/index.d.ts.map +1 -0
- package/hooks/index.d.ts +5 -0
- package/hooks/index.d.ts.map +1 -1
- package/hooks/useDomEventSource.d.ts +27 -0
- package/hooks/useDomEventSource.d.ts.map +1 -0
- package/hooks/useEventSubscription.d.ts +34 -0
- package/hooks/useEventSubscription.d.ts.map +1 -0
- package/hooks/usePublishEvent.d.ts +14 -0
- package/hooks/usePublishEvent.d.ts.map +1 -0
- package/index.d.ts +6 -2
- package/index.d.ts.map +1 -1
- package/index.js +208 -77
- package/package.json +6 -6
- package/registry/createWrappedServer.d.ts.map +1 -1
- package/router/index.d.ts +3 -1
- package/router/index.d.ts.map +1 -1
- package/router/index.js +456 -17
- package/router/route-changed.event.d.ts +8 -0
- package/router/route-changed.event.d.ts.map +1 -0
- package/router/router-bridge.d.ts +2 -0
- package/router/router-bridge.d.ts.map +1 -1
- package/router/useRouterBridge.d.ts +14 -2
- package/router/useRouterBridge.d.ts.map +1 -1
package/esm/router/index.mjs
CHANGED
|
@@ -1,8 +1,420 @@
|
|
|
1
1
|
// libs/react/src/router/useRouterBridge.ts
|
|
2
|
-
import { useEffect } from "react";
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
3
|
import { useLocation, useNavigate } from "react-router-dom";
|
|
4
4
|
|
|
5
|
+
// libs/react/src/hooks/usePublishEvent.ts
|
|
6
|
+
import { useCallback as useCallback2 } from "react";
|
|
7
|
+
|
|
8
|
+
// libs/react/src/hooks/useResolvedServer.ts
|
|
9
|
+
import { useContext as useContext2 } from "react";
|
|
10
|
+
|
|
11
|
+
// libs/react/src/provider/FrontMcpContext.ts
|
|
12
|
+
import { createContext } from "react";
|
|
13
|
+
|
|
14
|
+
// libs/react/src/components/ComponentRegistry.ts
|
|
15
|
+
var ComponentRegistry = class {
|
|
16
|
+
entries = /* @__PURE__ */ new Map();
|
|
17
|
+
register(uri, component, meta) {
|
|
18
|
+
const name = extractName(uri);
|
|
19
|
+
this.entries.set(uri, { uri, name, component, description: meta?.description });
|
|
20
|
+
}
|
|
21
|
+
registerAll(map) {
|
|
22
|
+
for (const [uri, component] of Object.entries(map)) {
|
|
23
|
+
this.register(uri, component);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
get(uri) {
|
|
27
|
+
return this.entries.get(uri)?.component;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Resolve a shorthand name like `'UserCard'` by trying
|
|
31
|
+
* `component://UserCard`, `element://UserCard`, `page://UserCard`.
|
|
32
|
+
*/
|
|
33
|
+
resolve(type) {
|
|
34
|
+
const exact = this.entries.get(type);
|
|
35
|
+
if (exact) return exact.component;
|
|
36
|
+
for (const protocol of ["component://", "element://", "page://"]) {
|
|
37
|
+
const entry = this.entries.get(`${protocol}${type}`);
|
|
38
|
+
if (entry) return entry.component;
|
|
39
|
+
}
|
|
40
|
+
return void 0;
|
|
41
|
+
}
|
|
42
|
+
has(uri) {
|
|
43
|
+
return this.entries.has(uri);
|
|
44
|
+
}
|
|
45
|
+
list() {
|
|
46
|
+
return Array.from(this.entries.values()).map(({ uri, name, description }) => ({
|
|
47
|
+
uri,
|
|
48
|
+
name,
|
|
49
|
+
description
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
clear() {
|
|
53
|
+
this.entries.clear();
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
function extractName(uri) {
|
|
57
|
+
const idx = uri.indexOf("://");
|
|
58
|
+
return idx >= 0 ? uri.slice(idx + 3) : uri;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// libs/react/src/registry/DynamicRegistry.ts
|
|
62
|
+
var DynamicRegistry = class {
|
|
63
|
+
tools = /* @__PURE__ */ new Map();
|
|
64
|
+
resources = /* @__PURE__ */ new Map();
|
|
65
|
+
/** Every live registration of a tool name, oldest first; the newest one is the definition in use. */
|
|
66
|
+
toolRegistrations = /* @__PURE__ */ new Map();
|
|
67
|
+
resourceRefCounts = /* @__PURE__ */ new Map();
|
|
68
|
+
listeners = /* @__PURE__ */ new Set();
|
|
69
|
+
resourceListeners = /* @__PURE__ */ new Set();
|
|
70
|
+
version = 0;
|
|
71
|
+
/**
|
|
72
|
+
* Register a dynamic tool. Returns an unregister function
|
|
73
|
+
* suitable for useEffect cleanup.
|
|
74
|
+
*
|
|
75
|
+
* Multiple registrations of the same name are ref-counted:
|
|
76
|
+
* subsequent registrations replace the definition (and notify, so
|
|
77
|
+
* the server registration follows the new description, schema or
|
|
78
|
+
* availability) but the tool is only removed when every registrant
|
|
79
|
+
* has unregistered. When the registrant whose definition is in use
|
|
80
|
+
* unregisters, the newest remaining registration takes over, so a
|
|
81
|
+
* call never reaches an unmounted component's `execute`.
|
|
82
|
+
*/
|
|
83
|
+
registerTool(def) {
|
|
84
|
+
const registrations = this.toolRegistrations.get(def.name) ?? [];
|
|
85
|
+
registrations.push(def);
|
|
86
|
+
this.toolRegistrations.set(def.name, registrations);
|
|
87
|
+
this.tools.set(def.name, def);
|
|
88
|
+
this.notify();
|
|
89
|
+
let called = false;
|
|
90
|
+
return () => {
|
|
91
|
+
if (called) return;
|
|
92
|
+
called = true;
|
|
93
|
+
this.removeToolRegistration(def);
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/** Drop the oldest registration of `name` (registrants should call the function `registerTool` returned). */
|
|
97
|
+
unregisterTool(name) {
|
|
98
|
+
const oldest = this.toolRegistrations.get(name)?.[0];
|
|
99
|
+
if (oldest) this.removeToolRegistration(oldest);
|
|
100
|
+
}
|
|
101
|
+
removeToolRegistration(def) {
|
|
102
|
+
const registrations = this.toolRegistrations.get(def.name);
|
|
103
|
+
const index = registrations?.lastIndexOf(def) ?? -1;
|
|
104
|
+
if (!registrations || index === -1) return;
|
|
105
|
+
registrations.splice(index, 1);
|
|
106
|
+
const newest = registrations[registrations.length - 1];
|
|
107
|
+
if (!newest) {
|
|
108
|
+
this.toolRegistrations.delete(def.name);
|
|
109
|
+
this.tools.delete(def.name);
|
|
110
|
+
this.notify();
|
|
111
|
+
} else if (this.tools.get(def.name) !== newest) {
|
|
112
|
+
this.tools.set(def.name, newest);
|
|
113
|
+
this.notify();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Register a dynamic resource. Returns an unregister function
|
|
118
|
+
* suitable for useEffect cleanup.
|
|
119
|
+
*
|
|
120
|
+
* Multiple registrations of the same URI are ref-counted.
|
|
121
|
+
*/
|
|
122
|
+
registerResource(def) {
|
|
123
|
+
const existing = this.resourceRefCounts.get(def.uri) ?? 0;
|
|
124
|
+
this.resourceRefCounts.set(def.uri, existing + 1);
|
|
125
|
+
this.resources.set(def.uri, def);
|
|
126
|
+
if (existing === 0) {
|
|
127
|
+
this.notify();
|
|
128
|
+
}
|
|
129
|
+
let called = false;
|
|
130
|
+
return () => {
|
|
131
|
+
if (called) return;
|
|
132
|
+
called = true;
|
|
133
|
+
this.unregisterResource(def.uri);
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
unregisterResource(uri) {
|
|
137
|
+
const count = this.resourceRefCounts.get(uri);
|
|
138
|
+
if (count == null) return;
|
|
139
|
+
if (count <= 1) {
|
|
140
|
+
this.resourceRefCounts.delete(uri);
|
|
141
|
+
this.resources.delete(uri);
|
|
142
|
+
this.notify();
|
|
143
|
+
} else {
|
|
144
|
+
this.resourceRefCounts.set(uri, count - 1);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/** Update the execute function for an existing tool (for stale closure prevention). */
|
|
148
|
+
updateToolExecute(name, execute) {
|
|
149
|
+
const existing = this.tools.get(name);
|
|
150
|
+
if (existing) {
|
|
151
|
+
existing.execute = execute;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/** Update the read function for an existing resource and notify subscribers. */
|
|
155
|
+
updateResourceRead(uri, read) {
|
|
156
|
+
const existing = this.resources.get(uri);
|
|
157
|
+
if (existing) {
|
|
158
|
+
existing.read = read;
|
|
159
|
+
this.notify();
|
|
160
|
+
this.resourceListeners.forEach((l) => {
|
|
161
|
+
l(uri);
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/** Listen for changes to a dynamic resource's content (the in-page `resources/updated`). */
|
|
166
|
+
onResourceUpdated(listener) {
|
|
167
|
+
this.resourceListeners.add(listener);
|
|
168
|
+
return () => {
|
|
169
|
+
this.resourceListeners.delete(listener);
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
getTools() {
|
|
173
|
+
return [...this.tools.values()];
|
|
174
|
+
}
|
|
175
|
+
getResources() {
|
|
176
|
+
return [...this.resources.values()];
|
|
177
|
+
}
|
|
178
|
+
findTool(name) {
|
|
179
|
+
return this.tools.get(name);
|
|
180
|
+
}
|
|
181
|
+
findResource(uri) {
|
|
182
|
+
return this.resources.get(uri);
|
|
183
|
+
}
|
|
184
|
+
hasTool(name) {
|
|
185
|
+
return this.tools.has(name);
|
|
186
|
+
}
|
|
187
|
+
hasResource(uri) {
|
|
188
|
+
return this.resources.has(uri);
|
|
189
|
+
}
|
|
190
|
+
subscribe(listener) {
|
|
191
|
+
this.listeners.add(listener);
|
|
192
|
+
return () => {
|
|
193
|
+
this.listeners.delete(listener);
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
getVersion() {
|
|
197
|
+
return this.version;
|
|
198
|
+
}
|
|
199
|
+
clear() {
|
|
200
|
+
if (this.tools.size === 0 && this.resources.size === 0) return;
|
|
201
|
+
this.tools.clear();
|
|
202
|
+
this.resources.clear();
|
|
203
|
+
this.toolRegistrations.clear();
|
|
204
|
+
this.resourceRefCounts.clear();
|
|
205
|
+
this.notify();
|
|
206
|
+
}
|
|
207
|
+
notify() {
|
|
208
|
+
this.version++;
|
|
209
|
+
this.listeners.forEach((l) => {
|
|
210
|
+
l();
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
// libs/react/src/provider/FrontMcpContext.ts
|
|
216
|
+
var CONTEXT_KEY = /* @__PURE__ */ Symbol.for("@frontmcp/react/FrontMcpContext");
|
|
217
|
+
function getOrCreateContext() {
|
|
218
|
+
const host = globalThis;
|
|
219
|
+
const existing = host[CONTEXT_KEY];
|
|
220
|
+
if (existing) return existing;
|
|
221
|
+
const defaultDynamicRegistry = new DynamicRegistry();
|
|
222
|
+
const created = createContext({
|
|
223
|
+
name: "default",
|
|
224
|
+
registry: new ComponentRegistry(),
|
|
225
|
+
dynamicRegistry: defaultDynamicRegistry,
|
|
226
|
+
getDynamicRegistry: () => defaultDynamicRegistry,
|
|
227
|
+
connect: async () => {
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
host[CONTEXT_KEY] = created;
|
|
231
|
+
return created;
|
|
232
|
+
}
|
|
233
|
+
var FrontMcpContext = getOrCreateContext();
|
|
234
|
+
|
|
235
|
+
// libs/react/src/hooks/useServer.ts
|
|
236
|
+
import { useCallback, useContext, useSyncExternalStore } from "react";
|
|
237
|
+
|
|
238
|
+
// libs/react/src/registry/ServerRegistry.ts
|
|
239
|
+
function sameListing(a, b) {
|
|
240
|
+
if (a === b) return true;
|
|
241
|
+
if (a.length !== b.length) return false;
|
|
242
|
+
try {
|
|
243
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
244
|
+
} catch {
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
var ServerRegistry = class {
|
|
249
|
+
entries = /* @__PURE__ */ new Map();
|
|
250
|
+
listeners = /* @__PURE__ */ new Set();
|
|
251
|
+
toolListWatches = /* @__PURE__ */ new Map();
|
|
252
|
+
version = 0;
|
|
253
|
+
register(name, server) {
|
|
254
|
+
this.stopToolListWatch(name);
|
|
255
|
+
this.entries.set(name, {
|
|
256
|
+
server,
|
|
257
|
+
client: null,
|
|
258
|
+
status: "idle",
|
|
259
|
+
error: null,
|
|
260
|
+
tools: [],
|
|
261
|
+
resources: [],
|
|
262
|
+
resourceTemplates: [],
|
|
263
|
+
prompts: []
|
|
264
|
+
});
|
|
265
|
+
this.notify();
|
|
266
|
+
}
|
|
267
|
+
unregister(name) {
|
|
268
|
+
this.stopToolListWatch(name);
|
|
269
|
+
this.entries.delete(name);
|
|
270
|
+
this.notify();
|
|
271
|
+
}
|
|
272
|
+
get(name) {
|
|
273
|
+
return this.entries.get(name);
|
|
274
|
+
}
|
|
275
|
+
has(name) {
|
|
276
|
+
return this.entries.has(name);
|
|
277
|
+
}
|
|
278
|
+
list() {
|
|
279
|
+
return [...this.entries.keys()];
|
|
280
|
+
}
|
|
281
|
+
async connect(name) {
|
|
282
|
+
const entry = this.entries.get(name);
|
|
283
|
+
if (!entry) throw new Error(`Server "${name}" not registered`);
|
|
284
|
+
if (entry.client) return entry.client;
|
|
285
|
+
this.entries.set(name, { ...entry, status: "connecting", error: null });
|
|
286
|
+
this.notify();
|
|
287
|
+
try {
|
|
288
|
+
const client = await entry.server.connect();
|
|
289
|
+
const [toolsResult, resourcesResult, templatesResult, promptsResult] = await Promise.all([
|
|
290
|
+
client.listTools(),
|
|
291
|
+
client.listResources(),
|
|
292
|
+
client.listResourceTemplates(),
|
|
293
|
+
client.listPrompts()
|
|
294
|
+
]);
|
|
295
|
+
const connected = {
|
|
296
|
+
...entry,
|
|
297
|
+
client,
|
|
298
|
+
tools: toolsResult,
|
|
299
|
+
resources: resourcesResult.resources ?? [],
|
|
300
|
+
resourceTemplates: templatesResult.resourceTemplates ?? [],
|
|
301
|
+
prompts: promptsResult.prompts ?? [],
|
|
302
|
+
status: "connected",
|
|
303
|
+
error: null
|
|
304
|
+
};
|
|
305
|
+
this.entries.set(name, connected);
|
|
306
|
+
this.notify();
|
|
307
|
+
this.watchToolList(name, client);
|
|
308
|
+
return client;
|
|
309
|
+
} catch (err) {
|
|
310
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
311
|
+
this.entries.set(name, { ...entry, error, status: "error" });
|
|
312
|
+
this.notify();
|
|
313
|
+
throw error;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
async connectAll() {
|
|
317
|
+
await Promise.all(this.list().map((name) => this.connect(name)));
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Keep `name`'s tool list current: re-list through `client` now, and whenever the server
|
|
321
|
+
* announces `notifications/tools/list_changed` (a dynamic tool registered or removed, say). The
|
|
322
|
+
* first listing catches a change made before the watch began. Only the newest listing is applied,
|
|
323
|
+
* and only while `client` is still the entry's client.
|
|
324
|
+
*/
|
|
325
|
+
watchToolList(name, client) {
|
|
326
|
+
this.stopToolListWatch(name);
|
|
327
|
+
if (typeof client.onNotification !== "function") return;
|
|
328
|
+
let latest = 0;
|
|
329
|
+
const relist = () => {
|
|
330
|
+
const listing = ++latest;
|
|
331
|
+
client.listTools().then((tools) => {
|
|
332
|
+
const entry = this.entries.get(name);
|
|
333
|
+
if (listing !== latest || !entry || entry.client !== client) return;
|
|
334
|
+
if (sameListing(entry.tools, tools)) return;
|
|
335
|
+
this.update(name, { tools });
|
|
336
|
+
}).catch(() => {
|
|
337
|
+
});
|
|
338
|
+
};
|
|
339
|
+
const stop = client.onNotification((notification) => {
|
|
340
|
+
if (notification.method === "notifications/tools/list_changed") relist();
|
|
341
|
+
});
|
|
342
|
+
this.toolListWatches.set(name, stop);
|
|
343
|
+
relist();
|
|
344
|
+
}
|
|
345
|
+
update(name, partial) {
|
|
346
|
+
const entry = this.entries.get(name);
|
|
347
|
+
if (entry) {
|
|
348
|
+
this.entries.set(name, { ...entry, ...partial });
|
|
349
|
+
this.notify();
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
clear() {
|
|
353
|
+
for (const name of [...this.toolListWatches.keys()]) this.stopToolListWatch(name);
|
|
354
|
+
this.entries.clear();
|
|
355
|
+
this.notify();
|
|
356
|
+
}
|
|
357
|
+
subscribe(listener) {
|
|
358
|
+
this.listeners.add(listener);
|
|
359
|
+
return () => {
|
|
360
|
+
this.listeners.delete(listener);
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
getVersion() {
|
|
364
|
+
return this.version;
|
|
365
|
+
}
|
|
366
|
+
stopToolListWatch(name) {
|
|
367
|
+
const stop = this.toolListWatches.get(name);
|
|
368
|
+
if (!stop) return;
|
|
369
|
+
this.toolListWatches.delete(name);
|
|
370
|
+
stop();
|
|
371
|
+
}
|
|
372
|
+
notify() {
|
|
373
|
+
this.version++;
|
|
374
|
+
this.listeners.forEach((l) => l());
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
var REGISTRY_KEY = /* @__PURE__ */ Symbol.for("@frontmcp/react/serverRegistry");
|
|
378
|
+
function getOrCreateRegistry() {
|
|
379
|
+
const host = globalThis;
|
|
380
|
+
return host[REGISTRY_KEY] ??= new ServerRegistry();
|
|
381
|
+
}
|
|
382
|
+
var serverRegistry = getOrCreateRegistry();
|
|
383
|
+
|
|
384
|
+
// libs/react/src/hooks/useServer.ts
|
|
385
|
+
function useServer(name) {
|
|
386
|
+
const ctx = useContext(FrontMcpContext);
|
|
387
|
+
const serverName = name ?? ctx.name;
|
|
388
|
+
const subscribe = useCallback((cb) => serverRegistry.subscribe(cb), []);
|
|
389
|
+
const getSnapshot = useCallback(() => {
|
|
390
|
+
return serverRegistry.get(serverName);
|
|
391
|
+
}, [serverName]);
|
|
392
|
+
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// libs/react/src/hooks/useResolvedServer.ts
|
|
396
|
+
function useResolvedServer(serverName) {
|
|
397
|
+
const ctx = useContext2(FrontMcpContext);
|
|
398
|
+
const resolvedName = serverName ?? ctx.name;
|
|
399
|
+
const entry = useServer(resolvedName);
|
|
400
|
+
return { entry, name: resolvedName, registry: ctx.registry, connect: ctx.connect };
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// libs/react/src/hooks/usePublishEvent.ts
|
|
404
|
+
function usePublishEvent(serverName) {
|
|
405
|
+
const { entry, name } = useResolvedServer(serverName);
|
|
406
|
+
const server = entry?.server;
|
|
407
|
+
return useCallback2(
|
|
408
|
+
((event2, data, options) => {
|
|
409
|
+
if (!server) return Promise.reject(new Error(`FrontMCP server "${name}" is not registered`));
|
|
410
|
+
return server.events.publish(event2, data, options);
|
|
411
|
+
}),
|
|
412
|
+
[server, name]
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
|
|
5
416
|
// libs/react/src/router/router-bridge.ts
|
|
417
|
+
var ROUTE_CHANGED_EVENT = "route.changed";
|
|
6
418
|
var currentNavigate = null;
|
|
7
419
|
var currentLocation = null;
|
|
8
420
|
function setNavigate(fn) {
|
|
@@ -23,9 +435,14 @@ function clearBridge() {
|
|
|
23
435
|
}
|
|
24
436
|
|
|
25
437
|
// libs/react/src/router/useRouterBridge.ts
|
|
26
|
-
function useRouterBridge() {
|
|
438
|
+
function useRouterBridge(options = {}) {
|
|
27
439
|
const navigate = useNavigate();
|
|
28
|
-
const
|
|
440
|
+
const { pathname, search, hash } = useLocation();
|
|
441
|
+
const { routerEvents } = options;
|
|
442
|
+
const eventsOptions = typeof routerEvents === "object" ? routerEvents : {};
|
|
443
|
+
const eventName = eventsOptions.eventName ?? ROUTE_CHANGED_EVENT;
|
|
444
|
+
const publish = usePublishEvent(eventsOptions.server);
|
|
445
|
+
const previousPath = useRef(void 0);
|
|
29
446
|
useEffect(() => {
|
|
30
447
|
setNavigate(navigate);
|
|
31
448
|
return () => {
|
|
@@ -33,12 +450,32 @@ function useRouterBridge() {
|
|
|
33
450
|
};
|
|
34
451
|
}, [navigate]);
|
|
35
452
|
useEffect(() => {
|
|
36
|
-
setLocation({
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
453
|
+
setLocation({ pathname, search, hash });
|
|
454
|
+
const path = `${pathname}${search}${hash}`;
|
|
455
|
+
const previous = previousPath.current;
|
|
456
|
+
previousPath.current = path;
|
|
457
|
+
if (!routerEvents || previous === void 0 || previous === path) return;
|
|
458
|
+
publish(eventName, { pathname, search, hash, previous }).catch((error) => {
|
|
459
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
460
|
+
console.warn(`[frontmcp] the navigation was not published as "${eventName}": ${message}`);
|
|
40
461
|
});
|
|
41
|
-
}, [
|
|
462
|
+
}, [pathname, search, hash, routerEvents, eventName, publish]);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
// libs/react/src/router/route-changed.event.ts
|
|
466
|
+
import { z } from "@frontmcp/lazy-zod";
|
|
467
|
+
import { event } from "@frontmcp/sdk";
|
|
468
|
+
function createRouteChangedEvent(name = ROUTE_CHANGED_EVENT) {
|
|
469
|
+
return event({
|
|
470
|
+
name,
|
|
471
|
+
description: "Fires when the page navigates to another route",
|
|
472
|
+
payloadSchema: {
|
|
473
|
+
pathname: z.string().describe("The path navigated to"),
|
|
474
|
+
search: z.string().describe('The query string, with its leading "?", or empty'),
|
|
475
|
+
hash: z.string().describe('The fragment, with its leading "#", or empty'),
|
|
476
|
+
previous: z.string().describe("The path, query and fragment navigated from")
|
|
477
|
+
}
|
|
478
|
+
});
|
|
42
479
|
}
|
|
43
480
|
|
|
44
481
|
// libs/react/src/router/navigate.tool.ts
|
|
@@ -137,15 +574,15 @@ var CurrentRouteResource = class _CurrentRouteResource {
|
|
|
137
574
|
};
|
|
138
575
|
|
|
139
576
|
// libs/react/src/router/router.entries.ts
|
|
140
|
-
import { z } from "@frontmcp/lazy-zod";
|
|
577
|
+
import { z as z2 } from "@frontmcp/lazy-zod";
|
|
141
578
|
import { resource, tool } from "@frontmcp/sdk";
|
|
142
579
|
function createRouterEntries() {
|
|
143
580
|
const navigate = tool({
|
|
144
581
|
name: NavigateTool.toolName,
|
|
145
582
|
description: NavigateTool.description,
|
|
146
583
|
inputSchema: {
|
|
147
|
-
path:
|
|
148
|
-
replace:
|
|
584
|
+
path: z2.string().describe("The URL path to navigate to"),
|
|
585
|
+
replace: z2.boolean().optional().describe("Replace current history entry instead of pushing")
|
|
149
586
|
}
|
|
150
587
|
})((input) => NavigateTool.execute(input));
|
|
151
588
|
const goBack = tool({
|
|
@@ -165,7 +602,9 @@ export {
|
|
|
165
602
|
CurrentRouteResource,
|
|
166
603
|
GoBackTool,
|
|
167
604
|
NavigateTool,
|
|
605
|
+
ROUTE_CHANGED_EVENT,
|
|
168
606
|
clearBridge,
|
|
607
|
+
createRouteChangedEvent,
|
|
169
608
|
createRouterEntries,
|
|
170
609
|
getLocation,
|
|
171
610
|
getNavigate,
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* createRemoteEventForwarder — publish MCP Events on another FrontMCP server through its HTTP publish endpoint
|
|
3
|
+
* (`events.publishEndpoint`, bearer mode), with the signed-in user's token.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* const remote = createRemoteEventForwarder({
|
|
8
|
+
* url: 'https://api.example.com/events/publish',
|
|
9
|
+
* getToken: () => auth.getAccessToken(),
|
|
10
|
+
* });
|
|
11
|
+
* await remote.forward('cart.checked_out', { cartId });
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
import type { EventPublishOptions, EventPublishResult } from '@frontmcp/sdk';
|
|
15
|
+
export interface RemoteEventForwarderOptions {
|
|
16
|
+
/** The publish endpoint of the remote server, `/events/publish` by default there. */
|
|
17
|
+
url: string;
|
|
18
|
+
/** The bearer token to send; none sends no `Authorization` header. */
|
|
19
|
+
getToken: () => string | undefined | null | Promise<string | undefined | null>;
|
|
20
|
+
/** The `fetch` to use. @default globalThis.fetch */
|
|
21
|
+
fetch?: typeof fetch;
|
|
22
|
+
}
|
|
23
|
+
export interface RemoteEventForwarder {
|
|
24
|
+
/** POST one event; resolves with the remote `{ eventId, matched }`. */
|
|
25
|
+
forward(name: string, data: Record<string, unknown>, options?: EventPublishOptions): Promise<EventPublishResult>;
|
|
26
|
+
}
|
|
27
|
+
/** The remote endpoint did not accept the event: 401/403 credentials, 404 unknown event, 422 invalid data, 413 size. */
|
|
28
|
+
export declare class RemoteEventForwardError extends Error {
|
|
29
|
+
readonly status: number;
|
|
30
|
+
readonly body: string;
|
|
31
|
+
/** Carries the HTTP status and the body the endpoint answered with. */
|
|
32
|
+
constructor(status: number, body: string);
|
|
33
|
+
}
|
|
34
|
+
/** A forwarder that POSTs `{ name, data, ...options }` as JSON to `url` with the user's bearer token. */
|
|
35
|
+
export declare function createRemoteEventForwarder(options: RemoteEventForwarderOptions): RemoteEventForwarder;
|
|
36
|
+
//# sourceMappingURL=createRemoteEventForwarder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createRemoteEventForwarder.d.ts","sourceRoot":"","sources":["../../src/events/createRemoteEventForwarder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAE7E,MAAM,WAAW,2BAA2B;IAC1C,qFAAqF;IACrF,GAAG,EAAE,MAAM,CAAC;IACZ,sEAAsE;IACtE,QAAQ,EAAE,MAAM,MAAM,GAAG,SAAS,GAAG,IAAI,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,GAAG,IAAI,CAAC,CAAC;IAC/E,oDAAoD;IACpD,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;CACtB;AAED,MAAM,WAAW,oBAAoB;IACnC,uEAAuE;IACvE,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAAC;CAClH;AAED,wHAAwH;AACxH,qBAAa,uBAAwB,SAAQ,KAAK;IAG9C,QAAQ,CAAC,MAAM,EAAE,MAAM;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM;IAHvB,uEAAuE;gBAE5D,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM;CAKxB;AAED,yGAAyG;AACzG,wBAAgB,0BAA0B,CAAC,OAAO,EAAE,2BAA2B,GAAG,oBAAoB,CAcrG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/events/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AACnG,YAAY,EAAE,oBAAoB,EAAE,2BAA2B,EAAE,MAAM,8BAA8B,CAAC"}
|
package/hooks/index.d.ts
CHANGED
|
@@ -18,4 +18,9 @@ export { useDynamicResource } from './useDynamicResource';
|
|
|
18
18
|
export type { UseDynamicResourceOptions } from './useDynamicResource';
|
|
19
19
|
export { useComponentTree } from './useComponentTree';
|
|
20
20
|
export type { UseComponentTreeOptions } from './useComponentTree';
|
|
21
|
+
export { useEventSubscription } from './useEventSubscription';
|
|
22
|
+
export type { EventSubscriptionStatus, UseEventSubscriptionOptions, UseEventSubscriptionResult, } from './useEventSubscription';
|
|
23
|
+
export { usePublishEvent } from './usePublishEvent';
|
|
24
|
+
export { useDomEventSource } from './useDomEventSource';
|
|
25
|
+
export type { DomEventSourceEvent, UseDomEventSourceOptions } from './useDomEventSource';
|
|
21
26
|
//# sourceMappingURL=index.d.ts.map
|
package/hooks/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,iBAAiB,EAAE,MAA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an MCP Event when a DOM element fires an event. It listens on the target it is given
|
|
3
|
+
* only: there are no global listeners.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```tsx
|
|
7
|
+
* const form = useRef<HTMLFormElement>(null);
|
|
8
|
+
* useDomEventSource(form, 'submit', () => ({ name: 'checkout.submitted', data: {} }));
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
import { type RefObject } from 'react';
|
|
12
|
+
import type { EventPublishOptions } from '@frontmcp/sdk';
|
|
13
|
+
/** The event to publish for a DOM event, or nothing to skip it. */
|
|
14
|
+
export interface DomEventSourceEvent {
|
|
15
|
+
name: string;
|
|
16
|
+
data: Record<string, unknown>;
|
|
17
|
+
options?: EventPublishOptions;
|
|
18
|
+
}
|
|
19
|
+
export interface UseDomEventSourceOptions {
|
|
20
|
+
/** The server to publish on; the provider's by default. */
|
|
21
|
+
server?: string;
|
|
22
|
+
/** Told when a publish fails; a warning is logged otherwise. */
|
|
23
|
+
onError?: (error: Error) => void;
|
|
24
|
+
}
|
|
25
|
+
/** Listen for `type` on `target` (an element, a ref to one, or any `EventTarget`) and publish what `toEvent` maps it to. */
|
|
26
|
+
export declare function useDomEventSource<DomEvent extends Event = Event>(target: EventTarget | RefObject<EventTarget | null> | null | undefined, type: string, toEvent: (domEvent: DomEvent) => DomEventSourceEvent | undefined, options?: UseDomEventSourceOptions): void;
|
|
27
|
+
//# sourceMappingURL=useDomEventSource.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDomEventSource.d.ts","sourceRoot":"","sources":["../../src/hooks/useDomEventSource.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAqB,KAAK,SAAS,EAAE,MAAM,OAAO,CAAC;AAE1D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAKzD,mEAAmE;AACnE,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9B,OAAO,CAAC,EAAE,mBAAmB,CAAC;CAC/B;AAED,MAAM,WAAW,wBAAwB;IACvC,2DAA2D;IAC3D,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,gEAAgE;IAChE,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;CAClC;AAED,4HAA4H;AAC5H,wBAAgB,iBAAiB,CAAC,QAAQ,SAAS,KAAK,GAAG,KAAK,EAC9D,MAAM,EAAE,WAAW,GAAG,SAAS,CAAC,WAAW,GAAG,IAAI,CAAC,GAAG,IAAI,GAAG,SAAS,EACtE,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,CAAC,QAAQ,EAAE,QAAQ,KAAK,mBAAmB,GAAG,SAAS,EAChE,OAAO,GAAE,wBAA6B,GACrC,IAAI,CAwBN"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* useEventSubscription — subscribe to an MCP Event of the provider's server through its in-process client, and run
|
|
3
|
+
* a handler for each occurrence. Delivery stays in the page: push over the in-memory connection, or poll.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```tsx
|
|
7
|
+
* const { status } = useEventSubscription('order.created', { status: 'paid' }, (occurrence) => {
|
|
8
|
+
* toast(`Order ${occurrence.data.orderId}`);
|
|
9
|
+
* });
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
import type { EventDeliveryMode, EventOccurrence } from '@frontmcp/sdk';
|
|
13
|
+
export interface UseEventSubscriptionOptions {
|
|
14
|
+
/** Runs for each occurrence. */
|
|
15
|
+
onEvent: (occurrence: EventOccurrence) => void;
|
|
16
|
+
/** Told when the subscription is refused or ended by the server. */
|
|
17
|
+
onError?: (error: Error) => void;
|
|
18
|
+
/** The server to subscribe on; the provider's by default. */
|
|
19
|
+
server?: string;
|
|
20
|
+
/** Force a delivery mode instead of letting the client choose. */
|
|
21
|
+
delivery?: EventDeliveryMode;
|
|
22
|
+
/** `false` holds no subscription. @default true */
|
|
23
|
+
enabled?: boolean;
|
|
24
|
+
}
|
|
25
|
+
export type EventSubscriptionStatus = 'idle' | 'subscribing' | 'active' | 'ended' | 'error';
|
|
26
|
+
export interface UseEventSubscriptionResult {
|
|
27
|
+
status: EventSubscriptionStatus;
|
|
28
|
+
/** The delivery mode in use, once subscribed. */
|
|
29
|
+
mode?: EventDeliveryMode;
|
|
30
|
+
error?: Error;
|
|
31
|
+
}
|
|
32
|
+
/** Subscribe while the component is mounted and the server's client is connected; unsubscribe on unmount. */
|
|
33
|
+
export declare function useEventSubscription(name: string, args: Record<string, unknown> | undefined, handlerOrOptions: UseEventSubscriptionOptions['onEvent'] | UseEventSubscriptionOptions): UseEventSubscriptionResult;
|
|
34
|
+
//# sourceMappingURL=useEventSubscription.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useEventSubscription.d.ts","sourceRoot":"","sources":["../../src/hooks/useEventSubscription.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAIH,OAAO,KAAK,EAAE,iBAAiB,EAAE,eAAe,EAAqB,MAAM,eAAe,CAAC;AAK3F,MAAM,WAAW,2BAA2B;IAC1C,gCAAgC;IAChC,OAAO,EAAE,CAAC,UAAU,EAAE,eAAe,KAAK,IAAI,CAAC;IAC/C,oEAAoE;IACpE,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IACjC,6DAA6D;IAC7D,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,kEAAkE;IAClE,QAAQ,CAAC,EAAE,iBAAiB,CAAC;IAC7B,mDAAmD;IACnD,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED,MAAM,MAAM,uBAAuB,GAAG,MAAM,GAAG,aAAa,GAAG,QAAQ,GAAG,OAAO,GAAG,OAAO,CAAC;AAE5F,MAAM,WAAW,0BAA0B;IACzC,MAAM,EAAE,uBAAuB,CAAC;IAChC,iDAAiD;IACjD,IAAI,CAAC,EAAE,iBAAiB,CAAC;IACzB,KAAK,CAAC,EAAE,KAAK,CAAC;CACf;AAOD,6GAA6G;AAC7G,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,EACzC,gBAAgB,EAAE,2BAA2B,CAAC,SAAS,CAAC,GAAG,2BAA2B,GACrF,0BAA0B,CA+C5B"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* usePublishEvent — a function that publishes an MCP Event on the provider's server, as `this.events.publish` does
|
|
3
|
+
* in a tool. Typed by the event: `publish(orderPlaced, data)` checks `data` against its payload schema's type.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```tsx
|
|
7
|
+
* const publish = usePublishEvent();
|
|
8
|
+
* <button onClick={() => publish(cartUpdated, { items: cart.length })}>Save</button>
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
import type { EventPublisher } from '@frontmcp/sdk';
|
|
12
|
+
/** Publish on the provider's server, or on the server `serverName` names. */
|
|
13
|
+
export declare function usePublishEvent(serverName?: string): EventPublisher['publish'];
|
|
14
|
+
//# sourceMappingURL=usePublishEvent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"usePublishEvent.d.ts","sourceRoot":"","sources":["../../src/hooks/usePublishEvent.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAIpD,6EAA6E;AAC7E,wBAAgB,eAAe,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,cAAc,CAAC,SAAS,CAAC,CAW9E"}
|