@mastra/mcp-docs-server 0.13.30-alpha.0 → 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 (65) hide show
  1. package/.docs/organized/changelogs/%40mastra%2Fagent-builder.md +9 -9
  2. package/.docs/organized/changelogs/%40mastra%2Fai-sdk.md +15 -0
  3. package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +8 -8
  4. package/.docs/organized/changelogs/%40mastra%2Fcore.md +29 -29
  5. package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +9 -9
  6. package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +16 -16
  7. package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +8 -8
  8. package/.docs/organized/changelogs/%40mastra%2Fmemory.md +16 -16
  9. package/.docs/organized/changelogs/%40mastra%2Fpg.md +16 -16
  10. package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +22 -22
  11. package/.docs/organized/changelogs/%40mastra%2Freact.md +13 -0
  12. package/.docs/organized/changelogs/%40mastra%2Fserver.md +8 -8
  13. package/.docs/organized/changelogs/create-mastra.md +19 -19
  14. package/.docs/organized/changelogs/mastra.md +27 -27
  15. package/.docs/organized/code-examples/agent.md +0 -1
  16. package/.docs/organized/code-examples/agui.md +2 -2
  17. package/.docs/organized/code-examples/client-side-tools.md +2 -2
  18. package/.docs/raw/agents/adding-voice.mdx +118 -25
  19. package/.docs/raw/agents/agent-memory.mdx +73 -89
  20. package/.docs/raw/agents/guardrails.mdx +1 -1
  21. package/.docs/raw/agents/overview.mdx +39 -7
  22. package/.docs/raw/agents/using-tools.mdx +95 -0
  23. package/.docs/raw/deployment/overview.mdx +9 -11
  24. package/.docs/raw/frameworks/agentic-uis/ai-sdk.mdx +1 -1
  25. package/.docs/raw/frameworks/servers/express.mdx +2 -2
  26. package/.docs/raw/getting-started/installation.mdx +34 -85
  27. package/.docs/raw/getting-started/mcp-docs-server.mdx +13 -1
  28. package/.docs/raw/index.mdx +49 -14
  29. package/.docs/raw/observability/ai-tracing/exporters/otel.mdx +3 -0
  30. package/.docs/raw/reference/observability/ai-tracing/exporters/otel.mdx +6 -0
  31. package/.docs/raw/reference/scorers/answer-relevancy.mdx +105 -7
  32. package/.docs/raw/reference/scorers/answer-similarity.mdx +266 -16
  33. package/.docs/raw/reference/scorers/bias.mdx +107 -6
  34. package/.docs/raw/reference/scorers/completeness.mdx +131 -8
  35. package/.docs/raw/reference/scorers/content-similarity.mdx +107 -8
  36. package/.docs/raw/reference/scorers/context-precision.mdx +234 -18
  37. package/.docs/raw/reference/scorers/context-relevance.mdx +418 -35
  38. package/.docs/raw/reference/scorers/faithfulness.mdx +122 -8
  39. package/.docs/raw/reference/scorers/hallucination.mdx +125 -8
  40. package/.docs/raw/reference/scorers/keyword-coverage.mdx +141 -9
  41. package/.docs/raw/reference/scorers/noise-sensitivity.mdx +478 -6
  42. package/.docs/raw/reference/scorers/prompt-alignment.mdx +351 -102
  43. package/.docs/raw/reference/scorers/textual-difference.mdx +134 -6
  44. package/.docs/raw/reference/scorers/tone-consistency.mdx +133 -0
  45. package/.docs/raw/reference/scorers/tool-call-accuracy.mdx +422 -65
  46. package/.docs/raw/reference/scorers/toxicity.mdx +125 -7
  47. package/.docs/raw/reference/workflows/workflow.mdx +33 -0
  48. package/.docs/raw/scorers/custom-scorers.mdx +244 -3
  49. package/.docs/raw/scorers/overview.mdx +8 -38
  50. package/.docs/raw/server-db/middleware.mdx +5 -2
  51. package/.docs/raw/server-db/runtime-context.mdx +178 -0
  52. package/.docs/raw/streaming/workflow-streaming.mdx +5 -1
  53. package/.docs/raw/tools-mcp/overview.mdx +25 -7
  54. package/.docs/raw/workflows/overview.mdx +28 -1
  55. package/CHANGELOG.md +7 -0
  56. package/package.json +4 -4
  57. package/.docs/raw/agents/runtime-context.mdx +0 -106
  58. package/.docs/raw/agents/using-tools-and-mcp.mdx +0 -241
  59. package/.docs/raw/getting-started/model-providers.mdx +0 -63
  60. package/.docs/raw/tools-mcp/runtime-context.mdx +0 -63
  61. /package/.docs/raw/{evals → scorers/evals-old-api}/custom-eval.mdx +0 -0
  62. /package/.docs/raw/{evals → scorers/evals-old-api}/overview.mdx +0 -0
  63. /package/.docs/raw/{evals → scorers/evals-old-api}/running-in-ci.mdx +0 -0
  64. /package/.docs/raw/{evals → scorers/evals-old-api}/textual-evals.mdx +0 -0
  65. /package/.docs/raw/{server-db → workflows}/snapshots.mdx +0 -0
