@modelcontextprotocol/server-threejs 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,110 @@
1
+ # Example: Three.js App
2
+
3
+ ![Screenshot](screenshot.png)
4
+
5
+ Interactive 3D scene renderer using Three.js. Demonstrates streaming code preview and full MCP App integration.
6
+
7
+ ## Features
8
+
9
+ - **Interactive 3D Rendering**: Execute JavaScript code to create and animate Three.js scenes
10
+ - **Streaming Preview**: See the scene build in real-time as code is being written
11
+ - **Built-in Helpers**: Pre-configured `OrbitControls`, post-processing effects (bloom), and render passes
12
+ - **Documentation Tool**: `learn_threejs` provides API docs and code examples on demand
13
+
14
+ ## Running
15
+
16
+ 1. Install dependencies:
17
+
18
+ ```bash
19
+ npm install
20
+ ```
21
+
22
+ 2. Build and start the server:
23
+
24
+ ```bash
25
+ npm run start:http # for Streamable HTTP transport
26
+ # OR
27
+ npm run start:stdio # for stdio transport
28
+ ```
29
+
30
+ 3. View using the [`basic-host`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-host) example or another MCP Apps-compatible host.
31
+
32
+ ### Tool Input
33
+
34
+ To test the example, copy the contents of [`test-input.json`](test-input.json) into the tool input field in `basic-host`.
35
+
36
+ The test input creates a simple scene with a rotating cube:
37
+
38
+ ```javascript
39
+ const scene = new THREE.Scene();
40
+ const camera = new THREE.PerspectiveCamera(60, width / height, 0.1, 100);
41
+ camera.position.set(2, 2, 2);
42
+
43
+ const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
44
+ renderer.setSize(width, height);
45
+ renderer.shadowMap.enabled = true;
46
+
47
+ const cube = new THREE.Mesh(
48
+ new THREE.BoxGeometry(),
49
+ new THREE.MeshStandardMaterial({ color: 0x00ff88 }),
50
+ );
51
+ cube.castShadow = true;
52
+ cube.position.y = 0.5;
53
+ scene.add(cube);
54
+
55
+ const floor = new THREE.Mesh(
56
+ new THREE.PlaneGeometry(5, 5),
57
+ new THREE.MeshStandardMaterial({ color: 0x222233 }),
58
+ );
59
+ floor.rotation.x = -Math.PI / 2;
60
+ floor.receiveShadow = true;
61
+ scene.add(floor);
62
+
63
+ const light = new THREE.DirectionalLight(0xffffff, 2);
64
+ light.position.set(3, 5, 3);
65
+ light.castShadow = true;
66
+ scene.add(light);
67
+ scene.add(new THREE.AmbientLight(0x404040));
68
+
69
+ function animate() {
70
+ requestAnimationFrame(animate);
71
+ cube.rotation.y += 0.01;
72
+ renderer.render(scene, camera);
73
+ }
74
+ animate();
75
+ ```
76
+
77
+ #### Available Three.js Globals
78
+
79
+ When writing custom code, these globals are available:
80
+
81
+ ```javascript
82
+ THREE; // Three.js library
83
+ canvas; // Pre-created canvas element
84
+ width; // Canvas width
85
+ height; // Canvas height
86
+ OrbitControls; // Camera controls
87
+ EffectComposer; // Post-processing composer
88
+ RenderPass; // Render pass
89
+ UnrealBloomPass; // Bloom effect
90
+ ```
91
+
92
+ ## Architecture
93
+
94
+ ### Server (`server.ts`)
95
+
96
+ Exposes two tools:
97
+
98
+ - `show_threejs_scene` - Renders a 3D scene from JavaScript code
99
+ - `learn_threejs` - Returns documentation and code examples for Three.js APIs
100
+
101
+ Supports Streamable HTTP and stdio transports.
102
+
103
+ ### App (`src/threejs-app.tsx`)
104
+
105
+ React component that:
106
+
107
+ - Receives tool inputs via the MCP App SDK
108
+ - Displays streaming preview from `toolInputsPartial.code` as code arrives
109
+ - Executes final code from `toolInputs.code` when complete
110
+ - Renders to a pre-created canvas with configurable height