Skip to content

Commit 6950161

Browse files
antonpk1claude
andcommitted
Add Three.js marketing example server
New example demonstrating interactive 3D scene rendering with: - show_threejs_scene tool with streaming code preview - learn_threejs documentation tool - Full WidgetProps interface for MCP App integration - Reusable mcp-app-wrapper.tsx pattern Also adds bun.lockb to .gitignore. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 43214f9 commit 6950161

File tree

13 files changed

+796
-0
lines changed

13 files changed

+796
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
dist/
33
node_modules/
44
yarn.lock
5+
bun.lockb
56
.vscode/
67
docs/api/
78
tmp/

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ Your `package.json` will then look like:
5757
- [`examples/basic-server-react`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-server-react) — Example MCP server with tools that return UI Apps (React)
5858
- [`examples/basic-server-vanillajs`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-server-vanillajs) — Example MCP server with tools that return UI Apps (vanilla JS)
5959
- [`examples/basic-host`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/basic-host) — Bare-bones example of hosting MCP Apps
60+
- [`examples/budget-allocator-server`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/budget-allocator-server) — Budget allocation with sliders and charts
61+
- [`examples/cohort-heatmap-server`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/cohort-heatmap-server) — Cohort retention heatmap
62+
- [`examples/customer-segmentation-server`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/customer-segmentation-server) — Customer segmentation scatter chart
63+
- [`examples/scenario-modeler-server`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/scenario-modeler-server) — SaaS financial scenario modeling
64+
- [`examples/system-monitor-server`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/system-monitor-server) — Real-time OS metrics monitoring
65+
- [`examples/threejs-server`](https://github.com/modelcontextprotocol/ext-apps/tree/main/examples/threejs-server) — Interactive 3D scene renderer with Three.js
6066

6167
To run the examples end-to-end:
6268

examples/threejs-server/README.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Three.js MCP Server
2+
3+
Interactive 3D scene renderer using Three.js. Demonstrates streaming code preview and full MCP App integration.
4+
5+
![Screenshot](screenshot.png)
6+
7+
## Tools
8+
9+
| Tool | Description |
10+
|------|-------------|
11+
| `show_threejs_scene` | Render 3D scene from JavaScript code |
12+
| `learn_threejs` | Get documentation and examples |
13+
14+
## Quick Start
15+
16+
```bash
17+
# Build
18+
npm run build
19+
20+
# Run (stdio mode for Claude Desktop)
21+
bun server.ts --stdio
22+
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+
```
38+
39+
## Key Files
40+
41+
### `src/mcp-app-wrapper.tsx`
42+
Generic wrapper handling MCP connection. Provides `WidgetProps` interface:
43+
44+
```tsx
45+
interface WidgetProps<TToolInput> {
46+
toolInputs: TToolInput | null; // Complete tool input
47+
toolInputsPartial: TToolInput | null; // Streaming partial input
48+
toolResult: CallToolResult | null; // Tool execution result
49+
hostContext: McpUiHostContext | null; // Theme, viewport, locale
50+
callServerTool: App["callServerTool"]; // Call MCP server tools
51+
sendMessage: App["sendMessage"]; // Send chat messages
52+
sendOpenLink: App["sendOpenLink"]; // Open URLs in browser
53+
sendLog: App["sendLog"]; // Debug logging
54+
}
55+
```
56+
57+
### `src/threejs-app.tsx`
58+
Widget component receiving all props. Uses:
59+
- `toolInputs.code` - JavaScript to execute
60+
- `toolInputsPartial.code` - Streaming preview
61+
- `toolInputs.height` - Canvas height
62+
63+
### `server.ts`
64+
MCP server with:
65+
- `show_threejs_scene` tool linked to UI resource
66+
- `learn_threejs` documentation tool
67+
- stdio + HTTP transport support
68+
69+
## Available Three.js Globals
70+
71+
```javascript
72+
THREE // Three.js library
73+
canvas // Pre-created canvas
74+
width, height // Canvas dimensions
75+
OrbitControls // Camera controls
76+
EffectComposer // Post-processing
77+
RenderPass // Render pass
78+
UnrealBloomPass // Bloom effect
79+
```
80+
81+
## Test Input
82+
83+
Copy contents of `test-input.json` to test in basic-host (`http://localhost:8080`).
84+
85+
## Example Code
86+
87+
```javascript
88+
const scene = new THREE.Scene();
89+
const camera = new THREE.PerspectiveCamera(60, width/height, 0.1, 100);
90+
camera.position.set(2, 2, 2);
91+
92+
const renderer = new THREE.WebGLRenderer({canvas, antialias: true});
93+
renderer.setSize(width, height);
94+
renderer.shadowMap.enabled = true;
95+
96+
const cube = new THREE.Mesh(
97+
new THREE.BoxGeometry(),
98+
new THREE.MeshStandardMaterial({color: 0x00ff88})
99+
);
100+
cube.castShadow = true;
101+
cube.position.y = 0.5;
102+
scene.add(cube);
103+
104+
const floor = new THREE.Mesh(
105+
new THREE.PlaneGeometry(5, 5),
106+
new THREE.MeshStandardMaterial({color: 0x222233})
107+
);
108+
floor.rotation.x = -Math.PI/2;
109+
floor.receiveShadow = true;
110+
scene.add(floor);
111+
112+
const light = new THREE.DirectionalLight(0xffffff, 2);
113+
light.position.set(3, 5, 3);
114+
light.castShadow = true;
115+
scene.add(light);
116+
scene.add(new THREE.AmbientLight(0x404040));
117+
118+
function animate() {
119+
requestAnimationFrame(animate);
120+
cube.rotation.y += 0.01;
121+
renderer.render(scene, camera);
122+
}
123+
animate();
124+
```
125+
126+
## Creating a New Widget
127+
128+
1. Copy this example
129+
2. Rename `threejs-app.tsx` to your widget name
130+
3. Define your `ToolInput` interface
131+
4. Implement your widget using the `WidgetProps`
132+
5. Update `server.ts` with your tools
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Three.js Widget</title>
7+
<link rel="stylesheet" href="/src/global.css">
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/mcp-app-wrapper.tsx"></script>
12+
</body>
13+
</html>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "threejs-server",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module",
6+
"scripts": {
7+
"build": "INPUT=mcp-app.html vite build",
8+
"watch": "INPUT=mcp-app.html vite build --watch",
9+
"serve": "bun server.ts",
10+
"start": "NODE_ENV=development npm run build && npm run serve",
11+
"dev": "NODE_ENV=development concurrently 'npm run watch' 'npm run serve'"
12+
},
13+
"dependencies": {
14+
"@modelcontextprotocol/ext-apps": "../..",
15+
"@modelcontextprotocol/sdk": "^1.22.0",
16+
"react": "^19.2.0",
17+
"react-dom": "^19.2.0",
18+
"three": "^0.181.0",
19+
"zod": "^3.25.0"
20+
},
21+
"devDependencies": {
22+
"@types/cors": "^2.8.19",
23+
"@types/express": "^5.0.0",
24+
"@types/node": "^22.0.0",
25+
"@types/react": "^19.2.2",
26+
"@types/react-dom": "^19.2.2",
27+
"@types/three": "^0.181.0",
28+
"@vitejs/plugin-react": "^4.3.4",
29+
"concurrently": "^9.2.1",
30+
"cors": "^2.8.5",
31+
"express": "^5.1.0",
32+
"typescript": "^5.9.3",
33+
"vite": "^6.0.0",
34+
"vite-plugin-singlefile": "^2.3.0"
35+
}
36+
}
167 KB
Loading

0 commit comments

Comments
 (0)