@@ -0,0 +1,178 @@
1
+ ---
2
+ title: "Runtime Context | Agents | Mastra Docs"
3
+ description: Learn how to use Mastra's RuntimeContext to provide dynamic, request-specific configuration to agents.
4
+ ---
5
+
6
+ import { Callout } from "nextra/components";
7
+
8
+ # Runtime Context
9
+
10
+ Agents, tools, and workflows can all accept `RuntimeContext` as a parameter, making request-specific values available to the underlying primitives.
11
+
12
+ ## When to use `RuntimeContext`
13
+
14
+ Use `RuntimeContext` when a primitive’s behavior should change based on runtime conditions. For example, you might switch models or storage backends based on user attributes, or adjust instructions and tool selection based on language.
15
+
16
+ <Callout>
17
+ **Note:** `RuntimeContext` is primarily used for passing data into specific
18
+ requests. It's distinct from agent memory, which handles conversation
19
+ history and state persistence across multiple calls.
20
+ </Callout>
21
+
22
+ ## Setting values
23
+
24
+ Pass `runtimeContext` into an agent, workflow, or tool call to make values available to all underlying primitives during execution. Use `.set()` to define values before making the call.
25
+
26
+ The `.set()` method takes two arguments:
27
+
28
+ 1. **key**: The name used to identify the value.
29
+ 2. **value**: The data to associate with that key.
30
+
31
+ ```typescript {7,9,13,21,28} showLineNumbers
32
+ import { RuntimeContext } from "@mastra/core/runtime-context";
33
+
34
+ export type UserTier = {
35
+ "user-tier": "enterprise" | "pro";
36
+ };
37
+
38
+ const runtimeContext = new RuntimeContext<UserTier>();
39
+
40
+ runtimeContext.set("user-tier", "enterprise");
41
+
42
+ const agent = mastra.getAgent("weatherAgent");
43
+ await agent.generate("What's the weather in London?", {
44
+ runtimeContext
45
+ })
46
+
47
+ const run = await mastra.getWorkflow("weatherWorkflow").createRunAsync();
48
+ await run.start({
49
+ inputData: {
50
+ location: "London"
51
+ },
52
+ runtimeContext
53
+ });
54
+
55
+ await weatherTool.execute({
56
+ context: {
57
+ location: "London"
58
+ },
59
+ runtimeContext
60
+ });
61
+ ```
62
+
63
+ ### Setting values based on request headers
64
+
65
+ You can populate `runtimeContext` dynamically in server middleware by extracting information from the request. In this example, the `temperature-unit` is set based on the Cloudflare `CF-IPCountry` header to ensure responses match the user's locale.
66
+
67
+ ```typescript filename="src/mastra/index.ts" showLineNumbers copy
68
+ import { Mastra } from "@mastra/core/mastra";
69
+ import { RuntimeContext } from "@mastra/core/runtime-context";
70
+ import { testWeatherAgent } from "./agents/test-weather-agent";
71
+
72
+ export const mastra = new Mastra({
73
+ agents: { testWeatherAgent },
74
+ server: {
75
+ middleware: [
76
+ async (context, next) => {
77
+ const country = context.req.header("CF-IPCountry");
78
+ const runtimeContext = context.get("runtimeContext");
79
+
80
+ runtimeContext.set("temperature-unit", country === "US" ? "fahrenheit" : "celsius");
81
+
82
+ await next();
83
+ }
84
+ ]
85
+ }
86
+ });
87
+ ```
88
+
89
+ > See [Middleware](../server-db/middleware.mdx) for how to use server middleware.
90
+
91
+ ## Accessing values with agents
92
+
93
+ You can access the `runtimeContext` argument from any supported configuration options in agents. These functions can be sync or `async`. Use the `.get()` method to read values from `runtimeContext`.
94
+
95
+ ```typescript {7-8,15,18,21} filename="src/mastra/agents/weather-agent.ts" showLineNumbers
96
+ export type UserTier = {
97
+ "user-tier": "enterprise" | "pro";
98
+ };
99
+
100
+ export const weatherAgent = new Agent({
101
+ name: "weather-agent",
102
+ instructions: async ({ runtimeContext }) => {
103
+ const userTier = runtimeContext.get("user-tier") as UserTier["user-tier"];
104
+
105
+ if (userTier === "enterprise") {
106
+ // ...
107
+ }
108
+ // ...
109
+ },
110
+ model: ({ runtimeContext }) => {
111
+ // ...
112
+ },
113
+ tools: ({ runtimeContext }) => {
114
+ // ...
115
+ },
116
+ memory: ({ runtimeContext }) => {
117
+ // ...
118
+ },
119
+ });
120
+ ```
121
+
122
+ You can also use `runtimeContext` with other options like `agents`, `workflows`, `scorers`, `inputProcessors`, and `outputProcessors`.
123
+
124
+ > See [Agent](../../reference/agents/agent.mdx) for a full list of configuration options.
125
+
126
+ ## Accessing values from workflow steps
127
+
128
+ You can access the `runtimeContext` argument from a workflow step's `execute` function. This function can be sync or async. Use the `.get()` method to read values from `runtimeContext`.
129
+
130
+ ```typescript {7-8} filename="src/mastra/workflows/weather-workflow.ts" showLineNumbers copy
131
+ export type UserTier = {
132
+ "user-tier": "enterprise" | "pro";
133
+ };
134
+
135
+ const stepOne = createStep({
136
+ id: "step-one",
137
+ execute: async ({ runtimeContext }) => {
138
+ const userTier = runtimeContext.get("user-tier") as UserTier["user-tier"];
139
+
140
+ if (userTier === "enterprise") {
141
+ // ...
142
+ }
143
+ // ...
144
+ }
145
+ });
146
+ ```
147
+
148
+ > See [createStep()](../../reference/workflows/step.mdx) for a full list of configuration options.
149
+
150
+ ## Accessing values with tools
151
+
152
+ You can access the `runtimeContext` argument from a tool’s `execute` function. This function is `async`. Use the `.get()` method to read values from `runtimeContext`.
153
+
154
+ ```typescript {7-8} filename="src/mastra/tools/weather-tool.ts" showLineNumbers
155
+ export type UserTier = {
156
+ "user-tier": "enterprise" | "pro";
157
+ };
158
+
159
+ export const weatherTool = createTool({
160
+ id: "weather-tool",
161
+ execute: async ({ runtimeContext }) => {
162
+ const userTier = runtimeContext.get("user-tier") as UserTier["user-tier"];
163
+
164
+ if (userTier === "enterprise") {
165
+ // ...
166
+ }
167
+ // ...
168
+ }
169
+ });
170
+ ```
171
+
172
+ > See [createTool()](../../reference/tools/create-tool.mdx) for a full list of configuration options.
173
+
174
+ ## Related
175
+
176
+ - [RuntimeContext Example](../../examples/agents/runtime-context.mdx)
177
+ - [Tool Runtime Context](../tools-mcp/runtime-context.mdx)
178
+ - [Server Middleware Runtime Context](../server-db/middleware.mdx)
@@ -18,6 +18,10 @@ By combining writable workflow streams with agent streaming, you gain fine-grain
18
18
 
