@mastra/mcp-docs-server 0.13.17-alpha.2 → 0.13.17-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/organized/changelogs/%40mastra%2Fagent-builder.md +27 -0
- package/.docs/organized/changelogs/%40mastra%2Fai-sdk.md +10 -0
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +24 -24
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +49 -49
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +16 -0
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +12 -12
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +32 -32
- package/.docs/organized/changelogs/%40mastra%2Fevals.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Ffirecrawl.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fgithub.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +22 -22
- package/.docs/organized/changelogs/%40mastra%2Fmcp-registry-registry.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +14 -14
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +31 -31
- package/.docs/organized/changelogs/%40mastra%2Fragie.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fschema-compat.md +7 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +23 -23
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google-gemini-live.md +10 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai-realtime.md +11 -11
- package/.docs/organized/changelogs/create-mastra.md +13 -13
- package/.docs/organized/changelogs/mastra.md +31 -31
- package/.docs/organized/code-examples/a2a.md +1 -1
- package/.docs/organized/code-examples/agent-network.md +1 -1
- package/.docs/organized/code-examples/agent.md +22 -1
- package/.docs/organized/code-examples/agui.md +1 -1
- package/.docs/organized/code-examples/ai-sdk-useChat.md +1 -1
- package/.docs/organized/code-examples/ai-sdk-v5.md +2 -2
- package/.docs/organized/code-examples/assistant-ui.md +3 -3
- package/.docs/organized/code-examples/bird-checker-with-express.md +1 -1
- package/.docs/organized/code-examples/bird-checker-with-nextjs-and-eval.md +1 -1
- package/.docs/organized/code-examples/bird-checker-with-nextjs.md +1 -1
- package/.docs/organized/code-examples/client-side-tools.md +1 -1
- package/.docs/organized/code-examples/crypto-chatbot.md +1 -1
- package/.docs/organized/code-examples/experimental-auth-weather-agent.md +1 -1
- package/.docs/organized/code-examples/fireworks-r1.md +1 -1
- package/.docs/organized/code-examples/mcp-configuration.md +2 -2
- package/.docs/organized/code-examples/mcp-registry-registry.md +1 -1
- package/.docs/organized/code-examples/memory-with-mem0.md +1 -1
- package/.docs/organized/code-examples/memory-with-processors.md +1 -1
- package/.docs/organized/code-examples/openapi-spec-writer.md +2 -2
- package/.docs/organized/code-examples/quick-start.md +1 -1
- package/.docs/organized/code-examples/stock-price-tool.md +1 -1
- package/.docs/organized/code-examples/weather-agent.md +1 -1
- package/.docs/organized/code-examples/workflow-ai-recruiter.md +1 -1
- package/.docs/organized/code-examples/workflow-with-inline-steps.md +1 -1
- package/.docs/organized/code-examples/workflow-with-memory.md +1 -1
- package/.docs/organized/code-examples/workflow-with-separate-steps.md +1 -1
- package/.docs/organized/code-examples/workflow-with-suspend-resume.md +1 -1
- package/.docs/raw/agents/overview.mdx +35 -4
- package/.docs/raw/deployment/monorepo.mdx +1 -1
- package/.docs/raw/frameworks/agentic-uis/ai-sdk.mdx +44 -14
- package/.docs/raw/getting-started/installation.mdx +52 -4
- package/.docs/raw/getting-started/templates.mdx +2 -22
- package/.docs/raw/reference/agents/generate.mdx +2 -2
- package/.docs/raw/reference/agents/getDefaultStreamOptions.mdx +2 -1
- package/.docs/raw/reference/agents/getDefaultVNextStreamOptions.mdx +1 -1
- package/.docs/raw/reference/agents/stream.mdx +2 -2
- package/.docs/raw/reference/agents/streamVNext.mdx +2 -4
- package/.docs/raw/reference/cli/build.mdx +0 -6
- package/.docs/raw/reference/cli/start.mdx +8 -1
- package/.docs/raw/reference/scorers/noise-sensitivity.mdx +237 -0
- package/.docs/raw/reference/scorers/prompt-alignment.mdx +369 -0
- package/.docs/raw/reference/workflows/run-methods/streamVNext.mdx +3 -5
- package/.docs/raw/scorers/off-the-shelf-scorers.mdx +2 -2
- package/.docs/raw/streaming/events.mdx +115 -0
- package/.docs/raw/streaming/overview.mdx +137 -0
- package/.docs/raw/streaming/tool-streaming.mdx +113 -0
- package/.docs/raw/streaming/workflow-streaming.mdx +97 -0
- package/.docs/raw/tools-mcp/overview.mdx +44 -0
- package/.docs/raw/workflows/overview.mdx +19 -17
- package/dist/stdio.js +5 -1
- package/dist/tools/docs.d.ts.map +1 -1
- package/package.json +7 -7
- package/.docs/raw/agents/streaming.mdx +0 -154
- package/.docs/raw/workflows/streaming.mdx +0 -115
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Streaming Events | Streaming | Mastra"
|
|
3
|
+
description: "Learn about the different types of streaming events in Mastra, including text deltas, tool calls, step events, and how to handle them in your applications."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Streaming Events
|
|
7
|
+
|
|
8
|
+
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.
|
|
9
|
+
|
|
10
|
+
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.
|
|
11
|
+
|
|
12
|
+
## Event types
|
|
13
|
+
|
|
14
|
+
Below is a complete list of events emitted from `.streamVNext()`.
|
|
15
|
+
Depending on whether you’re streaming from an **agent** or a **workflow**, only a subset of these events will occur:
|
|
16
|
+
|
|
17
|
+
- **start**: Marks the beginning of an agent or workflow run.
|
|
18
|
+
- **step-start**: Indicates a workflow step has begun execution.
|
|
19
|
+
- **text-delta**: Incremental text chunks as they're generated by the LLM.
|
|
20
|
+
- **tool-call**: When the agent decides to use a tool, including the tool name and arguments.
|
|
21
|
+
- **tool-result**: The result returned from tool execution.
|
|
22
|
+
- **step-finish**: Confirms that a specific step has fully finalized, and may include metadata like the finish reason for that step.
|
|
23
|
+
- **finish**: When the agent or workflow completes, including usage statistics.
|
|
24
|
+
|
|
25
|
+
## Inspecting agent streams
|
|
26
|
+
|
|
27
|
+
Iterate over the `stream` with a `for await` loop to inspect all emitted event chunks.
|
|
28
|
+
|
|
29
|
+
```typescript {3,7} showLineNumbers copy
|
|
30
|
+
const testAgent = mastra.getAgent("testAgent");
|
|
31
|
+
|
|
32
|
+
const stream = await testAgent.streamVNext([
|
|
33
|
+
{ role: "user", content: "Help me organize my day" },
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
for await (const chunk of stream) {
|
|
37
|
+
console.log(chunk);
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
> See [Agent.streamVNext()](../../reference/agents/streamVNext.mdx) for more information.
|
|
42
|
+
|
|
43
|
+
### Example agent output
|
|
44
|
+
|
|
45
|
+
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`.
|
|
46
|
+
|
|
47
|
+
```typescript {2,7,15}
|
|
48
|
+
{
|
|
49
|
+
type: 'start',
|
|
50
|
+
from: 'AGENT',
|
|
51
|
+
// ..
|
|
52
|
+
}
|
|
53
|
+
{
|
|
54
|
+
type: 'step-start',
|
|
55
|
+
from: 'AGENT',
|
|
56
|
+
payload: {
|
|
57
|
+
messageId: 'msg-cdUrkirvXw8A6oE4t5lzDuxi',
|
|
58
|
+
// ...
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
{
|
|
62
|
+
type: 'tool-call',
|
|
63
|
+
from: 'AGENT',
|
|
64
|
+
payload: {
|
|
65
|
+
toolCallId: 'call_jbhi3s1qvR6Aqt9axCfTBMsA',
|
|
66
|
+
toolName: 'testTool'
|
|
67
|
+
// ..
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Inspecting workflow streams
|
|
73
|
+
|
|
74
|
+
Iterate over the `stream` with a `for await` loop to inspect all emitted event chunks.
|
|
75
|
+
|
|
76
|
+
```typescript {5,11} showLineNumbers copy
|
|
77
|
+
const testWorkflow = mastra.getWorkflow("testWorkflow");
|
|
78
|
+
|
|
79
|
+
const run = await testWorkflow.createRunAsync();
|
|
80
|
+
|
|
81
|
+
const stream = await run.streamVNext({
|
|
82
|
+
inputData: {
|
|
83
|
+
value: "initial data"
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
for await (const chunk of stream) {
|
|
88
|
+
console.log(chunk);
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Example workflow output
|
|
93
|
+
|
|
94
|
+
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`.
|
|
95
|
+
|
|
96
|
+
```typescript {2,8,11}
|
|
97
|
+
{
|
|
98
|
+
type: 'start',
|
|
99
|
+
runId: '221333ed-d9ee-4737-922b-4ab4d9de73e6',
|
|
100
|
+
from: 'WORKFLOW',
|
|
101
|
+
// ...
|
|
102
|
+
}
|
|
103
|
+
{
|
|
104
|
+
type: 'step-start',
|
|
105
|
+
runId: '221333ed-d9ee-4737-922b-4ab4d9de73e6',
|
|
106
|
+
from: 'WORKFLOW',
|
|
107
|
+
payload: {
|
|
108
|
+
stepName: 'step-1',
|
|
109
|
+
args: { value: 'initial data' },
|
|
110
|
+
stepCallId: '9e8c5217-490b-4fe7-8c31-6e2353a3fc98',
|
|
111
|
+
startedAt: 1755269732792,
|
|
112
|
+
status: 'running'
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
```
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Streaming Overview | Streaming | Mastra"
|
|
3
|
+
description: "Streaming in Mastra enables real-time, incremental responses from both agents and workflows, providing immediate feedback as AI-generated content is produced."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { StreamVNextCallout } from "@/components/streamVNext-callout.tsx"
|
|
7
|
+
|
|
8
|
+
# Streaming Overview
|
|
9
|
+
|
|
10
|
+
<StreamVNextCallout />
|
|
11
|
+
|
|
12
|
+
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.
|
|
13
|
+
|
|
14
|
+
## Getting started
|
|
15
|
+
|
|
16
|
+
Mastra currently supports two streaming methods, this page explains how to use `streamVNext()`.
|
|
17
|
+
|
|
18
|
+
1. **`.stream()`**: Current stable API, supports **AI SDK v4** (`LanguageModelV1`).
|
|
19
|
+
2. **`.streamVNext()`**: Experimental API, supports **AI SDK v5** (`LanguageModelV2`).
|
|
20
|
+
|
|
21
|
+
## Streaming with agents
|
|
22
|
+
|
|
23
|
+
You can pass a single string for simple 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.
|
|
24
|
+
|
|
25
|
+
### Using `Agent.streamVNext()`
|
|
26
|
+
|
|
27
|
+
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.
|
|
28
|
+
|
|
29
|
+
```typescript {3,7} showLineNumbers copy
|
|
30
|
+
const testAgent = mastra.getAgent("testAgent");
|
|
31
|
+
|
|
32
|
+
const stream = await testAgent.streamVNext([
|
|
33
|
+
{ role: "user", content: "Help me organize my day" },
|
|
34
|
+
]);
|
|
35
|
+
|
|
36
|
+
for await (const chunk of stream.textStream) {
|
|
37
|
+
process.stdout.write(chunk);
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
> See [Agent.streamVNext()](../../reference/agents/streamVNext.mdx) for more information.
|
|
42
|
+
|
|
43
|
+
### Output from `Agent.streamVNext()`
|
|
44
|
+
|
|
45
|
+
The output streams the generated response from the agent.
|
|
46
|
+
|
|
47
|
+
```text
|
|
48
|
+
Of course!
|
|
49
|
+
To help you organize your day effectively, I need a bit more information.
|
|
50
|
+
Here are some questions to consider:
|
|
51
|
+
...
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Agent stream properties
|
|
55
|
+
|
|
56
|
+
An agent stream provides access to various response properties:
|
|
57
|
+
|
|
58
|
+
- **`stream.textStream`**: A readable stream that emits text chunks.
|
|
59
|
+
- **`stream.text`**: Promise that resolves to the full text response.
|
|
60
|
+
- **`stream.finishReason`**: The reason the agent stopped streaming.
|
|
61
|
+
- **`stream.usage`**: Token usage information.
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
### AI SDK v5 Compatibility
|
|
65
|
+
|
|
66
|
+
For integration with AI SDK v5, use `format` 'aisdk' to get an `AISDKV5OutputStream`:
|
|
67
|
+
|
|
68
|
+
```typescript {5} showLineNumbers copy
|
|
69
|
+
const testAgent = mastra.getAgent("testAgent");
|
|
70
|
+
|
|
71
|
+
const stream = await testAgent.streamVNext(
|
|
72
|
+
[{ role: "user", content: "Help me organize my day" }],
|
|
73
|
+
{ format: "aisdk" }
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
for await (const chunk of stream.textStream) {
|
|
77
|
+
process.stdout.write(chunk);
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Streaming with workflows
|
|
82
|
+
|
|
83
|
+
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 `.createRunAsync()`.
|
|
84
|
+
|
|
85
|
+
### Using `Run.streamVNext()`
|
|
86
|
+
|
|
87
|
+
This is the experimental API. It returns a `ReadableStream` of events directly.
|
|
88
|
+
|
|
89
|
+
```typescript {3,9} showLineNumbers copy
|
|
90
|
+
const run = await testWorkflow.createRunAsync();
|
|
91
|
+
|
|
92
|
+
const stream = await run.streamVNext({
|
|
93
|
+
inputData: {
|
|
94
|
+
value: "initial data"
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
for await (const chunk of stream) {
|
|
99
|
+
console.log(chunk);
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
> See [Run.streamVNext()](../../reference/workflows/run-methods/streamVNext.mdx) for more information.
|
|
104
|
+
|
|
105
|
+
### Output from `Run.streamVNext()`
|
|
106
|
+
|
|
107
|
+
The experimental API event structure includes `runId` and `from` at the top level, making it easier to identify and track workflow runs without digging into the payload.
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
// ...
|
|
111
|
+
{
|
|
112
|
+
type: 'step-start',
|
|
113
|
+
runId: '1eeaf01a-d2bf-4e3f-8d1b-027795ccd3df',
|
|
114
|
+
from: 'WORKFLOW',
|
|
115
|
+
payload: {
|
|
116
|
+
stepName: 'step-1',
|
|
117
|
+
args: { value: 'initial data' },
|
|
118
|
+
stepCallId: '8e15e618-be0e-4215-a5d6-08e58c152068',
|
|
119
|
+
startedAt: 1755121710066,
|
|
120
|
+
status: 'running'
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Workflow stream properties
|
|
126
|
+
|
|
127
|
+
A workflow stream provides access to various response properties:
|
|
128
|
+
|
|
129
|
+
- **`stream.status`**: The status of the workflow run.
|
|
130
|
+
- **`stream.result`**: The result of the workflow run.
|
|
131
|
+
- **`stream.usage`**: The total token usage of the workflow run.
|
|
132
|
+
|
|
133
|
+
## Related
|
|
134
|
+
|
|
135
|
+
- [Streaming events](./events.mdx)
|
|
136
|
+
- [Using Agents](../agents/overview.mdx)
|
|
137
|
+
- [Workflows overview](../workflows/overview.mdx)
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Tool Streaming | Streaming | Mastra"
|
|
3
|
+
description: "Learn how to use tool streaming in Mastra, including handling tool calls, tool results, and tool execution events during streaming."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { Callout } from "nextra/components";
|
|
7
|
+
|
|
8
|
+
# Tool streaming
|
|
9
|
+
|
|
10
|
+
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.
|
|
11
|
+
|
|
12
|
+
Streams can be written to in two main ways:
|
|
13
|
+
|
|
14
|
+
- **From within a tool**: every tool receives a `writer` argument, which is a writable stream you can use to push updates as execution progresses.
|
|
15
|
+
- **From an agent stream**: you can also pipe an agent’s `streamVNext` output directly into a tool’s writer, making it easy to chain agent responses into tool results without extra glue code.
|
|
16
|
+
|
|
17
|
+
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.
|
|
18
|
+
|
|
19
|
+
## Agent using tool
|
|
20
|
+
|
|
21
|
+
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.
|
|
22
|
+
|
|
23
|
+
```typescript {4,10} showLineNumbers copy
|
|
24
|
+
import { openai } from "@ai-sdk/openai";
|
|
25
|
+
import { Agent } from "@mastra/core/agent";
|
|
26
|
+
|
|
27
|
+
import { testTool } from "../tools/test-tool";
|
|
28
|
+
|
|
29
|
+
export const testAgent = new Agent({
|
|
30
|
+
name: "test-agent",
|
|
31
|
+
instructions: "You are a weather agent.",
|
|
32
|
+
model: openai("gpt-4o-mini"),
|
|
33
|
+
tools: { testTool }
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Using the `writer` argument
|
|
38
|
+
|
|
39
|
+
The `writer` argument is passed to 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.
|
|
40
|
+
|
|
41
|
+
<Callout type="warning">
|
|
42
|
+
You must `await` the call to `writer.write(...)` or else you will lock the stream and get a `WritableStream is locked` error.
|
|
43
|
+
</Callout>
|
|
44
|
+
|
|
45
|
+
```typescript {5,8,15} showLineNumbers copy
|
|
46
|
+
import { createTool } from "@mastra/core/tools";
|
|
47
|
+
|
|
48
|
+
export const testTool = createTool({
|
|
49
|
+
// ...
|
|
50
|
+
execute: async ({ context, writer }) => {
|
|
51
|
+
const { value } = context;
|
|
52
|
+
|
|
53
|
+
await writer?.write({
|
|
54
|
+
type: "custom-event",
|
|
55
|
+
status: "pending"
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const response = await fetch(...);
|
|
59
|
+
|
|
60
|
+
await writer?.write({
|
|
61
|
+
type: "custom-event",
|
|
62
|
+
status: "success"
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
value: ""
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Inspecting stream payloads
|
|
73
|
+
|
|
74
|
+
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.
|
|
75
|
+
|
|
76
|
+
```typescript showLineNumbers copy
|
|
77
|
+
const stream = await testAgent.streamVNext([
|
|
78
|
+
"What is the weather in London?",
|
|
79
|
+
"Use the testTool"
|
|
80
|
+
]);
|
|
81
|
+
|
|
82
|
+
for await (const chunk of stream) {
|
|
83
|
+
if (chunk.payload.output?.type === "custom-event") {
|
|
84
|
+
console.log(JSON.stringify(chunk, null, 2));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Tool using an agent
|
|
90
|
+
|
|
91
|
+
Pipe an agent’s `textStream` to the tool’s `writer`. This streams partial output, and Mastra automatically aggregates the agent’s usage into the tool run.
|
|
92
|
+
|
|
93
|
+
```typescript showLineNumbers copy
|
|
94
|
+
import { createTool } from "@mastra/core/tools";
|
|
95
|
+
import { z } from "zod";
|
|
96
|
+
|
|
97
|
+
export const testTool = createTool({
|
|
98
|
+
// ...
|
|
99
|
+
execute: async ({ context, mastra, writer }) => {
|
|
100
|
+
const { city } = context;
|
|
101
|
+
|
|
102
|
+
const testAgent = mastra?.getAgent("testAgent");
|
|
103
|
+
const stream = await testAgent?.streamVNext(`What is the weather in ${city}?`);
|
|
104
|
+
|
|
105
|
+
await stream!.textStream.pipeTo(writer!);
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
value: await stream!.text
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Workflow Streaming | Streaming | Mastra"
|
|
3
|
+
description: "Learn how to use workflow streaming in Mastra, including handling workflow execution events, step streaming, and workflow integration with agents and tools."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { Callout } from "nextra/components";
|
|
7
|
+
|
|
8
|
+
# Workflow streaming
|
|
9
|
+
|
|
10
|
+
Workflow streaming in Mastra enables workflows to send incremental results while they execute, rather than waiting until completion. This allows you to surface partial progress, intermediate states, or progressive data directly to users or upstream agents and workflows.
|
|
11
|
+
|
|
12
|
+
Streams can be written to in two main ways:
|
|
13
|
+
|
|
14
|
+
- **From within a workflow step**: every workflow step receives a `writer` argument, which is a writable stream you can use to push updates as execution progresses.
|
|
15
|
+
- **From an agent stream**: you can also pipe an agent's `streamVNext` output directly into a workflow step's writer, making it easy to chain agent responses into workflow results without extra glue code.
|
|
16
|
+
|
|
17
|
+
By combining writable workflow streams with agent streaming, you gain fine-grained control over how intermediate results flow through your system and into the user experience.
|
|
18
|
+
|
|
19
|
+
### Using the `writer` argument
|
|
20
|
+
|
|
21
|
+
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.
|
|
22
|
+
|
|
23
|
+
<Callout type="warning">
|
|
24
|
+
You must `await` the call to `writer.write(...)` or else you will lock the stream and get a `WritableStream is locked` error.
|
|
25
|
+
</Callout>
|
|
26
|
+
|
|
27
|
+
```typescript {5,8,15} showLineNumbers copy
|
|
28
|
+
import { createStep } from "@mastra/core/workflows";
|
|
29
|
+
|
|
30
|
+
export const testStep = createStep({
|
|
31
|
+
// ...
|
|
32
|
+
execute: async ({ inputData, writer }) => {
|
|
33
|
+
const { value } = inputData;
|
|
34
|
+
|
|
35
|
+
await writer?.write({
|
|
36
|
+
type: "custom-event",
|
|
37
|
+
status: "pending"
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const response = await fetch(...);
|
|
41
|
+
|
|
42
|
+
await writer?.write({
|
|
43
|
+
type: "custom-event",
|
|
44
|
+
status: "success"
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
value: ""
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Inspecting workflow stream payloads
|
|
55
|
+
|
|
56
|
+
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 step-specific data.
|
|
57
|
+
|
|
58
|
+
```typescript showLineNumbers copy
|
|
59
|
+
const testWorkflow = mastra.getWorkflow("testWorkflow");
|
|
60
|
+
|
|
61
|
+
const run = await testWorkflow.createRunAsync();
|
|
62
|
+
|
|
63
|
+
const stream = await run.streamVNext({
|
|
64
|
+
inputData: {
|
|
65
|
+
value: "initial data"
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
for await (const chunk of stream) {
|
|
70
|
+
console.log(chunk);
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Workflow using an agent
|
|
75
|
+
|
|
76
|
+
Pipe an agent's `textStream` to the workflow step's `writer`. This streams partial output, and Mastra automatically aggregates the agent's usage into the workflow run.
|
|
77
|
+
|
|
78
|
+
```typescript showLineNumbers copy
|
|
79
|
+
import { createStep } from "@mastra/core/workflows";
|
|
80
|
+
import { z } from "zod";
|
|
81
|
+
|
|
82
|
+
export const testStep = createStep({
|
|
83
|
+
// ...
|
|
84
|
+
execute: async ({ inputData, mastra, writer }) => {
|
|
85
|
+
const { city } = inputData
|
|
86
|
+
|
|
87
|
+
const testAgent = mastra?.getAgent("testAgent");
|
|
88
|
+
const stream = await testAgent?.streamVNext(`What is the weather in ${city}$?`);
|
|
89
|
+
|
|
90
|
+
await stream!.textStream.pipeTo(writer!);
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
value: await stream!.text,
|
|
94
|
+
};
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
```
|
|
@@ -3,6 +3,8 @@ title: "Tools Overview | Tools & MCP | Mastra Docs"
|
|
|
3
3
|
description: Understand what tools are in Mastra, how to add them to agents, and best practices for designing effective tools.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
+
import { Steps } from "nextra/components";
|
|
7
|
+
|
|
6
8
|
# Tools Overview
|
|
7
9
|
|
|
8
10
|
Tools are functions that agents can execute to perform specific tasks or access external information. They extend an agent's capabilities beyond simple text generation, allowing interaction with APIs, databases, or other systems.
|
|
@@ -65,3 +67,45 @@ Some providers that we include this layer for:
|
|
|
65
67
|
- **DeepSeek & Meta:** Apply similar compatibility logic to ensure schema alignment and tool usability.
|
|
66
68
|
|
|
67
69
|
This approach makes tool usage more reliable and model-agnostic for both custom and MCP tools.
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
## Testing tools locally
|
|
73
|
+
There are two ways to run and test tools.
|
|
74
|
+
|
|
75
|
+
<Steps>
|
|
76
|
+
|
|
77
|
+
### Mastra Playground
|
|
78
|
+
|
|
79
|
+
With the Mastra Dev Server running you can test a tool from the Mastra Playground by visiting [http://localhost:4111/tools](http://localhost:4111/tools) in your browser.
|
|
80
|
+
|
|
81
|
+
> For more information, see the [Local Dev Playground](/docs/server-db/local-dev-playground) documentation.
|
|
82
|
+
|
|
83
|
+
### Command line
|
|
84
|
+
|
|
85
|
+
Invoke a tool using `.execute()`.
|
|
86
|
+
|
|
87
|
+
```typescript filename="src/test-tool.ts" showLineNumbers copy
|
|
88
|
+
import { RuntimeContext } from "@mastra/core/runtime-context";
|
|
89
|
+
import { testTool } from "./mastra/tools/test-tool";
|
|
90
|
+
|
|
91
|
+
const runtimeContext = new RuntimeContext();
|
|
92
|
+
|
|
93
|
+
const result = await testTool.execute({
|
|
94
|
+
context: {
|
|
95
|
+
value: "foo"
|
|
96
|
+
},
|
|
97
|
+
runtimeContext
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
console.log(result);
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
> See [createTool()](../../reference/tools/create-tool.mdx) for more information.
|
|
104
|
+
|
|
105
|
+
To test this tool, run the following:
|
|
106
|
+
|
|
107
|
+
```bash copy
|
|
108
|
+
npx tsx src/test-tool.ts
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
</Steps>
|
|
@@ -130,7 +130,7 @@ export const testWorkflow = createWorkflow({
|
|
|
130
130
|
.commit();
|
|
131
131
|
```
|
|
132
132
|
|
|
133
|
-
|
|
133
|
+
## Register workflow
|
|
134
134
|
|
|
135
135
|
Register a workflow using `workflows` in the main Mastra instance:
|
|
136
136
|
|
|
@@ -154,20 +154,24 @@ export const mastra = new Mastra({
|
|
|
154
154
|
});
|
|
155
155
|
```
|
|
156
156
|
|
|
157
|
-
|
|
157
|
+
## Testing workflows locally
|
|
158
158
|
There are two ways to run and test workflows.
|
|
159
159
|
|
|
160
160
|
<Steps>
|
|
161
161
|
|
|
162
|
-
|
|
162
|
+
### Mastra Playground
|
|
163
163
|
|
|
164
164
|
With the Mastra Dev Server running you can run the workflow from the Mastra Playground by visiting [http://localhost:4111/workflows](http://localhost:4111/workflows) in your browser.
|
|
165
165
|
|
|
166
|
-
|
|
166
|
+
> For more information, see the [Local Dev Playground](/docs/server-db/local-dev-playground) documentation.
|
|
167
167
|
|
|
168
|
-
|
|
168
|
+
### Command line
|
|
169
|
+
|
|
170
|
+
Create a workflow run instance using `createRunAsync` and `start`:
|
|
169
171
|
|
|
170
172
|
```typescript {3,5} filename="src/test-workflow.ts" showLineNumbers copy
|
|
173
|
+
import "dotenv/config";
|
|
174
|
+
|
|
171
175
|
import { mastra } from "./mastra";
|
|
172
176
|
|
|
173
177
|
const run = await mastra.getWorkflow("testWorkflow").createRunAsync();
|
|
@@ -178,15 +182,13 @@ const result = await run.start({
|
|
|
178
182
|
}
|
|
179
183
|
});
|
|
180
184
|
|
|
181
|
-
|
|
182
|
-
console.log(JSON.stringify(result, null, 2));
|
|
185
|
+
console.log(result);
|
|
183
186
|
|
|
184
|
-
// Get the workflow output value
|
|
185
187
|
if (result.status === 'success') {
|
|
186
|
-
console.log(
|
|
188
|
+
console.log(result.result.output);
|
|
187
189
|
}
|
|
188
190
|
```
|
|
189
|
-
> see [createRunAsync](
|
|
191
|
+
> see [createRunAsync](../../reference/workflows/create-run.mdx) and [start](../../reference/workflows/run-methods/start.mdx) for more information.
|
|
190
192
|
|
|
191
193
|
To trigger this workflow, run the following:
|
|
192
194
|
|
|
@@ -196,11 +198,11 @@ npx tsx src/test-workflow.ts
|
|
|
196
198
|
|
|
197
199
|
</Steps>
|
|
198
200
|
|
|
199
|
-
|
|
201
|
+
### Run workflow results
|
|
200
202
|
|
|
201
203
|
The result of running a workflow using either `start()` or `resume()` will look like one of the following, depending on the outcome.
|
|
202
204
|
|
|
203
|
-
|
|
205
|
+
#### Status success
|
|
204
206
|
|
|
205
207
|
```json
|
|
206
208
|
{
|
|
@@ -224,7 +226,7 @@ The result of running a workflow using either `start()` or `resume()` will look
|
|
|
224
226
|
- **result**: Includes the final output of the workflow, typed according to the `outputSchema`
|
|
225
227
|
|
|
226
228
|
|
|
227
|
-
|
|
229
|
+
#### Status suspended
|
|
228
230
|
|
|
229
231
|
```json
|
|
230
232
|
{
|
|
@@ -246,7 +248,7 @@ The result of running a workflow using either `start()` or `resume()` will look
|
|
|
246
248
|
|
|
247
249
|
- **suspended**: An optional array listing any steps currently awaiting input before continuing
|
|
248
250
|
|
|
249
|
-
|
|
251
|
+
#### Status failed
|
|
250
252
|
|
|
251
253
|
```json
|
|
252
254
|
{
|
|
@@ -264,7 +266,7 @@ The result of running a workflow using either `start()` or `resume()` will look
|
|
|
264
266
|
```
|
|
265
267
|
- **error**: An optional field that includes the error message if the workflow fails
|
|
266
268
|
|
|
267
|
-
|
|
269
|
+
## Stream workflow
|
|
268
270
|
|
|
269
271
|
Similar to the run method shown above, workflows can also be streamed:
|
|
270
272
|
|
|
@@ -286,7 +288,7 @@ for await (const chunk of result.stream) {
|
|
|
286
288
|
|
|
287
289
|
> See [stream](/reference/workflows/run-methods/stream) and [messages](/reference/workflows/run-methods/stream#messages) for more information.
|
|
288
290
|
|
|
289
|
-
|
|
291
|
+
## Watch Workflow
|
|
290
292
|
|
|
291
293
|
A workflow can also be watched, allowing you to inspect each event that is emitted.
|
|
292
294
|
|
|
@@ -308,7 +310,7 @@ const result = await run.start({
|
|
|
308
310
|
|
|
309
311
|
> See [watch](/reference/workflows/run-methods/watch) for more information.
|
|
310
312
|
|
|
311
|
-
##
|
|
313
|
+
## Related
|
|
312
314
|
|
|
313
315
|
- The [Workflow Guide](../../guides/guide/ai-recruiter.mdx) in the Guides section is a tutorial that covers the main concepts.
|
|
314
316
|
- [Parallel Steps workflow example](../../examples/workflows/parallel-steps.mdx)
|
package/dist/stdio.js
CHANGED
|
@@ -798,7 +798,11 @@ async function listDirContents(dirPath) {
|
|
|
798
798
|
}
|
|
799
799
|
}
|
|
800
800
|
async function readMdxContent(docPath, queryKeywords) {
|
|
801
|
-
const fullPath = path3__default.join(docsBaseDir, docPath);
|
|
801
|
+
const fullPath = path3__default.resolve(path3__default.join(docsBaseDir, docPath));
|
|
802
|
+
if (!fullPath.startsWith(path3__default.resolve(docsBaseDir))) {
|
|
803
|
+
void logger.error(`Path traversal attempt detected`);
|
|
804
|
+
return { found: false };
|
|
805
|
+
}
|
|
802
806
|
void logger.debug(`Reading MDX content from: ${fullPath}`);
|
|
803
807
|
try {
|
|
804
808
|
const stats = await fs3.stat(fullPath);
|
package/dist/tools/docs.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"docs.d.ts","sourceRoot":"","sources":["../../src/tools/docs.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"docs.d.ts","sourceRoot":"","sources":["../../src/tools/docs.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAgKxB,eAAO,MAAM,eAAe;;;;;;;;;EAW1B,CAAC;AAEH,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,eAAe,CAAC,CAAC;AAExD,eAAO,MAAM,QAAQ;;;;;;;;;;;;;oBAkBG,SAAS;CAiDhC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/mcp-docs-server",
|
|
3
|
-
"version": "0.13.17-alpha.
|
|
3
|
+
"version": "0.13.17-alpha.4",
|
|
4
4
|
"description": "MCP server for accessing Mastra.ai documentation, changelogs, and news.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -30,10 +30,10 @@
|
|
|
30
30
|
"jsdom": "^26.1.0",
|
|
31
31
|
"turndown": "^7.2.0",
|
|
32
32
|
"uuid": "^11.1.0",
|
|
33
|
-
"zod": "^3.25.
|
|
34
|
-
"zod-to-json-schema": "^3.24.
|
|
35
|
-
"@mastra/
|
|
36
|
-
"@mastra/
|
|
33
|
+
"zod": "^3.25.76",
|
|
34
|
+
"zod-to-json-schema": "^3.24.6",
|
|
35
|
+
"@mastra/core": "0.15.3-alpha.4",
|
|
36
|
+
"@mastra/mcp": "^0.11.3-alpha.1"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@hono/node-server": "^1.17.1",
|
|
@@ -48,8 +48,8 @@
|
|
|
48
48
|
"tsx": "^4.19.4",
|
|
49
49
|
"typescript": "^5.8.3",
|
|
50
50
|
"vitest": "^3.2.4",
|
|
51
|
-
"@
|
|
52
|
-
"@
|
|
51
|
+
"@mastra/core": "0.15.3-alpha.4",
|
|
52
|
+
"@internal/lint": "0.0.34"
|
|
53
53
|
},
|
|
54
54
|
"scripts": {
|
|
55
55
|
"prepare-docs": "cross-env PREPARE=true node dist/prepare-docs/prepare.js",
|