Supporting Modules¶
ServerState¶
Encapsulates all mutable state for one MCP server instance.
Source code in src/napari_mcp/state.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
Functions¶
request_shutdown
¶
Request the MCP server to shut down.
Safe to call from any thread (e.g. the Qt main thread when the viewer window is destroyed).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
delay
|
float
|
Seconds to wait before stopping the event loop. A short delay
allows any in-flight MCP responses (e.g. from |
1.0
|
Source code in src/napari_mcp/state.py
gui_execute
¶
Run operation through GUI executor if set, else directly.
store_output
async
¶
store_output(tool_name: str, stdout: str = '', stderr: str = '', result_repr: str | None = None, **metadata: Any) -> str
Store tool output and return a unique ID.
Source code in src/napari_mcp/state.py
proxy_to_external
async
¶
Proxy a tool call to an external viewer if in AUTO_DETECT mode.
Returns None immediately in STANDALONE mode (zero overhead).
Source code in src/napari_mcp/state.py
detect_external_viewer
async
¶
Detect if an external napari viewer is available via MCP bridge.
Returns:
| Type | Description |
|---|---|
tuple of (found, info)
|
found is True if an external bridge was detected, False otherwise. info is the session information dict when found, else None. |
Source code in src/napari_mcp/state.py
external_session_information
async
¶
Get session information from the external viewer.
Source code in src/napari_mcp/state.py
Output Utilities¶
Output storage and truncation utilities.
Functions¶
truncate_output
¶
Truncate output to specified line limit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output
|
str
|
The output text to truncate. |
required |
line_limit
|
int
|
Maximum number of lines to return. If -1, return all lines. |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, bool]
|
Tuple of (truncated_output, was_truncated). |
Source code in src/napari_mcp/output.py
Shared Helpers¶
Shared helpers used by both server.py and bridge_server.py.
These functions extract logic that was previously duplicated between the standalone server and the bridge server.
Functions¶
parse_bool
¶
Parse a boolean value from various input types.
Handles bool, str ("true"/"false"/"1"/"0"/etc.), and None.
Source code in src/napari_mcp/_helpers.py
resolve_layer_type
¶
Resolve a layer type string to its canonical form.
Returns None if the type is not recognized.
build_layer_detail
¶
Build a detail dict for a single napari layer.
Used by session_information in both standalone and bridge modes.
Source code in src/napari_mcp/_helpers.py
create_layer_on_viewer
¶
create_layer_on_viewer(viewer: Any, resolved_data: Any, lt: str, *, name: str | None = None, colormap: str | None = None, blending: str | None = None, channel_axis: int | str | None = None, size: float | str | None = None, shape_type: str | None = None, edge_color: str | None = None, face_color: str | None = None, edge_width: float | str | None = None) -> dict[str, Any]
Add a layer to a napari viewer and return a result dict.
This is the shared core used by add_layer in both server.py
(standalone) and bridge_server.py (Qt thread). The caller is responsible
for calling process_events and holding locks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
viewer
|
Viewer
|
The napari viewer instance. |
required |
resolved_data
|
Any
|
The data to add (numpy array, list, tuple, etc.). |
required |
lt
|
str
|
Canonical layer type (one of: image, labels, points, shapes, vectors, tracks, surface). |
required |
Source code in src/napari_mcp/_helpers.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
run_code
¶
run_code(code: str, exec_globals: dict[str, Any], *, source_label: str = '<mcp-exec>') -> tuple[str, str, str | None, Exception | None]
Execute Python code with stdout/stderr capture.
This is the shared core used by both execute_code in server.py
(standalone) and bridge_server.py (Qt thread).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
Python code string. The last expression's value is captured. |
required |
exec_globals
|
dict
|
The execution namespace (both globals and locals). |
required |
source_label
|
str
|
Label for compile() filename, e.g. |
'<mcp-exec>'
|
Returns:
| Type | Description |
|---|---|
tuple of (stdout, stderr, result_repr, error)
|
|
Source code in src/napari_mcp/_helpers.py
build_truncated_response
¶
build_truncated_response(*, status: str, output_id: str, stdout_full: str, stderr_full: str, result_repr: str | None, line_limit: int | str, error: Exception | None = None) -> dict[str, Any]
Build a response dict with optional output truncation.
This is the shared pattern used by execute_code in both server.py
and bridge_server.py, and also by install_packages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
status
|
str
|
"ok" or "error". |
required |
output_id
|
str
|
The stored output ID. |
required |
stdout_full
|
str
|
Full stdout/stderr content. |
required |
stderr_full
|
str
|
Full stdout/stderr content. |
required |
result_repr
|
str or None
|
The repr of the last expression result. |
required |
line_limit
|
int or str
|
Maximum lines (-1 for unlimited). Strings are converted to int; invalid values fall back to 30. |
required |
error
|
Exception or None
|
The exception, if status == "error". |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The response dict ready to return from a tool. |
Source code in src/napari_mcp/_helpers.py
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | |