@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.
Files changed (30) hide show
  1. package/.docs/docs/agents/background-tasks.md +89 -14
  2. package/.docs/docs/agents/durable-agents.md +231 -0
  3. package/.docs/docs/agents/using-tools.md +52 -0
  4. package/.docs/docs/getting-started/build-with-ai.md +273 -8
  5. package/.docs/docs/server/pubsub.md +2 -2
  6. package/.docs/docs/workflows/overview.md +71 -0
  7. package/.docs/guides/build-your-ui/ai-sdk-ui.md +3 -3
  8. package/.docs/guides/concepts/streaming.md +317 -0
  9. package/.docs/guides/getting-started/quickstart.md +1 -1
  10. package/.docs/models/index.md +1 -1
  11. package/.docs/models/providers/opencode.md +2 -1
  12. package/.docs/reference/agents/durable-agent.md +30 -1
  13. package/.docs/reference/agents/getDefaultOptions.md +1 -1
  14. package/.docs/reference/agents/getDefaultStreamOptions.md +1 -1
  15. package/.docs/reference/agents/inngest-agent.md +339 -0
  16. package/.docs/reference/ai-sdk/handle-workflow-stream.md +1 -1
  17. package/.docs/reference/ai-sdk/workflow-route.md +1 -1
  18. package/.docs/reference/index.md +1 -0
  19. package/.docs/reference/streaming/workflows/timeTravelStream.md +1 -1
  20. package/.docs/reference/tools/create-tool.md +1 -1
  21. package/.docs/reference/workflows/run.md +1 -1
  22. package/CHANGELOG.md +7 -0
  23. package/package.json +5 -5
  24. package/.docs/docs/build-with-ai/mcp-docs-server.md +0 -238
  25. package/.docs/docs/build-with-ai/skills.md +0 -63
  26. package/.docs/docs/streaming/background-task-streaming.md +0 -80
  27. package/.docs/docs/streaming/events.md +0 -148
  28. package/.docs/docs/streaming/overview.md +0 -136
  29. package/.docs/docs/streaming/tool-streaming.md +0 -189
  30. package/.docs/docs/streaming/workflow-streaming.md +0 -109
