@mastra/mcp-docs-server 0.13.5-alpha.0 → 0.13.6
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%2Fstorage-test-utils.md +76 -76
- package/.docs/organized/changelogs/%40mastra%2Fagui.md +61 -61
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +54 -54
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +211 -211
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +75 -75
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +91 -91
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +185 -185
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +237 -237
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +143 -143
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-vercel.md +143 -143
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +235 -235
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +84 -84
- package/.docs/organized/changelogs/%40mastra%2Fevals.md +56 -56
- package/.docs/organized/changelogs/%40mastra%2Ffastembed.md +7 -0
- package/.docs/organized/changelogs/%40mastra%2Ffirecrawl.md +75 -75
- package/.docs/organized/changelogs/%40mastra%2Flance.md +55 -0
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +56 -56
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +25 -25
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +38 -38
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +119 -119
- package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +58 -58
- package/.docs/organized/changelogs/%40mastra%2Fmssql.md +69 -0
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +119 -119
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +244 -244
- package/.docs/organized/changelogs/%40mastra%2Frag.md +73 -73
- package/.docs/organized/changelogs/%40mastra%2Fschema-compat.md +24 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +211 -211
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +76 -76
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai-realtime.md +44 -44
- package/.docs/organized/changelogs/create-mastra.md +126 -126
- package/.docs/organized/changelogs/mastra.md +256 -256
- package/.docs/organized/code-examples/agent.md +6 -0
- package/.docs/organized/code-examples/agui.md +3 -3
- package/.docs/organized/code-examples/ai-sdk-useChat.md +2 -2
- package/.docs/organized/code-examples/ai-sdk-v5.md +201 -0
- package/.docs/organized/code-examples/assistant-ui.md +2 -2
- package/.docs/organized/code-examples/bird-checker-with-nextjs-and-eval.md +2 -2
- package/.docs/organized/code-examples/bird-checker-with-nextjs.md +2 -2
- package/.docs/organized/code-examples/client-side-tools.md +1 -1
- package/.docs/organized/code-examples/crypto-chatbot.md +5 -5
- package/.docs/organized/code-examples/openapi-spec-writer.md +2 -2
- package/.docs/organized/code-examples/workflow-with-suspend-resume.md +181 -0
- package/.docs/raw/agents/agent-memory.mdx +126 -0
- package/.docs/raw/agents/dynamic-agents.mdx +34 -2
- package/.docs/raw/agents/overview.mdx +21 -33
- package/.docs/raw/community/licensing.mdx +27 -19
- package/.docs/raw/deployment/cloud-providers/amazon-ec2.mdx +60 -26
- package/.docs/raw/deployment/cloud-providers/digital-ocean.mdx +1 -1
- package/.docs/raw/deployment/cloud-providers/index.mdx +44 -9
- package/.docs/raw/deployment/server-deployment.mdx +56 -0
- package/.docs/raw/deployment/serverless-platforms/cloudflare-deployer.mdx +9 -30
- package/.docs/raw/deployment/serverless-platforms/index.mdx +13 -13
- package/.docs/raw/frameworks/agentic-uis/ai-sdk.mdx +291 -216
- package/.docs/raw/frameworks/agentic-uis/assistant-ui.mdx +0 -34
- package/.docs/raw/frameworks/agentic-uis/copilotkit.mdx +162 -181
- package/.docs/raw/frameworks/servers/express.mdx +1 -1
- package/.docs/raw/frameworks/web-frameworks/astro.mdx +2 -2
- package/.docs/raw/frameworks/web-frameworks/next-js.mdx +1 -1
- package/.docs/raw/frameworks/web-frameworks/sveltekit.mdx +4 -4
- package/.docs/raw/frameworks/web-frameworks/vite-react.mdx +1 -1
- package/.docs/raw/getting-started/installation.mdx +10 -7
- package/.docs/raw/getting-started/model-capability.mdx +1 -1
- package/.docs/raw/memory/overview.mdx +8 -0
- package/.docs/raw/memory/semantic-recall.mdx +6 -0
- package/.docs/raw/observability/tracing.mdx +30 -0
- package/.docs/raw/rag/retrieval.mdx +24 -5
- package/.docs/raw/reference/agents/agent.mdx +2 -2
- package/.docs/raw/reference/cli/create-mastra.mdx +7 -0
- package/.docs/raw/reference/cli/dev.mdx +4 -3
- package/.docs/raw/reference/client-js/agents.mdx +8 -0
- package/.docs/raw/reference/memory/query.mdx +35 -14
- package/.docs/raw/reference/observability/providers/keywordsai.mdx +73 -0
- package/.docs/raw/reference/rag/rerankWithScorer.mdx +213 -0
- package/.docs/raw/reference/storage/mssql.mdx +108 -0
- package/.docs/raw/server-db/custom-api-routes.mdx +67 -0
- package/.docs/raw/server-db/production-server.mdx +66 -0
- package/.docs/raw/tools-mcp/mcp-overview.mdx +28 -7
- package/.docs/raw/workflows/control-flow.mdx +91 -93
- package/.docs/raw/workflows/input-data-mapping.mdx +31 -43
- package/.docs/raw/workflows/overview.mdx +22 -12
- package/.docs/raw/workflows/pausing-execution.mdx +49 -4
- package/.docs/raw/workflows/suspend-and-resume.mdx +17 -16
- package/.docs/raw/workflows/using-with-agents-and-tools.mdx +16 -13
- package/.docs/raw/workflows-legacy/overview.mdx +11 -0
- package/LICENSE.md +11 -42
- package/package.json +7 -9
- package/.docs/raw/deployment/custom-api-routes.mdx +0 -55
- package/.docs/raw/deployment/server.mdx +0 -116
- package/.docs/raw/frameworks/ai-sdk-v5.mdx +0 -91
- /package/.docs/raw/{local-dev/mastra-dev.mdx → server-db/local-dev-playground.mdx} +0 -0
- /package/.docs/raw/{client-js/overview.mdx → server-db/mastra-client.mdx} +0 -0
- /package/.docs/raw/{deployment → server-db}/middleware.mdx +0 -0
- /package/.docs/raw/{storage/overview.mdx → server-db/storage.mdx} +0 -0
|
@@ -3,141 +3,126 @@ title: "Using with Vercel AI SDK"
|
|
|
3
3
|
description: "Learn how Mastra leverages the Vercel AI SDK library and how you can leverage it further with Mastra"
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
-
import
|
|
6
|
+
import { Callout, Tabs } from "nextra/components";
|
|
7
7
|
|
|
8
|
-
# Using
|
|
8
|
+
# Using Vercel AI SDK
|
|
9
9
|
|
|
10
|
-
Mastra
|
|
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
|
-
|
|
12
|
+
## AI SDK v5 (beta)
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
Mastra also supports AI SDK v5 (beta) see the following section for v5 specific methods: [Vercel AI SDK v5](/docs/frameworks/agentic-uis/ai-sdk#vercel-ai-sdk-v5)
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
alt="Agent interaction trace showing spans, LLM calls, and tool executions"
|
|
21
|
-
style={{ maxWidth: "800px", width: "100%", margin: "8px 0" }}
|
|
22
|
-
className="nextra-image rounded-md py-8"
|
|
23
|
-
data-zoom
|
|
24
|
-
width={800}
|
|
25
|
-
height={400}
|
|
26
|
-
/>
|
|
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>
|
|
27
20
|
|
|
28
21
|
## Model routing
|
|
29
22
|
|
|
30
|
-
When creating agents in Mastra, you can specify any AI SDK-supported model
|
|
23
|
+
When creating agents in Mastra, you can specify any AI SDK-supported model.
|
|
31
24
|
|
|
32
|
-
```typescript
|
|
25
|
+
```typescript {7} filename="agents/weather-agent.ts" showLineNumbers copy
|
|
33
26
|
import { openai } from "@ai-sdk/openai";
|
|
34
27
|
import { Agent } from "@mastra/core/agent";
|
|
35
28
|
|
|
36
|
-
const
|
|
37
|
-
name: "
|
|
29
|
+
export const weatherAgent = new Agent({
|
|
30
|
+
name: "Weather Agent",
|
|
38
31
|
instructions: "Instructions for the agent...",
|
|
39
|
-
model: openai("gpt-4-turbo"),
|
|
32
|
+
model: openai("gpt-4-turbo"),
|
|
40
33
|
});
|
|
41
|
-
|
|
42
|
-
const result = await agent.generate("What is the weather like?");
|
|
43
34
|
```
|
|
44
35
|
|
|
45
|
-
|
|
36
|
+
> See [Model Providers](/docs/getting-started/model-providers) and [Model Capabilities](/docs/getting-started/model-capability) for more information.
|
|
46
37
|
|
|
47
|
-
|
|
38
|
+
## React Hooks
|
|
48
39
|
|
|
49
|
-
|
|
40
|
+
Mastra supports AI SDK hooks for connecting frontend components directly to agents using HTTP streams.
|
|
50
41
|
|
|
51
|
-
|
|
42
|
+
Install the required AI SDK React package:
|
|
52
43
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
44
|
+
<Tabs items={["npm", "yarn", "pnpm", "bun"]}>
|
|
45
|
+
<Tabs.Tab>
|
|
46
|
+
```bash copy
|
|
47
|
+
npm install @ai-sdk/react
|
|
48
|
+
```
|
|
49
|
+
</Tabs.Tab>
|
|
50
|
+
<Tabs.Tab>
|
|
51
|
+
```bash copy
|
|
52
|
+
yarn add @ai-sdk/react
|
|
53
|
+
```
|
|
54
|
+
</Tabs.Tab>
|
|
55
|
+
<Tabs.Tab>
|
|
56
|
+
```bash copy
|
|
57
|
+
pnpm add @ai-sdk/react
|
|
58
|
+
```
|
|
59
|
+
</Tabs.Tab>
|
|
60
|
+
<Tabs.Tab>
|
|
61
|
+
```bash copy
|
|
62
|
+
bun add @ai-sdk/react
|
|
63
|
+
```
|
|
64
|
+
</Tabs.Tab>
|
|
65
|
+
</Tabs>
|
|
57
66
|
|
|
58
|
-
```typescript filename="app/api/chat/route.ts" copy
|
|
59
|
-
import { mastra } from "@/src/mastra";
|
|
60
67
|
|
|
61
|
-
|
|
62
|
-
const { messages } = await req.json();
|
|
63
|
-
const myAgent = mastra.getAgent("weatherAgent");
|
|
64
|
-
const stream = await myAgent.stream(messages);
|
|
68
|
+
### Using the `useChat()` Hook
|
|
65
69
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
```
|
|
70
|
+
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.
|
|
71
|
+
|
|
72
|
+
```typescript {6} filename="app/test/chat.tsx" showLineNumbers copy
|
|
73
|
+
"use client";
|
|
69
74
|
|
|
70
|
-
|
|
71
|
-
import { useChat } from '@ai-sdk/react';
|
|
75
|
+
import { useChat } from "@ai-sdk/react";
|
|
72
76
|
|
|
73
|
-
export function
|
|
77
|
+
export function Chat() {
|
|
74
78
|
const { messages, input, handleInputChange, handleSubmit } = useChat({
|
|
75
|
-
api:
|
|
79
|
+
api: "api/chat"
|
|
76
80
|
});
|
|
77
|
-
|
|
78
81
|
return (
|
|
79
82
|
<div>
|
|
80
|
-
{
|
|
81
|
-
<div key={m.id}>
|
|
82
|
-
{m.role}: {m.content}
|
|
83
|
-
</div>
|
|
84
|
-
))}
|
|
83
|
+
<pre>{JSON.stringify(messages, null, 2)}</pre>
|
|
85
84
|
<form onSubmit={handleSubmit}>
|
|
86
|
-
<input
|
|
87
|
-
value={input}
|
|
88
|
-
onChange={handleInputChange}
|
|
89
|
-
placeholder="Say something..."
|
|
90
|
-
/>
|
|
85
|
+
<input value={input} onChange={handleInputChange} placeholder="Name of city" />
|
|
91
86
|
</form>
|
|
92
87
|
</div>
|
|
93
88
|
);
|
|
94
89
|
}
|
|
95
90
|
```
|
|
96
91
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
### useCompletion
|
|
100
|
-
|
|
101
|
-
For single-turn completions, use the `useCompletion` hook:
|
|
92
|
+
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.
|
|
102
93
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
- Works with the Mastra REST API agent stream endpoint `{MASTRA_BASE_URL}/agents/:agentId/stream` for data streams,
|
|
106
|
-
i.e. no structured output is defined.
|
|
107
|
-
|
|
108
|
-
```typescript filename="app/api/completion/route.ts" copy
|
|
109
|
-
import { mastra } from "@/src/mastra";
|
|
94
|
+
```typescript filename="app/api/chat/route.ts" showLineNumbers copy
|
|
95
|
+
import { mastra } from "../../mastra";
|
|
110
96
|
|
|
111
97
|
export async function POST(req: Request) {
|
|
112
|
-
const {
|
|
98
|
+
const { messages } = await req.json();
|
|
113
99
|
const myAgent = mastra.getAgent("weatherAgent");
|
|
114
|
-
const stream = await myAgent.stream(
|
|
100
|
+
const stream = await myAgent.stream(messages);
|
|
115
101
|
|
|
116
102
|
return stream.toDataStreamResponse();
|
|
117
103
|
}
|
|
118
104
|
```
|
|
119
105
|
|
|
120
|
-
|
|
106
|
+
> When using `useChat` with agent memory, refer to the [Agent Memory section](/docs/agents/agent-memory#usechat) for key implementation details.
|
|
107
|
+
|
|
108
|
+
### Using the `useCompletion()` Hook
|
|
109
|
+
|
|
110
|
+
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.
|
|
111
|
+
|
|
112
|
+
```typescript {6} filename="app/test/completion.tsx" showLineNumbers copy
|
|
113
|
+
"use client";
|
|
114
|
+
|
|
121
115
|
import { useCompletion } from "@ai-sdk/react";
|
|
122
116
|
|
|
123
|
-
export function
|
|
124
|
-
const {
|
|
125
|
-
completion
|
|
126
|
-
input,
|
|
127
|
-
handleInputChange,
|
|
128
|
-
handleSubmit,
|
|
129
|
-
} = useCompletion({
|
|
130
|
-
api: '/path-to-your-agent-stream-api-endpoint'
|
|
117
|
+
export function Completion() {
|
|
118
|
+
const { completion, input, handleInputChange, handleSubmit } = useCompletion({
|
|
119
|
+
api: "api/completion"
|
|
131
120
|
});
|
|
132
121
|
|
|
133
122
|
return (
|
|
134
123
|
<div>
|
|
135
124
|
<form onSubmit={handleSubmit}>
|
|
136
|
-
<input
|
|
137
|
-
value={input}
|
|
138
|
-
onChange={handleInputChange}
|
|
139
|
-
placeholder="Enter a prompt..."
|
|
140
|
-
/>
|
|
125
|
+
<input value={input} onChange={handleInputChange} placeholder="Name of city" />
|
|
141
126
|
</form>
|
|
142
127
|
<p>Completion result: {completion}</p>
|
|
143
128
|
</div>
|
|
@@ -145,113 +130,135 @@ export function CompletionComponent() {
|
|
|
145
130
|
}
|
|
146
131
|
```
|
|
147
132
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
For consuming text streams that represent JSON objects and parsing them into a complete object based on a schema.
|
|
133
|
+
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.
|
|
151
134
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
i.e. structured output is defined.
|
|
155
|
-
|
|
156
|
-
```typescript filename="app/api/use-object/route.ts" copy
|
|
157
|
-
import { mastra } from "@/src/mastra";
|
|
135
|
+
```typescript filename="app/api/completion/route.ts" showLineNumbers copy
|
|
136
|
+
import { mastra } from "../../../mastra";
|
|
158
137
|
|
|
159
138
|
export async function POST(req: Request) {
|
|
160
|
-
const
|
|
139
|
+
const { prompt } = await req.json();
|
|
161
140
|
const myAgent = mastra.getAgent("weatherAgent");
|
|
162
|
-
const stream = await myAgent.stream(
|
|
163
|
-
output: z.object({
|
|
164
|
-
weather: z.string(),
|
|
165
|
-
}),
|
|
166
|
-
});
|
|
141
|
+
const stream = await myAgent.stream([{ role: "user", content: prompt }]);
|
|
167
142
|
|
|
168
|
-
return stream.
|
|
143
|
+
return stream.toDataStreamResponse();
|
|
169
144
|
}
|
|
170
145
|
```
|
|
171
146
|
|
|
172
|
-
|
|
173
|
-
import { experimental_useObject as useObject } from '@ai-sdk/react';
|
|
147
|
+
### Using the `useObject()` Hook
|
|
174
148
|
|
|
175
|
-
|
|
149
|
+
The `useObject` hook consumes streamed text from a Mastra agent and parses it into a structured JSON object based on a defined schema.
|
|
150
|
+
|
|
151
|
+
```typescript {7} filename="app/test/object.tsx" showLineNumbers copy
|
|
152
|
+
"use client";
|
|
153
|
+
|
|
154
|
+
import { experimental_useObject as useObject } from "@ai-sdk/react";
|
|
155
|
+
import { z } from "zod";
|
|
156
|
+
|
|
157
|
+
export function Object() {
|
|
176
158
|
const { object, submit } = useObject({
|
|
177
|
-
api:
|
|
159
|
+
api: "api/object",
|
|
178
160
|
schema: z.object({
|
|
179
|
-
weather: z.string()
|
|
180
|
-
})
|
|
161
|
+
weather: z.string()
|
|
162
|
+
})
|
|
181
163
|
});
|
|
182
164
|
|
|
183
165
|
return (
|
|
184
166
|
<div>
|
|
185
|
-
<button onClick={() => submit(
|
|
186
|
-
{object
|
|
167
|
+
<button onClick={() => submit("London")}>Generate</button>
|
|
168
|
+
{object ? <pre>{JSON.stringify(object, null, 2)}</pre> : null}
|
|
187
169
|
</div>
|
|
188
170
|
);
|
|
189
171
|
}
|
|
190
172
|
```
|
|
191
173
|
|
|
192
|
-
|
|
174
|
+
Requests sent using the `useObject` hook are handled by a standard server route. This example shows how to define a POST route using a Next.js Route Handler.
|
|
193
175
|
|
|
194
|
-
|
|
176
|
+
```typescript filename="app/api/object/route.ts" showLineNumbers copy
|
|
177
|
+
import { mastra } from "../../../mastra";
|
|
178
|
+
import { z } from "zod";
|
|
195
179
|
|
|
196
|
-
|
|
180
|
+
export async function POST(req: Request) {
|
|
181
|
+
const body = await req.json();
|
|
182
|
+
const myAgent = mastra.getAgent("weatherAgent");
|
|
183
|
+
const stream = await myAgent.stream(body, {
|
|
184
|
+
output: z.object({
|
|
185
|
+
weather: z.string()
|
|
186
|
+
})
|
|
187
|
+
});
|
|
197
188
|
|
|
198
|
-
|
|
199
|
-
|
|
189
|
+
return stream.toTextStreamResponse();
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Passing additional data with `sendExtraMessageFields`
|
|
194
|
+
|
|
195
|
+
The `sendExtraMessageFields` option allows you to pass additional data from the frontend to Mastra. This data is available on the server as `RuntimeContext`.
|
|
196
|
+
|
|
197
|
+
```typescript {8,14-20} filename="app/test/chat-extra.tsx" showLineNumbers copy
|
|
198
|
+
"use client";
|
|
200
199
|
|
|
201
|
-
|
|
200
|
+
import { useChat } from "@ai-sdk/react";
|
|
201
|
+
|
|
202
|
+
export function ChatExtra() {
|
|
202
203
|
const { messages, input, handleInputChange, handleSubmit } = useChat({
|
|
203
|
-
api:
|
|
204
|
-
sendExtraMessageFields: true
|
|
204
|
+
api: "/api/chat-extra",
|
|
205
|
+
sendExtraMessageFields: true
|
|
205
206
|
});
|
|
206
207
|
|
|
207
208
|
const handleFormSubmit = (e: React.FormEvent) => {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
209
|
+
e.preventDefault();
|
|
210
|
+
handleSubmit(e, {
|
|
211
|
+
data: {
|
|
212
|
+
userId: "user123",
|
|
213
|
+
preferences: {
|
|
214
|
+
language: "en",
|
|
215
|
+
temperature: "celsius"
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
});
|
|
216
219
|
};
|
|
217
220
|
|
|
218
221
|
return (
|
|
219
|
-
<
|
|
220
|
-
<
|
|
221
|
-
|
|
222
|
+
<div>
|
|
223
|
+
<pre>{JSON.stringify(messages, null, 2)}</pre>
|
|
224
|
+
<form onSubmit={handleFormSubmit}>
|
|
225
|
+
<input value={input} onChange={handleInputChange} placeholder="Name of city" />
|
|
226
|
+
</form>
|
|
227
|
+
</div>
|
|
222
228
|
);
|
|
223
229
|
}
|
|
224
230
|
```
|
|
225
231
|
|
|
226
|
-
|
|
232
|
+
Requests sent using `sendExtraMessageFields` are handled by a standard server route. This example shows how to extract the custom data and populate a `RuntimeContext` instance.
|
|
227
233
|
|
|
228
|
-
```typescript filename="app/api/chat/route.ts" copy
|
|
229
|
-
import { mastra } from "
|
|
234
|
+
```typescript {8,12} filename="app/api/chat-extra/route.ts" showLineNumbers copy
|
|
235
|
+
import { mastra } from "../../../mastra";
|
|
230
236
|
import { RuntimeContext } from "@mastra/core/runtime-context";
|
|
231
237
|
|
|
232
238
|
export async function POST(req: Request) {
|
|
233
239
|
const { messages, data } = await req.json();
|
|
234
240
|
const myAgent = mastra.getAgent("weatherAgent");
|
|
235
|
-
|
|
241
|
+
|
|
236
242
|
const runtimeContext = new RuntimeContext();
|
|
237
|
-
|
|
243
|
+
|
|
238
244
|
if (data) {
|
|
239
|
-
|
|
245
|
+
for (const [key, value] of Object.entries(data)) {
|
|
240
246
|
runtimeContext.set(key, value);
|
|
241
|
-
}
|
|
247
|
+
}
|
|
242
248
|
}
|
|
243
|
-
|
|
249
|
+
|
|
244
250
|
const stream = await myAgent.stream(messages, { runtimeContext });
|
|
245
251
|
return stream.toDataStreamResponse();
|
|
246
252
|
}
|
|
247
253
|
```
|
|
248
254
|
|
|
249
|
-
#### Alternative: Server Middleware
|
|
250
255
|
|
|
251
|
-
|
|
256
|
+
### Handling `runtimeContext` with `server.middleware`
|
|
252
257
|
|
|
253
|
-
|
|
254
|
-
|
|
258
|
+
You can also populate the `RuntimeContext` by reading custom data in a server middleware:
|
|
259
|
+
|
|
260
|
+
```typescript {6} filename="mastra/index.ts" showLineNumbers copy
|
|
261
|
+
import { Mastra } from "@mastra/core/mastra";
|
|
255
262
|
|
|
256
263
|
export const mastra = new Mastra({
|
|
257
264
|
agents: { weatherAgent },
|
|
@@ -259,24 +266,20 @@ export const mastra = new Mastra({
|
|
|
259
266
|
middleware: [
|
|
260
267
|
async (c, next) => {
|
|
261
268
|
const runtimeContext = c.get("runtimeContext");
|
|
262
|
-
|
|
263
|
-
if (c.req.method ===
|
|
269
|
+
|
|
270
|
+
if (c.req.method === "POST") {
|
|
264
271
|
try {
|
|
265
|
-
// Clone the request since reading the body can only be done once
|
|
266
272
|
const clonedReq = c.req.raw.clone();
|
|
267
273
|
const body = await clonedReq.json();
|
|
268
|
-
|
|
269
|
-
|
|
274
|
+
|
|
270
275
|
if (body?.data) {
|
|
271
|
-
Object.entries(body.data)
|
|
276
|
+
for (const [key, value] of Object.entries(body.data)) {
|
|
272
277
|
runtimeContext.set(key, value);
|
|
273
|
-
}
|
|
278
|
+
}
|
|
274
279
|
}
|
|
275
280
|
} catch {
|
|
276
|
-
// Continue without additional data
|
|
277
281
|
}
|
|
278
282
|
}
|
|
279
|
-
|
|
280
283
|
await next();
|
|
281
284
|
},
|
|
282
285
|
],
|
|
@@ -284,115 +287,187 @@ export const mastra = new Mastra({
|
|
|
284
287
|
});
|
|
285
288
|
```
|
|
286
289
|
|
|
287
|
-
You can then access this data in your tools via the `runtimeContext` parameter. See the [Runtime Context documentation](/docs/agents/runtime-variables) for more details.
|
|
288
|
-
|
|
289
|
-
##
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
290
|
+
> You can then access this data in your tools via the `runtimeContext` parameter. See the [Runtime Context documentation](/docs/agents/runtime-variables) for more details.
|
|
291
|
+
|
|
292
|
+
## Streaming data
|
|
293
|
+
|
|
294
|
+
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`.
|
|
295
|
+
|
|
296
|
+
Install the required package:
|
|
297
|
+
|
|
298
|
+
<Tabs items={["npm", "yarn", "pnpm", "bun"]}>
|
|
299
|
+
<Tabs.Tab>
|
|
300
|
+
```bash copy
|
|
301
|
+
npm install ai
|
|
302
|
+
```
|
|
303
|
+
</Tabs.Tab>
|
|
304
|
+
<Tabs.Tab>
|
|
305
|
+
```bash copy
|
|
306
|
+
yarn add ai
|
|
307
|
+
```
|
|
308
|
+
</Tabs.Tab>
|
|
309
|
+
<Tabs.Tab>
|
|
310
|
+
```bash copy
|
|
311
|
+
pnpm add ai
|
|
312
|
+
```
|
|
313
|
+
</Tabs.Tab>
|
|
314
|
+
<Tabs.Tab>
|
|
315
|
+
```bash copy
|
|
316
|
+
bun add ai
|
|
317
|
+
```
|
|
318
|
+
</Tabs.Tab>
|
|
319
|
+
</Tabs>
|
|
320
|
+
|
|
321
|
+
### Using `createDataStream()`
|
|
322
|
+
|
|
323
|
+
The `createDataStream` function allows you to stream additional data to the client.
|
|
324
|
+
|
|
325
|
+
```typescript {1, 6} filename="mastra/agents/weather-agent.ts" showLineNumbers copy
|
|
322
326
|
import { createDataStream } from "ai";
|
|
323
327
|
import { Agent } from "@mastra/core/agent";
|
|
324
328
|
|
|
325
|
-
export const weatherAgent = new Agent({
|
|
326
|
-
name: "Weather Agent",
|
|
327
|
-
instructions: `
|
|
328
|
-
You are a helpful weather assistant that provides accurate weather information.
|
|
329
|
-
|
|
330
|
-
Your primary function is to help users get weather details for specific locations. When responding:
|
|
331
|
-
- Always ask for a location if none is provided
|
|
332
|
-
- If the location name isn't in English, please translate it
|
|
333
|
-
- If giving a location with multiple parts (e.g. "New York, NY"), use the most relevant part (e.g. "New York")
|
|
334
|
-
- Include relevant details like humidity, wind conditions, and precipitation
|
|
335
|
-
- Keep responses concise but informative
|
|
336
|
-
|
|
337
|
-
Use the weatherTool to fetch current weather data.
|
|
338
|
-
`,
|
|
339
|
-
model: openai("gpt-4o"),
|
|
340
|
-
tools: { weatherTool },
|
|
341
|
-
});
|
|
329
|
+
export const weatherAgent = new Agent({...});
|
|
342
330
|
|
|
343
|
-
|
|
331
|
+
createDataStream({
|
|
344
332
|
async execute(dataStream) {
|
|
345
|
-
// Write data
|
|
346
333
|
dataStream.writeData({ value: "Hello" });
|
|
347
334
|
|
|
348
|
-
// Write annotation
|
|
349
335
|
dataStream.writeMessageAnnotation({ type: "status", value: "processing" });
|
|
350
336
|
|
|
351
|
-
//mastra agent stream
|
|
352
337
|
const agentStream = await weatherAgent.stream("What is the weather");
|
|
353
338
|
|
|
354
|
-
// Merge agent stream
|
|
355
339
|
agentStream.mergeIntoDataStream(dataStream);
|
|
356
340
|
},
|
|
357
|
-
onError: (error) => `Custom error: ${error
|
|
341
|
+
onError: (error) => `Custom error: ${error}`
|
|
358
342
|
});
|
|
359
343
|
```
|
|
360
344
|
|
|
361
|
-
###
|
|
345
|
+
### Using `createDataStreamResponse()`
|
|
362
346
|
|
|
363
|
-
The `createDataStreamResponse` function creates a
|
|
347
|
+
The `createDataStreamResponse` function creates a response object that streams data to the client.
|
|
364
348
|
|
|
365
|
-
```typescript filename="app/api/chat/route.ts" copy
|
|
366
|
-
import { mastra } from "
|
|
349
|
+
```typescript {2,9} filename="app/api/chat-stream/route.ts" showLineNumbers copy
|
|
350
|
+
import { mastra } from "../../../mastra";
|
|
351
|
+
import { createDataStreamResponse } from "ai";
|
|
367
352
|
|
|
368
353
|
export async function POST(req: Request) {
|
|
369
354
|
const { messages } = await req.json();
|
|
370
355
|
const myAgent = mastra.getAgent("weatherAgent");
|
|
371
|
-
//mastra agent stream
|
|
372
356
|
const agentStream = await myAgent.stream(messages);
|
|
373
357
|
|
|
374
358
|
const response = createDataStreamResponse({
|
|
375
359
|
status: 200,
|
|
376
360
|
statusText: "OK",
|
|
377
361
|
headers: {
|
|
378
|
-
"Custom-Header": "value"
|
|
362
|
+
"Custom-Header": "value"
|
|
379
363
|
},
|
|
380
364
|
async execute(dataStream) {
|
|
381
|
-
// Write data
|
|
382
365
|
dataStream.writeData({ value: "Hello" });
|
|
383
366
|
|
|
384
|
-
// Write annotation
|
|
385
367
|
dataStream.writeMessageAnnotation({
|
|
386
368
|
type: "status",
|
|
387
|
-
value: "processing"
|
|
369
|
+
value: "processing"
|
|
388
370
|
});
|
|
389
371
|
|
|
390
|
-
// Merge agent stream
|
|
391
372
|
agentStream.mergeIntoDataStream(dataStream);
|
|
392
373
|
},
|
|
393
|
-
onError: (error) => `Custom error: ${error
|
|
374
|
+
onError: (error) => `Custom error: ${error}`
|
|
394
375
|
});
|
|
395
376
|
|
|
396
377
|
return response;
|
|
397
378
|
}
|
|
398
379
|
```
|
|
380
|
+
|
|
381
|
+
## Vercel AI SDK v5
|
|
382
|
+
|
|
383
|
+
This guide covers Mastra-specific considerations when migrating from AI SDK v4 to v5 beta.
|
|
384
|
+
|
|
385
|
+
> Please add any feedback or bug reports to the [AI SDK v5 mega issue in Github.](https://github.com/mastra-ai/mastra/issues/5470)
|
|
386
|
+
|
|
387
|
+
### Official migration guide
|
|
388
|
+
|
|
389
|
+
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.
|
|
390
|
+
|
|
391
|
+
This guide covers only the Mastra-specific aspects of the migration.
|
|
392
|
+
|
|
393
|
+
- **Data compatibility**: New data stored in v5 format will no longer work if you downgrade from the beta
|
|
394
|
+
- **Backup recommendation**: Keep DB backups from before you upgrade to v5 beta
|
|
395
|
+
- **Production use**: Wait for the AI SDK v5 stable release before using in production applications
|
|
396
|
+
- **Prerelease status**: The Mastra `ai-v5` tag is a prerelease version and may have bugs
|
|
397
|
+
|
|
398
|
+
### Memory and Storage
|
|
399
|
+
|
|
400
|
+
Mastra automatically handles AI SDK v4 data using its internal `MessageList` class, which manages format conversion—including v4 to v5. No database migrations are required; your existing messages are translated on the fly and continue working after you upgrade.
|
|
401
|
+
|
|
402
|
+
### Migration strategy
|
|
403
|
+
|
|
404
|
+
Migrating to AI SDK v5 with Mastra involves updating both your **backend** (Mastra server) and **frontend**.
|
|
405
|
+
We provide a compatibility mode to handle stream format conversion during the transition.
|
|
406
|
+
|
|
407
|
+
### Upgrade dependencies
|
|
408
|
+
|
|
409
|
+
Install the `@ai-v5` prerelease version of all Mastra packages:
|
|
410
|
+
|
|
411
|
+
<Tabs items={["npm", "yarn", "pnpm", "bun"]}>
|
|
412
|
+
<Tabs.Tab>
|
|
413
|
+
```bash copy
|
|
414
|
+
npm install mastra@ai-v5 @mastra/core@ai-v5 @mastra/memory@ai-v5
|
|
415
|
+
```
|
|
416
|
+
</Tabs.Tab>
|
|
417
|
+
<Tabs.Tab>
|
|
418
|
+
```bash copy
|
|
419
|
+
yarn add mastra@ai-v5 @mastra/core@ai-v5 @mastra/memory@ai-v5
|
|
420
|
+
```
|
|
421
|
+
</Tabs.Tab>
|
|
422
|
+
<Tabs.Tab>
|
|
423
|
+
```bash copy
|
|
424
|
+
pnpm add mastra@ai-v5 @mastra/core@ai-v5 @mastra/memory@ai-v5
|
|
425
|
+
```
|
|
426
|
+
</Tabs.Tab>
|
|
427
|
+
<Tabs.Tab>
|
|
428
|
+
```bash copy
|
|
429
|
+
bun add mastra@ai-v5 @mastra/core@ai-v5 @mastra/memory@ai-v5
|
|
430
|
+
```
|
|
431
|
+
</Tabs.Tab>
|
|
432
|
+
</Tabs>
|
|
433
|
+
|
|
434
|
+
Configure your Mastra instance with `v4` compatibility so your frontend, and the Mastra Playground will continue to work:
|
|
435
|
+
|
|
436
|
+
```typescript {7} filename="mastra/index.ts" showLineNumbers copy
|
|
437
|
+
import { Mastra } from "@mastra/core/mastra";
|
|
438
|
+
|
|
439
|
+
import { weatherAgent } from "./agents/weather-agent";
|
|
440
|
+
|
|
441
|
+
export const mastra = new Mastra({
|
|
442
|
+
agents: { weatherAgent },
|
|
443
|
+
aiSdkCompat: 'v4',
|
|
444
|
+
});
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
### Enabling stream compatibility
|
|
448
|
+
|
|
449
|
+
If your frontend calls a Mastra agent using a custom API route, wrap the `toUIMessageStreamResponse()` result with `createV4CompatibleResponse` to maintain AI SDK `v4` compatibility.
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
```typescript filename="app/api/chat/route.ts" showLineNumbers copy
|
|
453
|
+
import { mastra } from "../../../mastra";
|
|
454
|
+
import { createV4CompatibleResponse } from "@mastra/core/agent";
|
|
455
|
+
|
|
456
|
+
export async function POST(req: Request) {
|
|
457
|
+
const { messages } = await req.json();
|
|
458
|
+
const myAgent = mastra.getAgent("weatherAgent");
|
|
459
|
+
const stream = await myAgent.stream(messages);
|
|
460
|
+
|
|
461
|
+
return createV4CompatibleResponse(stream.toUIMessageStreamResponse().body!);
|
|
462
|
+
}
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
### Frontend upgrade
|
|
466
|
+
|
|
467
|
+
When you're ready, remove the compatibility flag and upgrade your frontend:
|
|
468
|
+
|
|
469
|
+
1. Remove `aiSdkCompat: 'v4'` from your Mastra configuration
|
|
470
|
+
2. Follow the AI SDK guide on upgrading your frontend dependencies
|
|
471
|
+
3. Update your frontend code for v5 breaking changes
|
|
472
|
+
|
|
473
|
+
|