@mcp-web/bridge 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.
Files changed (72) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +311 -0
  3. package/dist/adapters/bun.d.ts +95 -0
  4. package/dist/adapters/bun.d.ts.map +1 -0
  5. package/dist/adapters/bun.js +286 -0
  6. package/dist/adapters/bun.js.map +1 -0
  7. package/dist/adapters/deno.d.ts +89 -0
  8. package/dist/adapters/deno.d.ts.map +1 -0
  9. package/dist/adapters/deno.js +249 -0
  10. package/dist/adapters/deno.js.map +1 -0
  11. package/dist/adapters/index.d.ts +21 -0
  12. package/dist/adapters/index.d.ts.map +1 -0
  13. package/dist/adapters/index.js +21 -0
  14. package/dist/adapters/index.js.map +1 -0
  15. package/dist/adapters/node.d.ts +112 -0
  16. package/dist/adapters/node.d.ts.map +1 -0
  17. package/dist/adapters/node.js +309 -0
  18. package/dist/adapters/node.js.map +1 -0
  19. package/dist/adapters/partykit.d.ts +153 -0
  20. package/dist/adapters/partykit.d.ts.map +1 -0
  21. package/dist/adapters/partykit.js +372 -0
  22. package/dist/adapters/partykit.js.map +1 -0
  23. package/dist/bridge.d.ts +38 -0
  24. package/dist/bridge.d.ts.map +1 -0
  25. package/dist/bridge.js +1004 -0
  26. package/dist/bridge.js.map +1 -0
  27. package/dist/core.d.ts +75 -0
  28. package/dist/core.d.ts.map +1 -0
  29. package/dist/core.js +1508 -0
  30. package/dist/core.js.map +1 -0
  31. package/dist/index.d.ts +38 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +42 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/runtime/index.d.ts +11 -0
  36. package/dist/runtime/index.d.ts.map +1 -0
  37. package/dist/runtime/index.js +9 -0
  38. package/dist/runtime/index.js.map +1 -0
  39. package/dist/runtime/scheduler.d.ts +69 -0
  40. package/dist/runtime/scheduler.d.ts.map +1 -0
  41. package/dist/runtime/scheduler.js +88 -0
  42. package/dist/runtime/scheduler.js.map +1 -0
  43. package/dist/runtime/types.d.ts +144 -0
  44. package/dist/runtime/types.d.ts.map +1 -0
  45. package/dist/runtime/types.js +82 -0
  46. package/dist/runtime/types.js.map +1 -0
  47. package/dist/schemas.d.ts +6 -0
  48. package/dist/schemas.d.ts.map +1 -0
  49. package/dist/schemas.js +6 -0
  50. package/dist/schemas.js.map +1 -0
  51. package/dist/types.d.ts +130 -0
  52. package/dist/types.d.ts.map +1 -0
  53. package/dist/types.js +2 -0
  54. package/dist/types.js.map +1 -0
  55. package/package.json +28 -0
  56. package/src/adapters/bun.ts +354 -0
  57. package/src/adapters/deno.ts +282 -0
  58. package/src/adapters/index.ts +28 -0
  59. package/src/adapters/node.ts +385 -0
  60. package/src/adapters/partykit.ts +482 -0
  61. package/src/bridge.test.ts +64 -0
  62. package/src/core.ts +2176 -0
  63. package/src/index.ts +90 -0
  64. package/src/limits.test.ts +436 -0
  65. package/src/remote-mcp.test.ts +770 -0
  66. package/src/runtime/index.ts +24 -0
  67. package/src/runtime/scheduler.ts +130 -0
  68. package/src/runtime/types.ts +229 -0
  69. package/src/schemas.ts +6 -0
  70. package/src/session-naming.test.ts +443 -0
  71. package/src/types.ts +180 -0
  72. package/tsconfig.json +12 -0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2025 Fritz Lekschas
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,311 @@
1
+ # MCP Web Bridge
2
+
3
+ A bridge server that enables web frontend applications to be controlled by AI agents through the Model Context Protocol (MCP). The bridge mediates between web applications (via WebSocket) and AI agents (via HTTP/JSON-RPC).
4
+
5
+ ## Overview
6
+
7
+ The bridge server:
8
+ - Runs a **single-port server** for both WebSocket and HTTP connections
9
+ - Manages session authentication and tool registration
10
+ - Routes tool calls between AI agents and the appropriate frontend session
11
+ - Supports multiple JavaScript runtimes (Node.js, Deno, Bun, PartyKit/Cloudflare)
12
+
13
+ ## Quick Start
14
+
15
+ ### Node.js
16
+
17
+ ```typescript
18
+ import { MCPWebBridgeNode } from '@mcp-web/bridge';
19
+
20
+ const bridge = new MCPWebBridgeNode({
21
+ name: 'My App',
22
+ description: 'My awesome application',
23
+ port: 3001,
24
+ });
25
+
26
+ // Graceful shutdown
27
+ process.on('SIGINT', async () => {
28
+ await bridge.close();
29
+ process.exit(0);
30
+ });
31
+ ```
32
+
33
+ ### Deno
34
+
35
+ ```typescript
36
+ import { MCPWebBridgeDeno } from '@mcp-web/bridge';
37
+
38
+ const bridge = new MCPWebBridgeDeno({
39
+ name: 'My App',
40
+ description: 'My awesome application',
41
+ port: 3001,
42
+ });
43
+
44
+ // Bridge is now listening on ws://localhost:3001 and http://localhost:3001
45
+ ```
46
+
47
+ ### Bun
48
+
49
+ ```typescript
50
+ import { MCPWebBridgeBun } from '@mcp-web/bridge';
51
+
52
+ const bridge = new MCPWebBridgeBun({
53
+ name: 'My App',
54
+ description: 'My awesome application',
55
+ port: 3001,
56
+ });
57
+ ```
58
+
59
+ ### PartyKit / Cloudflare
60
+
61
+ ```typescript
62
+ // server.ts
63
+ import { createPartyKitBridge } from '@mcp-web/bridge';
64
+
65
+ export default createPartyKitBridge({
66
+ name: 'My App',
67
+ description: 'My awesome application on the edge',
68
+ });
69
+ ```
70
+
71
+ ## Runtime Adapters
72
+
73
+ The bridge supports multiple JavaScript runtimes through adapters. Each adapter wraps the runtime-agnostic core and provides I/O specific to that runtime.
74
+
75
+ ### MCPWebBridgeNode (Node.js)
76
+
77
+ **Status:** Production Ready ✅
78
+
79
+ The Node.js adapter uses `http.createServer()` with the `ws` library for WebSocket support.
80
+
81
+ ```typescript
82
+ import { MCPWebBridgeNode } from '@mcp-web/bridge';
83
+
84
+ const bridge = new MCPWebBridgeNode({
85
+ name: 'My App',
86
+ description: 'My application',
87
+ port: 3001, // Single port for HTTP + WebSocket
88
+ host: '0.0.0.0', // Optional: bind address
89
+ });
90
+
91
+ // Access the core for advanced usage
92
+ const handlers = bridge.getHandlers();
93
+
94
+ // Graceful shutdown
95
+ await bridge.close();
96
+ ```
97
+
98
+ **Features:**
99
+ - Single port for both HTTP and WebSocket (uses HTTP upgrade)
100
+ - Full session and query limit support
101
+ - Timer-based scheduler for session timeouts
102
+
103
+ ---
104
+
105
+ ### MCPWebBridgeDeno (Deno / Deno Deploy)
106
+
107
+ **Status:** Experimental 🧪
108
+
109
+ The Deno adapter uses `Deno.serve()` with native WebSocket upgrade support.
110
+
111
+ ```typescript
112
+ import { MCPWebBridgeDeno } from '@mcp-web/bridge';
113
+
114
+ const bridge = new MCPWebBridgeDeno({
115
+ name: 'My App',
116
+ description: 'My application',
117
+ port: 3001,
118
+ hostname: '0.0.0.0', // Optional
119
+ });
120
+ ```
121
+
122
+ **Deno Deploy:**
123
+
124
+ ```typescript
125
+ // main.ts - Entry point for Deno Deploy
126
+ import { MCPWebBridgeDeno } from '@mcp-web/bridge';
127
+
128
+ new MCPWebBridgeDeno({
129
+ name: 'Production Bridge',
130
+ description: 'MCP Web bridge on Deno Deploy',
131
+ port: Number(Deno.env.get('PORT')) || 8000,
132
+ });
133
+ ```
134
+
135
+ **Features:**
136
+ - Native Deno.serve() with WebSocket upgrade
137
+ - Works with Deno Deploy
138
+ - Timer-based scheduler
139
+
140
+ **Limitations:**
141
+ - Deno Deploy doesn't support monorepo subdirectories for entry points
142
+ - Consider using a separate repo or root-level entry point for deployment
143
+
144
+ ---
145
+
146
+ ### MCPWebBridgeBun (Bun)
147
+
148
+ **Status:** Experimental 🧪
149
+
150
+ The Bun adapter uses `Bun.serve()` with native WebSocket support.
151
+
152
+ ```typescript
153
+ import { MCPWebBridgeBun } from '@mcp-web/bridge';
154
+
155
+ const bridge = new MCPWebBridgeBun({
156
+ name: 'My App',
157
+ description: 'My application',
158
+ port: 3001,
159
+ hostname: '0.0.0.0', // Optional
160
+ });
161
+ ```
162
+
163
+ **Features:**
164
+ - Native Bun.serve() with built-in WebSocket
165
+ - Excellent performance
166
+ - Timer-based scheduler
167
+
168
+ ---
169
+
170
+ ### MCPWebBridgeParty (PartyKit / Cloudflare)
171
+
172
+ **Status:** Experimental 🧪
173
+
174
+ The PartyKit adapter enables deployment to Cloudflare's edge network with Durable Objects for state management.
175
+
176
+ ```typescript
177
+ // server.ts
178
+ import { createPartyKitBridge } from '@mcp-web/bridge';
179
+
180
+ export default createPartyKitBridge({
181
+ name: 'My App',
182
+ description: 'My application on the edge',
183
+ sessionCheckIntervalMs: 60000, // Optional: alarm interval
184
+ });
185
+ ```
186
+
187
+ **partykit.json:**
188
+ ```json
189
+ {
190
+ "name": "my-mcp-bridge",
191
+ "main": "server.ts",
192
+ "compatibility_date": "2024-01-01"
193
+ }
194
+ ```
195
+
196
+ **Deploy:**
197
+ ```bash
198
+ npx partykit deploy
199
+ ```
200
+
201
+ **Features:**
202
+ - Global edge deployment via Cloudflare
203
+ - Durable Objects for stateful WebSocket handling
204
+ - Hibernation support for cost efficiency
205
+ - Uses PartyKit Alarms instead of setInterval for session timeouts
206
+
207
+ **Key Differences:**
208
+ - No explicit port configuration (managed by PartyKit/Cloudflare)
209
+ - Uses `AlarmScheduler` instead of `TimerScheduler`
210
+ - State persists across hibernation cycles via `Party.storage`
211
+
212
+ ---
213
+
214
+ ## Custom Adapters
215
+
216
+ You can create custom adapters for other runtimes by using the core `MCPWebBridge` class directly:
217
+
218
+ ```typescript
219
+ import { MCPWebBridge, TimerScheduler } from '@mcp-web/bridge';
220
+ import type { BridgeHandlers, WebSocketConnection, HttpRequest, HttpResponse } from '@mcp-web/bridge';
221
+
222
+ // Create the core with a scheduler
223
+ const scheduler = new TimerScheduler();
224
+ const core = new MCPWebBridge(config, scheduler);
225
+
226
+ // Get handlers to wire up to your runtime
227
+ const handlers: BridgeHandlers = core.getHandlers();
228
+
229
+ // Implement these in your adapter:
230
+ // - handlers.onWebSocketConnect(sessionId, ws, url)
231
+ // - handlers.onWebSocketMessage(sessionId, ws, data)
232
+ // - handlers.onWebSocketClose(sessionId)
233
+ // - handlers.onHttpRequest(req) -> Promise<HttpResponse>
234
+ ```
235
+
236
+ ## Configuration Options
237
+
238
+ All adapters accept these common options from `MCPWebConfig`:
239
+
240
+ | Option | Type | Default | Description |
241
+ |--------|------|---------|-------------|
242
+ | `name` | `string` | **required** | Server name displayed to clients |
243
+ | `description` | `string` | **required** | Server description |
244
+ | `icon` | `string` | `undefined` | Optional icon URL or data URI |
245
+ | `agentUrl` | `string` | `undefined` | URL of the AI agent for query forwarding |
246
+ | `authToken` | `string` | `undefined` | Auth token for agent communication |
247
+ | `maxSessionsPerToken` | `number` | `undefined` | Max sessions per auth token |
248
+ | `onSessionLimitExceeded` | `'reject' \| 'close_oldest'` | `'reject'` | Behavior when limit exceeded |
249
+ | `maxInFlightQueriesPerToken` | `number` | `undefined` | Max concurrent queries per token |
250
+ | `sessionMaxDurationMs` | `number` | `undefined` | Max session duration (ms) |
251
+
252
+ ### Adapter-Specific Options
253
+
254
+ **Node.js / Deno / Bun:**
255
+ | Option | Type | Default | Description |
256
+ |--------|------|---------|-------------|
257
+ | `port` | `number` | `3001` | Port to listen on |
258
+ | `host` / `hostname` | `string` | `'0.0.0.0'` | Hostname to bind to |
259
+
260
+ **PartyKit:**
261
+ | Option | Type | Default | Description |
262
+ |--------|------|---------|-------------|
263
+ | `sessionCheckIntervalMs` | `number` | `60000` | Alarm interval for session checks |
264
+
265
+ ## Architecture
266
+
267
+ ```
268
+ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
269
+ │ AI Agent │────▶│ Bridge Server │◀────│ Web Frontend │
270
+ │ (HTTP/JSON-RPC)│ │ (Single Port) │ │ (WebSocket) │
271
+ └─────────────────┘ └─────────────────┘ └─────────────────┘
272
+
273
+ ┌──────────┴──────────┐
274
+ │ MCPWebBridge │
275
+ │ (Runtime-Agnostic │
276
+ │ Core Logic) │
277
+ └─────────────────────┘
278
+
279
+ ┌───────────────────┼───────────────────┐
280
+ │ │ │
281
+ ┌──────┴──────┐ ┌──────┴──────┐ ┌──────┴──────┐
282
+ │ Node.js │ │ Deno │ │ PartyKit │
283
+ │ Adapter │ │ Adapter │ │ Adapter │
284
+ └─────────────┘ └─────────────┘ └─────────────┘
285
+ ```
286
+
287
+ The bridge server acts as the central hub:
288
+ 1. **Frontend connections**: Web apps connect via WebSocket to register tools and receive tool calls
289
+ 2. **Agent connections**: AI agents connect via HTTP/JSON-RPC to discover and execute tools
290
+ 3. **Session management**: Each frontend gets a unique session with authentication
291
+ 4. **Tool routing**: Tool calls from agents are routed to the appropriate frontend session
292
+
293
+ ## Development
294
+
295
+ Build the package:
296
+ ```bash
297
+ pnpm build
298
+ ```
299
+
300
+ Run tests:
301
+ ```bash
302
+ pnpm test
303
+ ```
304
+
305
+ ## Next Steps
306
+
307
+ Once your bridge server is running:
308
+
309
+ 1. **Configure your web app** to connect using [@mcp-web/core](../core/)
310
+ 2. **Set up your AI agent** to connect using [@mcp-web/client](../client/)
311
+ 3. **Register tools** in your web app that the AI agent can call
@@ -0,0 +1,95 @@
1
+ /**
2
+ * MCPWebBridgeBun - Bun adapter for the MCP Web Bridge.
3
+ *
4
+ * Uses Bun.serve() with built-in WebSocket support for a single-port server.
5
+ * Bun's server natively handles both HTTP and WebSocket on the same port.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * // server.ts
10
+ * import { MCPWebBridgeBun } from '@mcp-web/bridge';
11
+ *
12
+ * const bridge = new MCPWebBridgeBun({
13
+ * name: 'My App',
14
+ * description: 'My awesome app',
15
+ * port: 3001,
16
+ * });
17
+ *
18
+ * // Bridge is now listening on ws://localhost:3001 and http://localhost:3001
19
+ * ```
20
+ *
21
+ * @example Production deployment
22
+ * ```typescript
23
+ * import { MCPWebBridgeBun } from '@mcp-web/bridge';
24
+ *
25
+ * const bridge = new MCPWebBridgeBun({
26
+ * name: 'Production Bridge',
27
+ * description: 'Production MCP Web bridge server',
28
+ * port: Number(process.env.PORT) || 3001,
29
+ * hostname: '0.0.0.0',
30
+ * });
31
+ *
32
+ * // Graceful shutdown
33
+ * process.on('SIGTERM', async () => {
34
+ * await bridge.close();
35
+ * process.exit(0);
36
+ * });
37
+ * ```
38
+ *
39
+ * @remarks
40
+ * This adapter requires Bun runtime (https://bun.sh).
41
+ * Bun's `Bun.serve()` provides excellent performance with native WebSocket support.
42
+ *
43
+ * Key features:
44
+ * - Single port for HTTP and WebSocket
45
+ * - Native TypeScript support
46
+ * - High performance HTTP and WebSocket handling
47
+ *
48
+ * @see https://bun.sh/docs/api/http
49
+ * @see https://bun.sh/docs/api/websockets
50
+ */
51
+ import type { MCPWebConfig } from '@mcp-web/types';
52
+ import { MCPWebBridge } from '../core.js';
53
+ /**
54
+ * Configuration for the Bun bridge adapter.
55
+ */
56
+ export interface MCPWebBridgeBunConfig extends Omit<MCPWebConfig, 'bridgeUrl'> {
57
+ /** Port to listen on (default: 3001) */
58
+ port?: number;
59
+ /** Hostname to bind to (default: '0.0.0.0') */
60
+ hostname?: string;
61
+ }
62
+ /**
63
+ * Bun adapter for MCPWebBridge.
64
+ * Provides a single-port server using Bun.serve() with native WebSocket support.
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * const bridge = new MCPWebBridgeBun({
69
+ * name: 'My App',
70
+ * description: 'My app',
71
+ * port: 3001,
72
+ * });
73
+ * ```
74
+ */
75
+ export declare class MCPWebBridgeBun {
76
+ #private;
77
+ constructor(config: MCPWebBridgeBunConfig);
78
+ /**
79
+ * Get the underlying MCPWebBridge core instance.
80
+ */
81
+ get core(): MCPWebBridge;
82
+ /**
83
+ * Get the bridge handlers for custom integrations.
84
+ */
85
+ getHandlers(): import("../runtime/types.js").BridgeHandlers;
86
+ /**
87
+ * Get the port the server is listening on.
88
+ */
89
+ get port(): number;
90
+ /**
91
+ * Gracefully shut down the bridge.
92
+ */
93
+ close(): Promise<void>;
94
+ }
95
+ //# sourceMappingURL=bun.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bun.d.ts","sourceRoot":"","sources":["../../src/adapters/bun.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAK1C;;GAEG;AACH,MAAM,WAAW,qBAAsB,SAAQ,IAAI,CAAC,YAAY,EAAE,WAAW,CAAC;IAC5E,wCAAwC;IACxC,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd,+CAA+C;IAC/C,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AA4FD;;;;;;;;;;;;GAYG;AACH,qBAAa,eAAe;;gBAOd,MAAM,EAAE,qBAAqB;IAgJzC;;OAEG;IACH,IAAI,IAAI,IAAI,YAAY,CAEvB;IAED;;OAEG;IACH,WAAW;IAIX;;OAEG;IACH,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAO7B"}