surfcad-mcp 0.3.4 → 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -12
- package/data/commands.json +453 -57
- package/data/helpers.json +41 -9
- package/package.json +1 -1
- package/src/bridge/relayHub.mjs +5 -2
- package/src/errors.mjs +43 -0
- package/src/helpers.mjs +1 -1
- package/src/prompt/buildGuidance.mjs +3 -2
- package/src/prompt/errorHints.mjs +7 -4
- package/src/prompt/guide.mjs +94 -7
- package/src/prompt/promptSession.mjs +127 -11
- package/src/prompt/promptTools.mjs +46 -6
- package/src/relayMessages.mjs +12 -0
- package/src/remote/httpFallback.mjs +11 -5
- package/src/remote/processExit.mjs +137 -0
- package/src/remote/session.mjs +208 -33
- package/src/server.mjs +14 -4
- package/src/unlistedTools.mjs +32 -0
package/src/remote/session.mjs
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
* The bearer token is never logged and is never placed in the URL.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
import { randomUUID } from 'node:crypto';
|
|
7
8
|
import { WebSocket } from 'ws';
|
|
8
9
|
import {
|
|
9
10
|
AGENT_HANDSHAKE_UPDATE_MESSAGE,
|
|
@@ -13,7 +14,9 @@ import {
|
|
|
13
14
|
import {
|
|
14
15
|
asRemoteError,
|
|
15
16
|
clientUpdateError,
|
|
17
|
+
authErrorFromBody,
|
|
16
18
|
pairingLostError,
|
|
19
|
+
tokenExpiredError,
|
|
17
20
|
tokenRevokedError,
|
|
18
21
|
} from '../errors.mjs';
|
|
19
22
|
import { PAIR_MSG } from '../relayMessages.mjs';
|
|
@@ -44,15 +47,30 @@ function openAuthorizedSocket(wsUrl, token) {
|
|
|
44
47
|
};
|
|
45
48
|
ws.once('unexpected-response', (_req, res) => {
|
|
46
49
|
const status = res.statusCode || 0;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
50
|
+
const chunks = [];
|
|
51
|
+
const failStatus = (json) => {
|
|
52
|
+
if (status === 401) {
|
|
53
|
+
fail(authErrorFromBody(json));
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
fail(Object.assign(new Error(`WebSocket upgrade failed (${status})`), {
|
|
57
|
+
code: 'ws_blocked',
|
|
58
|
+
statusCode: status,
|
|
59
|
+
}));
|
|
60
|
+
};
|
|
61
|
+
res.on('data', (chunk) => {
|
|
62
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
63
|
+
});
|
|
64
|
+
res.on('error', () => failStatus(null));
|
|
65
|
+
res.on('end', () => {
|
|
66
|
+
let json = null;
|
|
67
|
+
try {
|
|
68
|
+
json = JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
|
69
|
+
} catch {
|
|
70
|
+
json = null;
|
|
71
|
+
}
|
|
72
|
+
failStatus(json);
|
|
73
|
+
});
|
|
56
74
|
});
|
|
57
75
|
ws.once('error', (error) => {
|
|
58
76
|
const errno = error && typeof error === 'object' ? error.code : '';
|
|
@@ -86,6 +104,48 @@ function defaultLog(line) {
|
|
|
86
104
|
console.error(`${new Date().toISOString()} [surfcad-mcp] ${line}`);
|
|
87
105
|
}
|
|
88
106
|
|
|
107
|
+
/**
|
|
108
|
+
* Newlines and other controls become spaces so one close stays one log line.
|
|
109
|
+
* @param {string} text
|
|
110
|
+
*/
|
|
111
|
+
function singleLine(text) {
|
|
112
|
+
let out = '';
|
|
113
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
114
|
+
out += text.charCodeAt(i) < 32 ? ' ' : text[i];
|
|
115
|
+
}
|
|
116
|
+
return out;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Close reason for stderr. The bearer, this session's token, a join code,
|
|
121
|
+
* and any other long token-shaped run are removed. Newlines cannot break
|
|
122
|
+
* the line into a second record.
|
|
123
|
+
* @param {Buffer | string | undefined} reason
|
|
124
|
+
* @param {string} [token]
|
|
125
|
+
*/
|
|
126
|
+
function clipCloseReason(reason, token) {
|
|
127
|
+
let text = Buffer.isBuffer(reason) ? reason.toString('utf8') : String(reason || '');
|
|
128
|
+
if (token) text = text.split(token).join('');
|
|
129
|
+
text = text.replace(/Bearer\s+\S+/gi, '');
|
|
130
|
+
text = text.replace(/\b[A-Z0-9]{4}-[A-Z0-9]{4}\b/g, '');
|
|
131
|
+
text = text.replace(/\b[A-Za-z0-9_-]{32,}\b/g, '');
|
|
132
|
+
text = singleLine(text).replace(/\s+/g, ' ').trim();
|
|
133
|
+
if (text.length > 120) text = text.slice(0, 120);
|
|
134
|
+
return text;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @param {number} code
|
|
139
|
+
* @param {Buffer | string | undefined} reason
|
|
140
|
+
* @param {string} [token]
|
|
141
|
+
*/
|
|
142
|
+
function formatClose(code, reason, token) {
|
|
143
|
+
const numeric = Number(code);
|
|
144
|
+
const shown = Number.isInteger(numeric) ? numeric : 1006;
|
|
145
|
+
const why = clipCloseReason(reason, token);
|
|
146
|
+
return { code: shown, why };
|
|
147
|
+
}
|
|
148
|
+
|
|
89
149
|
export class RemoteSession {
|
|
90
150
|
/** @type {WebSocket | null} */
|
|
91
151
|
#ws = null;
|
|
@@ -102,6 +162,13 @@ export class RemoteSession {
|
|
|
102
162
|
#connecting = null;
|
|
103
163
|
/** @type {{ resolve: () => void, reject: (err: Error) => void } | null} */
|
|
104
164
|
#hello = null;
|
|
165
|
+
/** Frames that arrived before the hello ack. A prompt must not fail the handshake. */
|
|
166
|
+
#early = [];
|
|
167
|
+
#promptPull = 0;
|
|
168
|
+
/** This process's pull session: GET /agent/prompts offers each held prompt to it once. */
|
|
169
|
+
#pullSession = randomUUID().replace(/-/g, '');
|
|
170
|
+
/** @type {AbortController | null} */
|
|
171
|
+
#promptAbort = null;
|
|
105
172
|
/** @type {Map<string, { resolve: (v: unknown) => void, reject: (e: Error) => void }>} */
|
|
106
173
|
#pending = new Map();
|
|
107
174
|
#log;
|
|
@@ -138,6 +205,12 @@ export class RemoteSession {
|
|
|
138
205
|
: COMMAND_WAIT_MS;
|
|
139
206
|
this.#prompts = options.promptSession || createPromptSession({
|
|
140
207
|
send: (payload) => this.#sendToTab(payload),
|
|
208
|
+
// The server holds a tab prompt until a process reports it taken.
|
|
209
|
+
onTaken: (ids) => {
|
|
210
|
+
Promise.resolve()
|
|
211
|
+
.then(() => this.#sendToTab({ type: PAIR_MSG.AGENT_PROMPT_TAKEN, v: 1, ids }))
|
|
212
|
+
.catch(() => { /* advisory; the next hello or pull offers them again */ });
|
|
213
|
+
},
|
|
141
214
|
});
|
|
142
215
|
}
|
|
143
216
|
|
|
@@ -240,6 +313,7 @@ export class RemoteSession {
|
|
|
240
313
|
try {
|
|
241
314
|
try {
|
|
242
315
|
await this.#openWebSocket();
|
|
316
|
+
this.#stopPromptPull();
|
|
243
317
|
this.#transport = 'ws';
|
|
244
318
|
this.#state = 'paired';
|
|
245
319
|
this.#log('remote agent connected via websocket');
|
|
@@ -247,7 +321,7 @@ export class RemoteSession {
|
|
|
247
321
|
} catch (err) {
|
|
248
322
|
this.#dropSocket();
|
|
249
323
|
const code = err && typeof err === 'object' ? err.code : '';
|
|
250
|
-
if (code === 'token_revoked' || code === 'client_update_required') {
|
|
324
|
+
if (code === 'token_revoked' || code === 'expired' || code === 'client_update_required') {
|
|
251
325
|
// Stay 'pairing' so a later tool call reaches `call()` and surfaces this error.
|
|
252
326
|
this.#state = 'pairing';
|
|
253
327
|
throw err;
|
|
@@ -261,6 +335,7 @@ export class RemoteSession {
|
|
|
261
335
|
this.#transport = 'http';
|
|
262
336
|
this.#state = 'paired';
|
|
263
337
|
this.#log('remote agent connected via https fallback');
|
|
338
|
+
this.#startPromptPull();
|
|
264
339
|
}
|
|
265
340
|
} finally {
|
|
266
341
|
// Success used to leave the settled promise here, so a later connect()
|
|
@@ -320,6 +395,7 @@ export class RemoteSession {
|
|
|
320
395
|
const ws = this.#ws;
|
|
321
396
|
this.#ws = null;
|
|
322
397
|
this.#hello = null;
|
|
398
|
+
this.#early = [];
|
|
323
399
|
if (!ws) return;
|
|
324
400
|
ws.removeAllListeners();
|
|
325
401
|
try { ws.terminate(); } catch { /* noop */ }
|
|
@@ -338,12 +414,22 @@ export class RemoteSession {
|
|
|
338
414
|
}
|
|
339
415
|
if (!msg || typeof msg !== 'object') return;
|
|
340
416
|
if (this.#hello) {
|
|
341
|
-
const hello = this.#hello;
|
|
342
|
-
this.#hello = null;
|
|
343
417
|
if (msg.type === 'hello') {
|
|
418
|
+
const hello = this.#hello;
|
|
419
|
+
this.#hello = null;
|
|
344
420
|
hello.resolve();
|
|
421
|
+
const early = this.#early.splice(0);
|
|
422
|
+
for (const frame of early) this.#dispatchInbound(frame);
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
if (msg.type === PAIR_MSG.AGENT_PROMPT || msg.type === PAIR_MSG.AGENT_PROMPT_CANCEL) {
|
|
426
|
+
this.#early.push(msg);
|
|
427
|
+
if (this.#early.length > 20) this.#early.shift();
|
|
345
428
|
return;
|
|
346
429
|
}
|
|
430
|
+
const hello = this.#hello;
|
|
431
|
+
this.#hello = null;
|
|
432
|
+
this.#early = [];
|
|
347
433
|
const text = typeof msg.error === 'string' ? msg.error : '';
|
|
348
434
|
hello.reject(asRemoteError({
|
|
349
435
|
code: text.includes(AGENT_HANDSHAKE_UPDATE_MESSAGE) ? 'client_update_required' : 'ws_blocked',
|
|
@@ -351,24 +437,7 @@ export class RemoteSession {
|
|
|
351
437
|
}));
|
|
352
438
|
return;
|
|
353
439
|
}
|
|
354
|
-
if (msg
|
|
355
|
-
void this.#prompts.acceptPrompt(msg);
|
|
356
|
-
return;
|
|
357
|
-
}
|
|
358
|
-
if (msg.type === PAIR_MSG.AGENT_PROMPT_CANCEL) {
|
|
359
|
-
const cancelId = typeof msg.id === 'string' ? msg.id : '';
|
|
360
|
-
void this.#prompts.cancel(cancelId).then((result) => {
|
|
361
|
-
Promise.resolve(this.#sendToTab({
|
|
362
|
-
type: PAIR_MSG.AGENT_PROMPT_CANCEL_RESULT,
|
|
363
|
-
v: 1,
|
|
364
|
-
id: result.id,
|
|
365
|
-
state: result.state,
|
|
366
|
-
})).catch(() => {
|
|
367
|
-
/* socket closed before the result could be sent */
|
|
368
|
-
});
|
|
369
|
-
}, () => {});
|
|
370
|
-
return;
|
|
371
|
-
}
|
|
440
|
+
if (this.#dispatchInbound(msg)) return;
|
|
372
441
|
const id = msg.id == null ? '' : String(msg.id);
|
|
373
442
|
const waiter = id ? this.#pending.get(id) : undefined;
|
|
374
443
|
if (!waiter) return;
|
|
@@ -385,13 +454,19 @@ export class RemoteSession {
|
|
|
385
454
|
#onClose(code, reason) {
|
|
386
455
|
const why = Buffer.isBuffer(reason) ? reason.toString('utf8') : String(reason || '');
|
|
387
456
|
const hello = this.#hello;
|
|
457
|
+
const closed = formatClose(code, reason, this.#token);
|
|
458
|
+
if (hello) {
|
|
459
|
+
const detail = closed.why ? ` ${closed.why}` : '';
|
|
460
|
+
this.#log(`websocket closed before hello (${closed.code})${detail}`);
|
|
461
|
+
}
|
|
388
462
|
this.#hello = null;
|
|
463
|
+
this.#early = [];
|
|
389
464
|
this.#ws = null;
|
|
390
465
|
if (code === TOKEN_CLOSE_CODE) {
|
|
391
466
|
this.#transport = null;
|
|
392
467
|
this.#state = 'pairing';
|
|
393
468
|
this.#connecting = null;
|
|
394
|
-
const err = tokenRevokedError();
|
|
469
|
+
const err = why.trim() === 'token expired' ? tokenExpiredError() : tokenRevokedError();
|
|
395
470
|
if (hello) hello.reject(err);
|
|
396
471
|
this.#failPending(err);
|
|
397
472
|
return;
|
|
@@ -411,9 +486,108 @@ export class RemoteSession {
|
|
|
411
486
|
}
|
|
412
487
|
if (this.#state === 'paired' && this.#transport === 'ws') {
|
|
413
488
|
this.#transport = 'http';
|
|
414
|
-
|
|
489
|
+
const detail = closed.why ? ` ${closed.why}` : '';
|
|
490
|
+
this.#log(`websocket closed (${closed.code})${detail}; later commands use https`);
|
|
415
491
|
this.#failPending(pairingLostError('socket closed'));
|
|
492
|
+
this.#startPromptPull();
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Prompt frames from the agent socket or from GET /agent/prompts.
|
|
498
|
+
* @param {object} msg
|
|
499
|
+
* @returns {boolean} true when this frame was a prompt
|
|
500
|
+
*/
|
|
501
|
+
#dispatchInbound(msg) {
|
|
502
|
+
if (msg.type === PAIR_MSG.AGENT_PROMPT) {
|
|
503
|
+
void this.#prompts.acceptPrompt(msg);
|
|
504
|
+
return true;
|
|
505
|
+
}
|
|
506
|
+
if (msg.type === PAIR_MSG.AGENT_PROMPT_CANCEL) {
|
|
507
|
+
const cancelId = typeof msg.id === 'string' ? msg.id : '';
|
|
508
|
+
void this.#prompts.cancel(cancelId).then((result) => {
|
|
509
|
+
Promise.resolve(this.#sendToTab({
|
|
510
|
+
type: PAIR_MSG.AGENT_PROMPT_CANCEL_RESULT,
|
|
511
|
+
v: 1,
|
|
512
|
+
id: result.id,
|
|
513
|
+
state: result.state,
|
|
514
|
+
})).catch(() => {
|
|
515
|
+
/* socket closed before the result could be sent */
|
|
516
|
+
});
|
|
517
|
+
}, () => {});
|
|
518
|
+
return true;
|
|
519
|
+
}
|
|
520
|
+
return false;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
#stopPromptPull() {
|
|
524
|
+
this.#promptPull += 1;
|
|
525
|
+
const abort = this.#promptAbort;
|
|
526
|
+
this.#promptAbort = null;
|
|
527
|
+
try { abort?.abort(); } catch { /* noop */ }
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* HTTPS fallback has no agent socket, so tab prompts are pulled.
|
|
532
|
+
* A websocket session receives them on the socket instead.
|
|
533
|
+
*/
|
|
534
|
+
#startPromptPull() {
|
|
535
|
+
this.#log('prompt poll started');
|
|
536
|
+
this.#stopPromptPull();
|
|
537
|
+
const generation = this.#promptPull;
|
|
538
|
+
const run = async () => {
|
|
539
|
+
while (generation === this.#promptPull && this.#state === 'paired' && this.#transport === 'http') {
|
|
540
|
+
try {
|
|
541
|
+
const frames = await this.#pullPromptBatch();
|
|
542
|
+
if (generation !== this.#promptPull) return;
|
|
543
|
+
for (const frame of frames) this.#dispatchInbound(frame);
|
|
544
|
+
} catch (err) {
|
|
545
|
+
if (generation !== this.#promptPull) return;
|
|
546
|
+
const code = err && typeof err === 'object' ? err.code : '';
|
|
547
|
+
if (code === 'token_revoked' || code === 'expired' || code === 'client_update_required') return;
|
|
548
|
+
await new Promise((resolve) => {
|
|
549
|
+
const timer = setTimeout(resolve, 400);
|
|
550
|
+
if (typeof timer.unref === 'function') timer.unref();
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
void run();
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* @returns {Promise<object[]>}
|
|
560
|
+
*/
|
|
561
|
+
async #pullPromptBatch() {
|
|
562
|
+
const ac = new AbortController();
|
|
563
|
+
this.#promptAbort = ac;
|
|
564
|
+
const url = new URL(this.#urls.promptsUrl);
|
|
565
|
+
url.searchParams.set('timeout', '25000');
|
|
566
|
+
url.searchParams.set('session', this.#pullSession);
|
|
567
|
+
let res;
|
|
568
|
+
try {
|
|
569
|
+
res = await this.#fetch(url, {
|
|
570
|
+
method: 'GET',
|
|
571
|
+
headers: { Authorization: `Bearer ${this.#token}` },
|
|
572
|
+
signal: ac.signal,
|
|
573
|
+
});
|
|
574
|
+
} catch {
|
|
575
|
+
if (ac.signal.aborted) return [];
|
|
576
|
+
throw Object.assign(new Error('Could not reach the SurfCAD agent server.'), { code: 'unreachable' });
|
|
577
|
+
}
|
|
578
|
+
if (res.status === 401) {
|
|
579
|
+
let json = null;
|
|
580
|
+
try { json = await res.json(); } catch { json = null; }
|
|
581
|
+
throw authErrorFromBody(json);
|
|
582
|
+
}
|
|
583
|
+
if (!res.ok) {
|
|
584
|
+
try { await res.body?.cancel?.(); } catch { /* noop */ }
|
|
585
|
+
throw Object.assign(new Error(`Prompt pull failed (${res.status})`), { code: 'bridge_error' });
|
|
416
586
|
}
|
|
587
|
+
let json = null;
|
|
588
|
+
try { json = await res.json(); } catch { json = null; }
|
|
589
|
+
const messages = json && Array.isArray(json.messages) ? json.messages : [];
|
|
590
|
+
return messages.filter((frame) => frame && typeof frame === 'object');
|
|
417
591
|
}
|
|
418
592
|
|
|
419
593
|
/**
|
|
@@ -464,7 +638,7 @@ export class RemoteSession {
|
|
|
464
638
|
if (!res.ok) {
|
|
465
639
|
let json = null;
|
|
466
640
|
try { json = await res.json(); } catch { json = null; }
|
|
467
|
-
if (res.status === 401) throw
|
|
641
|
+
if (res.status === 401) throw authErrorFromBody(json);
|
|
468
642
|
const code = typeof json?.error === 'string' && json.error ? json.error : 'bridge_error';
|
|
469
643
|
const message = typeof json?.message === 'string' && json.message
|
|
470
644
|
? json.message
|
|
@@ -525,6 +699,7 @@ export class RemoteSession {
|
|
|
525
699
|
|
|
526
700
|
disconnect() {
|
|
527
701
|
this.#connecting = null;
|
|
702
|
+
this.#stopPromptPull();
|
|
528
703
|
this.#dropSocket();
|
|
529
704
|
this.#transport = null;
|
|
530
705
|
this.#state = 'unpaired';
|
package/src/server.mjs
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
} from './bridge/relayServer.mjs';
|
|
23
23
|
import { RelaySession } from './relay.mjs';
|
|
24
24
|
import { resolveAgentName, resolveServerUrl } from './remote/config.mjs';
|
|
25
|
+
import { handleRemoteStdinEnd, installRemoteProcessExitLogs } from './remote/processExit.mjs';
|
|
25
26
|
import { RemoteSession } from './remote/session.mjs';
|
|
26
27
|
import { readSavedLogin } from './remote/tokenStore.mjs';
|
|
27
28
|
import { waitForPairing } from './pairWait.mjs';
|
|
@@ -45,6 +46,7 @@ import {
|
|
|
45
46
|
import { withErrorHint, withRowHints } from './prompt/errorHints.mjs';
|
|
46
47
|
import { callListHelpersTool, LIST_HELPERS_TOOL_NAME, listHelpersToolDefinition } from './helpers.mjs';
|
|
47
48
|
import { mergeRegisteredIntoListCommands } from './listCommandsMerge.mjs';
|
|
49
|
+
import { tabAdvertisesTool } from './unlistedTools.mjs';
|
|
48
50
|
|
|
49
51
|
const PAIR_TOOL_NAME = 'pair';
|
|
50
52
|
|
|
@@ -203,6 +205,8 @@ export async function createSurfcadMcpServer(options = {}) {
|
|
|
203
205
|
const session = options.session || new RelaySession();
|
|
204
206
|
const bridgeNames = new Set(catalog.commands.map((command) => command.name));
|
|
205
207
|
const bridgeTools = bridgeToolDefinitions(catalog).map((tool) => augmentStatusTool(augmentScreenshotTool(tool)));
|
|
208
|
+
/** Names outside the shipped catalog the paired tab was asked about (tabAdvertisesTool). */
|
|
209
|
+
const unlistedToolCache = new Map();
|
|
206
210
|
const mcpOnlyNames = registeredToolDefinitions(bridgeTools)
|
|
207
211
|
.map((tool) => tool.name)
|
|
208
212
|
.filter((name) => !bridgeNames.has(name));
|
|
@@ -253,7 +257,7 @@ export async function createSurfcadMcpServer(options = {}) {
|
|
|
253
257
|
}
|
|
254
258
|
|
|
255
259
|
if (isPromptTool(name)) {
|
|
256
|
-
if (name !== 'set_status') session.noteAgentToolCall();
|
|
260
|
+
if (name !== 'set_status' && name !== 'post_progress' && name !== 'close_session') session.noteAgentToolCall();
|
|
257
261
|
try {
|
|
258
262
|
const out = await callPromptTool(session.prompts, name, args);
|
|
259
263
|
if (out.isError) return errorResult(out.error, name);
|
|
@@ -282,7 +286,10 @@ export async function createSurfcadMcpServer(options = {}) {
|
|
|
282
286
|
}
|
|
283
287
|
|
|
284
288
|
const known = bridgeTools.some((t) => t.name === name);
|
|
285
|
-
|
|
289
|
+
// A name this package's catalog lacks is refused outright when nothing
|
|
290
|
+
// is paired; a paired tab is asked whether it advertises it (a tool the
|
|
291
|
+
// app shipped after this npm release) and the call is forwarded if so.
|
|
292
|
+
if (!known && !session.isPaired()) {
|
|
286
293
|
return errorResult({ code: 'unknown_tool', message: `Unknown tool: ${name}` });
|
|
287
294
|
}
|
|
288
295
|
|
|
@@ -300,6 +307,9 @@ export async function createSurfcadMcpServer(options = {}) {
|
|
|
300
307
|
return errorResult(notPairedError(), name);
|
|
301
308
|
}
|
|
302
309
|
}
|
|
310
|
+
if (!known && !(await tabAdvertisesTool(session, name, unlistedToolCache))) {
|
|
311
|
+
return errorResult({ code: 'unknown_tool', message: `Unknown tool: ${name}` });
|
|
312
|
+
}
|
|
303
313
|
|
|
304
314
|
try {
|
|
305
315
|
// A model-changing tool enters building if set_status has not already.
|
|
@@ -444,9 +454,9 @@ async function runRemoteMcpServer(options) {
|
|
|
444
454
|
});
|
|
445
455
|
}
|
|
446
456
|
|
|
457
|
+
installRemoteProcessExitLogs();
|
|
447
458
|
const onStdinClosed = () => {
|
|
448
|
-
remote
|
|
449
|
-
setTimeout(() => process.exit(0), 0).unref();
|
|
459
|
+
handleRemoteStdinEnd(remote);
|
|
450
460
|
};
|
|
451
461
|
process.stdin.once('end', onStdinClosed);
|
|
452
462
|
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A tool the paired tab advertises but this package's catalog does not.
|
|
3
|
+
*
|
|
4
|
+
* `data/commands.json` ships with the npm release, so a tool the app added
|
|
5
|
+
* after that release (`insert_boss` at 0.3.4) is "Unknown tool" from an
|
|
6
|
+
* older `surfcad-mcp` even though the tab's `list_commands` lists it. When a
|
|
7
|
+
* paired session gets a name the catalog lacks, ask the tab once whether it
|
|
8
|
+
* has that command; forward the call when it does. Answers are cached per
|
|
9
|
+
* process. Not paired, or a tab that does not know the name either, stays
|
|
10
|
+
* `unknown_tool`.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {{ call: (name: string, args: object) => Promise<unknown> }} session
|
|
15
|
+
* @param {string} name
|
|
16
|
+
* @param {Map<string, boolean>} cache
|
|
17
|
+
* @returns {Promise<boolean>}
|
|
18
|
+
*/
|
|
19
|
+
export async function tabAdvertisesTool(session, name, cache) {
|
|
20
|
+
if (typeof name !== 'string' || !name) return false;
|
|
21
|
+
if (cache.has(name)) return cache.get(name) === true;
|
|
22
|
+
let known = false;
|
|
23
|
+
try {
|
|
24
|
+
const listed = await session.call('list_commands', { names: [name] });
|
|
25
|
+
const rows = listed && typeof listed === 'object' && Array.isArray(listed.commands) ? listed.commands : [];
|
|
26
|
+
known = rows.some((row) => row && row.name === name);
|
|
27
|
+
} catch {
|
|
28
|
+
known = false;
|
|
29
|
+
}
|
|
30
|
+
cache.set(name, known);
|
|
31
|
+
return known;
|
|
32
|
+
}
|