@mastra/mcp-docs-server 0.13.29 → 0.13.30-alpha.1

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 (126) hide show
  1. package/.docs/organized/changelogs/%40internal%2Fchangeset-cli.md +2 -0
  2. package/.docs/organized/changelogs/%40internal%2Fstorage-test-utils.md +9 -9
  3. package/.docs/organized/changelogs/%40internal%2Ftypes-builder.md +2 -0
  4. package/.docs/organized/changelogs/%40mastra%2Fagent-builder.md +31 -31
  5. package/.docs/organized/changelogs/%40mastra%2Fai-sdk.md +36 -0
  6. package/.docs/organized/changelogs/%40mastra%2Fastra.md +16 -16
  7. package/.docs/organized/changelogs/%40mastra%2Fchroma.md +16 -16
  8. package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +16 -16
  9. package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +28 -28
  10. package/.docs/organized/changelogs/%40mastra%2Fcloud.md +16 -16
  11. package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +16 -16
  12. package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +16 -16
  13. package/.docs/organized/changelogs/%40mastra%2Fcore.md +106 -106
  14. package/.docs/organized/changelogs/%40mastra%2Fcouchbase.md +16 -16
  15. package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +37 -37
  16. package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +25 -25
  17. package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +25 -25
  18. package/.docs/organized/changelogs/%40mastra%2Fdeployer-vercel.md +25 -25
  19. package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +49 -49
  20. package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +16 -16
  21. package/.docs/organized/changelogs/%40mastra%2Fevals.md +33 -33
  22. package/.docs/organized/changelogs/%40mastra%2Flance.md +16 -16
  23. package/.docs/organized/changelogs/%40mastra%2Flibsql.md +16 -16
  24. package/.docs/organized/changelogs/%40mastra%2Floggers.md +16 -16
  25. package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +23 -23
  26. package/.docs/organized/changelogs/%40mastra%2Fmcp-registry-registry.md +16 -16
  27. package/.docs/organized/changelogs/%40mastra%2Fmcp.md +16 -16
  28. package/.docs/organized/changelogs/%40mastra%2Fmemory.md +36 -36
  29. package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +16 -16
  30. package/.docs/organized/changelogs/%40mastra%2Fmssql.md +16 -16
  31. package/.docs/organized/changelogs/%40mastra%2Fopensearch.md +17 -17
  32. package/.docs/organized/changelogs/%40mastra%2Fpg.md +31 -31
  33. package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +16 -16
  34. package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +67 -67
  35. package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +16 -16
  36. package/.docs/organized/changelogs/%40mastra%2Frag.md +16 -16
  37. package/.docs/organized/changelogs/%40mastra%2Freact.md +37 -0
  38. package/.docs/organized/changelogs/%40mastra%2Fs3vectors.md +15 -0
  39. package/.docs/organized/changelogs/%40mastra%2Fserver.md +37 -37
  40. package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +16 -16
  41. package/.docs/organized/changelogs/%40mastra%2Fupstash.md +19 -19
  42. package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +17 -17
  43. package/.docs/organized/changelogs/%40mastra%2Fvoice-azure.md +18 -18
  44. package/.docs/organized/changelogs/%40mastra%2Fvoice-cloudflare.md +16 -16
  45. package/.docs/organized/changelogs/%40mastra%2Fvoice-deepgram.md +16 -16
  46. package/.docs/organized/changelogs/%40mastra%2Fvoice-elevenlabs.md +16 -16
  47. package/.docs/organized/changelogs/%40mastra%2Fvoice-gladia.md +16 -16
  48. package/.docs/organized/changelogs/%40mastra%2Fvoice-google-gemini-live.md +15 -0
  49. package/.docs/organized/changelogs/%40mastra%2Fvoice-google.md +16 -16
  50. package/.docs/organized/changelogs/%40mastra%2Fvoice-murf.md +16 -16
  51. package/.docs/organized/changelogs/%40mastra%2Fvoice-openai-realtime.md +16 -16
  52. package/.docs/organized/changelogs/%40mastra%2Fvoice-openai.md +16 -16
  53. package/.docs/organized/changelogs/%40mastra%2Fvoice-playai.md +16 -16
  54. package/.docs/organized/changelogs/%40mastra%2Fvoice-sarvam.md +16 -16
  55. package/.docs/organized/changelogs/%40mastra%2Fvoice-speechify.md +16 -16
  56. package/.docs/organized/changelogs/create-mastra.md +35 -35
  57. package/.docs/organized/changelogs/mastra.md +63 -63
  58. package/.docs/organized/code-examples/agent.md +26 -7
  59. package/.docs/organized/code-examples/agui.md +4 -4
  60. package/.docs/organized/code-examples/ai-elements.md +1 -1
  61. package/.docs/organized/code-examples/ai-sdk-useChat.md +2 -2
  62. package/.docs/organized/code-examples/ai-sdk-v5.md +2 -2
  63. package/.docs/organized/code-examples/assistant-ui.md +2 -2
  64. package/.docs/organized/code-examples/bird-checker-with-nextjs-and-eval.md +2 -2
  65. package/.docs/organized/code-examples/bird-checker-with-nextjs.md +2 -2
  66. package/.docs/organized/code-examples/client-side-tools.md +4 -4
  67. package/.docs/organized/code-examples/crypto-chatbot.md +2 -2
  68. package/.docs/organized/code-examples/heads-up-game.md +2 -2
  69. package/.docs/organized/code-examples/openapi-spec-writer.md +2 -2
  70. package/.docs/raw/agents/adding-voice.mdx +118 -25
  71. package/.docs/raw/agents/agent-memory.mdx +73 -89
  72. package/.docs/raw/agents/guardrails.mdx +1 -1
  73. package/.docs/raw/agents/networks.mdx +12 -6
  74. package/.docs/raw/agents/overview.mdx +46 -11
  75. package/.docs/raw/agents/using-tools.mdx +95 -0
  76. package/.docs/raw/deployment/overview.mdx +9 -11
  77. package/.docs/raw/frameworks/agentic-uis/ai-sdk.mdx +7 -4
  78. package/.docs/raw/frameworks/servers/express.mdx +2 -2
  79. package/.docs/raw/getting-started/installation.mdx +34 -132
  80. package/.docs/raw/getting-started/mcp-docs-server.mdx +13 -1
  81. package/.docs/raw/index.mdx +49 -14
  82. package/.docs/raw/observability/ai-tracing/exporters/otel.mdx +3 -0
  83. package/.docs/raw/reference/agents/generateLegacy.mdx +4 -4
  84. package/.docs/raw/reference/observability/ai-tracing/exporters/otel.mdx +6 -0
  85. package/.docs/raw/reference/scorers/answer-relevancy.mdx +105 -7
  86. package/.docs/raw/reference/scorers/answer-similarity.mdx +266 -16
  87. package/.docs/raw/reference/scorers/bias.mdx +107 -6
  88. package/.docs/raw/reference/scorers/completeness.mdx +131 -8
  89. package/.docs/raw/reference/scorers/content-similarity.mdx +107 -8
  90. package/.docs/raw/reference/scorers/context-precision.mdx +234 -18
  91. package/.docs/raw/reference/scorers/context-relevance.mdx +418 -35
  92. package/.docs/raw/reference/scorers/faithfulness.mdx +122 -8
  93. package/.docs/raw/reference/scorers/hallucination.mdx +125 -8
  94. package/.docs/raw/reference/scorers/keyword-coverage.mdx +141 -9
  95. package/.docs/raw/reference/scorers/noise-sensitivity.mdx +478 -6
  96. package/.docs/raw/reference/scorers/prompt-alignment.mdx +351 -102
  97. package/.docs/raw/reference/scorers/textual-difference.mdx +134 -6
  98. package/.docs/raw/reference/scorers/tone-consistency.mdx +133 -0
  99. package/.docs/raw/reference/scorers/tool-call-accuracy.mdx +422 -65
  100. package/.docs/raw/reference/scorers/toxicity.mdx +125 -7
  101. package/.docs/raw/reference/streaming/agents/MastraModelOutput.mdx +9 -5
  102. package/.docs/raw/reference/streaming/agents/streamLegacy.mdx +4 -4
  103. package/.docs/raw/reference/streaming/workflows/observeStream.mdx +49 -0
  104. package/.docs/raw/reference/streaming/workflows/observeStreamVNext.mdx +47 -0
  105. package/.docs/raw/reference/streaming/workflows/resumeStreamVNext.mdx +7 -5
  106. package/.docs/raw/reference/streaming/workflows/stream.mdx +1 -1
  107. package/.docs/raw/reference/workflows/workflow.mdx +33 -0
  108. package/.docs/raw/scorers/custom-scorers.mdx +244 -3
  109. package/.docs/raw/scorers/overview.mdx +8 -38
  110. package/.docs/raw/server-db/middleware.mdx +5 -2
  111. package/.docs/raw/server-db/runtime-context.mdx +178 -0
  112. package/.docs/raw/streaming/workflow-streaming.mdx +28 -1
  113. package/.docs/raw/tools-mcp/overview.mdx +25 -7
  114. package/.docs/raw/workflows/overview.mdx +28 -1
  115. package/CHANGELOG.md +15 -0
  116. package/package.json +6 -6
  117. package/.docs/raw/agents/runtime-context.mdx +0 -103
  118. package/.docs/raw/agents/using-tools-and-mcp.mdx +0 -241
  119. package/.docs/raw/getting-started/model-providers.mdx +0 -63
  120. package/.docs/raw/reference/agents/migration-guide.mdx +0 -291
  121. package/.docs/raw/tools-mcp/runtime-context.mdx +0 -63
  122. /package/.docs/raw/{evals → scorers/evals-old-api}/custom-eval.mdx +0 -0
  123. /package/.docs/raw/{evals → scorers/evals-old-api}/overview.mdx +0 -0
  124. /package/.docs/raw/{evals → scorers/evals-old-api}/running-in-ci.mdx +0 -0
  125. /package/.docs/raw/{evals → scorers/evals-old-api}/textual-evals.mdx +0 -0
  126. /package/.docs/raw/{server-db → workflows}/snapshots.mdx +0 -0
