|
1 | | -# Three.js MCP Server |
| 1 | +# Example: Three.js App |
2 | 2 |
|
3 | 3 | Interactive 3D scene renderer using Three.js. Demonstrates streaming code preview and full MCP App integration. |
4 | 4 |
|
5 | | - |
| 5 | +<table> |
| 6 | + <tr> |
| 7 | + <td><a href="https://modelcontextprotocol.github.io/ext-apps/screenshots/threejs-server/screenshot.png"><img src="https://modelcontextprotocol.github.io/ext-apps/screenshots/threejs-server/screenshot.png" alt="Three.js scene" width="400"></a></td> |
| 8 | + </tr> |
| 9 | +</table> |
6 | 10 |
|
7 | | -## Tools |
| 11 | +## Features |
8 | 12 |
|
9 | | -| Tool | Description | |
10 | | -| -------------------- | ------------------------------------ | |
11 | | -| `show_threejs_scene` | Render 3D scene from JavaScript code | |
12 | | -| `learn_threejs` | Get documentation and examples | |
| 13 | +- **Interactive 3D Rendering**: Execute JavaScript code to create and animate Three.js scenes |
| 14 | +- **Streaming Preview**: See the scene build in real-time as code is being written |
| 15 | +- **Built-in Helpers**: Pre-configured `OrbitControls`, post-processing effects (bloom), and render passes |
| 16 | +- **Documentation Tool**: `learn_threejs` provides API docs and code examples on demand |
13 | 17 |
|
14 | | -## Quick Start |
| 18 | +## Running |
15 | 19 |
|
16 | | -```bash |
17 | | -# Build |
18 | | -npm run build |
| 20 | +1. Install dependencies: |
19 | 21 |
|
20 | | -# Run (stdio mode for Claude Desktop) |
21 | | -bun server.ts --stdio |
| 22 | + ```bash |
| 23 | + npm install |
| 24 | + ``` |
22 | 25 |
|
23 | | -# Run (HTTP mode for basic-host) |
24 | | -bun server.ts |
25 | | -``` |
26 | | - |
27 | | -## Code Structure |
28 | | - |
29 | | -``` |
30 | | -threejs-server/ |
31 | | -├── server.ts # MCP server with tools |
32 | | -├── mcp-app.html # Entry HTML |
33 | | -└── src/ |
34 | | - ├── mcp-app-wrapper.tsx # Generic MCP App wrapper (reusable) |
35 | | - ├── threejs-app.tsx # Three.js widget component |
36 | | - └── global.css # Styles |
37 | | -``` |
| 26 | +2. Build and start the server: |
38 | 27 |
|
39 | | -## Key Files |
40 | | - |
41 | | -### `src/mcp-app-wrapper.tsx` |
42 | | - |
43 | | -Generic wrapper handling MCP connection. Provides `WidgetProps` interface: |
44 | | - |
45 | | -```tsx |
46 | | -interface WidgetProps<TToolInput> { |
47 | | - toolInputs: TToolInput | null; // Complete tool input |
48 | | - toolInputsPartial: TToolInput | null; // Streaming partial input |
49 | | - toolResult: CallToolResult | null; // Tool execution result |
50 | | - hostContext: McpUiHostContext | null; // Theme, viewport, locale |
51 | | - callServerTool: App["callServerTool"]; // Call MCP server tools |
52 | | - sendMessage: App["sendMessage"]; // Send chat messages |
53 | | - sendOpenLink: App["sendOpenLink"]; // Open URLs in browser |
54 | | - sendLog: App["sendLog"]; // Debug logging |
55 | | -} |
56 | | -``` |
| 28 | + ```bash |
| 29 | + npm start |
| 30 | + # OR for stdio: |
| 31 | + npm run build && bun server.ts --stdio |
| 32 | + ``` |
57 | 33 |
|
58 | | -### `src/threejs-app.tsx` |
| 34 | + The server will listen on `http://localhost:3001/mcp`. |
59 | 35 |
|
60 | | -Widget component receiving all props. Uses: |
| 36 | +3. View using the [`basic-host`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-host) example or another MCP Apps-compatible host. |
61 | 37 |
|
62 | | -- `toolInputs.code` - JavaScript to execute |
63 | | -- `toolInputsPartial.code` - Streaming preview |
64 | | -- `toolInputs.height` - Canvas height |
| 38 | +### Tool Input |
65 | 39 |
|
66 | | -### `server.ts` |
| 40 | +To test the example, copy the contents of [`test-input.json`](test-input.json) into the tool input field in `basic-host`. |
67 | 41 |
|
68 | | -MCP server with: |
69 | | - |
70 | | -- `show_threejs_scene` tool linked to UI resource |
71 | | -- `learn_threejs` documentation tool |
72 | | -- stdio + HTTP transport support |
73 | | - |
74 | | -## Available Three.js Globals |
75 | | - |
76 | | -```javascript |
77 | | -THREE; // Three.js library |
78 | | -canvas; // Pre-created canvas |
79 | | -(width, height); // Canvas dimensions |
80 | | -OrbitControls; // Camera controls |
81 | | -EffectComposer; // Post-processing |
82 | | -RenderPass; // Render pass |
83 | | -UnrealBloomPass; // Bloom effect |
84 | | -``` |
85 | | - |
86 | | -## Test Input |
87 | | - |
88 | | -Copy contents of `test-input.json` to test in basic-host (`http://localhost:8080`). |
89 | | - |
90 | | -## Example Code |
| 42 | +The test input creates a simple scene with a rotating cube: |
91 | 43 |
|
92 | 44 | ```javascript |
93 | 45 | const scene = new THREE.Scene(); |
@@ -128,10 +80,37 @@ function animate() { |
128 | 80 | animate(); |
129 | 81 | ``` |
130 | 82 |
|
131 | | -## Creating a New Widget |
| 83 | +#### Available Three.js Globals |
| 84 | + |
| 85 | +When writing custom code, these globals are available: |
| 86 | + |
| 87 | +```javascript |
| 88 | +THREE; // Three.js library |
| 89 | +canvas; // Pre-created canvas element |
| 90 | +width; // Canvas width |
| 91 | +height; // Canvas height |
| 92 | +OrbitControls; // Camera controls |
| 93 | +EffectComposer; // Post-processing composer |
| 94 | +RenderPass; // Render pass |
| 95 | +UnrealBloomPass; // Bloom effect |
| 96 | +``` |
| 97 | + |
| 98 | +## Architecture |
| 99 | + |
| 100 | +### Server (`server.ts`) |
| 101 | + |
| 102 | +Exposes two tools: |
| 103 | + |
| 104 | +- `show_threejs_scene` - Renders a 3D scene from JavaScript code |
| 105 | +- `learn_threejs` - Returns documentation and code examples for Three.js APIs |
| 106 | + |
| 107 | +Supports Streamable HTTP and stdio transports. |
| 108 | + |
| 109 | +### App (`src/threejs-app.tsx`) |
| 110 | + |
| 111 | +React component that: |
132 | 112 |
|
133 | | -1. Copy this example |
134 | | -2. Rename `threejs-app.tsx` to your widget name |
135 | | -3. Define your `ToolInput` interface |
136 | | -4. Implement your widget using the `WidgetProps` |
137 | | -5. Update `server.ts` with your tools |
| 113 | +- Receives tool inputs via the MCP App SDK |
| 114 | +- Displays streaming preview from `toolInputsPartial.code` as code arrives |
| 115 | +- Executes final code from `toolInputs.code` when complete |
| 116 | +- Renders to a pre-created canvas with configurable height |
0 commit comments