Full Example
Production-ready Phoenix LiveView example with real-time sync
Quick start
Why Phoenix LiveView?
Phoenix LiveView’s server-side state management creates a powerful pattern for WebMCP:- Server-authoritative state - Tools call into LiveView, which manages all state
- Real-time sync - Changes push to all connected clients instantly
- No client state bugs - AI tool calls and manual interactions stay in sync
The pattern
Use LiveView JavaScript hooks withmounted/destroyed lifecycle:
Installation
Add@mcp-b/global to your JavaScript dependencies:
Bidirectional communication
The power of LiveView + WebMCP is bidirectional:- AI → Server: Tool calls push events to LiveView via
this.pushEvent() - Server → Client: LiveView pushes updates to all connected clients
- Client → AI: Updated DOM state is visible to AI on next tool call
Common issues
Hook not mounting
Hook not mounting
Ensure the hook is registered in
app.js and the element has both phx-hook and a unique id attribute.Tool calls not reaching server
Tool calls not reaching server
Check that
this.pushEvent() is being called. LiveView hooks must use this.pushEvent(), not regular fetch calls.State out of sync
State out of sync
LiveView handles this automatically - tool calls go to server, server broadcasts to all clients.
