surfcad-mcp 0.3.2 → 0.3.3

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
@@ -12,7 +12,6 @@ import { bridgeToolDefinitions, loadCommandsCatalog } from './catalog.mjs';
12
12
  import {
13
13
  formatBridgeError,
14
14
  notPairedError,
15
- pairPendingError,
16
15
  pairingEnvHint,
17
16
  } from './errors.mjs';
18
17
  import { loadPackageVersion } from './packageVersion.mjs';
@@ -22,6 +21,10 @@ import {
22
21
  stopAgentBridgeRelay,
23
22
  } from './bridge/relayServer.mjs';
24
23
  import { RelaySession } from './relay.mjs';
24
+ import { resolveAgentName, resolveServerUrl } from './remote/config.mjs';
25
+ import { RemoteSession } from './remote/session.mjs';
26
+ import { readSavedLogin } from './remote/tokenStore.mjs';
27
+ import { waitForPairing } from './pairWait.mjs';
25
28
  import { augmentScreenshotTool, callPairedBridgeTool } from './screenshotFile.mjs';
26
29
  import {
27
30
  callPromptTool,
@@ -78,7 +81,7 @@ function registeredToolDefinitions(bridgeTools) {
78
81
  properties: {
79
82
  code: {
80
83
  type: 'string',
81
- description: '6-character pairing code from the SurfCAD Local agent panel',
84
+ description: '8-character pairing code from the SurfCAD Local agent panel',
82
85
  },
83
86
  },
84
87
  },
@@ -107,25 +110,6 @@ const AUTO_PAIR_WAIT_MS = 1000;
107
110
  /** The pair tool returns pair_pending before typical MCP client timeouts (60 s). */
108
111
  const PAIR_TOOL_WAIT_MS = 30_000;
109
112
 
110
- /**
111
- * @param {Promise<unknown>} attempt
112
- * @param {number} waitMs
113
- */
114
- async function waitForPairing(attempt, waitMs) {
115
- attempt.catch(() => {});
116
- let timer;
117
- const timeout = new Promise((resolve) => {
118
- timer = setTimeout(() => resolve(timeout), waitMs);
119
- });
120
- try {
121
- const outcome = await Promise.race([attempt, timeout]);
122
- if (outcome === timeout) throw pairPendingError();
123
- return outcome;
124
- } finally {
125
- clearTimeout(timer);
126
- }
127
- }
128
-
129
113
  /**
130
114
  * Compact JSON. Pretty printing put every number of a bbox or a face key on
131
115
  * its own line and made each payload about 2.4 times larger (list_commands
@@ -286,7 +270,7 @@ export async function createSurfcadMcpServer(options = {}) {
286
270
  if (!code) {
287
271
  return errorResult({
288
272
  code: 'bad_pairing_code',
289
- message: `Missing pairing code. Pass { "code": "......" } or set SURFCAD_PAIRING_CODE. ${pairingEnvHint()}`,
273
+ message: `Missing pairing code. Pass { "code": "........" } or set SURFCAD_PAIRING_CODE. ${pairingEnvHint()}`,
290
274
  }, name);
291
275
  }
292
276
  try {
@@ -364,9 +348,11 @@ export async function createSurfcadMcpServer(options = {}) {
364
348
  }
365
349
 
366
350
  /**
367
- * @param {{ autoPairFromEnv?: boolean }} [options]
351
+ * @param {{ autoPairFromEnv?: boolean, startRelay?: boolean, mode?: 'local' | 'remote', server?: string, name?: string, env?: NodeJS.ProcessEnv }} [options]
368
352
  */
369
353
  export async function runSurfcadMcpServer(options = {}) {
354
+ if (options.mode === 'remote') return runRemoteMcpServer(options);
355
+
370
356
  /** @type {Awaited<ReturnType<typeof ensureAgentBridgeRelay>> | null} */
371
357
  let ownedRelay = null;
372
358
  const startRelay = options.startRelay !== false && process.env.SURFCAD_MCP_START_RELAY !== '0';
@@ -425,3 +411,46 @@ export async function runSurfcadMcpServer(options = {}) {
425
411
 
426
412
  return { server, session, transport };
427
413
  }
414
+
415
+ /**
416
+ * Logged-in (or `--remote`) stdio server. Does not start the loopback relay.
417
+ * The bearer token is never logged.
418
+ * @param {{ server?: string, name?: string, env?: NodeJS.ProcessEnv }} options
419
+ */
420
+ async function runRemoteMcpServer(options) {
421
+ const env = options.env || process.env;
422
+ const saved = await readSavedLogin(env);
423
+ if (!saved?.token) {
424
+ throw Object.assign(new Error('No saved login. Run npx surfcad-mcp login.'), { code: 'not_logged_in' });
425
+ }
426
+ const serverUrl = resolveServerUrl({ flag: options.server, env, saved });
427
+ const pinned = resolveAgentName({ name: options.name, savedName: saved.name, env });
428
+ const remote = new RemoteSession({
429
+ serverUrl,
430
+ token: saved.token,
431
+ name: pinned,
432
+ fillNameFromClient: !pinned,
433
+ });
434
+ const { server, session } = await createSurfcadMcpServer({ session: remote });
435
+ server.oninitialized = () => {
436
+ remote.applyClientInfo(server.getClientVersion());
437
+ void remote.connect().catch((err) => {
438
+ console.error(`${new Date().toISOString()} [surfcad-mcp] ${err?.message || 'remote connect failed'}`);
439
+ });
440
+ };
441
+ if (!remote.needsClientName()) {
442
+ void remote.connect().catch((err) => {
443
+ console.error(`${new Date().toISOString()} [surfcad-mcp] ${err?.message || 'remote connect failed'}`);
444
+ });
445
+ }
446
+
447
+ const onStdinClosed = () => {
448
+ remote.disconnect();
449
+ setTimeout(() => process.exit(0), 0).unref();
450
+ };
451
+ process.stdin.once('end', onStdinClosed);
452
+
453
+ const transport = new StdioServerTransport();
454
+ await server.connect(transport);
455
+ return { server, session, transport };
456
+ }