@frontmcp/react 1.11.3 → 1.12.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/esm/index.mjs CHANGED
@@ -27,6 +27,7 @@ import {
27
27
  FrontMcpPlugin
28
28
  } from "@frontmcp/sdk";
29
29
  import { z as z2 } from "@frontmcp/sdk";
30
+ import { Event, FrontMcpEvent, event, frontMcpEvent } from "@frontmcp/sdk";
30
31
  import { ToolContext, ResourceContext, PromptContext, ExecutionContextBase } from "@frontmcp/sdk";
31
32
 
32
33
  // libs/react/src/registry/ServerRegistry.ts
@@ -545,6 +546,10 @@ function createWrappedServer(base, dynamicRegistry) {
545
546
  },
546
547
  async dispose() {
547
548
  return base.dispose();
549
+ },
550
+ /** The server's own event publisher. */
551
+ get events() {
552
+ return base.events;
548
553
  }
549
554
  };
550
555
  }
@@ -1350,6 +1355,123 @@ function useComponentTree(options) {
1350
1355
  });
1351
1356
  }
1352
1357
 
1358
+ // libs/react/src/hooks/useEventSubscription.ts
1359
+ import { useEffect as useEffect9, useRef as useRef5, useState as useState5 } from "react";
1360
+ function toError(error) {
1361
+ return error instanceof Error ? error : new Error(String(error));
1362
+ }
1363
+ function useEventSubscription(name, args, handlerOrOptions) {
1364
+ const options = typeof handlerOrOptions === "function" ? { onEvent: handlerOrOptions } : handlerOrOptions;
1365
+ const { entry } = useResolvedServer(options.server);
1366
+ const client = entry?.client;
1367
+ const argsKey = JSON.stringify(args ?? {});
1368
+ const enabled = options.enabled !== false;
1369
+ const { delivery } = options;
1370
+ const optionsRef = useRef5(options);
1371
+ const [state, setState] = useState5({ status: "idle" });
1372
+ useIsomorphicLayoutEffect(() => {
1373
+ optionsRef.current = options;
1374
+ });
1375
+ useEffect9(() => {
1376
+ if (!client || !enabled) {
1377
+ setState((current) => current.status === "idle" ? current : { status: "idle" });
1378
+ return;
1379
+ }
1380
+ let unmounted = false;
1381
+ let subscription;
1382
+ const run = async () => {
1383
+ setState({ status: "subscribing" });
1384
+ try {
1385
+ subscription = await client.events.subscribe(name, JSON.parse(argsKey), { delivery });
1386
+ if (unmounted) return void await subscription.cancel();
1387
+ const { mode } = subscription;
1388
+ setState({ status: "active", mode });
1389
+ for await (const occurrence of subscription) optionsRef.current.onEvent(occurrence);
1390
+ if (!unmounted) setState({ status: "ended", mode });
1391
+ } catch (caught) {
1392
+ if (unmounted) return;
1393
+ const error = toError(caught);
1394
+ setState({ status: "error", mode: subscription?.mode, error });
1395
+ optionsRef.current.onError?.(error);
1396
+ }
1397
+ };
1398
+ void run();
1399
+ return () => {
1400
+ unmounted = true;
1401
+ void subscription?.cancel();
1402
+ };
1403
+ }, [client, name, argsKey, enabled, delivery]);
1404
+ return state;
1405
+ }
1406
+
1407
+ // libs/react/src/hooks/usePublishEvent.ts
1408
+ import { useCallback as useCallback8 } from "react";
1409
+ function usePublishEvent(serverName) {
1410
+ const { entry, name } = useResolvedServer(serverName);
1411
+ const server = entry?.server;
1412
+ return useCallback8(
1413
+ ((event2, data, options) => {
1414
+ if (!server) return Promise.reject(new Error(`FrontMCP server "${name}" is not registered`));
1415
+ return server.events.publish(event2, data, options);
1416
+ }),
1417
+ [server, name]
1418
+ );
1419
+ }
1420
+
1421
+ // libs/react/src/hooks/useDomEventSource.ts
1422
+ import { useEffect as useEffect10, useRef as useRef6 } from "react";
1423
+ function useDomEventSource(target, type, toEvent, options = {}) {
1424
+ const publish = usePublishEvent(options.server);
1425
+ const callbacksRef = useRef6({ toEvent, onError: options.onError });
1426
+ useIsomorphicLayoutEffect(() => {
1427
+ callbacksRef.current = { toEvent, onError: options.onError };
1428
+ });
1429
+ useEffect10(() => {
1430
+ const element = target && "current" in target ? target.current : target;
1431
+ if (!element) return;
1432
+ const listener = (domEvent) => {
1433
+ const published = callbacksRef.current.toEvent(domEvent);
1434
+ if (!published) return;
1435
+ publish(published.name, published.data, published.options).catch((caught) => {
1436
+ const error = caught instanceof Error ? caught : new Error(String(caught));
1437
+ const report = callbacksRef.current.onError;
1438
+ if (report) report(error);
1439
+ else console.warn(`[frontmcp] event "${published.name}" was not published: ${error.message}`);
1440
+ });
1441
+ };
1442
+ element.addEventListener(type, listener);
1443
+ return () => element.removeEventListener(type, listener);
1444
+ }, [target, type, publish]);
1445
+ }
1446
+
1447
+ // libs/react/src/events/createRemoteEventForwarder.ts
1448
+ var RemoteEventForwardError = class extends Error {
1449
+ /** Carries the HTTP status and the body the endpoint answered with. */
1450
+ constructor(status, body) {
1451
+ super(`The remote publish endpoint answered ${status}: ${body.slice(0, 200)}`);
1452
+ this.status = status;
1453
+ this.body = body;
1454
+ this.name = "RemoteEventForwardError";
1455
+ }
1456
+ status;
1457
+ body;
1458
+ };
1459
+ function createRemoteEventForwarder(options) {
1460
+ return {
1461
+ /** POST the event with the current token; throws RemoteEventForwardError on a non-2xx answer. */
1462
+ async forward(name, data, publishOptions = {}) {
1463
+ const token = await options.getToken();
1464
+ const response = await (options.fetch ?? globalThis.fetch.bind(globalThis))(options.url, {
1465
+ method: "POST",
1466
+ headers: { "content-type": "application/json", ...token ? { authorization: `Bearer ${token}` } : {} },
1467
+ body: JSON.stringify({ name, data, ...publishOptions })
1468
+ });
1469
+ if (!response.ok) throw new RemoteEventForwardError(response.status, await response.text());
1470
+ return await response.json();
1471
+ }
1472
+ };
1473
+ }
1474
+
1353
1475
  // libs/react/src/components/DynamicRenderer.tsx
