@mastra/mcp-docs-server 0.13.27 → 0.13.28-alpha.2

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.
Files changed (103) hide show
  1. package/.docs/organized/changelogs/%40internal%2Fplayground.md +3 -1
  2. package/.docs/organized/changelogs/%40internal%2Fstorage-test-utils.md +10 -10
  3. package/.docs/organized/changelogs/%40internal%2Ftypes-builder.md +4 -0
  4. package/.docs/organized/changelogs/%40mastra%2Fagent-builder.md +21 -21
  5. package/.docs/organized/changelogs/%40mastra%2Fai-sdk.md +20 -0
  6. package/.docs/organized/changelogs/%40mastra%2Fastra.md +10 -10
  7. package/.docs/organized/changelogs/%40mastra%2Fchroma.md +11 -11
  8. package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +11 -11
  9. package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +41 -41
  10. package/.docs/organized/changelogs/%40mastra%2Fcloud.md +10 -10
  11. package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +10 -10
  12. package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +10 -10
  13. package/.docs/organized/changelogs/%40mastra%2Fcore.md +97 -97
  14. package/.docs/organized/changelogs/%40mastra%2Fcouchbase.md +10 -10
  15. package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +44 -44
  16. package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +31 -31
  17. package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +29 -29
  18. package/.docs/organized/changelogs/%40mastra%2Fdeployer-vercel.md +31 -31
  19. package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +75 -75
  20. package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +27 -27
  21. package/.docs/organized/changelogs/%40mastra%2Fevals.md +10 -10
  22. package/.docs/organized/changelogs/%40mastra%2Flance.md +10 -10
  23. package/.docs/organized/changelogs/%40mastra%2Flibsql.md +10 -10
  24. package/.docs/organized/changelogs/%40mastra%2Floggers.md +10 -10
  25. package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +33 -33
  26. package/.docs/organized/changelogs/%40mastra%2Fmcp-registry-registry.md +10 -10
  27. package/.docs/organized/changelogs/%40mastra%2Fmcp.md +21 -21
  28. package/.docs/organized/changelogs/%40mastra%2Fmemory.md +19 -19
  29. package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +10 -10
  30. package/.docs/organized/changelogs/%40mastra%2Fmssql.md +10 -10
  31. package/.docs/organized/changelogs/%40mastra%2Fopensearch.md +10 -10
  32. package/.docs/organized/changelogs/%40mastra%2Fpg.md +19 -19
  33. package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +10 -10
  34. package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +105 -105
  35. package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +11 -11
  36. package/.docs/organized/changelogs/%40mastra%2Frag.md +13 -13
  37. package/.docs/organized/changelogs/%40mastra%2Freact.md +40 -0
  38. package/.docs/organized/changelogs/%40mastra%2Fs3vectors.md +21 -0
  39. package/.docs/organized/changelogs/%40mastra%2Fserver.md +41 -41
  40. package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +10 -10
  41. package/.docs/organized/changelogs/%40mastra%2Fupstash.md +10 -10
  42. package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +10 -10
  43. package/.docs/organized/changelogs/%40mastra%2Fvoice-azure.md +10 -10
  44. package/.docs/organized/changelogs/%40mastra%2Fvoice-cloudflare.md +11 -11
  45. package/.docs/organized/changelogs/%40mastra%2Fvoice-deepgram.md +11 -11
  46. package/.docs/organized/changelogs/%40mastra%2Fvoice-elevenlabs.md +11 -11
  47. package/.docs/organized/changelogs/%40mastra%2Fvoice-gladia.md +10 -10
  48. package/.docs/organized/changelogs/%40mastra%2Fvoice-google-gemini-live.md +9 -0
  49. package/.docs/organized/changelogs/%40mastra%2Fvoice-google.md +13 -13
  50. package/.docs/organized/changelogs/%40mastra%2Fvoice-murf.md +10 -10
  51. package/.docs/organized/changelogs/%40mastra%2Fvoice-openai-realtime.md +10 -10
  52. package/.docs/organized/changelogs/%40mastra%2Fvoice-openai.md +10 -10
  53. package/.docs/organized/changelogs/%40mastra%2Fvoice-playai.md +11 -11
  54. package/.docs/organized/changelogs/%40mastra%2Fvoice-sarvam.md +11 -11
  55. package/.docs/organized/changelogs/%40mastra%2Fvoice-speechify.md +10 -10
  56. package/.docs/organized/changelogs/create-mastra.md +79 -79
  57. package/.docs/organized/changelogs/mastra.md +118 -118
  58. package/.docs/organized/code-examples/agent.md +2 -1
  59. package/.docs/organized/code-examples/heads-up-game.md +5 -5
  60. package/.docs/raw/agents/guardrails.mdx +335 -0
  61. package/.docs/raw/{networks-vnext/complex-task-execution.mdx → agents/networks.mdx} +29 -9
  62. package/.docs/raw/agents/overview.mdx +107 -63
  63. package/.docs/raw/agents/runtime-context.mdx +11 -16
  64. package/.docs/raw/agents/using-tools-and-mcp.mdx +1 -1
  65. package/.docs/raw/frameworks/agentic-uis/assistant-ui.mdx +9 -2
  66. package/.docs/raw/getting-started/mcp-docs-server.mdx +84 -179
  67. package/.docs/raw/getting-started/model-providers.mdx +5 -3
  68. package/.docs/raw/reference/agents/network.mdx +1 -1
  69. package/.docs/raw/reference/cli/create-mastra.mdx +61 -5
  70. package/.docs/raw/reference/cli/mastra.mdx +252 -0
  71. package/.docs/raw/reference/client-js/agents.mdx +1 -10
  72. package/.docs/raw/reference/processors/batch-parts-processor.mdx +111 -0
  73. package/.docs/raw/reference/processors/language-detector.mdx +154 -0
  74. package/.docs/raw/reference/processors/moderation-processor.mdx +145 -0
  75. package/.docs/raw/reference/processors/pii-detector.mdx +153 -0
  76. package/.docs/raw/reference/processors/prompt-injection-detector.mdx +130 -0
  77. package/.docs/raw/reference/processors/system-prompt-scrubber.mdx +145 -0
  78. package/.docs/raw/reference/processors/token-limiter-processor.mdx +136 -0
  79. package/.docs/raw/reference/processors/unicode-normalizer.mdx +114 -0
  80. package/.docs/raw/reference/streaming/ChunkType.mdx +2 -6
  81. package/.docs/raw/reference/streaming/agents/MastraModelOutput.mdx +1 -5
  82. package/.docs/raw/reference/streaming/workflows/resumeStreamVNext.mdx +1 -1
  83. package/.docs/raw/reference/streaming/workflows/stream.mdx +1 -1
  84. package/.docs/raw/reference/streaming/workflows/streamVNext.mdx +1 -1
  85. package/.docs/raw/reference/workflows/run-methods/resume.mdx +17 -1
  86. package/.docs/raw/reference/workflows/run-methods/start.mdx +17 -1
  87. package/.docs/raw/reference/workflows/step.mdx +11 -0
  88. package/.docs/raw/reference/workflows/workflow.mdx +7 -1
  89. package/.docs/raw/server-db/local-dev-playground.mdx +1 -1
  90. package/.docs/raw/workflows/overview.mdx +22 -5
  91. package/CHANGELOG.md +24 -0
  92. package/package.json +5 -5
  93. package/.docs/raw/agents/input-processors.mdx +0 -284
  94. package/.docs/raw/agents/output-processors.mdx +0 -328
  95. package/.docs/raw/networks-vnext/overview.mdx +0 -85
  96. package/.docs/raw/networks-vnext/single-task-execution.mdx +0 -135
  97. package/.docs/raw/reference/cli/build.mdx +0 -115
  98. package/.docs/raw/reference/cli/dev.mdx +0 -249
  99. package/.docs/raw/reference/cli/init.mdx +0 -97
  100. package/.docs/raw/reference/cli/lint.mdx +0 -56
  101. package/.docs/raw/reference/cli/mcp-docs-server.mdx +0 -82
  102. package/.docs/raw/reference/cli/scorers.mdx +0 -160
  103. package/.docs/raw/reference/cli/start.mdx +0 -50
