@mastra/mcp-docs-server 1.2.1-alpha.2 → 1.2.1-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.docs/docs/agents/background-tasks.md +89 -14
- package/.docs/docs/agents/durable-agents.md +231 -0
- package/.docs/docs/agents/using-tools.md +52 -0
- package/.docs/docs/getting-started/build-with-ai.md +273 -8
- package/.docs/docs/server/pubsub.md +2 -2
- package/.docs/docs/workflows/overview.md +71 -0
- package/.docs/guides/build-your-ui/ai-sdk-ui.md +3 -3
- package/.docs/guides/concepts/streaming.md +317 -0
- package/.docs/guides/getting-started/quickstart.md +1 -1
- package/.docs/models/index.md +1 -1
- package/.docs/models/providers/opencode.md +2 -1
- package/.docs/reference/agents/durable-agent.md +30 -1
- package/.docs/reference/agents/getDefaultOptions.md +1 -1
- package/.docs/reference/agents/getDefaultStreamOptions.md +1 -1
- package/.docs/reference/agents/inngest-agent.md +339 -0
- package/.docs/reference/ai-sdk/handle-workflow-stream.md +1 -1
- package/.docs/reference/ai-sdk/workflow-route.md +1 -1
- package/.docs/reference/index.md +1 -0
- package/.docs/reference/streaming/workflows/timeTravelStream.md +1 -1
- package/.docs/reference/tools/create-tool.md +1 -1
- package/.docs/reference/workflows/run.md +1 -1
- package/CHANGELOG.md +7 -0
- package/package.json +5 -5
- package/.docs/docs/build-with-ai/mcp-docs-server.md +0 -238
- package/.docs/docs/build-with-ai/skills.md +0 -63
- package/.docs/docs/streaming/background-task-streaming.md +0 -80
- package/.docs/docs/streaming/events.md +0 -148
- package/.docs/docs/streaming/overview.md +0 -136
- package/.docs/docs/streaming/tool-streaming.md +0 -189
- package/.docs/docs/streaming/workflow-streaming.md +0 -109
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
# Mastra skills
|
|
2
|
-
|
|
3
|
-
Mastra Skills are folders of instructions, scripts, and resources that agents can discover and use to gain Mastra knowledge. They contain setup instructions, best practices, and methods to fetch up-to-date information from Mastra's documentation.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
To install all available Mastra skills, run the following command:
|
|
8
|
-
|
|
9
|
-
**npm**:
|
|
10
|
-
|
|
11
|
-
```bash
|
|
12
|
-
npx skills add mastra-ai/skills
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
**pnpm**:
|
|
16
|
-
|
|
17
|
-
```bash
|
|
18
|
-
pnpm dlx skills add mastra-ai/skills
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
**Yarn**:
|
|
22
|
-
|
|
23
|
-
```bash
|
|
24
|
-
yarn dlx skills add mastra-ai/skills
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
**Bun**:
|
|
28
|
-
|
|
29
|
-
```bash
|
|
30
|
-
bun x skills add mastra-ai/skills
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
Mastra skills work with any coding agent that supports the [Skills standard](https://agentskills.io/), including Claude Code, Cursor, Codex, OpenCode, and others.
|
|
34
|
-
|
|
35
|
-
They're also available on [GitHub](https://github.com/mastra-ai/skills).
|
|
36
|
-
|
|
37
|
-
## Update skill
|
|
38
|
-
|
|
39
|
-
To update to the latest version of the Mastra skill, run:
|
|
40
|
-
|
|
41
|
-
**npm**:
|
|
42
|
-
|
|
43
|
-
```bash
|
|
44
|
-
npx skills update mastra
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
**pnpm**:
|
|
48
|
-
|
|
49
|
-
```bash
|
|
50
|
-
pnpm dlx skills update mastra
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
**Yarn**:
|
|
54
|
-
|
|
55
|
-
```bash
|
|
56
|
-
yarn dlx skills update mastra
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
**Bun**:
|
|
60
|
-
|
|
61
|
-
```bash
|
|
62
|
-
bun x skills update mastra
|
|
63
|
-
```
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
# Background task streaming
|
|
2
|
-
|
|
3
|
-
**Added in:** `@mastra/core@1.29.0`
|
|
4
|
-
|
|
5
|
-
`mastra.backgroundTaskManager.stream()` returns a `ReadableStream` of [background task](https://mastra.ai/docs/agents/background-tasks) lifecycle events. Use it to monitor running tasks across the system, for example to drive a status dashboard, surface progress in your own UI, or pipe events into an SSE response.
|
|
6
|
-
|
|
7
|
-
The stream emits the same chunk types that appear inside `Agent.streamUntilIdle()` (`background-task-running`, `background-task-output`, `background-task-completed`, `background-task-failed`, `background-task-cancelled`). See [background task chunks](https://mastra.ai/reference/streaming/ChunkType) for the full payload shapes.
|
|
8
|
-
|
|
9
|
-
> **Note:** Background tasks must be [enabled on the Mastra instance](https://mastra.ai/reference/configuration) before `backgroundTaskManager` is available. When disabled, `mastra.backgroundTaskManager` is `undefined`.
|
|
10
|
-
|
|
11
|
-
## Subscribe to all task events
|
|
12
|
-
|
|
13
|
-
Calling `stream()` with no filter returns a stream of every task event in the system. On connection, the stream emits a snapshot of all currently running tasks, then forwards live events as they happen.
|
|
14
|
-
|
|
15
|
-
```typescript
|
|
16
|
-
const bgManager = mastra.backgroundTaskManager
|
|
17
|
-
if (!bgManager) throw new Error('Background tasks are not enabled')
|
|
18
|
-
|
|
19
|
-
const controller = new AbortController()
|
|
20
|
-
const stream = bgManager.stream({ abortSignal: controller.signal })
|
|
21
|
-
|
|
22
|
-
for await (const chunk of stream) {
|
|
23
|
-
switch (chunk.type) {
|
|
24
|
-
case 'background-task-running':
|
|
25
|
-
console.log('started', chunk.payload.taskId, chunk.payload.toolName)
|
|
26
|
-
break
|
|
27
|
-
case 'background-task-completed':
|
|
28
|
-
console.log('done', chunk.payload.taskId, chunk.payload.result)
|
|
29
|
-
break
|
|
30
|
-
case 'background-task-failed':
|
|
31
|
-
console.error('failed', chunk.payload.taskId, chunk.payload.error)
|
|
32
|
-
break
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
The stream stays open until the caller's `AbortSignal` fires. Always pass an `abortSignal` so you can disconnect cleanly.
|
|
38
|
-
|
|
39
|
-
## Filter the stream
|
|
40
|
-
|
|
41
|
-
Pass any combination of filter options to narrow the events you receive. Filters apply to both the initial snapshot and the live event subscription.
|
|
42
|
-
|
|
43
|
-
```typescript
|
|
44
|
-
const stream = bgManager.stream({
|
|
45
|
-
agentId: 'researcher',
|
|
46
|
-
threadId: 't1',
|
|
47
|
-
resourceId: 'u1',
|
|
48
|
-
abortSignal: controller.signal,
|
|
49
|
-
})
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
| Filter | Description |
|
|
53
|
-
| ------------- | --------------------------------------------------- |
|
|
54
|
-
| `agentId` | Only events from tasks dispatched by this agent |
|
|
55
|
-
| `runId` | Only events from this specific agent run |
|
|
56
|
-
| `threadId` | Only events from tasks scoped to this memory thread |
|
|
57
|
-
| `resourceId` | Only events from tasks scoped to this resource |
|
|
58
|
-
| `taskId` | Only events for a single task |
|
|
59
|
-
| `abortSignal` | Closes the stream when the signal aborts |
|
|
60
|
-
|
|
61
|
-
## Look up task state directly
|
|
62
|
-
|
|
63
|
-
For one-off lookups instead of a live stream, use `getTask` and `listTasks`:
|
|
64
|
-
|
|
65
|
-
```typescript
|
|
66
|
-
const task = await mastra.backgroundTaskManager?.getTask(taskId)
|
|
67
|
-
const { tasks, total } = await mastra.backgroundTaskManager?.listTasks({
|
|
68
|
-
status: 'running',
|
|
69
|
-
agentId: 'researcher',
|
|
70
|
-
})
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
These read from storage rather than the pubsub stream, so they're suitable for paginated lists and detail views.
|
|
74
|
-
|
|
75
|
-
## Related
|
|
76
|
-
|
|
77
|
-
- [Background tasks](https://mastra.ai/docs/agents/background-tasks)
|
|
78
|
-
- [`Agent.streamUntilIdle()` reference](https://mastra.ai/reference/streaming/agents/streamUntilIdle)
|
|
79
|
-
- [Background task chunks](https://mastra.ai/reference/streaming/ChunkType)
|
|
80
|
-
- [backgroundTasks configuration reference](https://mastra.ai/reference/configuration)
|
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
# Streaming events
|
|
2
|
-
|
|
3
|
-
Streaming from agents or workflows provides real-time visibility into either the LLM’s output or the status of a workflow run. This feedback can be passed directly to the user, or used within applications to handle workflow status more effectively, creating a smoother and more responsive experience.
|
|
4
|
-
|
|
5
|
-
Events emitted from agents or workflows represent different stages of generation and execution, such as when a run starts, when text is produced, or when a tool is invoked.
|
|
6
|
-
|
|
7
|
-
## Event types
|
|
8
|
-
|
|
9
|
-
Below is a complete list of events emitted from `.stream()`. Depending on whether you’re streaming from an **agent** or a **workflow**, only a subset of these events will occur:
|
|
10
|
-
|
|
11
|
-
- **start**: Marks the beginning of an agent or workflow run.
|
|
12
|
-
- **step-start**: Indicates a workflow step has begun execution.
|
|
13
|
-
- **text-delta**: Incremental text chunks as they're generated by the LLM.
|
|
14
|
-
- **tool-call**: When the agent decides to use a tool, including the tool name and arguments.
|
|
15
|
-
- **tool-result**: The result returned from tool execution.
|
|
16
|
-
- **step-finish**: Confirms that a specific step has fully finalized, and may include metadata like the finish reason for that step.
|
|
17
|
-
- **finish**: When the agent or workflow completes, including usage statistics.
|
|
18
|
-
|
|
19
|
-
## Network event types
|
|
20
|
-
|
|
21
|
-
When using `agent.network()` for multi-agent collaboration, additional event types are emitted to track the orchestration flow:
|
|
22
|
-
|
|
23
|
-
- **routing-agent-start**: The routing agent begins analyzing the task to decide which primitive (agent/workflow/tool) to delegate to.
|
|
24
|
-
- **routing-agent-text-delta**: Incremental text as the routing agent processes the response from the selected primitive.
|
|
25
|
-
- **routing-agent-end**: The routing agent completes its selection, including the selected primitive and reason for selection.
|
|
26
|
-
- **agent-execution-start**: A delegated agent begins execution.
|
|
27
|
-
- **agent-execution-end**: A delegated agent completes execution.
|
|
28
|
-
- **agent-execution-event-\***: Events from the delegated agent's execution (e.g., `agent-execution-event-text-delta`).
|
|
29
|
-
- **workflow-execution-start**: A delegated workflow begins execution.
|
|
30
|
-
- **workflow-execution-end**: A delegated workflow completes execution.
|
|
31
|
-
- **workflow-execution-event-\***: Events from the delegated workflow's execution.
|
|
32
|
-
- **tool-execution-start**: A delegated tool begins execution.
|
|
33
|
-
- **tool-execution-end**: A delegated tool completes execution.
|
|
34
|
-
- **network-execution-event-step-finish**: A network iteration step completes.
|
|
35
|
-
- **network-execution-event-finish**: The entire network execution completes.
|
|
36
|
-
|
|
37
|
-
## Inspecting agent streams
|
|
38
|
-
|
|
39
|
-
Iterate over the `stream` with a `for await` loop to inspect all emitted event chunks.
|
|
40
|
-
|
|
41
|
-
```typescript
|
|
42
|
-
const testAgent = mastra.getAgent('testAgent')
|
|
43
|
-
|
|
44
|
-
const stream = await testAgent.stream([{ role: 'user', content: 'Help me organize my day' }])
|
|
45
|
-
|
|
46
|
-
for await (const chunk of stream) {
|
|
47
|
-
console.log(chunk)
|
|
48
|
-
}
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
> **Info:** Visit [Agent.stream()](https://mastra.ai/reference/streaming/agents/stream) for more information.
|
|
52
|
-
|
|
53
|
-
### Example agent output
|
|
54
|
-
|
|
55
|
-
Below is an example of events that may be emitted. Each event always includes a `type` and can include additional fields like `from` and `payload`.
|
|
56
|
-
|
|
57
|
-
```typescript
|
|
58
|
-
{
|
|
59
|
-
type: 'start',
|
|
60
|
-
from: 'AGENT',
|
|
61
|
-
// ..
|
|
62
|
-
}
|
|
63
|
-
{
|
|
64
|
-
type: 'step-start',
|
|
65
|
-
from: 'AGENT',
|
|
66
|
-
payload: {
|
|
67
|
-
messageId: 'msg-cdUrkirvXw8A6oE4t5lzDuxi',
|
|
68
|
-
// ...
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
{
|
|
72
|
-
type: 'tool-call',
|
|
73
|
-
from: 'AGENT',
|
|
74
|
-
payload: {
|
|
75
|
-
toolCallId: 'call_jbhi3s1qvR6Aqt9axCfTBMsA',
|
|
76
|
-
toolName: 'testTool'
|
|
77
|
-
// ..
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
## Inspecting workflow streams
|
|
83
|
-
|
|
84
|
-
Iterate over the `stream` with a `for await` loop to inspect all emitted event chunks.
|
|
85
|
-
|
|
86
|
-
```typescript
|
|
87
|
-
const testWorkflow = mastra.getWorkflow('testWorkflow')
|
|
88
|
-
|
|
89
|
-
const run = await testWorkflow.createRun()
|
|
90
|
-
|
|
91
|
-
const stream = await run.stream({
|
|
92
|
-
inputData: {
|
|
93
|
-
value: 'initial data',
|
|
94
|
-
},
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
for await (const chunk of stream) {
|
|
98
|
-
console.log(chunk)
|
|
99
|
-
}
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
### Example workflow output
|
|
103
|
-
|
|
104
|
-
Below is an example of events that may be emitted. Each event always includes a `type` and can include additional fields like `from` and `payload`.
|
|
105
|
-
|
|
106
|
-
```typescript
|
|
107
|
-
{
|
|
108
|
-
type: 'workflow-start',
|
|
109
|
-
runId: '221333ed-d9ee-4737-922b-4ab4d9de73e6',
|
|
110
|
-
from: 'WORKFLOW',
|
|
111
|
-
// ...
|
|
112
|
-
}
|
|
113
|
-
{
|
|
114
|
-
type: 'workflow-step-start',
|
|
115
|
-
runId: '221333ed-d9ee-4737-922b-4ab4d9de73e6',
|
|
116
|
-
from: 'WORKFLOW',
|
|
117
|
-
payload: {
|
|
118
|
-
stepName: 'step-1',
|
|
119
|
-
args: { value: 'initial data' },
|
|
120
|
-
stepCallId: '9e8c5217-490b-4fe7-8c31-6e2353a3fc98',
|
|
121
|
-
startedAt: 1755269732792,
|
|
122
|
-
status: 'running'
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
### Foreach progress events
|
|
128
|
-
|
|
129
|
-
When a workflow uses `.foreach()`, each iteration emits a `workflow-step-progress` event. You can use these to track real-time progress:
|
|
130
|
-
|
|
131
|
-
```typescript
|
|
132
|
-
for await (const chunk of stream) {
|
|
133
|
-
if (chunk.type === 'workflow-step-progress') {
|
|
134
|
-
console.log(
|
|
135
|
-
`${chunk.payload.id}: ${chunk.payload.completedCount}/${chunk.payload.totalCount} — ${chunk.payload.iterationStatus}`,
|
|
136
|
-
)
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
Each progress event includes:
|
|
142
|
-
|
|
143
|
-
- **`id`**: The step ID of the foreach step
|
|
144
|
-
- **`completedCount`**: Number of iterations completed so far
|
|
145
|
-
- **`totalCount`**: Total number of iterations
|
|
146
|
-
- **`currentIndex`**: Index of the iteration that completed
|
|
147
|
-
- **`iterationStatus`**: Status of the iteration (`success`, `failed`, or `suspended`)
|
|
148
|
-
- **`iterationOutput`**: Output of the iteration (when successful)
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
# Streaming overview
|
|
2
|
-
|
|
3
|
-
Mastra supports real-time, incremental responses from agents and workflows, allowing users to see output as it’s generated instead of waiting for completion. This is useful for chat, long-form content, multi-step workflows, or any scenario where immediate feedback matters.
|
|
4
|
-
|
|
5
|
-
## Getting started
|
|
6
|
-
|
|
7
|
-
Mastra's streaming API adapts based on your model version:
|
|
8
|
-
|
|
9
|
-
- **`.stream()`**: For V2 models, supports **AI SDK v5** and later (`LanguageModelV2`).
|
|
10
|
-
- **`.streamLegacy()`**: For V1 models, supports **AI SDK v4** (`LanguageModelV1`).
|
|
11
|
-
|
|
12
|
-
## Streaming with agents
|
|
13
|
-
|
|
14
|
-
You can pass a single string for basic prompts, an array of strings when providing multiple pieces of context, or an array of message objects with `role` and `content` for precise control over roles and conversational flows.
|
|
15
|
-
|
|
16
|
-
### Using `Agent.stream()`
|
|
17
|
-
|
|
18
|
-
A `textStream` breaks the response into chunks as it's generated, allowing output to stream progressively instead of arriving all at once. Iterate over the `textStream` using a `for await` loop to inspect each stream chunk.
|
|
19
|
-
|
|
20
|
-
```typescript
|
|
21
|
-
const testAgent = mastra.getAgent('testAgent')
|
|
22
|
-
|
|
23
|
-
const stream = await testAgent.stream([{ role: 'user', content: 'Help me organize my day' }])
|
|
24
|
-
|
|
25
|
-
for await (const chunk of stream.textStream) {
|
|
26
|
-
process.stdout.write(chunk)
|
|
27
|
-
}
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
> **Info:** Visit [Agent.stream()](https://mastra.ai/reference/streaming/agents/stream) for more information.
|
|
31
|
-
|
|
32
|
-
> **Tip:** For agents that dispatch [background tasks](https://mastra.ai/docs/agents/background-tasks), use [`Agent.streamUntilIdle()`](https://mastra.ai/reference/streaming/agents/streamUntilIdle) to keep the stream open until those tasks complete and the agent has had a chance to respond to their results.
|
|
33
|
-
|
|
34
|
-
### Output from `Agent.stream()`
|
|
35
|
-
|
|
36
|
-
The output streams the generated response from the agent.
|
|
37
|
-
|
|
38
|
-
```text
|
|
39
|
-
Of course!
|
|
40
|
-
To help you organize your day effectively, I need a bit more information.
|
|
41
|
-
Here are some questions to consider:
|
|
42
|
-
...
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
### Agent stream properties
|
|
46
|
-
|
|
47
|
-
An agent stream provides access to various response properties:
|
|
48
|
-
|
|
49
|
-
- **`stream.textStream`**: A readable stream that emits text chunks.
|
|
50
|
-
- **`stream.text`**: Promise that resolves to the full text response.
|
|
51
|
-
- **`stream.finishReason`**: The reason the agent stopped streaming.
|
|
52
|
-
- **`stream.usage`**: Token usage information.
|
|
53
|
-
|
|
54
|
-
### AI SDK v5+ Compatibility
|
|
55
|
-
|
|
56
|
-
AI SDK v5 (and later) uses `LanguageModelV2` for the model providers. If you are getting an error that you are using an AI SDK v4 model you will need to upgrade your model package to the next major version.
|
|
57
|
-
|
|
58
|
-
For integration with AI SDK v5+, use the `toAISdkV5Stream()` utility from `@mastra/ai-sdk` to convert Mastra streams to AI SDK-compatible format:
|
|
59
|
-
|
|
60
|
-
```typescript
|
|
61
|
-
import { toAISdkV5Stream } from '@mastra/ai-sdk'
|
|
62
|
-
|
|
63
|
-
const testAgent = mastra.getAgent('testAgent')
|
|
64
|
-
|
|
65
|
-
const stream = await testAgent.stream([{ role: 'user', content: 'Help me organize my day' }])
|
|
66
|
-
|
|
67
|
-
// Convert to AI SDK v5+ compatible stream
|
|
68
|
-
const aiSDKStream = toAISdkV5Stream(stream, { from: 'agent' })
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
For converting messages to AI SDK v5+ format, use the `toAISdkV5Messages()` utility from `@mastra/ai-sdk/ui`:
|
|
72
|
-
|
|
73
|
-
```typescript
|
|
74
|
-
import { toAISdkV5Messages } from '@mastra/ai-sdk/ui'
|
|
75
|
-
|
|
76
|
-
const messages = [{ role: 'user', content: 'Hello' }]
|
|
77
|
-
const aiSDKMessages = toAISdkV5Messages(messages)
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
## Streaming with workflows
|
|
81
|
-
|
|
82
|
-
Streaming from a workflow returns a sequence of structured events describing the run lifecycle, rather than incremental text chunks. This event-based format makes it possible to track and respond to workflow progress in real time once a run is created using `.createRun()`.
|
|
83
|
-
|
|
84
|
-
### Using `Run.stream()`
|
|
85
|
-
|
|
86
|
-
The `stream()` method returns a `ReadableStream` of events directly.
|
|
87
|
-
|
|
88
|
-
```typescript
|
|
89
|
-
const run = await testWorkflow.createRun()
|
|
90
|
-
|
|
91
|
-
const stream = await run.stream({
|
|
92
|
-
inputData: {
|
|
93
|
-
value: 'initial data',
|
|
94
|
-
},
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
for await (const chunk of stream) {
|
|
98
|
-
console.log(chunk)
|
|
99
|
-
}
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
> **Info:** Visit [Run.stream()](https://mastra.ai/reference/streaming/workflows/stream) for more information.
|
|
103
|
-
|
|
104
|
-
### Output from `Run.stream()`
|
|
105
|
-
|
|
106
|
-
The event structure includes `runId` and `from` at the top level, making it easier to identify and track workflow runs without digging into the payload.
|
|
107
|
-
|
|
108
|
-
```typescript
|
|
109
|
-
{
|
|
110
|
-
type: 'workflow-start',
|
|
111
|
-
runId: '1eeaf01a-d2bf-4e3f-8d1b-027795ccd3df',
|
|
112
|
-
from: 'WORKFLOW',
|
|
113
|
-
payload: {
|
|
114
|
-
stepName: 'step-1',
|
|
115
|
-
args: { value: 'initial data' },
|
|
116
|
-
stepCallId: '8e15e618-be0e-4215-a5d6-08e58c152068',
|
|
117
|
-
startedAt: 1755121710066,
|
|
118
|
-
status: 'running'
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
### Workflow stream properties
|
|
124
|
-
|
|
125
|
-
A workflow stream provides access to various response properties:
|
|
126
|
-
|
|
127
|
-
- **`stream.status`**: The status of the workflow run.
|
|
128
|
-
- **`stream.result`**: The result of the workflow run.
|
|
129
|
-
- **`stream.usage`**: The total token usage of the workflow run.
|
|
130
|
-
|
|
131
|
-
## Related
|
|
132
|
-
|
|
133
|
-
- [Streaming events](https://mastra.ai/docs/streaming/events)
|
|
134
|
-
- [Background tasks](https://mastra.ai/docs/agents/background-tasks)
|
|
135
|
-
- [Using Agents](https://mastra.ai/docs/agents/overview)
|
|
136
|
-
- [Workflows overview](https://mastra.ai/docs/workflows/overview)
|
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
# Tool streaming
|
|
2
|
-
|
|
3
|
-
Tool streaming in Mastra enables tools to send incremental results while they run, rather than waiting until execution finishes. This allows you to surface partial progress, intermediate states, or progressive data directly to users or upstream agents and workflows.
|
|
4
|
-
|
|
5
|
-
Streams can be written to in two main ways:
|
|
6
|
-
|
|
7
|
-
- **From within a tool**: every tool receives a `context.writer` object, which is a writable stream you can use to push updates as execution progresses.
|
|
8
|
-
- **From an agent stream**: you can also pipe an agent's `stream` output directly into a tool's writer, making it convenient to chain agent responses into tool results without extra glue code.
|
|
9
|
-
|
|
10
|
-
By combining writable tool streams with agent streaming, you gain fine grained control over how intermediate results flow through your system and into the user experience.
|
|
11
|
-
|
|
12
|
-
## Agent using tool
|
|
13
|
-
|
|
14
|
-
Agent streaming can be combined with tool calls, allowing tool outputs to be written directly into the agent’s streaming response. This makes it possible to surface tool activity as part of the overall interaction.
|
|
15
|
-
|
|
16
|
-
```typescript
|
|
17
|
-
import { Agent } from '@mastra/core/agent'
|
|
18
|
-
import { testTool } from '../tools/test-tool'
|
|
19
|
-
|
|
20
|
-
export const testAgent = new Agent({
|
|
21
|
-
id: 'test-agent',
|
|
22
|
-
name: 'Test Agent',
|
|
23
|
-
instructions: 'You are a weather agent.',
|
|
24
|
-
model: 'openai/gpt-5.5',
|
|
25
|
-
tools: { testTool },
|
|
26
|
-
})
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
### Using `context.writer`
|
|
30
|
-
|
|
31
|
-
The `context.writer` object is available in a tool's `execute()` function and can be used to emit custom events, data, or values into the active stream. This enables tools to provide intermediate results or status updates while execution is still in progress.
|
|
32
|
-
|
|
33
|
-
> **Warning:** You must `await` the call to `writer.write()` or else you will lock the stream and get a `WritableStream is locked` error.
|
|
34
|
-
|
|
35
|
-
```typescript
|
|
36
|
-
import { createTool } from '@mastra/core/tools'
|
|
37
|
-
|
|
38
|
-
export const testTool = createTool({
|
|
39
|
-
execute: async (inputData, context) => {
|
|
40
|
-
const { value } = inputData
|
|
41
|
-
|
|
42
|
-
await context?.writer?.write({
|
|
43
|
-
type: 'custom-event',
|
|
44
|
-
status: 'pending',
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
const response = await fetch()
|
|
48
|
-
|
|
49
|
-
await context?.writer?.write({
|
|
50
|
-
type: 'custom-event',
|
|
51
|
-
status: 'success',
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
return {
|
|
55
|
-
value: '',
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
})
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
You can also use `writer.custom()` to emit top-level stream chunks. This is useful when integrating with UI frameworks.
|
|
62
|
-
|
|
63
|
-
```typescript
|
|
64
|
-
import { createTool } from '@mastra/core/tools'
|
|
65
|
-
|
|
66
|
-
export const testTool = createTool({
|
|
67
|
-
execute: async (inputData, context) => {
|
|
68
|
-
const { value } = inputData
|
|
69
|
-
|
|
70
|
-
await context?.writer?.custom({
|
|
71
|
-
type: 'data-tool-progress',
|
|
72
|
-
status: 'pending',
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
const response = await fetch()
|
|
76
|
-
|
|
77
|
-
await context?.writer?.custom({
|
|
78
|
-
type: 'data-tool-progress',
|
|
79
|
-
status: 'success',
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
return {
|
|
83
|
-
value: '',
|
|
84
|
-
}
|
|
85
|
-
},
|
|
86
|
-
})
|
|
87
|
-
```
|
|
88
|
-
|
|
89
|
-
### Transient data chunks
|
|
90
|
-
|
|
91
|
-
By default, `data-*` chunks emitted with `writer.custom()` are persisted to storage as part of the message history. For chunks that are only needed during live streaming — such as progress updates or verbose log output — set `transient: true` to skip storage persistence. Transient chunks are still streamed to the client in real time but aren't saved to the database.
|
|
92
|
-
|
|
93
|
-
```typescript
|
|
94
|
-
await context?.writer?.custom({
|
|
95
|
-
type: 'data-build-log',
|
|
96
|
-
data: { line: 'Compiling module 3 of 12...' },
|
|
97
|
-
transient: true,
|
|
98
|
-
})
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
Use transient chunks when the data is large or high-frequency and only relevant during the live session. After a page refresh, transient chunks are no longer available — only the tool's return value and any non-transient chunks are loaded from storage.
|
|
102
|
-
|
|
103
|
-
### Inspecting stream payloads
|
|
104
|
-
|
|
105
|
-
Events written to the stream are included in the emitted chunks. These chunks can be inspected to access any custom fields, such as event types, intermediate values, or tool-specific data.
|
|
106
|
-
|
|
107
|
-
```typescript
|
|
108
|
-
const stream = await testAgent.stream(['What is the weather in London?', 'Use the testTool'])
|
|
109
|
-
|
|
110
|
-
for await (const chunk of stream) {
|
|
111
|
-
if (chunk.payload.output?.type === 'custom-event') {
|
|
112
|
-
console.log(JSON.stringify(chunk, null, 2))
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
## Tool lifecycle hooks
|
|
118
|
-
|
|
119
|
-
Tools support lifecycle hooks that allow you to monitor different stages of tool execution during streaming. These hooks are particularly useful for logging or analytics.
|
|
120
|
-
|
|
121
|
-
### Example: Using `onInputAvailable` and `onOutput`
|
|
122
|
-
|
|
123
|
-
```typescript
|
|
124
|
-
import { createTool } from '@mastra/core/tools'
|
|
125
|
-
import { z } from 'zod'
|
|
126
|
-
|
|
127
|
-
export const weatherTool = createTool({
|
|
128
|
-
id: 'weather-tool',
|
|
129
|
-
description: 'Get weather information',
|
|
130
|
-
inputSchema: z.object({
|
|
131
|
-
city: z.string(),
|
|
132
|
-
}),
|
|
133
|
-
outputSchema: z.object({
|
|
134
|
-
temperature: z.number(),
|
|
135
|
-
conditions: z.string(),
|
|
136
|
-
}),
|
|
137
|
-
// Called when the complete input is available
|
|
138
|
-
onInputAvailable: ({ input, toolCallId }) => {
|
|
139
|
-
console.log(`Weather requested for: ${input.city}`)
|
|
140
|
-
},
|
|
141
|
-
execute: async input => {
|
|
142
|
-
const weather = await fetchWeather(input.city)
|
|
143
|
-
return weather
|
|
144
|
-
},
|
|
145
|
-
// Called after successful execution
|
|
146
|
-
onOutput: ({ output, toolName }) => {
|
|
147
|
-
console.log(`${toolName} result: ${output.temperature}°F, ${output.conditions}`)
|
|
148
|
-
},
|
|
149
|
-
})
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
### Available Hooks
|
|
153
|
-
|
|
154
|
-
- **onInputStart**: Called when tool call input streaming begins
|
|
155
|
-
- **onInputDelta**: Called for each chunk of input as it streams in
|
|
156
|
-
- **onInputAvailable**: Called when complete input is parsed and validated
|
|
157
|
-
- **onOutput**: Called after the tool successfully executes with the output
|
|
158
|
-
|
|
159
|
-
For detailed documentation on all lifecycle hooks, see the [createTool() reference](https://mastra.ai/reference/tools/create-tool).
|
|
160
|
-
|
|
161
|
-
### Streaming tool input in UIs
|
|
162
|
-
|
|
163
|
-
When a model generates a tool call, the arguments arrive incrementally as `tool-call-delta` stream chunks before the final `tool-call` chunk. UIs can listen for the corresponding `tool_input_start`, `tool_input_delta`, and `tool_input_end` events to render tool arguments as they stream in — for example, showing a file path or command immediately rather than waiting for the complete tool call.
|
|
164
|
-
|
|
165
|
-
Using a partial JSON parser on the accumulated `argsTextDelta` fragments lets you extract usable argument values before the JSON is complete. This enables features like live diff previews for edit tools, streaming file content for write tools, and instant display of search patterns or file paths.
|
|
166
|
-
|
|
167
|
-
## Tool using an agent
|
|
168
|
-
|
|
169
|
-
Pipe an agent's `fullStream` to the tool's `writer`. This streams partial output, and Mastra automatically aggregates the agent's usage into the tool run.
|
|
170
|
-
|
|
171
|
-
```typescript
|
|
172
|
-
import { createTool } from '@mastra/core/tools'
|
|
173
|
-
import { z } from 'zod'
|
|
174
|
-
|
|
175
|
-
export const testTool = createTool({
|
|
176
|
-
execute: async (inputData, context) => {
|
|
177
|
-
const { city } = inputData
|
|
178
|
-
|
|
179
|
-
const agent = context?.mastra?.getAgent('testAgent')
|
|
180
|
-
const stream = await agent?.stream(`What is the weather in ${city}?`)
|
|
181
|
-
|
|
182
|
-
await stream!.fullStream.pipeTo(context?.writer!)
|
|
183
|
-
|
|
184
|
-
return {
|
|
185
|
-
value: await stream!.text,
|
|
186
|
-
}
|
|
187
|
-
},
|
|
188
|
-
})
|
|
189
|
-
```
|