@mcp-b/transports 0.0.0-beta-20260109203913
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 +21 -0
- package/README.md +579 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/index.d.ts +937 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +92 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 mcp-b
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,579 @@
|
|
|
1
|
+
# @mcp-b/transports
|
|
2
|
+
|
|
3
|
+
> Browser transport implementations for Model Context Protocol (MCP) - Connect AI agents like Claude, ChatGPT, and Gemini to web applications
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@mcp-b/transports)
|
|
6
|
+
[](https://www.npmjs.com/package/@mcp-b/transports)
|
|
7
|
+
[](https://opensource.org/licenses/MIT)
|
|
8
|
+
[](https://www.typescriptlang.org/)
|
|
9
|
+
[](https://bundlephobia.com/package/@mcp-b/transports)
|
|
10
|
+
|
|
11
|
+
📖 **[Full Documentation](https://docs.mcp-b.ai/packages/transports)** | 🚀 **[Quick Start](https://docs.mcp-b.ai/quickstart)** | 🔌 **[Transport Concepts](https://docs.mcp-b.ai/concepts/transports)**
|
|
12
|
+
|
|
13
|
+
**@mcp-b/transports** provides browser-native MCP transport implementations that enable AI agents and LLMs to communicate with web applications. Whether you're building Chrome extensions, embedded iframes, or in-page AI integrations, this library provides the secure messaging layer you need.
|
|
14
|
+
|
|
15
|
+
## Why Use @mcp-b/transports?
|
|
16
|
+
|
|
17
|
+
| Feature | Benefit |
|
|
18
|
+
|---------|---------|
|
|
19
|
+
| **Multiple Transport Types** | Choose the right transport for your architecture - Tab, Iframe, or Extension |
|
|
20
|
+
| **Secure by Default** | Origin validation, CORS configuration, and Chrome's secure messaging APIs |
|
|
21
|
+
| **AI-Agent Ready** | Built specifically for MCP, the standard protocol for AI tool integration |
|
|
22
|
+
| **TypeScript First** | Full type safety with comprehensive type definitions |
|
|
23
|
+
| **Zero Dependencies** | Only requires `zod` for schema validation |
|
|
24
|
+
| **Works Everywhere** | Chrome, Firefox, Edge, and any Chromium-based browser |
|
|
25
|
+
|
|
26
|
+
## Use Cases
|
|
27
|
+
|
|
28
|
+
- **Browser Extensions**: Build AI-powered extensions that interact with any webpage
|
|
29
|
+
- **Embedded AI Widgets**: Add AI capabilities to existing web applications via iframes
|
|
30
|
+
- **In-Page AI Assistants**: Create AI tools that work directly within your web app
|
|
31
|
+
- **Cross-Extension Communication**: Let extensions share AI tools with each other
|
|
32
|
+
- **Enterprise AI Portals**: Connect internal AI agents to web-based tools
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm install @mcp-b/transports
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Transport Types
|
|
41
|
+
|
|
42
|
+
### Tab Transports (In-Page Communication)
|
|
43
|
+
|
|
44
|
+
Use `TabServerTransport` and `TabClientTransport` when your MCP server and client are running in the same browser tab. The transport uses `window.postMessage` for secure communication with origin validation.
|
|
45
|
+
|
|
46
|
+
### Extension Transports (Cross-Context Communication)
|
|
47
|
+
|
|
48
|
+
Use `ExtensionClientTransport` and `ExtensionServerTransport` for communication between browser extension components (sidebar, popup, background) and web pages with MCP servers.
|
|
49
|
+
|
|
50
|
+
### Extension External Transports (Cross-Extension Communication)
|
|
51
|
+
|
|
52
|
+
Use `ExtensionExternalClientTransport` and `ExtensionExternalServerTransport` for communication between different browser extensions, enabling one extension to access MCP servers hosted by another extension.
|
|
53
|
+
|
|
54
|
+
## Tab Transport Examples
|
|
55
|
+
|
|
56
|
+
### Server Setup (Web Page)
|
|
57
|
+
|
|
58
|
+
Create an MCP server in your web page and expose it via `TabServerTransport`:
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import { TabServerTransport } from "@mcp-b/transports";
|
|
62
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
63
|
+
import { z } from "zod";
|
|
64
|
+
|
|
65
|
+
// Create MCP server with tools
|
|
66
|
+
const server = new McpServer(
|
|
67
|
+
{
|
|
68
|
+
name: "TODO-APP",
|
|
69
|
+
version: "1.0.0",
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
instructions:
|
|
73
|
+
"You are a helpful assistant that can create, update, and delete todos.",
|
|
74
|
+
}
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
// Register a tool
|
|
78
|
+
server.tool(
|
|
79
|
+
"createTodo",
|
|
80
|
+
"Creates a new todo item for the current user",
|
|
81
|
+
{
|
|
82
|
+
todoText: z.string().describe("The content of the todo item."),
|
|
83
|
+
},
|
|
84
|
+
async (args) => {
|
|
85
|
+
// Implementation here
|
|
86
|
+
return {
|
|
87
|
+
content: [
|
|
88
|
+
{
|
|
89
|
+
type: "text",
|
|
90
|
+
text: `Todo created: "${args.todoText}"`,
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
// Connect to transport with CORS configuration
|
|
98
|
+
const transport = new TabServerTransport({
|
|
99
|
+
allowedOrigins: ["*"], // Configure based on your security needs
|
|
100
|
+
});
|
|
101
|
+
await server.connect(transport);
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Client Setup (Same Page)
|
|
105
|
+
|
|
106
|
+
Connect to the server from within the same page or from an extension content script:
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
import { TabClientTransport } from "@mcp-b/transports";
|
|
110
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
111
|
+
|
|
112
|
+
// Create transport with target origin
|
|
113
|
+
const transport = new TabClientTransport({
|
|
114
|
+
targetOrigin: window.location.origin,
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
// Discover available servers
|
|
118
|
+
const availableServers = await transport.discover();
|
|
119
|
+
if (availableServers.length > 0) {
|
|
120
|
+
console.log(`Found server: ${availableServers[0].implementation.name}`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Create and connect client
|
|
124
|
+
const client = new Client({
|
|
125
|
+
name: "ExtensionProxyClient",
|
|
126
|
+
version: "1.0.0",
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
await client.connect(transport);
|
|
130
|
+
|
|
131
|
+
// Use the client
|
|
132
|
+
const result = await client.callTool({
|
|
133
|
+
name: "createTodo",
|
|
134
|
+
arguments: { todoText: "Buy groceries" },
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Tab Transport Configuration
|
|
139
|
+
|
|
140
|
+
#### TabClientTransport Options
|
|
141
|
+
|
|
142
|
+
- `targetOrigin` (required): Origin expected from the server window (for security).
|
|
143
|
+
- `channelId`: Override the default channel identifier (default: `mcp-default`).
|
|
144
|
+
|
|
145
|
+
#### TabServerTransport Options
|
|
146
|
+
|
|
147
|
+
- `allowedOrigins` (required): Whitelist of origins allowed to connect (for security).
|
|
148
|
+
- `channelId`: Override the default channel identifier (default: `mcp-default`).
|
|
149
|
+
|
|
150
|
+
## Iframe Transports (Parent-Child Communication)
|
|
151
|
+
|
|
152
|
+
Use `IframeParentTransport` and `IframeChildTransport` for cross-origin communication between a parent page and an iframe. These transports are specifically designed for iframe scenarios and support cross-origin messaging.
|
|
153
|
+
|
|
154
|
+
### Iframe Server Setup (Inside Iframe)
|
|
155
|
+
|
|
156
|
+
Create an MCP server inside an iframe that can be accessed by the parent page:
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
import { IframeChildTransport } from "@mcp-b/transports";
|
|
160
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
161
|
+
import { z } from "zod";
|
|
162
|
+
|
|
163
|
+
// Create MCP server
|
|
164
|
+
const server = new Server(
|
|
165
|
+
{
|
|
166
|
+
name: "IframeApp",
|
|
167
|
+
version: "1.0.0",
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
capabilities: {
|
|
171
|
+
tools: {},
|
|
172
|
+
},
|
|
173
|
+
}
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
// Register tools
|
|
177
|
+
server.tool(
|
|
178
|
+
"getIframeData",
|
|
179
|
+
"Get data from the iframe application",
|
|
180
|
+
{
|
|
181
|
+
key: z.string().describe("Data key to retrieve"),
|
|
182
|
+
},
|
|
183
|
+
async (args) => {
|
|
184
|
+
return {
|
|
185
|
+
content: [
|
|
186
|
+
{
|
|
187
|
+
type: "text",
|
|
188
|
+
text: `Retrieved: ${args.key}`,
|
|
189
|
+
},
|
|
190
|
+
],
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
// Connect to iframe transport
|
|
196
|
+
const transport = new IframeChildTransport({
|
|
197
|
+
allowedOrigins: ["https://parent-app.com"], // Parent page origin
|
|
198
|
+
// or use ['*'] to allow any origin (less secure)
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
await server.connect(transport);
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### Iframe Client Setup (Parent Page)
|
|
205
|
+
|
|
206
|
+
Connect from the parent page to the iframe's MCP server:
|
|
207
|
+
|
|
208
|
+
```typescript
|
|
209
|
+
import { IframeParentTransport } from "@mcp-b/transports";
|
|
210
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
211
|
+
|
|
212
|
+
// Get reference to iframe element
|
|
213
|
+
const iframe = document.querySelector('iframe');
|
|
214
|
+
|
|
215
|
+
// Wait for iframe to load
|
|
216
|
+
iframe.addEventListener('load', async () => {
|
|
217
|
+
// Create transport targeting the iframe
|
|
218
|
+
const transport = new IframeParentTransport({
|
|
219
|
+
iframe: iframe,
|
|
220
|
+
targetOrigin: 'https://iframe-app.com', // Iframe page origin
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
// Create MCP client
|
|
224
|
+
const client = new Client({
|
|
225
|
+
name: "ParentPage",
|
|
226
|
+
version: "1.0.0",
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// Connect and use
|
|
230
|
+
await client.connect(transport);
|
|
231
|
+
|
|
232
|
+
// List available tools from iframe
|
|
233
|
+
const tools = await client.listTools();
|
|
234
|
+
console.log("Tools from iframe:", tools.tools);
|
|
235
|
+
|
|
236
|
+
// Call a tool from the iframe
|
|
237
|
+
const result = await client.callTool({
|
|
238
|
+
name: "getIframeData",
|
|
239
|
+
arguments: { key: "user-preferences" },
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### Iframe Transport Configuration
|
|
245
|
+
|
|
246
|
+
#### IframeParentTransport Options
|
|
247
|
+
|
|
248
|
+
- `iframe` (required): Reference to the HTMLIFrameElement
|
|
249
|
+
- `targetOrigin` (required): Expected origin of the iframe (for security)
|
|
250
|
+
- `channelId`: Override the default channel identifier (default: `mcp-iframe`)
|
|
251
|
+
- `checkReadyRetryMs`: Interval to retry the ready handshake if iframe isn't ready yet (default: 250ms)
|
|
252
|
+
|
|
253
|
+
#### IframeChildTransport Options
|
|
254
|
+
|
|
255
|
+
- `allowedOrigins` (required): Whitelist of parent origins allowed to connect (for security)
|
|
256
|
+
- `channelId`: Override the default channel identifier (default: `mcp-iframe`)
|
|
257
|
+
- `serverReadyRetryMs`: Interval to retry broadcasting ready signal to parent (default: 250ms)
|
|
258
|
+
|
|
259
|
+
### Cross-Origin Support
|
|
260
|
+
|
|
261
|
+
Iframe transports are designed for cross-origin communication:
|
|
262
|
+
- Parent and iframe can be on different domains
|
|
263
|
+
- Origin validation is performed on both sides
|
|
264
|
+
- Uses secure `postMessage` API
|
|
265
|
+
- Retry mechanisms handle iframe loading timing issues
|
|
266
|
+
|
|
267
|
+
## Extension Transport Examples
|
|
268
|
+
|
|
269
|
+
### Background Script Setup
|
|
270
|
+
|
|
271
|
+
The extension background script acts as a hub, aggregating tools from multiple tabs:
|
|
272
|
+
|
|
273
|
+
```typescript
|
|
274
|
+
import { ExtensionServerTransport } from "@mcp-b/transports";
|
|
275
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
276
|
+
|
|
277
|
+
class McpHub {
|
|
278
|
+
private server: McpServer;
|
|
279
|
+
|
|
280
|
+
constructor() {
|
|
281
|
+
this.server = new McpServer({
|
|
282
|
+
name: "Extension-Hub",
|
|
283
|
+
version: "1.0.0",
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
this.setupConnections();
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
private setupConnections() {
|
|
290
|
+
chrome.runtime.onConnect.addListener((port) => {
|
|
291
|
+
if (port.name === "mcp") {
|
|
292
|
+
this.handleUiConnection(port);
|
|
293
|
+
} else if (port.name === "mcp-content-script-proxy") {
|
|
294
|
+
this.handleContentScriptConnection(port);
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
private async handleUiConnection(port: chrome.runtime.Port) {
|
|
300
|
+
const transport = new ExtensionServerTransport(port);
|
|
301
|
+
await this.server.connect(transport);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
### Content Script Bridge
|
|
307
|
+
|
|
308
|
+
Content scripts act as a bridge between the page's MCP server and the extension:
|
|
309
|
+
|
|
310
|
+
```typescript
|
|
311
|
+
import { TabClientTransport } from "@mcp-b/transports";
|
|
312
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
313
|
+
|
|
314
|
+
// Connect to the page's MCP server
|
|
315
|
+
const transport = new TabClientTransport({
|
|
316
|
+
targetOrigin: window.location.origin,
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
const client = new Client({
|
|
320
|
+
name: "ExtensionProxyClient",
|
|
321
|
+
version: "1.0.0",
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
// Connect to extension background
|
|
325
|
+
const backgroundPort = chrome.runtime.connect({
|
|
326
|
+
name: "mcp-content-script-proxy",
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
// Discover and connect to page server
|
|
330
|
+
await client.connect(transport);
|
|
331
|
+
const pageTools = await client.listTools();
|
|
332
|
+
|
|
333
|
+
// Register tools with background hub
|
|
334
|
+
backgroundPort.postMessage({
|
|
335
|
+
type: "register-tools",
|
|
336
|
+
tools: pageTools.tools,
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
// Handle tool execution requests from background
|
|
340
|
+
backgroundPort.onMessage.addListener(async (message) => {
|
|
341
|
+
if (message.type === "execute-tool") {
|
|
342
|
+
const result = await client.callTool({
|
|
343
|
+
name: message.toolName,
|
|
344
|
+
arguments: message.args || {},
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
backgroundPort.postMessage({
|
|
348
|
+
type: "tool-result",
|
|
349
|
+
requestId: message.requestId,
|
|
350
|
+
data: { success: true, payload: result },
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
### Extension UI Client
|
|
357
|
+
|
|
358
|
+
Connect from the extension's sidebar or popup to use tools from all connected pages:
|
|
359
|
+
|
|
360
|
+
```typescript
|
|
361
|
+
import { ExtensionClientTransport } from "@mcp-b/transports";
|
|
362
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
363
|
+
|
|
364
|
+
// Create transport - connects to the extension's background script
|
|
365
|
+
const transport = new ExtensionClientTransport({
|
|
366
|
+
portName: "mcp",
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
// Create MCP client
|
|
370
|
+
const client = new Client({
|
|
371
|
+
name: "Extension Sidepanel",
|
|
372
|
+
version: "1.0.0",
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
// Connect and use
|
|
376
|
+
await client.connect(transport);
|
|
377
|
+
|
|
378
|
+
// List all available tools from all connected tabs
|
|
379
|
+
const tools = await client.listTools();
|
|
380
|
+
|
|
381
|
+
// Call a tool from a specific website
|
|
382
|
+
const result = await client.callTool({
|
|
383
|
+
name: "website_tool_example_com_createTodo",
|
|
384
|
+
arguments: { todoText: "Review PR" },
|
|
385
|
+
});
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
## Extension External Transport Examples
|
|
389
|
+
|
|
390
|
+
### Server Extension Setup (Extension 1)
|
|
391
|
+
|
|
392
|
+
Create an MCP server in Extension 1 that can be accessed by other extensions:
|
|
393
|
+
|
|
394
|
+
```typescript
|
|
395
|
+
import { ExtensionExternalServerTransport } from "@mcp-b/transports";
|
|
396
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
397
|
+
import { z } from "zod";
|
|
398
|
+
|
|
399
|
+
// Create MCP server with tools
|
|
400
|
+
const server = new McpServer(
|
|
401
|
+
{
|
|
402
|
+
name: "MyExtensionAPI",
|
|
403
|
+
version: "1.0.0",
|
|
404
|
+
},
|
|
405
|
+
{
|
|
406
|
+
instructions: "Extension API for cross-extension communication",
|
|
407
|
+
}
|
|
408
|
+
);
|
|
409
|
+
|
|
410
|
+
// Register tools
|
|
411
|
+
server.tool("getBookmarks", "Retrieves user bookmarks", {}, async () => {
|
|
412
|
+
const bookmarks = await chrome.bookmarks.getTree();
|
|
413
|
+
return {
|
|
414
|
+
content: [
|
|
415
|
+
{
|
|
416
|
+
type: "text",
|
|
417
|
+
text: JSON.stringify(bookmarks, null, 2),
|
|
418
|
+
},
|
|
419
|
+
],
|
|
420
|
+
};
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
// Set up external connection listener in background script
|
|
424
|
+
chrome.runtime.onConnectExternal.addListener(async (port) => {
|
|
425
|
+
if (port.name === "mcp") {
|
|
426
|
+
// Optional: Add connection validation here
|
|
427
|
+
if (port.sender?.id !== "allowed-extension-id") {
|
|
428
|
+
port.disconnect();
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const transport = new ExtensionServerTransport(port);
|
|
433
|
+
await server.connect(transport);
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
### Client Extension Setup (Extension 2)
|
|
439
|
+
|
|
440
|
+
Connect from Extension 2 to use Extension 1's MCP server:
|
|
441
|
+
|
|
442
|
+
```typescript
|
|
443
|
+
import { ExtensionExternalClientTransport } from "@mcp-b/transports";
|
|
444
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
445
|
+
|
|
446
|
+
// Create transport targeting Extension 1
|
|
447
|
+
const transport = new ExtensionClientTransport({
|
|
448
|
+
extensionId: "server-extension-id",
|
|
449
|
+
portName: "mcp",
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
// Create MCP client
|
|
453
|
+
const client = new Client({
|
|
454
|
+
name: "ClientExtension",
|
|
455
|
+
version: "1.0.0",
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
// Connect and use
|
|
459
|
+
await client.connect(transport);
|
|
460
|
+
|
|
461
|
+
// List available tools from Extension 1
|
|
462
|
+
const tools = await client.listTools();
|
|
463
|
+
console.log("Available tools:", tools.tools);
|
|
464
|
+
|
|
465
|
+
// Call a tool from Extension 1
|
|
466
|
+
const result = await client.callTool({
|
|
467
|
+
name: "getBookmarks",
|
|
468
|
+
arguments: {},
|
|
469
|
+
});
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
### Extension External Transport Features
|
|
473
|
+
|
|
474
|
+
- **Cross-Extension Access**: Extensions can expose MCP servers to other extensions
|
|
475
|
+
- **Secure Communication**: Uses Chrome's `runtime.onConnectExternal` API
|
|
476
|
+
- **Connection Validation**: Server extension can validate incoming connections
|
|
477
|
+
- **Single Client**: Each server transport handles one client connection at a time
|
|
478
|
+
|
|
479
|
+
## Architecture Overview
|
|
480
|
+
|
|
481
|
+
### Tab Transport Flow
|
|
482
|
+
|
|
483
|
+
1. Server creates `TabServerTransport` and listens for messages via `window.postMessage`
|
|
484
|
+
2. Client creates `TabClientTransport` to connect to the server using the same channel
|
|
485
|
+
3. Communication happens securely with origin validation and message direction tracking
|
|
486
|
+
|
|
487
|
+
### Extension Transport Flow
|
|
488
|
+
|
|
489
|
+
1. Web pages run MCP servers with `TabServerTransport`
|
|
490
|
+
2. Content scripts discover these servers and relay to background script
|
|
491
|
+
3. Background script aggregates tools from all tabs using `ExtensionServerTransport`
|
|
492
|
+
4. Extension UI connects via `ExtensionClientTransport` to access all tools
|
|
493
|
+
|
|
494
|
+
### Extension External Transport Flow
|
|
495
|
+
|
|
496
|
+
1. Extension 1 creates `ExtensionExternalServerTransport` and listens on `chrome.runtime.onConnectExternal`
|
|
497
|
+
2. Extension 2 creates `ExtensionExternalClientTransport` targeting Extension 1's ID
|
|
498
|
+
3. Communication happens through Chrome's secure cross-extension messaging
|
|
499
|
+
4. Server extension can validate connections and control access
|
|
500
|
+
|
|
501
|
+
## Key Features
|
|
502
|
+
|
|
503
|
+
- **Automatic Server Discovery**: Tab clients can discover available servers
|
|
504
|
+
- **Cross-Origin Support**: Configure CORS for tab transports
|
|
505
|
+
- **Cross-Extension Communication**: Extensions can expose APIs to other extensions
|
|
506
|
+
- **Tool Namespacing**: Extension hub prefixes tools to avoid conflicts
|
|
507
|
+
- **Connection Management**: Automatic cleanup when tabs close
|
|
508
|
+
- **Type Safety**: Full TypeScript support with proper typing
|
|
509
|
+
|
|
510
|
+
## Security Considerations
|
|
511
|
+
|
|
512
|
+
- Tab transports respect origin restrictions
|
|
513
|
+
- Extension transports use Chrome's secure message passing
|
|
514
|
+
- External extension transports require `externally_connectable` manifest configuration
|
|
515
|
+
- Server extensions should validate incoming connections from other extensions
|
|
516
|
+
- Configure `allowedOrigins` appropriately for your use case
|
|
517
|
+
- Tools execute in their original context (web page or extension)
|
|
518
|
+
|
|
519
|
+
## Frequently Asked Questions
|
|
520
|
+
|
|
521
|
+
### What is MCP (Model Context Protocol)?
|
|
522
|
+
|
|
523
|
+
MCP is an open protocol that standardizes how AI applications connect to external tools and data sources. It's used by AI agents like Claude, ChatGPT, Cursor, and Copilot to interact with external systems. Learn more at [modelcontextprotocol.io](https://modelcontextprotocol.io).
|
|
524
|
+
|
|
525
|
+
### Which transport should I use?
|
|
526
|
+
|
|
527
|
+
| Scenario | Recommended Transport |
|
|
528
|
+
|----------|----------------------|
|
|
529
|
+
| Same-page communication | `TabServerTransport` / `TabClientTransport` |
|
|
530
|
+
| Parent page to iframe | `IframeParentTransport` / `IframeChildTransport` |
|
|
531
|
+
| Browser extension to webpage | `ExtensionClientTransport` / `ExtensionServerTransport` |
|
|
532
|
+
| Extension to extension | `ExtensionExternalClientTransport` / `ExtensionExternalServerTransport` |
|
|
533
|
+
|
|
534
|
+
### Is this compatible with Claude Desktop, Cursor, or VS Code Copilot?
|
|
535
|
+
|
|
536
|
+
Yes! These transports implement the MCP standard, making them compatible with any MCP client. Use `@mcp-b/chrome-devtools-mcp` to connect desktop AI agents to browser-based tools.
|
|
537
|
+
|
|
538
|
+
### How do I handle cross-origin communication?
|
|
539
|
+
|
|
540
|
+
All transports support origin validation. Configure `allowedOrigins` on server transports and `targetOrigin` on client transports to control which origins can communicate.
|
|
541
|
+
|
|
542
|
+
### Can I use this without a browser extension?
|
|
543
|
+
|
|
544
|
+
Yes! `TabServerTransport` and `TabClientTransport` work entirely within a web page using `window.postMessage`. No extension required.
|
|
545
|
+
|
|
546
|
+
## Comparison with Alternatives
|
|
547
|
+
|
|
548
|
+
| Feature | @mcp-b/transports | Raw postMessage | WebSocket |
|
|
549
|
+
|---------|------------------|-----------------|-----------|
|
|
550
|
+
| MCP Protocol Support | Yes | No | Manual |
|
|
551
|
+
| Type Safety | Full TypeScript | Manual | Manual |
|
|
552
|
+
| Origin Validation | Built-in | Manual | N/A |
|
|
553
|
+
| Extension Support | Native | Limited | Complex |
|
|
554
|
+
| Server Discovery | Automatic | Manual | Manual |
|
|
555
|
+
|
|
556
|
+
## Related Packages
|
|
557
|
+
|
|
558
|
+
- [`@mcp-b/global`](https://docs.mcp-b.ai/packages/global) - W3C Web Model Context API polyfill for registering tools
|
|
559
|
+
- [`@mcp-b/react-webmcp`](https://docs.mcp-b.ai/packages/react-webmcp) - React hooks for MCP tool registration
|
|
560
|
+
- [`@mcp-b/extension-tools`](https://docs.mcp-b.ai/packages/extension-tools) - 62+ Chrome Extension API tools for MCP
|
|
561
|
+
- [`@mcp-b/chrome-devtools-mcp`](https://docs.mcp-b.ai/packages/chrome-devtools-mcp) - Connect desktop AI agents to browser tools
|
|
562
|
+
- [`@modelcontextprotocol/sdk`](https://www.npmjs.com/package/@modelcontextprotocol/sdk) - Official MCP SDK
|
|
563
|
+
|
|
564
|
+
## Resources
|
|
565
|
+
|
|
566
|
+
- [WebMCP Documentation](https://docs.mcp-b.ai)
|
|
567
|
+
- [Transport Concepts](https://docs.mcp-b.ai/concepts/transports)
|
|
568
|
+
- [Model Context Protocol Spec](https://modelcontextprotocol.io)
|
|
569
|
+
- [MCP GitHub Repository](https://github.com/modelcontextprotocol)
|
|
570
|
+
|
|
571
|
+
## License
|
|
572
|
+
|
|
573
|
+
MIT - see [LICENSE](../../LICENSE) for details
|
|
574
|
+
|
|
575
|
+
## Support
|
|
576
|
+
|
|
577
|
+
- [GitHub Issues](https://github.com/WebMCP-org/npm-packages/issues)
|
|
578
|
+
- [Documentation](https://docs.mcp-b.ai)
|
|
579
|
+
- [Discord Community](https://discord.gg/a9fBR6Bw)
|