@mcp-fe/mcp-worker 0.0.16 → 0.0.17
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/index.js +153 -33
- package/mcp-service-worker.js +102 -52
- package/mcp-shared-worker.js +181 -74
- package/package.json +1 -1
- package/src/index.d.ts +2 -1
- package/src/index.d.ts.map +1 -1
- package/src/lib/logger.d.ts +17 -0
- package/src/lib/logger.d.ts.map +1 -0
- package/src/lib/mcp-controller.d.ts +1 -0
- package/src/lib/mcp-controller.d.ts.map +1 -1
- package/src/lib/worker-client.d.ts.map +1 -1
package/LICENSE
CHANGED
|
@@ -188,7 +188,7 @@
|
|
|
188
188
|
same page as the copyright notice for easier identification within
|
|
189
189
|
third-party archives.
|
|
190
190
|
|
|
191
|
-
Copyright 2026
|
|
191
|
+
Copyright 2026 Michal Kopecky
|
|
192
192
|
|
|
193
193
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
194
194
|
you may not use this file except in compliance with the License.
|
package/index.js
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
// libs/mcp-worker/src/lib/logger.ts
|
|
2
|
+
var isProduction = typeof process !== "undefined" && true;
|
|
3
|
+
var mcpDebug = typeof process !== "undefined" && process.env?.["MCP_DEBUG"];
|
|
4
|
+
var debugEnabled = mcpDebug === "true" || !isProduction && mcpDebug !== "false";
|
|
5
|
+
var logger = {
|
|
6
|
+
log: (...args) => {
|
|
7
|
+
if (debugEnabled) {
|
|
8
|
+
console.log(...args);
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
debug: (...args) => {
|
|
12
|
+
if (debugEnabled) {
|
|
13
|
+
console.debug(...args);
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
info: (...args) => {
|
|
17
|
+
console.info(...args);
|
|
18
|
+
},
|
|
19
|
+
error: (...args) => {
|
|
20
|
+
console.error(...args);
|
|
21
|
+
},
|
|
22
|
+
warn: (...args) => {
|
|
23
|
+
if (debugEnabled) {
|
|
24
|
+
console.warn(...args);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
1
29
|
// libs/mcp-worker/src/lib/worker-client.ts
|
|
2
30
|
var WorkerClient = class {
|
|
3
31
|
// Configurable worker script URLs (defaults kept for backward compatibility)
|
|
@@ -17,12 +45,20 @@ var WorkerClient = class {
|
|
|
17
45
|
// Initialize and choose worker implementation (prefer SharedWorker)
|
|
18
46
|
// Accept either a ServiceWorkerRegistration OR WorkerInitOptions to configure URLs
|
|
19
47
|
async init(registrationOrOptions) {
|
|
48
|
+
logger.log("[WorkerClient] init() called", {
|
|
49
|
+
hasOptions: !!registrationOrOptions,
|
|
50
|
+
currentWorkerType: this.workerType,
|
|
51
|
+
initInProgress: !!this.initPromise,
|
|
52
|
+
timestamp: Date.now()
|
|
53
|
+
});
|
|
20
54
|
let explicitRegistration;
|
|
21
55
|
const maybeReg = registrationOrOptions;
|
|
22
56
|
if (maybeReg && typeof maybeReg.scope === "string") {
|
|
23
57
|
explicitRegistration = registrationOrOptions;
|
|
58
|
+
logger.log("[WorkerClient] Using explicit ServiceWorker registration");
|
|
24
59
|
} else if (registrationOrOptions) {
|
|
25
60
|
const opts = registrationOrOptions;
|
|
61
|
+
logger.log("[WorkerClient] Using WorkerClientInitOptions:", opts);
|
|
26
62
|
if (opts.sharedWorkerUrl)
|
|
27
63
|
this.sharedWorkerUrl = opts.sharedWorkerUrl;
|
|
28
64
|
if (opts.serviceWorkerUrl)
|
|
@@ -42,11 +78,14 @@ var WorkerClient = class {
|
|
|
42
78
|
if (explicitRegistration) {
|
|
43
79
|
this.serviceWorkerRegistration = explicitRegistration;
|
|
44
80
|
this.workerType = "service";
|
|
45
|
-
|
|
81
|
+
logger.info(
|
|
46
82
|
"[WorkerClient] Using ServiceWorker (explicit registration)"
|
|
47
83
|
);
|
|
48
84
|
try {
|
|
49
|
-
const initMsg = {
|
|
85
|
+
const initMsg = {
|
|
86
|
+
type: "INIT",
|
|
87
|
+
backendUrl: this.backendWsUrl
|
|
88
|
+
};
|
|
50
89
|
if (this.pendingAuthToken)
|
|
51
90
|
initMsg["token"] = this.pendingAuthToken;
|
|
52
91
|
if (this.serviceWorkerRegistration.active) {
|
|
@@ -110,13 +149,19 @@ var WorkerClient = class {
|
|
|
110
149
|
const portAfterInit = this.sharedWorkerPort;
|
|
111
150
|
if (portAfterInit) {
|
|
112
151
|
try {
|
|
113
|
-
const initMsg = {
|
|
152
|
+
const initMsg = {
|
|
153
|
+
type: "INIT",
|
|
154
|
+
backendUrl: this.backendWsUrl
|
|
155
|
+
};
|
|
114
156
|
if (this.pendingAuthToken)
|
|
115
157
|
initMsg["token"] = this.pendingAuthToken;
|
|
116
158
|
portAfterInit.postMessage(initMsg);
|
|
117
159
|
this.pendingAuthToken = null;
|
|
118
160
|
} catch (e) {
|
|
119
|
-
|
|
161
|
+
logger.warn(
|
|
162
|
+
"[WorkerClient] Failed to send INIT to SharedWorker port:",
|
|
163
|
+
e
|
|
164
|
+
);
|
|
120
165
|
}
|
|
121
166
|
portAfterInit.onmessage = (ev) => {
|
|
122
167
|
try {
|
|
@@ -134,10 +179,10 @@ var WorkerClient = class {
|
|
|
134
179
|
}
|
|
135
180
|
};
|
|
136
181
|
}
|
|
137
|
-
|
|
182
|
+
logger.info("[WorkerClient] Using SharedWorker");
|
|
138
183
|
return true;
|
|
139
184
|
} catch (error) {
|
|
140
|
-
|
|
185
|
+
logger.warn(
|
|
141
186
|
"[WorkerClient] SharedWorker not available, falling back to ServiceWorker:",
|
|
142
187
|
error
|
|
143
188
|
);
|
|
@@ -151,7 +196,7 @@ var WorkerClient = class {
|
|
|
151
196
|
if (existingRegistration) {
|
|
152
197
|
this.serviceWorkerRegistration = existingRegistration;
|
|
153
198
|
this.workerType = "service";
|
|
154
|
-
|
|
199
|
+
logger.info(
|
|
155
200
|
"[WorkerClient] Using existing ServiceWorker registration"
|
|
156
201
|
);
|
|
157
202
|
return;
|
|
@@ -160,9 +205,12 @@ var WorkerClient = class {
|
|
|
160
205
|
this.serviceWorkerUrl
|
|
161
206
|
);
|
|
162
207
|
this.workerType = "service";
|
|
163
|
-
|
|
208
|
+
logger.info("[WorkerClient] Using MCP ServiceWorker (fallback)");
|
|
164
209
|
try {
|
|
165
|
-
const initMsg = {
|
|
210
|
+
const initMsg = {
|
|
211
|
+
type: "INIT",
|
|
212
|
+
backendUrl: this.backendWsUrl
|
|
213
|
+
};
|
|
166
214
|
if (this.pendingAuthToken)
|
|
167
215
|
initMsg["token"] = this.pendingAuthToken;
|
|
168
216
|
if (this.serviceWorkerRegistration.active) {
|
|
@@ -174,10 +222,7 @@ var WorkerClient = class {
|
|
|
174
222
|
} catch {
|
|
175
223
|
}
|
|
176
224
|
} catch (error) {
|
|
177
|
-
|
|
178
|
-
"[WorkerClient] Failed to register ServiceWorker:",
|
|
179
|
-
error
|
|
180
|
-
);
|
|
225
|
+
logger.error("[WorkerClient] Failed to register ServiceWorker:", error);
|
|
181
226
|
throw error;
|
|
182
227
|
}
|
|
183
228
|
} else {
|
|
@@ -186,32 +231,70 @@ var WorkerClient = class {
|
|
|
186
231
|
}
|
|
187
232
|
// Low-level request that expects a reply via MessageChannel
|
|
188
233
|
async request(type, payload, timeoutMs = 5e3) {
|
|
234
|
+
logger.log("[WorkerClient] Request started:", {
|
|
235
|
+
type,
|
|
236
|
+
payload,
|
|
237
|
+
timeoutMs,
|
|
238
|
+
workerType: this.workerType,
|
|
239
|
+
hasSharedWorkerPort: !!this.sharedWorkerPort,
|
|
240
|
+
hasServiceWorkerReg: !!this.serviceWorkerRegistration
|
|
241
|
+
});
|
|
189
242
|
if (this.workerType === "shared" && this.sharedWorkerPort) {
|
|
190
243
|
return new Promise((resolve, reject) => {
|
|
191
244
|
const mc = new MessageChannel();
|
|
245
|
+
const requestId = Math.random().toString(36).substring(7);
|
|
246
|
+
const startTime = Date.now();
|
|
192
247
|
const timer = setTimeout(() => {
|
|
248
|
+
const elapsed = Date.now() - startTime;
|
|
249
|
+
logger.error("[WorkerClient] Request timeout:", {
|
|
250
|
+
type,
|
|
251
|
+
requestId,
|
|
252
|
+
elapsed,
|
|
253
|
+
timeoutMs
|
|
254
|
+
});
|
|
193
255
|
mc.port1.onmessage = null;
|
|
194
256
|
reject(new Error("Request timeout"));
|
|
195
257
|
}, timeoutMs);
|
|
196
258
|
mc.port1.onmessage = (ev) => {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
259
|
+
try {
|
|
260
|
+
const elapsed = Date.now() - startTime;
|
|
261
|
+
logger.log("[WorkerClient] Request response received:", {
|
|
262
|
+
type,
|
|
263
|
+
requestId,
|
|
264
|
+
elapsed,
|
|
265
|
+
success: ev.data?.success
|
|
266
|
+
});
|
|
267
|
+
clearTimeout(timer);
|
|
268
|
+
if (ev.data && ev.data.success) {
|
|
269
|
+
resolve(ev.data);
|
|
270
|
+
} else if (ev.data && ev.data.success === false) {
|
|
271
|
+
reject(new Error(ev.data.error || "Worker error"));
|
|
272
|
+
} else {
|
|
273
|
+
resolve(ev.data);
|
|
274
|
+
}
|
|
275
|
+
} catch (handlerError) {
|
|
276
|
+
clearTimeout(timer);
|
|
277
|
+
mc.port1.onmessage = null;
|
|
278
|
+
reject(
|
|
279
|
+
handlerError instanceof Error ? handlerError : new Error(String(handlerError))
|
|
280
|
+
);
|
|
204
281
|
}
|
|
205
282
|
};
|
|
206
283
|
try {
|
|
207
284
|
const port = this.sharedWorkerPort;
|
|
208
285
|
if (!port) {
|
|
209
286
|
clearTimeout(timer);
|
|
287
|
+
logger.error("[WorkerClient] SharedWorker port not available");
|
|
210
288
|
return reject(new Error("SharedWorker port not available"));
|
|
211
289
|
}
|
|
290
|
+
logger.log("[WorkerClient] Posting message to SharedWorker:", {
|
|
291
|
+
type,
|
|
292
|
+
requestId
|
|
293
|
+
});
|
|
212
294
|
port.postMessage({ type, ...payload || {} }, [mc.port2]);
|
|
213
295
|
} catch (e) {
|
|
214
296
|
clearTimeout(timer);
|
|
297
|
+
logger.error("[WorkerClient] Failed to post message:", e);
|
|
215
298
|
reject(e instanceof Error ? e : new Error(String(e)));
|
|
216
299
|
}
|
|
217
300
|
});
|
|
@@ -221,6 +304,7 @@ var WorkerClient = class {
|
|
|
221
304
|
if (!reg)
|
|
222
305
|
throw new Error("Service worker registration missing");
|
|
223
306
|
if (!reg.active) {
|
|
307
|
+
logger.log("[WorkerClient] ServiceWorker not active, waiting...");
|
|
224
308
|
await navigator.serviceWorker.ready;
|
|
225
309
|
if (!reg.active) {
|
|
226
310
|
throw new Error("Service worker not active");
|
|
@@ -228,31 +312,66 @@ var WorkerClient = class {
|
|
|
228
312
|
}
|
|
229
313
|
return new Promise((resolve, reject) => {
|
|
230
314
|
const mc = new MessageChannel();
|
|
315
|
+
const requestId = Math.random().toString(36).substring(7);
|
|
316
|
+
const startTime = Date.now();
|
|
231
317
|
const timer = setTimeout(() => {
|
|
318
|
+
const elapsed = Date.now() - startTime;
|
|
319
|
+
logger.error("[WorkerClient] ServiceWorker request timeout:", {
|
|
320
|
+
type,
|
|
321
|
+
requestId,
|
|
322
|
+
elapsed,
|
|
323
|
+
timeoutMs
|
|
324
|
+
});
|
|
232
325
|
mc.port1.onmessage = null;
|
|
233
326
|
reject(new Error("Request timeout"));
|
|
234
327
|
}, timeoutMs);
|
|
235
328
|
mc.port1.onmessage = (ev) => {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
329
|
+
try {
|
|
330
|
+
const elapsed = Date.now() - startTime;
|
|
331
|
+
logger.log("[WorkerClient] ServiceWorker response received:", {
|
|
332
|
+
type,
|
|
333
|
+
requestId,
|
|
334
|
+
elapsed,
|
|
335
|
+
success: ev.data?.success
|
|
336
|
+
});
|
|
337
|
+
clearTimeout(timer);
|
|
338
|
+
if (ev.data && ev.data.success) {
|
|
339
|
+
resolve(ev.data);
|
|
340
|
+
} else if (ev.data && ev.data.success === false) {
|
|
341
|
+
reject(new Error(ev.data.error || "Worker error"));
|
|
342
|
+
} else {
|
|
343
|
+
resolve(ev.data);
|
|
344
|
+
}
|
|
345
|
+
} catch (handlerError) {
|
|
346
|
+
clearTimeout(timer);
|
|
347
|
+
mc.port1.onmessage = null;
|
|
348
|
+
reject(
|
|
349
|
+
handlerError instanceof Error ? handlerError : new Error(String(handlerError))
|
|
350
|
+
);
|
|
243
351
|
}
|
|
244
352
|
};
|
|
245
353
|
try {
|
|
246
354
|
const active = reg.active;
|
|
247
355
|
if (!active) {
|
|
248
356
|
clearTimeout(timer);
|
|
357
|
+
logger.error(
|
|
358
|
+
"[WorkerClient] ServiceWorker active instance not available"
|
|
359
|
+
);
|
|
249
360
|
return reject(
|
|
250
361
|
new Error("Service worker active instance not available")
|
|
251
362
|
);
|
|
252
363
|
}
|
|
364
|
+
logger.log("[WorkerClient] Posting message to ServiceWorker:", {
|
|
365
|
+
type,
|
|
366
|
+
requestId
|
|
367
|
+
});
|
|
253
368
|
active.postMessage({ type, ...payload || {} }, [mc.port2]);
|
|
254
369
|
} catch (e) {
|
|
255
370
|
clearTimeout(timer);
|
|
371
|
+
logger.error(
|
|
372
|
+
"[WorkerClient] Failed to post message to ServiceWorker:",
|
|
373
|
+
e
|
|
374
|
+
);
|
|
256
375
|
reject(e instanceof Error ? e : new Error(String(e)));
|
|
257
376
|
}
|
|
258
377
|
});
|
|
@@ -265,7 +384,7 @@ var WorkerClient = class {
|
|
|
265
384
|
try {
|
|
266
385
|
this.sharedWorkerPort.postMessage({ type, ...payload || {} });
|
|
267
386
|
} catch (e) {
|
|
268
|
-
|
|
387
|
+
logger.error("[WorkerClient] Failed to post to SharedWorker:", e);
|
|
269
388
|
}
|
|
270
389
|
return;
|
|
271
390
|
}
|
|
@@ -276,7 +395,7 @@ var WorkerClient = class {
|
|
|
276
395
|
...payload || {}
|
|
277
396
|
});
|
|
278
397
|
} catch (e) {
|
|
279
|
-
|
|
398
|
+
logger.error(
|
|
280
399
|
"[WorkerClient] Failed to post to ServiceWorker (active):",
|
|
281
400
|
e
|
|
282
401
|
);
|
|
@@ -290,7 +409,7 @@ var WorkerClient = class {
|
|
|
290
409
|
...payload || {}
|
|
291
410
|
});
|
|
292
411
|
} catch (e) {
|
|
293
|
-
|
|
412
|
+
logger.error(
|
|
294
413
|
"[WorkerClient] Failed to post to ServiceWorker.controller:",
|
|
295
414
|
e
|
|
296
415
|
);
|
|
@@ -311,7 +430,7 @@ var WorkerClient = class {
|
|
|
311
430
|
token
|
|
312
431
|
});
|
|
313
432
|
} catch (e) {
|
|
314
|
-
|
|
433
|
+
logger.error(
|
|
315
434
|
"[WorkerClient] Failed to send auth token to ServiceWorker:",
|
|
316
435
|
e
|
|
317
436
|
);
|
|
@@ -323,7 +442,7 @@ var WorkerClient = class {
|
|
|
323
442
|
token
|
|
324
443
|
});
|
|
325
444
|
} catch (e) {
|
|
326
|
-
|
|
445
|
+
logger.error(
|
|
327
446
|
"[WorkerClient] Failed to send auth token to ServiceWorker.controller:",
|
|
328
447
|
e
|
|
329
448
|
);
|
|
@@ -360,7 +479,7 @@ var WorkerClient = class {
|
|
|
360
479
|
this.sharedWorkerPort.postMessage({ type: "SET_AUTH_TOKEN", token });
|
|
361
480
|
this.pendingAuthToken = null;
|
|
362
481
|
} catch (e) {
|
|
363
|
-
|
|
482
|
+
logger.error(
|
|
364
483
|
"[WorkerClient] Failed to set auth token on SharedWorker:",
|
|
365
484
|
e
|
|
366
485
|
);
|
|
@@ -427,6 +546,7 @@ async function queryEvents(filters) {
|
|
|
427
546
|
// libs/mcp-worker/src/index.ts
|
|
428
547
|
var workerClient = new WorkerClient();
|
|
429
548
|
export {
|
|
549
|
+
logger,
|
|
430
550
|
queryEvents,
|
|
431
551
|
workerClient
|
|
432
552
|
};
|