@@ -0,0 +1,335 @@
1
+ ---
2
+ title: "Guardrails | Agents | Mastra Docs"
3
+ description: "Learn how to implement guardrails using input and output processors to secure and control AI interactions."
4
+ ---
5
+
6
+ # Guardrails
7
+
8
+ Agents use processors to apply guardrails to inputs and outputs. They run before or after each interaction, giving you a way to review, transform, or block information as it passes between the user and the agent.
9
+
10
+ Processors can be configured as:
11
+
12
+ - **`inputProcessors`**: Applied before messages reach the language model.
13
+ - **`outputProcessors`**: Applied to responses before they're returned to users.
14
+
15
+ Some processors are *hybrid*, meaning they can be used with either `inputProcessors` or `outputProcessors`, depending on where the logic should be applied.
16
+
17
+ ## When to use processors
18
+
19
+ Use processors for content moderation, prompt injection prevention, response sanitization, message transformation, and other security-related controls. Mastra provides several built-in input and output processors for common use cases.
20
+
21
+ ## Adding processors to an agent
22
+
23
+ Import and instantiate the relevant processor class, and pass it to your agent’s configuration using either the `inputProcessors` or `outputProcessors` parameter:
24
+
25
+ ```typescript {3,9-17} filename="src/mastra/agents/moderated-agent.ts" showLineNumbers copy
26
+ import { openai } from "@ai-sdk/openai";
27
+ import { Agent } from "@mastra/core/agent";
28
+ import { ModerationProcessor } from "@mastra/core/processors";
29
+
30
+ export const moderatedAgent = new Agent({
31
+ name: "moderated-agent",
32
+ instructions: "You are a helpful assistant",
33
+ model: openai("gpt-4o-mini"),
34
+ inputProcessors: [
35
+ new ModerationProcessor({
36
+ model: openai("gpt-4.1-nano"),
37
+ categories: ["hate", "harassment", "violence"],
38
+ threshold: 0.7,
39
+ strategy: "block",
40
+ instructions: "Detect and flag inappropriate content in user messages",
41
+ })
42
+ ]
43
+ });
44
+ ```
45
+
46
+ ## Input processors
47
+
48
+ Input processors are applied before user messages reach the language model. They are useful for normalization, validation, content moderation, prompt injection detection, and security checks.
49
+
50
+ ### Normalizing user messages
51
+
52
+ The `UnicodeNormalizer` is an input processor that cleans and normalizes user input by unifying Unicode characters, standardizing whitespace, and removing problematic symbols, allowing the LLM to better understand user messages.
53
+
54
+ ```typescript {6-9} filename="src/mastra/agents/normalized-agent.ts" showLineNumbers copy
55
+ import { UnicodeNormalizer } from "@mastra/core/processors";
56
+
57
+ export const normalizedAgent = new Agent({
58
+ // ...
59
+ inputProcessors: [
60
+ new UnicodeNormalizer({
61
+ stripControlChars: true,
62
+ collapseWhitespace: true,
63
+ })
64
+ ],
65
+ });
66
+ ```
67
+
68
+ > See [UnicodeNormalizer](../../reference/processors/unicode-normalizer.mdx) for a full list of configuration options.
69
+
70
+ ### Preventing prompt injection
71
+
72
+ The `PromptInjectionDetector` is an input processor that scans user messages for prompt injection, jailbreak attempts, and system override patterns. It uses an LLM to classify risky input and can block or rewrite it before it reaches the model.
73
+
74
+ ```typescript {6-11} filename="src/mastra/agents/secure-agent.ts" showLineNumbers copy
75
+ import { PromptInjectionDetector } from "@mastra/core/processors";
76
+
77
+ export const secureAgent = new Agent({
78
+ // ...
79
+ inputProcessors: [
80
+ new PromptInjectionDetector({
81
+ model: openai("gpt-4.1-nano"),
82
+ threshold: 0.8,
83
+ strategy: 'rewrite',
84
+ detectionTypes: ['injection', 'jailbreak', 'system-override'],
85
+ })
86
+ ],
87
+ });
88
+ ```
89
+
90
+ > See [PromptInjectionDetector](../../reference/processors/prompt-injection-detector.mdx) for a full list of configuration options.
91
+
92
+ ### Detecting and translating language
93
+
94
+ The `LanguageDetector` is an input processor that detects and translates user messages into a target language, enabling multilingual support while maintaining consistent interaction. It uses an LLM to identify the language and perform the translation.
95
+
96
+ ```typescript {6-11} filename="src/mastra/agents/multilingual-agent.ts" showLineNumbers copy
97
+ import { LanguageDetector } from "@mastra/core/processors";
98
+
99
+ export const multilingualAgent = new Agent({
100
+ // ...
101
+ inputProcessors: [
102
+ new LanguageDetector({
103
+ model: openai("gpt-4.1-nano"),
104
+ targetLanguages: ['English', 'en'],
105
+ strategy: 'translate',
106
+ threshold: 0.8,
107
+ })
108
+ ],
109
+ });
110
+ ```
111
+
112
+ > See [LanguageDetector](../../reference/processors/language-detector.mdx) for a full list of configuration options.
113
+
114
+ ## Output processors
115
+
116
+ Output processors are applied after the language model generates a response, but before it is returned to the user. They are useful for response optimization, moderation, transformation, and applying safety controls.
117
+
118
+ ### Batching streamed output
119
+
120
+ The `BatchPartsProcessor` is an output processor that combines multiple stream parts before emitting them to the client. This reduces network overhead and improves the user experience by consolidating small chunks into larger batches.
121
+
122
+ ```typescript {6-10} filename="src/mastra/agents/batched-agent.ts" showLineNumbers copy
123
+ import { BatchPartsProcessor } from "@mastra/core/processors";
124
+
125
+ export const batchedAgent = new Agent({
126
+ // ...
127
+ outputProcessors: [
128
+ new BatchPartsProcessor({
129
+ batchSize: 5,
130
+ maxWaitTime: 100,
131
+ emitOnNonText: true
132
+ })
133
+ ]
134
+ });
135
+ ```
136
+
137
+ > See [BatchPartsProcessor](../../reference/processors/batch-parts-processor.mdx) for a full list of configuration options.
138
+
139
+ ### Limiting token usage
140
+
141
+ The `TokenLimiterProcessor` is an output processor that limits the number of tokens in model responses. It helps manage cost and performance by truncating or blocking messages when the limit is exceeded.
142
+
143
+ ```typescript {6-10, 13-15} filename="src/mastra/agents/limited-agent.ts" showLineNumbers copy
144
+ import { TokenLimiterProcessor } from "@mastra/core/processors";
145
+
146
+ export const limitedAgent = new Agent({
147
+ // ...
148
+ outputProcessors: [
149
+ new TokenLimiterProcessor({
150
+ limit: 1000,
151
+ strategy: "truncate",
152
+ countMode: "cumulative"
153
+ })
154
+ ]
155
+ })
156
+ ```
157
+
158
+ > See [TokenLimiterProcessor](../../reference/processors/token-limiter-processor.mdx) for a full list of configuration options.
159
+
160
+ ### Scrubbing system prompts
161
+
162
+ The `SystemPromptScrubber` is an output processor that detects and redacts system prompts or other internal instructions from model responses. It helps prevent unintended disclosure of prompt content or configuration details that could introduce security risks. It uses an LLM to identify and redact sensitive content based on configured detection types.
163
+
164
+ ```typescript {5-13} filename="src/mastra/agents/scrubbed-agent.ts" copy showLineNumbers
165
+ import { SystemPromptScrubber } from "@mastra/core/processors";
166
+
167
+ const scrubbedAgent = new Agent({
168
+ outputProcessors: [
169
+ new SystemPromptScrubber({
170
+ model: openai("gpt-4.1-nano"),
171
+ strategy: "redact",
172
+ customPatterns: ["system prompt", "internal instructions"],
173
+ includeDetections: true,
174
+ instructions: "Detect and redact system prompts, internal instructions, and security-sensitive content",
175
+ redactionMethod: "placeholder",
176
+ placeholderText: "[REDACTED]"
177
+ })
178
+ ]
179
+ });
180
+ ```
181
+
182
+ > See [SystemPromptScrubber](../../reference/processors/system-prompt-scrubber.mdx) for a full list of configuration options.
183
+
184
+ ## Hybrid processors
185
+
186
+ Hybrid processors can be applied either before messages are sent to the language model or before responses are returned to the user. They are useful for tasks like content moderation and PII redaction.
187
+
188
+ ### Moderating input and output
189
+
190
+ The `ModerationProcessor` is a hybrid processor that detects inappropriate or harmful content across categories like hate, harassment, and violence. It can be used to moderate either user input or model output, depending on where it's applied. It uses an LLM to classify the message and can block or rewrite it based on your configuration.
191
+
192
+ ```typescript {6-11, 14-16} filename="src/mastra/agents/moderated-agent.ts" showLineNumbers copy
193
+ import { ModerationProcessor } from "@mastra/core/processors";
194
+
195
+ export const moderatedAgent = new Agent({
196
+ // ...
197
+ inputProcessors: [
198
+ new ModerationProcessor({
199
+ model: openai("gpt-4.1-nano"),
200
+ threshold: 0.7,
201
+ strategy: "block",
202
+ categories: ["hate", "harassment", "violence"]
203
+ })
204
+ ],
205
+ outputProcessors: [
206
+ new ModerationProcessor({
207
+ // ...
208
+ })
209
+ ]
210
+ });
211
+ ```
212
+
213
+ > See [ModerationProcessor](../../reference/processors/moderation-processor.mdx) for a full list of configuration options.
214
+
215
+ ### Detecting and redacting PII
216
+
217
+ The `PIIDetector` is a hybrid processor that detects and removes personally identifiable information such as emails, phone numbers, and credit cards. It can redact either user input or model output, depending on where it's applied. It uses an LLM to identify sensitive content based on configured detection types.
218
+
219
+ ```typescript {6-13, 16-18} filename="src/mastra/agents/private-agent.ts" showLineNumbers copy
220
+ import { PIIDetector } from "@mastra/core/processors";
221
+
222
+ export const privateAgent = new Agent({
223
+ // ...
224
+ inputProcessors: [
225
+ new PIIDetector({
226
+ model: openai("gpt-4.1-nano"),
227
+ threshold: 0.6,
228
+ strategy: 'redact',
229
+ redactionMethod: 'mask',
230
+ detectionTypes: ['email', 'phone', 'credit-card'],
231
+ instructions: "Detect and mask personally identifiable information."
232
+ })
233
+ ],
234
+ outputProcessors: [
235
+ new PIIDetector({
236
+ // ...
237
+ })
238
+ ]
239
+ });
240
+ ```
241
+
242
+ > See [PIIDetector](../../reference/processors/pii-detector.mdx) for a full list of configuration options.
243
+
244
+ ## Applying multiple processors
245
+
246
+ You can apply multiple processors by listing them in the `inputProcessors` or `outputProcessors` array. They run in sequence, with each processor receiving the output of the one before it.
247
+
248
+ A typical order might be:
249
+
250
+ 1. **Normalization**: Standardize input format (`UnicodeNormalizer`).
251
+ 2. **Security checks**: Detect threats or sensitive content (`PromptInjectionDetector`, `PIIDetector`).
252
+ 3. **Filtering**: Block or transform messages (`ModerationProcessor`).
253
+
254
+ The order affects behavior, so arrange processors to suit your goals.
255
+
256
+ ```typescript filename="src/mastra/agents/test-agent.ts" showLineNumbers copy
257
+ import {
258
+ UnicodeNormalizer,
259
+ ModerationProcessor,
260
+ PromptInjectionDetector,
261
+ PIIDetector
262
+ } from "@mastra/core/processors";
263
+
264
+ export const testAgent = new Agent({
265
+ // ...
266
+ inputProcessors: [
267
+ new UnicodeNormalizer({
268
+ //...
269
+ }),
270
+ new PromptInjectionDetector({
271
+ // ...
272
+ }),
273
+ new PIIDetector({
274
+ // ...
275
+ }),
276
+ new ModerationProcessor({
277
+ // ...
278
+ })
279
+ ],
280
+ });
281
+ ```
282
+
283
+ ## Processor strategies
284
+
285
+ Many of the built-in processors support a `strategy` parameter that controls how they handle flagged input or output. Supported values may include: `block`, `warn`, `detect`, or `redact`.
286
+
287
+ Most strategies allow the request to continue without interruption. When `block` is used, the processor calls its internal `abort()` function, which immediately stops the request and prevents any subsequent processors from running.
288
+
289
+ ```typescript {8} filename="src/mastra/agents/private-agent.ts" showLineNumbers copy
290
+ import { PIIDetector } from "@mastra/core/processors";
291
+
292
+ export const privateAgent = new Agent({
293
+ // ...
294
+ inputProcessors: [
295
+ new PIIDetector({
296
+ // ...
297
+ strategy: "block"
298
+ })
299
+ ]
300
+ })
301
+ ```
302
+
303
+ ### Handling blocked requests
304
+
305
+ When a processor blocks a request, the agent will still return successfully without throwing an error. To handle blocked requests, check for `tripwire` or `tripwireReason` in the response.
306
+
307
+ For example, if an agent uses the `PIIDetector` with `strategy: "block"` and the request includes a credit card number, it will be blocked and the response will include a `tripwireReason`.
308
+
309
+ #### `.generate()` example
310
+
311
+ ```typescript {3-4, } showLineNumbers
312
+ const result = await agent.generate("Is this credit card number valid?: 4543 1374 5089 4332");
313
+
314
+ console.error(result.tripwire);
315
+ console.error(result.tripwireReason);
316
+ ```
317
+ #### `.stream()` example
318
+
319
+ ```typescript {4-5} showLineNumbers
320
+ const stream = await agent.stream("Is this credit card number valid?: 4543 1374 5089 4332");
321
+
322
+ for await (const chunk of stream.fullStream) {
323
+ if (chunk.type === "tripwire") {
324
+ console.error(chunk.payload.tripwireReason);
325
+ }
326
+ }
327
+ ```
328
+
329
+ In this case, the `tripwireReason` indicates that a credit card number was detected:
330
+
331
+ ```text
332
+ PII detected. Types: credit-card
333
+ ```
334
+
335
+
@@ -1,27 +1,47 @@
1
1
  ---
