@mastra/mcp-docs-server 1.0.0-beta.6 → 1.0.0-beta.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.docs/organized/changelogs/%40mastra%2Fastra.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fchroma.md +12 -12
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +33 -33
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +58 -58
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +32 -32
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +32 -32
- package/.docs/organized/changelogs/%40mastra%2Fcodemod.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Fconvex.md +31 -0
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +176 -176
- package/.docs/organized/changelogs/%40mastra%2Fcouchbase.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +15 -15
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +32 -32
- package/.docs/organized/changelogs/%40mastra%2Felasticsearch.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Flance.md +33 -33
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +32 -32
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +12 -12
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +9 -9
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +43 -43
- package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +32 -32
- package/.docs/organized/changelogs/%40mastra%2Fmssql.md +32 -32
- package/.docs/organized/changelogs/%40mastra%2Fopensearch.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +32 -32
- package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +31 -31
- package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Freact.md +7 -0
- package/.docs/organized/changelogs/%40mastra%2Fs3vectors.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +58 -58
- package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +33 -33
- package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +10 -10
- package/.docs/organized/changelogs/create-mastra.md +7 -7
- package/.docs/organized/changelogs/mastra.md +14 -14
- package/.docs/organized/code-examples/ai-elements.md +1 -1
- package/.docs/organized/code-examples/ai-sdk-useChat.md +1 -1
- package/.docs/organized/code-examples/ai-sdk-v5.md +1 -1
- package/.docs/organized/code-examples/assistant-ui.md +1 -1
- package/.docs/organized/code-examples/bird-checker-with-nextjs-and-eval.md +1 -1
- package/.docs/organized/code-examples/bird-checker-with-nextjs.md +1 -1
- package/.docs/organized/code-examples/crypto-chatbot.md +1 -1
- package/.docs/organized/code-examples/mcp-server-adapters.md +2 -2
- package/.docs/organized/code-examples/server-app-access.md +2 -2
- package/.docs/organized/code-examples/server-express-adapter.md +87 -0
- package/.docs/organized/code-examples/server-hono-adapter.md +85 -0
- package/.docs/raw/agents/overview.mdx +2 -111
- package/.docs/raw/agents/processors.mdx +1 -1
- package/.docs/raw/agents/structured-output.mdx +224 -0
- package/.docs/raw/guides/migrations/upgrade-to-v1/workflows.mdx +10 -2
- package/.docs/raw/memory/working-memory.mdx +1 -0
- package/.docs/raw/observability/tracing/bridges/otel.mdx +25 -1
- package/.docs/raw/observability/tracing/exporters/arize.mdx +19 -0
- package/.docs/raw/observability/tracing/exporters/langfuse.mdx +63 -0
- package/.docs/raw/observability/tracing/exporters/otel.mdx +30 -19
- package/.docs/raw/observability/tracing/exporters/posthog.mdx +20 -0
- package/.docs/raw/observability/tracing/overview.mdx +6 -1
- package/.docs/raw/reference/observability/tracing/bridges/otel.mdx +27 -0
- package/.docs/raw/reference/observability/tracing/exporters/arize.mdx +25 -0
- package/.docs/raw/reference/observability/tracing/exporters/langfuse.mdx +43 -0
- package/.docs/raw/reference/observability/tracing/exporters/otel.mdx +27 -43
- package/.docs/raw/reference/server/create-route.mdx +314 -0
- package/.docs/raw/reference/server/express-adapter.mdx +193 -0
- package/.docs/raw/reference/server/hono-adapter.mdx +174 -0
- package/.docs/raw/reference/server/mastra-server.mdx +316 -0
- package/.docs/raw/reference/server/routes.mdx +250 -0
- package/.docs/raw/reference/streaming/workflows/timeTravelStream.mdx +170 -0
- package/.docs/raw/reference/tools/mcp-client.mdx +54 -1
- package/.docs/raw/reference/workflows/run-methods/timeTravel.mdx +310 -0
- package/.docs/raw/reference/workflows/run.mdx +14 -0
- package/.docs/raw/server-db/custom-adapters.mdx +380 -0
- package/.docs/raw/server-db/mastra-server.mdx +11 -32
- package/.docs/raw/server-db/server-adapters.mdx +286 -0
- package/CHANGELOG.md +8 -0
- package/package.json +4 -4
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
### package.json
|
|
2
|
+
```json
|
|
3
|
+
{
|
|
4
|
+
"name": "examples-server-express-adapter",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"@ai-sdk/openai": "latest",
|
|
7
|
+
"@mastra/core": "latest",
|
|
8
|
+
"@mastra/express": "latest",
|
|
9
|
+
"express": "^5.1.0",
|
|
10
|
+
"zod": "^3.25.76"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@types/express": "^5.0.5",
|
|
14
|
+
"@types/node": "22.13.17",
|
|
15
|
+
"tsx": "^4.19.3",
|
|
16
|
+
"typescript": "^5.8.3"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### mastra/agents/assistant.ts
|
|
22
|
+
```typescript
|
|
23
|
+
import { Agent } from '@mastra/core/agent';
|
|
24
|
+
import { openai } from '@ai-sdk/openai';
|
|
25
|
+
|
|
26
|
+
export const assistantAgent = new Agent({
|
|
27
|
+
id: 'assistantAgent',
|
|
28
|
+
name: 'Assistant',
|
|
29
|
+
instructions: 'You are a helpful assistant. Answer questions concisely.',
|
|
30
|
+
model: openai('gpt-4o-mini'),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### mastra/index.ts
|
|
36
|
+
```typescript
|
|
37
|
+
import { Mastra } from '@mastra/core/mastra';
|
|
38
|
+
|
|
39
|
+
import { assistantAgent } from './agents/assistant';
|
|
40
|
+
|
|
41
|
+
export const mastra = new Mastra({
|
|
42
|
+
agents: { assistantAgent },
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### server.ts
|
|
48
|
+
```typescript
|
|
49
|
+
/**
|
|
50
|
+
* Express Server Adapter Example
|
|
51
|
+
*
|
|
52
|
+
* This demonstrates how to use @mastra/express to run Mastra with Express.
|
|
53
|
+
*
|
|
54
|
+
* Features shown:
|
|
55
|
+
* - Basic server setup with MastraServer
|
|
56
|
+
* - Required express.json() middleware
|
|
57
|
+
* - Custom routes added after init()
|
|
58
|
+
* - Accessing Mastra context via res.locals
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
import express from 'express';
|
|
62
|
+
import { MastraServer } from '@mastra/express';
|
|
63
|
+
|
|
64
|
+
import { mastra } from './mastra';
|
|
65
|
+
|
|
66
|
+
const app = express();
|
|
67
|
+
app.use(express.json()); // Required for body parsing
|
|
68
|
+
|
|
69
|
+
const server = new MastraServer({ app, mastra });
|
|
70
|
+
|
|
71
|
+
await server.init();
|
|
72
|
+
|
|
73
|
+
// Custom route with access to Mastra context via res.locals
|
|
74
|
+
app.get('/health', (req, res) => {
|
|
75
|
+
const mastraInstance = res.locals.mastra;
|
|
76
|
+
const agents = Object.keys(mastraInstance.listAgents());
|
|
77
|
+
res.json({ status: 'ok', agents });
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const port = 4111;
|
|
81
|
+
|
|
82
|
+
app.listen(port, () => {
|
|
83
|
+
console.log(`Server running on http://localhost:${port}`);
|
|
84
|
+
console.log(`Try: curl http://localhost:${port}/api/agents`);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
```
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
### package.json
|
|
2
|
+
```json
|
|
3
|
+
{
|
|
4
|
+
"name": "examples-server-hono-adapter",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"@ai-sdk/openai": "latest",
|
|
7
|
+
"@mastra/core": "latest",
|
|
8
|
+
"@mastra/hono": "latest",
|
|
9
|
+
"@hono/node-server": "^1.14.3",
|
|
10
|
+
"hono": "^4.10.4",
|
|
11
|
+
"zod": "^3.25.76"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"@types/node": "22.13.17",
|
|
15
|
+
"tsx": "^4.19.3",
|
|
16
|
+
"typescript": "^5.8.3"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### mastra/agents/assistant.ts
|
|
22
|
+
```typescript
|
|
23
|
+
import { Agent } from '@mastra/core/agent';
|
|
24
|
+
import { openai } from '@ai-sdk/openai';
|
|
25
|
+
|
|
26
|
+
export const assistantAgent = new Agent({
|
|
27
|
+
id: 'assistantAgent',
|
|
28
|
+
name: 'Assistant',
|
|
29
|
+
instructions: 'You are a helpful assistant. Answer questions concisely.',
|
|
30
|
+
model: openai('gpt-4o-mini'),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### mastra/index.ts
|
|
36
|
+
```typescript
|
|
37
|
+
import { Mastra } from '@mastra/core/mastra';
|
|
38
|
+
|
|
39
|
+
import { assistantAgent } from './agents/assistant';
|
|
40
|
+
|
|
41
|
+
export const mastra = new Mastra({
|
|
42
|
+
agents: { assistantAgent },
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### server.ts
|
|
48
|
+
```typescript
|
|
49
|
+
/**
|
|
50
|
+
* Hono Server Adapter Example
|
|
51
|
+
*
|
|
52
|
+
* This demonstrates how to use @mastra/hono to run Mastra with Hono.
|
|
53
|
+
*
|
|
54
|
+
* Features shown:
|
|
55
|
+
* - Basic server setup with MastraServer
|
|
56
|
+
* - Custom routes added after init()
|
|
57
|
+
* - Accessing Mastra context in custom routes
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
import { serve } from '@hono/node-server';
|
|
61
|
+
import { Hono } from 'hono';
|
|
62
|
+
import { HonoBindings, HonoVariables, MastraServer } from '@mastra/hono';
|
|
63
|
+
|
|
64
|
+
import { mastra } from './mastra';
|
|
65
|
+
|
|
66
|
+
const app = new Hono<{ Bindings: HonoBindings; Variables: HonoVariables }>();
|
|
67
|
+
const server = new MastraServer({ app: app as any, mastra });
|
|
68
|
+
|
|
69
|
+
await server.init();
|
|
70
|
+
|
|
71
|
+
// Custom route with access to Mastra context
|
|
72
|
+
app.get('/health', c => {
|
|
73
|
+
const mastraInstance = c.get('mastra');
|
|
74
|
+
const agents = Object.keys(mastraInstance.listAgents());
|
|
75
|
+
return c.json({ status: 'ok', agents });
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const port = 4111;
|
|
79
|
+
|
|
80
|
+
serve({ fetch: app.fetch, port }, () => {
|
|
81
|
+
console.log(`Server running on http://localhost:${port}`);
|
|
82
|
+
console.log(`Try: curl http://localhost:${port}/api/agents`);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
```
|
|
@@ -212,118 +212,9 @@ for await (const chunk of stream.textStream) {
|
|
|
212
212
|
|
|
213
213
|
## Structured output
|
|
214
214
|
|
|
215
|
-
Agents can return structured, type-safe data
|
|
215
|
+
Agents can return structured, type-safe data using Zod or JSON Schema. The parsed result is available on `response.object`.
|
|
216
216
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
Define the `output` shape using [Zod](https://zod.dev/):
|
|
220
|
-
|
|
221
|
-
```typescript showLineNumbers copy
|
|
222
|
-
import { z } from "zod";
|
|
223
|
-
|
|
224
|
-
const response = await testAgent.generate(
|
|
225
|
-
[
|
|
226
|
-
{
|
|
227
|
-
role: "system",
|
|
228
|
-
content: "Provide a summary and keywords for the following text:",
|
|
229
|
-
},
|
|
230
|
-
{
|
|
231
|
-
role: "user",
|
|
232
|
-
content: "Monkey, Ice Cream, Boat",
|
|
233
|
-
},
|
|
234
|
-
],
|
|
235
|
-
{
|
|
236
|
-
structuredOutput: {
|
|
237
|
-
schema: z.object({
|
|
238
|
-
summary: z.string(),
|
|
239
|
-
keywords: z.array(z.string()),
|
|
240
|
-
}),
|
|
241
|
-
},
|
|
242
|
-
},
|
|
243
|
-
);
|
|
244
|
-
|
|
245
|
-
console.log(response.object);
|
|
246
|
-
```
|
|
247
|
-
|
|
248
|
-
### Structuring sub agent
|
|
249
|
-
|
|
250
|
-
Use the `model` property to have a separate agent generate the structured output for you.
|
|
251
|
-
|
|
252
|
-
```typescript showLineNumbers copy
|
|
253
|
-
import { z } from "zod";
|
|
254
|
-
|
|
255
|
-
const response = await testAgentWithTools.generate(
|
|
256
|
-
[
|
|
257
|
-
{
|
|
258
|
-
role: "system",
|
|
259
|
-
content: "Provide a summary and keywords for the following text:",
|
|
260
|
-
},
|
|
261
|
-
{
|
|
262
|
-
role: "user",
|
|
263
|
-
content: "Please use your test tool and let me know the results",
|
|
264
|
-
},
|
|
265
|
-
],
|
|
266
|
-
{
|
|
267
|
-
structuredOutput: {
|
|
268
|
-
schema: z.object({
|
|
269
|
-
summary: z.string(),
|
|
270
|
-
keywords: z.array(z.string()),
|
|
271
|
-
}),
|
|
272
|
-
model: "openai/gpt-5.1",
|
|
273
|
-
},
|
|
274
|
-
},
|
|
275
|
-
);
|
|
276
|
-
|
|
277
|
-
console.log(response.object);
|
|
278
|
-
console.log(response.toolResults);
|
|
279
|
-
```
|
|
280
|
-
|
|
281
|
-
### Response format
|
|
282
|
-
|
|
283
|
-
By default `structuredOutput` will use `response_format` to pass the schema to the model provider. If the model provider does not natively support `response_format` it's possible that this will error or not give the desired results. To keep using the same model use `jsonPromptInjection` to bypass response format and inject a system prompt message to coerce the model to return structured output.
|
|
284
|
-
|
|
285
|
-
```typescript showLineNumbers copy
|
|
286
|
-
import { z } from "zod";
|
|
287
|
-
|
|
288
|
-
const response = await testAgentThatDoesntSupportStructuredOutput.generate(
|
|
289
|
-
[
|
|
290
|
-
{
|
|
291
|
-
role: "system",
|
|
292
|
-
content: "Provide a summary and keywords for the following text:",
|
|
293
|
-
},
|
|
294
|
-
{
|
|
295
|
-
role: "user",
|
|
296
|
-
content: "Monkey, Ice Cream, Boat",
|
|
297
|
-
},
|
|
298
|
-
],
|
|
299
|
-
{
|
|
300
|
-
structuredOutput: {
|
|
301
|
-
schema: z.object({
|
|
302
|
-
summary: z.string(),
|
|
303
|
-
keywords: z.array(z.string()),
|
|
304
|
-
}),
|
|
305
|
-
jsonPromptInjection: true,
|
|
306
|
-
},
|
|
307
|
-
},
|
|
308
|
-
);
|
|
309
|
-
|
|
310
|
-
console.log(response.object);
|
|
311
|
-
```
|
|
312
|
-
|
|
313
|
-
:::info[Gemini 2.5 with tools]
|
|
314
|
-
|
|
315
|
-
Gemini 2.5 models do not support combining `response_format` (structured output) with function calling (tools) in the same API call. If your agent has tools and you're using `structuredOutput` with a Gemini 2.5 model, you must set `jsonPromptInjection: true` to avoid the error `Function calling with a response mime type: 'application/json' is unsupported`.
|
|
316
|
-
|
|
317
|
-
```typescript
|
|
318
|
-
const response = await agentWithTools.generate("Your prompt", {
|
|
319
|
-
structuredOutput: {
|
|
320
|
-
schema: yourSchema,
|
|
321
|
-
jsonPromptInjection: true, // Required for Gemini 2.5 when tools are present
|
|
322
|
-
},
|
|
323
|
-
});
|
|
324
|
-
```
|
|
325
|
-
|
|
326
|
-
:::
|
|
217
|
+
> See [Structured Output](/docs/v1/agents/structured-output) for more information.
|
|
327
218
|
|
|
328
219
|
## Analyzing images
|
|
329
220
|
|
|
@@ -276,4 +276,4 @@ const agent = new Agent({
|
|
|
276
276
|
## Related documentation
|
|
277
277
|
|
|
278
278
|
- [Guardrails](/docs/v1/agents/guardrails) - Security and validation processors
|
|
279
|
-
- [Memory Processors](/docs/v1/memory/memory-processors) - Memory-specific processors and automatic integration
|
|
279
|
+
- [Memory Processors](/docs/v1/memory/memory-processors) - Memory-specific processors and automatic integration
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Structured Output | Agents"
|
|
3
|
+
description: "Learn how to generate structured data from agents using schemas and validation."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import Tabs from "@theme/Tabs";
|
|
7
|
+
import TabItem from "@theme/TabItem";
|
|
8
|
+
|
|
9
|
+
# Structured Output
|
|
10
|
+
|
|
11
|
+
Structured output lets an agent return an object that matches the shape defined by a schema instead of returning text. The schema tells the model what fields to produce, and the model ensures the final result fits that shape.
|
|
12
|
+
|
|
13
|
+
## When to use structured output
|
|
14
|
+
|
|
15
|
+
Use structured output when you need an agent to return a data object rather than text. Having well defined fields can make it simpler to pull out the values you need for API calls, UI rendering, or application logic.
|
|
16
|
+
|
|
17
|
+
## Defining schemas
|
|
18
|
+
|
|
19
|
+
Agents can return structured data by defining the expected output with either [Zod](https://zod.dev/) or [JSON Schema](https://json-schema.org/). Zod is recommended because it provides TypeScript type inference and runtime validation, while JSON Schema is useful when you need a language agnostic format.
|
|
20
|
+
|
|
21
|
+
<Tabs>
|
|
22
|
+
<TabItem value="zod" label="Zod">
|
|
23
|
+
|
|
24
|
+
Define the `output` shape using [Zod](https://zod.dev/):
|
|
25
|
+
|
|
26
|
+
```typescript showLineNumbers copy
|
|
27
|
+
import { z } from "zod";
|
|
28
|
+
|
|
29
|
+
const response = await testAgent.generate("Help me plan my day.", {
|
|
30
|
+
structuredOutput: {
|
|
31
|
+
schema: z.array(
|
|
32
|
+
z.object({
|
|
33
|
+
name: z.string(),
|
|
34
|
+
activities: z.array(z.string()),
|
|
35
|
+
}),
|
|
36
|
+
),
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
console.log(response.object);
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
</TabItem>
|
|
44
|
+
<TabItem value="json-schema" label="JSON Schema">
|
|
45
|
+
|
|
46
|
+
You can also use JSON Schema to define your output structure:
|
|
47
|
+
|
|
48
|
+
```typescript showLineNumbers copy
|
|
49
|
+
const response = await testAgent.generate("Help me plan my day.", {
|
|
50
|
+
structuredOutput: {
|
|
51
|
+
schema: {
|
|
52
|
+
type: "array",
|
|
53
|
+
items: {
|
|
54
|
+
type: "object",
|
|
55
|
+
properties: {
|
|
56
|
+
name: { type: "string" },
|
|
57
|
+
activities: {
|
|
58
|
+
type: "array",
|
|
59
|
+
items: { type: "string" },
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
required: ["name", "activities"],
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
console.log(response.object);
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
</TabItem>
|
|
72
|
+
</Tabs>
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
> See [.generate()](/reference/v1/agents/generate#structuredoutput) for a full list of configuration options.
|
|
76
|
+
|
|
77
|
+
### Example output
|
|
78
|
+
|
|
79
|
+
The `response.object` will contain the structured data as defined by the schema.
|
|
80
|
+
|
|
81
|
+
```json
|
|
82
|
+
[
|
|
83
|
+
{
|
|
84
|
+
"name": "Morning Routine",
|
|
85
|
+
"activities": ["Wake up at 7am", "Exercise", "Shower", "Breakfast"]
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
"name": "Work",
|
|
89
|
+
"activities": ["Check emails", "Team meeting", "Lunch break"]
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
"name": "Evening",
|
|
93
|
+
"activities": ["Dinner", "Relax", "Read a book", "Sleep by 10pm"]
|
|
94
|
+
}
|
|
95
|
+
]
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Streaming
|
|
99
|
+
|
|
100
|
+
Streaming also supports structured output. The final structured object is available on `stream.fullStream` and after the stream completes on `stream.object`. Text stream chunks are still emitted, but they contain natural language text rather than structured data.
|
|
101
|
+
|
|
102
|
+
```typescript showLineNumbers copy
|
|
103
|
+
import { z } from "zod";
|
|
104
|
+
|
|
105
|
+
const stream = await testAgent.stream("Help me plan my day.", {
|
|
106
|
+
structuredOutput: {
|
|
107
|
+
schema: z.array(
|
|
108
|
+
z.object({
|
|
109
|
+
name: z.string(),
|
|
110
|
+
activities: z.array(z.string())
|
|
111
|
+
})
|
|
112
|
+
),
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
for await (const chunk of stream.fullStream) {
|
|
117
|
+
if (chunk.type === "object-result") {
|
|
118
|
+
console.log("\n", JSON.stringify(chunk, null, 2));
|
|
119
|
+
}
|
|
120
|
+
process.stdout.write(JSON.stringify(chunk));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
console.log(await stream.object)
|
|
124
|
+
|
|
125
|
+
for await (const chunk of stream.textStream) {
|
|
126
|
+
process.stdout.write(chunk);
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Structuring agent
|
|
131
|
+
|
|
132
|
+
When your main agent isn't proficient at creating structured output you can provide a `model` to `structuredOutput`. In this case, Mastra uses a second agent under the hood to extract structured data from the main agent's natural language response. This makes two LLM calls, one to generate the response and another to turn that response into the structured object, which adds some latency and cost but can improve accuracy for complex structuring tasks.
|
|
133
|
+
|
|
134
|
+
```typescript showLineNumbers copy
|
|
135
|
+
import { z } from "zod";
|
|
136
|
+
|
|
137
|
+
const response = await testAgent.generate("Analyze the TypeScript programming language.", {
|
|
138
|
+
structuredOutput: {
|
|
139
|
+
schema: z.object({
|
|
140
|
+
overview: z.string(),
|
|
141
|
+
strengths: z.array(z.string()),
|
|
142
|
+
weaknesses: z.array(z.string()),
|
|
143
|
+
useCases: z.array(z.object({
|
|
144
|
+
scenario: z.string(),
|
|
145
|
+
reasoning: z.string(),
|
|
146
|
+
})),
|
|
147
|
+
comparison: z.object({
|
|
148
|
+
similarTo: z.array(z.string()),
|
|
149
|
+
differentiators: z.array(z.string()),
|
|
150
|
+
}),
|
|
151
|
+
}),
|
|
152
|
+
model: "openai/gpt-4o",
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
console.log(response.object);
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Response format
|
|
160
|
+
|
|
161
|
+
By default, Mastra passes the schema to the model provider using the `response_format` API parameter. Most model providers have built-in support for this, which reliably enforces the schema.
|
|
162
|
+
|
|
163
|
+
If your model provider doesn't support `response_format`, you'll get an error from the API. When this happens, set `jsonPromptInjection: true`. This adds the schema to the system prompt instead, instructing the model to output JSON. This is less reliable than the API parameter approach.
|
|
164
|
+
|
|
165
|
+
```typescript showLineNumbers copy
|
|
166
|
+
import { z } from "zod";
|
|
167
|
+
|
|
168
|
+
const response = await testAgent.generate("Help me plan my day.", {
|
|
169
|
+
structuredOutput: {
|
|
170
|
+
schema: z.array(
|
|
171
|
+
z.object({
|
|
172
|
+
name: z.string(),
|
|
173
|
+
activities: z.array(z.string()),
|
|
174
|
+
}),
|
|
175
|
+
),
|
|
176
|
+
jsonPromptInjection: true,
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
console.log(response.object);
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
:::info[Gemini 2.5 with tools]
|
|
184
|
+
|
|
185
|
+
Gemini 2.5 models do not support combining `response_format` (structured output) with function calling (tools) in the same API call. If your agent has tools and you're using `structuredOutput` with a Gemini 2.5 model, you must set `jsonPromptInjection: true` to avoid the error `Function calling with a response mime type: 'application/json' is unsupported`.
|
|
186
|
+
|
|
187
|
+
```typescript
|
|
188
|
+
const response = await agentWithTools.generate("Your prompt", {
|
|
189
|
+
structuredOutput: {
|
|
190
|
+
schema: yourSchema,
|
|
191
|
+
jsonPromptInjection: true, // Required for Gemini 2.5 when tools are present
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
```
|
|
195
|
+
:::
|
|
196
|
+
|
|
197
|
+
## Error handling
|
|
198
|
+
|
|
199
|
+
When schema validation fails, you can control how errors are handled using `errorStrategy`. The default `strict` strategy throws an error, while `warn` logs a warning and continues. The `fallback` strategy returns the values provided using `fallbackValue`.
|
|
200
|
+
|
|
201
|
+
```typescript showLineNumbers copy
|
|
202
|
+
import { z } from "zod";
|
|
203
|
+
|
|
204
|
+
const response = await testAgent.generate("Tell me about TypeScript.", {
|
|
205
|
+
structuredOutput: {
|
|
206
|
+
schema: z.object({
|
|
207
|
+
summary: z.string(),
|
|
208
|
+
keyFeatures: z.array(z.string())
|
|
209
|
+
}),
|
|
210
|
+
errorStrategy: "fallback",
|
|
211
|
+
fallbackValue: {
|
|
212
|
+
summary: "TypeScript is a typed superset of JavaScript",
|
|
213
|
+
keyFeatures: ["Static typing", "Compiles to JavaScript", "Better tooling"]
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
console.log(response.object);
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## Related
|
|
222
|
+
|
|
223
|
+
- [Using Tools](/docs/v1/agents/using-tools)
|
|
224
|
+
- [Agent Memory](/docs/v1/agents/agent-memory)
|
|
@@ -177,8 +177,6 @@ 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
|
-
See [Run.resumeStream()](/reference/v1/streaming/workflows/resumeStream) for details.
|
|
181
|
-
|
|
182
180
|
## Removed
|
|
183
181
|
|
|
184
182
|
### `streamVNext`, `resumeStreamVNext`, and `observeStreamVNext` methods
|
|
@@ -189,6 +187,16 @@ To migrate, use the standard `stream()`, `resumeStream()`, and `observeStream()`
|
|
|
189
187
|
|
|
190
188
|
See [`Run.stream()`](/reference/v1/streaming/workflows/stream), [`Run.resumeStream()`](/reference/v1/streaming/workflows/resumeStream), and [`Run.observeStream()`](/reference/v1/streaming/workflows/observeStream) for details.
|
|
191
189
|
|
|
190
|
+
:::tip[Codemod]
|
|
191
|
+
|
|
192
|
+
You can use Mastra's codemod CLI to update your code automatically:
|
|
193
|
+
|
|
194
|
+
```shell
|
|
195
|
+
npx @mastra/codemod@beta v1/workflow-stream-vnext .
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
:::
|
|
199
|
+
|
|
192
200
|
### Legacy workflows export
|
|
193
201
|
|
|
194
202
|
The `./workflows/legacy` export path has been removed from `@mastra/core`. Legacy workflows are no longer supported.
|
|
@@ -131,6 +131,7 @@ Resource-scoped working memory requires specific storage adapters that support t
|
|
|
131
131
|
- **LibSQL** (`@mastra/libsql`)
|
|
132
132
|
- **PostgreSQL** (`@mastra/pg`)
|
|
133
133
|
- **Upstash** (`@mastra/upstash`)
|
|
134
|
+
- **MongoDB** (`@mastra/mongodb`)
|
|
134
135
|
|
|
135
136
|
## Custom Templates
|
|
136
137
|
|
|
@@ -129,6 +129,12 @@ Use the `--import` flag to ensure instrumentation loads before your application:
|
|
|
129
129
|
tsx --import ./instrumentation.ts ./src/index.ts
|
|
130
130
|
```
|
|
131
131
|
|
|
132
|
+
## Semantic Conventions
|
|
133
|
+
|
|
134
|
+
The OtelBridge exports Mastra spans using [OpenTelemetry Semantic Conventions for GenAI v1.38.0](https://github.com/open-telemetry/semantic-conventions/tree/v1.38.0/docs/gen-ai). This includes standardized span names (`chat {model}`, `execute_tool {tool_name}`, etc.) and attributes (`gen_ai.usage.input_tokens`, `gen_ai.request.model`, etc.).
|
|
135
|
+
|
|
136
|
+
For details on span naming and attributes, see the [OpenTelemetry Exporter semantic conventions](/docs/v1/observability/tracing/exporters/otel#opentelemetry-semantic-conventions).
|
|
137
|
+
|
|
132
138
|
## Trace Hierarchy
|
|
133
139
|
|
|
134
140
|
With the OtelBridge, your traces maintain proper hierarchy across OTEL and Mastra boundaries:
|
|
@@ -160,6 +166,25 @@ Both services must have:
|
|
|
160
166
|
2. W3C Trace Context propagator enabled
|
|
161
167
|
3. Mastra with OtelBridge configured
|
|
162
168
|
|
|
169
|
+
## Using Tags
|
|
170
|
+
|
|
171
|
+
Tags help you categorize and filter traces in your OTEL backend. Add tags when executing agents or workflows:
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
const result = await agent.generate({
|
|
175
|
+
messages: [{ role: "user", content: "Hello" }],
|
|
176
|
+
tracingOptions: {
|
|
177
|
+
tags: ["production", "experiment-v2", "user-request"],
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Tags are exported as a JSON string in the `mastra.tags` span attribute for broad backend compatibility. Common use cases include:
|
|
183
|
+
|
|
184
|
+
- Environment labels: `"production"`, `"staging"`
|
|
185
|
+
- Experiment tracking: `"experiment-v1"`, `"control-group"`
|
|
186
|
+
- Priority levels: `"priority-high"`, `"batch-job"`
|
|
187
|
+
|
|
163
188
|
## Troubleshooting
|
|
164
189
|
|
|
165
190
|
If traces aren't appearing or connecting as expected:
|
|
@@ -173,4 +198,3 @@ If traces aren't appearing or connecting as expected:
|
|
|
173
198
|
- [Tracing Overview](/docs/v1/observability/tracing/overview)
|
|
174
199
|
- [OpenTelemetry Exporter](/docs/v1/observability/tracing/exporters/otel) - For sending traces to OTEL backends
|
|
175
200
|
- [OtelBridge Reference](/reference/v1/observability/tracing/bridges/otel) - API documentation
|
|
176
|
-
- [OpenTelemetry GenAI Conventions](https://opentelemetry.io/docs/specs/semconv/gen-ai/)
|
|
@@ -203,6 +203,25 @@ Reserved fields such as `input`, `output`, `sessionId`, thread/user IDs, and Ope
|
|
|
203
203
|
|
|
204
204
|
This exporter implements the [OpenInference Semantic Conventions](https://github.com/Arize-ai/openinference/tree/main/spec) for generative AI applications, providing standardized trace structure across different observability platforms.
|
|
205
205
|
|
|
206
|
+
## Using Tags
|
|
207
|
+
|
|
208
|
+
Tags help you categorize and filter traces in Phoenix and Arize AX. Add tags when executing agents or workflows:
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
const result = await agent.generate({
|
|
212
|
+
messages: [{ role: "user", content: "Hello" }],
|
|
213
|
+
tracingOptions: {
|
|
214
|
+
tags: ["production", "experiment-v2", "user-request"],
|
|
215
|
+
},
|
|
216
|
+
});
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Tags appear as the `tag.tags` attribute following OpenInference conventions and can be used to filter and search traces. Common use cases include:
|
|
220
|
+
|
|
221
|
+
- Environment labels: `"production"`, `"staging"`
|
|
222
|
+
- Experiment tracking: `"experiment-v1"`, `"control-group"`
|
|
223
|
+
- Priority levels: `"priority-high"`, `"batch-job"`
|
|
224
|
+
|
|
206
225
|
## Related
|
|
207
226
|
|
|
208
227
|
- [Tracing Overview](/docs/v1/observability/tracing/overview)
|