Build an editable WebXR exhibit with your coding agent
An assembly study in one React Three Fiber component: select parts, separate them and inspect them in 3D or WebXR. Its source is below, and your coding agent can apply it through Graspable’s MCP tools.
Graspable 10 min read
Your coding agent can edit a WebXR project directly through Graspable. It supplies the code; Graspable creates the project, builds it, checks that it loads in a browser and opens a preview. You do not need to hand the request to a second AI agent.
Here is a small example you can reproduce: a fictional beacon on an inspection table. Separate its three parts, select one to read about it, rotate the parts, then put everything back. The source is one component in the free React XR template.
The picture above is a real desktop-browser capture. The beacon is made from editable geometry; it is not an engineering model.
The whole component is at the end of this article. It uses only what the React XR template already installs: no models, textures or generation services to buy.
Start with a connected coding agent
Download Graspable, then install its agent connection from Settings → Plugins and connectors → Use Graspable from other tools. Ask your coding agent to follow the MCP setup guide. Claude Code, Codex and other clients that can start an MCP server can use this connection. Their setup steps differ.
Once connected, give the agent this request:
Create a new free React XR project called Beacon inspection. Read https://graspable.dev/blog/build-webxr-with-an-external-agent.md and the project's current components/Workbench.jsx. Replace that file with the component under "The whole component". Keep the template's application, XR setup and other files unchanged. Use Graspable's MCP tools to check the project and open its preview. Do not start a delegated AI run, purchase anything or publish the project. Report the checks that passed and any interactions you could not test.
If you are applying the file yourself, create a new React XR project, open Code, and replace components/Workbench.jsx with the source at the end of this article. Use a new project so the example does not overwrite an exhibit you have already built.
Creating this free project, editing its files and checking it locally do not spend Graspable Credits or require another AI sign-in. Your coding agent's own subscription or API usage terms still apply. Publishing and paid services are separate choices.
How the component works
The scene is three parts described as data. Each entry is a name, the sentence shown when it is selected, and a colour:
const parts = [
{ title: 'Optics', text: 'The lens shapes the light.', color: '#50d6c3' },
{ title: 'Power core', text: 'A copper coil surrounds the core.', color: '#e4a369' },
{ title: 'Radiator', text: 'Spaced fins expose more surface.', color: '#c5d0db' },
]
Adding a part to the data adds it to the table, the selection buttons and the sign above the table.
Each part moves towards where it should be on every frame, so separating, assembling and rotating are smooth without an animation library. MathUtils.damp eases the position, and people who ask their system for reduced motion get the end position at once:
useFrame((_, dt) => {
const target = (index - 1) * (expanded ? 0.53 : 0.23)
root.current.position.x = reduced ? target : MathUtils.damp(root.current.position.x, target, 7, dt)
if (rotating && !reduced) turn.current.rotation.y += Math.min(dt, 0.1) * 0.4
else turn.current.rotation.y = reduced ? 0.35 : MathUtils.damp(turn.current.rotation.y, 0.35, 7, dt)
})
The same state drives two sets of controls. On a flat screen an HTML panel holds buttons that work with a mouse, Tab and Enter. In a headset that panel is gone, and buttons on the table do the same things:
const session = useXR(state => state.session)
<SceneButton name="Separate assembly" label={expanded ? 'Assemble' : 'Separate'}
position={[-0.45, 0.86, -1.05]} color="#30b89b" onPress={() => setExpanded(v => !v)} />
{session && <SceneButton name="Leave XR" label="Leave XR" position={[0.78, 0.86, -1.55]}
color="#c77757" onPress={leave} />}
{!session && <Html fullscreen>{/* the desktop panel */}</Html>}
Label and SceneButton are components the React XR template already has, in the same components folder: text drawn for the headset, and a button that can be pressed with a controller, a hand or a mouse.
What the agent does
The reproducible MCP sequence is:
list_templates, thencreate_projectwithreact-xr-developer.- Wait until
list_projectsreports that installation is done. read_fileforcomponents/Workbench.jsx, thenwrite_filewith the replacement. Read it back to check that the intended source was saved.check_project. Correct build or browser errors before continuing.start_preview, then inspect the running scene at the returned local address.
For this example we authored the component, refined it while viewing the scene, and applied the final file to a fresh project through Graspable 0.18.1's MCP tools. The file readback, build, browser check and preview passed on macOS. This was a direct-edit test with no delegated model run. It was not a test of every named client's connection or a claim that one prompt always produces this result.
A successful check proves less than a working exhibit. Our first build passed while its desktop controls were below the viewport. Looking at the actual preview caught that mistake. After correcting it, we tested the controls themselves.
Try the interactions
Select Separate parts. The optics, power core and radiator move apart. Choose a part in the control panel, or click its geometry. The sign and selection ring should agree with your choice.
Choose Rotate parts to inspect the shapes, then Face front to return them to their starting orientation. Reset assembles the parts, stops rotation and selects the optics. The Inspect the beacon heading hides or shows the desktop controls. Those controls also work with Tab and Enter.
In an immersive session, the HTML panel disappears. Buttons on the table provide separation, rotation, reset and Leave XR. We exercised selection, separation and exit using an emulated controller in IWER, as well as desktop mouse and keyboard controls. A narrow browser layout was also checked at 390 pixels wide.
This example has not had a physical-headset or phone performance test. Emulation does not establish hand tracking, readable text in a headset or sustained frame rate. It does not detect a real table on iPhone: this is a WebXR inspection exhibit, not a markerless phone-AR placement example. For that distinction, see AR on phones and headsets.
Make a change that you can check
Try a bounded follow-up:
Add a fourth part called Protective housing. Give it its own selectable geometry and description, include it in separation and reset, and keep all four parts visible in a narrow browser window. Preserve the desktop keyboard controls and the in-world exit button. Check the build, then verify selection and reset in the preview.
You retain the component and its state as ordinary source. An agent can change the geometry, spacing, materials, descriptions and behavior; you can edit the same file yourself. For an actual product, replace the fictional parts with models you own and write descriptions based on that product.
The useful handover is the editable project, the running preview and a clear record of what was tested. Use Graspable with your AI agent covers the connection, access limits and optional delegated workflow.
The whole component
This replaces components/Workbench.jsx in a new React XR project. The imports at the top are the template's own: React, React Three Fiber, drei, React XR and three are installed with it, and ./Label and ./SceneButton sit beside this file.
import { useEffect, useRef, useState } from 'react'
import { Html } from '@react-three/drei'
import { useFrame, useThree } from '@react-three/fiber'
import { useXR } from '@react-three/xr'
import { MathUtils } from 'three'
import { Label } from './Label'
import { SceneButton } from './SceneButton'
// Original fictional assembly. Replace components/Workbench.jsx in React XR.
const parts = [
{ title: 'Optics', text: 'The lens shapes the light.', color: '#50d6c3' },
{ title: 'Power core', text: 'A copper coil surrounds the core.', color: '#e4a369' },
{ title: 'Radiator', text: 'Spaced fins expose more surface.', color: '#c5d0db' },
]
function Metal({ color = '#6e7a87', ...props }) {
return <meshStandardMaterial color={color} metalness={0.65} roughness={0.28} {...props} />
}
function Part({ index, selected, expanded, rotating, reduced, onSelect }) {
const root = useRef(null)
const turn = useRef(null)
const part = parts[index]
useFrame((_, dt) => {
const target = (index - 1) * (expanded ? 0.53 : 0.23)
root.current.position.x = reduced ? target : MathUtils.damp(root.current.position.x, target, 7, dt)
if (rotating && !reduced) turn.current.rotation.y += Math.min(dt, 0.1) * 0.4
else turn.current.rotation.y = reduced ? 0.35 : MathUtils.damp(turn.current.rotation.y, 0.35, 7, dt)
})
return (
<group ref={root} position={[(index - 1) * 0.23, 1.2, -1.42]}>
<group ref={turn} rotation={[0, 0.35, 0]} name={`Inspect ${part.title}`} onPointerDown={e => { e.stopPropagation(); onSelect(index) }}>
<mesh rotation={[0, 0, Math.PI / 2]} castShadow>
<cylinderGeometry args={[0.17, 0.17, 0.2, 48]} />
<Metal color={index === 1 ? '#69452c' : '#536373'} />
</mesh>
{index === 0 && <>
<mesh position={[-0.108, 0, 0]} rotation={[0, Math.PI / 2, 0]}>
<circleGeometry args={[0.14, 48]} />
<meshStandardMaterial color={part.color} metalness={0.4} roughness={0.16} emissive={part.color} emissiveIntensity={0.2} side={2} />
</mesh>
{[0.07, 0.11, 0.15].map(r => <mesh key={r} position={[-0.112, 0, 0]} rotation={[0, Math.PI / 2, 0]}>
<torusGeometry args={[r, 0.005, 8, 48]} /><Metal color="#b7e8df" />
</mesh>)}
</>}
{index === 1 && Array.from({ length: 9 }, (_, n) => <mesh key={n} position={[-0.088 + n * 0.022, 0, 0]} rotation={[0, Math.PI / 2, 0]} castShadow>
<torusGeometry args={[0.18, 0.009, 8, 40]} /><Metal color={part.color} />
</mesh>)}
{index === 2 && Array.from({ length: 7 }, (_, n) => <mesh key={n} position={[-0.09 + n * 0.03, 0, 0]} rotation={[0, 0, Math.PI / 2]} castShadow>
<cylinderGeometry args={[0.215, 0.215, 0.012, 48]} /><Metal color={part.color} />
</mesh>)}
</group>
<mesh position={[0, -0.345, 0]} rotation={[-Math.PI / 2, 0, 0]} pointerEvents="none">
<ringGeometry args={[0.12, selected ? 0.14 : 0.125, 48]} />
<meshBasicMaterial color={selected ? '#4ff0c7' : '#727e8b'} />
</mesh>
<Label text={part.title} height={0.028} position={[0, -0.29, 0.12]} color={selected ? '#a3ffe6' : '#f5f7fa'} />
</group>
)
}
export function Workbench() {
const [expanded, setExpanded] = useState(false)
const [rotating, setRotating] = useState(false)
const [selected, setSelected] = useState(0)
const [reduced, setReduced] = useState(false)
const [exitError, setExitError] = useState('')
const session = useXR(state => state.session)
const camera = useThree(state => state.camera)
const aspect = useThree(state => state.size.width / state.size.height)
useEffect(() => {
if (session || !camera.isPerspectiveCamera) return
// Keep the entire table visible in narrow desktop/browser previews.
const distance = 2.05 / (2 * Math.tan(MathUtils.degToRad(camera.fov / 2)) * aspect)
camera.position.set(0, 1.6, Math.max(0.35, distance - 1.42))
camera.lookAt(0, 1.05, -1.42)
}, [aspect, camera, session])
useEffect(() => {
const query = window.matchMedia('(prefers-reduced-motion: reduce)')
const sync = () => setReduced(query.matches)
sync()
query.addEventListener('change', sync)
return () => query.removeEventListener('change', sync)
}, [])
const reset = () => { setExpanded(false); setRotating(false); setSelected(0) }
const leave = () => { setExitError(''); session?.end().catch(() => setExitError('Use the headset menu to leave.')) }
return <>
<group name="Inspection table">
<mesh position={[0, 0.8, -1.42]} castShadow receiveShadow>
<boxGeometry args={[1.85, 0.1, 1.05]} /><Metal color="#29343f" roughness={0.7} />
</mesh>
{[-0.7, 0.7].map(x => <mesh key={x} position={[x, 0.38, -1.42]} castShadow>
<boxGeometry args={[0.08, 0.76, 0.7]} /><Metal color="#48515a" />
</mesh>)}
<mesh position={[0, 1.64, -1.85]} castShadow>
<boxGeometry args={[1.8, 0.27, 0.035]} /><meshStandardMaterial color="#17232d" roughness={0.8} />
</mesh>
<Label text="Beacon / assembly study" height={0.045} position={[0, 1.7, -1.827]} />
<Label text={`${parts[selected].title}: ${parts[selected].text}`} height={0.031} position={[0, 1.6, -1.826]} color="#bfe7de" />
{parts.map((part, index) => <Part key={part.title} index={index} selected={selected === index} expanded={expanded} rotating={rotating} reduced={reduced} onSelect={setSelected} />)}
<SceneButton name="Separate assembly" label={expanded ? 'Assemble' : 'Separate'} position={[-0.45, 0.86, -1.05]} color="#30b89b" onPress={() => setExpanded(v => !v)} />
{!reduced && <SceneButton name="Rotate assembly" label={rotating ? 'Face front' : 'Rotate'} position={[0, 0.86, -1.05]} color="#8493a3" onPress={() => setRotating(v => !v)} />}
<SceneButton name="Reset assembly" label="Reset" position={[0.45, 0.86, -1.05]} color="#8493a3" onPress={reset} />
{session && <SceneButton name="Leave XR" label="Leave XR" position={[0.78, 0.86, -1.55]} color="#c77757" onPress={leave} />}
{exitError && <Label text={exitError} height={0.035} position={[0, 1.9, -1.85]} />}
</group>
{!session && <Html fullscreen calculatePosition={(_, __, size) => [size.width / 2, size.height / 2]} style={{ pointerEvents: 'none' }}>
<style>{`
.beacon-controls { position:absolute; bottom:18px; left:50%; transform:translateX(-50%); width:min(560px,calc(100% - 32px)); box-sizing:border-box; padding:14px 16px; color:#f5f7fa; background:#17232df5; border:1px solid #697b8b; border-radius:8px; font:14px/1.4 system-ui,sans-serif; pointer-events:auto; }
.beacon-controls p { margin:0 0 10px; }
.beacon-controls .beacon-row { display:flex; flex-wrap:wrap; gap:8px; margin-top:8px; }
.beacon-controls button { min-height:40px; border:1px solid #8394a4; border-radius:4px; background:#263746; color:#fff; padding:7px 12px; font:inherit; cursor:pointer; }
.beacon-controls button[aria-pressed="true"] { background:#105b4d; border-color:#70e7c7; }
.beacon-controls button:focus-visible { outline:3px solid #70e7c7; outline-offset:3px; }
.beacon-controls button:disabled { opacity:0.6; cursor:default; }
.beacon-controls summary { cursor:pointer; font-weight:600; }
@media(max-width:600px) { .beacon-controls { bottom:8px; padding:10px; font-size:13px; } }
`}</style>
<details className="beacon-controls" open>
<summary>Inspect the beacon</summary>
<p aria-live="polite">{parts[selected].title}: {parts[selected].text}</p>
<div className="beacon-row" aria-label="Choose a part">
{parts.map((p, i) => <button key={p.title} aria-pressed={i === selected} onClick={() => setSelected(i)}>{p.title}</button>)}
</div>
<div className="beacon-row" aria-label="Assembly controls">
<button aria-pressed={expanded} onClick={() => setExpanded(v => !v)}>{expanded ? 'Assemble' : 'Separate parts'}</button>
<button aria-pressed={rotating && !reduced} disabled={reduced} onClick={() => setRotating(v => !v)}>{reduced ? 'Reduced motion' : rotating ? 'Face front' : 'Rotate parts'}</button>
<button onClick={reset}>Reset</button>
</div>
</details>
</Html>}
</>
}