2
- title: "Complex Task Execution with AgentNetwork Loop Method"
3
- description: "This page demonstrates how to use the AgentNetwork's loop method in Mastra vNext to handle complex tasks that require multiple agents and workflows, including memory-based orchestration and multi-step execution."
2
+ title: "Handling Complex LLM Operations | Networks | Mastra"
3
+ description: "Networks in Mastra help you execute individual or multiple Mastra primitives in a non-deterministic way using a single API."
4
4
  ---
5
5
 
6
- ## Complex tasks requiring multiple primitives
6
+ # Agent.network()
7
7
 
8
- As an example, we have an AgentNetwork with 3 primitives at its disposal:
8
+ `Agent.network()` introduces a flexible, composable and non-deterministic way to orchestrate multiple specialized agents and workflows, enabling complex, reasoning and task completion.
9
+
10
+ There are two main problem areas that this system is designed to solve:
11
+
12
+ - Scenarios where a single agent is insufficient, and tasks require collaboration, routing, or sequential/parallel execution across multiple agents and workflows.
13
+ - Scenarios where the task is not fully defined and is initiated with unstructured input. A network allows your Agent to figure out which primitive to call and turn unstructured input into a structured task.
14
+
15
+ ## Differences from Workflows
16
+
17
+ - Workflows are linear or branched sequences of steps. This creates a deterministic flow of execution.
18
+ - `Agent.network()` adds a layer of non-deterministic LLM-based orchestration, allowing dynamic, multi-agent collaboration and routing. This creates a non-deterministic flow of execution.
19
+
20
+ ## Important details
21
+
22
+ - Providing memory to the Agent when using `network()` is _not_ optional, as it is required to store the task history. Memory is the core primitive used for any decisions on which primitives to run, as well as determine task completion.
23
+ - Any available primitives (agents, workflows) are used based on their descriptions. The better the description, the better the routing agent will be able to select the right primitive. For workflows, the input schema is also used to determine which inputs to use when calling the workflow. More descriptive naming yields better results.
24
+ - When primitives with overlapping capabilities are available, the agent will use the most specific primitive. For example, if both an agent and a workflow can do research, it will use the input schema of the workflow to determine which primitive to select.
25
+
26
+ ## Turning an Agent into a Network
27
+
28
+ As an example, we have an Agent with 3 primitives at its disposal:
9
29
 
