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/README.md CHANGED
@@ -64,7 +64,9 @@ Clear failures:
64
64
  | What happened | What you see |
65
65
  | --- | --- |
66
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 |
67
+ | A tab took the command but ran past its budget (120 s for `mold_*`, 30 s otherwise) | `command_timeout` |
68
+ | Token revoked (socket close 4001, `token revoked`) | Run `npx surfcad-mcp login` again |
69
+ | Pairing expired (HTTP `expired`, or socket close 4001, `token expired`) | Agent pairing expired, get a new join code |
68
70
  | Too many commands already waiting | `Too many commands are waiting` |
69
71
  | This package is older than the server | `please update surfcad-mcp` |
70
72
 
@@ -117,7 +119,7 @@ claude mcp add surfcad -- npx surfcad-mcp
117
119
 
118
120
  ## Tools
119
121
 
120
- Every command in `data/commands.json` is one MCP tool with the same name and argument schema, plus `pair`, `guide` (plain-text agent guidance by topic, no pairing needed; `dialect` is every script helper by signature; `errors.<code>` topics carry the recovery for an error code), `list_helpers` (the helper registry as JSON: `{ names?, group?, match?, page? }`, full doc and examples with `names`), and five in-process prompt tools. Every `insert_*` description ends with the code its block carries at default arguments. The guide topics are also MCP resources (`surfcad://guide/<topic>`, versioned with this package). Every error payload carries `hint` (the next call to make) and, where a topic exists, `see`. `GET /tools` on the loopback relay and that file list the bridge tools. `list_commands` returns those plus every other tool this server registers (`pair`, `guide`, `list_helpers`, and the prompt tools, including `reply_to_user` and `set_status`), in the `agent` group. Prefer `insert_*` feature tools. `set_script` is the freehand fallback and requires `part_id`.
122
+ Every command in `data/commands.json` is one MCP tool with the same name and argument schema, plus `pair`, `guide` (plain-text agent guidance by topic, no pairing needed; `dialect` is every script helper by signature; `errors.<code>` topics carry the recovery for an error code), `list_helpers` (the helper registry as JSON: `{ names?, group?, match?, page? }`, full doc and examples with `names`), and seven in-process prompt tools (`post_progress`, `close_session`, and the five prompt tools). Every `insert_*` description ends with the code its block carries at default arguments. The guide topics are also MCP resources (`surfcad://guide/<topic>`, versioned with this package). Every error payload carries `hint` (the next call to make) and, where a topic exists, `see`. `GET /tools` on the loopback relay and that file list the bridge tools. `list_commands` returns those plus every other tool this server registers (`pair`, `guide`, `list_helpers`, and the prompt tools, including `reply_to_user` and `set_status`), in the `agent` group. Prefer `insert_*` feature tools. `set_script` is the freehand fallback and requires `part_id`.
121
123
 
122
124
  Parts and assemblies are opaque surf ids. A vault path or display name is `unknown_part` / `unknown_assembly`. Nothing reaches git without an explicit `git_save` (or another `git_*` write below).
123
125
 
