@mcp-b/transports 1.0.3 → 1.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/README.md +129 -0
- package/dist/index.d.ts +539 -430
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -106,6 +106,135 @@ const result = await client.callTool({
|
|
|
106
106
|
});
|
|
107
107
|
```
|
|
108
108
|
|
|
109
|
+
### Tab Transport Configuration
|
|
110
|
+
|
|
111
|
+
#### TabClientTransport Options
|
|
112
|
+
|
|
113
|
+
- `targetOrigin` (required): Origin expected from the server window (for security).
|
|
114
|
+
- `channelId`: Override the default channel identifier (default: `mcp-default`).
|
|
115
|
+
|
|
116
|
+
#### TabServerTransport Options
|
|
117
|
+
|
|
118
|
+
- `allowedOrigins` (required): Whitelist of origins allowed to connect (for security).
|
|
119
|
+
- `channelId`: Override the default channel identifier (default: `mcp-default`).
|
|
120
|
+
|
|
121
|
+
## Iframe Transports (Parent-Child Communication)
|
|
122
|
+
|
|
123
|
+
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.
|
|
124
|
+
|
|
125
|
+
### Iframe Server Setup (Inside Iframe)
|
|
126
|
+
|
|
127
|
+
Create an MCP server inside an iframe that can be accessed by the parent page:
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
import { IframeChildTransport } from "@mcp-b/transports";
|
|
131
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
132
|
+
import { z } from "zod";
|
|
133
|
+
|
|
134
|
+
// Create MCP server
|
|
135
|
+
const server = new Server(
|
|
136
|
+
{
|
|
137
|
+
name: "IframeApp",
|
|
138
|
+
version: "1.0.0",
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
capabilities: {
|
|
142
|
+
tools: {},
|
|
143
|
+
},
|
|
144
|
+
}
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
// Register tools
|
|
148
|
+
server.tool(
|
|
149
|
+
"getIframeData",
|
|
150
|
+
"Get data from the iframe application",
|
|
151
|
+
{
|
|
152
|
+
key: z.string().describe("Data key to retrieve"),
|
|
153
|
+
},
|
|
154
|
+
async (args) => {
|
|
155
|
+
return {
|
|
156
|
+
content: [
|
|
157
|
+
{
|
|
158
|
+
type: "text",
|
|
159
|
+
text: `Retrieved: ${args.key}`,
|
|
160
|
+
},
|
|
161
|
+
],
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
// Connect to iframe transport
|
|
167
|
+
const transport = new IframeChildTransport({
|
|
168
|
+
allowedOrigins: ["https://parent-app.com"], // Parent page origin
|
|
169
|
+
// or use ['*'] to allow any origin (less secure)
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
await server.connect(transport);
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Iframe Client Setup (Parent Page)
|
|
176
|
+
|
|
177
|
+
Connect from the parent page to the iframe's MCP server:
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
import { IframeParentTransport } from "@mcp-b/transports";
|
|
181
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
182
|
+
|
|
183
|
+
// Get reference to iframe element
|
|
184
|
+
const iframe = document.querySelector('iframe');
|
|
185
|
+
|
|
186
|
+
// Wait for iframe to load
|
|
187
|
+
iframe.addEventListener('load', async () => {
|
|
188
|
+
// Create transport targeting the iframe
|
|
189
|
+
const transport = new IframeParentTransport({
|
|
190
|
+
iframe: iframe,
|
|
191
|
+
targetOrigin: 'https://iframe-app.com', // Iframe page origin
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// Create MCP client
|
|
195
|
+
const client = new Client({
|
|
196
|
+
name: "ParentPage",
|
|
197
|
+
version: "1.0.0",
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
// Connect and use
|
|
201
|
+
await client.connect(transport);
|
|
202
|
+
|
|
203
|
+
// List available tools from iframe
|
|
204
|
+
const tools = await client.listTools();
|
|
205
|
+
console.log("Tools from iframe:", tools.tools);
|
|
206
|
+
|
|
207
|
+
// Call a tool from the iframe
|
|
208
|
+
const result = await client.callTool({
|
|
209
|
+
name: "getIframeData",
|
|
210
|
+
arguments: { key: "user-preferences" },
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### Iframe Transport Configuration
|
|
216
|
+
|
|
217
|
+
#### IframeParentTransport Options
|
|
218
|
+
|
|
219
|
+
- `iframe` (required): Reference to the HTMLIFrameElement
|
|
220
|
+
- `targetOrigin` (required): Expected origin of the iframe (for security)
|
|
221
|
+
- `channelId`: Override the default channel identifier (default: `mcp-iframe`)
|
|
222
|
+
- `checkReadyRetryMs`: Interval to retry the ready handshake if iframe isn't ready yet (default: 250ms)
|
|
223
|
+
|
|
224
|
+
#### IframeChildTransport Options
|
|
225
|
+
|
|
226
|
+
- `allowedOrigins` (required): Whitelist of parent origins allowed to connect (for security)
|
|
227
|
+
- `channelId`: Override the default channel identifier (default: `mcp-iframe`)
|
|
228
|
+
- `serverReadyRetryMs`: Interval to retry broadcasting ready signal to parent (default: 250ms)
|
|
229
|
+
|
|
230
|
+
### Cross-Origin Support
|
|
231
|
+
|
|
232
|
+
Iframe transports are designed for cross-origin communication:
|
|
233
|
+
- Parent and iframe can be on different domains
|
|
234
|
+
- Origin validation is performed on both sides
|
|
235
|
+
- Uses secure `postMessage` API
|
|
236
|
+
- Retry mechanisms handle iframe loading timing issues
|
|
237
|
+
|
|
109
238
|
## Extension Transport Examples
|
|
110
239
|
|
|
111
240
|
### Background Script Setup
|