10
30
  - `agent1`: A general research agent that can do research on a given topic.
11
31
  - `agent2`: A general writing agent that can write a full report based on the researched material.
12
32
  - `workflow1`: A workflow that can research a given city and write a full report based on the researched material (using both agent1 and agent2).
13
33
 
14
- We use the `loop` method to create a task that requires multiple primitives. The AgentNetwork will, using memory, figure out which primitives to call and in which order, as well as when the task is complete.
34
+ We use the `network` method to create a task that requires multiple primitives. The Agent will, using memory, figure out which primitives to call and in which order, as well as when the task is complete.
35
+
15
36
 
16
37
  ```typescript
17
- import { NewAgentNetwork } from '@mastra/core/network/vNext';
18
38
  import { Agent } from '@mastra/core/agent';
19
39
  import { createStep, createWorkflow } from '@mastra/core/workflows';
40
+ import { RuntimeContext } from '@mastra/core/runtime-context';
20
41
  import { Memory } from '@mastra/memory';
21
42
  import { openai } from '@ai-sdk/openai';
22
43
  import { LibSQLStore } from '@mastra/libsql';
23
44
  import { z } from 'zod';
24
- import { RuntimeContext } from '@mastra/core/runtime-context';
25
45
 
26
46
  const memory = new Memory({
27
47
  storage: new LibSQLStore({
@@ -103,7 +123,7 @@ const agent2 = new Agent({
103
123
  model: openai('gpt-4o'),
104
124
  });
105
125
 
106
- const network = new NewAgentNetwork({
126
+ const routingAgent = new Agent({
107
127
  id: 'test-network',
108
128
  name: 'Test Network',
109
129
  instructions:
@@ -123,7 +143,7 @@ const runtimeContext = new RuntimeContext();
123
143
 
124
144
  console.log(
125
145
  // specifying the task, note that there is a mention here about using an agent for synthesis. This is because the routing agent can actually do some synthesis on results on its own, so this will force it to use agent2 instead
126
- await network.loop(
146
+ await routingAgent.network(
127
147
  'What are the biggest cities in France? Give me 3. How are they like? Find cities, then do thorough research on each city, and give me a final full report synthesizing all that information. Make sure to use an agent for synthesis.',
128
148
  { runtimeContext },
129
149
  ),
@@ -3,55 +3,77 @@ title: "Agent Overview | Agents | Mastra Docs"
3
3
  description: Overview of agents in Mastra, detailing their capabilities and how they interact with tools, workflows, and external systems.
4
4
  ---
5
5
 
6
- import { Steps } from "nextra/components";
6
+ import { Steps, Callout, Tabs } from "nextra/components";
7
7
 
8
8
  # Using Agents
9
9
 
10
- Agents let you build intelligent assistants powered by language models that can make decisions and perform actions. Each agent has required instructions and an LLM, with optional tools and memory.
11
-
12
- An agent coordinates conversations, calls tools when needed, maintains context through memory, and produces responses tailored to the interaction. Agents can operate on their own or work as part of larger workflows.
10
+ Agents use LLMs and tools to solve open-ended tasks. They reason about goals, decide which tools to use, retain conversation memory, and iterate internally until the model emits a final answer or an optional stop condition is met. Agents produce structured responses you can render in your UI or process programmatically. Use agents directly or compose them into workflows or agent networks.
13
11
 
14
12
  ![Agents overview](/image/agents/agents-overview.jpg)
15
13
 
16
- To create an agent:
17
-
18
- - Define **instructions** with the `Agent` class and set the **LLM** it will use.
19
- - Optionally configure **tools** and **memory** to extend functionality.
20
- - Run the agent to generate responses, with support for streaming, structured output, and dynamic configuration.
21
-
22
- This approach provides type safety and runtime validation, ensuring reliable behavior across all agent interactions.
23
-
24
14
  > **📹 Watch**: → An introduction to agents, and how they compare to workflows [YouTube (7 minutes)](https://youtu.be/0jg2g3sNvgw)
25
15
 
26
16
  ## Getting started
27
17
 
28
- To use agents, install the required dependencies:
18
+ <Tabs items={["Mastra model router", "Vercel AI SDK"]}>
19
+ <Tabs.Tab>
20
+ <Steps>
21
+ ### Install dependencies
22
+
23
+ Add the Mastra core package to your project:
29
24
 
30
25
  ```bash
