mastra-agentic-ui
Skill2 starsChanged 29 days agoApache-2.0View on GitHub
Build chat and generative UIs on top of Mastra agents and workflows with @mastra/ai-sdk and AI SDK UI (useChat, useCompletion), assistant-ui, CopilotKit, and OpenUI. Covers chatRoute, workflowRoute, networkRoute, handleChatStream, handleWorkflowStream, toAISdkStream, message converters, tool part rendering, custom data parts from writer.custom, browser-side client tools, suspended tool resume, memory-aware requests, and SSE heartbeats. Use this skill whenever the user wants a chat interface, streaming UI, tool cards, workflow progress UI, or wants to connect a React, Vue, Svelte, or Angular frontend to a Mastra agent. Triggers on @mastra/ai-sdk, chatRoute, handleChatStream, toAISdkStream, toAISdkMessages, toAISdkV5Messages, withSseHeartbeat, withMastra, useChat, DefaultChatTransport, prepareSendMessagesRequest, addToolOutput, tool-weatherTool part, data-workflow, assistant-ui, useChatRuntime, CopilotKit, registerCopilotKit, @ag-ui/mastra, OpenUI, AgentInterface.
In this repository46 other files it gives its agents.
- mastra-a2a-acpSkill
- mastra-agent-approval-hitlSkill
- mastra-agent-skillsSkill
- mastra-agentsSkill
- mastra-authSkill
- mastra-browserSkill
- mastra-build-playbookSkill
- mastra-channelsSkill
- mastra-classifierSkill
- mastra-cli-apiSkill
- mastra-client-sdkSkill
- mastra-datasets-experimentsSkill
- mastra-deploymentSkill
- mastra-evalsSkill
- mastra-file-based-agentsSkill
- mastra-framework-integrationSkill
- mastra-guideSkill
- mastra-harnessSkill
- mastra-knowledgeSkill
- mastra-mcpSkill
- mastra-memorySkill
- mastra-migrationsSkill
- mastra-modelsSkill
- mastra-multi-agentSkill
- mastra-observabilitySkill
- mastra-observational-memorySkill
- mastra-platformSkill
- mastra-processors-guardrailsSkill
- mastra-project-setupSkill
- mastra-ragSkill
- mastra-schedulesSkill
- mastra-semantic-recallSkill
- mastra-serverSkill
- mastra-signalsSkill
- mastra-storageSkill
- mastra-streamingSkill
- mastra-structured-outputSkill
- mastra-studio-editorSkill
- mastra-tool-integrationsSkill
- mastra-toolsSkill
- mastra-troubleshootingSkill
- mastra-vector-storesSkill
- mastra-voiceSkill
- mastra-workflowsSkill
- mastra-workflow-suspend-resumeSkill
- mastra-workspace-sandboxSkill
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 111
Verify the API: If node_modules/@mastra/ai-sdk exists, read its dist/docs/ and types.
- Distinctive: line 117
Add client tools or resume flows: if needed (deep dive below).
- Very specific: line 162
CopilotKit returns 500 after
mastra build: Add @copilotkit/runtime to bundler.externals.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Mastra Agentic UI
Stream Mastra agents and workflows into a frontend chat or generative UI, using AI SDK UI or an AG-UI based library.
When to use
- Building a chat page with
useChat()against a Mastra agent - Rendering tool calls, workflow steps, or progress events as components
- Running tools in the browser and feeding results back to the agent
- Resuming a suspended tool or approving a tool call from the UI
- Converting stored memory messages into
UIMessage[]for initial history - Using assistant-ui, CopilotKit, or OpenUI as the frontend
- Wrapping an existing AI SDK model with Mastra memory or processors (
withMastra)
When NOT to use
- Framework scaffolding and where to put the route file (use
mastra-framework-integration) - Calling agents from a frontend without AI SDK UI (use
mastra-client-sdk) - Stream chunk types on the server (
fullStream,textStream) (usemastra-streaming) - Tool definitions and
writeron the tool side (usemastra-tools) - Approval and suspension design (use
mastra-agent-approval-hitlandmastra-workflow-suspend-resume) - Server middleware, CORS, custom route plumbing (use
mastra-server) - Protecting the chat route (use
mastra-auth)
Required inputs
- The UI library: AI SDK UI (
@ai-sdk/react,@ai-sdk/vue,@ai-sdk/svelte,@ai-sdk/angular), assistant-ui, CopilotKit, or OpenUI - The installed
aimajor version (v5, v6, or v7) - Where the route runs: Mastra server (
chatRoute) or your framework (handleChatStream) - The agent or workflow ID, and whether memory is on
- Which tool outputs or events need custom rendering
The framework: Mastra stream in, AI SDK parts out
@mastra/ai-sdk converts Mastra streams into AI SDK UI message streams. Pick the helper by where the route lives:
| Target | On the Mastra server | In your own route |
| Agent chat | chatRoute() | handleChatStream() |
| Workflow | workflowRoute() | handleWorkflowStream() |
| Agent network (deprecated) | networkRoute() | handleNetworkStream() |
| Anything custom | registerApiRoute() + toAISdkStream() | toAISdkStream() |
// Mastra server: src/mastra/index.ts
import { Mastra } from '@mastra/core'
import { chatRoute } from '@mastra/ai-sdk'
export const mastra = new Mastra({
server: {
apiRoutes: [chatRoute({ path: '/chat/:agentId', version: 'v7' })],
},
})// Frontend
import { useChat } from '@ai-sdk/react'
import { DefaultChatTransport } from 'ai'
export function Chat() {
const { messages, sendMessage } = useChat({
transport: new DefaultChatTransport({ api: 'http://localhost:4111/chat/weatherAgent' }),
})
return (
<div>
{messages.map(m => (
<div key={m.id}>
{m.parts.map((part, i) => (part.type === 'text' ? <p key={i}>{part.text}</p> : null))}
</div>
))}
<button onClick={() => sendMessage({ text: 'Weather in Paris?' })}>Ask</button>
</div>
)
}Parts you will render
| Part type | Source | Notes |
text | Agent text | Plain text. |
tool-{toolKey} | Tool calls | States input-streaming, input-available, output-available, output-error. toolKey is the key in the agent's tools object. |
data-workflow, data-workflow-step | Workflow routes | Snapshot and per-step delta. |
data-tool-agent, data-tool-agent-step | Subagents | Disable with includeSubAgentMetadata: false. |
data-tool-call-suspended, data-tool-call-approval | Suspended or approval tools | Carry runId and toolCallId. |
data-structured-output | structuredOutput | Final object only. |
data-{custom} | await context.writer.custom({ type: 'data-...', data }) in a tool | Must start with data-. |
Workflow
- Verify the API. If
node_modules/@mastra/ai-sdkexists, read itsdist/docs/and types. Otherwise fetchhttps://mastra.ai/integrations/agentic-ui/ai-sdk-ui.mdandhttps://mastra.ai/reference/ai-sdk/chat-route.md. Trust these over memory. - Install
@mastra/ai-sdk,ai, and the UI binding for your framework. - Match versions. Pass
version: 'v6'or'v7'to routes, handlers,toAISdkStream(), andtoAISdkMessages()when the installedaiis not v5. - Add the route.
chatRoute()inserver.apiRoutes, orhandleChatStream()wrapped increateUIMessageStreamResponse(). - Wire memory. In
prepareSendMessagesRequest, send only the last message plusmemory: { thread, resource }. Load history withtoAISdkMessages(). - Render parts. Switch on
part.typeand the toolstate. Give tools anoutputSchemaso the UI knows the output shape. - Add client tools or resume flows if needed (deep dive below).
- Keep connections alive behind proxies with
heartbeatMson routes orwithSseHeartbeat(). - Test the stream in the browser and check the network tab for SSE chunks.
Deep dive: client tools and resume from useChat()
A tool without execute runs in the browser. The run ends at that step with a tool-{toolKey} part in input-available. Run the tool, add its output, and resend:
import { useChat } from '@ai-sdk/react'
import { DefaultChatTransport, lastAssistantMessageIsCompleteWithToolCalls } from 'ai'
const { messages, sendMessage, addToolOutput } = useChat({
transport: new DefaultChatTransport({ api: 'http://localhost:4111/chat' }),
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,
async onToolCall({ toolCall }) {
if (toolCall.toolName === 'addToCart') {
const input = toolCall.input as { productId: string }
const cart = await addToCartInBrowser(input.productId)
// Don't await addToolOutput inside onToolCall
addToolOutput({ tool: 'addToCart', toolCallId: toolCall.toolCallId, output: cart })
}
},
})Subagents can't run client tools. Define them on the agent the route talks to.
To resume a suspended tool, send runId and resumeData in the body (from the latest data-tool-call-suspended part). chatRoute() and handleChatStream() then call agent.resumeStream(). See references/examples.md.
Failure patterns
- Type errors on
UIMessageor stream parts. The helper emits v5 by default. Passversionmatching the installedai. - Duplicate or out-of-order messages with memory. The client sends the full history. Send only the last message with
memory.threadandmemory.resource. - Duplicated assistant message with
toAISdkStream(). PassoriginalMessages: messagestocreateUIMessageStream(). WritableStream is locked. Awriter.custom()call was not awaited.- Custom event never reaches the UI. Its
typedoes not start withdata-. - Tool part never matches. The UI checks
tool-get-weather(the toolid) instead oftool-weatherTool(thetoolsobject key). - Generation stops when the tab closes.
chatRoute()forwards the request abort signal. Build a custom route withconsumeStream()to keep going (seemastra-server). - Editing or regenerating a message does not change stored history. With memory, storage is the source of truth. Update stored messages, then send a new message.
- Stream drops during long tool calls. A proxy closed the idle connection. Set
heartbeatMsor wrap withwithSseHeartbeat(). - CopilotKit returns 500 after
mastra build. Add@copilotkit/runtimetobundler.externals. - New code built on
networkRoute(). Agent networks are deprecated. Use a supervisor agent withchatRoute()(seemastra-multi-agent).
Output format
A finished agentic UI contains:
- The route:
chatRoute()/workflowRoute()insrc/mastra/index.ts, or a framework route with ahandle*Stream()helper - The chat component with part rendering for text, each tool, and custom data parts
- Memory wiring (thread and resource from app state) and a history loader
- Any client tools and resume handling, with the matching server-side tool definitions
- A manual test note covering a tool call, an error state, and a reload with history
Reference files
references/api.md- Every@mastra/ai-sdkexport with options and defaults.references/libraries.md- assistant-ui, CopilotKit, and OpenUI setup and options.references/examples.md- Tool cards, custom progress events, request data to request context, suspended tool resume, workflow streaming.references/checklist.md- Agentic UI checklist.
Live docs
- https://mastra.ai/integrations/agentic-ui/ai-sdk-ui
- https://mastra.ai/integrations/agentic-ui/assistant-ui
- https://mastra.ai/integrations/agentic-ui/copilotkit
- https://mastra.ai/integrations/agentic-ui/openui
- https://mastra.ai/reference/ai-sdk/chat-route
- https://mastra.ai/reference/ai-sdk/handle-chat-stream
- https://mastra.ai/reference/ai-sdk/to-ai-sdk-stream
- https://mastra.ai/reference/ai-sdk/to-ai-sdk-messages
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