19
19
  ### Using the `writer` argument
20
20
 
21
+ <Callout type="warning">
22
+ The writer is only available when using `streamVNext`.
23
+ </Callout>
24
+
21
25
  The `writer` argument is passed to a workflow step's `execute` function and can be used to emit custom events, data, or values into the active stream. This enables workflow steps to provide intermediate results or status updates while execution is still in progress.
22
26
 
23
27
  <Callout type="warning">
@@ -60,7 +64,7 @@ const testWorkflow = mastra.getWorkflow("testWorkflow");
60
64
 
61
65
  const run = await testWorkflow.createRunAsync();
62
66
 
63
- const stream = await run.stream({
67
+ const stream = await run.streamVNext({
64
68
  inputData: {
65
69
  value: "initial data"
66
70
  }
@@ -56,18 +56,36 @@ When creating tools, keep tool descriptions simple and focused on **what** the t
56
56
 
57
57
  To make tools available to an agent, you configure them in the agent's definition. Mentioning available tools and their general purpose in the agent's system prompt can also improve tool usage. For detailed steps and examples, see the guide on [Using Tools and MCP with Agents](/docs/agents/using-tools-and-mcp#add-tools-to-an-agent).
58
58
 
59
- ## Compatibility Layer for Tool Schemas
59
+ ## Using `RuntimeContext`
60
60
 
61
- Different models interpret schemas differently. Some error when certain schema properties are passed and some ignore certain schema properties but don't throw an error. Mastra adds a compatibility layer for tool schemas, ensuring tools work consistently across different model providers and that the schema constraints are respected.
61
+ Use `RuntimeContext` to access request-specific values. This lets you conditionally adjust behavior based on the context of the request.
62
62
 
63
- Some providers that we include this layer for:
63
+ ```typescript filename="src/mastra/tools/test-tool.ts" showLineNumbers
64
+ export type UserTier = {
65
+ "user-tier": "enterprise" | "pro";
66
+ };
67
+
68
+ const advancedTools = () => {
69
+ // ...
70
+ };
64
71
 
65
- - **Google Gemini & Anthropic:** Remove unsupported schema properties and append relevant constraints to the tool description.
66
- - **OpenAI (including reasoning models):** Strip or adapt schema fields that are ignored or unsupported, and add instructions to the description for agent guidance.
67
- - **DeepSeek & Meta:** Apply similar compatibility logic to ensure schema alignment and tool usability.
72
+ const baseTools = () => {
73
+ // ...
74
+ };
68
75
 
69
- This approach makes tool usage more reliable and model-agnostic for both custom and MCP tools.
76
+ export const testTool = createTool({
77
+ // ...
78
+ execute: async ({ runtimeContext }) => {
79
+ const userTier = runtimeContext.get("user-tier") as UserTier["user-tier"];
80
+
81
+ return userTier === "enterprise"
82
+ ? advancedTools
83
+ : baseTools;
84
+ }
85
+ });
86
+ ```
70
87
 
88
+ > See [Runtime Context](../server-db/runtime-context.mdx) for more information.
71
89
 
72
90
  ## Testing tools locally
73
91
  There are two ways to run and test tools.
@@ -83,7 +83,11 @@ export const testWorkflow = createWorkflow({
83
83
  }),
84
84
  outputSchema: z.object({
85
85
  output: z.string()
86
- })
86
+ }),
87
+ options: {
88
+ // enables runtime validation of inputs and applies zod default values.
89
+ validateInputs: true,
90
+ }
87
91
  })
88
92
  .then(step1)
89
93
  .commit();
@@ -171,6 +175,29 @@ export const mastra = new Mastra({
171
175
  });
172
176
  ```
173
177
 
178
+ ## Using `RuntimeContext`
179
+
180
+ Use `RuntimeContext` to access request-specific values. This lets you conditionally adjust behavior based on the context of the request.
181
+
182
+ ```typescript filename="src/mastra/workflows/test-workflow.ts" showLineNumbers
183
+ export type UserTier = {
184
+ "user-tier": "enterprise" | "pro";
185
+ };
186
+
187
+ const stepOne = createStep({
188
+ // ...
189
+ execute: async ({ runtimeContext }) => {
190
+ const userTier = runtimeContext.get("user-tier") as UserTier["user-tier"];
191
+
192
+ const maxResults = userTier === "enterprise"
193
+ ? 1000
194
+ : 50;
195
+
196
+ return { maxResults };
197
+ }
198
+ });
199
+ ```
200
+
174
201
  ## Testing workflows locally
175
202
  There are two ways to run and test workflows.
176
203
 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @mastra/mcp-docs-server
2
2
 
3
+ ## 0.13.30-alpha.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [[`1ed9670`](https://github.com/mastra-ai/mastra/commit/1ed9670d3ca50cb60dc2e517738c5eef3968ed27), [`158381d`](https://github.com/mastra-ai/mastra/commit/158381d39335be934b81ef8a1947bccace492c25), [`fb703b9`](https://github.com/mastra-ai/mastra/commit/fb703b9634eeaff1a6eb2b5531ce0f9e8fb04727), [`37a2314`](https://github.com/mastra-ai/mastra/commit/37a23148e0e5a3b40d4f9f098b194671a8a49faf), [`05a9dee`](https://github.com/mastra-ai/mastra/commit/05a9dee3d355694d28847bfffb6289657fcf7dfa), [`e3c1077`](https://github.com/mastra-ai/mastra/commit/e3c107763aedd1643d3def5df450c235da9ff76c), [`1bccdb3`](https://github.com/mastra-ai/mastra/commit/1bccdb33eb90cbeba2dc5ece1c2561fb774b26b6), [`5ef944a`](https://github.com/mastra-ai/mastra/commit/5ef944a3721d93105675cac2b2311432ff8cc393), [`d6b186f`](https://github.com/mastra-ai/mastra/commit/d6b186fb08f1caf1b86f73d3a5ee88fb999ca3be), [`65493b3`](https://github.com/mastra-ai/mastra/commit/65493b31c36f6fdb78f9679f7e1ecf0c250aa5ee), [`a998b8f`](https://github.com/mastra-ai/mastra/commit/a998b8f858091c2ec47683e60766cf12d03001e4), [`8a37bdd`](https://github.com/mastra-ai/mastra/commit/8a37bddb6d8614a32c5b70303d583d80c620ea61)]:
8
+ - @mastra/core@0.21.0-alpha.1
9
+
3
10
  ## 0.13.30-alpha.0
4
11
 
5
12
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/mcp-docs-server",
3
- "version": "0.13.30-alpha.0",
3
+ "version": "0.13.30-alpha.1",
4
4
  "description": "MCP server for accessing Mastra.ai documentation, changelogs, and news.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -33,7 +33,7 @@
33
33
  "uuid": "^11.1.0",
34
34
  "zod": "^3.25.76",
35
35
  "zod-to-json-schema": "^3.24.6",
36
- "@mastra/core": "0.21.0-alpha.0",
36
+ "@mastra/core": "0.21.0-alpha.1",
37
37
  "@mastra/mcp": "^0.13.5-alpha.0"
38
38
  },
39
39
  "devDependencies": {
@@ -42,7 +42,7 @@
42
42
  "@types/node": "^20.19.0",
43
43
  "@types/turndown": "^5.0.5",
44
44
  "@wong2/mcp-cli": "^1.10.0",
45
- "cross-env": "^7.0.3",
45
+ "cross-env": "^10.1.0",
46
46
  "eslint": "^9.36.0",
47
47
  "hono": "^4.9.7",
48
48
  "tsup": "^8.5.0",
@@ -50,7 +50,7 @@
50
50
  "typescript": "^5.8.3",
51
51
  "vitest": "^3.2.4",
52
52
  "@internal/lint": "0.0.48",
53
- "@mastra/core": "0.21.0-alpha.0"
53
+ "@mastra/core": "0.21.0-alpha.1"
54
54
  },
55
55
  "homepage": "https://mastra.ai",
56
56
  "repository": {
@@ -1,106 +0,0 @@
1
- ---
2
- title: "Runtime Context | Agents | Mastra Docs"
3
- description: Learn how to use Mastra's RuntimeContext to provide dynamic, request-specific configuration to agents.
4
- ---
5
-
6
- # Runtime Context
7
-
8
- Agents use `RuntimeContext` to access request-specific values sent alongside user messages. These values let a single agent change its behavior, functionality, model, tools, or memory usage based on the needs of each request.
9
-
10
- ![Agents Runtime Context](/image/agents/agents-runtime-context.jpg)
11
-
12
- ## When to use `RuntimeContext`
13
-
14
- Use `RuntimeContext` when an agent's behavior should change based on the request or environment. For example, you might switch models or storage providers based on user details, or adjust instructions or tool selection based on the user's language.
15
-
16
- ## Configuring agents with `RuntimeContext`
17
-
18
- You can access the `runtimeContext` argument from any of the agent’s supported parameters. These functions can be sync or `async`, allowing you to use context values to fetch data, apply logic, or adjust behavior at runtime.
19
-
20
- ```typescript {3,6,9,12} filename="src/dynamic-agent.ts" showLineNumbers copy
21
- export const dynamicAgent = new Agent({
22
- name: "dynamic-agent",
23
- instructions: async ({ runtimeContext }) => {
24
- // ...
25
- },
26
- model: ({ runtimeContext }) => {
27
- // ...
28
- },
29
- tools: ({ runtimeContext }) => {
30
- // ...
31
- },
32
- memory: ({ runtimeContext }) => {
33
- // ...
34
- },
35
- });
36
- ```
37
-
38
- You can also use `runtimeContext` with other parameters like `agents`, `workflows`, `scorers`, `inputProcessors`, and `outputProcessors`.
39
-
40
- > See [Agent](../../reference/agents/agent.mdx) for a full list of configuration options.
41
-
42
- ## Setting values
43
-
44
- To set variables in `runtimeContext`, create a new instance using `new RuntimeContext()` and call `.set()` to define the values you want to include.
45
-
46
- The `.set()` method takes two arguments:
47
-
48
- 1. **key**: The name used to identify the value.
49
- 2. **value**: The data to associate with that key.
50
-
51
- After setting the values, pass the `runtimeContext` to `.generate()` or `.stream()` to make them available to the agent.
52
-
53
- ```typescript {8,11} filename="src/test-dynamic-agent.ts" showLineNumbers copy
54
- import { mastra } from "./mastra";
55
- import { RuntimeContext } from "@mastra/core/runtime-context";
56
- import { UserTier } from "./mastra/agents/dynamic-agent"
57
-
58
- const agent = mastra.getAgent("dynamicAgent");
59
-
60
- const runtimeContext = new RuntimeContext<UserTier>();
61
- runtimeContext.set("user-tier", "enterprise");
62
-
63
- const response = await agent.generate("Help plan my day.", {
64
- runtimeContext
65
- });
66
- ```
67
-
68
- ## Accessing values
69
-
70
- The example below accesses a `user-tier` value from `runtimeContext` to determine which model and instructions to use. The context is typed to provide safety and autocomplete when working with `.get()` and `.set()`.
71
-
72
- ```typescript {12,19} filename="src/mastra/agents/dynamic-agent.ts" showLineNumbers copy
73
- import { openai } from "@ai-sdk/openai";
74
- import { Agent } from "@mastra/core/agent";
75
- import { RuntimeContext } from "@mastra/core/runtime-context";
76
-
77
- export type UserTier = {
78
- "user-tier": "enterprise" | "pro";
79
- };
80
-
81
- export const dynamicAgent = new Agent({
82
- name: "dynamic-agent",
83
- instructions: async ({ runtimeContext }: { runtimeContext: RuntimeContext<UserTier> }) => {
84
- const userTier = runtimeContext.get("user-tier");
85
-
86
- const result = await db.query("SELECT instructions FROM config WHERE tier = ?", [userTier]);
87
-
88
- return result[0].instructions;
89
- },
90
- model: ({ runtimeContext }: { runtimeContext: RuntimeContext<UserTier> }) => {
91
- const userTier = runtimeContext.get("user-tier");
92
-
93
- return userTier === "enterprise"
94
- ? openai("gpt-4o-mini")
95
- : openai("gpt-4.1-nano");
96
- }
97
- });
98
- ```
99
-
100
- For a complete implementation example that demonstrates all runtime context capabilities including dynamic model selection, tools, memory, input/output processors, and quality scoring based on user subscription tiers, see our [Runtime Context Example](../../examples/agents/runtime-context.mdx).
101
-
102
- ## Related
103
-
104
- - [Runtime Context Example](../../examples/agents/runtime-context.mdx)
105
- - [Tool Runtime Context](../tools-mcp/runtime-context.mdx)
106
- - [Server Middleware Runtime Context](../server-db/middleware.mdx)
@@ -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).