31
- npm install @mastra/core @ai-sdk/openai
26
+ npm install @mastra/core
32
27
  ```
33
28
 
34
- > Mastra works with all AI SDK provider. See [Model Providers](../getting-started/model-providers.mdx) for more information.
29
+ ### Set your API key
30
+
31
+ Mastra's model router auto-detects environment variables for your chosen provider. For OpenAI, set `OPENAI_API_KEY`:
35
32
 
36
- Import the necessary class from the agents module, and an LLM provider:
33
+ ```bash filename=".env" copy
34
+ OPENAI_API_KEY=<your-api-key>
35
+ ```
36
+
37
+ > Mastra supports more than 600 models. Choose from the full list [here](../getting-started/model-providers.mdx).
38
+
39
+ ### Create an agent
40
+
41
+ Create an agent by instantiating the `Agent` class with system `instructions` and a `model`:
37
42
 
38
43
  ```typescript filename="src/mastra/agents/test-agent.ts" showLineNumbers copy
39
- import { openai } from "@ai-sdk/openai";
40
44
  import { Agent } from "@mastra/core/agent";
45
+
46
+ export const testAgent = new Agent({
47
+ name: "test-agent",
48
+ instructions: "You are a helpful assistant.",
49
+ model: "openai/gpt-4o-mini"
50
+ });
41
51
  ```
42
- ### LLM providers
52
+ </Steps>
53
+ </Tabs.Tab>
54
+ <Tabs.Tab>
55
+ <Steps>
56
+ ### Install dependencies
43
57
 
44
- Each LLM provider needs its own API key, named using the provider’s identifier:
58
+ Include the Mastra core package alongside the Vercel AI SDK provider you want to use:
59
+
60
+ ```bash
61
+ npm install @mastra/core @ai-sdk/openai
62
+ ```
63
+
64
+ ### Set your API key
65
+
66
+ Set the corresponding environment variable for your provider. For OpenAI via the AI SDK:
45
67
 
46
68
  ```bash filename=".env" copy