1354
1476
  import React2 from "react";
1355
1477
  function DynamicRenderer({ tree, registry, fallback }) {
@@ -1470,7 +1592,7 @@ function readDomBySelector(selector) {
1470
1592
  }
1471
1593
 
1472
1594
  // libs/react/src/components/ToolForm.tsx
1473
- import React3, { useCallback as useCallback8, useState as useState5 } from "react";
1595
+ import React3, { useCallback as useCallback9, useState as useState6 } from "react";
1474
1596
  function ToolForm({
1475
1597
  tool: tool2,
1476
1598
  onSubmit,
@@ -1480,7 +1602,7 @@ function ToolForm({
1480
1602
  const schema = tool2.inputSchema ?? {};
1481
1603
  const properties = schema["properties"] ?? {};
1482
1604
  const required = schema["required"] ?? [];
1483
- const [values, setValues] = useState5({});
1605
+ const [values, setValues] = useState6({});
1484
1606
  const initialValue = (key, prop) => {
1485
1607
  const options = prop["enum"];
1486
1608
  if (!options) return "";
@@ -1488,7 +1610,7 @@ function ToolForm({
1488
1610
  if (typeof fallback === "string" && options.includes(fallback)) return fallback;
1489
1611
  return required.includes(key) ? options[0] ?? "" : "";
1490
1612
  };
1491
- const handleSubmit = useCallback8(
1613
+ const handleSubmit = useCallback9(
1492
1614
  (e) => {
1493
1615
  e.preventDefault();
1494
1616
  const args = {};
@@ -1507,7 +1629,7 @@ function ToolForm({
1507
1629
  },
1508
1630
  [values, properties, required, onSubmit]
1509
1631
  );
1510
- const handleChange = useCallback8((key, value) => {
1632
+ const handleChange = useCallback9((key, value) => {
1511
1633
  setValues((prev) => ({ ...prev, [key]: value }));
1512
1634
  }, []);
1513
1635
  return React3.createElement(
@@ -1570,7 +1692,7 @@ function getFieldType(prop) {
1570
1692
  }
1571
1693
 
1572
1694
  // libs/react/src/components/PromptForm.tsx
1573
- import React4, { useCallback as useCallback9, useState as useState6 } from "react";
1695
+ import React4, { useCallback as useCallback10, useState as useState7 } from "react";
1574
1696
  function PromptForm({
1575
1697
  prompt: prompt2,
1576
1698
  onSubmit,
@@ -1578,8 +1700,8 @@ function PromptForm({
1578
1700
  submitLabel = "Get Prompt"
1579
1701
  }) {
1580
1702
  const args = prompt2.arguments ?? [];
1581
- const [values, setValues] = useState6({});
1582
- const handleSubmit = useCallback9(
1703
+ const [values, setValues] = useState7({});
1704
+ const handleSubmit = useCallback10(
1583
1705
  (e) => {
1584
1706
  e.preventDefault();
1585
1707
  const result = {};
@@ -1590,7 +1712,7 @@ function PromptForm({
1590
1712
  },
1591
1713
  [values, args, onSubmit]
1592
1714
  );
1593
- const handleChange = useCallback9((key, value) => {
1715
+ const handleChange = useCallback10((key, value) => {
1594
1716
  setValues((prev) => ({ ...prev, [key]: value }));
1595
1717
  }, []);
1596
1718
  return React4.createElement(
@@ -1707,7 +1829,7 @@ function OutputDisplay({ data, loading, error }) {
1707
1829
  }
1708
1830
 
1709
1831
  // libs/react/src/components/AgentContent.tsx
1710
- import React7, { useCallback as useCallback10, useState as useState7 } from "react";
1832
+ import React7, { useCallback as useCallback11, useState as useState8 } from "react";
1711
1833
  function AgentContent({
1712
1834
  name,
1713
1835
  description,
@@ -1716,8 +1838,8 @@ function AgentContent({
1716
1838
  fallback = null,
1717
1839
  server
1718
1840
  }) {
1719
- const [lastData, setLastData] = useState7(null);
1720
- const execute = useCallback10(
1841
+ const [lastData, setLastData] = useState8(null);
1842
+ const execute = useCallback11(
1721
1843
  async (args) => {
1722
1844
  setLastData(args);
1723
1845
  return {
@@ -1731,7 +1853,7 @@ function AgentContent({
1731
1853
  }
1732
1854
 
1733
1855
  // libs/react/src/components/AgentSearch.tsx
1734
- import React8, { useCallback as useCallback11, useState as useState8 } from "react";
1856
+ import React8, { useCallback as useCallback12, useState as useState9 } from "react";
1735
1857
  function AgentSearch({
1736
1858
  toolName,
1737
1859
  description,
@@ -1740,8 +1862,8 @@ function AgentSearch({
1740
1862
  renderInput,
1741
1863
  server
1742
1864
  }) {
1743
- const [query, setQuery] = useState8("");
1744
- const execute = useCallback11(
1865
+ const [query, setQuery] = useState9("");
1866
+ const execute = useCallback12(
1745
1867
  async (args) => {
1746
1868
  const results = args["results"] ?? args;
1747
1869
  onResults(results);
@@ -1764,7 +1886,7 @@ function AgentSearch({
1764
1886
  execute,
1765
1887
  server
1766
1888
  });
1767
- const readQuery = useCallback11(
1889
+ const readQuery = useCallback12(
1768
1890
  async () => ({
1769
1891
  contents: [{ uri: `search://${toolName}/query`, mimeType: "text/plain", text: query }]
1770
1892
  }),
@@ -1795,7 +1917,7 @@ function AgentSearch({
1795
1917
  }
1796
1918
 
1797
1919
  // libs/react/src/components/mcpComponent.tsx
1798
- import React9, { Suspense, useCallback as useCallback12, useState as useState9 } from "react";
1920
+ import React9, { Suspense, useCallback as useCallback13, useState as useState10 } from "react";
1799
1921
  import { z } from "@frontmcp/lazy-zod";
1800
1922
  var MCP_LAZY_MARKER = /* @__PURE__ */ Symbol.for("frontmcp:lazy");
1801
1923
  function mcpLazy(factory) {
@@ -1855,9 +1977,9 @@ function mcpComponent(component, options) {
1855
1977
  const isLazy = component !== null && !isTableMode && isLazyImport(component);
1856
1978
  const McpWrappedComponent = Object.assign(
1857
1979
  function McpWrappedComponentInner(directProps) {
1858
- const [lastData, setLastData] = useState9(null);
1859
- const [tableRows, setTableRows] = useState9(null);
1860
- const execute = useCallback12(
1980
+ const [lastData, setLastData] = useState10(null);
1981
+ const [tableRows, setTableRows] = useState10(null);
1982
+ const execute = useCallback13(
1861
1983
  async (args) => {
1862
1984
  if (isTableMode) {
1863
1985
  const data2 = args;
@@ -1989,11 +2111,13 @@ export {
1989
2111
  ComponentRegistry,
1990
2112
  DynamicRegistry,
1991
2113
  DynamicRenderer,
2114
+ Event,
1992
2115
  ExecutionContextBase,
1993
2116
  FrontMcp,
1994
2117
  FrontMcpAdapter,
1995
2118
  FrontMcpApp,
1996
2119
  FrontMcpContext,
2120
+ FrontMcpEvent,
1997
2121
  FrontMcpPlugin,
1998
2122
  FrontMcpPrompt,
1999
2123
  FrontMcpProvider,
@@ -2005,6 +2129,7 @@ export {
2005
2129
  Prompt,
2006
2130
  PromptContext,
2007
2131
  PromptForm,
2132
+ RemoteEventForwardError,
2008
2133
  Resource,
2009
2134
  ResourceContext,
2010
2135
  ResourceTemplate,
@@ -2021,7 +2146,10 @@ export {
2021
2146
  connectOpenAI,
2022
2147
  connectVercelAI,
2023
2148
  create,
2149
+ createRemoteEventForwarder,
2024
2150
  createStore,
2151
+ event,
2152
+ frontMcpEvent,
2025
2153
  frontMcpPrompt,
2026
2154
  frontMcpResource,
2027
2155
  frontMcpResourceTemplate,
@@ -2038,13 +2166,16 @@ export {
2038
2166
  tool,
2039
2167
  useCallTool,
2040
2168
  useComponentTree,
2169
+ useDomEventSource,
2041
2170
  useDynamicResource,
2042
2171
  useDynamicTool,
2172
+ useEventSubscription,
2043
2173
  useFrontMcp,
2044
2174
  useGetPrompt,
2045
2175
  useListPrompts,
2046
2176
  useListResources,
2047
2177
  useListTools,
2178
+ usePublishEvent,
2048
2179
  useReadResource,
2049
2180
  useResolvedServer,
2050
2181
  useServer,
package/esm/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frontmcp/react",
3
- "version": "1.11.3",
3
+ "version": "1.12.0",
4
4
  "description": "Run a FrontMCP server inside a React app: provider, hooks, components and AI SDK integration",
5
5
  "author": "AgentFront <info@agentfront.dev>",
6
6
  "homepage": "https://frontmcp.dev/reference/react",
@@ -89,9 +89,9 @@
89
89
  "node": ">=24.0.0"
90
90
  },
91
91
  "peerDependencies": {
92
- "@frontmcp/lazy-zod": "1.11.3",
93
- "@frontmcp/sdk": "1.11.3",
94
- "@frontmcp/utils": "1.11.3",
92
+ "@frontmcp/lazy-zod": "1.12.0",
93
+ "@frontmcp/sdk": "1.12.0",
94
+ "@frontmcp/utils": "1.12.0",
95
95
  "react": "^18.0.0 || ^19.0.0",
96
96
  "react-dom": "^18.0.0 || ^19.0.0",
97
97
  "react-router-dom": "^7.0.0"
@@ -105,8 +105,8 @@
105
105
  }
106
106
  },
107
107
  "dependencies": {
108
- "@frontmcp/sdk": "1.11.3",
109
- "@frontmcp/utils": "1.11.3"
108
+ "@frontmcp/sdk": "1.12.0",
109
+ "@frontmcp/utils": "1.12.0"
110
110
  },
111
111
  "devDependencies": {
112
112
  "typescript": "^5.9.3"