|
| 1 | +# Spector.js MCP Server |
| 2 | + |
| 3 | +An MCP (Model Context Protocol) server that lets AI assistants debug **any WebGL website** using [Spector.js](https://spector.babylonjs.com/) — a WebGL debugger. |
| 4 | + |
| 5 | +Load any URL, capture WebGL frames, and inspect draw calls, shaders, textures, and GL state — all from your AI-powered editor. Works with Three.js, Babylon.js, PlayCanvas, raw WebGL, and any other WebGL-based site. |
| 6 | + |
| 7 | +This server lives inside the Spector.js repository at `mcp/`. |
| 8 | + |
| 9 | +## Architecture |
| 10 | + |
| 11 | +``` |
| 12 | +AI Assistant ←→ MCP Server (stdio) ←→ Playwright (headless Chromium) ←→ Any WebGL Website + Spector.js |
| 13 | +``` |
| 14 | + |
| 15 | +The server launches a headless browser, navigates to any URL, injects Spector.js at runtime, and exposes WebGL debugging capabilities as MCP tools. |
| 16 | + |
| 17 | +When running from within the Spector.js repo, the server uses the locally built `dist/spector.bundle.js` from the repo root. This means you can test MCP tooling against local Spector.js changes without publishing. If the local bundle isn't found, it falls back to the npm-installed `spectorjs` package. |
| 18 | + |
| 19 | +## Tools |
| 20 | + |
| 21 | +### Navigation |
| 22 | +| Tool | Description | |
| 23 | +|------|-------------| |
| 24 | +| `load_url` | Navigate to any URL and prepare for WebGL debugging. Works with any WebGL site. | |
| 25 | +| `load_playground` | Convenience shortcut: load a Babylon.js Playground by snippet ID. Returns source code + screenshot. | |
| 26 | +| `take_screenshot` | Screenshot the current canvas. | |
| 27 | + |
| 28 | +### Canvas Selection |
| 29 | +| Tool | Description | |
| 30 | +|------|-------------| |
| 31 | +| `list_canvases` | List all canvas elements on the page with WebGL context info. | |
| 32 | +| `select_canvas` | Select which canvas to target when there are multiple on the page. | |
| 33 | + |
| 34 | +### Capture & Inspection |
| 35 | +| Tool | Description | |
| 36 | +|------|-------------| |
| 37 | +| `capture_frame` | Capture a single WebGL frame. Returns summary stats (commands, draw calls, programs, errors). | |
| 38 | +| `get_draw_calls` | List all draw calls from the last capture with command IDs, names, and arguments. | |
| 39 | +| `get_command_details` | Get full WebGL state at a specific command (blend, depth, stencil, bound textures, etc.). | |
| 40 | +| `get_shaders` | Get shader program source code (vertex + fragment) with compile/link status. | |
| 41 | +| `get_textures` | List texture uploads with dimensions, format, and type. | |
| 42 | +| `get_webgl_state` | Compare initial vs final GL state for the frame — shows what changed. | |
| 43 | + |
| 44 | +### Diagnostics |
| 45 | +| Tool | Description | |
| 46 | +|------|-------------| |
| 47 | +| `get_context_info` | WebGL context details: version, extensions, capabilities, renderer. | |
| 48 | +| `get_console_logs` | Browser console errors/warnings — catches JS errors and WebGL warnings. | |
| 49 | + |
| 50 | +## Setup |
| 51 | + |
| 52 | +### Prerequisites |
| 53 | +- Node.js 18+ |
| 54 | + |
| 55 | +### Install |
| 56 | + |
| 57 | +From the Spector.js repository root: |
| 58 | + |
| 59 | +```bash |
| 60 | +npm run mcp:install |
| 61 | +npm run mcp:build |
| 62 | +``` |
| 63 | + |
| 64 | +Or manually: |
| 65 | + |
| 66 | +```bash |
| 67 | +cd mcp |
| 68 | +npm install |
| 69 | +npx playwright install chromium |
| 70 | +npm run build |
| 71 | +``` |
| 72 | + |
| 73 | +### Configure MCP Client |
| 74 | + |
| 75 | +Replace `<SPECTOR_REPO>` with the absolute path to your Spector.js repo clone. |
| 76 | + |
| 77 | +**Claude Code / Copilot CLI** — add to your MCP settings: |
| 78 | +```json |
| 79 | +{ |
| 80 | + "mcpServers": { |
| 81 | + "spector": { |
| 82 | + "command": "node", |
| 83 | + "args": ["<SPECTOR_REPO>/mcp/dist/index.js"] |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +**VS Code (Copilot)** — add to `.vscode/mcp.json`: |
| 90 | +```json |
| 91 | +{ |
| 92 | + "servers": { |
| 93 | + "spector": { |
| 94 | + "type": "stdio", |
| 95 | + "command": "node", |
| 96 | + "args": ["<SPECTOR_REPO>/mcp/dist/index.js"] |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +## Typical Workflows |
| 103 | + |
| 104 | +### Debug any WebGL site |
| 105 | +1. **Load a URL:** |
| 106 | + > "Load https://threejs.org/examples/#webgl_animation_keyframes and capture a frame" |
| 107 | +
|
| 108 | +2. **Inspect rendering:** |
| 109 | + > "Show me the draw calls and shaders" |
| 110 | + > "Get the GL state at draw call #15" |
| 111 | +
|
| 112 | +### Debug a Babylon.js Playground |
| 113 | +1. **Load by snippet:** |
| 114 | + > "Load playground #6FVG3Y and show me the code" |
| 115 | +
|
| 116 | +2. **Capture and analyze:** |
| 117 | + > "Capture a frame. Are there any GL errors?" |
| 118 | +
|
| 119 | +### Multi-canvas pages |
| 120 | +1. **Discover canvases:** |
| 121 | + > "List the canvases on this page" |
| 122 | +
|
| 123 | +2. **Switch targets:** |
| 124 | + > "Select canvas #2 and capture a frame" |
| 125 | +
|
| 126 | +## Project Structure |
| 127 | + |
| 128 | +``` |
| 129 | +mcp/ |
| 130 | +├── src/ |
| 131 | +│ ├── index.ts # MCP server entry point with tool registrations |
| 132 | +│ ├── browser-manager.ts # BrowserManager class for Playwright browser lifecycle |
| 133 | +│ ├── spector-bridge.ts # Spector.js injection and frame capture |
| 134 | +│ ├── capture-analyzer.ts # Pure functions for analyzing capture data |
| 135 | +│ ├── constants.ts # Shared constants (timeouts, GL format maps) |
| 136 | +│ ├── errors.ts # Custom error hierarchy |
| 137 | +│ └── types.ts # Type definitions |
| 138 | +├── dist/ # Compiled output (generated by npm run build) |
| 139 | +├── package.json |
| 140 | +└── tsconfig.json |
| 141 | +``` |
| 142 | + |
| 143 | +## Development |
| 144 | + |
| 145 | +```bash |
| 146 | +npm run dev # Run with tsx (no build needed) |
| 147 | +npm run build # Build to dist/ |
| 148 | +npm start # Run the built version |
| 149 | +npm test # Run unit tests |
| 150 | +``` |
| 151 | + |
| 152 | +## Testing |
| 153 | + |
| 154 | +```bash |
| 155 | +npm test |
| 156 | +``` |
| 157 | + |
| 158 | +Runs unit tests for the capture analyzer via Jest. Tests cover the pure analysis functions in `src/capture-analyzer.ts` (draw call extraction, shader parsing, texture enumeration, state diffing). |
| 159 | + |
| 160 | +## Dependencies |
| 161 | + |
| 162 | +- [@modelcontextprotocol/sdk](https://github.com/modelcontextprotocol/typescript-sdk) — MCP server framework |
| 163 | +- [Playwright](https://playwright.dev/) — headless browser automation |
| 164 | +- [Spector.js](https://github.com/BabylonJS/Spector.js) — WebGL debugger (injected into the page at runtime) |
| 165 | +- [Zod](https://zod.dev/) — schema validation |
0 commit comments