> ## Documentation Index
> Fetch the complete documentation index at: https://flow.engineer/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Mastra

> 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.

```ts 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
}
```

## How it fits

* `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](https://flow.engineer/docs/unified-messaging-api/concepts/events-and-webhooks.md).
* 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](https://flow.engineer/docs/unified-messaging-api/observe/frameworks.md).

## Full example

[examples/mastra](https://github.com/flow-engineer/sdk/tree/main/examples/mastra) on GitHub.