47
69
  OPENAI_API_KEY=<your-api-key>
48
70
  ```
49
71
 
50
- > See the [AI SDK Providers](https://ai-sdk.dev/providers/ai-sdk-providers) in the Vercel AI SDK docs.
72
+ > See the [AI SDK Providers](https://ai-sdk.dev/providers/ai-sdk-providers) in the Vercel AI SDK docs for additional configuration options.
51
73
 
52
- ### Creating an agent
74
+ ### Create an agent
53
75
 
54
- To create an agent in Mastra, use the `Agent` class. Every agent must include `instructions` to define its behavior, and a `model` parameter to specify the LLM provider and model:
76
+ To create an agent in Mastra, use the `Agent` class. Every agent must include `instructions` to define its behavior, and a `model` parameter to specify the LLM provider and model. When using the Vercel AI SDK, provide the client to your agent's `model` field:
55
77
 
56
78
  ```typescript filename="src/mastra/agents/test-agent.ts" showLineNumbers copy
57
79
  import { openai } from "@ai-sdk/openai";
@@ -63,24 +85,21 @@ export const testAgent = new Agent({
63
85
  model: openai("gpt-4o-mini")
64
86
  });
65
87
  ```
88
+ </Steps>
89
+ </Tabs.Tab>
90
+ </Tabs>
66
91
 
67
92
  #### Instruction formats
68
93
 
69
94
  Instructions define the agent's behavior, personality, and capabilities.
70
95
  They are system-level prompts that establish the agent's core identity and expertise.
71
96
 
72
- Instructions can be provided in multiple formats for greater flexibility:
97
+ Instructions can be provided in multiple formats for greater flexibility. The examples below illustrate the supported shapes:
73
98
 
74
- ```typescript showLineNumbers copy
99
+ ```typescript copy
75
100
  // String (most common)
76
101
  instructions: "You are a helpful assistant."
77
102
 
78
- // System message object
79
- instructions: {
80
- role: "system",
81
- content: "You are an expert programmer."
82
- }
83
-
84
103
  // Array of strings
85
104
  instructions: [
86
105
  "You are a helpful assistant.",
@@ -93,11 +112,18 @@ instructions: [
93
112
  { role: "system", content: "You are a helpful assistant." },
94
113
  { role: "system", content: "You have expertise in TypeScript." }
95
114
  ]
115
+ ```
116
+
117
+ #### Provider-specific options
96
118
 
