Skip to content

Agents (MCP server)

An AI agent in the terminal - Claude Code for example - can work with you through the CAD Viewer you are looking at. You select faces or edges in the viewer and say "fillet the edges I selected"; the agent reads your selection, marks what it is about to change in a colour of its own, lets you confirm, then edits your code. It can also read and change the view, take pictures of the model and measure it.

The connection is an MCP server that ships with ocp-viewer-core. The agent starts it as a local process and talks to it over stdio; the server talks to the viewer on 127.0.0.1 the way show does. Nothing listens on the network for it.

Version and viewers

This page describes ocp-viewer-core 1.3.0. The server works with the VS Code CAD Viewer from 4.4.0 and OCP Viewer from 1.4.0. Jupyter CadQuery and build123d Studio are not covered yet.

Installation

The MCP SDK is an optional extra, so installing a viewer does not bring it. Install it into the Python environment your scripts run in, the one that has ocp_vscode or ocp_viewer:

pip install "ocp-viewer-core[mcp]"

In an environment managed by uv:

uv pip install --python <venv>/bin/python "ocp-viewer-core[mcp]"

On Windows the interpreter is <venv>\Scripts\python.exe.

Registering the server with the agent

Register the server once, with the absolute path of that environment's Python. Find it with the environment active:

python -c "import sys; print(sys.executable)"

For Claude Code:

claude mcp add --scope user ocp-viewer -- /path/to/venv/bin/python -m ocp_viewer_core.mcp

--scope user makes the server available in every project. Then check that it starts:

claude mcp list

ocp-viewer should be listed as connected. Inside a session, /mcp shows the server and reconnects it.

Why the absolute path: the agent starts the command as a new process, and a bare python is whatever comes first on the PATH of the shell the agent was started from - often an interpreter without the extra, and then the server stops at once.

Other agent CLIs register a stdio server the same way: the command is <python> -m ocp_viewer_core.mcp. After upgrading ocp-viewer-core in that environment nothing has to be registered again; the next session, or a reconnect, starts the new version.

Which viewer the agent talks to

The server looks for the viewer anew for every request, the way show does: the port in OCP_PORT, the open viewers in the registry ~/.ocpvscode, then port 3939. A viewer opened after the agent started is found.

With several viewers open the server does not guess: the agent gets the list of ports and asks you which one you mean. To tie the server to one viewer, register it with --port:

claude mcp add --scope user ocp-viewer -- /path/to/venv/bin/python -m ocp_viewer_core.mcp --port 3939

See Addressing a viewer for how the viewers get their ports.

Working with the agent

Select, then ask. The viewer is the shared pointing surface: "this", "these", "here" mean what you selected with the select tool or double-clicked. Select first, then tell the agent what to do with it. When something is ambiguous, a well-behaved agent highlights the candidates and asks you to select the one you mean and type "ok".

Three colours tell you who marked what:

Colour Meaning
blue your selection
brown the agent's marks (highlight)
dark green a component you selected that the agent also marked: you and the agent agree

Show before change. The agent marks what its code is going to touch before it edits your file, so you can correct it while nothing has changed yet.

Indices belong to one model. The face and edge numbers you see (e.g. edges_25) are valid for the model on screen. After your code changed and ran again they can mean something else, and the agent recomputes them. In your own code select_edges(part, [25, 27]) (and select_faces, select_vertices) turns viewer indices into shapes; edge_ids(part, shapes) (and face_ids, vertex_ids) from ocp_viewer_core.selectors goes the other way.

The agent's own view. The agent can take pictures of the model from any direction without moving your camera. When it changes what you see - camera, visibility, clipping, the active tool - it can put everything back afterwards.

Exact numbers come from code. measure answers from the viewer's mesh: types such as Plane, Cylinder or Circle are exact, values are exact for planes and straight edges and within the tessellation tolerance for curved geometry. For exact values the agent runs your code in a separate interpreter, with show stubbed out so your viewer keeps its model, and computes them from the B-rep.

The server also gives the agent these rules and step-by-step patterns through the guide tool, since not every agent reads a server's instructions by itself.

The tools

Tool What it does
get_selection what you selected (faces, edges, vertices, solids, grouped by object) and last double-clicked, with the number of the model it belongs to
highlight marks components in the agent's colour; an empty list removes the marks
select, clear_selection sets or clears your selection (needs the select tool on)
get_view your camera, with the screen's right, up and towards-you directions in world coordinates - for "the top left edge"
set_view, restore_view changes camera, visibility, clipping, the active tool and the tab, and puts it all back
render a picture: exactly your view (view="current"), or from a camera of its own - a preset view, any direction, framing given components (fit), zoomed - which does not move your view
scene the objects, their visibility and their face and edge counts
measure properties of components and objects, and the distance between two, from the viewer's mesh
list_viewers the open viewers and whether each answers
guide the rules and patterns for working with you

Pictures are on a white background, at most 1280 pixels on the long side unless the agent asks for more (up to 2576). The viewer renders them at its own canvas size: a hidden or minimised viewer gives small or clipped pictures.

Troubleshooting

/mcp shows "failed" or "Connection closed". The server stopped right after it started. Its error is in the agent's log; for Claude Code that is the folder mcp-logs-ocp-viewer under

  • macOS: ~/Library/Caches/claude-cli-nodejs/<project>/
  • Windows: %LOCALAPPDATA%\claude-cli-nodejs\Cache\<project>\

where <project> is the working folder's path with its separators replaced by dashes.

"No module named 'mcp'" / "needs the MCP SDK". The extra is missing in the environment the registered Python belongs to - see Installation. Install it there and reconnect.

"No viewer found" / "No viewer answers on port N". Open the viewer (in VS Code: the OCP CAD Viewer panel; standalone: python -m ocp_viewer) and ask again.

"Several viewers are open". Tell the agent which port you mean, or register the server with --port.

Pictures still show the hover highlight, or measure and the face and edge counts are missing. The viewer is older than VS Code CAD Viewer 4.4.0 or OCP Viewer 1.4.0; render of your view then falls back to a plain screenshot. Upgrade the viewer.