@mastra/mcp-docs-server 1.1.29-alpha.8 → 1.1.29

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.
@@ -1,6 +1,6 @@
1
1
  # ![Fireworks AI logo](https://models.dev/logos/fireworks-ai.svg)Fireworks AI
2
2
 
3
- Access 18 Fireworks AI models through Mastra's model router. Authentication is handled automatically using the `FIREWORKS_API_KEY` environment variable.
3
+ Access 19 Fireworks AI models through Mastra's model router. Authentication is handled automatically using the `FIREWORKS_API_KEY` environment variable.
4
4
 
5
5
  Learn more in the [Fireworks AI documentation](https://fireworks.ai/docs/).
6
6
 
@@ -36,6 +36,7 @@ for await (const chunk of stream) {
36
36
  | --------------------------------------------------------- | ------- | ----- | --------- | ----- | ----- | ----- | ---------- | ----------- |
37
37
  | `fireworks-ai/accounts/fireworks/models/deepseek-v3p1` | 164K | | | | | | $0.56 | $2 |
38
38
  | `fireworks-ai/accounts/fireworks/models/deepseek-v3p2` | 160K | | | | | | $0.56 | $2 |
39
+ | `fireworks-ai/accounts/fireworks/models/deepseek-v4-pro` | 1.0M | | | | | | $2 | $3 |
39
40
  | `fireworks-ai/accounts/fireworks/models/glm-4p5` | 131K | | | | | | $0.55 | $2 |
40
41
  | `fireworks-ai/accounts/fireworks/models/glm-4p5-air` | 131K | | | | | | $0.22 | $0.88 |
41
42
  | `fireworks-ai/accounts/fireworks/models/glm-4p7` | 198K | | | | | | $0.60 | $2 |
@@ -1,6 +1,6 @@
1
1
  # ![Kilo Gateway logo](https://models.dev/logos/kilo.svg)Kilo Gateway
2
2
 
3
- Access 335 Kilo Gateway models through Mastra's model router. Authentication is handled automatically using the `KILO_API_KEY` environment variable.
3
+ Access 337 Kilo Gateway models through Mastra's model router. Authentication is handled automatically using the `KILO_API_KEY` environment variable.
4
4
 
5
5
  Learn more in the [Kilo Gateway documentation](https://kilo.ai).
6
6
 
@@ -357,6 +357,8 @@ for await (const chunk of stream) {
357
357
  | `kilo/xiaomi/mimo-v2-flash` | 262K | | | | | | $0.09 | $0.29 |
358
358
  | `kilo/xiaomi/mimo-v2-omni` | 262K | | | | | | $0.40 | $2 |
359
359
  | `kilo/xiaomi/mimo-v2-pro` | 1.0M | | | | | | $1 | $3 |
360
+ | `kilo/xiaomi/mimo-v2.5` | 1.0M | | | | | | $0.40 | $2 |
361
+ | `kilo/xiaomi/mimo-v2.5-pro` | 1.0M | | | | | | $1 | $3 |
360
362
  | `kilo/z-ai/glm-4-32b` | 128K | | | | | | $0.10 | $0.10 |
361
363
  | `kilo/z-ai/glm-4.5` | 131K | | | | | | $0.60 | $2 |
362
364
  | `kilo/z-ai/glm-4.5-air` | 131K | | | | | | $0.13 | $0.85 |
@@ -151,6 +151,30 @@ for await (const part of uiMessageStream) {
151
151
  }
152
152
  ```
153
153
 
154
+ ### `streamUntilIdle()`
155
+
156
+ Stream a response and keep the stream open until every [background task](https://mastra.ai/docs/agents/background-tasks) dispatched during the run completes. The server re-enters the agentic loop on each task completion so the LLM can react to results in the same call. Requires background tasks to be [enabled on the Mastra instance](https://mastra.ai/reference/configuration) and a memory thread; otherwise the call falls through to a plain `stream()`.
157
+
158
+ ```typescript
159
+ const response = await agent.streamUntilIdle('Research solana for me', {
160
+ memory: {
161
+ thread: 'thread-1',
162
+ resource: 'resource-1',
163
+ },
164
+ maxIdleMs: 5 * 60_000,
165
+ })
166
+
167
+ response.processDataStream({
168
+ onChunk: async chunk => {
169
+ if (chunk.type === 'background-task-completed') {
170
+ console.log('task complete:', chunk.payload.taskId)
171
+ }
172
+ },
173
+ })
174
+ ```
175
+
176
+ The stream emits the same chunk types as `stream()`, plus `background-task-*` chunks for task lifecycle events. Visit [`Agent.streamUntilIdle()`](https://mastra.ai/reference/streaming/agents/streamUntilIdle) for the full server-side API and [background task chunks](https://mastra.ai/reference/streaming/ChunkType) for the payload shapes.
177
+
154
178
  ### `getTool()`
155
179
 
156
180
  Retrieve information about a specific tool available to the agent:
@@ -36,6 +36,69 @@ export const mastra = new Mastra({
36
36
  })
37
37
  ```
38
38
 
39
+ ### backgroundTasks
40
+
41
+ **Type:** `BackgroundTaskManagerConfig`
42
+
43
+ Enables and configures the background task manager. When enabled, agents can dispatch long-running tool calls (including subagent invocations) to run asynchronously while the agentic loop continues. Tasks are persisted, so a configured `storage` backend is required.
44
+
45
+ Visit the [Background tasks documentation](https://mastra.ai/docs/agents/background-tasks) to learn more.
46
+
47
+ ```typescript
48
+ import { Mastra } from '@mastra/core'
49
+ import { LibSQLStore } from '@mastra/libsql'
50
+
51
+ export const mastra = new Mastra({
52
+ storage: new LibSQLStore({
53
+ id: 'mastra-storage',
54
+ url: 'file:./mastra.db',
55
+ }),
56
+ backgroundTasks: {
57
+ enabled: true,
58
+ globalConcurrency: 10,
59
+ perAgentConcurrency: 5,
60
+ backpressure: 'queue',
61
+ defaultTimeoutMs: 300_000,
62
+ },
63
+ })
64
+ ```
65
+
66
+ **enabled** (`boolean`): Whether background tasks are enabled. The manager only initializes when this is true and a storage backend is configured. (Default: `false`)
67
+
68
+ **globalConcurrency** (`number`): Maximum number of background tasks running concurrently across all agents. (Default: `10`)
69
+
70
+ **perAgentConcurrency** (`number`): Maximum number of background tasks running concurrently for a single agent. (Default: `5`)
71
+
72
+ **backpressure** (`'queue' | 'reject' | 'fallback-sync'`): Behavior when a concurrency limit is reached. 'queue' waits for a slot, 'reject' throws on enqueue, 'fallback-sync' runs the tool synchronously in the agentic loop instead. (Default: `'queue'`)
73
+
74
+ **defaultTimeoutMs** (`number`): Default per-task timeout in milliseconds. Can be overridden per-tool or per-call. (Default: `300000`)
75
+
76
+ **defaultRetries** (`RetryConfig`): Default retry policy applied to tasks that fail.
77
+
78
+ **defaultRetries.maxRetries** (`number`): Maximum retry attempts before the task is marked failed.
79
+
80
+ **defaultRetries.retryDelayMs** (`number`): Delay between retries in milliseconds.
81
+
82
+ **defaultRetries.backoffMultiplier** (`number`): Multiplier applied to retryDelayMs on each subsequent attempt.
83
+
84
+ **defaultRetries.maxRetryDelayMs** (`number`): Upper bound on the retry delay regardless of backoff.
85
+
86
+ **defaultRetries.retryableErrors** (`(error: Error) => boolean`): Predicate that decides whether a given error should be retried. Default: retry all errors.
87
+
88
+ **cleanup** (`CleanupConfig`): Controls how long task records are kept and how often the cleanup process runs.
89
+
90
+ **cleanup.completedTtlMs** (`number`): How long to keep completed task records, in milliseconds. Default: 1 hour.
91
+
92
+ **cleanup.failedTtlMs** (`number`): How long to keep failed task records, in milliseconds. Default: 24 hours.
93
+
94
+ **cleanup.cleanupIntervalMs** (`number`): How often the cleanup process runs, in milliseconds. Default: 1 minute.
95
+
96
+ **waitTimeoutMs** (`number`): How long the agentic loop waits for a background task to complete before moving on. If a task has not finished within this time, the loop proceeds without setting isContinued. Default: undefined (do not wait). Can be overridden per-agent or per-tool.
97
+
98
+ **onTaskComplete** (`(task: BackgroundTask) => void | Promise<void>`): Global callback invoked when any background task completes successfully. Fires in addition to per-tool and per-agent callbacks.
99
+
100
+ **onTaskFailed** (`(task: BackgroundTask) => void | Promise<void>`): Global callback invoked when any background task fails. Fires in addition to per-tool and per-agent callbacks.
101
+
39
102
  ### deployer
40
103
 
41
104
  **Type:** `MastraDeployer`
@@ -90,6 +90,8 @@ await harness.sendMessage({ content: 'Hello!' })
90
90
 
91
91
  **subagents.stopWhen** (`LoopOptions['stopWhen']`): Optional stop condition for the spawned subagent.
92
92
 
93
+ **subagents.forked** (`boolean`): When \`true\`, calls to this subagent default to forked mode: the subagent runs on a clone of the parent thread, reusing the parent agent’s instructions, tools, and model so the prompt-cache prefix stays intact. Requires \`memory\` to be configured. The subagent definition’s own \`instructions\`, \`tools\`, \`allowedHarnessTools\`, \`allowedWorkspaceTools\`, \`defaultModelId\`, \`maxSteps\`, and \`stopWhen\` are ignored in forked mode. Callers can still override per-invocation via \`forked: false\` in the \`subagent\` tool input. See the \[Forked subagents]\(#forked-subagents) section below for full semantics.
94
+
93
95
  **resolveModel** (`(modelId: string) => MastraLanguageModel`): Converts a model ID string (e.g., \`"anthropic/claude-sonnet-4"\`) to a language model instance. Used by subagents and observational memory model resolution.
94
96
 
95
97
  **omConfig** (`HarnessOMConfig`): Default configuration for observational memory (observer/reflector model IDs and thresholds).
@@ -286,16 +288,21 @@ await harness.switchThread({ threadId: 'thread-abc123' })
286
288
 
287
289
  #### `listThreads(options?)`
288
290
 
289
- List threads from storage. By default, only threads for the current resource are returned.
291
+ List threads from storage. By default, only threads for the current resource are returned, and transient [forked subagent](#forked-subagents) threads are hidden so they don’t appear in user-facing thread pickers / startup flows.
290
292
 
291
293
  ```typescript
292
- // List threads for current resource
294
+ // List threads for current resource (forks hidden)
293
295
  const threads = await harness.listThreads()
294
296
 
295
- // List all threads across resources
297
+ // List all threads across resources (forks still hidden)
296
298
  const allThreads = await harness.listThreads({ allResources: true })
299
+
300
+ // Include forked subagent fork threads (debug / admin tooling only)
301
+ const everything = await harness.listThreads({ includeForkedSubagents: true })
297
302
  ```
298
303
 
304
+ Fork threads are tagged with `metadata.forkedSubagent === true` (and `metadata.parentThreadId`) by the harness. Set `includeForkedSubagents: true` to opt back into seeing them — e.g. for a debug panel.
305
+
299
306
  #### `renameThread({ title })`
300
307
 
301
308
  Update the title of the current thread.
@@ -677,6 +684,42 @@ await harness.setSubagentModelId({ modelId: 'anthropic/claude-sonnet-4-6' })
677
684
  await harness.setSubagentModelId({ modelId: 'anthropic/claude-haiku-3.5', agentType: 'explore' })
678
685
  ```
679
686
 
687
+ ### Forked subagents
688
+
689
+ By default, a subagent runs with a fresh context — it doesn't see the parent conversation. **Forked subagents** opt into a different model: the subagent runs on a clone of the parent thread and reuses the parent agent's full configuration. This is useful when the subagent needs the full context of the conversation so far (e.g., recalling earlier user-supplied facts), and when prompt-cache hit rates matter.
690
+
691
+ #### Enabling forked mode
692
+
693
+ Set `forked: true` either on the [`HarnessSubagent` definition](#configuration) (per-type default) or on each `subagent` tool call (per-invocation override):
694
+
695
+ ```typescript
696
+ // Per-type default — every call to this subagent forks unless overridden.
697
+ const subagents: HarnessSubagent[] = [
698
+ {
699
+ id: 'collaborator',
700
+ name: 'Collaborator',
701
+ description: 'Continues the conversation in a fork to try a different angle.',
702
+ instructions: '...',
703
+ forked: true,
704
+ },
705
+ ]
706
+ ```
707
+
708
+ The model can also pass `forked: true` (or `forked: false`) per-invocation in the `subagent` tool input; the per-invocation value wins.
709
+
710
+ #### Semantics and constraints
711
+
712
+ - **Memory required.** Forked mode calls `memory.cloneThread` to create the fork, so the harness must have `memory` configured and an active parent thread. Calls without those return a structured error rather than throwing.
713
+ - **Parent agent reused.** The fork runs through the parent agent's `stream(...)` call. The parent's instructions, tools, model, `maxSteps`, and `stopWhen` apply. The subagent definition's `instructions`, `tools`, `allowedHarnessTools`, `allowedWorkspaceTools`, `defaultModelId`, `maxSteps`, and `stopWhen` are ignored in forked mode — this is what preserves the prompt-cache prefix.
714
+ - **Toolsets inherited, recursive forks blocked at runtime.** Forks inherit the parent's toolsets verbatim (`ask_user`, `submit_plan`, user-configured harness tools, _including the `subagent` tool itself_) so the LLM request prefix — system prompt + tool list + tool schemas + tool descriptions — stays byte-identical to the parent's. This is what preserves the prompt cache. The `subagent` entry is kept on the model side but its `execute` is replaced inside the fork with a stub that returns a non-error "tool unavailable inside a forked subagent" message: nested forks are blocked at the runtime layer without perturbing the cached prefix.
715
+ - **Fork threads are tagged.** Each fork thread is created with `metadata.forkedSubagent === true` and `metadata.parentThreadId === <parent>`. By default, [`listThreads`](#listthreadsoptions) hides these so they don't show up in user-facing thread pickers / startup flows. Pass `includeForkedSubagents: true` to see them in admin / debug tooling.
716
+ - **Save-queue flushed before clone.** The agent stream batches message saves through a debounced `SaveQueueManager`, so the parent's latest user / assistant turn may not be on disk yet when the subagent tool call fires. The fork tool flushes pending saves first via the `flushMessages` callback on `AgentToolExecutionContext` before cloning, so the fork actually carries the latest turn. Flush failures are non-fatal — the clone still runs.
717
+ - **Parent thread untouched.** All subagent activity (messages, OM writes) lands on the fork. The parent thread is never appended to during a forked subagent run.
718
+
719
+ #### When to prefer non-forked mode
720
+
721
+ Forked mode trades isolation for context inheritance. If the subagent should run with a strictly smaller toolset, a different system prompt, or a cheaper model, use the default (non-forked) mode and pass any required context explicitly in the `task` description.
722
+
680
723
  ### Events
681
724
 
682
725
  #### `subscribe(listener)`
@@ -753,13 +796,13 @@ The harness emits events through registered listeners. The following table lists
753
796
 
754
797
  The harness provides built-in tools to agents in every mode:
755
798
 
756
- | Tool | Description |
757
- | ------------- | ------------------------------------------------------------------------------------------------------------------------- |
758
- | `ask_user` | Ask the user a question and wait for their response. Supports free text, single-select choices, and multi-select choices. |
759
- | `submit_plan` | Submit a plan for user review and approval. |
760
- | `task_write` | Create or update a structured task list for tracking progress. |
761
- | `task_check` | Check the completion status of the current task list. |
762
- | `subagent` | Spawn a focused subagent with constrained tools (only available when `subagents` is configured). |
799
+ | Tool | Description |
800
+ | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
801
+ | `ask_user` | Ask the user a question and wait for their response. Supports free text, single-select choices, and multi-select choices. |
802
+ | `submit_plan` | Submit a plan for user review and approval. |
803
+ | `task_write` | Create or update a structured task list for tracking progress. |
804
+ | `task_check` | Check the completion status of the current task list. |
805
+ | `subagent` | Spawn a focused subagent with constrained tools (only available when `subagents` is configured). Pass `forked: true` to inherit the parent conversation — see [Forked subagents](#forked-subagents). |
763
806
 
764
807
  ### `ask_user` selections
765
808
 
@@ -169,6 +169,7 @@ The Reference section provides documentation of Mastra's API, including paramete
169
169
  - [PromptInjectionDetector](https://mastra.ai/reference/processors/prompt-injection-detector)
170
170
  - [SemanticRecall](https://mastra.ai/reference/processors/semantic-recall-processor)
171
171
  - [SkillSearchProcessor](https://mastra.ai/reference/processors/skill-search-processor)
172
+ - [StreamErrorRetryProcessor](https://mastra.ai/reference/processors/stream-error-retry-processor)
172
173
  - [SystemPromptScrubber](https://mastra.ai/reference/processors/system-prompt-scrubber)
173
174
  - [TokenLimiterProcessor](https://mastra.ai/reference/processors/token-limiter-processor)
174
175
  - [ToolCallFilter](https://mastra.ai/reference/processors/tool-call-filter)
@@ -209,6 +210,7 @@ The Reference section provides documentation of Mastra's API, including paramete
209
210
  - [MastraModelOutput](https://mastra.ai/reference/streaming/agents/MastraModelOutput)
210
211
  - [.stream()](https://mastra.ai/reference/streaming/agents/stream)
211
212
  - [.streamLegacy()](https://mastra.ai/reference/streaming/agents/streamLegacy)
213
+ - [.streamUntilIdle()](https://mastra.ai/reference/streaming/agents/streamUntilIdle)
212
214
  - [.observeStream()](https://mastra.ai/reference/streaming/workflows/observeStream)
213
215
  - [.resumeStream()](https://mastra.ai/reference/streaming/workflows/resumeStream)
214
216
  - [.stream()](https://mastra.ai/reference/streaming/workflows/stream)
@@ -126,6 +126,21 @@ interface ObservabilityExporter {
126
126
  /** Initialize exporter with tracing configuration and/or access to Mastra */
127
127
  init?(options: InitExporterOptions): void
128
128
 
129
+ /** Handle tracing events */
130
+ onTracingEvent?(event: TracingEvent): void | Promise<void>
131
+
132
+ /** Handle log events */
133
+ onLogEvent?(event: LogEvent): void | Promise<void>
134
+
135
+ /** Handle metric events */
136
+ onMetricEvent?(event: MetricEvent): void | Promise<void>
137
+
138
+ /** Handle score events */
139
+ onScoreEvent?(event: ScoreEvent): void | Promise<void>
140
+
141
+ /** Handle feedback events */
142
+ onFeedbackEvent?(event: FeedbackEvent): void | Promise<void>
143
+
129
144
  /** Export tracing events */
130
145
  exportTracingEvent(event: TracingEvent): Promise<void>
131
146
 
@@ -154,6 +169,8 @@ interface ObservabilityExporter {
154
169
  }
155
170
  ```
156
171
 
172
+ Event callback payloads use observability event bus envelopes: `TracingEvent` carries span lifecycle events with `exportedSpan`, `LogEvent` wraps `ExportedLog` in `log`, `MetricEvent` wraps `ExportedMetric` in `metric`, `ScoreEvent` wraps `ExportedScore` in `score`, and `FeedbackEvent` wraps `ExportedFeedback` in `feedback`. For Cloud exporter behavior for these callbacks, see [CloudExporter](https://mastra.ai/reference/observability/tracing/exporters/cloud-exporter).
173
+
157
174
  ### `SpanOutputProcessor`
158
175
 
159
176
  Interface for span output processors.
@@ -0,0 +1,54 @@
1
+ # StreamErrorRetryProcessor
2
+
3
+ `StreamErrorRetryProcessor` is an **error processor** that retries transient errors emitted after an LLM stream starts. It includes built-in matching for OpenAI Responses stream errors and supports additional matchers for other provider-specific stream error shapes.
4
+
5
+ The processor isn't enabled by default in core. Add it to `errorProcessors` for agents that need stream-error retry handling.
6
+
7
+ ## Usage example
8
+
9
+ Add `StreamErrorRetryProcessor` to `errorProcessors`:
10
+
11
+ ```typescript
12
+ import { Agent } from '@mastra/core/agent'
13
+ import { StreamErrorRetryProcessor } from '@mastra/core/processors'
14
+
15
+ export const agent = new Agent({
16
+ name: 'openai-agent',
17
+ instructions: 'You are a helpful assistant.',
18
+ model: 'openai/gpt-5',
19
+ errorProcessors: [new StreamErrorRetryProcessor()],
20
+ })
21
+ ```
22
+
23
+ ## How it works
24
+
25
+ The processor checks the error and its cause chain for:
26
+
27
+ - Provider retry metadata: `isRetryable === true`
28
+ - Built-in OpenAI Responses stream error matching
29
+ - Matcher results: Any configured matcher that returns `true`
30
+
31
+ When the error is retryable, the processor returns `{ retry: true }`. It doesn't mutate messages.
32
+
33
+ ## Default OpenAI Responses matcher
34
+
35
+ `isRetryableOpenAIResponsesStreamError` matches OpenAI Responses stream error chunks with `type: 'error'` or `type: 'response.failed'`. It retries known transient OpenAI error codes and, as a fallback, errors with explicit retry guidance such as `You can retry your request`.
36
+
37
+ `StreamErrorRetryProcessor` includes this matcher by default. You can also import it and reuse it in custom retry logic.
38
+
39
+ ## Constructor parameters
40
+
41
+ **options** (`StreamErrorRetryProcessorOptions`): Configuration for retry handling.
42
+
43
+ ## Properties
44
+
45
+ **id** (`'stream-error-retry-processor'`): Processor identifier.
46
+
47
+ **name** (`'Stream Error Retry Processor'`): Processor display name.
48
+
49
+ **processAPIError** (`(args: ProcessAPIErrorArgs) => ProcessAPIErrorResult | void`): Retries stream errors up to the configured retry limit.
50
+
51
+ ## Related
52
+
53
+ - [Processor interface](https://mastra.ai/reference/processors/processor-interface)
54
+ - [Processors](https://mastra.ai/docs/agents/processors)
@@ -398,6 +398,146 @@ Contains output from workflow step execution, used primarily for usage tracking
398
398
 
399
399
  **payload.output** (`ChunkType`): Nested chunk data from step execution, typically containing finish events or other step results
400
400
 
401
+ ## Background task chunks
402
+
403
+ Emitted when a tool call is dispatched as a [background task](https://mastra.ai/docs/agents/background-tasks) and `streamUntilIdle()` is used.
404
+
405
+ ### background-task-started
406
+
407
+ Emitted when a tool call is enqueued as a background task and assigned a `taskId`.
408
+
409
+ **type** (`"background-task-started"`): Chunk type identifier
410
+
411
+ **payload** (`BackgroundTaskStartedPayload`): Identifies the newly enqueued task
412
+
413
+ **payload.taskId** (`string`): Unique identifier for the background task
414
+
415
+ **payload.toolName** (`string`): Name of the tool being executed
416
+
417
+ **payload.toolCallId** (`string`): Tool-call ID from the originating LLM tool call
418
+
419
+ ### background-task-running
420
+
421
+ Emitted when a worker picks up the task and execution begins.
422
+
423
+ **type** (`"background-task-running"`): Chunk type identifier
424
+
425
+ **payload** (`BackgroundTaskRunningPayload`): Details about the running task
426
+
427
+ **payload.taskId** (`string`): Unique identifier for the background task
428
+
429
+ **payload.toolName** (`string`): Name of the tool being executed
430
+
431
+ **payload.toolCallId** (`string`): Tool-call ID from the originating LLM tool call
432
+
433
+ **payload.runId** (`string`): Run ID of the agent that dispatched the task
434
+
435
+ **payload.agentId** (`string`): ID of the agent that dispatched the task
436
+
437
+ **payload.startedAt** (`Date`): Timestamp at which execution started
438
+
439
+ **payload.args** (`Record<string, unknown>`): Arguments passed to the tool's execute function
440
+
441
+ ### background-task-progress
442
+
443
+ Periodic snapshot of how many background tasks are currently running across the agent.
444
+
445
+ **type** (`"background-task-progress"`): Chunk type identifier
446
+
447
+ **payload** (`BackgroundTaskProgressPayload`): Aggregate progress for all running tasks
448
+
449
+ **payload.taskIds** (`string[]`): IDs of all currently running background tasks
450
+
451
+ **payload.runningCount** (`number`): Number of background tasks currently running
452
+
453
+ **payload.elapsedMs** (`number`): Milliseconds elapsed since the agent run started
454
+
455
+ ### background-task-output
456
+
457
+ A streamed output chunk emitted by the task's `execute` function. Wraps an inner [`tool-output`](#tool-output) chunk.
458
+
459
+ **type** (`"background-task-output"`): Chunk type identifier
460
+
461
+ **payload** (`BackgroundTaskOutputPayload`): Streamed output from the running task
462
+
463
+ **payload.taskId** (`string`): Unique identifier for the background task
464
+
465
+ **payload.toolName** (`string`): Name of the tool being executed
466
+
467
+ **payload.toolCallId** (`string`): Tool-call ID from the originating LLM tool call
468
+
469
+ **payload.runId** (`string`): Run ID of the agent that dispatched the task
470
+
471
+ **payload.agentId** (`string`): ID of the agent that dispatched the task
472
+
473
+ **payload.payload** (`ToolOutputChunk`): Inner tool-output chunk produced by the task
474
+
475
+ ### background-task-completed
476
+
477
+ Emitted when the task finishes successfully. Triggers a continuation turn when consumed by [`Agent.streamUntilIdle()`](https://mastra.ai/reference/streaming/agents/streamUntilIdle).
478
+
479
+ **type** (`"background-task-completed"`): Chunk type identifier
480
+
481
+ **payload** (`BackgroundTaskResultPayload`): The completed task's result
482
+
483
+ **payload.taskId** (`string`): Unique identifier for the background task
484
+
485
+ **payload.toolName** (`string`): Name of the tool that was executed
486
+
487
+ **payload.toolCallId** (`string`): Tool-call ID from the originating LLM tool call
488
+
489
+ **payload.agentId** (`string`): ID of the agent that dispatched the task
490
+
491
+ **payload.runId** (`string`): Run ID of the agent that dispatched the task
492
+
493
+ **payload.result** (`unknown`): The tool's resolved return value
494
+
495
+ **payload.completedAt** (`Date`): Timestamp at which the task completed
496
+
497
+ **payload.isError** (`boolean`): True when the tool returned an error result rather than throwing
498
+
499
+ ### background-task-failed
500
+
501
+ Emitted when the task throws or times out. Triggers a continuation turn when consumed by [`Agent.streamUntilIdle()`](https://mastra.ai/reference/streaming/agents/streamUntilIdle).
502
+
503
+ **type** (`"background-task-failed"`): Chunk type identifier
504
+
505
+ **payload** (`BackgroundTaskFailedPayload`): Failure details for the task
506
+
507
+ **payload.taskId** (`string`): Unique identifier for the background task
508
+
509
+ **payload.toolName** (`string`): Name of the tool that was executed
510
+
511
+ **payload.toolCallId** (`string`): Tool-call ID from the originating LLM tool call
512
+
513
+ **payload.runId** (`string`): Run ID of the agent that dispatched the task
514
+
515
+ **payload.agentId** (`string`): ID of the agent that dispatched the task
516
+
517
+ **payload.error** (`{ message: string }`): Error details thrown by the task
518
+
519
+ **payload.completedAt** (`Date`): Timestamp at which the task failed
520
+
521
+ ### background-task-cancelled
522
+
523
+ Emitted when the task is cancelled before completing. Triggers a continuation turn when consumed by [`Agent.streamUntilIdle()`](https://mastra.ai/reference/streaming/agents/streamUntilIdle).
524
+
525
+ **type** (`"background-task-cancelled"`): Chunk type identifier
526
+
527
+ **payload** (`BackgroundTaskCancelledPayload`): Cancellation details for the task
528
+
529
+ **payload.taskId** (`string`): Unique identifier for the background task
530
+
531
+ **payload.toolName** (`string`): Name of the tool that was executed
532
+
533
+ **payload.toolCallId** (`string`): Tool-call ID from the originating LLM tool call
534
+
535
+ **payload.runId** (`string`): Run ID of the agent that dispatched the task
536
+
537
+ **payload.agentId** (`string`): ID of the agent that dispatched the task
538
+
539
+ **payload.completedAt** (`Date`): Timestamp at which the task was cancelled
540
+
401
541
  ## Metadata and special chunks
402
542
 
403
543
  ### response-metadata
@@ -0,0 +1,94 @@
1
+ # Agent.streamUntilIdle()
2
+
3
+ **Added in:** `@mastra/core@1.28.0`
4
+
5
+ `streamUntilIdle()` streams an agent's response and keeps the stream open until every background task dispatched during the run completes. When a task finishes, its result is written to memory and the agentic loop re-enters automatically so the LLM can react to it. The stream closes once no tasks are running and no completions are queued.
6
+
7
+ Use it when the agent dispatches background tasks (typically long-running tools or subagents) and you want a single stream that spans the initial response **plus** every continuation triggered by a task completion. For foreground-only runs or if you prefer to manage the continuation manually (manually prompt agent to process the result), use [`Agent.stream()`](https://mastra.ai/reference/streaming/agents/stream).
8
+
9
+ ## Usage example
10
+
11
+ ```ts
12
+ const stream = await agent.streamUntilIdle('Research solana for me', {
13
+ memory: { thread: 't1', resource: 'u1' },
14
+ })
15
+
16
+ for await (const chunk of stream.fullStream) {
17
+ // chunks from the initial turn AND any continuation turns triggered by
18
+ // background task completions flow through here
19
+ }
20
+ ```
21
+
22
+ > **Info:** `streamUntilIdle()` requires both a [`BackgroundTaskManager`](https://mastra.ai/reference/configuration) and a [memory](https://mastra.ai/docs/memory/overview) backend. Without either, it falls through to a plain `agent.stream()` call.
23
+
24
+ ## Parameters
25
+
26
+ **messages** (`string | string[] | CoreMessage[] | AiMessageType[] | UIMessageWithMetadata[]`): The messages to send to the agent. Can be a single string, array of strings, or structured message objects.
27
+
28
+ **options** (`AgentExecutionOptions<Output> & { maxIdleMs?: number }`): Accepts every option that Agent.stream() accepts, plus maxIdleMs. See the Agent.stream() reference for the full list.
29
+
30
+ **options.maxIdleMs** (`number`): Closes the outer stream after this many ms of idleness between turns. The timer only runs while the wrapper is between turns, so a slow first token does not close the stream. Default: 5 minutes.
31
+
32
+ **options.memory** (`{ thread?: string | { id: string }; resource?: string }`): Memory thread and resource for the run. Required for continuations to write background task results back into the conversation.
33
+
34
+ **options.structuredOutput** (`PublicStructuredOutputOptions<Output>`): Schema-based structured output. Same shape as Agent.stream(). Note that aggregate properties resolve against the first turn only.
35
+
36
+ For every other option (`maxSteps`, `modelSettings`, `toolChoice`, `outputProcessors`, `onFinish`, `onChunk`, etc.), see the [`Agent.stream()` parameters](https://mastra.ai/reference/streaming/agents/stream). `streamUntilIdle()` forwards them to the initial turn.
37
+
38
+ ## Returns
39
+
40
+ **stream** (`MastraModelOutput<Output>`): A MastraModelOutput where fullStream spans the initial turn plus every auto-continuation. Aggregate properties (text, toolCalls, toolResults, finishReason, messageList, getFullOutput()) resolve against the first turn only.
41
+
42
+ ### Aggregate properties caveat
43
+
44
+ `streamUntilIdle()` returns a proxy over the first turn's `MastraModelOutput`. Only `fullStream` is replaced with a combined stream that spans every continuation. Every other property — `text`, `toolCalls`, `toolResults`, `finishReason`, `messageList`, `getFullOutput()` — resolves against the **first turn's** internal buffer.
45
+
46
+ If you need an aggregate view across all continuations, consume `fullStream` yourself and accumulate.
47
+
48
+ ## Continuation behavior
49
+
50
+ Internally, `streamUntilIdle()`:
51
+
52
+ 1. Runs the initial turn via `agent.stream(...)` and pipes its `fullStream` into the outer stream.
53
+ 2. Subscribes to background-task completion events for the resolved memory scope.
54
+ 3. Queues each terminal event (`background-task-completed`, `background-task-failed`, `background-task-cancelled`) and, when the outer wrapper is idle between turns, re-invokes `agent.stream([], ...)` with a directive listing the just-completed `toolCallId`s. The continuation turn flows into the same outer stream.
55
+ 4. Closes the outer stream once no tasks are running and no completions are queued.
56
+
57
+ ## Extended usage example
58
+
59
+ ### Cap idle time between turns
60
+
61
+ ```ts
62
+ const stream = await agent.streamUntilIdle('Kick off the long jobs', {
63
+ memory: { thread: 't1', resource: 'u1' },
64
+ maxIdleMs: 60_000, // close the stream after 1 minute of idleness between turns
65
+ })
66
+
67
+ for await (const chunk of stream.fullStream) {
68
+ if (chunk.type === 'background-task-completed') {
69
+ console.log('Task complete:', chunk.payload.taskId)
70
+ }
71
+ }
72
+ ```
73
+
74
+ ### Aggregate text across continuations
75
+
76
+ ```ts
77
+ const stream = await agent.streamUntilIdle('Research and summarize', {
78
+ memory: { thread: 't1', resource: 'u1' },
79
+ })
80
+
81
+ let fullText = ''
82
+ for await (const chunk of stream.fullStream) {
83
+ if (chunk.type === 'text-delta') {
84
+ fullText += chunk.payload.text
85
+ }
86
+ }
87
+ ```
88
+
89
+ ## Related
90
+
91
+ - [Background tasks](https://mastra.ai/docs/agents/background-tasks)
92
+ - [`Agent.stream()` reference](https://mastra.ai/reference/streaming/agents/stream)
93
+ - [backgroundTasks configuration reference](https://mastra.ai/reference/configuration)
94
+ - [Stream chunk types](https://mastra.ai/reference/streaming/ChunkType)