119
+ Each model provider also enables a few different options, including prompt caching and configuring reasoning. We provide a `providerOptions` flag to manage these. You can set `providerOptions` on the instruction level to set different caching strategy per system instruction/prompt.
120
+
121
+ ```typescript copy
97
122
  // With provider-specific options (e.g., caching, reasoning)
98
123
  instructions: {
99
124
  role: "system",
100
- content: "You are an expert code reviewer. Analyze code for bugs, performance issues, and best practices.",
125
+ content:
126
+ "You are an expert code reviewer. Analyze code for bugs, performance issues, and best practices.",
101
127
  providerOptions: {
102
128
  openai: { reasoning_effort: "high" }, // OpenAI's reasoning models
103
129
  anthropic: { cache_control: { type: "ephemeral" } } // Anthropic's prompt caching
@@ -105,12 +131,7 @@ instructions: {
105
131
  }
106
132
  ```
107
133
 
108
- Provider-specific options allow you to leverage unique features of different LLM providers:
109
- - **Anthropic caching**: Reduce costs by caching frequently-used instructions
110
- - **OpenAI reasoning**: Enable deeper analysis for complex tasks
111
- - **Custom parameters**: Pass any provider-specific configuration
112
-
113
- > See [Agent](../../reference/agents/agent.mdx) for more information.
134
+ > See the [Agent reference doc](../../reference/agents/agent.mdx) for more information.
114
135
 
