@frontmcp/react 1.9.6 → 1.9.7
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 +195 -172
- package/esm/package.json +14 -11
- package/package.json +14 -11
package/README.md
CHANGED
|
@@ -1,93 +1,135 @@
|
|
|
1
1
|
# @frontmcp/react
|
|
2
2
|
|
|
3
|
-
React
|
|
3
|
+
`@frontmcp/react` runs a FrontMCP server inside a React app. You build the server with `create()`, in the same process, and `FrontMcpProvider` connects a client to it. Components call its tools, read its resources and get its prompts with hooks, and can add tools of their own while they're mounted, so a model can fill in a card, move around the app or read its state. Use it to make a React app something an agent can drive. To build the UI a tool shows inside an AI host such as ChatGPT or Claude, use [`@frontmcp/ui`](https://www.npmjs.com/package/@frontmcp/ui) instead.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/@frontmcp/react) [](https://github.com/agentfront/frontmcp/blob/main/LICENSE)
|
|
6
|
+
|
|
7
|
+
## Install
|
|
6
8
|
|
|
7
9
|
```bash
|
|
8
10
|
npm install @frontmcp/react react react-dom
|
|
9
11
|
```
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
- `react` and `react-dom` 18 or 19 are peer dependencies.
|
|
14
|
+
- `@frontmcp/sdk` and `@frontmcp/utils` come with it, at the same version. `@frontmcp/react` re-exports what an app needs from the SDK (`create`, `connect` and its variants, the decorators and their function forms, the context classes, and `z`), so you don't import `@frontmcp/sdk` yourself. npm 7 and later also install its peer `@frontmcp/lazy-zod`, and `zod` with it.
|
|
15
|
+
- `react-router-dom` 7 is an optional peer, needed only for `@frontmcp/react/router`.
|
|
16
|
+
- In the browser, a bundler that follows the `browser` export condition, such as Vite, picks the SDK's browser build ([Running in a browser](https://frontmcp.dev/reference/react#running-in-a-browser)). In Node, for tests or server rendering, use Node.js 24 or later.
|
|
14
17
|
|
|
15
|
-
|
|
16
|
-
- `react-router-dom` (^7.0.0) — for `/router` entry point
|
|
18
|
+
## Usage
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
A tool, the server, and a component that calls it:
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
| `@frontmcp/react` | Provider, hooks, components, ServerRegistry, and SDK re-exports |
|
|
23
|
-
| `@frontmcp/react/ai` | AI SDK integration (`useAITools`, `useTools`, `createToolCallHandler`) |
|
|
24
|
-
| `@frontmcp/react/router` | React Router bridge (`useRouterBridge`, navigation tools, route resource) |
|
|
25
|
-
| `@frontmcp/react/state` | State management integration (Redux, Valtio, generic, store adapters) |
|
|
26
|
-
| `@frontmcp/react/api` | API client integration (OpenAPI, custom HTTP clients) |
|
|
22
|
+
```tsx
|
|
23
|
+
import { create, FrontMcpProvider, Tool, ToolContext, useCallTool, z } from '@frontmcp/react';
|
|
27
24
|
|
|
28
|
-
|
|
25
|
+
@Tool({ name: 'greet', description: 'Greet someone by name', inputSchema: { name: z.string() } })
|
|
26
|
+
class GreetTool extends ToolContext {
|
|
27
|
+
async execute({ name }: { name: string }) {
|
|
28
|
+
return { message: `Hello, ${name}!` };
|
|
29
|
+
}
|
|
30
|
+
}
|
|
29
31
|
|
|
30
|
-
|
|
31
|
-
import { create, FrontMcpProvider, useCallTool } from '@frontmcp/react';
|
|
32
|
+
const server = await create({ info: { name: 'my-app', version: '1.0.0' }, tools: [GreetTool] });
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
function Greeter() {
|
|
35
|
+
const [greet, { data, loading, error }] = useCallTool<{ name: string }, { structuredContent?: { message: string } }>(
|
|
36
|
+
'greet',
|
|
37
|
+
);
|
|
37
38
|
|
|
38
|
-
function App() {
|
|
39
39
|
return (
|
|
40
|
-
<
|
|
41
|
-
<
|
|
42
|
-
|
|
40
|
+
<div>
|
|
41
|
+
<button disabled={loading} onClick={() => greet({ name: 'Ada' })}>
|
|
42
|
+
Greet
|
|
43
|
+
</button>
|
|
44
|
+
{data?.structuredContent && <p>{data.structuredContent.message}</p>}
|
|
45
|
+
{error && <p role="alert">{error.message}</p>}
|
|
46
|
+
</div>
|
|
43
47
|
);
|
|
44
48
|
}
|
|
45
49
|
|
|
46
|
-
function
|
|
47
|
-
const [callTool, { data, loading }] = useCallTool('greet');
|
|
50
|
+
export function App() {
|
|
48
51
|
return (
|
|
49
|
-
<
|
|
50
|
-
|
|
51
|
-
</
|
|
52
|
+
<FrontMcpProvider server={server}>
|
|
53
|
+
<Greeter />
|
|
54
|
+
</FrontMcpProvider>
|
|
52
55
|
);
|
|
53
56
|
}
|
|
54
57
|
```
|
|
55
58
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
59
|
+
Clicking the button shows `Hello, Ada!`. `useCallTool()` returns `[call, state, reset]`, and `data` is the MCP result the client got, so the tool's return value is in `data.structuredContent`. A tool that fails comes back as a result with `isError: true` in `data`; `error` is set only when the call couldn't be made, such as `FrontMCP not connected`. Decorators need `"experimentalDecorators": true` in `tsconfig.json`.
|
|
60
|
+
|
|
61
|
+
### Entry points
|
|
62
|
+
|
|
63
|
+
| Import | What's in it |
|
|
64
|
+
| ------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
65
|
+
| `@frontmcp/react` | `FrontMcpProvider`, the hooks, the components, `mcpComponent`, `serverRegistry`, `DynamicRegistry` and `bindDynamicTools`, the store adapters, and the SDK re-exports listed above. |
|
|
66
|
+
| `@frontmcp/react/state` | `useStoreResource`, `useReduxResource`, `useValtioResource`: expose a store from inside a component. |
|
|
67
|
+
| `@frontmcp/react/api` | `useApiClient`, `parseOpenApiSpec`, `createFetchClient`: register OpenAPI operations as tools. |
|
|
68
|
+
| `@frontmcp/react/ai` | `useAITools`, `useTools`, `createToolCallHandler`: hand the server's tools to an AI SDK. |
|
|
69
|
+
| `@frontmcp/react/router` | `useRouterBridge`, `createRouterEntries`, and the `navigate`, `go_back` and `route://current` entries. Needs `react-router-dom` 7. |
|
|
70
|
+
|
|
71
|
+
### `FrontMcpProvider`
|
|
72
|
+
|
|
73
|
+
| Prop | Default | Description |
|
|
74
|
+
| ----------------- | ----------- | --------------------------------------------------------------------------------------------------------------------------- |
|
|
75
|
+
| `server` | | **Required.** The server from `create()`. |
|
|
76
|
+
| `name` | `"default"` | The server's name in `serverRegistry`, which hooks without a `server` option use. |
|
|
77
|
+
| `servers` | | More servers, by name. Hooks reach them with `{ server: 'name' }`. |
|
|
78
|
+
| `components` | | Components for `DynamicRenderer`, by URI, like `{ 'component://Card': Card }`. |
|
|
79
|
+
| `stores` | | Store adapters to expose as resources and tools ([Exposing app state](#exposing-app-state)). |
|
|
80
|
+
| `dynamicToolApps` | | The app that components' tools join, by server name. Only for a server with several apps. |
|
|
81
|
+
| `autoConnect` | `true` | Connect on mount. With `false`, call `connect()` from `useFrontMcp()`. |
|
|
82
|
+
| `onConnected` | | Called once the client is connected and the lists are loaded. |
|
|
83
|
+
| `onError` | | Called when connecting fails, and when the server refuses a tool a component registers. Without it, a refusal is a warning. |
|
|
84
|
+
|
|
85
|
+
### Hooks
|
|
86
|
+
|
|
87
|
+
Every hook takes `{ server: 'name' }` to use one of the provider's other `servers`.
|
|
88
|
+
|
|
89
|
+
| Hook | Returns |
|
|
90
|
+
| ------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
91
|
+
| `useFrontMcp(name?)` | `{ name, server, client, status, error, tools, resources, resourceTemplates, prompts, registry, connect }` |
|
|
92
|
+
| `useCallTool(name, options?)` | `[call, { data, loading, error, called }, reset]`. Options: `onSuccess`, `onError`, `resetOnToolChange`. |
|
|
93
|
+
| `useReadResource(uri, options?)` | Reads on mount: `{ data, loading, error, refetch }`. Without a URI, `[read, state]` to read on demand. |
|
|
94
|
+
| `useGetPrompt(name, options?)` | `[getPrompt, { data, loading, error }]` |
|
|
95
|
+
| `useListTools()`, `useListPrompts()` | The server's tools or prompts, kept current. |
|
|
96
|
+
| `useListResources()` | `{ resources, resourceTemplates }` |
|
|
97
|
+
| `useStoreResource(uri)` | `{ data, loading, error, refetch }` for a `state://` resource, read again whenever the server says it changed. |
|
|
98
|
+
| `useDynamicTool(options)` | Registers a tool while the component is mounted ([below](#giving-the-model-a-tool-from-a-component)). |
|
|
99
|
+
| `useDynamicResource(options)` | Registers a resource, `{ uri, name, description?, mimeType?, read }`, while the component is mounted. |
|
|
100
|
+
| `useComponentTree({ rootRef, uri?, ... })` | Registers a resource (`react://component-tree` by default) that describes the DOM under `rootRef`, named by each element's `data-component`. |
|
|
101
|
+
|
|
102
|
+
### Giving the model a tool from a component
|
|
68
103
|
|
|
69
104
|
```tsx
|
|
70
|
-
import {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
105
|
+
import { useDynamicTool, z } from '@frontmcp/react';
|
|
106
|
+
|
|
107
|
+
export function Cart({ isLoggedIn }: { isLoggedIn: boolean }) {
|
|
108
|
+
useDynamicTool({
|
|
109
|
+
name: 'add_to_cart',
|
|
110
|
+
description: 'Add an item to the shopping cart',
|
|
111
|
+
schema: z.object({ itemId: z.string(), quantity: z.number().optional() }),
|
|
112
|
+
execute: async ({ itemId, quantity }) => {
|
|
113
|
+
addToCart(itemId, quantity ?? 1);
|
|
114
|
+
return { content: [{ type: 'text', text: 'Added to cart' }] };
|
|
115
|
+
},
|
|
116
|
+
enabled: isLoggedIn,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
return <div>Cart</div>;
|
|
120
|
+
}
|
|
75
121
|
```
|
|
76
122
|
|
|
77
|
-
The provider
|
|
123
|
+
The input is checked against `schema` before `execute` runs, and `execute` gets it typed. A raw JSON Schema in `inputSchema` works too. The provider registers the tool with the server as a real tool, through `server.registerTool()`, so its calls run through the server's flow (plugin hooks, authorities, `availableWhen`) and every client sees it, an in-page agent through [`@frontmcp/plugin-webmcp`](https://www.npmjs.com/package/@frontmcp/plugin-webmcp) included. A name the server already has is refused and reported through the provider's `onError`.
|
|
78
124
|
|
|
79
|
-
|
|
125
|
+
### Letting a model show a component
|
|
80
126
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
Type-safe factory that wraps a React component + zod schema into an MCP-registered component:
|
|
127
|
+
`mcpComponent()` wraps a component so that a model can show it: while it's mounted it registers a tool, and the tool's input, checked against `schema`, becomes the component's props.
|
|
84
128
|
|
|
85
129
|
```tsx
|
|
86
|
-
import { z } from '
|
|
87
|
-
|
|
88
|
-
import { mcpComponent } from '@frontmcp/react';
|
|
130
|
+
import { mcpComponent, z } from '@frontmcp/react';
|
|
89
131
|
|
|
90
|
-
const WeatherCard = mcpComponent(
|
|
132
|
+
export const WeatherCard = mcpComponent(
|
|
91
133
|
({ city, temp }) => (
|
|
92
134
|
<div>
|
|
93
135
|
<h2>{city}</h2>
|
|
@@ -96,170 +138,151 @@ const WeatherCard = mcpComponent(
|
|
|
96
138
|
),
|
|
97
139
|
{
|
|
98
140
|
name: 'show_weather',
|
|
99
|
-
description: '
|
|
141
|
+
description: 'Show the weather for a city',
|
|
100
142
|
schema: z.object({ city: z.string(), temp: z.number() }),
|
|
101
|
-
fallback: <p>
|
|
143
|
+
fallback: <p>Waiting for weather data…</p>,
|
|
102
144
|
},
|
|
103
145
|
);
|
|
104
146
|
|
|
105
|
-
//
|
|
106
|
-
<WeatherCard />;
|
|
147
|
+
// Render it like any component: <WeatherCard />
|
|
107
148
|
```
|
|
108
149
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
When `component` is `null` and `columns` is provided, renders a `<table>`:
|
|
150
|
+
Props you pass yourself, like `<WeatherCard city="Oslo" />`, render right away and are merged over the model's. Call `mcpComponent()` at module level, not inside a component. The first argument can also be a lazy import wrapped in `mcpLazy(() => import('./HeavyChart'))`, or `null` with a `columns` option for a plain table, where the tool takes `{ rows: [...] }`:
|
|
112
151
|
|
|
113
152
|
```tsx
|
|
114
|
-
const OrderTable = mcpComponent(null, {
|
|
153
|
+
export const OrderTable = mcpComponent(null, {
|
|
115
154
|
name: 'show_orders',
|
|
155
|
+
description: 'Show a list of orders',
|
|
116
156
|
schema: z.object({ id: z.string(), product: z.string(), price: z.number() }),
|
|
117
157
|
columns: [
|
|
118
158
|
{ key: 'id', header: 'Order ID' },
|
|
119
159
|
{ key: 'product', header: 'Product' },
|
|
120
|
-
{ key: 'price', header: 'Price', render: (
|
|
160
|
+
{ key: 'price', header: 'Price', render: (value) => `$${value}` },
|
|
121
161
|
],
|
|
122
162
|
});
|
|
123
163
|
```
|
|
124
164
|
|
|
125
|
-
|
|
165
|
+
### Components
|
|
126
166
|
|
|
127
|
-
|
|
167
|
+
| Component | Description |
|
|
168
|
+
| -------------------------------------- | ---------------------------------------------------------------------------------------------- |
|
|
169
|
+
| `ToolForm` | A form generated from a tool's `inputSchema`. Unstyled, or your own fields with `renderField`. |
|
|
170
|
+
| `PromptForm` | A form generated from a prompt's arguments. |
|
|
171
|
+
| `ResourceViewer` | Shows a read result, `{ contents }`. |
|
|
172
|
+
| `OutputDisplay` | Shows a tool or prompt result as text or indented JSON. |
|
|
173
|
+
| `DynamicRenderer`, `ComponentRegistry` | Render a tree of `{ type, props?, children? }` nodes with components registered by URI. |
|
|
174
|
+
| `readDomById`, `readDomBySelector` | Read the page as a resource result, for a resource template. |
|
|
175
|
+
| `AgentContent`, `AgentSearch` | Deprecated: use `mcpComponent()`. |
|
|
128
176
|
|
|
129
|
-
|
|
130
|
-
| --------------------------------- | ---------------------------------------------- |
|
|
131
|
-
| `useCallTool(name, options?)` | Call an MCP tool with typed input/output |
|
|
132
|
-
| `useReadResource(uri?, options?)` | Lazy or auto-fetch resource reading |
|
|
133
|
-
| `useGetPrompt(name, options?)` | Fetch an MCP prompt by name |
|
|
134
|
-
| `useListTools(options?)` | Reactive tool list from the registry |
|
|
135
|
-
| `useListResources(options?)` | Reactive resource and template lists |
|
|
136
|
-
| `useListPrompts(options?)` | Reactive prompt list |
|
|
137
|
-
| `useStoreResource(uri, options?)` | Subscribe to `state://` URIs with live updates |
|
|
177
|
+
### Exposing app state
|
|
138
178
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
| Hook | Description |
|
|
142
|
-
| ----------------------------- | -------------------------------------------------------- |
|
|
143
|
-
| `useDynamicTool(options)` | Register an MCP tool on mount, unregister on unmount |
|
|
144
|
-
| `useDynamicResource(options)` | Register an MCP resource on mount, unregister on unmount |
|
|
145
|
-
| `useComponentTree(options)` | Expose the DOM subtree under a ref as an MCP resource |
|
|
146
|
-
|
|
147
|
-
All hooks accept `{ server: 'name' }` to target a specific server in multi-server setups.
|
|
148
|
-
|
|
149
|
-
### useDynamicTool with Zod
|
|
179
|
+
Each adapter in the provider's `stores` becomes a resource with the whole state (`state://<name>`), a resource per selector (`state://<name>/<selector>`) and a tool per action (`<name>_<action>`):
|
|
150
180
|
|
|
151
181
|
```tsx
|
|
152
|
-
import {
|
|
153
|
-
|
|
154
|
-
useDynamicTool({
|
|
155
|
-
name: 'add_to_cart',
|
|
156
|
-
description: 'Add item to shopping cart',
|
|
157
|
-
schema: z.object({
|
|
158
|
-
itemId: z.string(),
|
|
159
|
-
quantity: z.number().optional(),
|
|
160
|
-
}),
|
|
161
|
-
execute: async (args) => {
|
|
162
|
-
// args is typed as { itemId: string; quantity?: number }
|
|
163
|
-
addToCart(args.itemId, args.quantity ?? 1);
|
|
164
|
-
return { content: [{ type: 'text', text: 'Added to cart' }] };
|
|
165
|
-
},
|
|
166
|
-
enabled: isLoggedIn,
|
|
167
|
-
});
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
Also supports raw JSON Schema via `inputSchema` for backward compatibility.
|
|
182
|
+
import { createStore, FrontMcpProvider, reduxStore, valtioStore } from '@frontmcp/react';
|
|
171
183
|
|
|
172
|
-
|
|
184
|
+
const stores = [
|
|
185
|
+
reduxStore({
|
|
186
|
+
store: todoStore,
|
|
187
|
+
selectors: { todos: (state) => (state as { todos: unknown[] }).todos },
|
|
188
|
+
actions: { addTodo: (text) => ({ type: 'todos/add', text }) },
|
|
189
|
+
}),
|
|
190
|
+
valtioStore({ proxy: settings, subscribe, paths: { userName: 'user.name' } }),
|
|
191
|
+
createStore({ name: 'counter', getState: getCounter, subscribe: onCounterChange }),
|
|
192
|
+
];
|
|
173
193
|
|
|
174
|
-
|
|
194
|
+
export function Root() {
|
|
195
|
+
return (
|
|
196
|
+
<FrontMcpProvider server={server} stores={stores}>
|
|
197
|
+
<App />
|
|
198
|
+
</FrontMcpProvider>
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
```
|
|
175
202
|
|
|
176
|
-
`@frontmcp/react/state`
|
|
203
|
+
`reduxStore` names its store `redux` unless you pass `name`, and dispatches what each action creator returns. `valtioStore` defaults to `valtio`; pass it `subscribe` from `valtio/utils`. Keep the `stores` array outside the component. To expose a store only while a component is mounted, use the hooks from `@frontmcp/react/state` with the same options: `useReduxResource()`, `useValtioResource()`, and `useStoreResource()`, which takes `createStore()`'s options. That `useStoreResource()` registers a store; the one from `@frontmcp/react` reads a `state://` resource.
|
|
177
204
|
|
|
178
|
-
###
|
|
205
|
+
### Registering API operations as tools
|
|
179
206
|
|
|
180
207
|
```tsx
|
|
181
|
-
import {
|
|
208
|
+
import { createFetchClient, parseOpenApiSpec, useApiClient } from '@frontmcp/react/api';
|
|
182
209
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
});
|
|
210
|
+
const operations = parseOpenApiSpec(openApiDocument);
|
|
211
|
+
const client = createFetchClient();
|
|
212
|
+
|
|
213
|
+
export function ApiTools() {
|
|
214
|
+
useApiClient({ baseUrl: 'https://api.example.com', operations, client });
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
188
217
|
```
|
|
189
218
|
|
|
190
|
-
|
|
219
|
+
Each operation becomes a tool named `api_<operationId>` (set `prefix` to change `api`). `client` takes any `HttpClient`, an object with `request({ method, url, headers, body? })`, so you can use axios or a client that adds auth headers.
|
|
191
220
|
|
|
192
|
-
|
|
221
|
+
### Handing the tools to an AI SDK
|
|
193
222
|
|
|
194
223
|
```tsx
|
|
195
|
-
import {
|
|
224
|
+
import { useAITools } from '@frontmcp/react/ai';
|
|
196
225
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
store,
|
|
202
|
-
selectors: { count: (s) => s.count },
|
|
203
|
-
actions: { inc: () => increment() },
|
|
204
|
-
}),
|
|
205
|
-
valtioStore({
|
|
206
|
-
proxy,
|
|
207
|
-
subscribe,
|
|
208
|
-
paths: { userName: 'user.name' },
|
|
209
|
-
}),
|
|
210
|
-
createStore({
|
|
211
|
-
name: 'custom',
|
|
212
|
-
getState,
|
|
213
|
-
subscribe: customSubscribe,
|
|
214
|
-
}),
|
|
215
|
-
]}
|
|
216
|
-
>
|
|
217
|
-
<App />
|
|
218
|
-
</FrontMcpProvider>;
|
|
226
|
+
export function useOpenAITools() {
|
|
227
|
+
const { tools, callTool, loading } = useAITools('openai');
|
|
228
|
+
return { tools, callTool, ready: !loading && tools !== null };
|
|
229
|
+
}
|
|
219
230
|
```
|
|
220
231
|
|
|
221
|
-
|
|
232
|
+
`tools` is the server's tool list, components' tools included, in the platform's format; `callTool(name, args)` runs a tool and converts its result. `useAITools()` takes `"openai"`, `"claude"`, `"langchain"`, `"vercel-ai"` or `"raw"`. `useTools(platform)`, for `"openai"`, `"claude"` or `"vercel-ai"`, returns `processToolCalls(calls)` instead, which runs a batch of the model's tool calls and returns the messages to send back. Outside React, `createToolCallHandler(server, platform)` gives you the same `callTool`. Keep API keys on a server of your own.
|
|
233
|
+
|
|
234
|
+
### Letting a model move around the app
|
|
222
235
|
|
|
223
|
-
|
|
236
|
+
Put the router entries into the server, and call `useRouterBridge()` in a component inside both the router and the provider:
|
|
224
237
|
|
|
225
238
|
```tsx
|
|
226
|
-
import {
|
|
239
|
+
import { BrowserRouter, Route, Routes } from 'react-router-dom';
|
|
227
240
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
});
|
|
241
|
+
import { create, FrontMcpProvider } from '@frontmcp/react';
|
|
242
|
+
import { createRouterEntries, useRouterBridge } from '@frontmcp/react/router';
|
|
243
|
+
|
|
244
|
+
const { tools, resources } = createRouterEntries();
|
|
245
|
+
const server = await create({ info: { name: 'my-app', version: '1.0.0' }, tools, resources });
|
|
246
|
+
|
|
247
|
+
function RouterBridge() {
|
|
248
|
+
useRouterBridge();
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
export function App() {
|
|
253
|
+
return (
|
|
254
|
+
<BrowserRouter>
|
|
255
|
+
<FrontMcpProvider server={server}>
|
|
256
|
+
<RouterBridge />
|
|
257
|
+
<Routes>
|
|
258
|
+
<Route path="/home" element={<h1>Home</h1>} />
|
|
259
|
+
<Route path="/settings" element={<h1>Settings</h1>} />
|
|
260
|
+
</Routes>
|
|
261
|
+
</FrontMcpProvider>
|
|
262
|
+
</BrowserRouter>
|
|
263
|
+
);
|
|
264
|
+
}
|
|
233
265
|
```
|
|
234
266
|
|
|
235
|
-
|
|
267
|
+
The server then has the `navigate` tool (`{ path, replace? }`), the `go_back` tool and the `route://current` resource. `useRouterBridge()` only connects React Router to them; without it mounted, they answer `Router bridge not connected`.
|
|
236
268
|
|
|
237
|
-
|
|
238
|
-
| ------------------- | ---------------------------------------------------- |
|
|
239
|
-
| `ToolForm` | Auto-generates forms from tool `inputSchema` |
|
|
240
|
-
| `PromptForm` | Generates forms from prompt arguments |
|
|
241
|
-
| `ResourceViewer` | Displays `ReadResourceResult` contents |
|
|
242
|
-
| `OutputDisplay` | Renders tool/prompt output as formatted JSON or text |
|
|
243
|
-
| `DynamicRenderer` | Recursively renders `ComponentNode` trees |
|
|
244
|
-
| `ComponentRegistry` | Maps URI protocols to React components |
|
|
269
|
+
## Docs
|
|
245
270
|
|
|
246
|
-
|
|
271
|
+
- [`@frontmcp/react` reference](https://frontmcp.dev/reference/react): the provider, `serverRegistry`, stores, running in a browser, testing in Node
|
|
272
|
+
- [React hooks](https://frontmcp.dev/reference/react/hooks): every hook, including `/state`, `/api` and `/ai`
|
|
273
|
+
- [React components](https://frontmcp.dev/reference/react/components): `mcpComponent()`, forms, `DynamicRenderer`, DOM resources, router tools
|
|
274
|
+
- [`create()`](https://frontmcp.dev/reference/sdk/create): building the server the provider connects to
|
|
247
275
|
|
|
248
|
-
|
|
249
|
-
import { useRouterBridge } from '@frontmcp/react/router';
|
|
276
|
+
## Related packages
|
|
250
277
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
}
|
|
255
|
-
```
|
|
278
|
+
- [`@frontmcp/sdk`](https://www.npmjs.com/package/@frontmcp/sdk): the server framework; `create()`, tools, resources and prompts come from it.
|
|
279
|
+
- [`@frontmcp/plugin-webmcp`](https://www.npmjs.com/package/@frontmcp/plugin-webmcp): offers the server's tools, components' tools included, to in-browser agents.
|
|
280
|
+
- [`@frontmcp/ui`](https://www.npmjs.com/package/@frontmcp/ui): React components and hooks for widgets that render inside an AI host.
|
|
256
281
|
|
|
257
|
-
##
|
|
282
|
+
## Contributing
|
|
258
283
|
|
|
259
|
-
|
|
260
|
-
import { createToolCallHandler, useAITools, useTools } from '@frontmcp/react/ai';
|
|
261
|
-
```
|
|
284
|
+
Source: [`libs/react`](https://github.com/agentfront/frontmcp/tree/main/libs/react). Issues and pull requests are welcome; see [CONTRIBUTING.md](https://github.com/agentfront/frontmcp/blob/main/CONTRIBUTING.md).
|
|
262
285
|
|
|
263
286
|
## License
|
|
264
287
|
|
|
265
|
-
Apache-2.0
|
|
288
|
+
Apache-2.0. See [LICENSE](https://github.com/agentfront/frontmcp/blob/main/LICENSE).
|
package/esm/package.json
CHANGED
|
@@ -1,25 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontmcp/react",
|
|
3
|
-
"version": "1.9.
|
|
4
|
-
"description": "React hooks, components
|
|
3
|
+
"version": "1.9.7",
|
|
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",
|
|
7
7
|
"license": "Apache-2.0",
|
|
8
8
|
"keywords": [
|
|
9
|
+
"frontmcp",
|
|
9
10
|
"mcp",
|
|
11
|
+
"model-context-protocol",
|
|
10
12
|
"react",
|
|
11
13
|
"hooks",
|
|
14
|
+
"agent",
|
|
15
|
+
"webmcp",
|
|
12
16
|
"ai",
|
|
13
|
-
"tools",
|
|
14
17
|
"openai",
|
|
15
18
|
"claude",
|
|
16
19
|
"vercel-ai",
|
|
17
|
-
"agentfront"
|
|
18
|
-
"frontmcp"
|
|
20
|
+
"agentfront"
|
|
19
21
|
],
|
|
20
22
|
"repository": {
|
|
21
23
|
"type": "git",
|
|
22
|
-
"url": "git+https://github.com/agentfront/frontmcp.git"
|
|
24
|
+
"url": "git+https://github.com/agentfront/frontmcp.git",
|
|
25
|
+
"directory": "libs/react"
|
|
23
26
|
},
|
|
24
27
|
"bugs": {
|
|
25
28
|
"url": "https://github.com/agentfront/frontmcp/issues"
|
|
@@ -86,9 +89,9 @@
|
|
|
86
89
|
"node": ">=24.0.0"
|
|
87
90
|
},
|
|
88
91
|
"peerDependencies": {
|
|
89
|
-
"@frontmcp/lazy-zod": "1.9.
|
|
90
|
-
"@frontmcp/sdk": "1.9.
|
|
91
|
-
"@frontmcp/utils": "1.9.
|
|
92
|
+
"@frontmcp/lazy-zod": "1.9.7",
|
|
93
|
+
"@frontmcp/sdk": "1.9.7",
|
|
94
|
+
"@frontmcp/utils": "1.9.7",
|
|
92
95
|
"react": "^18.0.0 || ^19.0.0",
|
|
93
96
|
"react-dom": "^18.0.0 || ^19.0.0",
|
|
94
97
|
"react-router-dom": "^7.0.0"
|
|
@@ -102,8 +105,8 @@
|
|
|
102
105
|
}
|
|
103
106
|
},
|
|
104
107
|
"dependencies": {
|
|
105
|
-
"@frontmcp/sdk": "1.9.
|
|
106
|
-
"@frontmcp/utils": "1.9.
|
|
108
|
+
"@frontmcp/sdk": "1.9.7",
|
|
109
|
+
"@frontmcp/utils": "1.9.7"
|
|
107
110
|
},
|
|
108
111
|
"devDependencies": {
|
|
109
112
|
"typescript": "^5.9.3"
|
package/package.json
CHANGED
|
@@ -1,25 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontmcp/react",
|
|
3
|
-
"version": "1.9.
|
|
4
|
-
"description": "React hooks, components
|
|
3
|
+
"version": "1.9.7",
|
|
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",
|
|
7
7
|
"license": "Apache-2.0",
|
|
8
8
|
"keywords": [
|
|
9
|
+
"frontmcp",
|
|
9
10
|
"mcp",
|
|
11
|
+
"model-context-protocol",
|
|
10
12
|
"react",
|
|
11
13
|
"hooks",
|
|
14
|
+
"agent",
|
|
15
|
+
"webmcp",
|
|
12
16
|
"ai",
|
|
13
|
-
"tools",
|
|
14
17
|
"openai",
|
|
15
18
|
"claude",
|
|
16
19
|
"vercel-ai",
|
|
17
|
-
"agentfront"
|
|
18
|
-
"frontmcp"
|
|
20
|
+
"agentfront"
|
|
19
21
|
],
|
|
20
22
|
"repository": {
|
|
21
23
|
"type": "git",
|
|
22
|
-
"url": "git+https://github.com/agentfront/frontmcp.git"
|
|
24
|
+
"url": "git+https://github.com/agentfront/frontmcp.git",
|
|
25
|
+
"directory": "libs/react"
|
|
23
26
|
},
|
|
24
27
|
"bugs": {
|
|
25
28
|
"url": "https://github.com/agentfront/frontmcp/issues"
|
|
@@ -86,9 +89,9 @@
|
|
|
86
89
|
"node": ">=24.0.0"
|
|
87
90
|
},
|
|
88
91
|
"peerDependencies": {
|
|
89
|
-
"@frontmcp/lazy-zod": "1.9.
|
|
90
|
-
"@frontmcp/sdk": "1.9.
|
|
91
|
-
"@frontmcp/utils": "1.9.
|
|
92
|
+
"@frontmcp/lazy-zod": "1.9.7",
|
|
93
|
+
"@frontmcp/sdk": "1.9.7",
|
|
94
|
+
"@frontmcp/utils": "1.9.7",
|
|
92
95
|
"react": "^18.0.0 || ^19.0.0",
|
|
93
96
|
"react-dom": "^18.0.0 || ^19.0.0",
|
|
94
97
|
"react-router-dom": "^7.0.0"
|
|
@@ -102,8 +105,8 @@
|
|
|
102
105
|
}
|
|
103
106
|
},
|
|
104
107
|
"dependencies": {
|
|
105
|
-
"@frontmcp/sdk": "1.9.
|
|
106
|
-
"@frontmcp/utils": "1.9.
|
|
108
|
+
"@frontmcp/sdk": "1.9.7",
|
|
109
|
+
"@frontmcp/utils": "1.9.7"
|
|
107
110
|
},
|
|
108
111
|
"devDependencies": {
|
|
109
112
|
"typescript": "^5.9.3"
|