copper3d 3.11.0 → 3.11.2

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
@@ -37,6 +37,8 @@ assistant sees always matches the version you actually depend on.
37
37
  Four read-only tools: `copper3d_search_api`, `copper3d_get_symbol`,
38
38
  `copper3d_list_guides`, `copper3d_get_guide`.
39
39
 
40
+ Full guide: [AI Coding Assistants (MCP)](https://linkungao.github.io/copper3d_visualisation/guide/mcp-server).
41
+
40
42
  **Claude Code** — one command, no path needed:
41
43
 
42
44
  ```bash
@@ -297,6 +299,10 @@ const nrrdTools = new Copper.NrrdTools(container, {
297
299
  layers: ['tumour', 'edema', 'necrosis', 'vessel']
298
300
  });
299
301
 
302
+ // Read-only reference viewer — shows the image, nothing to annotate into.
303
+ // Scrub / zoom / pan / window-level / crosshair all still work.
304
+ const reference = new Copper.NrrdTools(container, { layers: [] });
305
+
300
306
  // Optional: show current slice index in a panel
301
307
  nrrdTools.setDisplaySliceIndexPanel(document.getElementById('slice-panel') as HTMLDivElement);
302
308
 
@@ -382,10 +388,16 @@ clears the undo stack each time a slice arrives.
382
388
  // Register once after initialization
383
389
  const callbackId = scene.addPreRenderCallbackFunction(nrrdTools.start);
384
390
 
385
- // Unregister on teardown
391
+ // Unregister on teardown, then release the instance
386
392
  scene.removePreRenderCallbackFunction(callbackId);
393
+ nrrdTools.dispose();
387
394
  ```
388
395
 
396
+ `dispose()` releases what the instance holds outside its own DOM subtree — the event router's
397
+ listeners (its window `blur` listener pins the whole engine graph), a pending slice step, and
398
+ the drawing-flag timer. Without it, an app that builds a viewer per case leaks one engine graph
399
+ per case. The instance must not be used afterwards; calling it twice is safe.
400
+
389
401
  ---
390
402
 
391
403
  ### 5. Drawing Setup
@@ -633,6 +645,7 @@ yourself. It throws on an unknown layer, or a channel outside `[1, 255]`.
633
645
  | | `switchAllSlicesArrayData(slices)` | Swap the series (resets view state) |
634
646
  | | `switchSlicesPreservingView(slices)` | Swap the series, keep slice index / zoom / pan |
635
647
  | **Render** | `start` | Frame callback — pass to render loop |
648
+ | **Lifecycle** | `dispose()` | Release the event router's listeners, a pending slice step and the drawing-flag timer. Instance unusable afterwards; safe to call twice |
636
649
  | **Layer** | `setActiveLayer(id)` | Switch drawing target layer |
637
650
  | | `getActiveLayer()` | Read current layer |
638
651
  | | `setLayerVisible(id, bool)` | Toggle layer in composite view |
@@ -0,0 +1,139 @@
1
+ # AI Coding Assistants (MCP) <Badge type="tip" text="3.11.0" />
2
+
3
+ copper3d ships a machine-readable index of its own API and guides. Point your AI assistant
4
+ at it through the [`copper3d-mcp`](https://www.npmjs.com/package/copper3d-mcp) server and it
5
+ can look up real signatures instead of inventing them.
6
+
7
+ ## Why this exists
8
+
9
+ copper3d has roughly **1,200 public symbols across 42 modules**, plus the hand-written guides
10
+ in this site. That does not fit in any model's context window, and it is not in any model's
11
+ training data — so assistants tend to produce plausible-looking calls that do not exist.
12
+
13
+ The server makes that surface searchable on demand.
14
+
15
+ ::: tip It carries no documentation of its own
16
+ It reads the index that ships inside the copper3d installed in **your** project, so what the
17
+ assistant sees always matches the version you actually depend on — not whatever is newest on
18
+ this site.
19
+ :::
20
+
21
+ ## Requirements
22
+
23
+ ```bash
24
+ npm install copper3d
25
+ ```
26
+
27
+ The index lives at `node_modules/copper3d/ai-index/`. Releases from before 3.11.0 do not
28
+ contain it.
29
+
30
+ Working on copper3d itself rather than using it? The server also recognises the copper3d
31
+ repository, where `npm run build` puts the index at `<repo>/ai-index`, so no extra
32
+ configuration is needed there either.
33
+
34
+ ## Setup
35
+
36
+ ### Claude Code
37
+
38
+ The working directory is handled for you, so no path is needed:
39
+
40
+ ```bash
41
+ # just you, in this project
42
+ claude mcp add copper3d -- npx -y copper3d-mcp
43
+
44
+ # commit to .mcp.json so the team gets it on clone
45
+ claude mcp add --scope project copper3d -- npx -y copper3d-mcp
46
+
47
+ # all your projects
48
+ claude mcp add --scope user copper3d -- npx -y copper3d-mcp
49
+ ```
50
+
51
+ ### Every other client
52
+
53
+ Other clients start the server with a working directory that is **not** your project
54
+ ([background](https://github.com/anthropics/claude-code/issues/75266)), so they have to be
55
+ told where it is with `--project`.
56
+
57
+ | Client | Config file | Root key | Needs `--project` |
58
+ |---|---|---|---|
59
+ | Claude Code | `claude mcp add`, or `.mcp.json` | `mcpServers` | no |
60
+ | Cursor | `.cursor/mcp.json` or `~/.cursor/mcp.json` | `mcpServers` | **yes** |
61
+ | VS Code / Copilot | `.vscode/mcp.json` | **`servers`** | **yes** |
62
+ | Claude Desktop | `%APPDATA%\Claude\claude_desktop_config.json`<br>`~/Library/Application Support/Claude/claude_desktop_config.json` | `mcpServers` | **yes** |
63
+ | Windsurf | `~/.codeium/windsurf/mcp_config.json` | `mcpServers` | **yes** |
64
+
65
+ **Cursor / Claude Desktop / Windsurf** — same shape in all three:
66
+
67
+ ```json
68
+ {
69
+ "mcpServers": {
70
+ "copper3d": {
71
+ "command": "npx",
72
+ "args": ["-y", "copper3d-mcp", "--project", "/absolute/path/to/your/project"]
73
+ }
74
+ }
75
+ }
76
+ ```
77
+
78
+ Claude Desktop needs the absolute path in particular: it has no project, and it ignores the
79
+ `cwd` field.
80
+
81
+ **VS Code / Copilot** — note the different root key, and that `${workspaceFolder}` works
82
+ here:
83
+
84
+ ```json
85
+ {
86
+ "servers": {
87
+ "copper3d": {
88
+ "type": "stdio",
89
+ "command": "npx",
90
+ "args": ["-y", "copper3d-mcp", "--project", "${workspaceFolder}"]
91
+ }
92
+ }
93
+ }
94
+ ```
95
+
96
+ ## The four tools
97
+
98
+ All read-only.
99
+
100
+ | Tool | What it does |
101
+ |---|---|
102
+ | `copper3d_search_api` | Search by name, module, summary or parameter type. Members are indexed on their own, so `aiApplyMask` finds `NrrdTools.aiApplyMask` |
103
+ | `copper3d_get_symbol` | One symbol in full: signature, every parameter, return type, source location, related guides |
104
+ | `copper3d_list_guides` | Table of contents for the hand-written guides |
105
+ | `copper3d_get_guide` | Full markdown of one guide |
106
+
107
+ ## Options
108
+
109
+ | | |
110
+ |---|---|
111
+ | `--project <path>` | Project directory to find `node_modules/copper3d` under |
112
+ | `COPPER3D_INDEX_PATH` | Skip discovery entirely: point straight at an `ai-index` directory |
113
+
114
+ Without either, the server looks at the MCP workspace roots, then `CLAUDE_PROJECT_DIR`, then
115
+ walks up from the working directory.
116
+
117
+ ::: tip A failed lookup says so
118
+ If it finds no index it reports that and lists every path it tried. It never quietly returns
119
+ empty results — an assistant reading "no matches" would conclude the library has no API.
120
+ :::
121
+
122
+ ## Troubleshooting
123
+
124
+ | Symptom | Cause | Fix |
125
+ |---|---|---|
126
+ | Tools never appear in VS Code | Used `mcpServers` in `.vscode/mcp.json` | VS Code calls it `servers` |
127
+ | "AI index was not found" | copper3d not installed, or the client lost the project directory | `npm i copper3d`, then add `--project /abs/path` |
128
+ | Assistant still invents APIs | Server not connected | `claude mcp list`, or your client's MCP panel |
129
+
130
+ ## What it does not do
131
+
132
+ Documentation lookup only. copper3d runs in a browser on WebGL; the server is a Node process.
133
+ It cannot render a scene, execute your code, take screenshots or inspect a live canvas.
134
+
135
+ ## Versioning
136
+
137
+ `copper3d-mcp` is versioned independently of `copper3d`. Because it only reads, a copper3d
138
+ release does not require a new release of the server — update copper3d and the assistant sees
139
+ the new API straight away.
@@ -113,6 +113,26 @@ custom id either silently pushed onto another layer's history or threw on undo.
113
113
  created on demand, keyed by whatever id you pass.
114
114
  :::
115
115
 
116
+ #### Example: read-only reference viewer <Badge type="tip" text="3.11.1" />
117
+
118
+ Pass an empty list for a viewer that shows the image but has nothing to annotate into — a
119
+ side-by-side reference panel, a prior study, a QA view:
120
+
121
+ ```typescript
122
+ const reference = new Copper.NrrdTools(container, { layers: [] });
123
+ ```
124
+
125
+ Slice scrubbing, zoom, pan, window/level and the crosshair all work as usual. The drawing
126
+ tools have no layer to target and skip their work rather than failing: `setCurrentLayer()`
127
+ returns `null` and every caller checks it.
128
+
129
+ ::: tip Which one to reach for
130
+ `layers: []` is a **structural** choice — this instance will never annotate.
131
+ `setAnnotationSuspended(true)` (§5.6) is a **temporary** one on an instance that will, such as
132
+ during loading. Use the flag if the user gets to draw later; use the empty list if they never
133
+ do.
134
+ :::
135
+
116
136
  #### Optional display panel
117
137
 
118
138
  Attach a panel element to show current slice index in the viewer:
@@ -351,6 +371,46 @@ function animate() {
351
371
  animate();
352
372
  ```
353
373
 
374
+ #### Teardown — `dispose()` <Badge type="tip" text="3.11.2" />
375
+
376
+ ```typescript
377
+ scene.removePreRenderCallbackFunction(callbackId);
378
+ nrrdTools.dispose();
379
+ container.remove(); // your DOM, your call
380
+ ```
381
+
382
+ `dispose()` releases what the instance holds **outside its own DOM subtree**, so a viewer that
383
+ is torn down can actually be garbage-collected:
384
+
385
+ | Released | Why it matters |
386
+ |----------|----------------|
387
+ | The event router's listeners | Its window `blur` listener closes over the router, which reaches the whole engine graph — one live listener pins the entire instance |
388
+ | A pending slice step | The `requestAnimationFrame` `setSliceMoving` coalesces into |
389
+ | The drawing-flag timer | The pending `setIsDrawFalse` timeout |
390
+
391
+ The instance must not be used afterwards. It is safe to call more than once, so an unmount
392
+ hook that may run twice needs no guard.
393
+
394
+ ::: warning It does not remove your container or free the volumes
395
+ `dispose()` deliberately stops at the boundary of what `NrrdTools` attached elsewhere. The
396
+ host element you passed to the constructor is yours to remove, and the `MaskVolume` buffers go
397
+ when the instance does. Drop your reference to `nrrdTools` after calling it.
398
+ :::
399
+
400
+ **Vue 3:**
401
+
402
+ ```typescript
403
+ import { onBeforeUnmount } from 'vue';
404
+
405
+ onBeforeUnmount(() => {
406
+ scene.removePreRenderCallbackFunction(callbackId);
407
+ nrrdTools.dispose();
408
+ });
409
+ ```
410
+
411
+ An app that builds a viewer per case and never calls this leaks one engine graph per case —
412
+ see [Scene Resources](./scene-resources) for the scene-level counterpart.
413
+
354
414
  ---
355
415
 
356
416
  ### 5. Drawing Setup
@@ -1706,6 +1766,7 @@ cap it below what the byte allows. Capping it lower is your product's call, not
1706
1766
  | | `switchAllSlicesArrayData(slices)` | Swap the loaded series (resets slice index / zoom / pan) |
1707
1767
  | | `switchSlicesPreservingView(slices)` | Swap the loaded series, keeping slice index, zoom and pan |
1708
1768
  | **Render** | `start` | Frame callback — pass to render loop |
1769
+ | **Lifecycle** | `dispose()` | Release the event router's listeners, a pending slice step and the drawing-flag timer. Instance unusable afterwards; safe to call twice |
1709
1770
  | **Layer** | `setActiveLayer(id)` | Switch drawing target layer |
1710
1771
  | | `getActiveLayer()` | Read current layer |
1711
1772
  | | `setLayerVisible(id, bool)` | Toggle layer in composite view |
@@ -130,6 +130,49 @@ scene class that has none (`disposeScene` calls it optionally).
130
130
 
131
131
  ---
132
132
 
133
+ ## 2b. `NrrdTools.dispose()` <Badge type="tip" text="3.11.2" />
134
+
135
+ ```ts
136
+ nrrdTools.dispose(): void
137
+ ```
138
+
139
+ The same idea one level up. A segmentation viewer also attaches things outside itself, and
140
+ `disposeScene()` knows nothing about it — `NrrdTools` is not a scene, so nothing calls this
141
+ for you.
142
+
143
+ | Released | Why |
144
+ |----------|-----|
145
+ | The event router's listeners | Its window `blur` listener closes over the router, which reaches the whole engine graph — canvases, `MaskVolume` buffers, undo stacks. One listener pins all of it |
146
+ | A pending slice step | The `requestAnimationFrame` that `setSliceMoving` coalesces into |
147
+ | The drawing-flag timer | The pending `setIsDrawFalse` timeout |
148
+
149
+ An app that builds a viewer per case and never calls it leaks one engine graph per case, and
150
+ each one's `blur` listener goes on firing for the life of the page — the same shape of leak as
151
+ the `resize` listener in §2a, with considerably more held behind it.
152
+
153
+ ```ts
154
+ scene.removePreRenderCallbackFunction(callbackId);
155
+ nrrdTools.dispose();
156
+ ```
157
+
158
+ The instance must not be used afterwards. Safe to call twice.
159
+
160
+ It stops at that boundary on purpose: the host element you passed to the constructor is yours
161
+ to remove, and the volumes go when the instance does. Drop your reference to it.
162
+
163
+ ::: tip Full teardown order, viewer plus scene
164
+ ```ts
165
+ scene.removePreRenderCallbackFunction(callbackId); // stop the frame callback first
166
+ nrrdTools.dispose(); // then the viewer's listeners/timers
167
+ renderer.disposeScene(sceneName); // then the scene contents
168
+ container.remove(); // finally your own DOM
169
+ ```
170
+ Unregistering the frame callback first means nothing can run against a half-disposed
171
+ instance.
172
+ :::
173
+
174
+ ---
175
+
133
176
  ## 3. Residency budget
134
177
 
135
178
  ```ts
@@ -119,6 +119,7 @@ protectedData.maskData.volumes = {
119
119
  | `setActiveLayer` | `(layerId: string): void` | Set the active Layer; also updates fillColor/brushColor |
120
120
  | `setActiveChannel` | `(channel: ChannelValue): void` | Set the active Channel (1–`MAX_ENGINE_CHANNEL`); updates brush color |
121
121
  | `clearChannel` | `(layerId: string, channel: number): void` | Erase one label across a whole layer, undoably. Throws on an unknown layer (deliberately not via `getVolumeForLayer`, whose fallback would erase a different layer) or a channel outside [1, 255] |
122
+ | `setCurrentLayer` | `(): { ctx, canvas } \| null` *(ToolHost)* | The layer canvas drawing should target. **Returns `null`** when the instance has no layers at all — `new NrrdTools(el, { layers: [] })`, a read-only reference viewer. Every caller must skip its work on `null` rather than dereference |
122
123
  | `getLayerVolume` | `(layerId: string): Uint8Array \| null` | A **copy** of the layer's voxel buffer — not the live array, which a caller could mutate with nothing invalidating the slice cache. Delegates to `DrawToolCore.getLayerVolume` |
123
124
  | `replaceLayerVolume` | `(layerId, data, opts?): void` | Replace the whole buffer. `{ undoable: true }` pushes one `VolumeSnapshot`. Does **not** fire `onLayerVolumeReplaced` itself — that fires only on a later undo/redo, when the backend holds the newer volume and must be told to fall back |
124
125
  | `copyLayerData` | `(sourceLayerId, targetLayerId): void` | Direct buffer copy between layers, **not** undoable. Warns and returns if either volume is unresolvable or the lengths differ; preserves the target's colour map |
@@ -533,6 +534,7 @@ nrrdTools.setCalculateDistanceSphere(200, 150, 42, 'skin');
533
534
  | Method | Description |
534
535
  |--------|-------------|
535
536
  | `drag(opts?)` | Enable drag-to-scroll slice navigation |
537
+ | `dispose()` | Teardown: `eventRouter.unbindAll()`, cancel the pending slice-step `requestAnimationFrame`, clear the `setIsDrawFalse` timer. Releases only what the instance attached outside its own DOM subtree — the window `blur` listener is the one that pins the whole engine graph. Instance unusable afterwards; idempotent |
536
538
  | `setAnnotationSuspended(bool)` | Block every input that can write into a mask, at `DrawToolCore.onCanvasPointerDown`. Slice scrubbing, zoom, pan and the crosshair stay live (see 7.1) |
537
539
  | `isAnnotationSuspended()` | Query the suspension state |
538
540
  | `setSliceOrientation(axis)` | Switch viewing axis. Calls `ensureAxisExtracted` for every loaded contrast first, so a narrowed-axes load can still switch planes |
@@ -1057,6 +1059,28 @@ onCanvasPointerDown(e)
1057
1059
  `handleOnDrawingMouseMove` carries the same early return, so a drag that began before
1058
1060
  suspension cannot continue painting through it.
1059
1061
 
1062
+ ##### Pointer-up is gated too <Badge type="tip" text="3.11.1" />
1063
+
1064
+ `onCanvasPointerUp(e)` is the matching named method, extracted for the same reason — so the
1065
+ gate can be exercised directly. Its left-button condition is **not** simply "are we in draw
1066
+ mode":
1067
+
1068
+ ```ts
1069
+ if (
1070
+ this.drawingTool.painting ||
1071
+ (this.eventRouter.getMode() === 'draw' && !this.annotationSuspended)
1072
+ ) { this.drawingTool.onPointerUp(e); ... }
1073
+ ```
1074
+
1075
+ `getMode() === 'draw'` reports only that Shift is held with a drawing tool selected — it does
1076
+ not know whether a stroke began. On a suspended instance pointer-down is gated, so no stroke
1077
+ ever starts, yet holding Shift and releasing the mouse still reported `'draw'` and reached
1078
+ `drawingTool.onPointerUp`, which threw inside `setCurrentLayer` with no layer to target.
1079
+
1080
+ `drawingTool.painting` is the reliable signal: it is only ever true once a stroke has actually
1081
+ started. A suspended instance can never set it, so it never reaches `onPointerUp`; an
1082
+ in-progress stroke that outlives a mid-drag suspension still gets its proper release.
1083
+
1060
1084
  Two consequences of gating here rather than per tool:
1061
1085
 
1062
1086
  - A tool added later is covered without being told about suspension.
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "schemaVersion": 1,
3
- "copper3dVersion": "3.11.0",
4
- "generatedAt": "2026-09-22T10:04:04.605Z",
5
- "symbolCount": 1194,
3
+ "copper3dVersion": "3.11.2",
4
+ "generatedAt": "2026-09-29T21:39:47.021Z",
5
+ "symbolCount": 1195,
6
6
  "guides": [
7
7
  {
8
8
  "id": "4D-MRI-LV-rendering",
@@ -76,13 +76,21 @@
76
76
  "lang": "en",
77
77
  "bytes": 9327
78
78
  },
79
+ {
80
+ "id": "mcp-server",
81
+ "file": "mcp-server.md",
82
+ "title": "AI Coding Assistants (MCP) <Badge type=\"tip\" text=\"3.11.0\" />",
83
+ "summary": "copper3d ships a machine-readable index of its own API and guides. Point your AI assistant at it through the [`copper3d-mcp`](https://www.npmjs.com/package/copper3d-mcp) server and it can look up real signatures instead of inventing them.",
84
+ "lang": "en",
85
+ "bytes": 4931
86
+ },
79
87
  {
80
88
  "id": "nrrd-tools",
81
89
  "file": "nrrd-tools.md",
82
90
  "title": "NrrdTools Usage Guide",
83
91
  "summary": "Copper3D `NrrdTools` — Medical Image Segmentation Annotation Engine",
84
92
  "lang": "en",
85
- "bytes": 69747
93
+ "bytes": 72310
86
94
  },
87
95
  {
88
96
  "id": "on-demand-controls",
@@ -106,7 +114,7 @@
106
114
  "title": "copper3d — Scene Resources",
107
115
  "summary": "Freeing what a scene holds, bounding how much stays resident, and crossfading one model into another. All exported from the package entry (`copper3d`).",
108
116
  "lang": "en",
109
- "bytes": 9093
117
+ "bytes": 10866
110
118
  },
111
119
  {
112
120
  "id": "segmentation-module",
@@ -114,7 +122,7 @@
114
122
  "title": "Segmentation Module Documentation",
115
123
  "summary": "⚠️ **Note**: All line number references in this document are from historical versions. After multiple rounds of refactoring (State Management Refactor, NrrdTools God Class Split, inheritance → composition refactor), these references are ou…",
116
124
  "lang": "en",
117
- "bytes": 72269
125
+ "bytes": 73962
118
126
  },
119
127
  {
120
128
  "id": "surface-annotation-api",