Skip to content

Commit b790f3d

Browse files
ochafikclaude
andcommitted
Add default demo code for Three.js example
When no code is provided, show a rotating green cube demo. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 70c1215 commit b790f3d

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

examples/threejs-server/server.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,32 @@ import { RESOURCE_MIME_TYPE, RESOURCE_URI_META_KEY } from "../../dist/src/app";
1717
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3001;
1818
const DIST_DIR = path.join(import.meta.dirname, "dist");
1919

20+
// Default code example for the Three.js widget
21+
const DEFAULT_THREEJS_CODE = `const scene = new THREE.Scene();
22+
const camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000);
23+
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
24+
renderer.setSize(width, height);
25+
renderer.setClearColor(0x1a1a2e);
26+
27+
const cube = new THREE.Mesh(
28+
new THREE.BoxGeometry(1, 1, 1),
29+
new THREE.MeshStandardMaterial({ color: 0x00ff88 })
30+
);
31+
scene.add(cube);
32+
33+
scene.add(new THREE.DirectionalLight(0xffffff, 1));
34+
scene.add(new THREE.AmbientLight(0x404040));
35+
36+
camera.position.z = 3;
37+
38+
function animate() {
39+
requestAnimationFrame(animate);
40+
cube.rotation.x += 0.01;
41+
cube.rotation.y += 0.01;
42+
renderer.render(scene, camera);
43+
}
44+
animate();`;
45+
2046
const THREEJS_DOCUMENTATION = `# Three.js Widget Documentation
2147
2248
## Available Globals
@@ -123,7 +149,12 @@ const server = new McpServer({
123149
description:
124150
"Render an interactive 3D scene with custom Three.js code. Available globals: THREE, OrbitControls, EffectComposer, RenderPass, UnrealBloomPass, canvas, width, height.",
125151
inputSchema: {
126-
code: z.string().describe("JavaScript code to render the 3D scene"),
152+
code: z
153+
.string()
154+
.optional()
155+
.describe(
156+
"JavaScript code to render the 3D scene (defaults to a rotating cube demo)",
157+
),
127158
height: z
128159
.number()
129160
.int()
@@ -138,7 +169,10 @@ const server = new McpServer({
138169
content: [
139170
{
140171
type: "text",
141-
text: JSON.stringify({ code, height: height || 400 }),
172+
text: JSON.stringify({
173+
code: code || DEFAULT_THREEJS_CODE,
174+
height: height || 400,
175+
}),
142176
},
143177
],
144178
};

examples/threejs-server/src/threejs-app.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,32 @@ interface ThreeJSToolInput {
1717
height?: number;
1818
}
1919

20+
// Default demo code shown when no code is provided
21+
const DEFAULT_THREEJS_CODE = `const scene = new THREE.Scene();
22+
const camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000);
23+
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
24+
renderer.setSize(width, height);
25+
renderer.setClearColor(0x1a1a2e);
26+
27+
const cube = new THREE.Mesh(
28+
new THREE.BoxGeometry(1, 1, 1),
29+
new THREE.MeshStandardMaterial({ color: 0x00ff88 })
30+
);
31+
scene.add(cube);
32+
33+
scene.add(new THREE.DirectionalLight(0xffffff, 1));
34+
scene.add(new THREE.AmbientLight(0x404040));
35+
36+
camera.position.z = 3;
37+
38+
function animate() {
39+
requestAnimationFrame(animate);
40+
cube.rotation.x += 0.01;
41+
cube.rotation.y += 0.01;
42+
renderer.render(scene, camera);
43+
}
44+
animate();`;
45+
2046
type ThreeJSAppProps = WidgetProps<ThreeJSToolInput>;
2147

2248
const SHIMMER_STYLE = `
@@ -125,7 +151,7 @@ export default function ThreeJSApp({
125151
const containerRef = useRef<HTMLDivElement>(null);
126152

127153
const height = toolInputs?.height ?? toolInputsPartial?.height ?? 400;
128-
const code = toolInputs?.code;
154+
const code = toolInputs?.code || DEFAULT_THREEJS_CODE;
129155
const partialCode = toolInputsPartial?.code;
130156
const isStreaming = !toolInputs && !!toolInputsPartial;
131157

0 commit comments

Comments
 (0)