Skip to main content

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 with mounted/destroyed lifecycle:

Installation

Add @mcp-b/global to your JavaScript dependencies:

Bidirectional communication

The power of LiveView + WebMCP is bidirectional:
  1. AI → Server: Tool calls push events to LiveView via this.pushEvent()
  2. Server → Client: LiveView pushes updates to all connected clients
  3. Client → AI: Updated DOM state is visible to AI on next tool call

Common issues

Ensure the hook is registered in app.js and the element has both phx-hook and a unique id attribute.
Check that this.pushEvent() is being called. LiveView hooks must use this.pushEvent(), not regular fetch calls.
LiveView handles this automatically - tool calls go to server, server broadcasts to all clients.

Development

Use Chrome DevTools MCP for AI-driven development - your AI can write, discover, and test tools in real-time.