@mcp-fe/mcp-worker 0.0.16 → 0.1.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/LICENSE +1 -1
- package/README.md +184 -349
- package/docs/api.md +418 -0
- package/docs/architecture.md +195 -0
- package/docs/guide.md +454 -0
- package/docs/index.md +109 -0
- package/docs/initialization.md +188 -0
- package/docs/worker-details.md +435 -0
- package/index.js +514 -34
- package/mcp-service-worker.js +605 -254
- package/mcp-shared-worker.js +664 -243
- package/package.json +1 -1
- package/src/index.d.ts +4 -2
- package/src/index.d.ts.map +1 -1
- package/src/lib/built-in-tools.d.ts +9 -0
- package/src/lib/built-in-tools.d.ts.map +1 -0
- package/src/lib/logger.d.ts +17 -0
- package/src/lib/logger.d.ts.map +1 -0
- package/src/lib/mcp-controller.d.ts +19 -0
- package/src/lib/mcp-controller.d.ts.map +1 -1
- package/src/lib/mcp-server.d.ts +11 -0
- package/src/lib/mcp-server.d.ts.map +1 -1
- package/src/lib/tool-registry.d.ts +28 -0
- package/src/lib/tool-registry.d.ts.map +1 -0
- package/src/lib/worker-client.d.ts +123 -0
- package/src/lib/worker-client.d.ts.map +1 -1
package/LICENSE
CHANGED
|
@@ -188,7 +188,7 @@
|
|
|
188
188
|
same page as the copyright notice for easier identification within
|
|
189
189
|
third-party archives.
|
|
190
190
|
|
|
191
|
-
Copyright 2026
|
|
191
|
+
Copyright 2026 Michal Kopecky
|
|
192
192
|
|
|
193
193
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
194
194
|
you may not use this file except in compliance with the License.
|
package/README.md
CHANGED
|
@@ -1,479 +1,314 @@
|
|
|
1
1
|
# @mcp-fe/mcp-worker
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Browser-based MCP server running in Web Workers. Connect AI agents directly to your frontend application state.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## What is MCP-FE Worker?
|
|
6
6
|
|
|
7
|
-
`@mcp-fe/mcp-worker` turns your browser into
|
|
7
|
+
`@mcp-fe/mcp-worker` turns your browser into a queryable MCP server. It allows AI agents (like Claude) to:
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
- 🔍 Query user interactions in real-time
|
|
10
|
+
- 📊 Access application state directly
|
|
11
|
+
- 🎯 Register custom tools dynamically
|
|
12
|
+
- 💾 Store and retrieve events from IndexedDB
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
- **Dual Worker Support**: Uses SharedWorker (preferred) with ServiceWorker fallback
|
|
13
|
-
- **IndexedDB Storage**: Persistent storage for user events and application state
|
|
14
|
-
- **WebSocket Transport**: Real-time connection to MCP proxy servers
|
|
15
|
-
- **Zero Backend Dependencies**: Runs entirely in the browser
|
|
16
|
-
- **Authentication Support**: Built-in token-based authentication
|
|
17
|
-
- **Connection Management**: Automatic reconnection and status monitoring
|
|
14
|
+
The MCP server runs in a Web Worker in your browser, requiring an MCP proxy server to bridge communication with AI agents.
|
|
18
15
|
|
|
19
|
-
##
|
|
20
|
-
|
|
21
|
-
The package implements a **Worker-as-MCP-Edge-Server** pattern:
|
|
22
|
-
|
|
23
|
-
```
|
|
24
|
-
Frontend App ←→ WorkerClient ←→ Web Worker (MCP Server) ←→ WebSocket ←→ MCP Proxy ←→ AI Agent
|
|
25
|
-
```
|
|
16
|
+
## Key Concepts
|
|
26
17
|
|
|
27
|
-
|
|
28
|
-
2. **WorkerClient**: Manages worker lifecycle and provides clean API
|
|
29
|
-
3. **Web Worker**: Implements MCP server endpoints, stores data in IndexedDB
|
|
30
|
-
4. **WebSocket**: Maintains persistent connection to MCP proxy server
|
|
31
|
-
5. **MCP Proxy**: Bridges browser worker with external AI agents
|
|
32
|
-
6. **AI Agent**: Queries frontend state using standard MCP tools
|
|
18
|
+
### MCP Server in Browser
|
|
33
19
|
|
|
34
|
-
|
|
20
|
+
This library runs an **MCP server in your browser** using Web Workers, exposing frontend application context to AI agents. This enables AI agents to query live browser state (DOM, localStorage, React state, etc.) through the standard MCP protocol.
|
|
35
21
|
|
|
36
|
-
|
|
37
|
-
npm install @mcp-fe/mcp-worker
|
|
38
|
-
# or
|
|
39
|
-
pnpm add @mcp-fe/mcp-worker
|
|
40
|
-
# or
|
|
41
|
-
yarn add @mcp-fe/mcp-worker
|
|
42
|
-
```
|
|
22
|
+
The key advantage is **making frontend context accessible** to AI agents without custom backend code for each use case.
|
|
43
23
|
|
|
44
|
-
|
|
24
|
+
### Dual Worker Strategy
|
|
45
25
|
|
|
46
|
-
|
|
26
|
+
The library uses **SharedWorker** (preferred) or **ServiceWorker** (fallback):
|
|
47
27
|
|
|
48
|
-
|
|
28
|
+
- **SharedWorker**: Single instance shared across tabs, persistent connection
|
|
29
|
+
- **ServiceWorker**: Universal browser support, automatic fallback
|
|
49
30
|
|
|
50
|
-
|
|
51
|
-
# Copy worker files to your public directory
|
|
52
|
-
cp node_modules/@mcp-fe/mcp-worker/mcp-shared-worker.js public/
|
|
53
|
-
cp node_modules/@mcp-fe/mcp-worker/mcp-service-worker.js public/
|
|
54
|
-
```
|
|
31
|
+
### Dynamic Tool Registration
|
|
55
32
|
|
|
56
|
-
|
|
33
|
+
Register custom MCP tools at runtime with **handlers running in the main thread**:
|
|
57
34
|
|
|
58
|
-
**Vite example:**
|
|
59
35
|
```typescript
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
external: ['@mcp-fe/mcp-worker/mcp-*.js']
|
|
70
|
-
}
|
|
36
|
+
await workerClient.registerTool(
|
|
37
|
+
'get_user_data',
|
|
38
|
+
'Get current user information',
|
|
39
|
+
{ type: 'object', properties: {} },
|
|
40
|
+
async () => {
|
|
41
|
+
const user = getCurrentUser(); // Full browser access!
|
|
42
|
+
return {
|
|
43
|
+
content: [{ type: 'text', text: JSON.stringify(user) }]
|
|
44
|
+
};
|
|
71
45
|
}
|
|
72
|
-
|
|
46
|
+
);
|
|
73
47
|
```
|
|
74
48
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
49
|
+
Handlers have full access to:
|
|
50
|
+
- ✅ React context, hooks, state
|
|
51
|
+
- ✅ DOM API, localStorage
|
|
52
|
+
- ✅ All imports and dependencies
|
|
53
|
+
- ✅ Closures and external variables
|
|
79
54
|
|
|
80
|
-
|
|
81
|
-
async function initMCP() {
|
|
82
|
-
try {
|
|
83
|
-
await workerClient.init({
|
|
84
|
-
sharedWorkerUrl: '/mcp-shared-worker.js', // optional, default value
|
|
85
|
-
serviceWorkerUrl: '/mcp-service-worker.js', // optional, default value
|
|
86
|
-
backendWsUrl: 'ws://localhost:3001' // your MCP proxy server
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
console.log('MCP Worker initialized successfully');
|
|
90
|
-
} catch (error) {
|
|
91
|
-
console.error('Failed to initialize MCP Worker:', error);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
55
|
+
## Architecture
|
|
94
56
|
|
|
95
|
-
// Call during app startup
|
|
96
|
-
initMCP();
|
|
97
57
|
```
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
```typescript
|
|
102
|
-
// Set authentication token for user-specific data
|
|
103
|
-
workerClient.setAuthToken('Bearer your-jwt-token-here');
|
|
104
|
-
|
|
105
|
-
// Or queue the token before initialization
|
|
106
|
-
workerClient.setAuthToken('Bearer token');
|
|
107
|
-
await workerClient.init(/* options */);
|
|
58
|
+
Frontend App ←→ WorkerClient ←→ Web Worker ←→ WebSocket ←→ MCP Proxy ←→ AI Agent
|
|
59
|
+
↓
|
|
60
|
+
IndexedDB
|
|
108
61
|
```
|
|
109
62
|
|
|
110
|
-
|
|
63
|
+
1. **Frontend App** - Your application
|
|
64
|
+
2. **WorkerClient** - Simple API for worker communication
|
|
65
|
+
3. **Web Worker** - MCP server running in background
|
|
66
|
+
4. **WebSocket** - Real-time connection to proxy
|
|
67
|
+
5. **MCP Proxy** - Bridges browser with AI agents
|
|
68
|
+
6. **AI Agent** - Queries your app via MCP protocol
|
|
111
69
|
|
|
112
|
-
|
|
70
|
+
## Quick Start
|
|
113
71
|
|
|
114
|
-
|
|
72
|
+
### Installation
|
|
115
73
|
|
|
116
|
-
|
|
74
|
+
```bash
|
|
75
|
+
npm install @mcp-fe/mcp-worker
|
|
76
|
+
# or
|
|
77
|
+
pnpm add @mcp-fe/mcp-worker
|
|
78
|
+
```
|
|
117
79
|
|
|
118
|
-
|
|
80
|
+
### 1. Setup Worker Files
|
|
119
81
|
|
|
120
|
-
|
|
121
|
-
- `options?: WorkerClientInitOptions | ServiceWorkerRegistration`
|
|
82
|
+
Copy worker scripts to your public directory:
|
|
122
83
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
sharedWorkerUrl?: string; // Default: '/mcp-shared-worker.js'
|
|
127
|
-
serviceWorkerUrl?: string; // Default: '/mcp-service-worker.js'
|
|
128
|
-
backendWsUrl?: string; // Default: 'ws://localhost:3001'
|
|
129
|
-
}
|
|
84
|
+
```bash
|
|
85
|
+
cp node_modules/@mcp-fe/mcp-worker/mcp-shared-worker.js public/
|
|
86
|
+
cp node_modules/@mcp-fe/mcp-worker/mcp-service-worker.js public/
|
|
130
87
|
```
|
|
131
88
|
|
|
132
|
-
|
|
89
|
+
### 2. Initialize
|
|
90
|
+
|
|
133
91
|
```typescript
|
|
134
|
-
|
|
135
|
-
await workerClient.init();
|
|
92
|
+
import { workerClient } from '@mcp-fe/mcp-worker';
|
|
136
93
|
|
|
137
|
-
// Custom configuration
|
|
138
94
|
await workerClient.init({
|
|
139
|
-
backendWsUrl: '
|
|
140
|
-
sharedWorkerUrl: '/workers/mcp-shared-worker.js'
|
|
95
|
+
backendWsUrl: 'ws://localhost:3001' // Your MCP proxy URL
|
|
141
96
|
});
|
|
142
|
-
|
|
143
|
-
// Use existing ServiceWorker registration
|
|
144
|
-
const registration = await navigator.serviceWorker.register('/mcp-service-worker.js');
|
|
145
|
-
await workerClient.init(registration);
|
|
146
97
|
```
|
|
147
98
|
|
|
148
|
-
|
|
99
|
+
### 3. Store Events
|
|
149
100
|
|
|
150
|
-
Send a fire-and-forget message to the worker.
|
|
151
|
-
|
|
152
|
-
**Parameters:**
|
|
153
|
-
- `type: string` - Message type
|
|
154
|
-
- `payload?: Record<string, unknown>` - Message payload
|
|
155
|
-
|
|
156
|
-
**Example:**
|
|
157
101
|
```typescript
|
|
158
|
-
// Store a user event
|
|
159
102
|
await workerClient.post('STORE_EVENT', {
|
|
160
103
|
event: {
|
|
161
104
|
type: 'click',
|
|
162
105
|
element: 'button',
|
|
163
|
-
elementText: 'Submit
|
|
164
|
-
path: '/checkout',
|
|
106
|
+
elementText: 'Submit',
|
|
165
107
|
timestamp: Date.now()
|
|
166
108
|
}
|
|
167
109
|
});
|
|
168
110
|
```
|
|
169
111
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
Send a request expecting a response via MessageChannel.
|
|
173
|
-
|
|
174
|
-
**Parameters:**
|
|
175
|
-
- `type: string` - Request type
|
|
176
|
-
- `payload?: Record<string, unknown>` - Request payload
|
|
177
|
-
- `timeoutMs?: number` - Timeout in milliseconds (default: 5000)
|
|
178
|
-
|
|
179
|
-
**Returns:** `Promise<T>` - Response data
|
|
180
|
-
|
|
181
|
-
**Example:**
|
|
182
|
-
```typescript
|
|
183
|
-
// Get stored events
|
|
184
|
-
const response = await workerClient.request('GET_EVENTS', {
|
|
185
|
-
type: 'click',
|
|
186
|
-
limit: 10
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
console.log('Recent clicks:', response.events);
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
#### `workerClient.setAuthToken(token)`
|
|
193
|
-
|
|
194
|
-
Set authentication token for the current session.
|
|
195
|
-
|
|
196
|
-
**Parameters:**
|
|
197
|
-
- `token: string` - Authentication token (e.g., JWT)
|
|
198
|
-
|
|
199
|
-
**Example:**
|
|
200
|
-
```typescript
|
|
201
|
-
// Set token after user login
|
|
202
|
-
workerClient.setAuthToken('Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6...');
|
|
203
|
-
|
|
204
|
-
// Clear token on logout
|
|
205
|
-
workerClient.setAuthToken('');
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
#### `workerClient.getConnectionStatus()`
|
|
209
|
-
|
|
210
|
-
Get current connection status to the MCP proxy server.
|
|
211
|
-
|
|
212
|
-
**Returns:** `Promise<boolean>` - Connection status
|
|
112
|
+
### 4. Register Custom Tools
|
|
213
113
|
|
|
214
|
-
**Example:**
|
|
215
114
|
```typescript
|
|
216
|
-
|
|
217
|
-
|
|
115
|
+
await workerClient.registerTool(
|
|
116
|
+
'get_todos',
|
|
117
|
+
'Get all todos',
|
|
118
|
+
{ type: 'object', properties: {} },
|
|
119
|
+
async () => ({
|
|
120
|
+
content: [{ type: 'text', text: JSON.stringify(todos) }]
|
|
121
|
+
})
|
|
122
|
+
);
|
|
218
123
|
```
|
|
219
124
|
|
|
220
|
-
|
|
125
|
+
**That's it!** AI agents can now query your app via MCP protocol.
|
|
221
126
|
|
|
222
|
-
|
|
127
|
+
## Documentation
|
|
223
128
|
|
|
224
|
-
|
|
225
|
-
// Listen for connection changes
|
|
226
|
-
const handleConnectionChange = (connected: boolean) => {
|
|
227
|
-
console.log('Connection status changed:', connected);
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
workerClient.onConnectionStatus(handleConnectionChange);
|
|
231
|
-
|
|
232
|
-
// Stop listening
|
|
233
|
-
workerClient.offConnectionStatus(handleConnectionChange);
|
|
234
|
-
```
|
|
129
|
+
### Core Documentation
|
|
235
130
|
|
|
236
|
-
|
|
131
|
+
- **[Quick Start Guide](./docs/guide.md)** - Complete guide to dynamic tool registration
|
|
132
|
+
- **[API Reference](./docs/api.md)** - Full API documentation
|
|
133
|
+
- **[Worker Details](./docs/worker-details.md)** - Implementation details
|
|
134
|
+
- **[Architecture](./docs/architecture.md)** - How the proxy pattern works
|
|
135
|
+
- **[Initialization](./docs/initialization.md)** - Init queue handling
|
|
237
136
|
|
|
238
|
-
###
|
|
137
|
+
### Examples
|
|
239
138
|
|
|
240
|
-
**
|
|
241
|
-
-
|
|
242
|
-
-
|
|
243
|
-
- Better for multi-tab applications
|
|
244
|
-
- Supported in Chrome, Firefox, and Safari
|
|
139
|
+
- **[Quick Start Examples](./examples/quick-start.ts)** - 4 simple examples
|
|
140
|
+
- **[Advanced Examples](./examples/dynamic-tools.ts)** - Validation, async, error handling
|
|
141
|
+
- **[Examples Guide](./examples/README.md)** - How to use examples
|
|
245
142
|
|
|
246
|
-
|
|
247
|
-
- Runs in background with browser-managed lifecycle
|
|
248
|
-
- Automatic fallback when SharedWorker is unavailable
|
|
249
|
-
- Handles offline scenarios and background sync
|
|
250
|
-
- Universal browser support
|
|
143
|
+
### React Integration
|
|
251
144
|
|
|
252
|
-
|
|
145
|
+
- **[React Hooks Guide](../react-event-tracker/REACT_MCP_TOOLS.md)** - React integration
|
|
146
|
+
- **[React Examples](../react-event-tracker/src/examples/ReactMCPToolsExamples.tsx)** - Component examples
|
|
253
147
|
|
|
254
|
-
|
|
148
|
+
## Common Use Cases
|
|
255
149
|
|
|
256
|
-
|
|
150
|
+
### Track User Interactions
|
|
257
151
|
|
|
258
152
|
```typescript
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
path?: string;
|
|
264
|
-
from?: string; // navigation: previous route
|
|
265
|
-
to?: string; // navigation: current route
|
|
266
|
-
element?: string; // interaction: element tag
|
|
267
|
-
elementId?: string; // interaction: element ID
|
|
268
|
-
elementClass?: string; // interaction: element classes
|
|
269
|
-
elementText?: string; // interaction: element text content
|
|
270
|
-
metadata?: Record<string, unknown>;
|
|
271
|
-
}
|
|
153
|
+
// Clicks, navigation, form inputs
|
|
154
|
+
await workerClient.post('STORE_EVENT', {
|
|
155
|
+
event: { type: 'click', element: 'button', ... }
|
|
156
|
+
});
|
|
272
157
|
```
|
|
273
158
|
|
|
274
|
-
###
|
|
275
|
-
|
|
276
|
-
The worker exposes these MCP tools to AI agents:
|
|
277
|
-
|
|
278
|
-
- `get_user_events` - Query stored user interaction events
|
|
279
|
-
- `get_connection_status` - Check WebSocket connection status
|
|
280
|
-
- `get_session_info` - Get current session information
|
|
281
|
-
|
|
282
|
-
## Advanced Usage
|
|
283
|
-
|
|
284
|
-
### Custom Event Storage
|
|
159
|
+
### Expose Application State
|
|
285
160
|
|
|
286
161
|
```typescript
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
type: 'custom',
|
|
291
|
-
timestamp: Date.now(),
|
|
292
|
-
metadata: {
|
|
293
|
-
eventName: 'purchase_completed',
|
|
294
|
-
orderId: '12345',
|
|
295
|
-
amount: 99.99,
|
|
296
|
-
currency: 'USD'
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
});
|
|
162
|
+
await workerClient.registerTool('get_cart', 'Get shopping cart', ...,
|
|
163
|
+
async () => ({ content: [{ type: 'text', text: JSON.stringify(cart) }] })
|
|
164
|
+
);
|
|
300
165
|
```
|
|
301
166
|
|
|
302
|
-
###
|
|
167
|
+
### Query Stored Events
|
|
303
168
|
|
|
304
169
|
```typescript
|
|
305
|
-
|
|
306
|
-
const response = await workerClient.request('GET_EVENTS', {
|
|
170
|
+
const events = await workerClient.request('GET_EVENTS', {
|
|
307
171
|
type: 'navigation',
|
|
308
|
-
startTime: Date.now() - (60 * 60 * 1000),
|
|
309
172
|
limit: 50
|
|
310
173
|
});
|
|
311
|
-
|
|
312
|
-
// Get clicks on specific elements
|
|
313
|
-
const clicks = await workerClient.request('GET_EVENTS', {
|
|
314
|
-
type: 'click',
|
|
315
|
-
path: '/checkout',
|
|
316
|
-
limit: 20
|
|
317
|
-
});
|
|
318
174
|
```
|
|
319
175
|
|
|
320
|
-
###
|
|
176
|
+
### Monitor Connection Status
|
|
321
177
|
|
|
322
178
|
```typescript
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
if (error.message.includes('SharedWorker')) {
|
|
327
|
-
console.log('SharedWorker not supported, falling back to ServiceWorker');
|
|
328
|
-
} else {
|
|
329
|
-
console.error('Worker initialization failed:', error);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
// Handle request timeouts
|
|
334
|
-
try {
|
|
335
|
-
const data = await workerClient.request('GET_EVENTS', {}, 2000); // 2s timeout
|
|
336
|
-
} catch (error) {
|
|
337
|
-
if (error.message.includes('timeout')) {
|
|
338
|
-
console.log('Request timed out, worker may be busy');
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
```
|
|
342
|
-
|
|
343
|
-
## Integration with Higher-Level Libraries
|
|
344
|
-
|
|
345
|
-
This package is designed to be used with higher-level integration libraries:
|
|
346
|
-
|
|
347
|
-
- **[@mcp-fe/event-tracker](../event-tracker/README.md)**: Framework-agnostic event tracking
|
|
348
|
-
- **[@mcp-fe/react-event-tracker](../react-event-tracker/README.md)**: React-specific hooks and components
|
|
349
|
-
|
|
350
|
-
**Example with event-tracker:**
|
|
351
|
-
```typescript
|
|
352
|
-
import { initEventTracker, trackEvent } from '@mcp-fe/event-tracker';
|
|
353
|
-
|
|
354
|
-
// Initialize (uses @mcp-fe/mcp-worker internally)
|
|
355
|
-
await initEventTracker({
|
|
356
|
-
backendWsUrl: 'ws://localhost:3001'
|
|
357
|
-
});
|
|
358
|
-
|
|
359
|
-
// Track events (stored via mcp-worker)
|
|
360
|
-
await trackEvent({
|
|
361
|
-
type: 'click',
|
|
362
|
-
element: 'button',
|
|
363
|
-
elementText: 'Save Changes'
|
|
179
|
+
const connected = await workerClient.getConnectionStatus();
|
|
180
|
+
workerClient.onConnectionStatus((connected) => {
|
|
181
|
+
console.log('MCP connection:', connected);
|
|
364
182
|
});
|
|
365
183
|
```
|
|
366
184
|
|
|
367
|
-
##
|
|
368
|
-
|
|
369
|
-
The worker connects to an MCP proxy server that bridges browser workers with AI agents. You need a Node.js MCP proxy running to use this package effectively.
|
|
185
|
+
## MCP Proxy Server
|
|
370
186
|
|
|
371
|
-
|
|
187
|
+
The worker connects to an MCP proxy server that bridges browser with AI agents.
|
|
372
188
|
|
|
373
|
-
|
|
189
|
+
### Using Docker (Recommended)
|
|
374
190
|
|
|
375
191
|
```bash
|
|
376
|
-
# Pull and run the MCP proxy server
|
|
377
192
|
docker pull ghcr.io/mcp-fe/mcp-fe/mcp-server:main
|
|
378
193
|
docker run -p 3001:3001 ghcr.io/mcp-fe/mcp-fe/mcp-server:main
|
|
379
194
|
```
|
|
380
195
|
|
|
381
|
-
|
|
196
|
+
Server available at `ws://localhost:3001`
|
|
382
197
|
|
|
198
|
+
### Development
|
|
383
199
|
|
|
384
|
-
**With Environment Variables:**
|
|
385
200
|
```bash
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
201
|
+
git clone https://github.com/mcp-fe/mcp-fe.git
|
|
202
|
+
cd mcp-fe
|
|
203
|
+
pnpm install
|
|
204
|
+
nx serve mcp-server
|
|
390
205
|
```
|
|
391
206
|
|
|
392
|
-
|
|
207
|
+
See [mcp-server docs](../../apps/mcp-server/README.md) for complete setup.
|
|
393
208
|
|
|
394
|
-
|
|
209
|
+
## Features
|
|
395
210
|
|
|
396
|
-
|
|
397
|
-
# Clone the MCP-FE repository
|
|
398
|
-
git clone https://github.com/mcp-fe/mcp-fe.git
|
|
399
|
-
cd mcp-fe
|
|
211
|
+
### Dynamic Tool Registration
|
|
400
212
|
|
|
401
|
-
|
|
402
|
-
pnpm install
|
|
213
|
+
Register custom MCP tools at runtime:
|
|
403
214
|
|
|
404
|
-
|
|
405
|
-
|
|
215
|
+
```typescript
|
|
216
|
+
await workerClient.registerTool(
|
|
217
|
+
'get_user_data',
|
|
218
|
+
'Get current user information',
|
|
219
|
+
{ type: 'object', properties: {} },
|
|
220
|
+
async () => {
|
|
221
|
+
const user = getCurrentUser(); // Full browser access!
|
|
222
|
+
return {
|
|
223
|
+
content: [{
|
|
224
|
+
type: 'text',
|
|
225
|
+
text: JSON.stringify(user)
|
|
226
|
+
}]
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
);
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
**Learn more:**
|
|
233
|
+
- [Guide](./docs/guide.md) - Complete step-by-step guide
|
|
234
|
+
- [Quick Start Examples](./examples/quick-start.ts) - Ready-to-use examples
|
|
235
|
+
- [Advanced Examples](./examples/dynamic-tools.ts) - Validation, async, error handling
|
|
236
|
+
- [React Integration](../react-event-tracker/REACT_MCP_TOOLS.md) - React hooks
|
|
237
|
+
|
|
238
|
+
### Event Storage
|
|
239
|
+
|
|
240
|
+
Store and query user interactions:
|
|
241
|
+
|
|
242
|
+
```typescript
|
|
243
|
+
// Store event
|
|
244
|
+
await workerClient.post('STORE_EVENT', {
|
|
245
|
+
event: { type: 'click', element: 'button', ... }
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
// Query events
|
|
249
|
+
const events = await workerClient.request('GET_EVENTS', {
|
|
250
|
+
type: 'click',
|
|
251
|
+
limit: 10
|
|
252
|
+
});
|
|
406
253
|
```
|
|
407
254
|
|
|
408
|
-
|
|
409
|
-
- WebSocket connections from browser workers
|
|
410
|
-
- MCP protocol message routing
|
|
411
|
-
- Tool call forwarding between AI agents and frontend applications
|
|
412
|
-
- Connection management and error handling
|
|
255
|
+
### Connection Management
|
|
413
256
|
|
|
414
|
-
|
|
257
|
+
Monitor MCP proxy connection:
|
|
258
|
+
|
|
259
|
+
```typescript
|
|
260
|
+
const connected = await workerClient.getConnectionStatus();
|
|
261
|
+
workerClient.onConnectionStatus((connected) => {
|
|
262
|
+
console.log('Status:', connected);
|
|
263
|
+
});
|
|
264
|
+
```
|
|
415
265
|
|
|
416
266
|
## Browser Compatibility
|
|
417
267
|
|
|
418
|
-
- **Chrome/Chromium
|
|
419
|
-
- **Firefox
|
|
420
|
-
- **Safari
|
|
421
|
-
- **Edge
|
|
268
|
+
- ✅ **Chrome/Chromium 80+** - Full support
|
|
269
|
+
- ✅ **Firefox 78+** - Full support
|
|
270
|
+
- ✅ **Safari 16.4+** - Full support
|
|
271
|
+
- ✅ **Edge 80+** - Full support (Chromium-based)
|
|
422
272
|
|
|
423
|
-
**
|
|
424
|
-
- ES2020+ support
|
|
425
|
-
- WebWorker support
|
|
426
|
-
- IndexedDB support
|
|
427
|
-
- WebSocket support (for MCP proxy connection)
|
|
273
|
+
**Requirements:** ES2020+, WebWorker, IndexedDB, WebSocket
|
|
428
274
|
|
|
429
|
-
|
|
275
|
+
See [Worker Details](./docs/worker-details.md) for more information.
|
|
430
276
|
|
|
431
|
-
|
|
277
|
+
## Troubleshooting
|
|
432
278
|
|
|
433
|
-
|
|
279
|
+
### Worker files not found (404)
|
|
434
280
|
|
|
435
|
-
|
|
436
|
-
1. Ensure worker files are copied to your public directory
|
|
437
|
-
2. Verify the URLs match your server configuration
|
|
438
|
-
3. Check browser Network tab for 404 errors
|
|
281
|
+
Ensure worker files are in your public directory and paths match:
|
|
439
282
|
|
|
440
283
|
```typescript
|
|
441
|
-
// Custom paths if needed
|
|
442
284
|
await workerClient.init({
|
|
443
|
-
sharedWorkerUrl: '/
|
|
444
|
-
serviceWorkerUrl: '/
|
|
285
|
+
sharedWorkerUrl: '/path/to/mcp-shared-worker.js',
|
|
286
|
+
serviceWorkerUrl: '/path/to/mcp-service-worker.js'
|
|
445
287
|
});
|
|
446
288
|
```
|
|
447
289
|
|
|
448
|
-
### Connection
|
|
449
|
-
|
|
450
|
-
**Problem**: `getConnectionStatus()` returns `false`
|
|
451
|
-
|
|
452
|
-
**Solution**:
|
|
453
|
-
1. Verify MCP proxy server is running on the specified URL
|
|
454
|
-
2. Check WebSocket connection in browser Developer Tools
|
|
455
|
-
3. Verify CORS settings if proxy is on different origin
|
|
290
|
+
### Connection issues
|
|
456
291
|
|
|
457
|
-
|
|
292
|
+
1. Verify MCP proxy server is running
|
|
293
|
+
2. Check WebSocket connection in DevTools Network tab
|
|
294
|
+
3. Verify CORS settings if on different origin
|
|
458
295
|
|
|
459
|
-
|
|
296
|
+
### SharedWorker not available
|
|
460
297
|
|
|
461
|
-
|
|
462
|
-
1. SharedWorker requires HTTPS in production
|
|
463
|
-
2. Some browsers disable SharedWorker in private/incognito mode
|
|
464
|
-
3. Enterprise browser policies may block SharedWorker
|
|
298
|
+
SharedWorker requires HTTPS in production and may be blocked in incognito mode. The library automatically falls back to ServiceWorker.
|
|
465
299
|
|
|
300
|
+
**For more help:** See [Worker Details](./docs/worker-details.md#troubleshooting)
|
|
466
301
|
|
|
467
302
|
## Related Packages
|
|
468
303
|
|
|
469
|
-
-
|
|
470
|
-
-
|
|
471
|
-
-
|
|
304
|
+
- [Main MCP-FE Project](../../README.md) - Complete documentation
|
|
305
|
+
- [@mcp-fe/event-tracker](../event-tracker/README.md) - Framework-agnostic event tracking
|
|
306
|
+
- [@mcp-fe/react-event-tracker](../react-event-tracker/README.md) - React integration hooks
|
|
472
307
|
|
|
473
308
|
## License
|
|
474
309
|
|
|
475
|
-
Licensed under the Apache License, Version 2.0. See
|
|
310
|
+
Licensed under the Apache License, Version 2.0. See [LICENSE](../../LICENSE) for details.
|
|
476
311
|
|
|
477
312
|
---
|
|
478
313
|
|
|
479
|
-
**
|
|
314
|
+
**For most applications, consider using [@mcp-fe/react-event-tracker](../react-event-tracker/README.md) for a more convenient React-focused API.**
|