surfcad-mcp 0.3.3 → 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/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
- if (!known) {
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.disconnect();
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
+ }