Skip to content

Ask the docs

Answers come only from these docs and are written by an AI model (Gemini), so check the linked pages. Your question is not kept. Do not include personal details or keys.

Mastra

Mastra · Mastra WhatsApp · Mastra agent Telegram · Mastra iMessage · Mastra messaging integration

Deliver a Mastra agent's streamed replies through Flow Messaging.

Receive messages through Flow, run your Mastra agent and deliver its answer as chat bubbles. Keep the agent’s existing tools and workflows.

TypeScript
// npm install @flow-engineer/messaging @mastra/core @ai-sdk/openai
import { Agent } from "@mastra/core/agent";
import { openai } from "@ai-sdk/openai";
import { FlowMessaging, contentText } from "@flow-engineer/messaging";
const flow = new FlowMessaging();
const agent = new Agent({
id: "bakery-assistant",
name: "Bakery assistant",
instructions: "You help customers of Asha's Bakery with orders and delivery. Keep answers short.",
model: openai("gpt-4.1-mini"),
});
for await (const event of flow.events.stream({ types: ["message.received"] })) {
const stream = await agent.stream(contentText(event.data.message.content));
await event.conversation.reply(stream); // reads stream.textStream
}
  • reply() reads the stream’s textStream, keeps typing on and sends a bubble at each paragraph break.
  • Mastra memory: use the Flow conversation.id as the thread ID and conversation.contact (the contact’s ID) as the resource ID, so each person keeps their own history.
  • In a Mastra server, receive Flow webhooks on a custom route and call the same code; verify signatures with flow.webhooks.constructEvent(rawBody, signatureHeader, secret), which returns the typed event with event.conversation ready to reply on. See Events and webhooks.
  • To see each turn’s model and tool calls and get every reply graded, send this agent’s traces to Flow Observe: Traces from agent frameworks.

examples/mastra on GitHub.