@@ -0,0 +1,317 @@
1
+ # Streaming
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
+ 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.
132
+
133
+ 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.
134
+
135
+ ## Event types
136
+
137
+ 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:
138
+
139
+ - **start**: Marks the beginning of an agent or workflow run.
140
+ - **step-start**: Indicates a workflow step has begun execution.
141
+ - **text-delta**: Incremental text chunks as they're generated by the LLM.
142
+ - **tool-call**: When the agent decides to use a tool, including the tool name and arguments.
143
+ - **tool-result**: The result returned from tool execution.
144
+ - **step-finish**: Confirms that a specific step has fully finalized, and may include metadata like the finish reason for that step.
145
+ - **finish**: When the agent or workflow completes, including usage statistics.
146
+
147
+ ## Inspecting agent streams
148
+
149
+ Iterate over the `stream` with a `for await` loop to inspect all emitted event chunks.
150
+
151
+ ```typescript
152
+ const testAgent = mastra.getAgent('testAgent')
153
+
154
+ const stream = await testAgent.stream([{ role: 'user', content: 'Help me organize my day' }])
155
+
156
+ for await (const chunk of stream) {
157
+ console.log(chunk)
158
+ }
159
+ ```
160
+
161
+ > **Info:** Visit [Agent.stream()](https://mastra.ai/reference/streaming/agents/stream) for more information.
162
+
163
+ ### Example agent output
164
+
165
+ 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`.
166
+
167
+ ```typescript
168
+ {
169
+ type: 'start',
170
+ from: 'AGENT',
171
+ // ..
172
+ }
173
+ {
174
+ type: 'step-start',
175
+ from: 'AGENT',
176
+ payload: {
177
+ messageId: 'msg-cdUrkirvXw8A6oE4t5lzDuxi',
178
+ // ...
179
+ }
180
+ }
181
+ {
182
+ type: 'tool-call',
183
+ from: 'AGENT',
184
+ payload: {
185
+ toolCallId: 'call_jbhi3s1qvR6Aqt9axCfTBMsA',
186
+ toolName: 'testTool'
187
+ // ..
188
+ }
189
+ }
190
+ ```
191
+
192
+ ## Writer API
193
+
194
+ The `writer` API is shared by tools and workflow steps — see the Tools and Workflows docs for feature-specific examples.
195
+
196
+ ## Agent using tool
197
+
198
+ 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.
199
+
200
+ ```typescript
201
+ import { Agent } from '@mastra/core/agent'
202
+ import { testTool } from '../tools/test-tool'
203
+
204
+ export const testAgent = new Agent({
205
+ id: 'test-agent',
206
+ name: 'Test Agent',
207
+ instructions: 'You are a weather agent.',
208
+ model: 'openai/gpt-5.5',
209
+ tools: { testTool },
210
+ })
211
+ ```
212
+
213
+ ### Using `context.writer`
214
+
215
+ 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.
216
+
217
+ > **Warning:** You must `await` the call to `writer.write()` or else you will lock the stream and get a `WritableStream is locked` error.
218
+
219
+ ```typescript
220
+ import { createTool } from '@mastra/core/tools'
221
+
222
+ export const testTool = createTool({
223
+ execute: async (inputData, context) => {
224
+ const { value } = inputData
225
+
226
+ await context?.writer?.write({
227
+ type: 'custom-event',
228
+ status: 'pending',
229
+ })
230
+
231
+ const response = await fetch()
232
+
233
+ await context?.writer?.write({
234
+ type: 'custom-event',
235
+ status: 'success',
236
+ })
237
+
238
+ return {
239
+ value: '',
240
+ }
241
+ },
242
+ })
243
+ ```
244
+
245
+ You can also use `writer.custom()` to emit top-level stream chunks. This is useful when integrating with UI frameworks.
246
+
247
+ ```typescript
248
+ import { createTool } from '@mastra/core/tools'
249
+
250
+ export const testTool = createTool({
251
+ execute: async (inputData, context) => {
252
+ const { value } = inputData
253
+
254
+ await context?.writer?.custom({
255
+ type: 'data-tool-progress',
256
+ status: 'pending',
257
+ })
258
+
259
+ const response = await fetch()
260
+
261
+ await context?.writer?.custom({
262
+ type: 'data-tool-progress',
263
+ status: 'success',
264
+ })
265
+
266
+ return {
267
+ value: '',
268
+ }
269
+ },
270
+ })
271
+ ```
272
+
273
+ ### Transient data chunks
274
+
275
+ 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.
276
+
277
+ ```typescript
278
+ await context?.writer?.custom({
279
+ type: 'data-build-log',
280
+ data: { line: 'Compiling module 3 of 12...' },
281
+ transient: true,
282
+ })
283
+ ```
284
+
285
+ 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.
286
+
287
+ ## Using the `writer` argument
288
+
289
+ The `writer` argument is passed to a workflow step's `execute` function and can be used to emit custom events, data, or values into the active stream. This enables workflow steps to provide intermediate results or status updates while execution is still in progress.
290
+
291
+ > **Warning:** You must `await` the call to `writer.write(...)` or else you will lock the stream and get a `WritableStream is locked` error.
292
+
293
+ ```typescript
294
+ import { createStep } from "@mastra/core/workflows";
295
+
296
+ export const testStep = createStep({
297
+ execute: async ({ inputData, writer }) => {
298
+ const { value } = inputData;
299
+
300
+ await writer?.write({
301
+ type: "custom-event",
302
+ status: "pending"
303
+ });
304
+
305
+ const response = await fetch(...);
306
+
307
+ await writer?.write({
308
+ type: "custom-event",
309
+ status: "success"
310
+ });
311
+
312
+ return {
313
+ value: ""
314
+ };
315
+ },
316
+ });
317
+ ```
@@ -46,7 +46,7 @@ The wizard creates a new directory for your project with a `src/mastra` folder c
46
46
  - `workflows/weather-workflow.ts`: A workflow that runs the weather agent
47
47
  - `scorers/weather-scorer.ts`: A scorer to evaluate the weather agent's responses
48
48
 
49
- Depending on your choices, you'll also end up with [Mastra Skills](https://mastra.ai/docs/build-with-ai/skills) or the [MCP Docs Server](https://mastra.ai/docs/build-with-ai/mcp-docs-server) installed.
49
+ Depending on your choices, you'll also end up with [Mastra Skills](https://mastra.ai/docs/getting-started/build-with-ai) or the [MCP Docs Server](https://mastra.ai/docs/getting-started/build-with-ai) installed.
50
50
 
51
51
  > **Tip:** You can use [flags](https://mastra.ai/reference/cli/create-mastra) with `create mastra` like `--no-example` to skip the example weather agent or `--template` to start from a specific [template](https://mastra.ai/templates).
52
52
 
@@ -1,6 +1,6 @@
1
1
  # Model Providers
2
2
 
3
- Mastra provides a unified interface for working with LLMs across multiple providers, giving you access to 4562 models from 133 providers through a single API.
3
+ Mastra provides a unified interface for working with LLMs across multiple providers, giving you access to 4563 models from 133 providers through a single API.
4
4
 
5
5
  ## Features
6
6
 
@@ -1,6 +1,6 @@
1
1
  # ![OpenCode Zen logo](https://models.dev/logos/opencode.svg)OpenCode Zen
2
2
 
3
- Access 45 OpenCode Zen models through Mastra's model router. Authentication is handled automatically using the `OPENCODE_API_KEY` environment variable.
3
+ Access 46 OpenCode Zen models through Mastra's model router. Authentication is handled automatically using the `OPENCODE_API_KEY` environment variable.
4
4
 
5
5
  Learn more in the [OpenCode Zen documentation](https://opencode.ai/docs/zen).
6
6
 
@@ -52,6 +52,7 @@ for await (const chunk of stream) {
52
52
  | `opencode/gemini-3.5-flash` | 1.0M | | | | | | $2 | $9 |
53
53
  | `opencode/glm-5` | 205K | | | | | | $1 | $3 |
54
54
  | `opencode/glm-5.1` | 205K | | | | | | $1 | $4 |
55
+ | `opencode/glm-5.2` | 1.0M | | | | | | $1 | $4 |
55
56
  | `opencode/gpt-5` | 400K | | | | | | $1 | $9 |
56
57
  | `opencode/gpt-5-codex` | 400K | | | | | | $1 | $9 |
57
58
  | `opencode/gpt-5-nano` | 400K | | | | | | $0.05 | $0.40 |
@@ -2,7 +2,7 @@
2
2
 
3
3
  `DurableAgent` wraps an existing [`Agent`](https://mastra.ai/reference/agents/agent) with durable execution and resumable streams. It runs the agentic loop so that a client can disconnect and reconnect without missing events, and it streams those events over [PubSub](https://mastra.ai/docs/server/pubsub). Use it when a run must outlive a single request or survive a dropped connection.
4
4
 
5
- Create one with the [`createDurableAgent`](#createdurableagentoptions) factory, or use [`createEventedAgent`](#createeventedagentoptions) for fire-and-forget execution on the built-in workflow engine. For Inngest-powered execution, use `createInngestAgent` from `@mastra/inngest`.
5
+ Create one with the [`createDurableAgent`](#createdurableagentoptions) factory, or use [`createEventedAgent`](#createeventedagentoptions) for fire-and-forget execution on the built-in workflow engine. For Inngest-powered execution, use [`createInngestAgent`](https://mastra.ai/reference/agents/inngest-agent) from `@mastra/inngest`.
6
6
 
7
7
  ## Usage example
8
8
 
@@ -153,6 +153,32 @@ Returns: `Promise<DurableAgentStreamResult>`
153
153
 
154
154
  > **Warning:** The `cleanup()` returned by `observe()` destroys the run's registry entries and cached events. Only call it when you are done with the run. If the run is suspended and you intend to resume later, don't call `cleanup()` — let the auto-cleanup timer handle it after the run finishes or errors. Auto-cleanup doesn't fire on suspended events.
155
155
 
156
+ #### `prepare(messages, options?)`
157
+
158
+ Prepares a run for durable execution without starting it. Registers the run in the internal registry and returns the serialized workflow input. Use this when you need to control when and how the workflow is triggered.
159
+
160
+ ```typescript
161
+ const { runId, messageId, workflowInput, threadId, resourceId } = await durableAgent.prepare(
162
+ 'Summarize the document',
163
+ {
164
+ memory: { threadId: 'thread-1', resourceId: 'user-1' },
165
+ },
166
+ )
167
+ ```
168
+
169
+ Returns:
170
+
171
+ ```typescript
172
+ interface PrepareResult {
173
+ runId: string
174
+ messageId: string
175
+ workflowInput: any
176
+ registryEntry: object
177
+ threadId?: string
178
+ resourceId?: string
179
+ }
180
+ ```
181
+
156
182
  ## Stream options
157
183
 
158
184
  `stream()` accepts a `DurableAgentStreamOptions` object. It supports the agent execution options below, plus lifecycle callbacks.
@@ -191,6 +217,8 @@ Returns: `Promise<DurableAgentStreamResult>`
191
217
 
192
218
  **structuredOutput** (`object`): Structured output configuration.
193
219
 
220
+ **untilIdle** (`boolean | { maxIdleMs?: number }`): When set, keeps the stream open across background-task continuations until the agent is idle. Pass \`true\` for the default 5-minute idle timeout, or \`{ maxIdleMs }\` to customise. Equivalent to the deprecated \`streamUntilIdle()\` method.
221
+
194
222
  **versions** (`object`): Version overrides for sub-agent delegation.
195
223
 
196
224
  **onChunk** (`(chunk: ChunkType) => void | Promise<void>`): Called for each streamed chunk.
@@ -234,6 +262,7 @@ interface DurableAgentStreamResult<OUTPUT = undefined> {
234
262
 
235
263
  ## Related
236
264
 
265
+ - [`createInngestAgent()`](https://mastra.ai/reference/agents/inngest-agent)
237
266
  - [Agent class](https://mastra.ai/reference/agents/agent)
238
267
  - [PubSub](https://mastra.ai/docs/server/pubsub)
239
268
  - [`.getMemory()`](https://mastra.ai/reference/agents/getMemory)
@@ -28,5 +28,5 @@ await agent.getDefaultOptions({
28
28
 
29
29
  ## Related
30
30
 
31
- - [Streaming with agents](https://mastra.ai/docs/streaming/overview)
31
+ - [Streaming with agents](https://mastra.ai/guides/concepts/streaming)
32
32
  - [Request Context](https://mastra.ai/docs/server/request-context)
@@ -30,5 +30,5 @@ await agent.getDefaultStreamOptionsLegacy({
30
30
 
31
31
  ## Related
32
32
 
33
- - [Streaming with agents](https://mastra.ai/docs/streaming/overview)
33
+ - [Streaming with agents](https://mastra.ai/guides/concepts/streaming)
34
34
  - [Request Context](https://mastra.ai/docs/server/request-context)