@@ -1,241 +0,0 @@
1
- ---
2
- title: "Tools and MCP | Agents | Mastra Docs"
3
- description: Learn how to create tools, add them to Mastra agents, and integrate tools from MCP servers.
4
- ---
5
-
6
- # Tools and MCP
7
-
8
- Tools are typed functions that can be executed by agents or workflows to help them carry out specific tasks like sending a message, querying a database, or calling an external API. Each tool defines its expected inputs, contains the logic to perform its operation, and may delegate execution to an MCP client for accessing external systems. They give agents a structured way to move beyond language generation and enable deterministic outcomes through well-defined interfaces.
9
-
10
- Mastra supports two patterns for providing tools to agents:
11
-
12
- - **Direct assignment**: Static tools available at initialization
13
- - **Function-based**: Dynamic tools resolved based on runtime context
14
-
15
- ## Creating a tool
16
-
17
- This example shows how to create a simple tool that asynchronously fetches data from a weather API:
18
-
19
- ```typescript filename="src/mastra/tools/weather-tool.ts" showLineNumbers copy
20
- import { createTool } from "@mastra/core/tools";
21
- import { z } from "zod";
22
-
23
- export const weatherTool = createTool({
24
- id: "weather-tool",
25
- description: "Fetches the current weather for a given city",
26
- inputSchema: z.object({
27
- city: z.string(),
28
- }),
29
- execute: async ({ context }) => {
30
- const { city } = context;
31
- const response = await fetch(`https://weather.service?city=${city}`);
32
-
33
- const { temperature, conditions } = await response.json();
34
-
35
- return { temperature, conditions };
36
- },
37
- });
38
- ```
39
-
40
- For details on creating and designing tools, see the [Tools Overview](../tools-mcp/overview.mdx).
41
-
42
- ## Adding tools to an agent
43
-
44
- To make a tool available to an agent, add it to the `tools` property in the agent's configuration.
45
-
46
- ```typescript {3,12} filename="src/mastra/agents/weather-agent.ts" showLineNumbers copy
47
- import { Agent } from "@mastra/core/agent";
48
- import { openai } from "@ai-sdk/openai";
49
- import { weatherTool } from "../tools/weather-tool";
50
-
51
- export const weatherAgent = new Agent({
52
- name: "Weather Agent",
53
- instructions: `
54
- You are a helpful assistant that provides weather information.
55
- When asked about the weather, use the weatherTool to fetch the data.`,
56
- model: openai("gpt-4o-mini"),
57
- tools: {
58
- weatherTool,
59
- },
60
- });
61
- ```
62
-
63
- When you call the agent, it can now decide to use the configured tool based on its instructions and the user's prompt.
64
-
65
- ## Adding MCP tools to an agent
66
-
67
- [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) provides a standardized way for AI models to discover and interact with external tools and resources. You can connect your Mastra agent to MCP servers to use tools provided by third parties.
68
-
69
- For more details on MCP concepts and how to set up MCP clients and servers, see the [MCP Overview](/docs/tools-mcp/mcp-overview).
70
-
71
- ### Installation
72
-
73
- First, install the Mastra MCP package:
74
-
75
- ```bash npm2yarn copy
76
- npm install @mastra/mcp@latest
77
- ```
78
-
79
- ### Using MCP tools
80
-
81
- Because there are so many MCP server registries to choose from, we've created an [MCP Registry Registry](https://mastra.ai/mcp-registry-registry) to help you find MCP servers.
82
-
83
- Once you have a server you want to use with your agent, import the Mastra `MCPClient` and add the server configuration.
84
-
85
- ```typescript filename="src/mastra/mcp.ts" {1,7-16}
86
- import { MCPClient } from "@mastra/mcp";
87
-
88
- // Configure MCPClient to connect to your server(s)
89
- export const mcp = new MCPClient({
90
- servers: {
91
- filesystem: {
92
- command: "npx",
93
- args: [
94
- "-y",
95
- "@modelcontextprotocol/server-filesystem",
96
- "/Users/username/Downloads",
97
- ],
98
- },
99
- },
100
- });
101
- ```
102
-
103
- Then connect your agent to the server tools:
104
-
105
- ```typescript filename="src/mastra/agents/mcpAgent.ts" {7}
106
- import { Agent } from "@mastra/core/agent";
107
- import { openai } from "@ai-sdk/openai";
108
- import { mcp } from "../mcp";
109
-
110
- // Create an agent and add tools from the MCP client
111
- const agent = new Agent({
112
- name: "Agent with MCP Tools",
113
- instructions: "You can use tools from connected MCP servers.",
114
- model: openai("gpt-4o-mini"),
115
- tools: await mcp.getTools(),
116
- });
117
- ```
118
-
119
- When creating agents that will consume an MCP server in the same repo they need to connect to, always use function based tools to prevent race conditions.
120
-
121
- ```typescript filename="src/mastra/agents/selfReferencingAgent.ts"
122
- import { Agent } from "@mastra/core/agent";
123
- import { MCPServer } from "@mastra/mcp";
124
- import { MCPClient } from "@mastra/mcp";
125
- import { openai } from "@ai-sdk/openai";
126
-
127
- const myAgent = new Agent({
128
- name: "My Agent",
129
- description: "An agent that can use tools from an http MCP server",
130
- instructions: "You can use remote calculation tools.",
131
- model: openai("gpt-4o-mini"),
132
- tools: async () => {
133
- // Tools resolve when needed, not during initialization
134
- const mcpClient = new MCPClient({
135
- servers: {
136
- myServer: {
137
- url: new URL("http://localhost:4111/api/mcp/mcpServer/mcp"),
138
- },
139
- },
140
- });
141
- return await mcpClient.getTools();
142
- },
143
- });
144
-
145
- // This works because tools resolve after server startup
146
- export const mcpServer = new MCPServer({
147
- name: "My MCP Server",
148
- agents: {
149
- myAgent
150
- },
151
- });
152
- ```
153
-
154
- For more details on configuring `MCPClient` and the difference between static and dynamic MCP server configurations, see the [MCP Overview](/docs/tools-mcp/mcp-overview).
155
-
156
- ## Accessing MCP resources
157
-
158
- In addition to tools, MCP servers can also expose resources - data or content that can be retrieved and used in your application.
159
-
160
- ```typescript filename="src/mastra/resources.ts" {3-8}
161
- import { mcp } from "./mcp";
162
-
163
- // Get resources from all connected MCP servers
164
- const resources = await mcp.getResources();
165
-
166
- // Access resources from a specific server
167
- if (resources.filesystem) {
168
- const resource = resources.filesystem.find(
169
- (r) => r.uri === "filesystem://Downloads",
170
- );
171
- console.log(`Resource: ${resource?.name}`);
172
- }
173
- ```
174
-
175
- Each resource has a URI, name, description, and MIME type. The `getResources()` method handles errors gracefully - if a server fails or doesn't support resources, it will be omitted from the results.
176
-
177
- ## Accessing MCP prompts
178
-
179
- MCP servers can also expose prompts, which represent structured message templates or conversational context for agents.
180
-
181
- ### Listing prompts
182
-
183
- ```typescript filename="src/mastra/prompts.ts"
184
- import { mcp } from "./mcp";
185
-
186
- // Get prompts from all connected MCP servers
187
- const prompts = await mcp.prompts.list();
188
-
189
- // Access prompts from a specific server
190
- if (prompts.weather) {
191
- const prompt = prompts.weather.find(
192
- (p) => p.name === "current"
193
- );
194
- console.log(`Prompt: ${prompt?.name}`);
195
- }
196
- ```
197
-
198
- Each prompt has a name, description, and (optional) version.
199
-
200
- ### Retrieving a prompt and its messages
201
-
202
- ```typescript filename="src/mastra/prompts.ts"
203
- const { prompt, messages } = await mcp.prompts.get({ serverName: "weather", name: "current" });
204
- console.log(prompt); // { name: "current", version: "v1", ... }
205
- console.log(messages); // [ { role: "assistant", content: { type: "text", text: "..." } }, ... ]
206
- ```
207
-
208
- ## Exposing agents as tools via `MCPServer`
209
-
210
- In addition to using tools from MCP servers, your Mastra Agents themselves can be exposed as tools to any MCP-compatible client using Mastra's `MCPServer`.
211
-
212
- When an `Agent` instance is provided to an `MCPServer` configuration:
213
-
214
- - It is automatically converted into a callable tool.
215
- - The tool is named `ask_<agentKey>`, where `<agentKey>` is the identifier you used when adding the agent to the `MCPServer`'s `agents` configuration.
216
- - The agent's `description` property (which must be a non-empty string) is used to generate the tool's description.
217
-
218
- This allows other AI models or MCP clients to interact with your Mastra Agents as if they were standard tools, typically by "asking" them a question.
219
-
220
- **Example `MCPServer` Configuration with an Agent:**
221
-
222
- ```typescript filename="src/mastra/mcp.ts"
223
- import { Agent } from "@mastra/core/agent";
224
- import { MCPServer } from "@mastra/mcp";
225
- import { openai } from "@ai-sdk/openai";
226
- import { weatherInfo } from "../tools/weatherInfo";
227
- import { generalHelper } from "../agents/generalHelper";
228
-
229
- const server = new MCPServer({
230
- name: "My Custom Server with Agent-Tool",
231
- version: "1.0.0",
232
- tools: {
233
- weatherInfo,
234
- },
235
- agents: { generalHelper }, // Exposes 'ask_generalHelper' tool
236
- });
237
- ```
238
-
239
- For an agent to be successfully converted into a tool by `MCPServer`, its `description` property must be set to a non-empty string in its constructor configuration. If the description is missing or empty, `MCPServer` will throw an error during initialization.
240
-
241
- For more details on setting up and configuring `MCPServer`, refer to the [MCPServer reference documentation](/reference/tools/mcp-server).
@@ -1,63 +0,0 @@
1
- ---
2
- title: "Model Providers | Getting Started | Mastra Docs"
3
- description: "Learn how to configure and use different model providers with Mastra."
4
- ---
5
-
6
- import { Callout } from "nextra/components"
7
-
8
- # Model Providers
9
-
10
- Mastra's unified model router gives you access to 541+ models from 38 providers with a single API. Switch between models and providers without changing your code. Automatic environment variable detection handles authentication, while TypeScript provides full autocomplete for every model.
11
-
12
- ## Quick Start
13
-
14
- Simply use the `provider/model` string pattern:
15
-
16
- ```typescript showLineNumbers copy filename="src/mastra/agents/weather-agent.ts"
17
- import { Agent } from "@mastra/core";
18
-
19
- const agent = new Agent({
20
- name: "WeatherAgent",
21
- instructions: "You are a helpful weather assistant",
22
- model: "openai/gpt-4o"
23
- });
24
-
25
- const result = await agent.generate("What is the weather like?");
26
- ```
27
-
28
- ## Browse Providers
29
-
30
- <Callout>
31
- **[→ View all 38 providers and 7 gateways](../../models)**
32
-
33
- Explore our complete catalog with logos, model counts, and documentation for each provider.
34
- </Callout>
35
-
36
- ## Configuration
37
-
38
- Models automatically detect API keys from environment variables.
39
-
40
- ## AI SDK Compatibility
41
-
42
- While Mastra provides built-in support for 541+ models, you can also use [Vercel AI SDK](https://sdk.vercel.ai/providers/ai-sdk-providers) model providers for additional flexibility:
43
-
44
- ```typescript
45
- import { openai } from "@ai-sdk/openai";
46
- import { Agent } from "@mastra/core";
47
-
48
- const agent = new Agent({
49
- name: "AISDKAgent",
50
- model: openai("gpt-4-turbo") // AI SDK model provider
51
- });
52
- ```
53
-
54
- <Callout type="info">
55
- **Recommendation**: Use Mastra's built-in model router (`"provider/model"` strings) for simplicity. Use AI SDK providers only when you need specific features not available in the built-in providers.
56
- </Callout>
57
-
58
- ## Learn More
59
-
60
- - [📚 Browse All Model Providers](../../models) - Complete list with examples
61
- - [🚀 Agent Documentation](../../reference/agents/agent.mdx) - Using models with agents
62
- - [⚙️ Environment Variables](../getting-started/installation.mdx#add-your-api-key) - Configuration guide
63
- - [🔧 Tool Configuration](../agents/using-tools-and-mcp.mdx#adding-tools-to-an-agent) - Adding tools to agents
@@ -1,291 +0,0 @@
1
- # Migration Guide: VNext to Standard APIs
2
-
3
- ## Overview
4
-
5
- As of `v 0.20.00`, the `streamVNext()` and `generateVNext()` methods in Mastra agents have been renamed to `stream()` and `generate()` respectively. These are now the standard APIs with full AI SDK v5 compatibility. The original `stream()` and `generate()` methods have been renamed to `streamLegacy()` and `generateLegacy()` to maintain backward compatibility with AI SDK v4.
6
-
7
- ### Continue using AI SDK v4 models
8
- - Rename all your `stream()` and `generate()` calls to `streamLegacy()` and `generateLegacy()` respectively. No other change is needed.
9
-
10
- ### Continue using AI SDK v5 models
11
- - Rename all your `streamVNext()` and `generateVNext()` calls to `stream()` and `generate()` respectively. No other change is needed.
12
-
13
- ### Upgrade from AI SDK v4 models to v5 models
14
-
15
- First bump all your model provider packages by a major version. This will ensure that they are all v5 models now. Follow the guide below to understand the differences.
16
-
17
- ## Key Differences
18
-
19
- ### 1. Model version support
20
-
21
- - **Legacy APIs (`generateLegacy`, `streamLegacy`)**: Only support AI SDK v4 models (specificationVersion: 'v1')
22
- - **Current APIs (`generate`, `stream`)**: Only support AI SDK v5 models (specificationVersion: 'v2')
23
- - This is enforced at runtime with clear error messages
24
-
25
- ### 2. Return types
26
-
27
- #### Legacy methods return AI SDK v4 types
28
-
29
- - **`generateLegacy()`**:
30
- - `GenerateTextResult` or `GenerateObjectResult`
31
-
32
- - **`streamLegacy()`**:
33
- - `StreamTextResult` or `StreamObjectResult`
34
-
35
- #### New stream methods return Mastra/AI SDK v5 types
36
-
37
- - **`generate()`**:
38
- - When `format: 'mastra'` (default): Returns `MastraModelOutput.getFullOutput()` result
39
- - When `format: 'aisdk'`: Returns `AISDKV5OutputStream.getFullOutput()` result (AI SDK v5 compatible)
40
- - Internally calls `stream()` and awaits `getFullOutput()`
41
-
42
- - **`stream()`**:
43
- - When `format: 'mastra'` (default): Returns `MastraModelOutput<OUTPUT>`
44
- - When `format: 'aisdk'`: Returns `AISDKV5OutputStream<OUTPUT>` (AI SDK v5 compatible)
45
-
46
- #### Format Control
47
-
48
- - **Legacy**: No format control, always returns AI SDK v4 types
49
- - **New stream**: Can choose format via `format` option ('mastra' or 'aisdk')
50
-
51
- ```typescript
52
- // Mastra native format (default)
53
- const result = await agent.stream(messages, {
54
- format: 'mastra'
55
- });
56
-
57
- // AI SDK v5 compatibility
58
- const result = await agent.stream(messages, {
59
- format: 'aisdk'
60
- });
61
- ```
62
-
63
- ### 3. New Options in Non-Legacy APIs
64
-
65
- The following options are available in `stream()` and `generate()` but NOT in their legacy counterparts:
66
-
67
- 1. **`format`** - Choose between 'mastra' or 'aisdk' output format
68
-
69
- ```typescript
70
- const result = await agent.stream(messages, {
71
- format: 'aisdk' // or 'mastra' (default)
72
- });
73
- ```
74
-
75
- 2. **`system`** - Custom system message (separate from instructions)
76
-
77
- ```typescript
78
- const result = await agent.stream(messages, {
79
- system: 'You are a helpful assistant'
80
- });
81
- ```
82
-
83
- 3. **`structuredOutput`** - Enhanced structured output with model override and custom options
84
-
85
- - If no model is added it will use the agent's default model.
86
- - Error strategy when the object does not conform to the schema is `warn` (log a warning), `error` (throw an error), or `fallback` (return a default fallback value of your choice).
87
-
88
- ```typescript
89
- const result = await agent.generate(messages, {
90
- structuredOutput: {
91
- schema: z.object({
92
- name: z.string(),
93
- age: z.number()
94
- }),
95
- model: openai('gpt-4o-mini'), // Optional model override for structuring
96
- errorStrategy: 'fallback',
97
- fallbackValue: { name: 'unknown', age: 0 },
98
- instructions: 'Extract user information' // Override default structuring instructions
99
- }
100
- });
101
- ```
102
-
103
- 4. **`stopWhen`** - Flexible stop conditions (step count, token limit, etc.)
104
-
105
- ```typescript
106
- const result = await agent.stream(messages, {
107
- stopWhen: ({ steps, totalTokens }) => steps >= 5 || totalTokens >= 10000
108
- });
109
- ```
110
-
111
- 5. **`providerOptions`** - Provider-specific options (e.g., OpenAI-specific settings)
112
-
113
- ```typescript
114
- const result = await agent.stream(messages, {
115
- providerOptions: {
116
- openai: {
117
- store: true,
118
- metadata: { userId: '123' }
119
- }
120
- }
121
- });
122
- ```
123
-
124
- 6. **`onChunk`** - Callback for each streaming chunk
125
-
126
- ```typescript
127
- const result = await agent.stream(messages, {
128
- onChunk: (chunk) => {
129
- console.log('Received chunk:', chunk);
130
- }
131
- });
132
- ```
133
-
134
- 7. **`onError`** - Error callback
135
-
136
- ```typescript
137
- const result = await agent.stream(messages, {
138
- onError: (error) => {
139
- console.error('Stream error:', error);
140
- }
141
- });
142
- ```
143
-
144
- 8. **`onAbort`** - Abort callback
145
-
146
- ```typescript
147
- const result = await agent.stream(messages, {
148
- onAbort: () => {
149
- console.log('Stream aborted');
150
- }
151
- });
152
- ```
153
-
154
- 9. **`activeTools`** - Specify which tools are active for this execution
155
-
156
- ```typescript
157
- const result = await agent.stream(messages, {
158
- activeTools: ['search', 'calculator'] // Only these tools will be available
159
- });
160
- ```
161
-
162
- 10. **`abortSignal`** - AbortSignal for cancellation
163
-
164
- ```typescript
165
- const controller = new AbortController();
166
- const result = await agent.stream(messages, {
167
- abortSignal: controller.signal
168
- });
169
-
170
- // Later: controller.abort();
171
- ```
172
-
173
- 11. **`prepareStep`** - Callback before each step in multi-step execution
174
-
175
- ```typescript
176
- const result = await agent.stream(messages, {
177
- prepareStep: ({ step, state }) => {
178
- console.log('About to execute step:', step);
179
- return { /* modified state */ };
180
- }
181
- });
182
- ```
183
-
184
- 12. **`requireToolApproval`** - Require approval for all tool calls
185
-
186
- ```typescript
187
- const result = await agent.stream(messages, {
188
- requireToolApproval: true
189
- });
190
- ```
191
-
192
- ### 4. Options That Still Exist But Have Been Moved
193
-
194
- #### `temperature` and Other Model Settings
195
-
196
- Unified in `modelSettings`
197
-
198
- ```typescript
199
- const result = await agent.stream(messages, {
200
- modelSettings: {
201
- temperature: 0.7,
202
- maxTokens: 1000,
203
- topP: 0.9
204
- }
205
- });
206
- ```
207
-
208
- #### `resourceId` and `threadId`
209
-
210
- Moved to memory object.
211
-
212
- ```typescript
213
- const result = await agent.stream(messages, {
214
- memory: {
215
- resource: 'user-123',
216
- thread: 'thread-456'
217
- }
218
- });
219
- ```
220
-
221
- ### 5. Options That Are Deprecated or Removed
222
-
223
- #### `experimental_output`
224
-
225
- Use `structuredOutput` instead to allow for tool calls and an object return.
226
-
227
- ```typescript
228
- const result = await agent.generate(messages, {
229
- structuredOutput: {
230
- schema: z.object({
231
- summary: z.string()
232
- })
233
- }
234
- });
235
- ```
236
-
237
- #### `output`
238
-
239
- The `output` property is deprecated in favor of `structuredOutput`, to achieve the same results use maxSteps 1 with `structuredOutput`.
240
-
241
- ```typescript
242
- const result = await agent.generate(messages, {
243
- structuredOutput: {
244
- schema: {
245
- z.object({
246
- name: z.string()
247
- })
248
- }
249
- },
250
- maxSteps: 1
251
- });
252
- ```
253
-
254
- #### `memoryOptions` was removed
255
-
256
- Use `memory` instead
257
- ```typescript
258
- const result = await agent.generate(messages, {
259
- memory: {
260
- ...
261
- }
262
- });
263
- ```
264
-
265
- ### 6. Type Changes
266
-
267
- #### `context`
268
-
269
- - **Legacy**: `CoreMessage[]`
270
- - **New format**: `ModelMessage[]`
271
-
272
- #### `toolChoice` uses the AI SDK v5 `ToolChoice` type
273
-
274
- ```typescript
275
- type ToolChoice<TOOLS extends Record<string, unknown>> = 'auto' | 'none' | 'required' | {
276
- type: 'tool';
277
- toolName: Extract<keyof TOOLS, string>;
278
- };
279
- ```
280
-
281
-
282
- ## Migration Checklist
283
-
284
- ### If you're already using `streamVNext` and `generateVNext`
285
-
286
- Just find/replace the methods to `stream` and `generate` respectively.
287
-
288
- ### If you're using the old `stream` and `generate`
289
-
290
- Decide whether you want to upgrade or not. If you don't, just find/replace to `streamLegacy` and `generateLegacy`.
291
-
@@ -1,63 +0,0 @@
1
- ---
2
- title: "Runtime context | Tools & MCP | Mastra Docs"
3
- description: Learn how to use Mastra's RuntimeContext to provide dynamic, request-specific configuration to tools.
4
- ---
5
-
6
- import { Callout } from "nextra/components";
7
-
8
- # Tool Runtime Context
9
-
10
- Mastra provides `RuntimeContext`, a dependency injection system that lets you configure tools with runtime variables. If you find yourself creating multiple tools that perform similar tasks, runtime context allows you to consolidate them into a single, more flexible tool.
11
-
12
- ## Overview
13
-
14
- The dependency injection system allows you to:
15
-
16
- 1. Pass runtime configuration variables to tools through a type-safe `runtimeContext`.
17
- 2. Access these variables within tool execution context.
18
- 3. Modify tool behavior without changing the underlying code.
19
- 4. Share configuration across multiple tools within the same agent.
20
-
21
- <Callout>
22
- **Note:** `RuntimeContext` is primarily used for passing data *into* tool
23
- executions. It's distinct from agent memory, which handles conversation
24
- history and state persistence across multiple calls.
25
- </Callout>
26
-
27
-
28
- ## Accessing `runtimeContext` in tools
29
-
30
- Tools can access the same `runtimeContext` used by their parent agent, allowing them to adjust behavior based on runtime configuration. In this example, the `temperature-unit` is retrieved within the tool’s `execute` function to ensure consistent formatting with the agent’s instructions.
31
-
32
- ```typescript {14-15} filename="src/mastra/tools/test-weather-tool" showLineNumbers copy
33
- import { createTool } from "@mastra/core/tools";
34
- import { z } from "zod";
35
-
36
- type WeatherRuntimeContext = {
37
- "temperature-unit": "celsius" | "fahrenheit";
38
- };
39
-
40
- export const testWeatherTool = createTool({
41
- id: "getWeather",
42
- description: "Get the current weather for a location",
43
- inputSchema: z.object({
44
- location: z.string().describe("The location to get weather for")
45
- }),
46
- execute: async ({ context, runtimeContext }) => {
47
- const temperatureUnit = runtimeContext.get("temperature-unit") as WeatherRuntimeContext["temperature-unit"];
48
-
49
- const weather = await fetchWeather(context.location, temperatureUnit);
50
-
51
- return { result: weather };
52
- }
53
- });
54
-
55
- async function fetchWeather(location: string, temperatureUnit: WeatherRuntimeContext["temperature-unit"]) {
56
- // ...
57
- }
58
- ```
59
-
60
-
61
- ## Related
62
-
63
- [Agent Runtime Context](../agents/runtime-context.mdx)