@mcp-fe/mcp-worker 0.1.6 → 0.1.8

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.
Files changed (47) hide show
  1. package/README.md +26 -0
  2. package/docs/index.md +15 -4
  3. package/docs/initialization.md +2 -2
  4. package/docs/project-structure.md +172 -0
  5. package/docs/structured-output.md +484 -0
  6. package/index.js +34 -250
  7. package/mcp-service-worker.js +572 -7468
  8. package/mcp-shared-worker.js +574 -7470
  9. package/package.json +1 -1
  10. package/src/client/index.d.ts +10 -0
  11. package/src/client/index.d.ts.map +1 -0
  12. package/src/client/worker-client.d.ts.map +1 -0
  13. package/src/index.d.ts +3 -7
  14. package/src/index.d.ts.map +1 -1
  15. package/src/shared/database.d.ts +8 -0
  16. package/src/shared/database.d.ts.map +1 -0
  17. package/src/shared/logger.d.ts.map +1 -0
  18. package/src/{lib/tool-registry.d.ts → shared/types.d.ts} +27 -13
  19. package/src/shared/types.d.ts.map +1 -0
  20. package/src/worker/built-in-tools.d.ts.map +1 -0
  21. package/src/worker/index.d.ts +12 -0
  22. package/src/worker/index.d.ts.map +1 -0
  23. package/src/{lib → worker}/mcp-controller.d.ts +14 -2
  24. package/src/worker/mcp-controller.d.ts.map +1 -0
  25. package/src/worker/mcp-server.d.ts.map +1 -0
  26. package/src/{lib → worker}/tab-manager.d.ts +2 -5
  27. package/src/worker/tab-manager.d.ts.map +1 -0
  28. package/src/worker/tool-registry.d.ts +17 -0
  29. package/src/worker/tool-registry.d.ts.map +1 -0
  30. package/src/worker/validation.d.ts +31 -0
  31. package/src/worker/validation.d.ts.map +1 -0
  32. package/src/worker/websocket-transport.d.ts.map +1 -0
  33. package/src/lib/built-in-tools.d.ts.map +0 -1
  34. package/src/lib/database.d.ts +0 -27
  35. package/src/lib/database.d.ts.map +0 -1
  36. package/src/lib/logger.d.ts.map +0 -1
  37. package/src/lib/mcp-controller.d.ts.map +0 -1
  38. package/src/lib/mcp-server.d.ts.map +0 -1
  39. package/src/lib/tab-manager.d.ts.map +0 -1
  40. package/src/lib/tool-registry.d.ts.map +0 -1
  41. package/src/lib/websocket-transport.d.ts.map +0 -1
  42. package/src/lib/worker-client.d.ts.map +0 -1
  43. /package/src/{lib → client}/worker-client.d.ts +0 -0
  44. /package/src/{lib → shared}/logger.d.ts +0 -0
  45. /package/src/{lib → worker}/built-in-tools.d.ts +0 -0
  46. /package/src/{lib → worker}/mcp-server.d.ts +0 -0
  47. /package/src/{lib → worker}/websocket-transport.d.ts +0 -0
package/README.md CHANGED
@@ -67,6 +67,31 @@ Frontend App ←→ WorkerClient ←→ Web Worker ←→ WebSocket ←→ MCP P
67
67
  5. **MCP Proxy** - Bridges browser with AI agents
68
68
  6. **AI Agent** - Queries your app via MCP protocol
69
69
 
70
+ ### Project Structure
71
+
72
+ The library is organized into three main directories:
73
+
74
+ ```
75
+ libs/mcp-worker/src/
76
+ ├── client/ # Client-side code (application runtime)
77
+ │ ├── worker-client.ts # Main WorkerClient class
78
+ │ └── index.ts # Client API exports
79
+ ├── worker/ # Worker-side code (background processing)
80
+ │ ├── mcp-controller.ts # MCP server controller
81
+ │ ├── mcp-server.ts # MCP server setup
82
+ │ ├── tab-manager.ts # Multi-tab coordination
83
+ │ ├── tool-registry.ts # Dynamic tool management
84
+ │ └── built-in-tools.ts # Default MCP tools
85
+ └── shared/ # Shared code (both contexts)
86
+ ├── types.ts # TypeScript type definitions
87
+ ├── logger.ts # Logging utilities
88
+ └── database.ts # IndexedDB operations
89
+ ```
90
+
91
+ **Entry points:**
92
+ - `mcp-shared-worker.ts` - SharedWorker implementation (preferred)
93
+ - `mcp-service-worker.ts` - ServiceWorker fallback
94
+
70
95
  ## Quick Start
71
96
 
72
97
  ### Installation
