@ractive-ch/tab-mcp-client 0.0.0-stage → 0.1.0-alpha.1
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/AI_NOTICE +9 -0
- package/LICENSE +21 -0
- package/README.md +57 -2
- package/THIRD-PARTY-NOTICES.txt +32 -0
- package/cdn/connection-cfq1n4kh.min.js +10 -0
- package/cdn/connection-cfq1n4kh.min.js.map +20 -0
- package/cdn/tab-mcp-client.min.js +5 -0
- package/cdn/tab-mcp-client.min.js.map +13 -0
- package/cdn/ui-aac2cw9f.min.js +4 -0
- package/cdn/ui-aac2cw9f.min.js.map +10 -0
- package/connection-1gf7vqz2.js +900 -0
- package/connection-1gf7vqz2.js.map +13 -0
- package/connection.d.ts +19 -0
- package/index.d.ts +7 -0
- package/index.js +543 -0
- package/index.js.map +13 -0
- package/internal.d.ts +39 -0
- package/mapping.d.ts +28 -0
- package/mcp.d.ts +41 -0
- package/package.json +35 -5
- package/protocol.d.ts +122 -0
- package/registry.d.ts +29 -0
- package/secret.d.ts +18 -0
- package/tab-mcp.d.ts +84 -0
- package/types.d.ts +211 -0
- package/ui-168746sb.js +233 -0
- package/ui-168746sb.js.map +10 -0
- package/ui.d.ts +26 -0
- package/version.d.ts +3 -0
|
@@ -0,0 +1,900 @@
|
|
|
1
|
+
// src/mcp.ts
|
|
2
|
+
import { Validator } from "@cfworker/json-schema";
|
|
3
|
+
|
|
4
|
+
// src/mapping.ts
|
|
5
|
+
var ANNOTATIONS_META_KEY = "ch.ractive.tab-mcp/annotations";
|
|
6
|
+
var WEBMCP_HINTS = [
|
|
7
|
+
"readOnlyHint",
|
|
8
|
+
"untrustedContentHint",
|
|
9
|
+
"consequentialHint",
|
|
10
|
+
"debugging"
|
|
11
|
+
];
|
|
12
|
+
var MCP_ONLY_HINTS = [
|
|
13
|
+
"destructiveHint",
|
|
14
|
+
"idempotentHint",
|
|
15
|
+
"openWorldHint"
|
|
16
|
+
];
|
|
17
|
+
function mapAnnotations(hints) {
|
|
18
|
+
const annotations = {};
|
|
19
|
+
if (hints.readOnlyHint === true) {
|
|
20
|
+
annotations["readOnlyHint"] = true;
|
|
21
|
+
annotations["destructiveHint"] = false;
|
|
22
|
+
} else if (hints.readOnlyHint === false) {
|
|
23
|
+
annotations["readOnlyHint"] = false;
|
|
24
|
+
}
|
|
25
|
+
if (hints.consequentialHint === true)
|
|
26
|
+
annotations["destructiveHint"] = true;
|
|
27
|
+
if (hints.untrustedContentHint === true)
|
|
28
|
+
annotations["openWorldHint"] = true;
|
|
29
|
+
for (const key of MCP_ONLY_HINTS) {
|
|
30
|
+
const value = hints[key];
|
|
31
|
+
if (value !== undefined)
|
|
32
|
+
annotations[key] = value;
|
|
33
|
+
}
|
|
34
|
+
const meta = {};
|
|
35
|
+
for (const key of WEBMCP_HINTS) {
|
|
36
|
+
const value = hints[key];
|
|
37
|
+
if (value !== undefined)
|
|
38
|
+
meta[key] = value;
|
|
39
|
+
}
|
|
40
|
+
const out = {};
|
|
41
|
+
if (Object.keys(annotations).length > 0)
|
|
42
|
+
out.annotations = annotations;
|
|
43
|
+
if (Object.keys(meta).length > 0)
|
|
44
|
+
out.meta = meta;
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
function errorResult(message) {
|
|
48
|
+
return { content: [{ type: "text", text: message }], isError: true };
|
|
49
|
+
}
|
|
50
|
+
function isPlainObject(v) {
|
|
51
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
52
|
+
}
|
|
53
|
+
function toCallToolResult(value) {
|
|
54
|
+
if (typeof value === "string")
|
|
55
|
+
return { content: [{ type: "text", text: value }] };
|
|
56
|
+
if (value === undefined)
|
|
57
|
+
return { content: [] };
|
|
58
|
+
if (isPlainObject(value) && Array.isArray(value["content"])) {
|
|
59
|
+
return value;
|
|
60
|
+
}
|
|
61
|
+
let text;
|
|
62
|
+
try {
|
|
63
|
+
text = JSON.stringify(value);
|
|
64
|
+
} catch {}
|
|
65
|
+
if (text === undefined)
|
|
66
|
+
return errorResult("The tool returned a value that is not JSON.");
|
|
67
|
+
const result = { content: [{ type: "text", text }] };
|
|
68
|
+
if (isPlainObject(value))
|
|
69
|
+
result.structuredContent = value;
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
function thrownResult(error) {
|
|
73
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
74
|
+
return errorResult(message || "The tool failed.");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/mcp.ts
|
|
78
|
+
var MCP_PROTOCOL_VERSION = "2026-07-28";
|
|
79
|
+
var META_VERSION = "io.modelcontextprotocol/protocolVersion";
|
|
80
|
+
var META_CAPABILITIES = "io.modelcontextprotocol/clientCapabilities";
|
|
81
|
+
var META_CLIENT_INFO = "io.modelcontextprotocol/clientInfo";
|
|
82
|
+
var META_SERVER_INFO = "io.modelcontextprotocol/serverInfo";
|
|
83
|
+
var META_SUBSCRIPTION = "io.modelcontextprotocol/subscriptionId";
|
|
84
|
+
var HEADER_MISMATCH = -32020;
|
|
85
|
+
var UNSUPPORTED_VERSION = -32022;
|
|
86
|
+
var encoder = new TextEncoder;
|
|
87
|
+
function isObject(v) {
|
|
88
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
89
|
+
}
|
|
90
|
+
function json(status, value) {
|
|
91
|
+
return new Response(JSON.stringify(value), {
|
|
92
|
+
status,
|
|
93
|
+
headers: { "content-type": "application/json" }
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
function rpcError(status, id, code, message, data) {
|
|
97
|
+
const error = { code, message };
|
|
98
|
+
if (data !== undefined)
|
|
99
|
+
error["data"] = data;
|
|
100
|
+
return json(status, { jsonrpc: "2.0", id, error });
|
|
101
|
+
}
|
|
102
|
+
function mismatch(id, header, body) {
|
|
103
|
+
return rpcError(400, id, HEADER_MISMATCH, `Bad Request: the request headers and body disagree: ${body}`, {
|
|
104
|
+
mismatch: { header, body }
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
function sseEvent(message) {
|
|
108
|
+
return encoder.encode(`event: message
|
|
109
|
+
data: ${JSON.stringify(message)}
|
|
110
|
+
|
|
111
|
+
`);
|
|
112
|
+
}
|
|
113
|
+
var SSE_HEADERS = {
|
|
114
|
+
"content-type": "text/event-stream",
|
|
115
|
+
"cache-control": "no-cache"
|
|
116
|
+
};
|
|
117
|
+
function isJsonContentType(value) {
|
|
118
|
+
return (value ?? "").split(";")[0]?.trim().toLowerCase() === "application/json";
|
|
119
|
+
}
|
|
120
|
+
function decodeHeaderValue(value) {
|
|
121
|
+
if (!(value.startsWith("=?base64?") && value.endsWith("?=")))
|
|
122
|
+
return value;
|
|
123
|
+
const b64 = value.slice(9, -2);
|
|
124
|
+
if (!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(b64))
|
|
125
|
+
return;
|
|
126
|
+
try {
|
|
127
|
+
const bin = atob(b64);
|
|
128
|
+
const bytes = Uint8Array.from(bin, (c) => c.charCodeAt(0));
|
|
129
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
130
|
+
} catch {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
function envelopeIssue(meta) {
|
|
135
|
+
if (!(META_VERSION in meta))
|
|
136
|
+
return { key: META_VERSION, problem: "missing" };
|
|
137
|
+
if (!(META_CAPABILITIES in meta))
|
|
138
|
+
return { key: META_CAPABILITIES, problem: "missing" };
|
|
139
|
+
if (typeof meta[META_VERSION] !== "string") {
|
|
140
|
+
return { key: META_VERSION, problem: "expected a protocol version string" };
|
|
141
|
+
}
|
|
142
|
+
if (!isObject(meta[META_CAPABILITIES]))
|
|
143
|
+
return { key: META_CAPABILITIES, problem: "expected an object" };
|
|
144
|
+
const info = meta[META_CLIENT_INFO];
|
|
145
|
+
if (info !== undefined && !(isObject(info) && typeof info["name"] === "string" && typeof info["version"] === "string")) {
|
|
146
|
+
return { key: META_CLIENT_INFO, problem: "expected {name, version}" };
|
|
147
|
+
}
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
function honoured(params) {
|
|
151
|
+
const requested = params["notifications"];
|
|
152
|
+
return isObject(requested) && requested["toolsListChanged"] === true ? { toolsListChanged: true } : {};
|
|
153
|
+
}
|
|
154
|
+
function createMcpHandler(options) {
|
|
155
|
+
const { tools, server } = options;
|
|
156
|
+
const serverInfo = { name: server.name, version: server.version };
|
|
157
|
+
if (server.title !== undefined)
|
|
158
|
+
serverInfo["title"] = server.title;
|
|
159
|
+
const validators = new WeakMap;
|
|
160
|
+
const listeners = new Set;
|
|
161
|
+
let changeQueued = false;
|
|
162
|
+
const resultMeta = (extra = {}) => ({
|
|
163
|
+
...extra,
|
|
164
|
+
[META_SERVER_INFO]: serverInfo
|
|
165
|
+
});
|
|
166
|
+
function result(id, value) {
|
|
167
|
+
return {
|
|
168
|
+
jsonrpc: "2.0",
|
|
169
|
+
id,
|
|
170
|
+
result: { ...value, resultType: "complete", _meta: resultMeta() }
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
function validate(tool, args) {
|
|
174
|
+
let validator = validators.get(tool);
|
|
175
|
+
if (validator === undefined) {
|
|
176
|
+
validator = new Validator(tool.inputSchema, "2020-12", false);
|
|
177
|
+
validators.set(tool, validator);
|
|
178
|
+
}
|
|
179
|
+
const outcome = validator.validate(args);
|
|
180
|
+
if (outcome.valid)
|
|
181
|
+
return;
|
|
182
|
+
const detail = outcome.errors.filter((e) => e.keyword !== "properties" && e.keyword !== "$ref").map((e) => `${e.instanceLocation} ${e.error}`).slice(0, 5).join("; ");
|
|
183
|
+
return `Input validation error: Invalid arguments for tool ${tool.name}: ${detail || "the arguments do not match the schema"}`;
|
|
184
|
+
}
|
|
185
|
+
function listTools() {
|
|
186
|
+
return tools.list().map((tool) => {
|
|
187
|
+
const entry = { name: tool.name };
|
|
188
|
+
if (tool.title !== undefined)
|
|
189
|
+
entry["title"] = tool.title;
|
|
190
|
+
entry["description"] = tool.description;
|
|
191
|
+
entry["inputSchema"] = tool.inputSchema;
|
|
192
|
+
const { annotations, meta } = mapAnnotations(tool.annotations);
|
|
193
|
+
if (annotations !== undefined)
|
|
194
|
+
entry["annotations"] = annotations;
|
|
195
|
+
if (meta !== undefined)
|
|
196
|
+
entry["_meta"] = { [ANNOTATIONS_META_KEY]: meta };
|
|
197
|
+
return entry;
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
async function runTool(params, signal, grant) {
|
|
201
|
+
const name = params["name"];
|
|
202
|
+
if (typeof name !== "string")
|
|
203
|
+
return { invalid: "Invalid params: name must be a string" };
|
|
204
|
+
const tool = tools.get(name);
|
|
205
|
+
if (tool === undefined)
|
|
206
|
+
return { invalid: `Tool ${name} not found` };
|
|
207
|
+
const raw = params["arguments"] ?? {};
|
|
208
|
+
if (!isObject(raw))
|
|
209
|
+
return { invalid: "Invalid params: arguments must be an object" };
|
|
210
|
+
const invalid = validate(tool, raw);
|
|
211
|
+
if (invalid !== undefined)
|
|
212
|
+
return errorResult(invalid);
|
|
213
|
+
const client = options.client(grant);
|
|
214
|
+
if (tool.annotations.consequentialHint === true) {
|
|
215
|
+
const question = {
|
|
216
|
+
tool: { name: tool.name, description: tool.description },
|
|
217
|
+
arguments: raw,
|
|
218
|
+
client,
|
|
219
|
+
signal
|
|
220
|
+
};
|
|
221
|
+
if (tool.title !== undefined)
|
|
222
|
+
question.tool.title = tool.title;
|
|
223
|
+
let allowed = false;
|
|
224
|
+
try {
|
|
225
|
+
allowed = await options.confirm(question);
|
|
226
|
+
} catch {
|
|
227
|
+
allowed = false;
|
|
228
|
+
}
|
|
229
|
+
if (!allowed)
|
|
230
|
+
return errorResult("The user declined");
|
|
231
|
+
}
|
|
232
|
+
if (signal.aborted)
|
|
233
|
+
return errorResult("Cancelled");
|
|
234
|
+
try {
|
|
235
|
+
return toCallToolResult(await tool.execute(raw, { signal, client }));
|
|
236
|
+
} catch (error) {
|
|
237
|
+
return thrownResult(error);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function callTool(id, params, signal, grant) {
|
|
241
|
+
const stream = new ReadableStream({
|
|
242
|
+
start: (controller) => {
|
|
243
|
+
const finish = (message) => {
|
|
244
|
+
if (signal.aborted)
|
|
245
|
+
return;
|
|
246
|
+
let event;
|
|
247
|
+
try {
|
|
248
|
+
event = sseEvent(message);
|
|
249
|
+
} catch {
|
|
250
|
+
event = sseEvent(result(id, errorResult("The tool returned a result that is not JSON.")));
|
|
251
|
+
}
|
|
252
|
+
try {
|
|
253
|
+
controller.enqueue(event);
|
|
254
|
+
controller.close();
|
|
255
|
+
} catch {}
|
|
256
|
+
};
|
|
257
|
+
runTool(params, signal, grant).then((outcome) => finish(typeof outcome["invalid"] === "string" ? {
|
|
258
|
+
jsonrpc: "2.0",
|
|
259
|
+
id,
|
|
260
|
+
error: { code: -32602, message: outcome["invalid"] }
|
|
261
|
+
} : result(id, outcome)), () => finish({
|
|
262
|
+
jsonrpc: "2.0",
|
|
263
|
+
id,
|
|
264
|
+
error: { code: -32603, message: "Internal error" }
|
|
265
|
+
}));
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
return new Response(stream, { status: 200, headers: SSE_HEADERS });
|
|
269
|
+
}
|
|
270
|
+
function listen(id, params, signal) {
|
|
271
|
+
const notifications = honoured(params);
|
|
272
|
+
const finalResult = {
|
|
273
|
+
jsonrpc: "2.0",
|
|
274
|
+
id,
|
|
275
|
+
result: {
|
|
276
|
+
resultType: "complete",
|
|
277
|
+
_meta: resultMeta({ [META_SUBSCRIPTION]: id })
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
let own;
|
|
281
|
+
const stream = new ReadableStream({
|
|
282
|
+
start: (controller) => {
|
|
283
|
+
controller.enqueue(sseEvent({
|
|
284
|
+
jsonrpc: "2.0",
|
|
285
|
+
method: "notifications/subscriptions/acknowledged",
|
|
286
|
+
params: { notifications, _meta: { [META_SUBSCRIPTION]: id } }
|
|
287
|
+
}));
|
|
288
|
+
if (Object.keys(notifications).length === 0) {
|
|
289
|
+
controller.enqueue(sseEvent(finalResult));
|
|
290
|
+
controller.close();
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
const listener = {
|
|
294
|
+
id,
|
|
295
|
+
send: (message) => {
|
|
296
|
+
try {
|
|
297
|
+
controller.enqueue(sseEvent(message));
|
|
298
|
+
} catch {
|
|
299
|
+
listeners.delete(listener);
|
|
300
|
+
}
|
|
301
|
+
},
|
|
302
|
+
end: () => {
|
|
303
|
+
listeners.delete(listener);
|
|
304
|
+
try {
|
|
305
|
+
controller.enqueue(sseEvent(finalResult));
|
|
306
|
+
controller.close();
|
|
307
|
+
} catch {}
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
own = listener;
|
|
311
|
+
listeners.add(listener);
|
|
312
|
+
signal.addEventListener("abort", () => {
|
|
313
|
+
listeners.delete(listener);
|
|
314
|
+
try {
|
|
315
|
+
controller.close();
|
|
316
|
+
} catch {}
|
|
317
|
+
});
|
|
318
|
+
},
|
|
319
|
+
cancel: () => {
|
|
320
|
+
if (own !== undefined)
|
|
321
|
+
listeners.delete(own);
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
return new Response(stream, { status: 200, headers: SSE_HEADERS });
|
|
325
|
+
}
|
|
326
|
+
function check(request, body) {
|
|
327
|
+
if (Array.isArray(body)) {
|
|
328
|
+
return rpcError(400, null, -32600, "Bad Request: JSON-RPC batches are not supported by this endpoint");
|
|
329
|
+
}
|
|
330
|
+
if (!isObject(body) || body["jsonrpc"] !== "2.0") {
|
|
331
|
+
return rpcError(400, null, -32600, "Bad Request: the request body is not a valid JSON-RPC message");
|
|
332
|
+
}
|
|
333
|
+
const method = body["method"];
|
|
334
|
+
const rawId = body["id"];
|
|
335
|
+
if (typeof method !== "string") {
|
|
336
|
+
return "result" in body || "error" in body ? rpcError(400, null, -32600, "Bad Request: JSON-RPC responses cannot be posted to this endpoint") : rpcError(400, null, -32600, "Bad Request: the request body is not a valid JSON-RPC message");
|
|
337
|
+
}
|
|
338
|
+
const headerVersion = request.headers.get("mcp-protocol-version");
|
|
339
|
+
const headerMethod = request.headers.get("mcp-method");
|
|
340
|
+
const params = isObject(body["params"]) ? body["params"] : {};
|
|
341
|
+
const meta = isObject(params["_meta"]) ? params["_meta"] : undefined;
|
|
342
|
+
const claim = meta !== undefined && META_VERSION in meta;
|
|
343
|
+
const claimed = typeof meta?.[META_VERSION] === "string" ? meta[META_VERSION] : undefined;
|
|
344
|
+
const headerModern = headerVersion !== null && headerVersion >= MCP_PROTOCOL_VERSION;
|
|
345
|
+
if (rawId === undefined) {
|
|
346
|
+
if (claimed !== undefined && headerVersion !== null && headerVersion !== claimed) {
|
|
347
|
+
return mismatch(null, headerVersion, `the notification envelope names protocol version ${claimed} but the MCP-Protocol-Version header names ${headerVersion}`);
|
|
348
|
+
}
|
|
349
|
+
if ((claim || headerModern) && headerMethod !== null && headerMethod !== method) {
|
|
350
|
+
return mismatch(null, headerMethod, `the notification body names method ${method} but the Mcp-Method header names ${headerMethod}`);
|
|
351
|
+
}
|
|
352
|
+
return "notification";
|
|
353
|
+
}
|
|
354
|
+
if (typeof rawId !== "string" && typeof rawId !== "number") {
|
|
355
|
+
return rpcError(400, null, -32600, "Bad Request: the request body is not a valid JSON-RPC message");
|
|
356
|
+
}
|
|
357
|
+
const id = rawId;
|
|
358
|
+
if (!claim || method === "initialize" && (claimed === undefined || claimed < MCP_PROTOCOL_VERSION)) {
|
|
359
|
+
if (method === "initialize") {
|
|
360
|
+
if (headerModern) {
|
|
361
|
+
return mismatch(id, headerVersion, "an initialize request (legacy handshake) was sent with a modern MCP-Protocol-Version header");
|
|
362
|
+
}
|
|
363
|
+
const requested = typeof params["protocolVersion"] === "string" ? params["protocolVersion"] : undefined;
|
|
364
|
+
return unsupported(id, requested);
|
|
365
|
+
}
|
|
366
|
+
if (headerModern) {
|
|
367
|
+
const missing = meta === undefined ? ["_meta"] : [META_VERSION];
|
|
368
|
+
return rpcError(400, id, -32602, `Invalid params: the MCP-Protocol-Version header names protocol revision ${headerVersion}, but the request is missing the required per-request envelope key(s): ${missing.join(", ")}`, { envelope: { missing } });
|
|
369
|
+
}
|
|
370
|
+
return unsupported(id, headerVersion ?? undefined);
|
|
371
|
+
}
|
|
372
|
+
const issue = envelopeIssue(meta);
|
|
373
|
+
if (issue !== undefined) {
|
|
374
|
+
return rpcError(400, id, -32602, `Invalid _meta envelope for protocol revision ${MCP_PROTOCOL_VERSION}: ${issue.key}: ${issue.problem}`, { envelope: issue });
|
|
375
|
+
}
|
|
376
|
+
if (headerVersion !== null && claimed !== undefined && headerVersion !== claimed) {
|
|
377
|
+
return mismatch(id, headerVersion, `the body envelope names protocol version ${claimed} but the MCP-Protocol-Version header names ${headerVersion}`);
|
|
378
|
+
}
|
|
379
|
+
if (headerMethod !== null && headerMethod !== method) {
|
|
380
|
+
return mismatch(id, headerMethod, `the body names method ${method} but the Mcp-Method header names ${headerMethod}`);
|
|
381
|
+
}
|
|
382
|
+
if (claimed !== MCP_PROTOCOL_VERSION)
|
|
383
|
+
return unsupported(id, claimed);
|
|
384
|
+
if (headerVersion === null) {
|
|
385
|
+
return mismatch(id, "(missing)", `the body envelope names protocol version ${claimed} but the required MCP-Protocol-Version header is absent`);
|
|
386
|
+
}
|
|
387
|
+
if (headerMethod === null) {
|
|
388
|
+
return mismatch(id, "(missing)", `the body names method ${method} but the required Mcp-Method header is absent`);
|
|
389
|
+
}
|
|
390
|
+
if (method === "tools/call" && typeof params["name"] === "string") {
|
|
391
|
+
const name = params["name"];
|
|
392
|
+
const header = request.headers.get("mcp-name");
|
|
393
|
+
if (header === null) {
|
|
394
|
+
return mismatch(id, "(missing)", `the body carries params.name="${name}" but the required Mcp-Name header is absent`);
|
|
395
|
+
}
|
|
396
|
+
const decoded = decodeHeaderValue(header);
|
|
397
|
+
if (decoded === undefined) {
|
|
398
|
+
return mismatch(id, header, "the Mcp-Name header carries an invalid Base64 sentinel value");
|
|
399
|
+
}
|
|
400
|
+
if (decoded !== name) {
|
|
401
|
+
return mismatch(id, header, `the body carries params.name="${name}" but the Mcp-Name header names "${decoded}"`);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
return { id, method, params };
|
|
405
|
+
}
|
|
406
|
+
function unsupported(id, requested) {
|
|
407
|
+
return requested === undefined ? rpcError(400, id, UNSUPPORTED_VERSION, "Unsupported protocol version: the request did not name a protocol version", { supported: [MCP_PROTOCOL_VERSION] }) : rpcError(400, id, UNSUPPORTED_VERSION, `Unsupported protocol version: ${requested}`, { supported: [MCP_PROTOCOL_VERSION], requested });
|
|
408
|
+
}
|
|
409
|
+
async function handle(request, context) {
|
|
410
|
+
if (request.method.toUpperCase() !== "POST")
|
|
411
|
+
return rpcError(405, null, -32000, "Method not allowed.");
|
|
412
|
+
if (!isJsonContentType(request.headers.get("content-type"))) {
|
|
413
|
+
return rpcError(415, null, -32000, "Unsupported Media Type: Content-Type must be application/json");
|
|
414
|
+
}
|
|
415
|
+
let body;
|
|
416
|
+
try {
|
|
417
|
+
body = JSON.parse(context.body ?? await request.text());
|
|
418
|
+
} catch {
|
|
419
|
+
return rpcError(400, null, -32700, "Parse error: the request body is not valid JSON");
|
|
420
|
+
}
|
|
421
|
+
const checked = check(request, body);
|
|
422
|
+
if (checked instanceof Response)
|
|
423
|
+
return checked;
|
|
424
|
+
if (checked === "notification")
|
|
425
|
+
return new Response(null, { status: 202 });
|
|
426
|
+
const { id, method, params } = checked;
|
|
427
|
+
switch (method) {
|
|
428
|
+
case "server/discover": {
|
|
429
|
+
const discover = {
|
|
430
|
+
supportedVersions: [MCP_PROTOCOL_VERSION],
|
|
431
|
+
capabilities: { tools: { listChanged: true } }
|
|
432
|
+
};
|
|
433
|
+
if (server.instructions !== undefined)
|
|
434
|
+
discover["instructions"] = server.instructions;
|
|
435
|
+
discover["ttlMs"] = 0;
|
|
436
|
+
discover["cacheScope"] = "private";
|
|
437
|
+
return json(200, result(id, discover));
|
|
438
|
+
}
|
|
439
|
+
case "tools/list":
|
|
440
|
+
return json(200, result(id, { tools: listTools(), ttlMs: 0, cacheScope: "private" }));
|
|
441
|
+
case "tools/call":
|
|
442
|
+
return callTool(id, params, request.signal, context.grant);
|
|
443
|
+
case "subscriptions/listen":
|
|
444
|
+
return listen(id, params, request.signal);
|
|
445
|
+
default:
|
|
446
|
+
return rpcError(404, id, -32601, "Method not found");
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return {
|
|
450
|
+
handle: (request, context) => handle(request, context).catch(() => rpcError(500, null, -32603, "Internal server error")),
|
|
451
|
+
toolsChanged() {
|
|
452
|
+
if (changeQueued)
|
|
453
|
+
return;
|
|
454
|
+
changeQueued = true;
|
|
455
|
+
queueMicrotask(() => {
|
|
456
|
+
changeQueued = false;
|
|
457
|
+
for (const listener of [...listeners]) {
|
|
458
|
+
listener.send({
|
|
459
|
+
jsonrpc: "2.0",
|
|
460
|
+
method: "notifications/tools/list_changed",
|
|
461
|
+
params: { _meta: { [META_SUBSCRIPTION]: listener.id } }
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
});
|
|
465
|
+
},
|
|
466
|
+
close() {
|
|
467
|
+
for (const listener of [...listeners])
|
|
468
|
+
listener.end();
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// src/protocol.ts
|
|
474
|
+
var SUBPROTOCOL = "tab-mcp.v1";
|
|
475
|
+
function isObject2(v) {
|
|
476
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
477
|
+
}
|
|
478
|
+
var str = (v) => typeof v === "string";
|
|
479
|
+
function isGrant(v) {
|
|
480
|
+
if (!isObject2(v) || !str(v["grantId"]) || !str(v["createdAt"]))
|
|
481
|
+
return false;
|
|
482
|
+
const c = v["client"];
|
|
483
|
+
return isObject2(c) && str(c["id"]) && str(c["name"]) && str(c["host"]);
|
|
484
|
+
}
|
|
485
|
+
function isLimits(v) {
|
|
486
|
+
if (!isObject2(v))
|
|
487
|
+
return false;
|
|
488
|
+
return [
|
|
489
|
+
"maxMessageBytes",
|
|
490
|
+
"maxBodyBytes",
|
|
491
|
+
"requestTimeoutMs",
|
|
492
|
+
"idleTimeoutMs",
|
|
493
|
+
"heartbeatMs",
|
|
494
|
+
"keepAliveMs",
|
|
495
|
+
"maxConcurrentRequests"
|
|
496
|
+
].every((k) => typeof v[k] === "number");
|
|
497
|
+
}
|
|
498
|
+
function isStringRecord(v) {
|
|
499
|
+
return isObject2(v) && Object.values(v).every(str);
|
|
500
|
+
}
|
|
501
|
+
function parseRelayMessage(text) {
|
|
502
|
+
let v;
|
|
503
|
+
try {
|
|
504
|
+
v = JSON.parse(text);
|
|
505
|
+
} catch {
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
if (!isObject2(v))
|
|
509
|
+
return;
|
|
510
|
+
switch (v["type"]) {
|
|
511
|
+
case "session":
|
|
512
|
+
return str(v["sessionId"]) && str(v["address"]) && str(v["origin"]) && str(v["expiresAt"]) && isLimits(v["limits"]) && Array.isArray(v["grants"]) && v["grants"].every(isGrant) && (v["appName"] === undefined || str(v["appName"])) ? v : undefined;
|
|
513
|
+
case "ping":
|
|
514
|
+
return typeof v["t"] === "number" ? v : undefined;
|
|
515
|
+
case "bye":
|
|
516
|
+
return { type: "bye", reason: str(v["reason"]) ? v["reason"] : "" };
|
|
517
|
+
case "http.request":
|
|
518
|
+
return str(v["id"]) && str(v["method"]) && str(v["path"]) && isStringRecord(v["headers"]) && str(v["body"]) && isObject2(v["grant"]) && str(v["grant"]["grantId"]) && str(v["grant"]["clientId"]) ? v : undefined;
|
|
519
|
+
case "http.cancel":
|
|
520
|
+
return str(v["id"]) ? v : undefined;
|
|
521
|
+
case "pairing.code":
|
|
522
|
+
return str(v["code"]) && str(v["expiresAt"]) ? v : undefined;
|
|
523
|
+
case "grant.added":
|
|
524
|
+
return isGrant(v["grant"]) ? v : undefined;
|
|
525
|
+
case "grant.removed":
|
|
526
|
+
return str(v["grantId"]) ? {
|
|
527
|
+
type: "grant.removed",
|
|
528
|
+
grantId: v["grantId"],
|
|
529
|
+
reason: str(v["reason"]) ? v["reason"] : ""
|
|
530
|
+
} : undefined;
|
|
531
|
+
case "error":
|
|
532
|
+
return str(v["code"]) ? v : undefined;
|
|
533
|
+
default:
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// src/connection.ts
|
|
539
|
+
var CONNECT_TIMEOUT_MS = 15000;
|
|
540
|
+
var CLOSE_REASONS = {
|
|
541
|
+
1000: "closed",
|
|
542
|
+
1008: "policy",
|
|
543
|
+
1009: "too_large",
|
|
544
|
+
4001: "expired",
|
|
545
|
+
4002: "idle",
|
|
546
|
+
4003: "heartbeat",
|
|
547
|
+
4004: "shutdown",
|
|
548
|
+
4005: "replaced",
|
|
549
|
+
4008: "rate_limited"
|
|
550
|
+
};
|
|
551
|
+
var BYE_REASONS = {
|
|
552
|
+
replaced: "replaced",
|
|
553
|
+
expired: "expired",
|
|
554
|
+
idle: "idle",
|
|
555
|
+
shutdown: "shutdown"
|
|
556
|
+
};
|
|
557
|
+
var RESPONSE_HEADERS = ["content-type", "cache-control"];
|
|
558
|
+
function pageTitle() {
|
|
559
|
+
const title = globalThis.document?.title;
|
|
560
|
+
return typeof title === "string" ? title.slice(0, 300) : "";
|
|
561
|
+
}
|
|
562
|
+
function frameInfo(text) {
|
|
563
|
+
try {
|
|
564
|
+
const v = JSON.parse(text);
|
|
565
|
+
if (typeof v !== "object" || v === null || typeof v["type"] !== "string")
|
|
566
|
+
return;
|
|
567
|
+
return typeof v["id"] === "string" ? { type: v["type"], id: v["id"] } : { type: v["type"] };
|
|
568
|
+
} catch {
|
|
569
|
+
return;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
var KNOWN_TYPES = new Set([
|
|
573
|
+
"session",
|
|
574
|
+
"ping",
|
|
575
|
+
"bye",
|
|
576
|
+
"http.request",
|
|
577
|
+
"http.cancel",
|
|
578
|
+
"pairing.code",
|
|
579
|
+
"grant.added",
|
|
580
|
+
"grant.removed",
|
|
581
|
+
"error"
|
|
582
|
+
]);
|
|
583
|
+
var ENVELOPE_BYTES = 512;
|
|
584
|
+
var WORST_BYTES_PER_UNIT = 6;
|
|
585
|
+
var MIN_CHUNK_UNITS = 64;
|
|
586
|
+
var MIN_MESSAGE_BYTES = ENVELOPE_BYTES + WORST_BYTES_PER_UNIT * MIN_CHUNK_UNITS;
|
|
587
|
+
function chunkSize(maxMessageBytes) {
|
|
588
|
+
const units = Math.floor((maxMessageBytes - ENVELOPE_BYTES) / WORST_BYTES_PER_UNIT);
|
|
589
|
+
return units >= MIN_CHUNK_UNITS ? units : undefined;
|
|
590
|
+
}
|
|
591
|
+
function* pieces(text, max) {
|
|
592
|
+
let i = 0;
|
|
593
|
+
while (i < text.length) {
|
|
594
|
+
let end = Math.min(i + max, text.length);
|
|
595
|
+
const last = text.charCodeAt(end - 1);
|
|
596
|
+
if (end < text.length && last >= 55296 && last <= 56319) {
|
|
597
|
+
end += end - 1 > i ? -1 : 1;
|
|
598
|
+
}
|
|
599
|
+
yield text.slice(i, end);
|
|
600
|
+
i = end;
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
function openConnection(host, url, secret) {
|
|
604
|
+
const ws = new WebSocket(url, SUBPROTOCOL);
|
|
605
|
+
const inflight = new Map;
|
|
606
|
+
let state = "opening";
|
|
607
|
+
let byeReason;
|
|
608
|
+
let maxChunk = 64 * 1024;
|
|
609
|
+
let heartbeat;
|
|
610
|
+
let heartbeatMs = 0;
|
|
611
|
+
let resolveReady;
|
|
612
|
+
let rejectReady;
|
|
613
|
+
const ready = new Promise((resolve, reject) => {
|
|
614
|
+
resolveReady = resolve;
|
|
615
|
+
rejectReady = reject;
|
|
616
|
+
});
|
|
617
|
+
ready.catch(() => {});
|
|
618
|
+
const connectTimer = setTimeout(() => {
|
|
619
|
+
fail(new Error("The relay did not answer in time."));
|
|
620
|
+
}, CONNECT_TIMEOUT_MS);
|
|
621
|
+
const handler = createMcpHandler({
|
|
622
|
+
tools: host.registry,
|
|
623
|
+
server: host.server,
|
|
624
|
+
confirm: (request) => host.confirm(request),
|
|
625
|
+
client: (grant) => host.clientFor(grant)
|
|
626
|
+
});
|
|
627
|
+
const stopWatching = host.registry.onChange(() => handler.toolsChanged());
|
|
628
|
+
function send(message) {
|
|
629
|
+
if (ws.readyState !== WebSocket.OPEN)
|
|
630
|
+
return false;
|
|
631
|
+
try {
|
|
632
|
+
ws.send(JSON.stringify(message));
|
|
633
|
+
return true;
|
|
634
|
+
} catch {
|
|
635
|
+
return false;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
function armHeartbeat() {
|
|
639
|
+
if (heartbeatMs <= 0)
|
|
640
|
+
return;
|
|
641
|
+
clearTimeout(heartbeat);
|
|
642
|
+
heartbeat = setTimeout(() => {
|
|
643
|
+
byeReason ??= "heartbeat";
|
|
644
|
+
finish(4003);
|
|
645
|
+
try {
|
|
646
|
+
ws.close();
|
|
647
|
+
} catch {}
|
|
648
|
+
}, heartbeatMs * 2.5);
|
|
649
|
+
}
|
|
650
|
+
function fail(error) {
|
|
651
|
+
if (state !== "opening")
|
|
652
|
+
return;
|
|
653
|
+
finish(0);
|
|
654
|
+
rejectReady(error);
|
|
655
|
+
try {
|
|
656
|
+
ws.close();
|
|
657
|
+
} catch {}
|
|
658
|
+
}
|
|
659
|
+
function finish(code) {
|
|
660
|
+
if (state === "closed")
|
|
661
|
+
return;
|
|
662
|
+
const wasReady = state === "ready";
|
|
663
|
+
state = "closed";
|
|
664
|
+
clearTimeout(connectTimer);
|
|
665
|
+
clearTimeout(heartbeat);
|
|
666
|
+
stopWatching();
|
|
667
|
+
for (const controller of inflight.values())
|
|
668
|
+
controller.abort();
|
|
669
|
+
inflight.clear();
|
|
670
|
+
handler.close();
|
|
671
|
+
if (wasReady) {
|
|
672
|
+
const reason = byeReason ?? CLOSE_REASONS[code] ?? (code === 1006 ? "network" : "closed");
|
|
673
|
+
host.closed(reason, code);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
async function tunnel(msg) {
|
|
677
|
+
const controller = new AbortController;
|
|
678
|
+
inflight.set(msg.id, controller);
|
|
679
|
+
const live = () => state === "ready" && inflight.get(msg.id) === controller && !controller.signal.aborted;
|
|
680
|
+
try {
|
|
681
|
+
const request = new Request(`https://tab.invalid${msg.path}`, {
|
|
682
|
+
method: msg.method,
|
|
683
|
+
headers: msg.headers,
|
|
684
|
+
signal: controller.signal
|
|
685
|
+
});
|
|
686
|
+
const response = await handler.handle(request, {
|
|
687
|
+
grant: msg.grant,
|
|
688
|
+
body: msg.body
|
|
689
|
+
});
|
|
690
|
+
if (!live()) {
|
|
691
|
+
await response.body?.cancel().catch(() => {});
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
const headers = {};
|
|
695
|
+
for (const name of RESPONSE_HEADERS) {
|
|
696
|
+
const value = response.headers.get(name);
|
|
697
|
+
if (value !== null)
|
|
698
|
+
headers[name] = value;
|
|
699
|
+
}
|
|
700
|
+
const streamed = (headers["content-type"] ?? "").startsWith("text/event-stream");
|
|
701
|
+
if (!streamed || response.body === null) {
|
|
702
|
+
const body = await response.text();
|
|
703
|
+
if (!live())
|
|
704
|
+
return;
|
|
705
|
+
if (body.length <= maxChunk) {
|
|
706
|
+
send({
|
|
707
|
+
type: "http.response",
|
|
708
|
+
id: msg.id,
|
|
709
|
+
status: response.status,
|
|
710
|
+
headers,
|
|
711
|
+
body
|
|
712
|
+
});
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
send({
|
|
716
|
+
type: "http.response.start",
|
|
717
|
+
id: msg.id,
|
|
718
|
+
status: response.status,
|
|
719
|
+
headers
|
|
720
|
+
});
|
|
721
|
+
for (const data of pieces(body, maxChunk)) {
|
|
722
|
+
if (!live())
|
|
723
|
+
break;
|
|
724
|
+
send({ type: "http.response.chunk", id: msg.id, data });
|
|
725
|
+
}
|
|
726
|
+
if (live())
|
|
727
|
+
send({ type: "http.response.end", id: msg.id });
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
send({
|
|
731
|
+
type: "http.response.start",
|
|
732
|
+
id: msg.id,
|
|
733
|
+
status: response.status,
|
|
734
|
+
headers
|
|
735
|
+
});
|
|
736
|
+
const reader = response.body.getReader();
|
|
737
|
+
const decoder = new TextDecoder;
|
|
738
|
+
controller.signal.addEventListener("abort", () => {
|
|
739
|
+
reader.cancel().catch(() => {});
|
|
740
|
+
});
|
|
741
|
+
for (;; ) {
|
|
742
|
+
const { done, value } = await reader.read();
|
|
743
|
+
if (done || !live())
|
|
744
|
+
break;
|
|
745
|
+
const text = decoder.decode(value, { stream: true });
|
|
746
|
+
for (const data of pieces(text, maxChunk)) {
|
|
747
|
+
send({ type: "http.response.chunk", id: msg.id, data });
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
if (live())
|
|
751
|
+
send({ type: "http.response.end", id: msg.id });
|
|
752
|
+
} catch (error) {
|
|
753
|
+
if (live()) {
|
|
754
|
+
host.error(error instanceof Error ? error : new Error(String(error)));
|
|
755
|
+
send({
|
|
756
|
+
type: "http.response",
|
|
757
|
+
id: msg.id,
|
|
758
|
+
status: 500,
|
|
759
|
+
headers: { "content-type": "application/json" },
|
|
760
|
+
body: JSON.stringify({
|
|
761
|
+
jsonrpc: "2.0",
|
|
762
|
+
id: null,
|
|
763
|
+
error: { code: -32603, message: "Internal server error" }
|
|
764
|
+
})
|
|
765
|
+
});
|
|
766
|
+
}
|
|
767
|
+
} finally {
|
|
768
|
+
if (inflight.get(msg.id) === controller)
|
|
769
|
+
inflight.delete(msg.id);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
function onMessage(message) {
|
|
773
|
+
if (state === "opening") {
|
|
774
|
+
if (message.type === "session") {
|
|
775
|
+
const chunk = chunkSize(message.limits.maxMessageBytes);
|
|
776
|
+
if (chunk === undefined) {
|
|
777
|
+
fail(new Error(`The relay's message limit (${message.limits.maxMessageBytes} bytes) is too small; tab-mcp needs at least ${MIN_MESSAGE_BYTES}.`));
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
maxChunk = chunk;
|
|
781
|
+
state = "ready";
|
|
782
|
+
clearTimeout(connectTimer);
|
|
783
|
+
heartbeatMs = message.limits.heartbeatMs;
|
|
784
|
+
armHeartbeat();
|
|
785
|
+
resolveReady(host.sessionStarted(message));
|
|
786
|
+
} else if (message.type === "error") {
|
|
787
|
+
fail(new Error(`The relay refused the connection: ${message.message || message.code}`));
|
|
788
|
+
} else if (message.type === "bye") {
|
|
789
|
+
fail(new Error(`The relay ended the connection: ${message.reason}`));
|
|
790
|
+
}
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
if (state !== "ready")
|
|
794
|
+
return;
|
|
795
|
+
switch (message.type) {
|
|
796
|
+
case "ping":
|
|
797
|
+
armHeartbeat();
|
|
798
|
+
send({ type: "pong", t: message.t });
|
|
799
|
+
return;
|
|
800
|
+
case "http.request":
|
|
801
|
+
tunnel(message);
|
|
802
|
+
return;
|
|
803
|
+
case "http.cancel":
|
|
804
|
+
inflight.get(message.id)?.abort();
|
|
805
|
+
inflight.delete(message.id);
|
|
806
|
+
return;
|
|
807
|
+
case "pairing.code":
|
|
808
|
+
host.pairingCode({ code: message.code, expiresAt: message.expiresAt });
|
|
809
|
+
return;
|
|
810
|
+
case "grant.added":
|
|
811
|
+
host.grantAdded(message.grant);
|
|
812
|
+
return;
|
|
813
|
+
case "grant.removed":
|
|
814
|
+
host.grantRemoved(message.grantId);
|
|
815
|
+
return;
|
|
816
|
+
case "bye":
|
|
817
|
+
byeReason = BYE_REASONS[message.reason] ?? "closed";
|
|
818
|
+
if (byeReason === "replaced")
|
|
819
|
+
host.replaced();
|
|
820
|
+
return;
|
|
821
|
+
case "error":
|
|
822
|
+
host.error(new Error(`Relay error ${message.code}: ${message.message}`));
|
|
823
|
+
return;
|
|
824
|
+
case "session":
|
|
825
|
+
return;
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
ws.addEventListener("open", () => {
|
|
829
|
+
if (ws.protocol !== "" && ws.protocol !== SUBPROTOCOL) {
|
|
830
|
+
fail(new Error(`The relay speaks ${ws.protocol}, not ${SUBPROTOCOL}.`));
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
const hello = {
|
|
834
|
+
type: "hello",
|
|
835
|
+
library: host.library,
|
|
836
|
+
page: { title: pageTitle() },
|
|
837
|
+
secret
|
|
838
|
+
};
|
|
839
|
+
if (host.appName !== undefined)
|
|
840
|
+
hello.app = { name: host.appName };
|
|
841
|
+
send(hello);
|
|
842
|
+
});
|
|
843
|
+
ws.addEventListener("message", (event) => {
|
|
844
|
+
if (typeof event.data !== "string")
|
|
845
|
+
return;
|
|
846
|
+
const message = parseRelayMessage(event.data);
|
|
847
|
+
if (message !== undefined) {
|
|
848
|
+
if (state === "ready")
|
|
849
|
+
armHeartbeat();
|
|
850
|
+
onMessage(message);
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
const info = frameInfo(event.data);
|
|
854
|
+
if (info?.id !== undefined) {
|
|
855
|
+
send(KNOWN_TYPES.has(info.type) ? {
|
|
856
|
+
type: "error",
|
|
857
|
+
code: "bad_message",
|
|
858
|
+
message: `malformed ${info.type}`,
|
|
859
|
+
id: info.id
|
|
860
|
+
} : {
|
|
861
|
+
type: "error",
|
|
862
|
+
code: "unknown_type",
|
|
863
|
+
message: `unknown type ${info.type}`,
|
|
864
|
+
id: info.id
|
|
865
|
+
});
|
|
866
|
+
}
|
|
867
|
+
});
|
|
868
|
+
ws.addEventListener("close", (event) => {
|
|
869
|
+
if (state === "opening") {
|
|
870
|
+
finish(event.code);
|
|
871
|
+
rejectReady(new Error(event.code === 1006 ? "Could not connect to the relay (refused, unreachable, or this page's origin is not allowed)." : `The relay closed the connection (${event.code}${event.reason ? `: ${event.reason}` : ""}).`));
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
finish(event.code);
|
|
875
|
+
});
|
|
876
|
+
return {
|
|
877
|
+
ready,
|
|
878
|
+
send,
|
|
879
|
+
close(reason) {
|
|
880
|
+
if (state === "closed")
|
|
881
|
+
return;
|
|
882
|
+
if (state === "opening") {
|
|
883
|
+
fail(new DOMException("The connection was cancelled", "AbortError"));
|
|
884
|
+
return;
|
|
885
|
+
}
|
|
886
|
+
byeReason = reason;
|
|
887
|
+
send({ type: "bye", reason: "user" });
|
|
888
|
+
finish(1000);
|
|
889
|
+
try {
|
|
890
|
+
ws.close(1000, "bye");
|
|
891
|
+
} catch {}
|
|
892
|
+
}
|
|
893
|
+
};
|
|
894
|
+
}
|
|
895
|
+
export {
|
|
896
|
+
openConnection
|
|
897
|
+
};
|
|
898
|
+
|
|
899
|
+
//# debugId=A284BD4DD9FAEEA364756E2164756E21
|
|
900
|
+
//# sourceMappingURL=connection-1gf7vqz2.js.map
|