@mastra/mcp-docs-server 0.13.30 → 0.13.31
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 +4 -0
- package/.docs/organized/changelogs/%40internal%2Fstorage-test-utils.md +15 -15
- package/.docs/organized/changelogs/%40internal%2Ftypes-builder.md +4 -0
- package/.docs/organized/changelogs/%40mastra%2Fagent-builder.md +22 -22
- package/.docs/organized/changelogs/%40mastra%2Fai-sdk.md +48 -6
- package/.docs/organized/changelogs/%40mastra%2Fastra.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fchroma.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +28 -28
- package/.docs/organized/changelogs/%40mastra%2Fcloud.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +120 -120
- package/.docs/organized/changelogs/%40mastra%2Fcouchbase.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +37 -37
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +41 -41
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +41 -41
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-vercel.md +41 -41
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +65 -65
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fevals.md +33 -33
- package/.docs/organized/changelogs/%40mastra%2Flance.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +23 -23
- package/.docs/organized/changelogs/%40mastra%2Fmcp-registry-registry.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +30 -30
- package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fmssql.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fopensearch.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +41 -41
- package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +71 -71
- package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Frag.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Freact.md +41 -0
- package/.docs/organized/changelogs/%40mastra%2Fs3vectors.md +15 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +37 -37
- package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fvoice-azure.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Fvoice-cloudflare.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fvoice-deepgram.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fvoice-elevenlabs.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fvoice-gladia.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google-gemini-live.md +15 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fvoice-murf.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai-realtime.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Fvoice-playai.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fvoice-sarvam.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fvoice-speechify.md +16 -16
- package/.docs/organized/changelogs/create-mastra.md +41 -41
- package/.docs/organized/changelogs/mastra.md +73 -73
- package/.docs/organized/code-examples/heads-up-game.md +5 -5
- package/.docs/raw/agents/agent-memory.mdx +1 -1
- package/.docs/raw/agents/networks.mdx +0 -2
- package/.docs/raw/agents/overview.mdx +71 -38
- package/.docs/raw/agents/using-tools.mdx +1 -1
- package/.docs/raw/frameworks/agentic-uis/ai-sdk.mdx +284 -266
- package/.docs/raw/reference/agents/generate.mdx +13 -5
- package/.docs/raw/reference/agents/getDefaultGenerateOptions.mdx +1 -1
- package/.docs/raw/reference/agents/getDefaultStreamOptions.mdx +2 -2
- package/.docs/raw/reference/agents/getInstructions.mdx +1 -1
- package/.docs/raw/reference/agents/getLLM.mdx +1 -1
- package/.docs/raw/reference/agents/getMemory.mdx +1 -1
- package/.docs/raw/reference/agents/getModel.mdx +1 -1
- package/.docs/raw/reference/agents/getScorers.mdx +1 -1
- package/.docs/raw/reference/cli/create-mastra.mdx +16 -0
- package/.docs/raw/reference/cli/mastra.mdx +7 -1
- package/.docs/raw/reference/streaming/ChunkType.mdx +1 -1
- package/.docs/raw/reference/streaming/agents/MastraModelOutput.mdx +1 -2
- package/.docs/raw/reference/streaming/agents/stream.mdx +18 -10
- package/.docs/raw/reference/streaming/agents/streamLegacy.mdx +2 -2
- package/.docs/raw/reference/tools/create-tool.mdx +1 -1
- package/.docs/raw/reference/tools/graph-rag-tool.mdx +2 -2
- package/.docs/raw/reference/tools/vector-query-tool.mdx +2 -2
- package/.docs/raw/reference/vectors/pg.mdx +83 -2
- package/.docs/raw/server-db/runtime-context.mdx +4 -2
- package/.docs/raw/streaming/events.mdx +108 -0
- package/.docs/raw/streaming/overview.mdx +41 -1
- package/.docs/raw/tools-mcp/overview.mdx +2 -38
- package/.docs/raw/workflows/overview.mdx +137 -229
- package/CHANGELOG.md +14 -0
- package/package.json +4 -4
|
@@ -9,213 +9,396 @@ import { Callout, Tabs } from "nextra/components";
|
|
|
9
9
|
|
|
10
10
|
Mastra integrates with [Vercel's AI SDK](https://sdk.vercel.ai) to support model routing, React Hooks, and data streaming methods.
|
|
11
11
|
|
|
12
|
-
##
|
|
13
|
-
|
|
14
|
-
Mastra also supports AI SDK v5 see the following section for v5 specific methods: [Vercel AI SDK v5](/docs/frameworks/agentic-uis/ai-sdk#vercel-ai-sdk-v5)
|
|
15
|
-
|
|
16
|
-
<Callout type="warning">
|
|
17
|
-
The code examples contained with this page assume you're using the Next.js App Router at the root of your
|
|
18
|
-
project, e.g., `app` rather than `src/app`.
|
|
19
|
-
</Callout>
|
|
20
|
-
|
|
21
|
-
## Model routing
|
|
12
|
+
## Model Routing
|
|
22
13
|
|
|
23
14
|
When creating agents in Mastra, you can specify any AI SDK-supported model.
|
|
24
15
|
|
|
25
|
-
```typescript {
|
|
26
|
-
import { openai } from "@ai-sdk/openai";
|
|
16
|
+
```typescript {6} filename="agents/weather-agent.ts" copy
|
|
27
17
|
import { Agent } from "@mastra/core/agent";
|
|
28
18
|
|
|
29
19
|
export const weatherAgent = new Agent({
|
|
30
20
|
name: "Weather Agent",
|
|
31
21
|
instructions: "Instructions for the agent...",
|
|
32
|
-
model: openai
|
|
22
|
+
model: "openai/gpt-4-turbo",
|
|
33
23
|
});
|
|
34
24
|
```
|
|
35
25
|
|
|
36
26
|
> See [Model Providers](/models) and [Model Capabilities](/models) for more information.
|
|
37
27
|
|
|
38
|
-
##
|
|
28
|
+
## Streaming
|
|
39
29
|
|
|
40
|
-
Mastra
|
|
41
|
-
|
|
42
|
-
Install the required AI SDK React package:
|
|
30
|
+
The recommended way of using Mastra and AI SDK together is by installing the `@mastra/ai-sdk` package. `@mastra/ai-sdk` provides custom API routes and utilities for streaming Mastra agents in AI SDK-compatible formats. Including chat, workflow, and network route handlers, along with utilities and exported types for UI integrations.
|
|
43
31
|
|
|
44
32
|
<Tabs items={["npm", "yarn", "pnpm", "bun"]}>
|
|
45
33
|
<Tabs.Tab>
|
|
46
34
|
```bash copy
|
|
47
|
-
npm install @ai-sdk
|
|
35
|
+
npm install @mastra/ai-sdk
|
|
48
36
|
```
|
|
49
37
|
</Tabs.Tab>
|
|
50
38
|
<Tabs.Tab>
|
|
51
39
|
```bash copy
|
|
52
|
-
yarn add @ai-sdk
|
|
40
|
+
yarn add @mastra/ai-sdk
|
|
53
41
|
```
|
|
54
42
|
</Tabs.Tab>
|
|
55
43
|
<Tabs.Tab>
|
|
56
44
|
```bash copy
|
|
57
|
-
pnpm add @ai-sdk
|
|
45
|
+
pnpm add @mastra/ai-sdk
|
|
58
46
|
```
|
|
59
47
|
</Tabs.Tab>
|
|
60
48
|
<Tabs.Tab>
|
|
61
49
|
```bash copy
|
|
62
|
-
bun add @ai-sdk
|
|
50
|
+
bun add @mastra/ai-sdk
|
|
63
51
|
```
|
|
64
52
|
</Tabs.Tab>
|
|
65
53
|
</Tabs>
|
|
66
54
|
|
|
55
|
+
### `chatRoute()`
|
|
56
|
+
|
|
57
|
+
When setting up a [custom API route](/docs/server-db/custom-api-routes), use the `chatRoute()` utility to create a route handler that automatically formats the agent stream into an AI SDK-compatible format.
|
|
58
|
+
|
|
59
|
+
```typescript filename="src/mastra/index.ts" copy
|
|
60
|
+
import { Mastra } from '@mastra/core/mastra';
|
|
61
|
+
import { chatRoute } from '@mastra/ai-sdk';
|
|
62
|
+
|
|
63
|
+
export const mastra = new Mastra({
|
|
64
|
+
server: {
|
|
65
|
+
apiRoutes: [
|
|
66
|
+
chatRoute({
|
|
67
|
+
path: '/chat',
|
|
68
|
+
agent: 'weatherAgent',
|
|
69
|
+
}),
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Once you have your `/chat` API route set up, you can call the `useChat()` hook in your application.
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
const { error, status, sendMessage, messages, regenerate, stop } =
|
|
79
|
+
useChat({
|
|
80
|
+
transport: new DefaultChatTransport({
|
|
81
|
+
api: 'http://localhost:4111/chat',
|
|
82
|
+
}),
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
### `workflowRoute()`
|
|
86
|
+
|
|
87
|
+
Use the `workflowRoute()` utility to create a route handler that automatically formats the workflow stream into an AI SDK-compatible format.
|
|
88
|
+
|
|
89
|
+
```typescript filename="src/mastra/index.ts" copy
|
|
90
|
+
import { Mastra } from '@mastra/core/mastra';
|
|
91
|
+
import { workflowRoute } from '@mastra/ai-sdk';
|
|
92
|
+
|
|
93
|
+
export const mastra = new Mastra({
|
|
94
|
+
server: {
|
|
95
|
+
apiRoutes: [
|
|
96
|
+
workflowRoute({
|
|
97
|
+
path: '/workflow',
|
|
98
|
+
agent: 'weatherAgent',
|
|
99
|
+
}),
|
|
100
|
+
],
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Once you have your `/workflow` API route set up, you can call the `useChat()` hook in your application.
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
const { error, status, sendMessage, messages, regenerate, stop } =
|
|
109
|
+
useChat({
|
|
110
|
+
transport: new DefaultChatTransport({
|
|
111
|
+
api: 'http://localhost:4111/workflow',
|
|
112
|
+
prepareSendMessagesRequest({messages}) {
|
|
113
|
+
return {
|
|
114
|
+
body: {
|
|
115
|
+
inputData: {
|
|
116
|
+
city: messages[messages.length - 1].parts[0].text
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}),
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### `networkRoute()`
|
|
126
|
+
|
|
127
|
+
Use the `networkRoute()` utility to create a route handler that automatically formats the agent network stream into an AI SDK-compatible format.
|
|
128
|
+
|
|
129
|
+
```typescript filename="src/mastra/index.ts" copy
|
|
130
|
+
import { Mastra } from '@mastra/core/mastra';
|
|
131
|
+
import { networkRoute } from '@mastra/ai-sdk';
|
|
132
|
+
|
|
133
|
+
export const mastra = new Mastra({
|
|
134
|
+
server: {
|
|
135
|
+
apiRoutes: [
|
|
136
|
+
networkRoute({
|
|
137
|
+
path: '/network',
|
|
138
|
+
agent: 'weatherAgent',
|
|
139
|
+
}),
|
|
140
|
+
],
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Once you have your `/network` API route set up, you can call the `useChat()` hook in your application.
|
|
67
146
|
|
|
68
|
-
|
|
147
|
+
```typescript
|
|
148
|
+
const { error, status, sendMessage, messages, regenerate, stop } =
|
|
149
|
+
useChat({
|
|
150
|
+
transport: new DefaultChatTransport({
|
|
151
|
+
api: 'http://localhost:4111/network',
|
|
152
|
+
}),
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Custom UI
|
|
69
157
|
|
|
70
|
-
The `
|
|
158
|
+
The `@mastra/ai-sdk` package transforms and emits Mastra streams (e.g workflow, network streams) into AI SDK-compatible format.
|
|
71
159
|
|
|
72
|
-
|
|
160
|
+
- **Top-level parts**: These are streamed via direct workflow and network stream transformations (e.g in `workflowRoute()` and `networkRoute()`)
|
|
161
|
+
- `data-workflow`: Aggregates a workflow run with step inputs/outputs and final usage.
|
|
162
|
+
- `data-network`: Aggregates a routing/network run with ordered steps (agent/workflow/tool executions) and outputs.
|
|
163
|
+
|
|
164
|
+
- **Nested parts**: These are streamed via nested and merged streams from within a tool's `execute()` method.
|
|
165
|
+
- `data-tool-workflow`: Nested workflow emitted from within a tool stream.
|
|
166
|
+
- `data-tool-network`: Nested network emitted from within an tool stream.
|
|
167
|
+
- `data-tool-agent`: Nested agent emitted from within an tool stream.
|
|
168
|
+
|
|
169
|
+
Here's an example: For a [nested agent stream within a tool](/docs/streaming/tool-streaming#tool-using-an-agent), `data-tool-agent` UI message parts will be emitted and can be leveraged on the client as documented below:
|
|
170
|
+
|
|
171
|
+
```typescript filename="app/page.tsx" copy
|
|
73
172
|
"use client";
|
|
74
173
|
|
|
75
174
|
import { useChat } from "@ai-sdk/react";
|
|
175
|
+
import { AgentTool } from '../ui/agent-tool';
|
|
176
|
+
import type { AgentDataPart } from "@mastra/ai-sdk";
|
|
76
177
|
|
|
77
|
-
export function
|
|
78
|
-
const { messages
|
|
79
|
-
|
|
178
|
+
export default function Page() {
|
|
179
|
+
const { messages } = useChat({
|
|
180
|
+
transport: new DefaultChatTransport({
|
|
181
|
+
api: 'http://localhost:4111/chat',
|
|
182
|
+
}),
|
|
80
183
|
});
|
|
184
|
+
|
|
81
185
|
return (
|
|
82
186
|
<div>
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
187
|
+
{messages.map((message) => (
|
|
188
|
+
<div key={message.id}>
|
|
189
|
+
{message.parts.map((part, i) => {
|
|
190
|
+
switch (part.type) {
|
|
191
|
+
case 'data-tool-agent':
|
|
192
|
+
return (
|
|
193
|
+
<AgentTool {...part.data as AgentDataPart} key={`${message.id}-${i}`} />
|
|
194
|
+
);
|
|
195
|
+
default:
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
})}
|
|
199
|
+
</div>
|
|
200
|
+
))}
|
|
87
201
|
</div>
|
|
88
202
|
);
|
|
89
203
|
}
|
|
90
204
|
```
|
|
91
205
|
|
|
92
|
-
|
|
206
|
+
```typescript filename="ui/agent-tool.ts" copy
|
|
207
|
+
import { Tool, ToolContent, ToolHeader, ToolOutput } from "../ai-elements/tool";
|
|
208
|
+
import type { AgentDataPart } from "@mastra/ai-sdk";
|
|
209
|
+
|
|
210
|
+
export const AgentTool = ({ id, text, status }: AgentDataPart) => {
|
|
211
|
+
return (
|
|
212
|
+
<Tool>
|
|
213
|
+
<ToolHeader
|
|
214
|
+
type={`${id}`}
|
|
215
|
+
state={status === 'finished' ? 'output-available' : 'input-available'}
|
|
216
|
+
/>
|
|
217
|
+
<ToolContent>
|
|
218
|
+
<ToolOutput output={text} />
|
|
219
|
+
</ToolContent>
|
|
220
|
+
</Tool>
|
|
221
|
+
);
|
|
222
|
+
};
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### Stream Transformations
|
|
93
226
|
|
|
94
|
-
|
|
227
|
+
To manually transform Mastra's streams to AI SDK-compatible format, use the `toAISdkFormat()` utility.
|
|
228
|
+
|
|
229
|
+
```typescript filename="app/api/chat/route.ts" copy {3,13}
|
|
95
230
|
import { mastra } from "../../mastra";
|
|
231
|
+
import { createUIMessageStream, createUIMessageStreamResponse } from 'ai';
|
|
232
|
+
import { toAISdkFormat } from '@mastra/ai-sdk'
|
|
96
233
|
|
|
97
234
|
export async function POST(req: Request) {
|
|
98
235
|
const { messages } = await req.json();
|
|
99
236
|
const myAgent = mastra.getAgent("weatherAgent");
|
|
100
237
|
const stream = await myAgent.stream(messages);
|
|
101
238
|
|
|
102
|
-
|
|
239
|
+
// Transform stream into AI SDK format and create UI messages stream
|
|
240
|
+
const uiMessageStream = createUIMessageStream({
|
|
241
|
+
execute: async ({ writer }) => {
|
|
242
|
+
for await (const part of toAISdkFormat(stream, { from: 'agent' })!) {
|
|
243
|
+
writer.write(part);
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
// Create a Response that streams the UI message stream to the client
|
|
249
|
+
return createUIMessageStreamResponse({
|
|
250
|
+
stream: uiMessageStream,
|
|
251
|
+
});
|
|
103
252
|
}
|
|
104
253
|
```
|
|
105
254
|
|
|
106
|
-
|
|
255
|
+
## UI Hooks
|
|
256
|
+
|
|
257
|
+
Mastra supports AI SDK UI hooks for connecting frontend components directly to agents using HTTP streams.
|
|
258
|
+
|
|
259
|
+
Install the required AI SDK React package:
|
|
260
|
+
|
|
261
|
+
<Tabs items={["npm", "yarn", "pnpm", "bun"]}>
|
|
262
|
+
<Tabs.Tab>
|
|
263
|
+
```bash copy
|
|
264
|
+
npm install @ai-sdk/react
|
|
265
|
+
```
|
|
266
|
+
</Tabs.Tab>
|
|
267
|
+
<Tabs.Tab>
|
|
268
|
+
```bash copy
|
|
269
|
+
yarn add @ai-sdk/react
|
|
270
|
+
```
|
|
271
|
+
</Tabs.Tab>
|
|
272
|
+
<Tabs.Tab>
|
|
273
|
+
```bash copy
|
|
274
|
+
pnpm add @ai-sdk/react
|
|
275
|
+
```
|
|
276
|
+
</Tabs.Tab>
|
|
277
|
+
<Tabs.Tab>
|
|
278
|
+
```bash copy
|
|
279
|
+
bun add @ai-sdk/react
|
|
280
|
+
```
|
|
281
|
+
</Tabs.Tab>
|
|
282
|
+
</Tabs>
|
|
283
|
+
|
|
107
284
|
|
|
108
|
-
### Using
|
|
285
|
+
### Using `useChat()`
|
|
109
286
|
|
|
110
|
-
The `
|
|
287
|
+
The `useChat()` hook handles real-time chat interactions between your frontend and a Mastra agent, enabling you to send prompts and receive streaming responses over HTTP.
|
|
111
288
|
|
|
112
|
-
```typescript {
|
|
289
|
+
```typescript {8-12} filename="app/test/chat.tsx" copy
|
|
113
290
|
"use client";
|
|
114
291
|
|
|
115
|
-
import {
|
|
292
|
+
import { useChat } from "@ai-sdk/react";
|
|
293
|
+
import { useState } from "react";
|
|
116
294
|
|
|
117
|
-
export function
|
|
118
|
-
const
|
|
119
|
-
|
|
295
|
+
export function Chat() {
|
|
296
|
+
const [inputValue, setInputValue] = useState('')
|
|
297
|
+
const { messages, sendMessage} = useChat({
|
|
298
|
+
transport: new DefaultChatTransport({
|
|
299
|
+
api: 'http://localhost:4111/chat',
|
|
300
|
+
}),
|
|
120
301
|
});
|
|
121
302
|
|
|
303
|
+
const handleFormSubmit = (e: React.FormEvent) => {
|
|
304
|
+
e.preventDefault();
|
|
305
|
+
sendMessage({ text: inputValue });
|
|
306
|
+
};
|
|
307
|
+
|
|
122
308
|
return (
|
|
123
309
|
<div>
|
|
124
|
-
<
|
|
125
|
-
|
|
310
|
+
<pre>{JSON.stringify(messages, null, 2)}</pre>
|
|
311
|
+
<form onSubmit={handleFormSubmit}>
|
|
312
|
+
<input value={inputValue} onChange={e=>setInputValue(e.target.value)} placeholder="Name of city" />
|
|
126
313
|
</form>
|
|
127
|
-
<p>Completion result: {completion}</p>
|
|
128
314
|
</div>
|
|
129
315
|
);
|
|
130
316
|
}
|
|
131
317
|
```
|
|
132
318
|
|
|
133
|
-
Requests sent using the `
|
|
319
|
+
Requests sent using the `useChat()` hook are handled by a standard server route. This example shows how to define a POST route using a Next.js Route Handler.
|
|
134
320
|
|
|
135
|
-
```typescript filename="app/api/
|
|
136
|
-
import { mastra } from "
|
|
321
|
+
```typescript filename="app/api/chat/route.ts" copy
|
|
322
|
+
import { mastra } from "../../mastra";
|
|
137
323
|
|
|
138
324
|
export async function POST(req: Request) {
|
|
139
|
-
const {
|
|
325
|
+
const { messages } = await req.json();
|
|
140
326
|
const myAgent = mastra.getAgent("weatherAgent");
|
|
141
|
-
const stream = await myAgent.stream(
|
|
327
|
+
const stream = await myAgent.stream(messages, { format: 'aisdk' });
|
|
142
328
|
|
|
143
|
-
return stream.
|
|
329
|
+
return stream.toUIMessageStreamResponse()
|
|
144
330
|
}
|
|
145
331
|
```
|
|
146
332
|
|
|
147
|
-
|
|
333
|
+
> When using `useChat()` with agent memory, refer to the [Agent Memory section](/docs/agents/agent-memory#usechat) for key implementation details.
|
|
148
334
|
|
|
149
|
-
|
|
335
|
+
### Using `useCompletion()`
|
|
150
336
|
|
|
151
|
-
|
|
337
|
+
The `useCompletion()` hook handles single-turn completions between your frontend and a Mastra agent, allowing you to send a prompt and receive a streamed response over HTTP.
|
|
338
|
+
|
|
339
|
+
```typescript {6-8} filename="app/test/completion.tsx" copy
|
|
152
340
|
"use client";
|
|
153
341
|
|
|
154
|
-
import {
|
|
155
|
-
import { z } from "zod";
|
|
342
|
+
import { useCompletion } from "@ai-sdk/react";
|
|
156
343
|
|
|
157
|
-
export function
|
|
158
|
-
const {
|
|
159
|
-
api: "api/
|
|
160
|
-
schema: z.object({
|
|
161
|
-
weather: z.string()
|
|
162
|
-
})
|
|
344
|
+
export function Completion() {
|
|
345
|
+
const { completion, input, handleInputChange, handleSubmit } = useCompletion({
|
|
346
|
+
api: "api/completion"
|
|
163
347
|
});
|
|
164
348
|
|
|
165
349
|
return (
|
|
166
350
|
<div>
|
|
167
|
-
<
|
|
168
|
-
|
|
351
|
+
<form onSubmit={handleSubmit}>
|
|
352
|
+
<input value={input} onChange={handleInputChange} placeholder="Name of city" />
|
|
353
|
+
</form>
|
|
354
|
+
<p>Completion result: {completion}</p>
|
|
169
355
|
</div>
|
|
170
356
|
);
|
|
171
357
|
}
|
|
172
358
|
```
|
|
173
359
|
|
|
174
|
-
Requests sent using the `
|
|
360
|
+
Requests sent using the `useCompletion()` hook are handled by a standard server route. This example shows how to define a POST route using a Next.js Route Handler.
|
|
175
361
|
|
|
176
|
-
```typescript filename="app/api/
|
|
362
|
+
```typescript filename="app/api/completion/route.ts" copy
|
|
177
363
|
import { mastra } from "../../../mastra";
|
|
178
|
-
import { z } from "zod";
|
|
179
364
|
|
|
180
365
|
export async function POST(req: Request) {
|
|
181
|
-
const
|
|
366
|
+
const { prompt } = await req.json();
|
|
182
367
|
const myAgent = mastra.getAgent("weatherAgent");
|
|
183
|
-
const stream = await myAgent.stream(
|
|
184
|
-
structuredOutput: {
|
|
185
|
-
schema: z.object({
|
|
186
|
-
weather: z.string()
|
|
187
|
-
})
|
|
188
|
-
},
|
|
189
|
-
maxSteps: 1
|
|
190
|
-
});
|
|
368
|
+
const stream = await myAgent.stream([{ role: "user", content: prompt }], { format: 'aisdk' });
|
|
191
369
|
|
|
192
|
-
return stream.
|
|
370
|
+
return stream.toUIMessageStreamResponse()
|
|
193
371
|
}
|
|
194
372
|
```
|
|
195
373
|
|
|
196
|
-
### Passing additional data
|
|
374
|
+
### Passing additional data
|
|
197
375
|
|
|
198
|
-
|
|
376
|
+
`sendMessage()` allows you to pass additional data from the frontend to Mastra. This data can then be used on the server as `RuntimeContext`.
|
|
199
377
|
|
|
200
|
-
```typescript {
|
|
378
|
+
```typescript {16-26} filename="app/test/chat-extra.tsx" copy
|
|
201
379
|
"use client";
|
|
202
380
|
|
|
203
381
|
import { useChat } from "@ai-sdk/react";
|
|
382
|
+
import { useState } from "react";
|
|
204
383
|
|
|
205
384
|
export function ChatExtra() {
|
|
206
|
-
const
|
|
207
|
-
|
|
208
|
-
|
|
385
|
+
const [inputValue, setInputValue] = useState('')
|
|
386
|
+
const { messages, sendMessage } = useChat({
|
|
387
|
+
transport: new DefaultChatTransport({
|
|
388
|
+
api: 'http://localhost:4111/chat',
|
|
389
|
+
}),
|
|
209
390
|
});
|
|
210
391
|
|
|
211
392
|
const handleFormSubmit = (e: React.FormEvent) => {
|
|
212
393
|
e.preventDefault();
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
394
|
+
sendMessage({ text: inputValue }, {
|
|
395
|
+
body: {
|
|
396
|
+
data: {
|
|
397
|
+
userId: "user123",
|
|
398
|
+
preferences: {
|
|
399
|
+
language: "en",
|
|
400
|
+
temperature: "celsius"
|
|
401
|
+
}
|
|
219
402
|
}
|
|
220
403
|
}
|
|
221
404
|
});
|
|
@@ -225,16 +408,14 @@ export function ChatExtra() {
|
|
|
225
408
|
<div>
|
|
226
409
|
<pre>{JSON.stringify(messages, null, 2)}</pre>
|
|
227
410
|
<form onSubmit={handleFormSubmit}>
|
|
228
|
-
<input value={
|
|
411
|
+
<input value={inputValue} onChange={e=>setInputValue(e.target.value)} placeholder="Name of city" />
|
|
229
412
|
</form>
|
|
230
413
|
</div>
|
|
231
414
|
);
|
|
232
415
|
}
|
|
233
416
|
```
|
|
234
417
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
```typescript {8,12} filename="app/api/chat-extra/route.ts" showLineNumbers copy
|
|
418
|
+
```typescript {8,12} filename="app/api/chat-extra/route.ts" copy
|
|
238
419
|
import { mastra } from "../../../mastra";
|
|
239
420
|
import { RuntimeContext } from "@mastra/core/runtime-context";
|
|
240
421
|
|
|
@@ -250,17 +431,16 @@ export async function POST(req: Request) {
|
|
|
250
431
|
}
|
|
251
432
|
}
|
|
252
433
|
|
|
253
|
-
const stream = await myAgent.stream(messages, { runtimeContext });
|
|
254
|
-
return stream.
|
|
434
|
+
const stream = await myAgent.stream(messages, { runtimeContext, format: 'aisdk' });
|
|
435
|
+
return stream.toUIMessageStreamResponse();
|
|
255
436
|
}
|
|
256
437
|
```
|
|
257
438
|
|
|
258
|
-
|
|
259
439
|
### Handling `runtimeContext` with `server.middleware`
|
|
260
440
|
|
|
261
441
|
You can also populate the `RuntimeContext` by reading custom data in a server middleware:
|
|
262
442
|
|
|
263
|
-
```typescript {
|
|
443
|
+
```typescript {8,17} filename="mastra/index.ts" copy
|
|
264
444
|
import { Mastra } from "@mastra/core/mastra";
|
|
265
445
|
|
|
266
446
|
export const mastra = new Mastra({
|
|
@@ -290,118 +470,9 @@ export const mastra = new Mastra({
|
|
|
290
470
|
});
|
|
291
471
|
```
|
|
292
472
|
|
|
293
|
-
> You can then access this data in your tools via the `runtimeContext` parameter. See the [
|
|
294
|
-
|
|
295
|
-
## Streaming data
|
|
296
|
-
|
|
297
|
-
The `ai` package provides utilities for managing custom data streams. In some cases, you may want to send structured updates or annotations to the client using an agent's `dataStream`.
|
|
298
|
-
|
|
299
|
-
Install the required package:
|
|
300
|
-
|
|
301
|
-
<Tabs items={["npm", "yarn", "pnpm", "bun"]}>
|
|
302
|
-
<Tabs.Tab>
|
|
303
|
-
```bash copy
|
|
304
|
-
npm install ai
|
|
305
|
-
```
|
|
306
|
-
</Tabs.Tab>
|
|
307
|
-
<Tabs.Tab>
|
|
308
|
-
```bash copy
|
|
309
|
-
yarn add ai
|
|
310
|
-
```
|
|
311
|
-
</Tabs.Tab>
|
|
312
|
-
<Tabs.Tab>
|
|
313
|
-
```bash copy
|
|
314
|
-
pnpm add ai
|
|
315
|
-
```
|
|
316
|
-
</Tabs.Tab>
|
|
317
|
-
<Tabs.Tab>
|
|
318
|
-
```bash copy
|
|
319
|
-
bun add ai
|
|
320
|
-
```
|
|
321
|
-
</Tabs.Tab>
|
|
322
|
-
</Tabs>
|
|
323
|
-
|
|
324
|
-
### Using `createDataStream()`
|
|
325
|
-
|
|
326
|
-
The `createDataStream` function allows you to stream additional data to the client.
|
|
327
|
-
|
|
328
|
-
```typescript {1, 6} filename="mastra/agents/weather-agent.ts" showLineNumbers copy
|
|
329
|
-
import { createDataStream } from "ai";
|
|
330
|
-
import { Agent } from "@mastra/core/agent";
|
|
331
|
-
|
|
332
|
-
export const weatherAgent = new Agent({...});
|
|
333
|
-
|
|
334
|
-
createDataStream({
|
|
335
|
-
async execute(dataStream) {
|
|
336
|
-
dataStream.writeData({ value: "Hello" });
|
|
337
|
-
|
|
338
|
-
dataStream.writeMessageAnnotation({ type: "status", value: "processing" });
|
|
339
|
-
|
|
340
|
-
const agentStream = await weatherAgent.stream("What is the weather");
|
|
341
|
-
|
|
342
|
-
agentStream.mergeIntoDataStream(dataStream);
|
|
343
|
-
},
|
|
344
|
-
onError: (error) => `Custom error: ${error}`
|
|
345
|
-
});
|
|
346
|
-
```
|
|
347
|
-
|
|
348
|
-
### Using `createDataStreamResponse()`
|
|
349
|
-
|
|
350
|
-
The `createDataStreamResponse` function creates a response object that streams data to the client.
|
|
351
|
-
|
|
352
|
-
```typescript {2,9} filename="app/api/chat-stream/route.ts" showLineNumbers copy
|
|
353
|
-
import { mastra } from "../../../mastra";
|
|
354
|
-
import { createDataStreamResponse } from "ai";
|
|
355
|
-
|
|
356
|
-
export async function POST(req: Request) {
|
|
357
|
-
const { messages } = await req.json();
|
|
358
|
-
const myAgent = mastra.getAgent("weatherAgent");
|
|
359
|
-
const agentStream = await myAgent.stream(messages);
|
|
360
|
-
|
|
361
|
-
const response = createDataStreamResponse({
|
|
362
|
-
status: 200,
|
|
363
|
-
statusText: "OK",
|
|
364
|
-
headers: {
|
|
365
|
-
"Custom-Header": "value"
|
|
366
|
-
},
|
|
367
|
-
async execute(dataStream) {
|
|
368
|
-
dataStream.writeData({ value: "Hello" });
|
|
369
|
-
|
|
370
|
-
dataStream.writeMessageAnnotation({
|
|
371
|
-
type: "status",
|
|
372
|
-
value: "processing"
|
|
373
|
-
});
|
|
374
|
-
|
|
375
|
-
agentStream.mergeIntoDataStream(dataStream);
|
|
376
|
-
},
|
|
377
|
-
onError: (error) => `Custom error: ${error}`
|
|
378
|
-
});
|
|
379
|
-
|
|
380
|
-
return response;
|
|
381
|
-
}
|
|
382
|
-
```
|
|
383
|
-
|
|
384
|
-
## Vercel AI SDK v5
|
|
473
|
+
> You can then access this data in your tools via the `runtimeContext` parameter. See the [Runtime Context documentation](/docs/server-db/runtime-context) for more details.
|
|
385
474
|
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
> Please add any feedback or bug reports to the [AI SDK v5 mega issue in Github.](https://github.com/mastra-ai/mastra/issues/5470)
|
|
389
|
-
|
|
390
|
-
### Stream Support
|
|
391
|
-
|
|
392
|
-
Mastra's experimental `stream` method now includes native AI SDK v5 support through the `format` parameter. This provides seamless integration with AI SDK v5's streaming interfaces without requiring compatibility wrappers.
|
|
393
|
-
|
|
394
|
-
```typescript
|
|
395
|
-
// Use stream with AI SDK v5 format
|
|
396
|
-
const stream = await agent.stream(messages, {
|
|
397
|
-
format: 'aisdk' // Enable AI SDK v5 compatibility
|
|
398
|
-
});
|
|
399
|
-
|
|
400
|
-
// The stream is now compatible with AI SDK v5 interfaces
|
|
401
|
-
return stream.toUIMessageStreamResponse();
|
|
402
|
-
```
|
|
403
|
-
|
|
404
|
-
### Official migration guide
|
|
475
|
+
## Migrating from AI SDK v4 to v5
|
|
405
476
|
|
|
406
477
|
Follow the official [AI SDK v5 Migration Guide](https://v5.ai-sdk.dev/docs/migration-guides/migration-guide-5-0) for all AI SDK core breaking changes, package updates, and API changes.
|
|
407
478
|
|
|
@@ -416,7 +487,7 @@ Mastra automatically handles AI SDK v4 data using its internal `MessageList` cla
|
|
|
416
487
|
|
|
417
488
|
### Message Format Conversion
|
|
418
489
|
|
|
419
|
-
For cases where you need to manually convert messages between AI SDK and Mastra formats, use the `convertMessages` utility:
|
|
490
|
+
For cases where you need to manually convert messages between AI SDK and Mastra formats, use the `convertMessages()` utility:
|
|
420
491
|
|
|
421
492
|
```typescript
|
|
422
493
|
import { convertMessages } from '@mastra/core/agent';
|
|
@@ -433,68 +504,15 @@ const aiv5Messages = convertMessages(mastraMessages).to('AIV5.Core');
|
|
|
433
504
|
|
|
434
505
|
This utility is helpful when you want to fetch messages directly from your storage DB and convert them for use in AI SDK.
|
|
435
506
|
|
|
436
|
-
### Enabling stream compatibility
|
|
437
|
-
|
|
438
|
-
To enable AI SDK v5 compatibility, use the `@mastra/ai-sdk` package:
|
|
439
|
-
|
|
440
|
-
<Tabs items={["npm", "yarn", "pnpm", "bun"]}>
|
|
441
|
-
<Tabs.Tab>
|
|
442
|
-
```bash copy
|
|
443
|
-
npm install @mastra/ai-sdk
|
|
444
|
-
```
|
|
445
|
-
</Tabs.Tab>
|
|
446
|
-
<Tabs.Tab>
|
|
447
|
-
```bash copy
|
|
448
|
-
yarn add @mastra/ai-sdk
|
|
449
|
-
```
|
|
450
|
-
</Tabs.Tab>
|
|
451
|
-
<Tabs.Tab>
|
|
452
|
-
```bash copy
|
|
453
|
-
pnpm add @mastra/ai-sdk
|
|
454
|
-
```
|
|
455
|
-
</Tabs.Tab>
|
|
456
|
-
<Tabs.Tab>
|
|
457
|
-
```bash copy
|
|
458
|
-
bun add @mastra/ai-sdk
|
|
459
|
-
```
|
|
460
|
-
</Tabs.Tab>
|
|
461
|
-
</Tabs>
|
|
462
|
-
|
|
463
|
-
```typescript filename="src/mastra/index.ts" showLineNumbers copy
|
|
464
|
-
import { Mastra } from '@mastra/core/mastra';
|
|
465
|
-
import { chatRoute } from '@mastra/ai-sdk';
|
|
466
|
-
|
|
467
|
-
export const mastra = new Mastra({
|
|
468
|
-
server: {
|
|
469
|
-
apiRoutes: [
|
|
470
|
-
chatRoute({
|
|
471
|
-
path: '/chat',
|
|
472
|
-
agent: 'weatherAgent',
|
|
473
|
-
}),
|
|
474
|
-
],
|
|
475
|
-
},
|
|
476
|
-
});
|
|
477
|
-
```
|
|
478
|
-
|
|
479
|
-
In your application call the `useChat()` hook.
|
|
480
|
-
```typescript
|
|
481
|
-
const { error, status, sendMessage, messages, regenerate, stop } =
|
|
482
|
-
useChat({
|
|
483
|
-
transport: new DefaultChatTransport({
|
|
484
|
-
api: 'http://localhost:4111/chat',
|
|
485
|
-
}),
|
|
486
|
-
});
|
|
487
|
-
```
|
|
488
|
-
|
|
489
507
|
### Type Inference for Tools
|
|
490
508
|
|
|
491
509
|
When using tools with TypeScript in AI SDK v5, Mastra provides type inference helpers to ensure type safety for your tool inputs and outputs.
|
|
492
510
|
|
|
493
|
-
#### InferUITool
|
|
511
|
+
#### `InferUITool`
|
|
494
512
|
|
|
495
513
|
The `InferUITool` type helper infers the input and output types of a single Mastra tool:
|
|
496
514
|
|
|
497
|
-
```typescript filename="app/types.ts"
|
|
515
|
+
```typescript filename="app/types.ts" copy
|
|
498
516
|
import { InferUITool, createTool } from "@mastra/core/tools";
|
|
499
517
|
import { z } from "zod";
|
|
500
518
|
|
|
@@ -525,11 +543,11 @@ type WeatherUITool = InferUITool<typeof weatherTool>;
|
|
|
525
543
|
// }
|
|
526
544
|
```
|
|
527
545
|
|
|
528
|
-
#### InferUITools
|
|
546
|
+
#### `InferUITools`
|
|
529
547
|
|
|
530
548
|
The `InferUITools` type helper infers the input and output types of multiple tools:
|
|
531
549
|
|
|
532
|
-
```typescript filename="app/mastra/tools.ts"
|
|
550
|
+
```typescript filename="app/mastra/tools.ts" copy
|
|
533
551
|
import { InferUITools, createTool } from "@mastra/core/tools";
|
|
534
552
|
import { z } from "zod";
|
|
535
553
|
|