115
136
  ### Registering an agent
116
137
 
@@ -133,18 +154,23 @@ You can call agents from workflow steps, tools, the Mastra Client, or the comman
133
154
  ```typescript showLineNumbers copy
134
155
  const testAgent = mastra.getAgent("testAgent");
135
156
  ```
157
+ <Callout type="info">
158
+ <p>
159
+ `mastra.getAgent()` is preferred over a direct import, since it preserves the Mastra instance configuration (tools registered, telemetry, vector stores configuration for agent memory, etc.)
160
+ </p>
161
+ </Callout>
136
162
 
137
163
  > See [Calling agents](../../examples/agents/calling-agents.mdx) for more information.
138
164
 
139
165
  ## Generating responses
140
166
 
141
- Use `.generate()` to get a response from an agent. Pass a single string for simple prompts, an array of strings when providing multiple pieces of context, or an array of message objects with `role` and `content` for precise control over roles and conversational flows.
167
+ Agents can return results in two ways: generating the full output before returning it or streaming tokens in real time. Choose the approach that fits your use case: generate for short, internal responses or debugging, and stream to deliver pixels to end users as quickly as possible.
142
168
 
143
- > See [.generate()](../../reference/agents/generate.mdx) for more information.
169
+ <Tabs items={["Generate", "Stream"]}>
170
+ <Tabs.Tab>
171
+ Pass a single string for simple prompts, an array of strings when providing multiple pieces of context, or an array of message objects with `role` and `content`.
144
172
 
145
- ### Generating text
146
-
147
- Call `.generate()` with an array of message objects containing `role` and `content`. The `role` defines the speaker for each message. Typical roles are `user` for human input, `assistant` for agent responses, and `system` for instructions. This structure helps the LLM maintain conversation flow and generate contextually appropriate responses.
173
+ (The `role` defines the speaker for each message. Typical roles are `user` for human input, `assistant` for agent responses, and `system` for instructions.)
148
174
 
149
175
  ```typescript showLineNumbers copy
150
176
  const response = await testAgent.generate([
@@ -156,6 +182,45 @@ const response = await testAgent.generate([
156
182
 
157
183
  console.log(response.text);
158
184
  ```
185
+ </Tabs.Tab>
186
+ <Tabs.Tab>
187
+ Pass a single string for simple prompts, an array of strings when providing multiple pieces of context, or an array of message objects with `role` and `content`.
188
+
189
+ (The `role` defines the speaker for each message. Typical roles are `user` for human input, `assistant` for agent responses, and `system` for instructions.)
190
+
191
+ ```typescript showLineNumbers copy
192
+ const stream = await testAgent.stream([
193
+ { role: "user", content: "Help me organize my day" },
194
+ { role: "user", content: "My day starts at 9am and finishes at 5.30pm" },
195
+ { role: "user", content: "I take lunch between 12:30 and 13:30" },
196
+ { role: "user", content: "I have meetings Monday to Friday between 10:30 and 11:30" }
197
+ ]);
198
+
199
+ for await (const chunk of stream.textStream) {
200
+ process.stdout.write(chunk);
201
+ }
202
+ ```
203
+
204
+ ### Completion using `onFinish()`
205
+
206
+ When streaming responses, the `onFinish()` callback runs after the LLM finishes generating its response and all tool executions are complete.
207
+ It provides the final `text`, execution `steps`, `finishReason`, token `usage` statistics, and other metadata useful for monitoring or logging.
208
+
209
+ ```typescript showLineNumbers copy
210
+ const stream = await testAgent.stream("Help me organize my day", {
211
+ onFinish: ({ steps, text, finishReason, usage }) => {
212
+ console.log({ steps, text, finishReason, usage });
213
+ }
214
+ });
215
+
216
+ for await (const chunk of stream.textStream) {
217
+ process.stdout.write(chunk);
218
+ }
219
+ ```
220
+ </Tabs.Tab>
221
+ </Tabs>
222
+
223
+ > See [.generate()](../../reference/agents/generate.mdx) or [.stream()](../../reference/agents/stream.mdx) for more information.
159
224
 
160
225
  ## Structured output
161
226
 
@@ -189,27 +254,6 @@ const response = await testAgent.generate(
189
254
 
190
255
  console.log(response.object);
191
256
  ```
192
-
193
- #### Agents with tools
194
-
195
- To generate structured output with agents that use tools, use the `output` property:
196
-
197
- ```typescript {6} showLineNumbers copy
198
- import { z } from "zod";
199
-
200
- const response = await testAgent.generate(
201
- // ...
202
- {
203
- output: z.object({
204
- summary: z.string(),
205
- keywords: z.array(z.string())
206
- })
207
- }
208
- );
209
-
210
- console.log(response.object);
211
- ```
212
-
213
257
  ## Working with images
214
258
 
215
259
  Agents can analyze and describe images by processing both the visual content and any text within them. To enable image analysis, pass an object with `type: 'image'` and the image URL in the `content` array. You can combine image content with text prompts to guide the agent's analysis.