viewer-mcp
# viewer-mcp
An MCP (Model Context Protocol) server that lets an AI agent build and edit a live **three.js / WebGL** scene in plain language. Ask Claude "put a red sphere on top of the box", and the scene in your browser updates as the tools run.

## What it shows
| Area | How it's done |
|---|---|
| **MCP tools** | 7 tools (`get_scene`, `list_objects`, `add_object`, `update_object`, `remove_object`, `set_camera`, `clear_scene`). Inputs are validated with Zod schemas, and tools carry read-only/destructive annotations. |
| **Guardrails** | Coordinate bounds, scale limits, a cap on object count and hex-only colors. When a request breaks a rule, the tool returns an error the model can act on instead of crashing. A failed update leaves the scene unchanged. |
| **Human-in-the-loop** | `clear_scene` asks the *user* to confirm through MCP elicitation. If the user declines, or the client can't ask, nothing is deleted. The model can't approve the deletion itself. |
| **Traceability** | Every tool call writes one structured JSON trace line (trace id, args, outcome, duration) to stderr. stdout stays reserved for the protocol. |
| **Evaluation harness** | `evals/scenarios.json` defines tasks and the scene state each should end in. CI runs the reference solution for each scenario through a real MCP client. `npm run eval:llm` gives the same scenarios to Claude and grades the final scene. |
| **Tests & CI** | 22 tests with Vitest: unit, MCP integration and viewer bridge. GitHub Actions runs typecheck, tests and build on Node 20 and 22. |
| **Viewer** | three.js + WebGL, kept in sync over WebSocket. Meshes are matched by object id, so only objects that changed are rebuilt. |
```
Claude / MCP host ──stdio──▶ MCP server ──▶ SceneStore (guardrails) ──WebSocket──▶ three.js viewer
│
└─▶ JSON traces (stderr)
```
## Run it
```bash
npm install
npm run build
npm run demo # builds a sample scene through real MCP tool calls
# open http://127.0.0.1:5174
```
### Use it from Claude Desktop / Claude Code
```json
{
"mcpServers": {
"viewer": { "command": "node", "args": ["/absolute/path/to/viewer-mcp/dist/src/main.js"] }
}
}
```
Open http://127.0.0.1:5174, then ask: *"Build a small table with four legs and put a blue vase on it."*
## Test and evaluate
```bash
npm test # unit + integration + scripted eval scenarios
npm run eval:llm # optional: Claude runs every scenario (uses API credits)
```
The LLM eval uses `claude-opus-5-5` by default (override with `EVAL_MODEL`). It reads credentials from `ANTHROPIC_API_KEY` or an `ant auth login` profile. Set `EVAL_ONLY=<text>` to run matching scenarios and `EVAL_VERBOSE=1` to print Claude's tool calls and replies.
### Latest results (Nov 2026, `claude-opus-5-5`, effort `medium`)
**7/7 scenarios passed**, using about 32K tokens in total.
| Scenario | Result |
|---|---|
| Add a red cube | Pass |
| Stack a sphere on a box | Pass |
| Recolor by name, leave others alone | Pass |
| Out-of-bounds move is refused | Pass |
| Clear scene, user confirms | Pass |
| Clear scene, user declines | Pass |
| Clear scene, client cannot ask the user | Pass |
On the first run, the out-of-bounds scenario failed (6/7). The transcript showed Claude read the ±50 limit in the tool schema, never made the invalid call, and explained the limit to the user. The scenario had required the server guardrail to fire, which tested how the move was blocked rather than whether it was. It now checks that the object didn't move. The server-side guardrail still has its own test in `test/server.test.ts`.
## Layout
```
src/scene.ts scene model + guardrails
src/server.ts MCP tools, tracing wrapper, elicitation
src/bridge.ts HTTP + WebSocket bridge to the viewer
viewer/index.html three.js / WebGL viewer
evals/ scenarios, harness, Claude-driven eval
test/ Vitest suites
```
## License
MIT, see [LICENSE](LICENSE).
TDQS
Scored across 7 tools
Most tools map to a distinct action (add/update/remove/clear/set_camera), but list_objects and get_scene both return scene contents, which could cause an agent to pick the wrong one. The descriptions do differentiate them (filtered list vs. full scene plus camera), so confusion is limited.
All seven tools follow a clean snake_case verb_noun pattern (list_objects, get_scene, add_object, update_object, remove_object, clear_scene, set_camera). No mixed conventions or vague verbs.
Seven tools is a well-scoped surface for a 3D scene viewer, and each one clearly earns its place. There are no redundant or filler tools.
The object lifecycle is fully covered (list, read, add, update, remove, bulk clear) plus camera positioning and reading via get_scene. Minor gaps exist, such as no way to read or reset the camera independently or duplicate objects, but core workflows are complete.