@mastra/mcp-docs-server 1.0.0-beta.7 → 1.0.0-beta.9
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/%40internal%2Fchangeset-cli.md +1 -15
- package/.docs/organized/changelogs/%40internal%2Fexternal-types.md +1 -7
- package/.docs/organized/changelogs/%40internal%2Ftypes-builder.md +1 -55
- package/.docs/organized/changelogs/%40mastra%2Fagent-builder.md +12 -12
- package/.docs/organized/changelogs/%40mastra%2Fai-sdk.md +48 -48
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +18 -18
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Fcodemod.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Fconvex.md +16 -0
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +78 -78
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +18 -18
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +23 -23
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Flance.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +65 -65
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +29 -29
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +12 -12
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Fmssql.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +69 -69
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +31 -31
- package/.docs/organized/changelogs/%40mastra%2Freact.md +14 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +56 -56
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +17 -17
- package/.docs/organized/changelogs/create-mastra.md +13 -13
- package/.docs/organized/changelogs/mastra.md +21 -21
- package/.docs/organized/code-examples/mcp-server-adapters.md +1 -2
- package/.docs/organized/code-examples/processors-with-ai-sdk.md +14 -0
- package/.docs/organized/code-examples/server-app-access.md +1 -1
- package/.docs/organized/code-examples/server-hono-adapter.md +1 -1
- package/.docs/raw/getting-started/studio.mdx +4 -2
- package/.docs/raw/guides/agent-frameworks/ai-sdk.mdx +161 -0
- package/.docs/raw/guides/build-your-ui/ai-sdk-ui.mdx +381 -431
- package/.docs/raw/guides/getting-started/quickstart.mdx +11 -0
- package/.docs/raw/guides/migrations/upgrade-to-v1/tools.mdx +3 -3
- package/.docs/raw/guides/migrations/upgrade-to-v1/workflows.mdx +31 -0
- package/.docs/raw/reference/ai-sdk/chat-route.mdx +127 -0
- package/.docs/raw/reference/ai-sdk/handle-chat-stream.mdx +117 -0
- package/.docs/raw/reference/ai-sdk/handle-network-stream.mdx +64 -0
- package/.docs/raw/reference/ai-sdk/handle-workflow-stream.mdx +116 -0
- package/.docs/raw/reference/ai-sdk/network-route.mdx +99 -0
- package/.docs/raw/reference/ai-sdk/to-ai-sdk-stream.mdx +289 -0
- package/.docs/raw/reference/ai-sdk/workflow-route.mdx +110 -0
- package/.docs/raw/reference/client-js/agents.mdx +251 -67
- package/.docs/raw/reference/client-js/mastra-client.mdx +2 -2
- package/.docs/raw/reference/client-js/memory.mdx +4 -1
- package/.docs/raw/reference/core/getMemory.mdx +73 -0
- package/.docs/raw/reference/core/getStoredAgentById.mdx +183 -0
- package/.docs/raw/reference/core/listMemory.mdx +70 -0
- package/.docs/raw/reference/core/listStoredAgents.mdx +151 -0
- package/.docs/raw/reference/core/mastra-class.mdx +8 -0
- package/.docs/raw/reference/server/express-adapter.mdx +52 -0
- package/.docs/raw/reference/server/hono-adapter.mdx +54 -0
- package/.docs/raw/server-db/custom-api-routes.mdx +5 -5
- package/.docs/raw/server-db/server-adapters.mdx +94 -91
- package/.docs/raw/streaming/tool-streaming.mdx +10 -14
- package/.docs/raw/workflows/workflow-state.mdx +4 -5
- package/CHANGELOG.md +15 -0
- package/package.json +4 -4
|
@@ -53,6 +53,17 @@ yarn create mastra@beta
|
|
|
53
53
|
bun create mastra@beta
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
+
:::warning
|
|
57
|
+
|
|
58
|
+
Due to a [known issue with Bun](https://github.com/oven-sh/bun/issues/25314) you'll need to run these steps after creating the project:
|
|
59
|
+
|
|
60
|
+
- `bun add @mastra/server@beta`
|
|
61
|
+
- Delete your `node_modules` folder
|
|
62
|
+
- Delete your `bun.lock` file
|
|
63
|
+
- Run `bun install` to reinstall your packages
|
|
64
|
+
|
|
65
|
+
:::
|
|
66
|
+
|
|
56
67
|
</TabItem>
|
|
57
68
|
</Tabs>
|
|
58
69
|
|
|
@@ -11,11 +11,11 @@ Tool execution signatures have been updated to use separate input and context pa
|
|
|
11
11
|
|
|
12
12
|
### `createTool` execute signature to `(inputData, context)` format
|
|
13
13
|
|
|
14
|
-
All
|
|
14
|
+
All `createTool` execute functions now use a new signature with separate `inputData` and `context` parameters instead of a single destructured object. This change provides clearer separation between tool inputs and execution context.
|
|
15
15
|
|
|
16
16
|
**Note:** This change only applies to `createTool`. If you're using `createStep` for workflows, the signature remains `async (inputData, context)` and does not need to be changed.
|
|
17
17
|
|
|
18
|
-
To migrate, update
|
|
18
|
+
To migrate, update `createTool` signatures to use `inputData` as the first parameter (typed from `inputSchema`) and `context` as the second parameter.
|
|
19
19
|
|
|
20
20
|
```diff
|
|
21
21
|
createTool({
|
|
@@ -35,7 +35,7 @@ To migrate, update **`createTool`** signatures to use `inputData` as the first p
|
|
|
35
35
|
|
|
36
36
|
### `createTool` context properties organization
|
|
37
37
|
|
|
38
|
-
Context properties in
|
|
38
|
+
Context properties in `createTool` are now organized into namespaces. Agent-specific properties are under `context.agent`, workflow-specific properties are under `context.workflow`, and MCP-specific properties are under `context.mcp`. This change provides better organization and clearer API surface.
|
|
39
39
|
|
|
40
40
|
For tools that are executed inside an agent, access agent-specific properties through `context.agent`.
|
|
41
41
|
|
|
@@ -177,6 +177,21 @@ To migrate, update any code that consumes branch outputs to handle optional valu
|
|
|
177
177
|
|
|
178
178
|
If your code depends on non-optional types, add runtime checks or provide default values when accessing branch outputs.
|
|
179
179
|
|
|
180
|
+
### `setState` is now async and the data passed is validated
|
|
181
|
+
|
|
182
|
+
The `setState` function is now async. The data passed is now validated against the `stateSchema` defined in the step.
|
|
183
|
+
The state data validation also uses the `validateInputs` flag to determine whether to validate the state data or not.
|
|
184
|
+
Also, when calling `setState`, you can now pass only the state data being updated, instead of adding the previous state spread (`...state`).
|
|
185
|
+
|
|
186
|
+
To migrate, update the `setState` function to be async.
|
|
187
|
+
|
|
188
|
+
```diff
|
|
189
|
+
- setState({ ...state, sharedCounter: state.sharedCounter + 1 });
|
|
190
|
+
+ await setState({ sharedCounter: state.sharedCounter + 1 });
|
|
191
|
+
+ // await setState({ ...state, sharedCounter: state.sharedCounter + 1 });
|
|
192
|
+
+ // this also works, as the previous state spread remains supported
|
|
193
|
+
```
|
|
194
|
+
|
|
180
195
|
## Removed
|
|
181
196
|
|
|
182
197
|
### `streamVNext`, `resumeStreamVNext`, and `observeStreamVNext` methods
|
|
@@ -197,6 +212,22 @@ npx @mastra/codemod@beta v1/workflow-stream-vnext .
|
|
|
197
212
|
|
|
198
213
|
:::
|
|
199
214
|
|
|
215
|
+
### `suspend` and `setState` are not available in step condition functions parameters
|
|
216
|
+
|
|
217
|
+
The `suspend` and `setState` functions are not available in step condition functions parameters.
|
|
218
|
+
|
|
219
|
+
To migrate, use the `suspend` function in the step execute function instead.
|
|
220
|
+
|
|
221
|
+
```diff
|
|
222
|
+
.dowhile(step, async ({ suspend, state, setState }) => {
|
|
223
|
+
- setState({...state, updatedState: "updated state"})
|
|
224
|
+
- await suspend({ reason: "Suspension reason" });
|
|
225
|
+
+ // Use the suspend/setState in the step execute function instead
|
|
226
|
+
});
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
This is the same for `dountil` and `branch` condition functions parameters.
|
|
230
|
+
|
|
200
231
|
### Legacy workflows export
|
|
201
232
|
|
|
202
233
|
The `./workflows/legacy` export path has been removed from `@mastra/core`. Legacy workflows are no longer supported.
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Reference: chatRoute() | AI SDK"
|
|
3
|
+
description: API reference for chatRoute(), a function to create chat route handlers for streaming agent conversations in AI SDK-compatible format.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import PropertiesTable from "@site/src/components/PropertiesTable";
|
|
7
|
+
|
|
8
|
+
# chatRoute()
|
|
9
|
+
|
|
10
|
+
Creates a chat route handler for streaming agent conversations using the AI SDK format. This function registers an HTTP `POST` endpoint that accepts messages, executes an agent, and streams the response back to the client in AI SDK-compatible format. You have to use it inside a [custom API route](/docs/v1/server-db/custom-api-routes).
|
|
11
|
+
|
|
12
|
+
Use [`handleChatStream()`](/reference/v1/ai-sdk/handle-chat-stream) if you need a framework-agnostic handler.
|
|
13
|
+
|
|
14
|
+
## Usage example
|
|
15
|
+
|
|
16
|
+
This example shows how to set up a chat route at the `/chat` endpoint that uses an agent with the ID `weatherAgent`.
|
|
17
|
+
|
|
18
|
+
```typescript title="src/mastra/index.ts" copy
|
|
19
|
+
import { Mastra } from "@mastra/core";
|
|
20
|
+
import { chatRoute } from "@mastra/ai-sdk";
|
|
21
|
+
|
|
22
|
+
export const mastra = new Mastra({
|
|
23
|
+
server: {
|
|
24
|
+
apiRoutes: [
|
|
25
|
+
chatRoute({
|
|
26
|
+
path: "/chat",
|
|
27
|
+
agent: "weatherAgent",
|
|
28
|
+
}),
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
You can also use dynamic agent routing based on an `agentId`. The URL `/chat/weatherAgent` will resolve to the agent with the ID `weatherAgent`.
|
|
35
|
+
|
|
36
|
+
```typescript title="src/mastra/index.ts" copy
|
|
37
|
+
import { Mastra } from "@mastra/core";
|
|
38
|
+
import { chatRoute } from "@mastra/ai-sdk";
|
|
39
|
+
|
|
40
|
+
export const mastra = new Mastra({
|
|
41
|
+
server: {
|
|
42
|
+
apiRoutes: [
|
|
43
|
+
chatRoute({
|
|
44
|
+
path: "/chat/:agentId",
|
|
45
|
+
}),
|
|
46
|
+
],
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Parameters
|
|
52
|
+
|
|
53
|
+
<PropertiesTable
|
|
54
|
+
content={[
|
|
55
|
+
{
|
|
56
|
+
name: "path",
|
|
57
|
+
type: "string",
|
|
58
|
+
description: "The route path (e.g., `/chat` or `/chat/:agentId`). Include `:agentId` for dynamic agent routing.",
|
|
59
|
+
isOptional: false,
|
|
60
|
+
defaultValue: "'/chat/:agentId'",
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name: "agent",
|
|
64
|
+
type: "string",
|
|
65
|
+
description: "The ID of the agent to use for this chat route. Required if the path doesn't include `:agentId`.",
|
|
66
|
+
isOptional: true,
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
name: "defaultOptions",
|
|
70
|
+
type: "AgentExecutionOptions",
|
|
71
|
+
description: "Default options passed to agent execution. These can include instructions, memory configuration, maxSteps, and other execution settings.",
|
|
72
|
+
isOptional: true,
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
name: "sendStart",
|
|
76
|
+
type: "boolean",
|
|
77
|
+
description: "Whether to send start events in the stream.",
|
|
78
|
+
isOptional: true,
|
|
79
|
+
defaultValue: "true",
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
name: "sendFinish",
|
|
83
|
+
type: "boolean",
|
|
84
|
+
description: "Whether to send finish events in the stream.",
|
|
85
|
+
isOptional: true,
|
|
86
|
+
defaultValue: "true",
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: "sendReasoning",
|
|
90
|
+
type: "boolean",
|
|
91
|
+
description: "Whether to include reasoning steps in the stream.",
|
|
92
|
+
isOptional: true,
|
|
93
|
+
defaultValue: "false",
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
name: "sendSources",
|
|
97
|
+
type: "boolean",
|
|
98
|
+
description: "Whether to include source citations in the stream.",
|
|
99
|
+
isOptional: true,
|
|
100
|
+
defaultValue: "false",
|
|
101
|
+
},
|
|
102
|
+
]}
|
|
103
|
+
/>
|
|
104
|
+
|
|
105
|
+
## Additional configuration
|
|
106
|
+
|
|
107
|
+
You can use [`prepareSendMessagesRequest`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-chat#transport.default-chat-transport.prepare-send-messages-request) to customize the request sent to the chat route, for example to pass additional configuration to the agent:
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
const { error, status, sendMessage, messages, regenerate, stop } = useChat({
|
|
111
|
+
transport: new DefaultChatTransport({
|
|
112
|
+
api: "http://localhost:4111/chat",
|
|
113
|
+
prepareSendMessagesRequest({ messages }) {
|
|
114
|
+
return {
|
|
115
|
+
body: {
|
|
116
|
+
messages,
|
|
117
|
+
// Pass memory config
|
|
118
|
+
memory: {
|
|
119
|
+
thread: "user-1",
|
|
120
|
+
resource: "user-1",
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
},
|
|
125
|
+
}),
|
|
126
|
+
});
|
|
127
|
+
```
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Reference: handleChatStream() | AI SDK"
|
|
3
|
+
description: API reference for handleChatStream(), a framework-agnostic handler for streaming agent chat in AI SDK-compatible format.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import PropertiesTable from "@site/src/components/PropertiesTable";
|
|
7
|
+
|
|
8
|
+
# handleChatStream()
|
|
9
|
+
|
|
10
|
+
Framework-agnostic handler for streaming agent chat in AI SDK-compatible format. Use this function directly when you need to handle chat streaming outside Hono or Mastra's own [apiRoutes](/docs/v1/server-db/custom-api-routes) feature.
|
|
11
|
+
|
|
12
|
+
`handleChatStream()` returns a `ReadableStream` that you can wrap with [`createUIMessageStreamResponse()`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/create-ui-message-stream-response).
|
|
13
|
+
|
|
14
|
+
Use [`chatRoute()`](/reference/v1/ai-sdk/chat-route) if you want to create a chat route inside a Mastra server.
|
|
15
|
+
|
|
16
|
+
## Usage example
|
|
17
|
+
|
|
18
|
+
Next.js App Router example:
|
|
19
|
+
|
|
20
|
+
```typescript title="app/api/chat/route.ts" copy
|
|
21
|
+
import { handleChatStream } from '@mastra/ai-sdk';
|
|
22
|
+
import { createUIMessageStreamResponse } from 'ai';
|
|
23
|
+
import { mastra } from '@/src/mastra';
|
|
24
|
+
|
|
25
|
+
export async function POST(req: Request) {
|
|
26
|
+
const params = await req.json();
|
|
27
|
+
const stream = await handleChatStream({
|
|
28
|
+
mastra,
|
|
29
|
+
agentId: 'weatherAgent',
|
|
30
|
+
params,
|
|
31
|
+
});
|
|
32
|
+
return createUIMessageStreamResponse({ stream });
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Parameters
|
|
37
|
+
|
|
38
|
+
<PropertiesTable
|
|
39
|
+
content={[
|
|
40
|
+
{
|
|
41
|
+
name: "mastra",
|
|
42
|
+
type: "Mastra",
|
|
43
|
+
description: "The Mastra instance containing registered agents.",
|
|
44
|
+
isOptional: false,
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: "agentId",
|
|
48
|
+
type: "string",
|
|
49
|
+
description: "The ID of the agent to use for chat.",
|
|
50
|
+
isOptional: false,
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
name: "params",
|
|
54
|
+
type: "ChatStreamHandlerParams",
|
|
55
|
+
description: "Parameters for the chat stream, including messages and optional resume data.",
|
|
56
|
+
isOptional: false,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: "params.messages",
|
|
60
|
+
type: "UIMessage[]",
|
|
61
|
+
description: "Array of messages in the conversation.",
|
|
62
|
+
isOptional: false,
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
name: "params.resumeData",
|
|
66
|
+
type: "Record<string, any>",
|
|
67
|
+
description: "Data for resuming a suspended agent execution. Requires `runId` to be set.",
|
|
68
|
+
isOptional: true,
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: "params.runId",
|
|
72
|
+
type: "string",
|
|
73
|
+
description: "The run ID. Required when `resumeData` is provided.",
|
|
74
|
+
isOptional: true,
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
name: "params.requestContext",
|
|
78
|
+
type: "RequestContext",
|
|
79
|
+
description: "Request context to pass to the agent execution.",
|
|
80
|
+
isOptional: true,
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
name: "defaultOptions",
|
|
84
|
+
type: "AgentExecutionOptions",
|
|
85
|
+
description: "Default options passed to agent execution. These are merged with params, with params taking precedence.",
|
|
86
|
+
isOptional: true,
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: "sendStart",
|
|
90
|
+
type: "boolean",
|
|
91
|
+
description: "Whether to send start events in the stream.",
|
|
92
|
+
isOptional: true,
|
|
93
|
+
defaultValue: "true",
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
name: "sendFinish",
|
|
97
|
+
type: "boolean",
|
|
98
|
+
description: "Whether to send finish events in the stream.",
|
|
99
|
+
isOptional: true,
|
|
100
|
+
defaultValue: "true",
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
name: "sendReasoning",
|
|
104
|
+
type: "boolean",
|
|
105
|
+
description: "Whether to include reasoning steps in the stream.",
|
|
106
|
+
isOptional: true,
|
|
107
|
+
defaultValue: "false",
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
name: "sendSources",
|
|
111
|
+
type: "boolean",
|
|
112
|
+
description: "Whether to include source citations in the stream.",
|
|
113
|
+
isOptional: true,
|
|
114
|
+
defaultValue: "false",
|
|
115
|
+
},
|
|
116
|
+
]}
|
|
117
|
+
/>
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Reference: handleNetworkStream() | AI SDK"
|
|
3
|
+
description: API reference for handleNetworkStream(), a framework-agnostic handler for streaming network execution in AI SDK-compatible format.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import PropertiesTable from "@site/src/components/PropertiesTable";
|
|
7
|
+
|
|
8
|
+
# handleNetworkStream()
|
|
9
|
+
|
|
10
|
+
Framework-agnostic handler for streaming network execution in AI SDK-compatible format. Use this function directly when you need to handle network streaming outside Hono or Mastra's own [apiRoutes](/docs/v1/server-db/custom-api-routes) feature.
|
|
11
|
+
|
|
12
|
+
`handleNetworkStream()` returns a `ReadableStream` that you can wrap with [`createUIMessageStreamResponse()`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/create-ui-message-stream-response).
|
|
13
|
+
|
|
14
|
+
Use [`networkRoute()`](/reference/v1/ai-sdk/network-route) if you want to create a network route inside a Mastra server.
|
|
15
|
+
## Usage example
|
|
16
|
+
|
|
17
|
+
Next.js App Router example:
|
|
18
|
+
|
|
19
|
+
```typescript title="app/api/network/route.ts" copy
|
|
20
|
+
import { handleNetworkStream } from '@mastra/ai-sdk';
|
|
21
|
+
import { createUIMessageStreamResponse } from 'ai';
|
|
22
|
+
import { mastra } from '@/src/mastra';
|
|
23
|
+
|
|
24
|
+
export async function POST(req: Request) {
|
|
25
|
+
const params = await req.json();
|
|
26
|
+
const stream = await handleNetworkStream({
|
|
27
|
+
mastra,
|
|
28
|
+
agentId: 'routingAgent',
|
|
29
|
+
params,
|
|
30
|
+
});
|
|
31
|
+
return createUIMessageStreamResponse({ stream });
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Parameters
|
|
36
|
+
|
|
37
|
+
<PropertiesTable
|
|
38
|
+
content={[
|
|
39
|
+
{
|
|
40
|
+
name: "mastra",
|
|
41
|
+
type: "Mastra",
|
|
42
|
+
description: "The Mastra instance to use for agent lookup and execution.",
|
|
43
|
+
isOptional: false,
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
name: "agentId",
|
|
47
|
+
type: "string",
|
|
48
|
+
description: "The ID of the routing agent to execute as a network.",
|
|
49
|
+
isOptional: false,
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
name: "params",
|
|
53
|
+
type: "NetworkStreamHandlerParams",
|
|
54
|
+
description: "The request parameters containing messages and execution options. Includes `messages` (required) and any AgentExecutionOptions like `memory`, `maxSteps`, `runId`, etc.",
|
|
55
|
+
isOptional: false,
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: "defaultOptions",
|
|
59
|
+
type: "AgentExecutionOptions",
|
|
60
|
+
description: "Default options passed to agent execution. These are merged with params, with params taking precedence.",
|
|
61
|
+
isOptional: true,
|
|
62
|
+
},
|
|
63
|
+
]}
|
|
64
|
+
/>
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Reference: handleWorkflowStream() | AI SDK"
|
|
3
|
+
description: API reference for handleWorkflowStream(), a framework-agnostic handler for streaming workflow execution in AI SDK-compatible format.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import PropertiesTable from "@site/src/components/PropertiesTable";
|
|
7
|
+
|
|
8
|
+
# handleWorkflowStream()
|
|
9
|
+
|
|
10
|
+
Framework-agnostic handler for streaming workflow execution in AI SDK-compatible format. Use this function directly when you need to handle workflow streaming outside Hono or Mastra's own [apiRoutes](/docs/v1/server-db/custom-api-routes) feature.
|
|
11
|
+
|
|
12
|
+
`handleWorkflowStream()` returns a `ReadableStream` that you can wrap with [`createUIMessageStreamResponse()`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/create-ui-message-stream-response).
|
|
13
|
+
|
|
14
|
+
Use [`workflowRoute()`](/reference/v1/ai-sdk/workflow-route) if you want to create a workflow route inside a Mastra server.
|
|
15
|
+
|
|
16
|
+
:::tip Agent streaming in workflows
|
|
17
|
+
|
|
18
|
+
When a workflow step pipes an agent's stream to the workflow writer (e.g., `await response.fullStream.pipeTo(writer)`), the agent's text chunks and tool calls are forwarded to the UI stream in real time, even when the agent runs inside workflow steps.
|
|
19
|
+
|
|
20
|
+
See [Workflow Streaming](/docs/v1/streaming/workflow-streaming#streaming-agent-text-chunks-to-ui) for more details.
|
|
21
|
+
|
|
22
|
+
:::
|
|
23
|
+
|
|
24
|
+
## Usage example
|
|
25
|
+
|
|
26
|
+
Next.js App Router example:
|
|
27
|
+
|
|
28
|
+
```typescript title="app/api/workflow/route.ts" copy
|
|
29
|
+
import { handleWorkflowStream } from '@mastra/ai-sdk';
|
|
30
|
+
import { createUIMessageStreamResponse } from 'ai';
|
|
31
|
+
import { mastra } from '@/src/mastra';
|
|
32
|
+
|
|
33
|
+
export async function POST(req: Request) {
|
|
34
|
+
const params = await req.json();
|
|
35
|
+
const stream = await handleWorkflowStream({
|
|
36
|
+
mastra,
|
|
37
|
+
workflowId: 'weatherWorkflow',
|
|
38
|
+
params,
|
|
39
|
+
});
|
|
40
|
+
return createUIMessageStreamResponse({ stream });
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Parameters
|
|
45
|
+
|
|
46
|
+
<PropertiesTable
|
|
47
|
+
content={[
|
|
48
|
+
{
|
|
49
|
+
name: "mastra",
|
|
50
|
+
type: "Mastra",
|
|
51
|
+
description: "The Mastra instance containing registered workflows.",
|
|
52
|
+
isOptional: false,
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: "workflowId",
|
|
56
|
+
type: "string",
|
|
57
|
+
description: "The ID of the workflow to execute.",
|
|
58
|
+
isOptional: false,
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
name: "params",
|
|
62
|
+
type: "WorkflowStreamHandlerParams",
|
|
63
|
+
description: "Parameters for the workflow stream.",
|
|
64
|
+
isOptional: false,
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
name: "params.runId",
|
|
68
|
+
type: "string",
|
|
69
|
+
description: "Optional run ID for the workflow execution.",
|
|
70
|
+
isOptional: true,
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
name: "params.resourceId",
|
|
74
|
+
type: "string",
|
|
75
|
+
description: "Optional resource ID for the workflow run.",
|
|
76
|
+
isOptional: true,
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
name: "params.inputData",
|
|
80
|
+
type: "Record<string, any>",
|
|
81
|
+
description: "Input data for starting a new workflow execution.",
|
|
82
|
+
isOptional: true,
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
name: "params.resumeData",
|
|
86
|
+
type: "Record<string, any>",
|
|
87
|
+
description: "Data for resuming a suspended workflow execution.",
|
|
88
|
+
isOptional: true,
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
name: "params.requestContext",
|
|
92
|
+
type: "RequestContext",
|
|
93
|
+
description: "Request context to pass to the workflow execution.",
|
|
94
|
+
isOptional: true,
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
name: "params.tracingOptions",
|
|
98
|
+
type: "TracingOptions",
|
|
99
|
+
description: "Options for tracing and observability.",
|
|
100
|
+
isOptional: true,
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
name: "params.step",
|
|
104
|
+
type: "string",
|
|
105
|
+
description: "Specific step to target in the workflow.",
|
|
106
|
+
isOptional: true,
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
name: "includeTextStreamParts",
|
|
110
|
+
type: "boolean",
|
|
111
|
+
description: "Whether to include text stream parts in the output.",
|
|
112
|
+
isOptional: true,
|
|
113
|
+
defaultValue: "true",
|
|
114
|
+
},
|
|
115
|
+
]}
|
|
116
|
+
/>
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Reference: networkRoute() | AI SDK"
|
|
3
|
+
description: API reference for networkRoute(), a function to create network route handlers for streaming network execution in AI SDK-compatible format.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import PropertiesTable from "@site/src/components/PropertiesTable";
|
|
7
|
+
|
|
8
|
+
# networkRoute()
|
|
9
|
+
|
|
10
|
+
Creates a network route handler for streaming network execution using the AI SDK format. This function registers an HTTP `POST` endpoint that accepts messages, executes an agent network, and streams the response back to the client in AI SDK-compatible format. Agent networks allow a routing agent to delegate tasks to other agents. You have to use it inside a [custom API route](/docs/v1/server-db/custom-api-routes).
|
|
11
|
+
|
|
12
|
+
Use [`handleNetworkStream()`](/reference/v1/ai-sdk/handle-network-stream) if you need a framework-agnostic handler.
|
|
13
|
+
|
|
14
|
+
## Usage example
|
|
15
|
+
|
|
16
|
+
This example shows how to set up a network route at the `/network` endpoint that uses an agent with the ID `weatherAgent`.
|
|
17
|
+
|
|
18
|
+
```typescript title="src/mastra/index.ts" copy
|
|
19
|
+
import { Mastra } from "@mastra/core";
|
|
20
|
+
import { networkRoute } from "@mastra/ai-sdk";
|
|
21
|
+
|
|
22
|
+
export const mastra = new Mastra({
|
|
23
|
+
server: {
|
|
24
|
+
apiRoutes: [
|
|
25
|
+
networkRoute({
|
|
26
|
+
path: "/network",
|
|
27
|
+
agent: "weatherAgent",
|
|
28
|
+
}),
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
You can also use dynamic agent routing based on an `agentId`. The URL `/network/weatherAgent` will resolve to the agent with the ID `weatherAgent`.
|
|
35
|
+
|
|
36
|
+
```typescript title="src/mastra/index.ts" copy
|
|
37
|
+
import { Mastra } from "@mastra/core";
|
|
38
|
+
import { networkRoute } from "@mastra/ai-sdk";
|
|
39
|
+
|
|
40
|
+
export const mastra = new Mastra({
|
|
41
|
+
server: {
|
|
42
|
+
apiRoutes: [
|
|
43
|
+
networkRoute({
|
|
44
|
+
path: "/network/:agentId",
|
|
45
|
+
}),
|
|
46
|
+
],
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Parameters
|
|
52
|
+
|
|
53
|
+
<PropertiesTable
|
|
54
|
+
content={[
|
|
55
|
+
{
|
|
56
|
+
name: "path",
|
|
57
|
+
type: "string",
|
|
58
|
+
description: "The route path (e.g., `/network` or `/network/:agentId`). Include `:agentId` for dynamic agent routing.",
|
|
59
|
+
isOptional: false,
|
|
60
|
+
defaultValue: "'/network/:agentId'",
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name: "agent",
|
|
64
|
+
type: "string",
|
|
65
|
+
description: "The ID of the routing agent to use for this network route. Required if the path doesn't include `:agentId`.",
|
|
66
|
+
isOptional: true,
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
name: "defaultOptions",
|
|
70
|
+
type: "AgentExecutionOptions",
|
|
71
|
+
description: "Default options passed to agent execution. These can include instructions, memory configuration, maxSteps, and other execution settings.",
|
|
72
|
+
isOptional: true,
|
|
73
|
+
},
|
|
74
|
+
]}
|
|
75
|
+
/>
|
|
76
|
+
|
|
77
|
+
## Additional configuration
|
|
78
|
+
|
|
79
|
+
You can use [`prepareSendMessagesRequest`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-chat#transport.default-chat-transport.prepare-send-messages-request) to customize the request sent to the network route, for example to pass additional configuration to the agent:
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
const { error, status, sendMessage, messages, regenerate, stop } = useChat({
|
|
83
|
+
transport: new DefaultChatTransport({
|
|
84
|
+
api: "http://localhost:4111/network",
|
|
85
|
+
prepareSendMessagesRequest({ messages }) {
|
|
86
|
+
return {
|
|
87
|
+
body: {
|
|
88
|
+
messages,
|
|
89
|
+
// Pass memory config
|
|
90
|
+
memory: {
|
|
91
|
+
thread: "user-1",
|
|
92
|
+
resource: "user-1",
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
},
|
|
97
|
+
}),
|
|
98
|
+
});
|
|
99
|
+
```
|