An OpenUI example showing how to wire a Mastra agent backend to OpenUI's generative UI frontend using the AG-UI protocol.
- Using
agUIAdapter()as thestreamProtocolin thellmconfig of OpenUI's<AgentInterface />component - A Mastra
AgentwithcreateTooltools (weather and stock price) running in a Next.js API route - An OpenUI Cloud custom gateway so Mastra talks Completions at
https://api.thesys.dev/v1/embed - Streaming AG-UI protocol events from the server to the client via SSE
- Mint an OpenUI Cloud key into
.env.local:
pnpm generate:apiKey- Install dependencies from this example directory:
pnpm install --ignore-workspace- Run the dev server from this directory:
pnpm devOpen http://localhost:3000 to see the chat interface.
The server (src/app/api/chat/route.ts) wraps a Mastra Agent with @ag-ui/mastra's MastraAgent, which emits AG-UI protocol events. These events are serialized as SSE and streamed to the client.
Mastra's built-in model router treats google/... as Gemini and strips the provider. src/openui-cloud-gateway.ts registers an openui-cloud gateway so the agent model openui-cloud/thesys/google/gemini-3.6-flash-free is sent to OpenUI Cloud Completions as google/gemini-3.6-flash-free. The gateway is registered on the Mastra instance in src/mastra.ts. The provider id is thesys (not google) so Mastra does not apply native-Gemini message rewrites on the OpenAI-compatible Cloud path.
The frontend (src/app/page.tsx) renders OpenUI's <AgentInterface /> (the artifact chat interface with thread history), passing it an llm whose streamProtocol is agUIAdapter() from @openuidev/react-ui. Storage is optional — AgentInterface defaults to in-memory storage (wiped on reload) — so no storage prop is needed. The adapter parses the SSE stream into internal chat events that drive the UI.
To add more tools, define them with createTool in src/tools.ts and pass them to the Agent.
pnpm verify