@frontmcp/react 1.11.2 → 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/ai/createToolHandler.d.ts +1 -1
- package/ai/createToolHandler.d.ts.map +1 -1
- package/ai/types.d.ts +2 -2
- package/ai/types.d.ts.map +1 -1
- package/ai/useAITools.d.ts +1 -1
- package/ai/useAITools.d.ts.map +1 -1
- package/ai/useTools.d.ts +1 -1
- package/ai/useTools.d.ts.map +1 -1
- package/components/AgentContent.d.ts +1 -2
- package/components/AgentContent.d.ts.map +1 -1
- package/components/AgentSearch.d.ts +1 -2
- package/components/AgentSearch.d.ts.map +1 -1
- package/components/DynamicRenderer.d.ts.map +1 -1
- package/components/PromptForm.d.ts +1 -1
- package/components/PromptForm.d.ts.map +1 -1
- package/esm/ai/index.mjs +5 -3
- package/esm/index.mjs +153 -22
- package/esm/package.json +6 -6
- package/esm/router/index.mjs +451 -12
- 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/useCallTool.d.ts.map +1 -1
- package/hooks/useComponentTree.d.ts +1 -1
- package/hooks/useComponentTree.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/useGetPrompt.d.ts.map +1 -1
- package/hooks/usePublishEvent.d.ts +14 -0
- package/hooks/usePublishEvent.d.ts.map +1 -0
- package/hooks/useReadResource.d.ts.map +1 -1
- package/hooks/useResolvedServer.d.ts +1 -1
- package/hooks/useResolvedServer.d.ts.map +1 -1
- 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/current-route.resource.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/state/useStoreRegistration.d.ts.map +1 -1
- package/state/useValtioResource.d.ts.map +1 -1
package/router/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/router/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AACvD,YAAY,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACtD,YAAY,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,WAAW,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/router/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,YAAY,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,uBAAuB,EAAE,MAAM,uBAAuB,CAAC;AAChE,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AACvD,YAAY,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACtD,YAAY,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,WAAW,EAAE,WAAW,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC"}
|
package/router/index.js
CHANGED
|
@@ -23,7 +23,9 @@ __export(router_exports, {
|
|
|
23
23
|
CurrentRouteResource: () => CurrentRouteResource,
|
|
24
24
|
GoBackTool: () => GoBackTool,
|
|
25
25
|
NavigateTool: () => NavigateTool,
|
|
26
|
+
ROUTE_CHANGED_EVENT: () => ROUTE_CHANGED_EVENT,
|
|
26
27
|
clearBridge: () => clearBridge,
|
|
28
|
+
createRouteChangedEvent: () => createRouteChangedEvent,
|
|
27
29
|
createRouterEntries: () => createRouterEntries,
|
|
28
30
|
getLocation: () => getLocation,
|
|
29
31
|
getNavigate: () => getNavigate,
|
|
@@ -34,10 +36,422 @@ __export(router_exports, {
|
|
|
34
36
|
module.exports = __toCommonJS(router_exports);
|
|
35
37
|
|
|
36
38
|
// libs/react/src/router/useRouterBridge.ts
|
|
37
|
-
var
|
|
39
|
+
var import_react5 = require("react");
|
|
38
40
|
var import_react_router_dom = require("react-router-dom");
|
|
39
41
|
|
|
42
|
+
// libs/react/src/hooks/usePublishEvent.ts
|
|
43
|
+
var import_react4 = require("react");
|
|
44
|
+
|
|
45
|
+
// libs/react/src/hooks/useResolvedServer.ts
|
|
46
|
+
var import_react3 = require("react");
|
|
47
|
+
|
|
48
|
+
// libs/react/src/provider/FrontMcpContext.ts
|
|
49
|
+
var import_react = require("react");
|
|
50
|
+
|
|
51
|
+
// libs/react/src/components/ComponentRegistry.ts
|
|
52
|
+
var ComponentRegistry = class {
|
|
53
|
+
entries = /* @__PURE__ */ new Map();
|
|
54
|
+
register(uri, component, meta) {
|
|
55
|
+
const name = extractName(uri);
|
|
56
|
+
this.entries.set(uri, { uri, name, component, description: meta?.description });
|
|
57
|
+
}
|
|
58
|
+
registerAll(map) {
|
|
59
|
+
for (const [uri, component] of Object.entries(map)) {
|
|
60
|
+
this.register(uri, component);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
get(uri) {
|
|
64
|
+
return this.entries.get(uri)?.component;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Resolve a shorthand name like `'UserCard'` by trying
|
|
68
|
+
* `component://UserCard`, `element://UserCard`, `page://UserCard`.
|
|
69
|
+
*/
|
|
70
|
+
resolve(type) {
|
|
71
|
+
const exact = this.entries.get(type);
|
|
72
|
+
if (exact) return exact.component;
|
|
73
|
+
for (const protocol of ["component://", "element://", "page://"]) {
|
|
74
|
+
const entry = this.entries.get(`${protocol}${type}`);
|
|
75
|
+
if (entry) return entry.component;
|
|
76
|
+
}
|
|
77
|
+
return void 0;
|
|
78
|
+
}
|
|
79
|
+
has(uri) {
|
|
80
|
+
return this.entries.has(uri);
|
|
81
|
+
}
|
|
82
|
+
list() {
|
|
83
|
+
return Array.from(this.entries.values()).map(({ uri, name, description }) => ({
|
|
84
|
+
uri,
|
|
85
|
+
name,
|
|
86
|
+
description
|
|
87
|
+
}));
|
|
88
|
+
}
|
|
89
|
+
clear() {
|
|
90
|
+
this.entries.clear();
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
function extractName(uri) {
|
|
94
|
+
const idx = uri.indexOf("://");
|
|
95
|
+
return idx >= 0 ? uri.slice(idx + 3) : uri;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// libs/react/src/registry/DynamicRegistry.ts
|
|
99
|
+
var DynamicRegistry = class {
|
|
100
|
+
tools = /* @__PURE__ */ new Map();
|
|
101
|
+
resources = /* @__PURE__ */ new Map();
|
|
102
|
+
/** Every live registration of a tool name, oldest first; the newest one is the definition in use. */
|
|
103
|
+
toolRegistrations = /* @__PURE__ */ new Map();
|
|
104
|
+
resourceRefCounts = /* @__PURE__ */ new Map();
|
|
105
|
+
listeners = /* @__PURE__ */ new Set();
|
|
106
|
+
resourceListeners = /* @__PURE__ */ new Set();
|
|
107
|
+
version = 0;
|
|
108
|
+
/**
|
|
109
|
+
* Register a dynamic tool. Returns an unregister function
|
|
110
|
+
* suitable for useEffect cleanup.
|
|
111
|
+
*
|
|
112
|
+
* Multiple registrations of the same name are ref-counted:
|
|
113
|
+
* subsequent registrations replace the definition (and notify, so
|
|
114
|
+
* the server registration follows the new description, schema or
|
|
115
|
+
* availability) but the tool is only removed when every registrant
|
|
116
|
+
* has unregistered. When the registrant whose definition is in use
|
|
117
|
+
* unregisters, the newest remaining registration takes over, so a
|
|
118
|
+
* call never reaches an unmounted component's `execute`.
|
|
119
|
+
*/
|
|
120
|
+
registerTool(def) {
|
|
121
|
+
const registrations = this.toolRegistrations.get(def.name) ?? [];
|
|
122
|
+
registrations.push(def);
|
|
123
|
+
this.toolRegistrations.set(def.name, registrations);
|
|
124
|
+
this.tools.set(def.name, def);
|
|
125
|
+
this.notify();
|
|
126
|
+
let called = false;
|
|
127
|
+
return () => {
|
|
128
|
+
if (called) return;
|
|
129
|
+
called = true;
|
|
130
|
+
this.removeToolRegistration(def);
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
/** Drop the oldest registration of `name` (registrants should call the function `registerTool` returned). */
|
|
134
|
+
unregisterTool(name) {
|
|
135
|
+
const oldest = this.toolRegistrations.get(name)?.[0];
|
|
136
|
+
if (oldest) this.removeToolRegistration(oldest);
|
|
137
|
+
}
|
|
138
|
+
removeToolRegistration(def) {
|
|
139
|
+
const registrations = this.toolRegistrations.get(def.name);
|
|
140
|
+
const index = registrations?.lastIndexOf(def) ?? -1;
|
|
141
|
+
if (!registrations || index === -1) return;
|
|
142
|
+
registrations.splice(index, 1);
|
|
143
|
+
const newest = registrations[registrations.length - 1];
|
|
144
|
+
if (!newest) {
|
|
145
|
+
this.toolRegistrations.delete(def.name);
|
|
146
|
+
this.tools.delete(def.name);
|
|
147
|
+
this.notify();
|
|
148
|
+
} else if (this.tools.get(def.name) !== newest) {
|
|
149
|
+
this.tools.set(def.name, newest);
|
|
150
|
+
this.notify();
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Register a dynamic resource. Returns an unregister function
|
|
155
|
+
* suitable for useEffect cleanup.
|
|
156
|
+
*
|
|
157
|
+
* Multiple registrations of the same URI are ref-counted.
|
|
158
|
+
*/
|
|
159
|
+
registerResource(def) {
|
|
160
|
+
const existing = this.resourceRefCounts.get(def.uri) ?? 0;
|
|
161
|
+
this.resourceRefCounts.set(def.uri, existing + 1);
|
|
162
|
+
this.resources.set(def.uri, def);
|
|
163
|
+
if (existing === 0) {
|
|
164
|
+
this.notify();
|
|
165
|
+
}
|
|
166
|
+
let called = false;
|
|
167
|
+
return () => {
|
|
168
|
+
if (called) return;
|
|
169
|
+
called = true;
|
|
170
|
+
this.unregisterResource(def.uri);
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
unregisterResource(uri) {
|
|
174
|
+
const count = this.resourceRefCounts.get(uri);
|
|
175
|
+
if (count == null) return;
|
|
176
|
+
if (count <= 1) {
|
|
177
|
+
this.resourceRefCounts.delete(uri);
|
|
178
|
+
this.resources.delete(uri);
|
|
179
|
+
this.notify();
|
|
180
|
+
} else {
|
|
181
|
+
this.resourceRefCounts.set(uri, count - 1);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/** Update the execute function for an existing tool (for stale closure prevention). */
|
|
185
|
+
updateToolExecute(name, execute) {
|
|
186
|
+
const existing = this.tools.get(name);
|
|
187
|
+
if (existing) {
|
|
188
|
+
existing.execute = execute;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/** Update the read function for an existing resource and notify subscribers. */
|
|
192
|
+
updateResourceRead(uri, read) {
|
|
193
|
+
const existing = this.resources.get(uri);
|
|
194
|
+
if (existing) {
|
|
195
|
+
existing.read = read;
|
|
196
|
+
this.notify();
|
|
197
|
+
this.resourceListeners.forEach((l) => {
|
|
198
|
+
l(uri);
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/** Listen for changes to a dynamic resource's content (the in-page `resources/updated`). */
|
|
203
|
+
onResourceUpdated(listener) {
|
|
204
|
+
this.resourceListeners.add(listener);
|
|
205
|
+
return () => {
|
|
206
|
+
this.resourceListeners.delete(listener);
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
getTools() {
|
|
210
|
+
return [...this.tools.values()];
|
|
211
|
+
}
|
|
212
|
+
getResources() {
|
|
213
|
+
return [...this.resources.values()];
|
|
214
|
+
}
|
|
215
|
+
findTool(name) {
|
|
216
|
+
return this.tools.get(name);
|
|
217
|
+
}
|
|
218
|
+
findResource(uri) {
|
|
219
|
+
return this.resources.get(uri);
|
|
220
|
+
}
|
|
221
|
+
hasTool(name) {
|
|
222
|
+
return this.tools.has(name);
|
|
223
|
+
}
|
|
224
|
+
hasResource(uri) {
|
|
225
|
+
return this.resources.has(uri);
|
|
226
|
+
}
|
|
227
|
+
subscribe(listener) {
|
|
228
|
+
this.listeners.add(listener);
|
|
229
|
+
return () => {
|
|
230
|
+
this.listeners.delete(listener);
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
getVersion() {
|
|
234
|
+
return this.version;
|
|
235
|
+
}
|
|
236
|
+
clear() {
|
|
237
|
+
if (this.tools.size === 0 && this.resources.size === 0) return;
|
|
238
|
+
this.tools.clear();
|
|
239
|
+
this.resources.clear();
|
|
240
|
+
this.toolRegistrations.clear();
|
|
241
|
+
this.resourceRefCounts.clear();
|
|
242
|
+
this.notify();
|
|
243
|
+
}
|
|
244
|
+
notify() {
|
|
245
|
+
this.version++;
|
|
246
|
+
this.listeners.forEach((l) => {
|
|
247
|
+
l();
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
// libs/react/src/provider/FrontMcpContext.ts
|
|
253
|
+
var CONTEXT_KEY = /* @__PURE__ */ Symbol.for("@frontmcp/react/FrontMcpContext");
|
|
254
|
+
function getOrCreateContext() {
|
|
255
|
+
const host = globalThis;
|
|
256
|
+
const existing = host[CONTEXT_KEY];
|
|
257
|
+
if (existing) return existing;
|
|
258
|
+
const defaultDynamicRegistry = new DynamicRegistry();
|
|
259
|
+
const created = (0, import_react.createContext)({
|
|
260
|
+
name: "default",
|
|
261
|
+
registry: new ComponentRegistry(),
|
|
262
|
+
dynamicRegistry: defaultDynamicRegistry,
|
|
263
|
+
getDynamicRegistry: () => defaultDynamicRegistry,
|
|
264
|
+
connect: async () => {
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
host[CONTEXT_KEY] = created;
|
|
268
|
+
return created;
|
|
269
|
+
}
|
|
270
|
+
var FrontMcpContext = getOrCreateContext();
|
|
271
|
+
|
|
272
|
+
// libs/react/src/hooks/useServer.ts
|
|
273
|
+
var import_react2 = require("react");
|
|
274
|
+
|
|
275
|
+
// libs/react/src/registry/ServerRegistry.ts
|
|
276
|
+
function sameListing(a, b) {
|
|
277
|
+
if (a === b) return true;
|
|
278
|
+
if (a.length !== b.length) return false;
|
|
279
|
+
try {
|
|
280
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
281
|
+
} catch {
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
var ServerRegistry = class {
|
|
286
|
+
entries = /* @__PURE__ */ new Map();
|
|
287
|
+
listeners = /* @__PURE__ */ new Set();
|
|
288
|
+
toolListWatches = /* @__PURE__ */ new Map();
|
|
289
|
+
version = 0;
|
|
290
|
+
register(name, server) {
|
|
291
|
+
this.stopToolListWatch(name);
|
|
292
|
+
this.entries.set(name, {
|
|
293
|
+
server,
|
|
294
|
+
client: null,
|
|
295
|
+
status: "idle",
|
|
296
|
+
error: null,
|
|
297
|
+
tools: [],
|
|
298
|
+
resources: [],
|
|
299
|
+
resourceTemplates: [],
|
|
300
|
+
prompts: []
|
|
301
|
+
});
|
|
302
|
+
this.notify();
|
|
303
|
+
}
|
|
304
|
+
unregister(name) {
|
|
305
|
+
this.stopToolListWatch(name);
|
|
306
|
+
this.entries.delete(name);
|
|
307
|
+
this.notify();
|
|
308
|
+
}
|
|
309
|
+
get(name) {
|
|
310
|
+
return this.entries.get(name);
|
|
311
|
+
}
|
|
312
|
+
has(name) {
|
|
313
|
+
return this.entries.has(name);
|
|
314
|
+
}
|
|
315
|
+
list() {
|
|
316
|
+
return [...this.entries.keys()];
|
|
317
|
+
}
|
|
318
|
+
async connect(name) {
|
|
319
|
+
const entry = this.entries.get(name);
|
|
320
|
+
if (!entry) throw new Error(`Server "${name}" not registered`);
|
|
321
|
+
if (entry.client) return entry.client;
|
|
322
|
+
this.entries.set(name, { ...entry, status: "connecting", error: null });
|
|
323
|
+
this.notify();
|
|
324
|
+
try {
|
|
325
|
+
const client = await entry.server.connect();
|
|
326
|
+
const [toolsResult, resourcesResult, templatesResult, promptsResult] = await Promise.all([
|
|
327
|
+
client.listTools(),
|
|
328
|
+
client.listResources(),
|
|
329
|
+
client.listResourceTemplates(),
|
|
330
|
+
client.listPrompts()
|
|
331
|
+
]);
|
|
332
|
+
const connected = {
|
|
333
|
+
...entry,
|
|
334
|
+
client,
|
|
335
|
+
tools: toolsResult,
|
|
336
|
+
resources: resourcesResult.resources ?? [],
|
|
337
|
+
resourceTemplates: templatesResult.resourceTemplates ?? [],
|
|
338
|
+
prompts: promptsResult.prompts ?? [],
|
|
339
|
+
status: "connected",
|
|
340
|
+
error: null
|
|
341
|
+
};
|
|
342
|
+
this.entries.set(name, connected);
|
|
343
|
+
this.notify();
|
|
344
|
+
this.watchToolList(name, client);
|
|
345
|
+
return client;
|
|
346
|
+
} catch (err) {
|
|
347
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
348
|
+
this.entries.set(name, { ...entry, error, status: "error" });
|
|
349
|
+
this.notify();
|
|
350
|
+
throw error;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
async connectAll() {
|
|
354
|
+
await Promise.all(this.list().map((name) => this.connect(name)));
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Keep `name`'s tool list current: re-list through `client` now, and whenever the server
|
|
358
|
+
* announces `notifications/tools/list_changed` (a dynamic tool registered or removed, say). The
|
|
359
|
+
* first listing catches a change made before the watch began. Only the newest listing is applied,
|
|
360
|
+
* and only while `client` is still the entry's client.
|
|
361
|
+
*/
|
|
362
|
+
watchToolList(name, client) {
|
|
363
|
+
this.stopToolListWatch(name);
|
|
364
|
+
if (typeof client.onNotification !== "function") return;
|
|
365
|
+
let latest = 0;
|
|
366
|
+
const relist = () => {
|
|
367
|
+
const listing = ++latest;
|
|
368
|
+
client.listTools().then((tools) => {
|
|
369
|
+
const entry = this.entries.get(name);
|
|
370
|
+
if (listing !== latest || !entry || entry.client !== client) return;
|
|
371
|
+
if (sameListing(entry.tools, tools)) return;
|
|
372
|
+
this.update(name, { tools });
|
|
373
|
+
}).catch(() => {
|
|
374
|
+
});
|
|
375
|
+
};
|
|
376
|
+
const stop = client.onNotification((notification) => {
|
|
377
|
+
if (notification.method === "notifications/tools/list_changed") relist();
|
|
378
|
+
});
|
|
379
|
+
this.toolListWatches.set(name, stop);
|
|
380
|
+
relist();
|
|
381
|
+
}
|
|
382
|
+
update(name, partial) {
|
|
383
|
+
const entry = this.entries.get(name);
|
|
384
|
+
if (entry) {
|
|
385
|
+
this.entries.set(name, { ...entry, ...partial });
|
|
386
|
+
this.notify();
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
clear() {
|
|
390
|
+
for (const name of [...this.toolListWatches.keys()]) this.stopToolListWatch(name);
|
|
391
|
+
this.entries.clear();
|
|
392
|
+
this.notify();
|
|
393
|
+
}
|
|
394
|
+
subscribe(listener) {
|
|
395
|
+
this.listeners.add(listener);
|
|
396
|
+
return () => {
|
|
397
|
+
this.listeners.delete(listener);
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
getVersion() {
|
|
401
|
+
return this.version;
|
|
402
|
+
}
|
|
403
|
+
stopToolListWatch(name) {
|
|
404
|
+
const stop = this.toolListWatches.get(name);
|
|
405
|
+
if (!stop) return;
|
|
406
|
+
this.toolListWatches.delete(name);
|
|
407
|
+
stop();
|
|
408
|
+
}
|
|
409
|
+
notify() {
|
|
410
|
+
this.version++;
|
|
411
|
+
this.listeners.forEach((l) => l());
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
var REGISTRY_KEY = /* @__PURE__ */ Symbol.for("@frontmcp/react/serverRegistry");
|
|
415
|
+
function getOrCreateRegistry() {
|
|
416
|
+
const host = globalThis;
|
|
417
|
+
return host[REGISTRY_KEY] ??= new ServerRegistry();
|
|
418
|
+
}
|
|
419
|
+
var serverRegistry = getOrCreateRegistry();
|
|
420
|
+
|
|
421
|
+
// libs/react/src/hooks/useServer.ts
|
|
422
|
+
function useServer(name) {
|
|
423
|
+
const ctx = (0, import_react2.useContext)(FrontMcpContext);
|
|
424
|
+
const serverName = name ?? ctx.name;
|
|
425
|
+
const subscribe = (0, import_react2.useCallback)((cb) => serverRegistry.subscribe(cb), []);
|
|
426
|
+
const getSnapshot = (0, import_react2.useCallback)(() => {
|
|
427
|
+
return serverRegistry.get(serverName);
|
|
428
|
+
}, [serverName]);
|
|
429
|
+
return (0, import_react2.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// libs/react/src/hooks/useResolvedServer.ts
|
|
433
|
+
function useResolvedServer(serverName) {
|
|
434
|
+
const ctx = (0, import_react3.useContext)(FrontMcpContext);
|
|
435
|
+
const resolvedName = serverName ?? ctx.name;
|
|
436
|
+
const entry = useServer(resolvedName);
|
|
437
|
+
return { entry, name: resolvedName, registry: ctx.registry, connect: ctx.connect };
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// libs/react/src/hooks/usePublishEvent.ts
|
|
441
|
+
function usePublishEvent(serverName) {
|
|
442
|
+
const { entry, name } = useResolvedServer(serverName);
|
|
443
|
+
const server = entry?.server;
|
|
444
|
+
return (0, import_react4.useCallback)(
|
|
445
|
+
((event2, data, options) => {
|
|
446
|
+
if (!server) return Promise.reject(new Error(`FrontMCP server "${name}" is not registered`));
|
|
447
|
+
return server.events.publish(event2, data, options);
|
|
448
|
+
}),
|
|
449
|
+
[server, name]
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
|
|
40
453
|
// libs/react/src/router/router-bridge.ts
|
|
454
|
+
var ROUTE_CHANGED_EVENT = "route.changed";
|
|
41
455
|
var currentNavigate = null;
|
|
42
456
|
var currentLocation = null;
|
|
43
457
|
function setNavigate(fn) {
|
|
@@ -58,22 +472,47 @@ function clearBridge() {
|
|
|
58
472
|
}
|
|
59
473
|
|
|
60
474
|
// libs/react/src/router/useRouterBridge.ts
|
|
61
|
-
function useRouterBridge() {
|
|
475
|
+
function useRouterBridge(options = {}) {
|
|
62
476
|
const navigate = (0, import_react_router_dom.useNavigate)();
|
|
63
|
-
const
|
|
64
|
-
|
|
477
|
+
const { pathname, search, hash } = (0, import_react_router_dom.useLocation)();
|
|
478
|
+
const { routerEvents } = options;
|
|
479
|
+
const eventsOptions = typeof routerEvents === "object" ? routerEvents : {};
|
|
480
|
+
const eventName = eventsOptions.eventName ?? ROUTE_CHANGED_EVENT;
|
|
481
|
+
const publish = usePublishEvent(eventsOptions.server);
|
|
482
|
+
const previousPath = (0, import_react5.useRef)(void 0);
|
|
483
|
+
(0, import_react5.useEffect)(() => {
|
|
65
484
|
setNavigate(navigate);
|
|
66
485
|
return () => {
|
|
67
486
|
clearBridge();
|
|
68
487
|
};
|
|
69
488
|
}, [navigate]);
|
|
70
|
-
(0,
|
|
71
|
-
setLocation({
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
489
|
+
(0, import_react5.useEffect)(() => {
|
|
490
|
+
setLocation({ pathname, search, hash });
|
|
491
|
+
const path = `${pathname}${search}${hash}`;
|
|
492
|
+
const previous = previousPath.current;
|
|
493
|
+
previousPath.current = path;
|
|
494
|
+
if (!routerEvents || previous === void 0 || previous === path) return;
|
|
495
|
+
publish(eventName, { pathname, search, hash, previous }).catch((error) => {
|
|
496
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
497
|
+
console.warn(`[frontmcp] the navigation was not published as "${eventName}": ${message}`);
|
|
75
498
|
});
|
|
76
|
-
}, [
|
|
499
|
+
}, [pathname, search, hash, routerEvents, eventName, publish]);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
// libs/react/src/router/route-changed.event.ts
|
|
503
|
+
var import_lazy_zod = require("@frontmcp/lazy-zod");
|
|
504
|
+
var import_sdk = require("@frontmcp/sdk");
|
|
505
|
+
function createRouteChangedEvent(name = ROUTE_CHANGED_EVENT) {
|
|
506
|
+
return (0, import_sdk.event)({
|
|
507
|
+
name,
|
|
508
|
+
description: "Fires when the page navigates to another route",
|
|
509
|
+
payloadSchema: {
|
|
510
|
+
pathname: import_lazy_zod.z.string().describe("The path navigated to"),
|
|
511
|
+
search: import_lazy_zod.z.string().describe('The query string, with its leading "?", or empty'),
|
|
512
|
+
hash: import_lazy_zod.z.string().describe('The fragment, with its leading "#", or empty'),
|
|
513
|
+
previous: import_lazy_zod.z.string().describe("The path, query and fragment navigated from")
|
|
514
|
+
}
|
|
515
|
+
});
|
|
77
516
|
}
|
|
78
517
|
|
|
79
518
|
// libs/react/src/router/navigate.tool.ts
|
|
@@ -172,23 +611,23 @@ var CurrentRouteResource = class _CurrentRouteResource {
|
|
|
172
611
|
};
|
|
173
612
|
|
|
174
613
|
// libs/react/src/router/router.entries.ts
|
|
175
|
-
var
|
|
176
|
-
var
|
|
614
|
+
var import_lazy_zod2 = require("@frontmcp/lazy-zod");
|
|
615
|
+
var import_sdk2 = require("@frontmcp/sdk");
|
|
177
616
|
function createRouterEntries() {
|
|
178
|
-
const navigate = (0,
|
|
617
|
+
const navigate = (0, import_sdk2.tool)({
|
|
179
618
|
name: NavigateTool.toolName,
|
|
180
619
|
description: NavigateTool.description,
|
|
181
620
|
inputSchema: {
|
|
182
|
-
path:
|
|
183
|
-
replace:
|
|
621
|
+
path: import_lazy_zod2.z.string().describe("The URL path to navigate to"),
|
|
622
|
+
replace: import_lazy_zod2.z.boolean().optional().describe("Replace current history entry instead of pushing")
|
|
184
623
|
}
|
|
185
624
|
})((input) => NavigateTool.execute(input));
|
|
186
|
-
const goBack = (0,
|
|
625
|
+
const goBack = (0, import_sdk2.tool)({
|
|
187
626
|
name: GoBackTool.toolName,
|
|
188
627
|
description: GoBackTool.description,
|
|
189
628
|
inputSchema: {}
|
|
190
629
|
})(() => GoBackTool.execute());
|
|
191
|
-
const currentRoute = (0,
|
|
630
|
+
const currentRoute = (0, import_sdk2.resource)({
|
|
192
631
|
name: CurrentRouteResource.resourceName,
|
|
193
632
|
uri: CurrentRouteResource.uri,
|
|
194
633
|
description: CurrentRouteResource.description,
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** The event a navigation publishes: declare it on the server (`create({ eventDefinitions })`) to let clients subscribe. */
|
|
2
|
+
export declare function createRouteChangedEvent(name?: string): import("@frontmcp/sdk").EventFunction<Record<string, never>, {
|
|
3
|
+
pathname: string;
|
|
4
|
+
search: string;
|
|
5
|
+
hash: string;
|
|
6
|
+
previous: string;
|
|
7
|
+
}>;
|
|
8
|
+
//# sourceMappingURL=route-changed.event.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route-changed.event.d.ts","sourceRoot":"","sources":["../../src/router/route-changed.event.ts"],"names":[],"mappings":"AAKA,4HAA4H;AAC5H,wBAAgB,uBAAuB,CAAC,IAAI,GAAE,MAA4B;;;;;GAWzE"}
|
|
@@ -4,6 +4,8 @@
|
|
|
4
4
|
* Single-threaded browser context means no async context needed.
|
|
5
5
|
* `useRouterBridge()` populates this on every render/route change.
|
|
6
6
|
*/
|
|
7
|
+
/** The name `useRouterBridge({ routerEvents: true })` publishes under. */
|
|
8
|
+
export declare const ROUTE_CHANGED_EVENT = "route.changed";
|
|
7
9
|
export interface BridgeLocation {
|
|
8
10
|
pathname: string;
|
|
9
11
|
search: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router-bridge.d.ts","sourceRoot":"","sources":["../../src/router/router-bridge.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,UAAU,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EAAE;IAAE,OAAO,CAAC,EAAE,OAAO,CAAA;CAAE,KAAK,IAAI,CAAC;AAKxF,wBAAgB,WAAW,CAAC,EAAE,EAAE,UAAU,GAAG,IAAI,CAEhD;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,cAAc,GAAG,IAAI,CAErD;AAED,wBAAgB,WAAW,IAAI,UAAU,GAAG,IAAI,CAE/C;AAED,wBAAgB,WAAW,IAAI,cAAc,GAAG,IAAI,CAEnD;AAED,wBAAgB,WAAW,IAAI,IAAI,CAGlC"}
|
|
1
|
+
{"version":3,"file":"router-bridge.d.ts","sourceRoot":"","sources":["../../src/router/router-bridge.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,0EAA0E;AAC1E,eAAO,MAAM,mBAAmB,kBAAkB,CAAC;AAEnD,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,UAAU,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EAAE;IAAE,OAAO,CAAC,EAAE,OAAO,CAAA;CAAE,KAAK,IAAI,CAAC;AAKxF,wBAAgB,WAAW,CAAC,EAAE,EAAE,UAAU,GAAG,IAAI,CAEhD;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,cAAc,GAAG,IAAI,CAErD;AAED,wBAAgB,WAAW,IAAI,UAAU,GAAG,IAAI,CAE/C;AAED,wBAAgB,WAAW,IAAI,cAAc,GAAG,IAAI,CAEnD;AAED,wBAAgB,WAAW,IAAI,IAAI,CAGlC"}
|
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* useRouterBridge — wires React Router's navigate/location into the bridge singleton.
|
|
3
3
|
*
|
|
4
|
-
* Must be called inside a React Router tree (e.g., <BrowserRouter>).
|
|
4
|
+
* Must be called inside a React Router tree (e.g., <BrowserRouter>). With `routerEvents`, each navigation also
|
|
5
|
+
* publishes an MCP Event (`route.changed` by default) on the provider's server.
|
|
5
6
|
*/
|
|
6
|
-
export
|
|
7
|
+
export interface RouterBridgeOptions {
|
|
8
|
+
/**
|
|
9
|
+
* Publish an event on each navigation: `true` for `route.changed` on the provider's server, or the event name and
|
|
10
|
+
* the server. The server declares the event with `createRouteChangedEvent()`. Off by default.
|
|
11
|
+
*/
|
|
12
|
+
routerEvents?: boolean | {
|
|
13
|
+
eventName?: string;
|
|
14
|
+
server?: string;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/** Keep the bridge current, and publish navigations when `routerEvents` asks for it. */
|
|
18
|
+
export declare function useRouterBridge(options?: RouterBridgeOptions): void;
|
|
7
19
|
//# sourceMappingURL=useRouterBridge.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useRouterBridge.d.ts","sourceRoot":"","sources":["../../src/router/useRouterBridge.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"useRouterBridge.d.ts","sourceRoot":"","sources":["../../src/router/useRouterBridge.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAQH,MAAM,WAAW,mBAAmB;IAClC;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,GAAG;QAAE,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAClE;AAED,wFAAwF;AACxF,wBAAgB,eAAe,CAAC,OAAO,GAAE,mBAAwB,GAAG,IAAI,CA2BvE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStoreRegistration.d.ts","sourceRoot":"","sources":["../../src/state/useStoreRegistration.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;
|
|
1
|
+
{"version":3,"file":"useStoreRegistration.d.ts","sourceRoot":"","sources":["../../src/state/useStoreRegistration.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAMH,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAU7C,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,YAAY,EAAE,EAAE,eAAe,EAAE,eAAe,GAAG,IAAI,CAkHnG"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useValtioResource.d.ts","sourceRoot":"","sources":["../../src/state/useValtioResource.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"useValtioResource.d.ts","sourceRoot":"","sources":["../../src/state/useValtioResource.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AAa3D,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,qBAAqB,GAAG,IAAI,CAqCtE"}
|