@@ -130,6 +155,7 @@ await workerClient.registerTool(
130
155
 
131
156
  - **[Quick Start Guide](./docs/guide.md)** - Complete guide to dynamic tool registration
132
157
  - **[API Reference](./docs/api.md)** - Full API documentation
158
+ - **[Project Structure](./docs/project-structure.md)** - Codebase organization explained
133
159
  - **[Worker Details](./docs/worker-details.md)** - Implementation details
134
160
  - **[Architecture](./docs/architecture.md)** - How the proxy pattern works
135
161
  - **[Initialization](./docs/initialization.md)** - Init queue handling
package/docs/index.md CHANGED
@@ -12,6 +12,7 @@ Complete guide to all documentation and examples in the MCP Worker library.
12
12
  | **Complete API reference** | [API Reference](./api.md) |
13
13
  | **Multi-tab applications** | [Multi-Tab Guide](./multi-tab.md) |
14
14
  | **Worker implementation** | [Worker Details](./worker-details.md) |
15
+ | **Understand codebase structure** | [Project Structure](./project-structure.md) |
15
16
  | **Use with React** | [React Hooks Guide](../../react-event-tracker/REACT_MCP_TOOLS.md) |
16
17
  | **Understand architecture** | [Architecture](./architecture.md) |
17
18
  | **Handle initialization** | [Initialization](./initialization.md) |
@@ -29,16 +30,26 @@ libs/mcp-worker/
29
30
  │ ├── multi-tab.md ← Multi-tab support
30
31
  │ ├── worker-details.md ← Worker implementation
31
32
  │ ├── architecture.md ← Technical architecture
32
- └── initialization.md ← Init handling
33
+ ├── initialization.md ← Init handling
34
+ │ ├── project-structure.md ← Codebase organization
35
+ │ └── tab-manager.md ← Tab management
33
36
 
34
37
  ├── examples/ ← Code examples
35
38
  │ ├── README.md ← Examples guide
36
39
  │ ├── quick-start.ts ← Simple examples
37
- └── dynamic-tools.ts ← Advanced patterns
40
+ ├── dynamic-tools.ts ← Advanced patterns
41
+ │ └── structured-output.ts ← Structured outputs
38
42
 
39
43
  └── src/ ← Source code
40
- └── lib/
41
- └── worker-client.ts
44
+ ├── client/ ← Application runtime
45
+ └── worker-client.ts
46
+ ├── worker/ ← Worker implementation
47
+ │ ├── mcp-controller.ts
48
+ │ └── ...
49
+ └── shared/ ← Shared utilities
50
+ ├── types.ts
51
+ ├── logger.ts
52
+ └── database.ts
42
53
 
43
54
  libs/react-event-tracker/
44
55
  ├── REACT_MCP_TOOLS.md ← React hooks docs
@@ -182,7 +182,7 @@ For contributors:
182
182
  - Queues are processed in order (FIFO)
183
183
 
184
184
  See source code for implementation details:
185
- - `libs/mcp-worker/src/lib/worker-client.ts`
186
- - `libs/mcp-worker/src/lib/mcp-controller.ts`
185
+ - `libs/mcp-worker/src/client/worker-client.ts`
186
+ - `libs/mcp-worker/src/worker/mcp-controller.ts`
187
187
 
188
188
 
@@ -0,0 +1,172 @@
1
+ # Project Structure
2
+
3
+ This document explains the organization of the `@mcp-fe/mcp-worker` codebase.
4
+
5
+ ## Directory Layout
6
+
7
+ ```
8
+ libs/mcp-worker/src/
9
+ ├── index.ts # Main entry point (re-exports from client/)
10
+ ├── mcp-shared-worker.ts # SharedWorker entry point
11
+ ├── mcp-service-worker.ts # ServiceWorker entry point
12
+ ├── client/ # Client-side code (application runtime)
13
+ │ ├── index.ts # Client API exports
14
+ │ └── worker-client.ts # Main WorkerClient class
15
+ ├── worker/ # Worker-side code (background processing)
16
+ │ ├── index.ts # Worker internal exports
17
+ │ ├── mcp-controller.ts # MCP server controller & lifecycle
18
+ │ ├── mcp-server.ts # MCP server setup & handlers
19
+ │ ├── websocket-transport.ts # WebSocket transport for MCP
20
+ │ ├── tool-registry.ts # Dynamic tool registration
21
+ │ ├── tab-manager.ts # Multi-tab coordination
22
+ │ ├── built-in-tools.ts # Default MCP tools
23
+ │ └── tab-manager.spec.ts # Tests for TabManager
24
+ └── shared/ # Shared code (both contexts)
25
+ ├── types.ts # TypeScript type definitions
26
+ ├── logger.ts # Logging utilities
27
+ └── database.ts # IndexedDB operations
28
+ ```
29
+
30
+ ## Module Responsibilities
31
+
32
+ ### Client (`client/`)
33
+
34
+ **Purpose:** Code that runs in the main browser thread (your application).
35
+
36
+ **Key Files:**
37
+ - `worker-client.ts` - Main API for communicating with workers
38
+ - Handles worker initialization (SharedWorker vs ServiceWorker)
39
+ - Manages tool registration and lifecycle
40
+ - Handles multi-tab coordination
41
+ - Provides request/response messaging
42
+
43
+ **Used by:** Your application code
44
+
45
+ **Example:**
46
+ ```typescript
47
+ import { workerClient } from '@mcp-fe/mcp-worker';
48
+ await workerClient.init();
49
+ ```
50
+
51
+ ### Worker (`worker/`)
52
+
53
+ **Purpose:** Code that runs inside Web Workers (background processing).
54
+
55
+ **Key Files:**
56
+ - `mcp-controller.ts` - Main controller for MCP server lifecycle
57
+ - WebSocket connection management
58
+ - Tool call routing and execution
59
+ - Tab management coordination
60
+ - Event storage and querying
61
+
62
+ - `mcp-server.ts` - MCP server setup using @modelcontextprotocol/sdk
63
+ - Request handlers (ListTools, CallTool)
64
+ - Server configuration and capabilities
65
+
66
+ - `tool-registry.ts` - Dynamic tool management
67
+ - Tool definition storage
68
+ - Handler registration and lookup
69
+ - Tool lifecycle management
70
+
71
+ - `tab-manager.ts` - Multi-tab coordination
72
+ - Tab registration and tracking
73
+ - Active tab management
74
+ - Smart tool routing across tabs
75
+
76
+ - `built-in-tools.ts` - Default MCP tools
77
+ - Event querying tools
78
+ - Tab listing tools
79
+ - Navigation history tools
80
+
81
+ **Used by:** Worker entry points (`mcp-shared-worker.ts`, `mcp-service-worker.ts`)
82
+
83
+ ### Shared (`shared/`)
84
+
85
+ **Purpose:** Code used by both client and worker contexts.
86
+
87
+ **Key Files:**
88
+ - `types.ts` - TypeScript type definitions
89
+ - `UserEvent`, `ToolDefinition`, `ToolHandler`
90
+ - `TabInfo`, `EventFilters`, etc.
91
+ - Ensures type consistency across contexts
92
+
93
+ - `logger.ts` - Logging utilities
94
+ - Environment-aware logging (dev vs production)
95
+ - Consistent logging interface
96
+ - Works in both main thread and workers
97
+
98
+ - `database.ts` - IndexedDB operations
99
+ - Event storage and retrieval
100
+ - Query filtering and pagination
101
+ - Available in both contexts (IndexedDB works everywhere)
102
+
103
+ **Used by:** Both client and worker code
104
+
105
+ ## Communication Flow
106
+
107
+ ```
108
+ Application Code
109
+ ↓ (imports)
110
+ client/worker-client.ts
111
+ ↓ (postMessage)
112
+ mcp-shared-worker.ts or mcp-service-worker.ts
113
+ ↓ (uses)
114
+ worker/mcp-controller.ts
115
+ ↓ (uses)
116
+ worker/mcp-server.ts
117
+ ↓ (uses)
118
+ worker/tool-registry.ts
119
+ ↓ (WebSocket)
120
+ MCP Proxy Server
121
+ ```
122
+
123
+ ## Import Patterns
124
+
125
+ ### For Application Code
126
+
127
+ ```typescript
128
+ // Import from the main package
129
+ import { workerClient, type ToolDefinition } from '@mcp-fe/mcp-worker';
130
+ ```
131
+
132
+ ### Internal Worker Code
133
+
134
+ ```typescript
135
+ // Worker modules import from shared/
136
+ import { logger } from '../shared/logger';
137
+ import type { UserEvent } from '../shared/types';
138
+
139
+ // Worker modules import from other worker modules
140
+ import { toolRegistry } from './tool-registry';
141
+ import { TabManager } from './tab-manager';
142
+ ```
143
+
144
+ ### Internal Client Code
145
+
146
+ ```typescript
147
+ // Client modules import from shared/
148
+ import { logger } from '../shared/logger';
149
+ import type { ToolDefinition } from '../shared/types';
150
+ ```
151
+
152
+ ## Why This Structure?
153
+
154
+ ### Clear Separation of Concerns
155
+ - **Client code** only deals with communication and API
156
+ - **Worker code** handles MCP protocol and business logic
157
+ - **Shared code** provides common utilities and types
158
+
159
+ ### Better Tree-Shaking
160
+ - Applications only import client code
161
+ - Worker bundles only include worker code
162
+ - No unnecessary code in either bundle
163
+
164
+ ### Maintainability
165
+ - Easy to find where specific functionality lives
166
+ - Clear boundaries between contexts
167
+ - Prevents accidental mixing of client/worker code
168
+
169
+ ### Future-Proof
170
+ - Ready for splitting into separate npm packages if needed
171
+ - Can add more worker types (e.g., dedicated workers)
172
+ - Easy to add more shared utilities