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/README.md +55 -7
- package/bin/surfcad-mcp.js +12 -3
- package/data/commands.json +4424 -129
- package/data/helpers.json +35 -11
- package/package.json +2 -2
- package/src/bridge/bridgePopup.mjs +28 -1
- package/src/bridge/localAddress.mjs +43 -5
- package/src/bridge/relayHub.mjs +31 -9
- package/src/cli.mjs +229 -0
- package/src/commandProtocol.mjs +126 -0
- package/src/errors.mjs +80 -1
- package/src/helpers.mjs +1 -1
- package/src/pairWait.mjs +34 -0
- package/src/prompt/errorHints.mjs +21 -4
- package/src/prompt/guide.mjs +4 -4
- package/src/prompt/promptSession.mjs +2 -0
- package/src/relay.mjs +8 -5
- package/src/remote/config.mjs +177 -0
- package/src/remote/httpFallback.mjs +289 -0
- package/src/remote/login.mjs +192 -0
- package/src/remote/logout.mjs +67 -0
- package/src/remote/session.mjs +502 -0
- package/src/remote/tokenStore.mjs +109 -0
- package/src/server.mjs +52 -23
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# surfcad-mcp
|
|
2
2
|
|
|
3
|
-
Stdio [Model Context Protocol](https://modelcontextprotocol.io/) server for the SurfCAD browser agent bridge. Each tool maps to one `window.surfcad` command from the shipped command catalog (`data/commands.json`). The server pairs to your open SurfCAD tab through the **M1** loopback WebSocket relay (`ws://127.0.0.1:17341` by default).
|
|
3
|
+
Stdio [Model Context Protocol](https://modelcontextprotocol.io/) server for the SurfCAD browser agent bridge. Each tool maps to one `window.surfcad` command from the shipped command catalog (`data/commands.json`). The server pairs to your open SurfCAD tab through the **M1** loopback WebSocket relay (`ws://127.0.0.1:17341` by default). A saved login uses the server relay instead; the tool list does not change.
|
|
4
4
|
|
|
5
5
|
## Install and run
|
|
6
6
|
|
|
@@ -16,15 +16,63 @@ Environment variables:
|
|
|
16
16
|
|
|
17
17
|
| Variable | Purpose |
|
|
18
18
|
| --- | --- |
|
|
19
|
-
| `SURFCAD_PAIRING_CODE` |
|
|
20
|
-
| `SURFCAD_AGENT_BRIDGE_WS_URL` |
|
|
21
|
-
| `
|
|
19
|
+
| `SURFCAD_PAIRING_CODE` | 8-character code from the SurfCAD **Connect Agent** dialog (optional if you use the `pair` tool). When set, the local relay stays selected even if a remote login is saved. |
|
|
20
|
+
| `SURFCAD_AGENT_BRIDGE_WS_URL` | Loopback relay URL (default `ws://127.0.0.1:17341`). When set, the local relay stays selected even if a remote login is saved. |
|
|
21
|
+
| `SURFCAD_AGENT_BRIDGE_WS_PORT` | Loopback relay port. When set, the local relay stays selected even if a remote login is saved. |
|
|
22
|
+
| `SURFCAD_MCP_START_RELAY` | Set to `0` to skip starting the local relay (tests only). |
|
|
23
|
+
| `SURFCAD_AGENT_SERVER` | Origin for remote login and the remote client (default `https://surfcad.com`). Dev and staging only. `--server` wins. |
|
|
24
|
+
| `SURFCAD_AGENT_NAME` | Agent name when `--name` is omitted. The MCP client name is used after that, when the stdio server is connected. |
|
|
25
|
+
| `SURFCAD_MCP_TRANSPORT` | `local` or `remote`. Same as `--local` / `--remote`. |
|
|
26
|
+
| `SURFCAD_MCP_CONFIG_DIR` | Directory for the saved remote token. Default `$XDG_CONFIG_HOME/surfcad-mcp` or `~/.config/surfcad-mcp`. |
|
|
27
|
+
|
|
28
|
+
## Which relay
|
|
29
|
+
|
|
30
|
+
The local loopback relay is the default. `npx surfcad-mcp` starts it and pairs with the 8-character code.
|
|
31
|
+
|
|
32
|
+
A saved login selects the server relay at `https://surfcad.com/agent`. `--remote` selects it too, and exits with a message to run `login` when no token is saved.
|
|
33
|
+
|
|
34
|
+
These keep the local relay even when a login is saved, so an existing local MCP config and the in-repo goldens stay on the loopback relay:
|
|
35
|
+
|
|
36
|
+
- `--local` or `SURFCAD_MCP_TRANSPORT=local`
|
|
37
|
+
- `SURFCAD_AGENT_BRIDGE_WS_URL` or `SURFCAD_AGENT_BRIDGE_WS_PORT`
|
|
38
|
+
- a non-empty `SURFCAD_PAIRING_CODE`
|
|
39
|
+
|
|
40
|
+
`--remote` and `SURFCAD_MCP_TRANSPORT=remote` win over that list.
|
|
41
|
+
|
|
42
|
+
## Remote login
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
npx surfcad-mcp login
|
|
46
|
+
npx surfcad-mcp login --name "Cursor"
|
|
47
|
+
npx surfcad-mcp login --code ABCD-EFGH
|
|
48
|
+
npx surfcad-mcp status
|
|
49
|
+
npx surfcad-mcp logout
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
On Safari, iOS, or a phone-width page, SurfCAD shows a join code. `login --code ABCD-EFGH` redeems it, writes the token the same way device-code login does, and does not print the token. Add `--server` for a dev or staging origin. Cleartext `http://` is still only allowed for localhost, 127.0.0.1, and ::1. Desktop Chrome and the local bridge keep the device-code flow below.
|
|
53
|
+
|
|
54
|
+
`login` without `--code` runs device-code pairing against `https://surfcad.com/agent`. It generates 32 random bytes, sends only their SHA-256, prints an `ABCD-EFGH` code and the approval URL (`https://surfcad.com/agent`), then polls with the secret until you approve, deny, or the code expires. The token is written to the config directory at mode 0600 (the directory is 0700). It is not printed and it is not logged. `status` shows the server, name, machine, and saved time, never the token. `logout` calls `DELETE /agent/tokens/self` with that token, then deletes the file. If the server cannot be reached, the file is still deleted and the command says the login may stay valid until it expires.
|
|
55
|
+
|
|
56
|
+
Sign in on the approval page and enter the code. Leave the SurfCAD tab open. The client connects out over WebSocket (`wss://surfcad.com/agent`) with `Authorization: Bearer`. The first frame is `{ "type": "hello", "v": 1 }`. If WebSocket is blocked, commands fall back to `POST /agent/commands` and then Server-Sent Events, or a long-poll `GET` if the event stream fails. In-viewer prompts travel on the WebSocket.
|
|
57
|
+
|
|
58
|
+
`--server https://staging.example` or `SURFCAD_AGENT_SERVER` overrides the origin for dev and staging. https becomes `wss://`. `http://` and `ws://` are accepted only for localhost, 127.0.0.1, and ::1.
|
|
59
|
+
|
|
60
|
+
The approval name is `--name`, then `SURFCAD_AGENT_NAME`, then the name stored at login. `login` runs before any MCP client exists, so it cannot see `clientInfo`. When none of those is set, the stdio server sends the MCP client name (`clientInfo.name`) on hello. With no name the server says "an agent".
|
|
61
|
+
|
|
62
|
+
Clear failures:
|
|
63
|
+
|
|
64
|
+
| What happened | What you see |
|
|
65
|
+
| --- | --- |
|
|
66
|
+
| No SurfCAD tab for about 30 seconds | `SurfCAD tab is asleep` |
|
|
67
|
+
| Token revoked or expired (socket close 4001) | Run `npx surfcad-mcp login` again |
|
|
68
|
+
| Too many commands already waiting | `Too many commands are waiting` |
|
|
69
|
+
| This package is older than the server | `please update surfcad-mcp` |
|
|
22
70
|
|
|
23
71
|
## Pair with SurfCAD
|
|
24
72
|
|
|
25
73
|
1. Open [https://surfcad.com](https://surfcad.com) (or local dev).
|
|
26
|
-
2. Open the **profile** chip and choose **Connect Agent**. When the relay is found,
|
|
27
|
-
3. Add `surfcad-mcp` to your MCP client (`npx surfcad-mcp`), set `SURFCAD_PAIRING_CODE` to that code, or call the MCP **`pair`** tool with `{ "code": "
|
|
74
|
+
2. Open the **profile** chip and choose **Connect Agent**. When the relay is found, an eight-character pairing code appears. Leave the tab open.
|
|
75
|
+
3. Add `surfcad-mcp` to your MCP client (`npx surfcad-mcp`), set `SURFCAD_PAIRING_CODE` to that code, or call the MCP **`pair`** tool with `{ "code": "BCDFGHJK" }`.
|
|
28
76
|
4. Click **Allow** (the green check) in SurfCAD when prompted.
|
|
29
77
|
|
|
30
78
|
The MCP server instructions start with `call register_webhook { url, bearer? } if you can receive https webhooks, otherwise poll read_messages.` The next sentence tells the agent: when you finish a task started from the SurfCAD in-viewer prompt pane, send a closing reply with `reply_to_user` (`final: true`) summarising what was done, so the user gets confirmation. On failure or partial completion, say what was done, what failed and why, and what is left. The instructions continue with a quick-start (under 2 KB) that points at the `guide` tool. `guide { topic: "start" }` repeats that closing-reply rule.
|
|
@@ -46,7 +94,7 @@ Safari on a Mac blocks https pages from opening `ws://127.0.0.1` directly. In Sa
|
|
|
46
94
|
"command": "npx",
|
|
47
95
|
"args": ["-y", "surfcad-mcp"],
|
|
48
96
|
"env": {
|
|
49
|
-
"SURFCAD_PAIRING_CODE": "
|
|
97
|
+
"SURFCAD_PAIRING_CODE": "BCDFGHJK"
|
|
50
98
|
}
|
|
51
99
|
}
|
|
52
100
|
}
|
package/bin/surfcad-mcp.js
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { runCli } from '../src/cli.mjs';
|
|
2
3
|
import { runSurfcadMcpServer } from '../src/server.mjs';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
try {
|
|
6
|
+
const outcome = await runCli(process.argv.slice(2));
|
|
7
|
+
if (outcome.action === 'exit') process.exit(outcome.code);
|
|
8
|
+
await runSurfcadMcpServer({
|
|
9
|
+
mode: outcome.mode,
|
|
10
|
+
server: outcome.server,
|
|
11
|
+
name: outcome.name,
|
|
12
|
+
});
|
|
13
|
+
} catch (err) {
|
|
14
|
+
console.error(err?.stack || err?.message || err);
|
|
6
15
|
process.exit(1);
|
|
7
|
-
}
|
|
16
|
+
}
|