@mcp-b/transports 4.0.0 → 5.0.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 +130 -101
- package/dist/index.d.ts +86 -829
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/package.json +17 -14
- package/dist/index.d.ts.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,353 +1,65 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="chrome" />
|
|
2
|
+
import { JSONRPCMessage, Transport, TransportSendOptions } from "@modelcontextprotocol/server";
|
|
2
3
|
|
|
3
|
-
//#region src/
|
|
4
|
+
//#region src/post-message.d.ts
|
|
4
5
|
/**
|
|
5
|
-
*
|
|
6
|
+
* Channel discriminators. Both ends of a pair must agree or the envelope check in
|
|
7
|
+
* `isMcpMessage` silently drops every message — no error, just a hang.
|
|
6
8
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* Unique identifier for a stream of events
|
|
10
|
-
*/
|
|
11
|
-
type StreamId = string;
|
|
12
|
-
/**
|
|
13
|
-
* Options for connecting to an MCP server
|
|
14
|
-
*/
|
|
15
|
-
interface MCPConnectOptions {
|
|
16
|
-
/**
|
|
17
|
-
* The event ID to resume from if reconnecting
|
|
18
|
-
*/
|
|
19
|
-
resumeFrom?: EventId;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Information about the MCP server
|
|
23
|
-
*/
|
|
24
|
-
interface MCPServerInfo {
|
|
25
|
-
/**
|
|
26
|
-
* Unique identifier for this server instance
|
|
27
|
-
*/
|
|
28
|
-
instanceId: string;
|
|
29
|
-
/**
|
|
30
|
-
* Whether the server maintains session state
|
|
31
|
-
*/
|
|
32
|
-
stateful: boolean;
|
|
33
|
-
/**
|
|
34
|
-
* Whether the server has event storage enabled
|
|
35
|
-
*/
|
|
36
|
-
hasEventStore: boolean;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Event storage interface for accessing stored events
|
|
40
|
-
*/
|
|
41
|
-
interface MCPEventStore {
|
|
42
|
-
/**
|
|
43
|
-
* Get stored events, optionally filtered by client and/or after a specific event
|
|
44
|
-
* @param clientId - Optional client ID to filter events
|
|
45
|
-
* @param afterEventId - Optional event ID to get events after
|
|
46
|
-
* @param limit - Maximum number of events to return (default: 100)
|
|
47
|
-
*/
|
|
48
|
-
getEvents(clientId?: string, afterEventId?: EventId, limit?: number): StoredEvent[];
|
|
49
|
-
/**
|
|
50
|
-
* Get the ID of the last event, optionally for a specific client
|
|
51
|
-
* @param clientId - Optional client ID to filter by
|
|
52
|
-
*/
|
|
53
|
-
getLastEventId(clientId?: string): EventId | null;
|
|
54
|
-
/**
|
|
55
|
-
* Clear stored events, optionally for a specific client
|
|
56
|
-
* @param clientId - Optional client ID to clear events for
|
|
57
|
-
*/
|
|
58
|
-
clearEvents(clientId?: string): void;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* The MCP interface exposed on window for browser environments
|
|
62
|
-
*/
|
|
63
|
-
interface MCPBrowserInterface {
|
|
64
|
-
/**
|
|
65
|
-
* Connect a client to the MCP server
|
|
66
|
-
* @param clientId - Unique identifier for the client
|
|
67
|
-
* @param options - Optional connection options
|
|
68
|
-
* @returns MessagePort for communication or null if connection fails
|
|
69
|
-
*/
|
|
70
|
-
connect(clientId: string, options?: MCPConnectOptions): MessagePort | null;
|
|
71
|
-
/**
|
|
72
|
-
* Disconnect a client from the MCP server
|
|
73
|
-
* @param clientId - The client ID to disconnect
|
|
74
|
-
*/
|
|
75
|
-
disconnect(clientId: string): void;
|
|
76
|
-
/**
|
|
77
|
-
* Terminate a client's session and clean up all associated resources
|
|
78
|
-
* @param clientId - The client ID to terminate
|
|
79
|
-
*/
|
|
80
|
-
terminateSession?(clientId: string): void;
|
|
81
|
-
/**
|
|
82
|
-
* Check if the MCP server is available and running
|
|
83
|
-
*/
|
|
84
|
-
isServerAvailable(): boolean;
|
|
85
|
-
/**
|
|
86
|
-
* Get information about the MCP server
|
|
87
|
-
*/
|
|
88
|
-
getServerInfo(): MCPServerInfo;
|
|
89
|
-
/**
|
|
90
|
-
* Event storage access (only available in stateful mode with event store)
|
|
91
|
-
*/
|
|
92
|
-
events?: MCPEventStore;
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Extended Window interface with MCP support
|
|
96
|
-
*/
|
|
97
|
-
interface MCPWindow extends Window {
|
|
98
|
-
mcp?: MCPBrowserInterface;
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Message types for internal MCP communication
|
|
102
|
-
*/
|
|
103
|
-
interface MCPServerInfoMessage {
|
|
104
|
-
type: 'mcp-server-info';
|
|
105
|
-
serverInstanceId: string;
|
|
106
|
-
serverSessionId?: string;
|
|
107
|
-
hasEventStore: boolean;
|
|
108
|
-
streamId: StreamId;
|
|
109
|
-
}
|
|
110
|
-
interface MCPEventMessage {
|
|
111
|
-
type: 'mcp-event';
|
|
112
|
-
eventId: EventId;
|
|
113
|
-
message: JSONRPCMessage;
|
|
114
|
-
}
|
|
115
|
-
interface MCPReplayEventMessage {
|
|
116
|
-
type: 'mcp-replay-event';
|
|
117
|
-
eventId: EventId;
|
|
118
|
-
message: JSONRPCMessage;
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Stored event with metadata for event sourcing
|
|
122
|
-
*/
|
|
123
|
-
interface StoredEvent {
|
|
124
|
-
eventId: EventId;
|
|
125
|
-
streamId: StreamId;
|
|
126
|
-
message: JSONRPCMessage;
|
|
127
|
-
timestamp: number;
|
|
128
|
-
clientId: string;
|
|
129
|
-
}
|
|
130
|
-
declare enum NativeMessageType {
|
|
131
|
-
START = "start",
|
|
132
|
-
STARTED = "started",
|
|
133
|
-
STOP = "stop",
|
|
134
|
-
STOPPED = "stopped",
|
|
135
|
-
PING = "ping",
|
|
136
|
-
PONG = "pong",
|
|
137
|
-
ERROR = "error",
|
|
138
|
-
LIST_TOOLS = "list_tools",
|
|
139
|
-
CALL_TOOL = "call_tool",
|
|
140
|
-
TOOL_LIST_UPDATED = "tool_list_updated",
|
|
141
|
-
TOOL_LIST_UPDATED_ACK = "tool_list_updated_ack",
|
|
142
|
-
PROCESS_DATA = "process_data",
|
|
143
|
-
SERVER_STARTED = "server_started",
|
|
144
|
-
SERVER_STOPPED = "server_stopped",
|
|
145
|
-
ERROR_FROM_NATIVE_HOST = "error_from_native_host",
|
|
146
|
-
CONNECT_NATIVE = "connectNative",
|
|
147
|
-
PING_NATIVE = "ping_native",
|
|
148
|
-
DISCONNECT_NATIVE = "disconnect_native"
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Chrome Extension Constants
|
|
152
|
-
* Centralized configuration values and magic constants
|
|
153
|
-
*/
|
|
154
|
-
declare const NATIVE_HOST: {
|
|
155
|
-
readonly NAME: "com.chromemcp.nativehost";
|
|
156
|
-
readonly DEFAULT_PORT: 12306;
|
|
157
|
-
};
|
|
158
|
-
declare const ERROR_MESSAGES: {
|
|
159
|
-
readonly NATIVE_CONNECTION_FAILED: "Failed to connect to native host";
|
|
160
|
-
readonly NATIVE_DISCONNECTED: "Native connection disconnected";
|
|
161
|
-
readonly SERVER_STATUS_LOAD_FAILED: "Failed to load server status";
|
|
162
|
-
readonly TOOL_EXECUTION_FAILED: "Tool execution failed";
|
|
163
|
-
readonly SERVER_STATUS_SAVE_FAILED: "Failed to save server status";
|
|
164
|
-
};
|
|
165
|
-
declare const SUCCESS_MESSAGES: {
|
|
166
|
-
readonly TOOL_EXECUTED: "Tool executed successfully";
|
|
167
|
-
readonly CONNECTION_ESTABLISHED: "Connection established";
|
|
168
|
-
readonly SERVER_STARTED: "Server started successfully";
|
|
169
|
-
readonly SERVER_STOPPED: "Server stopped successfully";
|
|
170
|
-
};
|
|
171
|
-
declare const STORAGE_KEYS: {
|
|
172
|
-
readonly SERVER_STATUS: "serverStatus";
|
|
173
|
-
};
|
|
174
|
-
declare const BACKGROUND_MESSAGE_TYPES: {
|
|
175
|
-
readonly GET_SERVER_STATUS: "get_server_status";
|
|
176
|
-
readonly REFRESH_SERVER_STATUS: "refresh_server_status";
|
|
177
|
-
readonly SERVER_STATUS_CHANGED: "server_status_changed";
|
|
178
|
-
};
|
|
179
|
-
declare const HOST_NAME: "com.chromemcp.nativehost";
|
|
180
|
-
/**
|
|
181
|
-
* Server status management interface
|
|
182
|
-
*/
|
|
183
|
-
interface ServerStatus {
|
|
184
|
-
isRunning: boolean;
|
|
185
|
-
port?: number;
|
|
186
|
-
lastUpdated: number;
|
|
187
|
-
}
|
|
9
|
+
declare const DEFAULT_TAB_CHANNEL_ID = "mcp-default";
|
|
10
|
+
declare const DEFAULT_IFRAME_CHANNEL_ID = "mcp-iframe";
|
|
188
11
|
//#endregion
|
|
189
12
|
//#region src/ExtensionClientTransport.d.ts
|
|
190
|
-
/**
|
|
191
|
-
* Configuration options for ExtensionClientTransport
|
|
192
|
-
*/
|
|
193
13
|
interface ExtensionClientTransportOptions {
|
|
194
|
-
/**
|
|
195
|
-
* The extension ID to connect to (optional for same-extension connections)
|
|
196
|
-
*/
|
|
14
|
+
/** Extension ID to connect to. Omit for the current extension. */
|
|
197
15
|
extensionId?: string;
|
|
198
|
-
/**
|
|
199
|
-
* Port name for the connection
|
|
200
|
-
* Default: 'mcp'
|
|
201
|
-
*/
|
|
16
|
+
/** Port name (default: `mcp`). */
|
|
202
17
|
portName?: string;
|
|
203
|
-
/**
|
|
204
|
-
* Enable automatic reconnection on disconnect
|
|
205
|
-
* Default: true
|
|
206
|
-
*/
|
|
207
|
-
autoReconnect?: boolean;
|
|
208
|
-
/**
|
|
209
|
-
* Maximum number of reconnection attempts
|
|
210
|
-
* Default: 10
|
|
211
|
-
*/
|
|
212
|
-
maxReconnectAttempts?: number;
|
|
213
|
-
/**
|
|
214
|
-
* Initial reconnection delay in milliseconds
|
|
215
|
-
* Default: 1000
|
|
216
|
-
*/
|
|
217
|
-
reconnectDelay?: number;
|
|
218
|
-
/**
|
|
219
|
-
* Maximum reconnection delay in milliseconds
|
|
220
|
-
* Default: 30000
|
|
221
|
-
*/
|
|
222
|
-
maxReconnectDelay?: number;
|
|
223
|
-
/**
|
|
224
|
-
* Reconnection backoff multiplier
|
|
225
|
-
* Default: 1.5
|
|
226
|
-
*/
|
|
227
|
-
reconnectBackoffMultiplier?: number;
|
|
228
18
|
}
|
|
229
|
-
/**
|
|
230
|
-
* Client transport for Chrome extensions using Port-based messaging.
|
|
231
|
-
* This transport can be used in content scripts, popup scripts, or sidepanel scripts
|
|
232
|
-
* to connect to a server running in the background service worker.
|
|
233
|
-
*
|
|
234
|
-
* Features automatic reconnection to handle background service worker lifecycle.
|
|
235
|
-
*/
|
|
19
|
+
/** Client transport for Chrome extension Port messaging. */
|
|
236
20
|
declare class ExtensionClientTransport implements Transport {
|
|
237
21
|
private _port;
|
|
238
|
-
private _extensionId;
|
|
239
|
-
private _portName;
|
|
22
|
+
private readonly _extensionId;
|
|
23
|
+
private readonly _portName;
|
|
240
24
|
private _messageHandler;
|
|
241
25
|
private _disconnectHandler;
|
|
242
|
-
private
|
|
243
|
-
private
|
|
244
|
-
private _reconnectTimer;
|
|
245
|
-
private _currentReconnectDelay;
|
|
246
|
-
private _isStarted;
|
|
247
|
-
private _isClosed;
|
|
248
|
-
private _autoReconnect;
|
|
249
|
-
private _maxReconnectAttempts;
|
|
250
|
-
private _reconnectDelay;
|
|
251
|
-
private _maxReconnectDelay;
|
|
252
|
-
private _reconnectBackoffMultiplier;
|
|
26
|
+
private _started;
|
|
27
|
+
private _closed;
|
|
253
28
|
onclose?: () => void;
|
|
254
29
|
onerror?: (error: Error) => void;
|
|
255
|
-
onmessage?:
|
|
30
|
+
onmessage?: Transport['onmessage'];
|
|
256
31
|
constructor(options?: ExtensionClientTransportOptions);
|
|
257
|
-
/**
|
|
258
|
-
* Starts the transport by connecting to the extension port
|
|
259
|
-
*/
|
|
260
32
|
start(): Promise<void>;
|
|
261
|
-
/**
|
|
262
|
-
* Connects to the extension port
|
|
263
|
-
*/
|
|
264
|
-
private _connect;
|
|
265
|
-
/**
|
|
266
|
-
* Sends a message to the server
|
|
267
|
-
*/
|
|
268
33
|
send(message: JSONRPCMessage, _options?: TransportSendOptions): Promise<void>;
|
|
269
|
-
/**
|
|
270
|
-
* Closes the transport
|
|
271
|
-
*/
|
|
272
34
|
close(): Promise<void>;
|
|
273
|
-
|
|
274
|
-
* Cleans up event listeners and references
|
|
275
|
-
*/
|
|
35
|
+
private _finishClose;
|
|
276
36
|
private _cleanup;
|
|
277
|
-
/**
|
|
278
|
-
* Schedules a reconnection attempt
|
|
279
|
-
*/
|
|
280
|
-
private _scheduleReconnect;
|
|
281
|
-
/**
|
|
282
|
-
* Attempts to reconnect to the extension
|
|
283
|
-
*/
|
|
284
|
-
private _attemptReconnect;
|
|
285
37
|
}
|
|
286
38
|
//#endregion
|
|
287
39
|
//#region src/ExtensionServerTransport.d.ts
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
*/
|
|
291
|
-
type ExtensionServerTransportOptions = {
|
|
292
|
-
/**
|
|
293
|
-
* Enable keep-alive mechanism to prevent service worker shutdown
|
|
294
|
-
* Default: true
|
|
295
|
-
*/
|
|
40
|
+
interface ExtensionServerTransportOptions {
|
|
41
|
+
/** Send keep-alive messages (default: true). */
|
|
296
42
|
keepAlive?: boolean;
|
|
297
|
-
/**
|
|
298
|
-
* Keep-alive interval in milliseconds
|
|
299
|
-
* Default: 25000 (25 seconds, less than Chrome's 30-second timeout)
|
|
300
|
-
*/
|
|
43
|
+
/** Keep-alive interval in milliseconds (default: 25,000). */
|
|
301
44
|
keepAliveInterval?: number;
|
|
302
|
-
}
|
|
303
|
-
/**
|
|
304
|
-
* Server transport for Chrome extensions using Port-based messaging.
|
|
305
|
-
* This transport handles a single client connection through Chrome's port messaging API.
|
|
306
|
-
* It should be used in the extension's background service worker.
|
|
307
|
-
*
|
|
308
|
-
* Features:
|
|
309
|
-
* - Keep-alive mechanism to prevent service worker shutdown
|
|
310
|
-
* - Graceful connection state management
|
|
311
|
-
*/
|
|
45
|
+
}
|
|
46
|
+
/** Server transport for one Chrome extension Port connection. */
|
|
312
47
|
declare class ExtensionServerTransport implements Transport {
|
|
313
48
|
private _port;
|
|
314
49
|
private _started;
|
|
50
|
+
private _closed;
|
|
315
51
|
private _messageHandler;
|
|
316
52
|
private _disconnectHandler;
|
|
317
53
|
private _keepAliveTimer;
|
|
318
|
-
private
|
|
319
|
-
private _connectionInfo;
|
|
54
|
+
private readonly _keepAliveInterval;
|
|
55
|
+
private readonly _connectionInfo;
|
|
320
56
|
onclose?: () => void;
|
|
321
57
|
onerror?: (error: Error) => void;
|
|
322
|
-
onmessage?:
|
|
58
|
+
onmessage?: Transport['onmessage'];
|
|
323
59
|
constructor(port: chrome.runtime.Port, options?: ExtensionServerTransportOptions);
|
|
324
|
-
/**
|
|
325
|
-
* Starts the transport and begins handling messages
|
|
326
|
-
*/
|
|
327
60
|
start(): Promise<void>;
|
|
328
|
-
/**
|
|
329
|
-
* Sends a message to the client
|
|
330
|
-
*/
|
|
331
61
|
send(message: JSONRPCMessage, _options?: TransportSendOptions): Promise<void>;
|
|
332
|
-
/**
|
|
333
|
-
* Closes the transport
|
|
334
|
-
*/
|
|
335
62
|
close(): Promise<void>;
|
|
336
|
-
/**
|
|
337
|
-
* Cleans up event listeners and references
|
|
338
|
-
*/
|
|
339
|
-
private _cleanup;
|
|
340
|
-
/**
|
|
341
|
-
* Starts the keep-alive mechanism
|
|
342
|
-
*/
|
|
343
|
-
private _startKeepAlive;
|
|
344
|
-
/**
|
|
345
|
-
* Stops the keep-alive mechanism
|
|
346
|
-
*/
|
|
347
|
-
private _stopKeepAlive;
|
|
348
|
-
/**
|
|
349
|
-
* Gets connection information
|
|
350
|
-
*/
|
|
351
63
|
getConnectionInfo(): {
|
|
352
64
|
uptime: number;
|
|
353
65
|
isConnected: boolean;
|
|
@@ -355,584 +67,129 @@ declare class ExtensionServerTransport implements Transport {
|
|
|
355
67
|
lastMessageAt: number;
|
|
356
68
|
messageCount: number;
|
|
357
69
|
};
|
|
70
|
+
private _finishClose;
|
|
71
|
+
private _cleanup;
|
|
358
72
|
}
|
|
359
73
|
//#endregion
|
|
360
74
|
//#region src/IframeChildTransport.d.ts
|
|
361
75
|
interface IframeChildTransportOptions {
|
|
362
|
-
/**
|
|
363
|
-
allowedOrigins: string[];
|
|
364
|
-
/**
|
|
76
|
+
/** Parent origins allowed to connect. Pass `['*']` only to disable validation deliberately. */
|
|
77
|
+
allowedOrigins: readonly string[];
|
|
78
|
+
/** Channel name (default: `mcp-iframe`). */
|
|
365
79
|
channelId?: string;
|
|
366
|
-
/** Retry interval for broadcasting ready signal in milliseconds (default: 250) */
|
|
367
|
-
serverReadyRetryMs?: number;
|
|
368
80
|
}
|
|
369
|
-
/**
|
|
370
|
-
* IframeChildTransport - Server transport for iframe
|
|
371
|
-
*
|
|
372
|
-
* Use this transport when an iframe wants to expose an MCP server to its parent page.
|
|
373
|
-
* Supports cross-origin communication.
|
|
374
|
-
*
|
|
375
|
-
* @example
|
|
376
|
-
* ```typescript
|
|
377
|
-
* const transport = new IframeChildTransport({
|
|
378
|
-
* allowedOrigins: ['https://parent-app.com'],
|
|
379
|
-
* });
|
|
380
|
-
*
|
|
381
|
-
* const server = new Server({ name: 'IframeApp', version: '1.0.0' });
|
|
382
|
-
* await server.connect(transport);
|
|
383
|
-
* ```
|
|
384
|
-
*/
|
|
81
|
+
/** Server transport for an MCP server running inside an iframe. */
|
|
385
82
|
declare class IframeChildTransport implements Transport {
|
|
386
83
|
private _started;
|
|
387
|
-
private
|
|
388
|
-
private
|
|
389
|
-
private
|
|
84
|
+
private _closed;
|
|
85
|
+
private readonly _allowedOrigins;
|
|
86
|
+
private readonly _channelId;
|
|
87
|
+
private _messageHandler;
|
|
390
88
|
private _clientOrigin?;
|
|
391
|
-
|
|
392
|
-
|
|
89
|
+
/**
|
|
90
|
+
* Fires when the connected parent's origin is first established, and again if a
|
|
91
|
+
* different parent takes over the channel. Lets a server scope per-tool exposure
|
|
92
|
+
* to the embedder it is actually talking to.
|
|
93
|
+
*/
|
|
94
|
+
onclientorigin?: (origin: string) => void;
|
|
393
95
|
onclose?: () => void;
|
|
394
96
|
onerror?: (error: Error) => void;
|
|
395
|
-
onmessage?:
|
|
97
|
+
onmessage?: Transport['onmessage'];
|
|
396
98
|
constructor(options: IframeChildTransportOptions);
|
|
99
|
+
/** Origin of the connected parent, or `undefined` before its first message arrives. */
|
|
100
|
+
get clientOrigin(): string | undefined;
|
|
397
101
|
start(): Promise<void>;
|
|
398
|
-
|
|
399
|
-
private scheduleServerReadyRetry;
|
|
400
|
-
private clearServerReadyRetry;
|
|
401
|
-
send(message: JSONRPCMessage): Promise<void>;
|
|
102
|
+
send(message: JSONRPCMessage, _options?: TransportSendOptions): Promise<void>;
|
|
402
103
|
close(): Promise<void>;
|
|
104
|
+
private _broadcastServerReady;
|
|
403
105
|
}
|
|
404
106
|
//#endregion
|
|
405
107
|
//#region src/IframeParentTransport.d.ts
|
|
406
108
|
interface IframeParentTransportOptions {
|
|
407
|
-
/** Reference to the iframe element */
|
|
408
109
|
iframe: HTMLIFrameElement;
|
|
409
|
-
/** Expected origin
|
|
410
|
-
targetOrigin
|
|
411
|
-
/**
|
|
110
|
+
/** Expected iframe origin. Pass `'*'` only to disable origin validation deliberately. */
|
|
111
|
+
targetOrigin: string;
|
|
112
|
+
/** Channel name (default: `mcp-iframe`). */
|
|
412
113
|
channelId?: string;
|
|
413
|
-
/** Retry interval
|
|
114
|
+
/** Retry interval while `iframe.contentWindow` is unavailable (default: 250ms). */
|
|
414
115
|
checkReadyRetryMs?: number;
|
|
415
116
|
}
|
|
416
|
-
/**
|
|
417
|
-
* IframeParentTransport - Client transport for parent page
|
|
418
|
-
*
|
|
419
|
-
* Use this transport when the parent page wants to connect to an MCP server
|
|
420
|
-
* running inside an iframe. Supports cross-origin communication.
|
|
421
|
-
*
|
|
422
|
-
* @example
|
|
423
|
-
* ```typescript
|
|
424
|
-
* const iframe = document.querySelector('iframe');
|
|
425
|
-
* const transport = new IframeParentTransport({
|
|
426
|
-
* iframe,
|
|
427
|
-
* targetOrigin: 'https://iframe-app.com',
|
|
428
|
-
* });
|
|
429
|
-
*
|
|
430
|
-
* const client = new Client({ name: 'Parent', version: '1.0.0' });
|
|
431
|
-
* await client.connect(transport);
|
|
432
|
-
* ```
|
|
433
|
-
*/
|
|
117
|
+
/** Client transport for an MCP server running inside an iframe. */
|
|
434
118
|
declare class IframeParentTransport implements Transport {
|
|
435
119
|
private _started;
|
|
436
|
-
private
|
|
437
|
-
private
|
|
438
|
-
private
|
|
439
|
-
private
|
|
440
|
-
private _checkReadyTimeout;
|
|
120
|
+
private _closed;
|
|
121
|
+
private readonly _iframe;
|
|
122
|
+
private readonly _targetOrigin;
|
|
123
|
+
private readonly _channelId;
|
|
441
124
|
private readonly _checkReadyRetryMs;
|
|
442
|
-
|
|
125
|
+
private _messageHandler;
|
|
126
|
+
private _checkReadyTimeout;
|
|
443
127
|
private _serverReadyResolve;
|
|
444
128
|
private _serverReadyReject;
|
|
129
|
+
private _serverReadySettled;
|
|
130
|
+
readonly serverReadyPromise: Promise<void>;
|
|
445
131
|
onclose?: () => void;
|
|
446
132
|
onerror?: (error: Error) => void;
|
|
447
|
-
onmessage?:
|
|
133
|
+
onmessage?: Transport['onmessage'];
|
|
448
134
|
constructor(options: IframeParentTransportOptions);
|
|
449
135
|
start(): Promise<void>;
|
|
450
|
-
|
|
451
|
-
private scheduleCheckReadyRetry;
|
|
452
|
-
private clearCheckReadyRetry;
|
|
453
|
-
send(message: JSONRPCMessage): Promise<void>;
|
|
136
|
+
send(message: JSONRPCMessage, _options?: TransportSendOptions): Promise<void>;
|
|
454
137
|
close(): Promise<void>;
|
|
138
|
+
private _sendCheckReady;
|
|
139
|
+
private _clearCheckReadyRetry;
|
|
140
|
+
private _resolveServerReady;
|
|
455
141
|
}
|
|
456
142
|
//#endregion
|
|
457
143
|
//#region src/TabClientTransport.d.ts
|
|
458
|
-
/**
|
|
459
|
-
* Configuration options for TabClientTransport.
|
|
460
|
-
*
|
|
461
|
-
* @see {@link TabClientTransport}
|
|
462
|
-
*/
|
|
463
144
|
interface TabClientTransportOptions {
|
|
464
|
-
/**
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
* **Security**: This origin is checked against `event.origin` for all incoming
|
|
468
|
-
* messages to prevent cross-origin attacks. Only messages from this origin will
|
|
469
|
-
* be processed.
|
|
470
|
-
*
|
|
471
|
-
* @example 'https://example.com'
|
|
472
|
-
* @example 'http://localhost:3000'
|
|
473
|
-
*/
|
|
474
|
-
targetOrigin?: string;
|
|
475
|
-
/**
|
|
476
|
-
* Channel identifier for message routing.
|
|
477
|
-
*
|
|
478
|
-
* Multiple transports can coexist on the same page by using different channel IDs.
|
|
479
|
-
* This allows for isolated communication channels between different MCP clients
|
|
480
|
-
* and servers.
|
|
481
|
-
*
|
|
482
|
-
* @default 'mcp-default'
|
|
483
|
-
*/
|
|
145
|
+
/** Expected server origin. Pass `'*'` only to disable origin validation deliberately. */
|
|
146
|
+
targetOrigin: string;
|
|
147
|
+
/** Channel name (default: `mcp-default`). */
|
|
484
148
|
channelId?: string;
|
|
485
|
-
/**
|
|
486
|
-
* Request timeout in milliseconds.
|
|
487
|
-
*
|
|
488
|
-
* If a request doesn't receive a response within this time, a timeout error
|
|
489
|
-
* is synthesized and delivered to the client. This prevents infinite hangs
|
|
490
|
-
* when the server becomes unresponsive due to:
|
|
491
|
-
* - Page navigation
|
|
492
|
-
* - JavaScript errors
|
|
493
|
-
* - Network issues
|
|
494
|
-
* - Server crashes
|
|
495
|
-
*
|
|
496
|
-
* **Design rationale**: 10 seconds is appropriate for most tool operations.
|
|
497
|
-
* For operations that may take longer (e.g., complex computations, slow network
|
|
498
|
-
* requests), increase this value via the configuration option.
|
|
499
|
-
*
|
|
500
|
-
* @default 10000 (10 seconds)
|
|
501
|
-
* @see {@link _handleRequestTimeout} for timeout implementation
|
|
502
|
-
*/
|
|
503
|
-
requestTimeout?: number;
|
|
504
149
|
}
|
|
505
|
-
/**
|
|
506
|
-
* Client-side transport for same-window MCP communication via postMessage.
|
|
507
|
-
*
|
|
508
|
-
* This transport connects an MCP client to a TabServerTransport running in the same
|
|
509
|
-
* window. Communication occurs via the browser's `window.postMessage()` API, which
|
|
510
|
-
* provides:
|
|
511
|
-
* - Same-window message passing (no network overhead)
|
|
512
|
-
* - Origin validation for security
|
|
513
|
-
* - Asynchronous message delivery
|
|
514
|
-
*
|
|
515
|
-
* **Architecture**:
|
|
516
|
-
* ```
|
|
517
|
-
* ┌─────────────────┐ ┌──────────────────┐
|
|
518
|
-
* │ MCP Client │ postMessage() │ MCP Server │
|
|
519
|
-
* │ (This side) │ ←─────────────────→│ (TabServerTransport)
|
|
520
|
-
* └─────────────────┘ └──────────────────┘
|
|
521
|
-
* ```
|
|
522
|
-
*
|
|
523
|
-
* **Key features**:
|
|
524
|
-
* - Request timeout to prevent infinite hangs (default 10s)
|
|
525
|
-
* - Server ready detection via handshake
|
|
526
|
-
* - Origin validation for security
|
|
527
|
-
* - Channel-based message routing
|
|
528
|
-
*
|
|
529
|
-
* **Use cases**:
|
|
530
|
-
* - MCP client running in content script connecting to page context
|
|
531
|
-
* - MCP client in extension popup connecting to background page
|
|
532
|
-
* - Testing and development scenarios
|
|
533
|
-
*
|
|
534
|
-
* @example Basic usage
|
|
535
|
-
* ```typescript
|
|
536
|
-
* const transport = new TabClientTransport({
|
|
537
|
-
* targetOrigin: 'https://example.com',
|
|
538
|
-
* channelId: 'my-mcp-channel',
|
|
539
|
-
* requestTimeout: 10000, // Optional (default)
|
|
540
|
-
* });
|
|
541
|
-
*
|
|
542
|
-
* // Wait for server to be ready
|
|
543
|
-
* await transport.start();
|
|
544
|
-
* await transport.serverReadyPromise;
|
|
545
|
-
*
|
|
546
|
-
* // Now safe to send messages
|
|
547
|
-
* await transport.send({
|
|
548
|
-
* jsonrpc: '2.0',
|
|
549
|
-
* id: 1,
|
|
550
|
-
* method: 'tools/call',
|
|
551
|
-
* params: { name: 'my_tool', arguments: {} }
|
|
552
|
-
* });
|
|
553
|
-
* ```
|
|
554
|
-
*
|
|
555
|
-
* @example With custom timeout
|
|
556
|
-
* ```typescript
|
|
557
|
-
* const transport = new TabClientTransport({
|
|
558
|
-
* targetOrigin: '*',
|
|
559
|
-
* requestTimeout: 60000, // 60 seconds for slow operations
|
|
560
|
-
* });
|
|
561
|
-
* ```
|
|
562
|
-
*
|
|
563
|
-
* @see {@link TabServerTransport} for the server-side implementation
|
|
564
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage} for postMessage API
|
|
565
|
-
*/
|
|
150
|
+
/** Client transport for an MCP server running in the same window. */
|
|
566
151
|
declare class TabClientTransport implements Transport {
|
|
567
|
-
/** Transport state flag */
|
|
568
152
|
private _started;
|
|
569
|
-
|
|
153
|
+
private _closed;
|
|
570
154
|
private readonly _targetOrigin;
|
|
571
|
-
/** Channel ID for message routing */
|
|
572
155
|
private readonly _channelId;
|
|
573
|
-
|
|
574
|
-
private
|
|
575
|
-
|
|
576
|
-
private _messageHandler?;
|
|
577
|
-
/**
|
|
578
|
-
* Promise that resolves when the server is ready to receive messages.
|
|
579
|
-
*
|
|
580
|
-
* **Usage**: Always `await` this promise after calling `start()` and before
|
|
581
|
-
* sending messages. Sending messages before the server is ready may result
|
|
582
|
-
* in lost messages.
|
|
583
|
-
*
|
|
584
|
-
* @example
|
|
585
|
-
* ```typescript
|
|
586
|
-
* await transport.start();
|
|
587
|
-
* await transport.serverReadyPromise;
|
|
588
|
-
* // Now safe to send messages
|
|
589
|
-
* ```
|
|
590
|
-
*/
|
|
591
|
-
readonly serverReadyPromise: Promise<void>;
|
|
592
|
-
/** Internal resolver for serverReadyPromise */
|
|
593
|
-
private readonly _serverReadyResolve;
|
|
594
|
-
/** Internal rejector for serverReadyPromise */
|
|
595
|
-
private readonly _serverReadyReject;
|
|
596
|
-
/** Tracks if serverReadyPromise has been settled */
|
|
156
|
+
private _messageHandler;
|
|
157
|
+
private _serverReadyResolve;
|
|
158
|
+
private _serverReadyReject;
|
|
597
159
|
private _serverReadySettled;
|
|
598
|
-
/**
|
|
599
|
-
|
|
600
|
-
*
|
|
601
|
-
* **Key**: Request ID (from JSON-RPC message)
|
|
602
|
-
* **Value**: Timeout ID and original request
|
|
603
|
-
*
|
|
604
|
-
* When a response is received, the entry is removed and timeout cleared.
|
|
605
|
-
* If timeout expires first, an error response is synthesized.
|
|
606
|
-
*/
|
|
607
|
-
private readonly _activeRequests;
|
|
608
|
-
/** Callback invoked when transport closes */
|
|
160
|
+
/** Resolves when the server signals readiness. `send()` awaits it automatically. */
|
|
161
|
+
readonly serverReadyPromise: Promise<void>;
|
|
609
162
|
onclose?: () => void;
|
|
610
|
-
/** Callback invoked on errors */
|
|
611
163
|
onerror?: (error: Error) => void;
|
|
612
|
-
|
|
613
|
-
onmessage?: (message: JSONRPCMessage) => void;
|
|
614
|
-
/**
|
|
615
|
-
* Creates a new TabClientTransport instance.
|
|
616
|
-
*
|
|
617
|
-
* **Note**: The transport is not started automatically. Call `start()` to begin
|
|
618
|
-
* listening for messages.
|
|
619
|
-
*
|
|
620
|
-
* @param options - Configuration options
|
|
621
|
-
* @throws {Error} If targetOrigin is not specified
|
|
622
|
-
*/
|
|
164
|
+
onmessage?: Transport['onmessage'];
|
|
623
165
|
constructor(options: TabClientTransportOptions);
|
|
624
|
-
/**
|
|
625
|
-
* Starts the transport by registering message listeners.
|
|
626
|
-
*
|
|
627
|
-
* **Lifecycle**:
|
|
628
|
-
* 1. Register `window.addEventListener('message', ...)` handler
|
|
629
|
-
* 2. Send server ready check (in case server started first)
|
|
630
|
-
* 3. Wait for server ready signal via `serverReadyPromise`
|
|
631
|
-
*
|
|
632
|
-
* **Note**: Always `await transport.serverReadyPromise` after calling this method
|
|
633
|
-
* and before sending messages.
|
|
634
|
-
*
|
|
635
|
-
* @throws {Error} If transport is already started
|
|
636
|
-
*/
|
|
637
166
|
start(): Promise<void>;
|
|
638
|
-
|
|
639
|
-
* Sends a JSON-RPC message to the server.
|
|
640
|
-
*
|
|
641
|
-
* **Request timeout**: If this is a request (has `method` and `id`), a timeout
|
|
642
|
-
* is started. If the server doesn't respond within `requestTimeout` milliseconds,
|
|
643
|
-
* an error response is synthesized.
|
|
644
|
-
*
|
|
645
|
-
* **Await server ready**: This method automatically awaits `serverReadyPromise`
|
|
646
|
-
* before sending, ensuring messages aren't lost.
|
|
647
|
-
*
|
|
648
|
-
* @param message - JSON-RPC message to send
|
|
649
|
-
* @throws {Error} If transport is not started
|
|
650
|
-
*/
|
|
651
|
-
send(message: JSONRPCMessage): Promise<void>;
|
|
652
|
-
/**
|
|
653
|
-
* Closes the transport and cleans up resources.
|
|
654
|
-
*
|
|
655
|
-
* **Cleanup performed**:
|
|
656
|
-
* - Removes message event listener
|
|
657
|
-
* - Clears all active request timeouts
|
|
658
|
-
* - Rejects server ready promise if still pending
|
|
659
|
-
* - Invokes `onclose` callback
|
|
660
|
-
*
|
|
661
|
-
* **Note**: After calling this method, the transport cannot be reused.
|
|
662
|
-
* Create a new instance if needed.
|
|
663
|
-
*/
|
|
167
|
+
send(message: JSONRPCMessage, _options?: TransportSendOptions): Promise<void>;
|
|
664
168
|
close(): Promise<void>;
|
|
665
|
-
|
|
666
|
-
* Sends a server ready check message.
|
|
667
|
-
*
|
|
668
|
-
* This prompts the server to respond with 'mcp-server-ready' signal,
|
|
669
|
-
* resolving the `serverReadyPromise`. Useful when the server may have
|
|
670
|
-
* started before the client.
|
|
671
|
-
*
|
|
672
|
-
* @private
|
|
673
|
-
*/
|
|
674
|
-
private _sendCheckReady;
|
|
675
|
-
/**
|
|
676
|
-
* Starts timeout tracking for a request.
|
|
677
|
-
*
|
|
678
|
-
* **Behavior**: After `requestTimeout` milliseconds, if no response is received,
|
|
679
|
-
* `_handleRequestTimeout` is called to synthesize an error response.
|
|
680
|
-
*
|
|
681
|
-
* **Note**: Only requests (messages with `method` and `id`) are tracked.
|
|
682
|
-
* Notifications (no `id`) and responses are not tracked.
|
|
683
|
-
*
|
|
684
|
-
* @param message - JSON-RPC request message
|
|
685
|
-
* @private
|
|
686
|
-
*/
|
|
687
|
-
private _startRequestTimeout;
|
|
688
|
-
/**
|
|
689
|
-
* Clears timeout tracking for a response.
|
|
690
|
-
*
|
|
691
|
-
* Called when a response (with `result` or `error`) is received.
|
|
692
|
-
* Clears the timeout and removes the tracking entry.
|
|
693
|
-
*
|
|
694
|
-
* @param message - JSON-RPC response message
|
|
695
|
-
* @private
|
|
696
|
-
*/
|
|
697
|
-
private _clearRequestTimeout;
|
|
698
|
-
/**
|
|
699
|
-
* Handles request timeout by synthesizing an error response.
|
|
700
|
-
*
|
|
701
|
-
* **Error response format** (JSON-RPC 2.0):
|
|
702
|
-
* ```json
|
|
703
|
-
* {
|
|
704
|
-
* "jsonrpc": "2.0",
|
|
705
|
-
* "id": "<request-id>",
|
|
706
|
-
* "error": {
|
|
707
|
-
* "code": -32000,
|
|
708
|
-
* "message": "Request timeout - server may have navigated or become unresponsive",
|
|
709
|
-
* "data": {
|
|
710
|
-
* "timeoutMs": 10000,
|
|
711
|
-
* "originalMethod": "tools/call"
|
|
712
|
-
* }
|
|
713
|
-
* }
|
|
714
|
-
* }
|
|
715
|
-
* ```
|
|
716
|
-
*
|
|
717
|
-
* **Error code**: `-32000` (Server error) per JSON-RPC 2.0 specification.
|
|
718
|
-
*
|
|
719
|
-
* @param requestId - ID of the timed-out request
|
|
720
|
-
* @private
|
|
721
|
-
*/
|
|
722
|
-
private _handleRequestTimeout;
|
|
169
|
+
private _resolveServerReady;
|
|
723
170
|
}
|
|
724
171
|
//#endregion
|
|
725
172
|
//#region src/TabServerTransport.d.ts
|
|
726
173
|
interface TabServerTransportOptions {
|
|
727
|
-
/**
|
|
728
|
-
allowedOrigins: string[];
|
|
729
|
-
/**
|
|
174
|
+
/** Origins allowed to connect. Pass `['*']` only to disable origin validation deliberately. */
|
|
175
|
+
allowedOrigins: readonly string[];
|
|
176
|
+
/** Channel name (default: `mcp-default`). */
|
|
730
177
|
channelId?: string;
|
|
731
178
|
}
|
|
732
179
|
declare class TabServerTransport implements Transport {
|
|
733
180
|
private _started;
|
|
734
|
-
private
|
|
735
|
-
private
|
|
736
|
-
private
|
|
737
|
-
private _beforeUnloadHandler?;
|
|
738
|
-
private _cleanupInterval?;
|
|
739
|
-
private _pendingRequests;
|
|
740
|
-
private readonly SELF_TARGET_ORIGIN;
|
|
741
|
-
private readonly REQUEST_TIMEOUT_MS;
|
|
742
|
-
onclose?: () => void;
|
|
743
|
-
onerror?: (error: Error) => void;
|
|
744
|
-
onmessage?: (message: JSONRPCMessage) => void;
|
|
745
|
-
constructor(options: TabServerTransportOptions);
|
|
746
|
-
start(): Promise<void>;
|
|
747
|
-
send(message: JSONRPCMessage): Promise<void>;
|
|
748
|
-
/**
|
|
749
|
-
* Handle page navigation by sending interrupted responses for all pending requests.
|
|
750
|
-
* Called during beforeunload event.
|
|
751
|
-
* @private
|
|
752
|
-
*/
|
|
753
|
-
private _handleBeforeUnload;
|
|
754
|
-
/**
|
|
755
|
-
* Clean up stale requests that have been pending for too long.
|
|
756
|
-
* Called periodically to prevent memory leaks.
|
|
757
|
-
* @private
|
|
758
|
-
*/
|
|
759
|
-
private _cleanupStaleRequests;
|
|
760
|
-
close(): Promise<void>;
|
|
761
|
-
}
|
|
762
|
-
//#endregion
|
|
763
|
-
//#region src/UserScriptClientTransport.d.ts
|
|
764
|
-
/**
|
|
765
|
-
* Configuration options for UserScriptClientTransport
|
|
766
|
-
*/
|
|
767
|
-
interface UserScriptClientTransportOptions {
|
|
768
|
-
/**
|
|
769
|
-
* The extension ID to connect to (optional for same-extension connections)
|
|
770
|
-
*/
|
|
771
|
-
extensionId?: string;
|
|
772
|
-
/**
|
|
773
|
-
* Port name for the connection
|
|
774
|
-
* Default: 'mcp'
|
|
775
|
-
*/
|
|
776
|
-
portName?: string;
|
|
777
|
-
/**
|
|
778
|
-
* Enable automatic reconnection on disconnect
|
|
779
|
-
* Default: true
|
|
780
|
-
*/
|
|
781
|
-
autoReconnect?: boolean;
|
|
782
|
-
/**
|
|
783
|
-
* Maximum number of reconnection attempts
|
|
784
|
-
* Default: 10
|
|
785
|
-
*/
|
|
786
|
-
maxReconnectAttempts?: number;
|
|
787
|
-
/**
|
|
788
|
-
* Initial reconnection delay in milliseconds
|
|
789
|
-
* Default: 1000
|
|
790
|
-
*/
|
|
791
|
-
reconnectDelay?: number;
|
|
792
|
-
/**
|
|
793
|
-
* Maximum reconnection delay in milliseconds
|
|
794
|
-
* Default: 30000
|
|
795
|
-
*/
|
|
796
|
-
maxReconnectDelay?: number;
|
|
797
|
-
/**
|
|
798
|
-
* Reconnection backoff multiplier
|
|
799
|
-
* Default: 1.5
|
|
800
|
-
*/
|
|
801
|
-
reconnectBackoffMultiplier?: number;
|
|
802
|
-
}
|
|
803
|
-
/**
|
|
804
|
-
* Client transport for Chrome MV3 User Scripts using Port-based messaging.
|
|
805
|
-
* This transport can be used inside a User Script context to connect to the
|
|
806
|
-
* extension's background service worker. On the extension side, connections
|
|
807
|
-
* are received via chrome.runtime.onUserScriptConnect.
|
|
808
|
-
*
|
|
809
|
-
* Features automatic reconnection to handle background service worker lifecycle.
|
|
810
|
-
*/
|
|
811
|
-
declare class UserScriptClientTransport implements Transport {
|
|
812
|
-
private _port;
|
|
813
|
-
private _extensionId;
|
|
814
|
-
private _portName;
|
|
815
|
-
private _messageHandler;
|
|
816
|
-
private _disconnectHandler;
|
|
817
|
-
private _isReconnecting;
|
|
818
|
-
private _reconnectAttempts;
|
|
819
|
-
private _reconnectTimer;
|
|
820
|
-
private _currentReconnectDelay;
|
|
821
|
-
private _isStarted;
|
|
822
|
-
private _isClosed;
|
|
823
|
-
private _autoReconnect;
|
|
824
|
-
private _maxReconnectAttempts;
|
|
825
|
-
private _reconnectDelay;
|
|
826
|
-
private _maxReconnectDelay;
|
|
827
|
-
private _reconnectBackoffMultiplier;
|
|
828
|
-
onclose?: () => void;
|
|
829
|
-
onerror?: (error: Error) => void;
|
|
830
|
-
onmessage?: (message: JSONRPCMessage) => void;
|
|
831
|
-
constructor(options?: UserScriptClientTransportOptions);
|
|
832
|
-
/**
|
|
833
|
-
* Starts the transport by connecting to the extension port
|
|
834
|
-
*/
|
|
835
|
-
start(): Promise<void>;
|
|
836
|
-
/**
|
|
837
|
-
* Connects to the extension port
|
|
838
|
-
*/
|
|
839
|
-
private _connect;
|
|
840
|
-
/**
|
|
841
|
-
* Sends a message to the server
|
|
842
|
-
*/
|
|
843
|
-
send(message: JSONRPCMessage, _options?: TransportSendOptions): Promise<void>;
|
|
844
|
-
/**
|
|
845
|
-
* Closes the transport
|
|
846
|
-
*/
|
|
847
|
-
close(): Promise<void>;
|
|
848
|
-
/**
|
|
849
|
-
* Cleans up event listeners and references
|
|
850
|
-
*/
|
|
851
|
-
private _cleanup;
|
|
852
|
-
/**
|
|
853
|
-
* Schedules a reconnection attempt
|
|
854
|
-
*/
|
|
855
|
-
private _scheduleReconnect;
|
|
856
|
-
/**
|
|
857
|
-
* Attempts to reconnect to the extension
|
|
858
|
-
*/
|
|
859
|
-
private _attemptReconnect;
|
|
860
|
-
}
|
|
861
|
-
//#endregion
|
|
862
|
-
//#region src/UserScriptServerTransport.d.ts
|
|
863
|
-
/**
|
|
864
|
-
* Configuration options for UserScriptServerTransport
|
|
865
|
-
*/
|
|
866
|
-
type UserScriptServerTransportOptions = {
|
|
867
|
-
/**
|
|
868
|
-
* Enable keep-alive mechanism to prevent service worker shutdown
|
|
869
|
-
* Default: true
|
|
870
|
-
*/
|
|
871
|
-
keepAlive?: boolean;
|
|
872
|
-
/**
|
|
873
|
-
* Keep-alive interval in milliseconds
|
|
874
|
-
* Default: 25000 (25 seconds, less than Chrome's 30-second timeout)
|
|
875
|
-
*/
|
|
876
|
-
keepAliveInterval?: number;
|
|
877
|
-
};
|
|
878
|
-
/**
|
|
879
|
-
* Server transport for Chrome MV3 User Scripts using Port-based messaging.
|
|
880
|
-
* This transport handles a single client connection through Chrome's port
|
|
881
|
-
* messaging API. It should be used in the extension's background service
|
|
882
|
-
* worker. Connections are initiated from User Scripts via chrome.runtime.connect
|
|
883
|
-
* and received here via chrome.runtime.onUserScriptConnect.
|
|
884
|
-
*
|
|
885
|
-
* Features:
|
|
886
|
-
* - Keep-alive mechanism to prevent service worker shutdown
|
|
887
|
-
* - Graceful connection state management
|
|
888
|
-
*/
|
|
889
|
-
declare class UserScriptServerTransport implements Transport {
|
|
890
|
-
private _port;
|
|
891
|
-
private _started;
|
|
181
|
+
private _closed;
|
|
182
|
+
private readonly _allowedOrigins;
|
|
183
|
+
private readonly _channelId;
|
|
892
184
|
private _messageHandler;
|
|
893
|
-
private _disconnectHandler;
|
|
894
|
-
private _keepAliveTimer;
|
|
895
|
-
private _options;
|
|
896
|
-
private _connectionInfo;
|
|
897
185
|
onclose?: () => void;
|
|
898
186
|
onerror?: (error: Error) => void;
|
|
899
|
-
onmessage?:
|
|
900
|
-
constructor(
|
|
901
|
-
/**
|
|
902
|
-
* Starts the transport and begins handling messages
|
|
903
|
-
*/
|
|
187
|
+
onmessage?: Transport['onmessage'];
|
|
188
|
+
constructor(options: TabServerTransportOptions);
|
|
904
189
|
start(): Promise<void>;
|
|
905
|
-
/**
|
|
906
|
-
* Sends a message to the client
|
|
907
|
-
*/
|
|
908
190
|
send(message: JSONRPCMessage, _options?: TransportSendOptions): Promise<void>;
|
|
909
|
-
/**
|
|
910
|
-
* Closes the transport
|
|
911
|
-
*/
|
|
912
191
|
close(): Promise<void>;
|
|
913
|
-
/**
|
|
914
|
-
* Cleans up event listeners and references
|
|
915
|
-
*/
|
|
916
|
-
private _cleanup;
|
|
917
|
-
/**
|
|
918
|
-
* Starts the keep-alive mechanism
|
|
919
|
-
*/
|
|
920
|
-
private _startKeepAlive;
|
|
921
|
-
/**
|
|
922
|
-
* Stops the keep-alive mechanism
|
|
923
|
-
*/
|
|
924
|
-
private _stopKeepAlive;
|
|
925
|
-
/**
|
|
926
|
-
* Gets connection information
|
|
927
|
-
*/
|
|
928
|
-
getConnectionInfo(): {
|
|
929
|
-
uptime: number;
|
|
930
|
-
isConnected: boolean;
|
|
931
|
-
connectedAt: number;
|
|
932
|
-
lastMessageAt: number;
|
|
933
|
-
messageCount: number;
|
|
934
|
-
};
|
|
935
192
|
}
|
|
936
193
|
//#endregion
|
|
937
|
-
export {
|
|
194
|
+
export { DEFAULT_IFRAME_CHANNEL_ID, DEFAULT_TAB_CHANNEL_ID, ExtensionClientTransport, ExtensionClientTransportOptions, ExtensionServerTransport, ExtensionServerTransportOptions, IframeChildTransport, IframeChildTransportOptions, IframeParentTransport, IframeParentTransportOptions, TabClientTransport, TabClientTransportOptions, TabServerTransport, TabServerTransportOptions };
|
|
938
195
|
//# sourceMappingURL=index.d.ts.map
|