@@ -142,15 +144,15 @@ Results are compact JSON. Every tool whose result carries `status` (the model-ch
142
144
  | `insert_part` | `{ part_id?, assembly_id?, copy?, instance?, position? }` | Insert by reference (same ids). `copy: true` mints a new id. `instance: true` adds another placement; `position` is a rough block-out. A position on an unjointed row in a multi-part assembly adds `warning` `unjointed_part`. No git write. |
143
145
  | `delete_part` | `{ part_id, scope, confirm? }` | `scope: "assembly"` drops the row and does not commit. `scope: "repo"` deletes `parts/<id>.js` in one commit after `confirm: true` and returns the assemblies that use the part. A failed scan is `delete_refused`. |
144
146
  | `delete_assembly` | `{ assembly_id, confirm: true, delete_parts? }` | Delete that assembly whether it is open or not. `confirm: true` required. A committed assembly is one commit; a never-saved open assembly leaves the working copy only. |
145
- | `git_save` | `{ message }` | The vault write. `consent_pending` while Allow is up. `pending_save` if offline. `upgrade_required` on a legacy vault. `conflict` / `repo-moved` when a remote edit overlaps. |
147
+ | `git_save` | `{ message }` | The vault write. `consent_pending` while Allow is up. `pending_save` if offline. `unsupported_vault_version` when the vault marker is not the current format. `conflict` / `repo-moved` when a remote edit overlaps. |
146
148
  | `git_add_to_repo` | `{ message }` | Alias of `git_save`. Prefer `git_save`. |
147
149
  | `git_move_to_repo` | `{ vault_name? }` | First Save of a local assembly. Consent on a real repo. |
148
- | `git_upgrade_vault` | `{ confirm: true }` | One-time v1 layout upgrade. `confirm: true` required. |
150
+ | `git_upgrade_vault` | `{ confirm: true }` | Generic vault-format upgrade. Current version returns `already_upgraded`. An older version with no registered step returns `unsupported_vault_version`. `confirm: true` is required only when a step is registered. |
149
151
  | `git_list_branches` | `{}` | Branches and the current one (read-only). |
150
152
  | `git_branch_create` | `{ name }` | Create a branch. Switches when the working copy is clean. |
151
- | `git_branch_switch` | `{ branch, confirm? }` | Switch branch; `confirm: true` when the working copy is dirty. |
153
+ | `git_branch_switch` | `{ branch, confirm?, choice? }` | Switch branch; `confirm: true` when the working copy is dirty. A file conflict returns `conflict` and does not switch. `choice` `use_local` or `use_remote` (both `confirm: true`) does not write git. |
152
154
  | `git_branch_delete` | `{ branch, confirm: true }` | Delete a branch. |
153
- | `git_behind_resolve` | `{ action, confirm: true }` | `reload` is Get latest (no git write). `branch` saves mine to a new branch. No path argument. |
155
+ | `git_behind_resolve` | `{ action, confirm: true }` | `reload` is Get latest (no git write). `branch` is Create a new branch (a save-time git write). No path argument. |
154
156
  | `git_force_overwrite` | `{ confirm: true }` | Overwrite the overlapping remote files. |
155
157
 
156
158
  ### Geometry engine crashes
@@ -165,15 +167,15 @@ These five tools stay inside the local `surfcad-mcp` process. They are not SurfC
165
167
  | --- | --- | --- |
166
168
  | `register_webhook` | `{ url, bearer? }` | Remember an https URL that receives each user prompt as `POST` JSON `{ "type": "surfcad.prompt", ... }`. Optional `bearer` is sent as `Authorization: Bearer <bearer>` (non-empty, at most 4096 characters, no CR or LF). Rejects `http`, credentials in the URL, localhost, `.local` names, and private or loopback addresses. The URL and bearer are held only in memory, are never logged, are never written to disk, are never returned or put in status, are never sent to the SurfCAD tab, and are cleared on `unregister_webhook` and when the pairing session ends. A cancel of a prompt that was already delivered is POSTed as `{ "type": "cancel", "id" }` with the same bearer and timeout. Stop work on that id. |
167
169
  | `unregister_webhook` | `{}` | Drop that URL and bearer. Later prompts are queued. |
168
- | `read_messages` | `{ max?, wait_ms? }` | Return queued prompts, oldest first, and remove them. A screenshot is the next MCP image content block (`image/png`), not part of the text. A cancel item `{ "type": "cancel", "id" }` means the user stopped a prompt you already received and no webhook is registered. Stop work on that id. Every response includes `recent`: the last 50 prompts this session received, oldest first, each `{ id, state }`, in memory only. `acked` is only the ids this call removed. A later `read_messages {}` with an empty queue still lists those ids, so you can recover one after the ack. `status.last` is only the newest. When you finish the task from a prompt you received, close that chat with `reply_to_user`. |
169
- | `reply_to_user` | `{ in_reply_to, text, final? }` | Plain-text reply for a prompt id this session received. The final reply is expected and closes the task: when you finish a task started from the in-viewer prompt pane, send `reply_to_user` with `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. Several replies may share one id, including after `read_messages` has acked it. `final: true` marks the agent idle. An `in_reply_to` that was never delivered to this session is `unknown_prompt` and nothing is sent to the tab. A cancelled id returns `{ "ok": false, "error": "cancelled" }` and is not shown in the tab. |
170
- | `set_status` | `{ state, label?, id? }` | Progress only. It does not close the task; a closing `reply_to_user` does. `state: "responding"` (alias `typing`) keeps the chat open with bouncing dots. `state: "building"` (alias `working`) means you are creating or editing geometry: the chat collapses and the field shows "Agent is working...". Call `building` only once you have everything you need and are actually modeling. Listing parts, faces, edges, or transforms, `status`, and `read_messages` must not use `building`. The legacy boolean `working: true` is `building`. A model-changing tool also enters building if you skip the call. Building stays until the final reply, Stop, or disconnect. No activity for 15 minutes, while responding or building, shows the stall notice and leaves Stop up. A cancelled id returns `{ "ok": false, "error": "cancelled" }`. |
170
+ | `read_messages` | `{ max?, wait_ms? }` | Return queued prompts, oldest first, and remove them. A screenshot is the next MCP image content block (`image/png`), not part of the text. A cancel item `{ "type": "cancel", "id" }` means the user stopped a prompt you already received and no webhook is registered. Stop work on that id. Every response includes `recent`: the last 50 prompts this session received, oldest first, each `{ id, state }`, in memory only, including on a remote session. An idle session that has not received a prompt has `recent: []`. `acked` is only the ids this call removed. A later `read_messages {}` with an empty queue still lists those ids, so you can recover one after the ack. `status.last` is only the newest. When you finish the task from a prompt you received, close that chat with `reply_to_user`. |
171
+ | `reply_to_user` | `{ text, in_reply_to?, final? }` | Plain-text reply for a prompt id this session received. The final reply is expected and closes the task: when you finish a task started from the in-viewer prompt pane, send `reply_to_user` with `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. Several replies may share one id, including after `read_messages` has acked it, and the id may come from an earlier process (a fresh `surfcad-mcp` per tool call). `final: true` marks the agent idle. Omit `in_reply_to` to post one plain-text message on this paired session when no prompt is waiting. That note is not a prompt id, is limited to one per second, does not close a prompt, and is shown as text, not HTML. An `in_reply_to` that is not a prompt id is `unknown_prompt` and nothing is sent to the tab. A cancelled id returns `{ "ok": false, "error": "cancelled" }` and is not shown in the tab. |
172
+ | `set_status` | `{ state, label?, id? }` | Progress only. It does not close the task; a closing `reply_to_user` does. `state: "responding"` (alias `typing`) keeps the chat open with bouncing dots. `state: "building"` (alias `working`) means you are creating or editing geometry: the chat collapses and the field shows "Agent is working...". Call `building` only once you have everything you need and are actually modeling. Listing parts, faces, edges, or transforms, `status`, and `read_messages` must not use `building`. The legacy boolean `working: true` is `building`. A model-changing tool also enters building if you skip the call. Building stays until the final reply, Stop, or disconnect. No activity for 15 minutes, while responding or building, shows the stall notice and leaves Stop up. With no prompt in flight the tab still shows it: the page keeps a session-level state fed by `set_status`, every model-changing tool call, and `post_progress`, and goes idle 60 s after the last one. A cancelled id returns `{ "ok": false, "error": "cancelled" }`. |
171
173
 
172
- A paired SurfCAD tab sends `agent.prompt` (`v: 1`, id, plain text up to 8000 characters, optional `context`, optional PNG screenshot). One prompt per second. A screenshot over 2 MB is rejected. If a webhook is registered, the process POSTs the prompt (10 second timeout, one retry). When `bearer` was set, that POST includes `Authorization: Bearer <bearer>`. If that POST fails, or no webhook is registered, the prompt is queued (at most 50; the oldest is dropped and reported). The tab gets `agent.prompt.ack` with `delivery` `webhook`, `queued`, or `failed`. Replies and working state go back as `agent.reply` and `agent.status`.
174
+ A paired SurfCAD tab sends `agent.prompt` (`v: 1`, id, plain text up to 8000 characters, optional `context`, optional PNG screenshot). On a remote login the same frame arrives on the agent WebSocket, or, when that socket is blocked, from `GET /agent/prompts` (bearer, long-poll). Both paths are accepted the same way. While the agent is present the server holds a prompt until a process reports it taken (`agent.prompt.taken`, sent when `read_messages` returns it or the webhook POST succeeds), offering it to each new socket after hello and once to each pull session (`GET /agent/prompts?session=<id>`), so a one-shot process per tool call does not lose it; the tab hears `queued` at once when no socket is open. A prompt that arrives twice in one process is kept once. One prompt per second. A screenshot over 2 MB is rejected. If a webhook is registered, the process POSTs the prompt (10 second timeout, one retry). When `bearer` was set, that POST includes `Authorization: Bearer <bearer>`. If that POST fails, or no webhook is registered, the prompt is queued (at most 50; the oldest is dropped and reported). The tab gets `agent.prompt.ack` with `delivery` `webhook`, `queued`, or `failed`. Replies and working state go back as `agent.reply` and `agent.status`.
173
175
 
174
176
  When you finish a task started from the 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. `set_status` is for progress and does not close the task. Call `set_status { state: "building" }` only once you have everything you need and are actually creating or editing geometry. Reads stay on responding.
175
177
 
176
- `reply_to_user` only accepts an `in_reply_to` that this session received (queued, delivered by webhook or `read_messages`, or already replied). Any other id is an error `unknown_prompt` and no `agent.reply` is sent. The page also drops a reply whose id is not a prompt the user sent, so it is not drawn in the chat. `read_messages` includes `recent`, the last 50 prompts in this session (`{ id, state }`, oldest first, memory only). That list stays after the ack, including when the queue is empty. `status.last` is only the newest id.
178
+ `reply_to_user` accepts an `in_reply_to` that this process received (queued, delivered by webhook or `read_messages`, or already replied) or a prompt id from an earlier process. Anything that is not a prompt id is an error `unknown_prompt` and no `agent.reply` is sent. Omit `in_reply_to` to post one plain-text `agent.message` on this paired session (no prompt id, one per second). The page drops a reply whose id is not a prompt the user sent, and it draws a session note as text, not HTML. `read_messages` includes `recent`, the last 50 prompts in this session (`{ id, state }`, oldest first, memory only), on a local or remote session. An idle session has `recent: []`. That list stays after the ack, including when the queue is empty. `status.last` is only the newest id.
177
179
 
178
180
  Stop in the prompt card sends `agent.prompt.cancel` (`v: 1`, the current prompt id) over the same paired connection. Each prompt is `queued`, `delivered` (webhook POST succeeded, or `read_messages` returned it), `replied` (a final reply), or `cancelled`. A prompt that is still queued is removed and is not returned by `read_messages`. A prompt that was already delivered is marked `cancelled`. If a webhook is registered, the process POSTs `{ "type": "cancel", "id" }` with the same bearer, https, and timeout rules; if that POST fails, or no webhook is registered, the next `read_messages` returns `{ "type": "cancel", "id" }`. Stop work on that id. `reply_to_user` and `set_status` for a cancelled id return `{ "ok": false, "error": "cancelled" }` and nothing is sent to the tab. The tab clears its working state. Cancelling a prompt that already has a final reply returns `already_final`. An unknown id returns `not_found`.
179
181
 
@@ -205,7 +207,7 @@ The [`surfcad-mcp`](https://www.npmjs.com/package/surfcad-mcp) package is publis
205
207
 
206
208
  Ways to release a new version:
207
209
 
208
- 1. **Version bump on `main`** — In a PR, bump `version` in `packages/surfcad-mcp/package.json`. After merge, when the **CI** workflow completes successfully on `main` and the version differs from the parent commit, publish runs automatically (skipped if that version is already on npm).
210
+ 1. **Version bump on `main`** — In a PR, bump `version` in `packages/surfcad-mcp/package.json`. After merge, when the **CI** workflow completes successfully on `main` and that version is not on npm yet, publish runs automatically. (It asks npm, not the parent commit: CI cancels an in-progress main run when the next commit lands, so the bump commit's own CI may never finish; the next green run publishes instead.)
209
211
  2. **Git tag** — Push a tag `surfcad-mcp-vX.Y.Z` that exactly matches the `version` field at that commit (for example `surfcad-mcp-v0.2.1` for `"version": "0.2.1"`).
210
212
  3. **Manual** — In GitHub → Actions → **Publish surfcad-mcp** → **Run workflow** on branch `main`.
211
213
 
@@ -2,9 +2,33 @@
2
2
  import { runCli } from '../src/cli.mjs';
3
3
  import { runSurfcadMcpServer } from '../src/server.mjs';
4
4
 
5
+ /**
6
+ * `process.exit` drops a stderr write that has not reached the OS yet,
7
+ * so a bad flag can look like a silent non-zero exit. Finish the write first.
8
+ * @param {NodeJS.WriteStream} stream
9
+ */
10
+ function flushed(stream) {
11
+ return new Promise((resolve) => {
12
+ if (typeof stream.write !== 'function') {
13
+ resolve();
14
+ return;
15
+ }
16
+ // A full pipe with no reader must not hang login. 50 ms is enough for
17
+ // a live stderr to take the error and the usage text.
18
+ const timer = setTimeout(resolve, 50);
19
+ stream.write('', () => {
20
+ clearTimeout(timer);
21
+ resolve();
22
+ });
23
+ });
24
+ }
25
+
5
26
  try {
6
27
  const outcome = await runCli(process.argv.slice(2));
7
- if (outcome.action === 'exit') process.exit(outcome.code);
28
+ if (outcome.action === 'exit') {
29
+ await Promise.all([flushed(process.stdout), flushed(process.stderr)]);
30
+ process.exit(outcome.code);
31
+ }
8
32
  await runSurfcadMcpServer({
9
33
  mode: outcome.mode,
10
34
  server: outcome.server,