@mastra/mcp-docs-server 0.13.19-alpha.1 → 0.13.19-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.
- package/.docs/organized/changelogs/%40mastra%2Fagent-builder.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +8 -8
- package/.docs/organized/changelogs/%40mastra%2Fcloud.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +13 -13
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +8 -0
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +9 -9
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +8 -8
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +12 -12
- package/.docs/raw/agents/dynamic-agents.mdx +5 -5
- package/.docs/raw/agents/runtime-context.mdx +103 -0
- package/.docs/raw/frameworks/agentic-uis/ai-sdk.mdx +1 -1
- package/.docs/raw/reference/agents/getDefaultGenerateOptions.mdx +1 -1
- package/.docs/raw/reference/agents/getDefaultStreamOptions.mdx +1 -1
- package/.docs/raw/reference/agents/getDefaultVNextStreamOptions.mdx +1 -1
- package/.docs/raw/reference/agents/getInstructions.mdx +1 -1
- package/.docs/raw/reference/agents/getLLM.mdx +1 -1
- package/.docs/raw/reference/agents/getMemory.mdx +1 -1
- package/.docs/raw/reference/agents/getModel.mdx +1 -1
- package/.docs/raw/reference/agents/getScorers.mdx +1 -1
- package/.docs/raw/reference/agents/streamVNext.mdx +120 -29
- package/.docs/raw/reference/tools/create-tool.mdx +1 -1
- package/.docs/raw/reference/tools/graph-rag-tool.mdx +2 -2
- package/.docs/raw/reference/tools/vector-query-tool.mdx +2 -2
- package/.docs/raw/tools-mcp/runtime-context.mdx +63 -0
- package/CHANGELOG.md +7 -0
- package/package.json +4 -4
- package/.docs/raw/agents/runtime-variables.mdx +0 -116
- package/.docs/raw/tools-mcp/dynamic-context.mdx +0 -131
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @mastra/agent-builder
|
|
2
2
|
|
|
3
|
+
## 0.0.3-alpha.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Move `zod` from `dependencies` to `devDependencies` and add it to `peerDependencies` ([#7585](https://github.com/mastra-ai/mastra/pull/7585))
|
|
8
|
+
|
|
9
|
+
- Updated dependencies [[`827d876`](https://github.com/mastra-ai/mastra/commit/827d8766f36a900afcaf64a040f7ba76249009b3), [`7eaf1d1`](https://github.com/mastra-ai/mastra/commit/7eaf1d1cec7e828d7a98efc2a748ac395bbdba3b), [`f3427cd`](https://github.com/mastra-ai/mastra/commit/f3427cdaf9eecd63360dfc897a4acbf5f4143a4e), [`05f13b8`](https://github.com/mastra-ai/mastra/commit/05f13b8fb269ccfc4de98e9db58dbe16eae55a5e)]:
|
|
10
|
+
- @mastra/core@0.16.1-alpha.2
|
|
11
|
+
|
|
3
12
|
## 0.0.3-alpha.0
|
|
4
13
|
|
|
5
14
|
### Patch Changes
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# @mastra/client-js
|
|
2
2
|
|
|
3
|
+
## 0.12.1-alpha.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [[`827d876`](https://github.com/mastra-ai/mastra/commit/827d8766f36a900afcaf64a040f7ba76249009b3), [`7eaf1d1`](https://github.com/mastra-ai/mastra/commit/7eaf1d1cec7e828d7a98efc2a748ac395bbdba3b), [`f3427cd`](https://github.com/mastra-ai/mastra/commit/f3427cdaf9eecd63360dfc897a4acbf5f4143a4e), [`05f13b8`](https://github.com/mastra-ai/mastra/commit/05f13b8fb269ccfc4de98e9db58dbe16eae55a5e)]:
|
|
8
|
+
- @mastra/core@0.16.1-alpha.2
|
|
9
|
+
|
|
3
10
|
## 0.12.1-alpha.1
|
|
4
11
|
|
|
5
12
|
### Patch Changes
|
|
@@ -291,12 +298,5 @@
|
|
|
291
298
|
|
|
292
299
|
- [#6949](https://github.com/mastra-ai/mastra/pull/6949) [`e6f5046`](https://github.com/mastra-ai/mastra/commit/e6f50467aff317e67e8bd74c485c3fbe2a5a6db1) Thanks [@CalebBarnes](https://github.com/CalebBarnes)! - stream/generate vnext: simplify internal output schema handling, improve types and typescript generics, and add jsdoc comments
|
|
293
300
|
|
|
294
|
-
- Updated dependencies [[`0778757`](https://github.com/mastra-ai/mastra/commit/07787570e4addbd501522037bd2542c3d9e26822), [`bf504a8`](https://github.com/mastra-ai/mastra/commit/bf504a833051f6f321d832cc7d631f3cb86d657b), [`e6f5046`](https://github.com/mastra-ai/mastra/commit/e6f50467aff317e67e8bd74c485c3fbe2a5a6db1), [`9613558`](https://github.com/mastra-ai/mastra/commit/9613558e6475f4710e05d1be7553a32ee7bddc20)]:
|
|
295
|
-
- @mastra/core@0.14.2-alpha.0
|
|
296
|
-
|
|
297
|
-
## 0.10.23
|
|
298
|
-
|
|
299
|
-
### Patch Changes
|
|
300
|
-
|
|
301
301
|
|
|
302
|
-
...
|
|
302
|
+
... 2416 more lines hidden. See full changelog in package directory.
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @mastra/cloud
|
|
2
2
|
|
|
3
|
+
## 0.1.13-alpha.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Rename cloud exporter to MastraCloudAITracingExporter and provide default value for trace endpoint ([#7600](https://github.com/mastra-ai/mastra/pull/7600))
|
|
8
|
+
|
|
9
|
+
- Updated dependencies [[`827d876`](https://github.com/mastra-ai/mastra/commit/827d8766f36a900afcaf64a040f7ba76249009b3), [`7eaf1d1`](https://github.com/mastra-ai/mastra/commit/7eaf1d1cec7e828d7a98efc2a748ac395bbdba3b), [`f3427cd`](https://github.com/mastra-ai/mastra/commit/f3427cdaf9eecd63360dfc897a4acbf5f4143a4e), [`05f13b8`](https://github.com/mastra-ai/mastra/commit/05f13b8fb269ccfc4de98e9db58dbe16eae55a5e)]:
|
|
10
|
+
- @mastra/core@0.16.1-alpha.2
|
|
11
|
+
|
|
3
12
|
## 0.1.12
|
|
4
13
|
|
|
5
14
|
### Patch Changes
|
|
@@ -289,14 +298,5 @@
|
|
|
289
298
|
|
|
290
299
|
- 63f6b7d: dependencies updates:
|
|
291
300
|
- Updated dependency [`@opentelemetry/otlp-transformer@^0.201.1` ↗︎](https://www.npmjs.com/package/@opentelemetry/otlp-transformer/v/0.201.1) (from `^0.201.0`, in `dependencies`)
|
|
292
|
-
- Updated dependency [`@opentelemetry/otlp-exporter-base@^0.201.1` ↗︎](https://www.npmjs.com/package/@opentelemetry/otlp-exporter-base/v/0.201.1) (from `^0.201.0`, in `dependencies`)
|
|
293
|
-
- Updated dependencies [63f6b7d]
|
|
294
|
-
- Updated dependencies [36f1c36]
|
|
295
|
-
- Updated dependencies [10d352e]
|
|
296
|
-
- Updated dependencies [53d3c37]
|
|
297
|
-
- @mastra/core@0.10.6-alpha.0
|
|
298
|
-
|
|
299
|
-
## 0.1.2
|
|
300
|
-
|
|
301
301
|
|
|
302
|
-
...
|
|
302
|
+
... 401 more lines hidden. See full changelog in package directory.
|
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @mastra/core
|
|
2
2
|
|
|
3
|
+
## 0.16.1-alpha.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Fixed provider defined tools for stream/generate vnext ([#7642](https://github.com/mastra-ai/mastra/pull/7642))
|
|
8
|
+
|
|
9
|
+
- Change to createRunAsync ([#7632](https://github.com/mastra-ai/mastra/pull/7632))
|
|
10
|
+
|
|
11
|
+
- Flatten loop config in stream options and pass to loop options ([#7643](https://github.com/mastra-ai/mastra/pull/7643))
|
|
12
|
+
|
|
13
|
+
- Fix issue with response message id consistency between stream/generate response and the message ids saveed in the DB. Also fixed the custom generatorId implementation to work with this. ([#7606](https://github.com/mastra-ai/mastra/pull/7606))
|
|
14
|
+
|
|
3
15
|
## 0.16.1-alpha.1
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -287,16 +299,4 @@
|
|
|
287
299
|
|
|
288
300
|
## 0.15.2
|
|
289
301
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
- Updated dependencies [[`c6113ed`](https://github.com/mastra-ai/mastra/commit/c6113ed7f9df297e130d94436ceee310273d6430)]:
|
|
293
|
-
- @mastra/schema-compat@0.11.1
|
|
294
|
-
|
|
295
|
-
## 0.15.0
|
|
296
|
-
|
|
297
|
-
### Minor Changes
|
|
298
|
-
|
|
299
|
-
- [#7032](https://github.com/mastra-ai/mastra/pull/7032) [`1191ce9`](https://github.com/mastra-ai/mastra/commit/1191ce946b40ed291e7877a349f8388e3cff7e5c) Thanks [@wardpeet](https://github.com/wardpeet)! - Bump zod peerdep to 3.25.0 to support both v3/v4
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
... 3082 more lines hidden. See full changelog in package directory.
|
|
302
|
+
... 3094 more lines hidden. See full changelog in package directory.
|
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# @mastra/deployer-cloud
|
|
2
2
|
|
|
3
|
+
## 0.16.1-alpha.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [[`827d876`](https://github.com/mastra-ai/mastra/commit/827d8766f36a900afcaf64a040f7ba76249009b3), [`7eaf1d1`](https://github.com/mastra-ai/mastra/commit/7eaf1d1cec7e828d7a98efc2a748ac395bbdba3b), [`f3427cd`](https://github.com/mastra-ai/mastra/commit/f3427cdaf9eecd63360dfc897a4acbf5f4143a4e), [`05f13b8`](https://github.com/mastra-ai/mastra/commit/05f13b8fb269ccfc4de98e9db58dbe16eae55a5e)]:
|
|
8
|
+
- @mastra/core@0.16.1-alpha.2
|
|
9
|
+
- @mastra/deployer@0.16.1-alpha.2
|
|
10
|
+
|
|
3
11
|
## 0.16.1-alpha.1
|
|
4
12
|
|
|
5
13
|
### Patch Changes
|
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# @mastra/deployer
|
|
2
2
|
|
|
3
|
+
## 0.16.1-alpha.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [[`827d876`](https://github.com/mastra-ai/mastra/commit/827d8766f36a900afcaf64a040f7ba76249009b3), [`7eaf1d1`](https://github.com/mastra-ai/mastra/commit/7eaf1d1cec7e828d7a98efc2a748ac395bbdba3b), [`f3427cd`](https://github.com/mastra-ai/mastra/commit/f3427cdaf9eecd63360dfc897a4acbf5f4143a4e), [`81d1383`](https://github.com/mastra-ai/mastra/commit/81d13836fe81c5f02a86e6f40416005898a405ba), [`05f13b8`](https://github.com/mastra-ai/mastra/commit/05f13b8fb269ccfc4de98e9db58dbe16eae55a5e)]:
|
|
8
|
+
- @mastra/core@0.16.1-alpha.2
|
|
9
|
+
- @mastra/server@0.16.1-alpha.2
|
|
10
|
+
|
|
3
11
|
## 0.16.1-alpha.1
|
|
4
12
|
|
|
5
13
|
### Patch Changes
|
|
@@ -291,12 +299,4 @@
|
|
|
291
299
|
|
|
292
300
|
### Patch Changes
|
|
293
301
|
|
|
294
|
-
|
|
295
|
-
- Updated dependency [`rollup@~4.46.4` ↗︎](https://www.npmjs.com/package/rollup/v/4.46.4) (from `~4.46.2`, in `dependencies`)
|
|
296
|
-
|
|
297
|
-
- [#6965](https://github.com/mastra-ai/mastra/pull/6965) [`2b38a60`](https://github.com/mastra-ai/mastra/commit/2b38a60da0c1153028d8241c7748b41c5fb81121) Thanks [@dane-ai-mastra](https://github.com/apps/dane-ai-mastra)! - dependencies updates:
|
|
298
|
-
- Updated dependency [`rollup@~4.47.1` ↗︎](https://www.npmjs.com/package/rollup/v/4.47.1) (from `~4.46.4`, in `dependencies`)
|
|
299
|
-
|
|
300
|
-
- [#6995](https://github.com/mastra-ai/mastra/pull/6995) [`681252d`](https://github.com/mastra-ai/mastra/commit/681252d20e57fcee6821377dea96cacab3bc230f) Thanks [@wardpeet](https://github.com/wardpeet)! - Improve type resolving
|
|
301
|
-
|
|
302
|
-
... 3927 more lines hidden. See full changelog in package directory.
|
|
302
|
+
... 3935 more lines hidden. See full changelog in package directory.
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# @mastra/mcp-docs-server
|
|
2
2
|
|
|
3
|
+
## 0.13.19-alpha.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [[`827d876`](https://github.com/mastra-ai/mastra/commit/827d8766f36a900afcaf64a040f7ba76249009b3), [`7eaf1d1`](https://github.com/mastra-ai/mastra/commit/7eaf1d1cec7e828d7a98efc2a748ac395bbdba3b), [`f3427cd`](https://github.com/mastra-ai/mastra/commit/f3427cdaf9eecd63360dfc897a4acbf5f4143a4e), [`05f13b8`](https://github.com/mastra-ai/mastra/commit/05f13b8fb269ccfc4de98e9db58dbe16eae55a5e)]:
|
|
8
|
+
- @mastra/core@0.16.1-alpha.2
|
|
9
|
+
|
|
3
10
|
## 0.13.19-alpha.1
|
|
4
11
|
|
|
5
12
|
### Patch Changes
|
|
@@ -291,12 +298,5 @@
|
|
|
291
298
|
|
|
292
299
|
### Patch Changes
|
|
293
300
|
|
|
294
|
-
- Updated dependencies [[`6e7e120`](https://github.com/mastra-ai/mastra/commit/6e7e1207d6e8d8b838f9024f90bd10df1181ba27), [`a5a23d9`](https://github.com/mastra-ai/mastra/commit/a5a23d981920d458dc6078919992a5338931ef02)]:
|
|
295
|
-
- @mastra/core@0.14.1-alpha.0
|
|
296
|
-
|
|
297
|
-
## 0.13.12
|
|
298
|
-
|
|
299
|
-
### Patch Changes
|
|
300
|
-
|
|
301
301
|
|
|
302
|
-
...
|
|
302
|
+
... 1670 more lines hidden. See full changelog in package directory.
|
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# @mastra/server
|
|
2
2
|
|
|
3
|
+
## 0.16.1-alpha.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Flatten loop config in stream options and pass to loop options ([#7643](https://github.com/mastra-ai/mastra/pull/7643))
|
|
8
|
+
|
|
9
|
+
- Fix a bug where `typescript` was bundled into `dist` ([#7585](https://github.com/mastra-ai/mastra/pull/7585))
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [[`827d876`](https://github.com/mastra-ai/mastra/commit/827d8766f36a900afcaf64a040f7ba76249009b3), [`7eaf1d1`](https://github.com/mastra-ai/mastra/commit/7eaf1d1cec7e828d7a98efc2a748ac395bbdba3b), [`f3427cd`](https://github.com/mastra-ai/mastra/commit/f3427cdaf9eecd63360dfc897a4acbf5f4143a4e), [`05f13b8`](https://github.com/mastra-ai/mastra/commit/05f13b8fb269ccfc4de98e9db58dbe16eae55a5e)]:
|
|
12
|
+
- @mastra/core@0.16.1-alpha.2
|
|
13
|
+
|
|
3
14
|
## 0.16.1-alpha.1
|
|
4
15
|
|
|
5
16
|
### Patch Changes
|
|
@@ -287,16 +298,5 @@
|
|
|
287
298
|
- Updated dependencies [[`943a7f3`](https://github.com/mastra-ai/mastra/commit/943a7f3dbc6a8ab3f9b7bc7c8a1c5b319c3d7f56), [`be49354`](https://github.com/mastra-ai/mastra/commit/be493546dca540101923ec700feb31f9a13939f2), [`d591ab3`](https://github.com/mastra-ai/mastra/commit/d591ab3ecc985c1870c0db347f8d7a20f7360536), [`ba82abe`](https://github.com/mastra-ai/mastra/commit/ba82abe76e869316bb5a9c95e8ea3946f3436fae), [`727f7e5`](https://github.com/mastra-ai/mastra/commit/727f7e5086e62e0dfe3356fb6dcd8bcb420af246), [`82d9f64`](https://github.com/mastra-ai/mastra/commit/82d9f647fbe4f0177320e7c05073fce88599aa95), [`4189486`](https://github.com/mastra-ai/mastra/commit/4189486c6718fda78347bdf4ce4d3fc33b2236e1), [`ca8ec2f`](https://github.com/mastra-ai/mastra/commit/ca8ec2f61884b9dfec5fc0d5f4f29d281ad13c01)]:
|
|
288
299
|
- @mastra/core@0.14.2-alpha.1
|
|
289
300
|
|
|
290
|
-
## 0.14.2-alpha.0
|
|
291
|
-
|
|
292
|
-
### Patch Changes
|
|
293
|
-
|
|
294
|
-
- [#6933](https://github.com/mastra-ai/mastra/pull/6933) [`bf504a8`](https://github.com/mastra-ai/mastra/commit/bf504a833051f6f321d832cc7d631f3cb86d657b) Thanks [@NikAiyer](https://github.com/NikAiyer)! - Add util functions for workflow server handlers and made processor process function async
|
|
295
|
-
|
|
296
|
-
- Updated dependencies [[`0778757`](https://github.com/mastra-ai/mastra/commit/07787570e4addbd501522037bd2542c3d9e26822), [`bf504a8`](https://github.com/mastra-ai/mastra/commit/bf504a833051f6f321d832cc7d631f3cb86d657b), [`e6f5046`](https://github.com/mastra-ai/mastra/commit/e6f50467aff317e67e8bd74c485c3fbe2a5a6db1), [`9613558`](https://github.com/mastra-ai/mastra/commit/9613558e6475f4710e05d1be7553a32ee7bddc20)]:
|
|
297
|
-
- @mastra/core@0.14.2-alpha.0
|
|
298
|
-
|
|
299
|
-
## 0.14.1
|
|
300
|
-
|
|
301
301
|
|
|
302
|
-
...
|
|
302
|
+
... 3231 more lines hidden. See full changelog in package directory.
|
|
@@ -5,7 +5,7 @@ description: Dynamically configure your agent's instruction, model, tools, and m
|
|
|
5
5
|
|
|
6
6
|
# Dynamic Agents
|
|
7
7
|
|
|
8
|
-
Dynamic agents use [runtime context](./runtime-
|
|
8
|
+
Dynamic agents use [runtime context](./runtime-context), like user IDs and other important parameters, to adjust their settings in real-time.
|
|
9
9
|
|
|
10
10
|
This means they can change the model they use, update their instructions, select different tools, and configure memory as needed.
|
|
11
11
|
|
|
@@ -25,12 +25,12 @@ const supportAgent = new Agent({
|
|
|
25
25
|
|
|
26
26
|
return `You are a customer support agent for our SaaS platform.
|
|
27
27
|
The current user is on the ${userTier} tier and prefers ${language} language.
|
|
28
|
-
|
|
28
|
+
|
|
29
29
|
For ${userTier} tier users:
|
|
30
30
|
${userTier === "free" ? "- Provide basic support and documentation links" : ""}
|
|
31
31
|
${userTier === "pro" ? "- Offer detailed technical support and best practices" : ""}
|
|
32
32
|
${userTier === "enterprise" ? "- Provide priority support with custom solutions" : ""}
|
|
33
|
-
|
|
33
|
+
|
|
34
34
|
Always respond in ${language} language.`;
|
|
35
35
|
},
|
|
36
36
|
|
|
@@ -58,7 +58,7 @@ const supportAgent = new Agent({
|
|
|
58
58
|
|
|
59
59
|
memory: ({ runtimeContext }) => {
|
|
60
60
|
const userTier = runtimeContext.get("user-tier");
|
|
61
|
-
|
|
61
|
+
|
|
62
62
|
if (userTier === "enterprise") {
|
|
63
63
|
return new Memory({
|
|
64
64
|
storage: new LibSQLStore({ url: "file:enterprise.db" }),
|
|
@@ -76,7 +76,7 @@ const supportAgent = new Agent({
|
|
|
76
76
|
},
|
|
77
77
|
});
|
|
78
78
|
}
|
|
79
|
-
|
|
79
|
+
|
|
80
80
|
// Basic memory for free tier
|
|
81
81
|
return new Memory({
|
|
82
82
|
storage: new LibSQLStore({ url: "file:free.db" }),
|
|
@@ -0,0 +1,103 @@
|
|
|
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
|
+
# Agent Runtime Context
|
|
7
|
+
|
|
8
|
+
Mastra provides `RuntimeContext`, a dependency injection system that lets you configure agents with runtime variables. If you find yourself creating multiple agents that perform similar tasks, runtime context allows you to consolidate them into a single, more flexible agent.
|
|
9
|
+
|
|
10
|
+
## Overview
|
|
11
|
+
|
|
12
|
+
The dependency injection system allows you to:
|
|
13
|
+
|
|
14
|
+
1. Pass runtime configuration variables to agents through a type-safe `runtimeContext`
|
|
15
|
+
2. Access these variables within tool execution contexts
|
|
16
|
+
3. Modify agent behavior without changing the underlying code
|
|
17
|
+
4. Share configuration across multiple tools within the same agent
|
|
18
|
+
|
|
19
|
+
## Using `runtimeContext` with agents
|
|
20
|
+
|
|
21
|
+
Agents can access `runtimeContext` via the `instructions` parameter, and retrieve variables using `runtimeContext.get()`. This allows agent behavior to adapt dynamically based on user input or external configuration, without changing the underlying implementation.
|
|
22
|
+
|
|
23
|
+
```typescript {6-7,14} filename="src/mastra/agents/test-weather-agent.ts" showLineNumbers copy
|
|
24
|
+
import { openai } from "@ai-sdk/openai";
|
|
25
|
+
import { Agent } from "@mastra/core/agent";
|
|
26
|
+
|
|
27
|
+
export const testWeatherAgent = new Agent({
|
|
28
|
+
name: "test-weather-agent",
|
|
29
|
+
instructions: async ({ runtimeContext }) => {
|
|
30
|
+
const temperatureUnit = runtimeContext.get("temperature-unit");
|
|
31
|
+
|
|
32
|
+
return `You are a weather assistant that provides current weather information for any city.
|
|
33
|
+
|
|
34
|
+
When a user asks for weather:
|
|
35
|
+
- Extract the city name from their request
|
|
36
|
+
- Respond with: temperature, feels-like temperature, humidity, wind speed, and conditions
|
|
37
|
+
- Use ${temperatureUnit} for all temperature values
|
|
38
|
+
- If no city is mentioned, ask for a city name
|
|
39
|
+
`;
|
|
40
|
+
},
|
|
41
|
+
model: openai("gpt-4o-mini")
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Example usage
|
|
46
|
+
|
|
47
|
+
In this example, `temperature-unit` is set using `runtimeContext.set()` to either **celsius** or **fahrenheit**, allowing the agent to respond with temperatures in the appropriate unit.
|
|
48
|
+
|
|
49
|
+
```typescript {12} filename="src/test-weather-agent.ts" showLineNumbers copy
|
|
50
|
+
import { mastra } from "./mastra";
|
|
51
|
+
import { RuntimeContext } from "@mastra/core/runtime-context";
|
|
52
|
+
|
|
53
|
+
const agent = mastra.getAgent("testWeatherAgent");
|
|
54
|
+
|
|
55
|
+
export type WeatherRuntimeContext = {
|
|
56
|
+
"temperature-unit": "celsius" | "fahrenheit";
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const runtimeContext = new RuntimeContext<WeatherRuntimeContext>();
|
|
60
|
+
|
|
61
|
+
runtimeContext.set("temperature-unit", "fahrenheit");
|
|
62
|
+
|
|
63
|
+
const response = await agent.generate("What's the weather in London?", {
|
|
64
|
+
runtimeContext
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
console.log(response.text);
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Accessing `runtimeContext` with server middleware
|
|
71
|
+
|
|
72
|
+
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.
|
|
73
|
+
|
|
74
|
+
```typescript {2,12-21} filename="src/mastra/index.ts" showLineNumbers copy
|
|
75
|
+
import { Mastra } from "@mastra/core/mastra";
|
|
76
|
+
import { RuntimeContext } from "@mastra/core/runtime-context";
|
|
77
|
+
import { testWeatherAgent } from "./agents/test-weather-agent";
|
|
78
|
+
|
|
79
|
+
type WeatherRuntimeContext = {
|
|
80
|
+
"temperature-unit": "celsius" | "fahrenheit";
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const mastra = new Mastra({
|
|
84
|
+
agents: { testWeatherAgent },
|
|
85
|
+
server: {
|
|
86
|
+
middleware: [
|
|
87
|
+
async (context, next) => {
|
|
88
|
+
const country = context.req.header("CF-IPCountry");
|
|
89
|
+
const runtimeContext = context.get("runtimeContext") as RuntimeContext<WeatherRuntimeContext>;
|
|
90
|
+
|
|
91
|
+
runtimeContext.set("temperature-unit", country === "US" ? "fahrenheit" : "celsius");
|
|
92
|
+
|
|
93
|
+
await next();
|
|
94
|
+
}
|
|
95
|
+
]
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Related
|
|
101
|
+
|
|
102
|
+
- [Dynamic Agents](./dynamic-agents.mdx)
|
|
103
|
+
- [Tool Runtime Context](../tools-mcp/runtime-context.mdx)
|
|
@@ -287,7 +287,7 @@ export const mastra = new Mastra({
|
|
|
287
287
|
});
|
|
288
288
|
```
|
|
289
289
|
|
|
290
|
-
> You can then access this data in your tools via the `runtimeContext` parameter. See the [Runtime Context documentation](/docs/agents/runtime-
|
|
290
|
+
> You can then access this data in your tools via the `runtimeContext` parameter. See the [Agent Runtime Context documentation](/docs/agents/runtime-context) for more details.
|
|
291
291
|
|
|
292
292
|
## Streaming data
|
|
293
293
|
|
|
@@ -65,4 +65,4 @@ await agent.getDefaultStreamOptions({
|
|
|
65
65
|
|
|
66
66
|
- [Generating responses](../../docs/agents/overview.mdx#generating-responses)
|
|
67
67
|
- [Streaming responses](../../docs/agents/overview.mdx#streaming-responses)
|
|
68
|
-
- [Runtime
|
|
68
|
+
- [Agent Runtime Context](../../docs/agents/runtime-context.mdx)
|
|
@@ -64,4 +64,4 @@ await agent.getDefaultVNextStreamOptions({
|
|
|
64
64
|
## Related
|
|
65
65
|
|
|
66
66
|
- [Streaming with agents](../../docs/streaming/overview.mdx#streaming-with-agents)
|
|
67
|
-
- [Runtime
|
|
67
|
+
- [Agent Runtime Context](../../docs/agents/runtime-context.mdx)
|
|
@@ -41,7 +41,7 @@ const aiSdkStream = await agent.streamVNext("message for agent", {
|
|
|
41
41
|
]}
|
|
42
42
|
/>
|
|
43
43
|
|
|
44
|
-
### Options
|
|
44
|
+
### Options
|
|
45
45
|
|
|
46
46
|
<PropertiesTable
|
|
47
47
|
content={[
|
|
@@ -52,16 +52,99 @@ const aiSdkStream = await agent.streamVNext("message for agent", {
|
|
|
52
52
|
defaultValue: "'mastra'",
|
|
53
53
|
description: "Determines the output stream format. Use 'mastra' for Mastra's native format (default) or 'aisdk' for AI SDK v5 compatibility.",
|
|
54
54
|
},
|
|
55
|
+
{
|
|
56
|
+
name: "maxSteps",
|
|
57
|
+
type: "number",
|
|
58
|
+
isOptional: true,
|
|
59
|
+
description: "Maximum number of steps to run during execution.",
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
name: "scorers",
|
|
63
|
+
type: "MastraScorers | Record<string, { scorer: MastraScorer['name']; sampling?: ScoringSamplingConfig }>",
|
|
64
|
+
isOptional: true,
|
|
65
|
+
description: "Evaluation scorers to run on the execution results.",
|
|
66
|
+
properties: [
|
|
67
|
+
{
|
|
68
|
+
parameters: [{
|
|
69
|
+
name: "scorer",
|
|
70
|
+
type: "string",
|
|
71
|
+
isOptional: false,
|
|
72
|
+
description: "Name of the scorer to use."
|
|
73
|
+
}]
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
parameters: [{
|
|
77
|
+
name: "sampling",
|
|
78
|
+
type: "ScoringSamplingConfig",
|
|
79
|
+
isOptional: true,
|
|
80
|
+
description: "Sampling configuration for the scorer.",
|
|
81
|
+
properties: [
|
|
82
|
+
{
|
|
83
|
+
parameters: [{
|
|
84
|
+
name: "type",
|
|
85
|
+
type: "'none' | 'ratio'",
|
|
86
|
+
isOptional: false,
|
|
87
|
+
description: "Type of sampling strategy. Use 'none' to disable sampling or 'ratio' for percentage-based sampling."
|
|
88
|
+
}]
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
parameters: [{
|
|
92
|
+
name: "rate",
|
|
93
|
+
type: "number",
|
|
94
|
+
isOptional: true,
|
|
95
|
+
description: "Sampling rate (0-1). Required when type is 'ratio'."
|
|
96
|
+
}]
|
|
97
|
+
}
|
|
98
|
+
]
|
|
99
|
+
}]
|
|
100
|
+
}
|
|
101
|
+
]
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
name: "tracingContext",
|
|
105
|
+
type: "TracingContext",
|
|
106
|
+
isOptional: true,
|
|
107
|
+
description: "AI tracing context for span hierarchy and metadata.",
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
name: "returnScorerData",
|
|
111
|
+
type: "boolean",
|
|
112
|
+
isOptional: true,
|
|
113
|
+
description: "Whether to return detailed scoring data in the response.",
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
name: "onChunk",
|
|
117
|
+
type: "(chunk: ChunkType) => Promise<void> | void",
|
|
118
|
+
isOptional: true,
|
|
119
|
+
description: "Callback function called for each chunk during streaming."
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
name: "onError",
|
|
123
|
+
type: "({ error }: { error: Error | string }) => Promise<void> | void",
|
|
124
|
+
isOptional: true,
|
|
125
|
+
description: "Callback function called when an error occurs during streaming."
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
name: "onAbort",
|
|
129
|
+
type: "(event: any) => Promise<void> | void",
|
|
130
|
+
isOptional: true,
|
|
131
|
+
description: "Callback function called when the stream is aborted."
|
|
132
|
+
},
|
|
55
133
|
{
|
|
56
134
|
name: "abortSignal",
|
|
57
135
|
type: "AbortSignal",
|
|
58
136
|
isOptional: true,
|
|
59
|
-
description:
|
|
60
|
-
|
|
137
|
+
description: "Signal object that allows you to abort the agent's execution. When the signal is aborted, all ongoing operations will be terminated."
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
name: "activeTools",
|
|
141
|
+
type: "Array<keyof ToolSet> | undefined",
|
|
142
|
+
isOptional: true,
|
|
143
|
+
description: "Array of active tool names that can be used during execution."
|
|
61
144
|
},
|
|
62
145
|
{
|
|
63
146
|
name: "context",
|
|
64
|
-
type: "
|
|
147
|
+
type: "ModelMessage[]",
|
|
65
148
|
isOptional: true,
|
|
66
149
|
description: "Additional context messages to provide to the agent.",
|
|
67
150
|
},
|
|
@@ -83,8 +166,8 @@ const aiSdkStream = await agent.streamVNext("message for agent", {
|
|
|
83
166
|
parameters: [{
|
|
84
167
|
name: "model",
|
|
85
168
|
type: "MastraLanguageModel",
|
|
86
|
-
isOptional:
|
|
87
|
-
description: "Model to use for the internal structuring agent."
|
|
169
|
+
isOptional: true,
|
|
170
|
+
description: "Model to use for the internal structuring agent. If not provided, falls back to the agent's model."
|
|
88
171
|
}]
|
|
89
172
|
},
|
|
90
173
|
{
|
|
@@ -178,13 +261,6 @@ const aiSdkStream = await agent.streamVNext("message for agent", {
|
|
|
178
261
|
}
|
|
179
262
|
]
|
|
180
263
|
},
|
|
181
|
-
{
|
|
182
|
-
name: "maxRetries",
|
|
183
|
-
type: "number",
|
|
184
|
-
isOptional: true,
|
|
185
|
-
defaultValue: "2",
|
|
186
|
-
description: "Maximum number of retries. Set to 0 to disable retries.",
|
|
187
|
-
},
|
|
188
264
|
{
|
|
189
265
|
name: "memoryOptions",
|
|
190
266
|
type: "MemoryConfig",
|
|
@@ -271,10 +347,10 @@ const aiSdkStream = await agent.streamVNext("message for agent", {
|
|
|
271
347
|
},
|
|
272
348
|
{
|
|
273
349
|
parameters: [{
|
|
274
|
-
name: "
|
|
350
|
+
name: "maxRetries",
|
|
275
351
|
type: "number",
|
|
276
352
|
isOptional: true,
|
|
277
|
-
description: "Maximum number of
|
|
353
|
+
description: "Maximum number of retries for failed requests."
|
|
278
354
|
}]
|
|
279
355
|
},
|
|
280
356
|
{
|
|
@@ -317,14 +393,6 @@ const aiSdkStream = await agent.streamVNext("message for agent", {
|
|
|
317
393
|
description: "Stop sequences. If set, the model will stop generating text when one of the stop sequences is generated."
|
|
318
394
|
}]
|
|
319
395
|
},
|
|
320
|
-
{
|
|
321
|
-
parameters: [{
|
|
322
|
-
name: "seed",
|
|
323
|
-
type: "number",
|
|
324
|
-
isOptional: true,
|
|
325
|
-
description: "The seed (integer) to use for random sampling. If set and supported by the model, calls will generate deterministic results."
|
|
326
|
-
}]
|
|
327
|
-
}
|
|
328
396
|
]
|
|
329
397
|
},
|
|
330
398
|
{
|
|
@@ -443,12 +511,6 @@ const aiSdkStream = await agent.streamVNext("message for agent", {
|
|
|
443
511
|
isOptional: true,
|
|
444
512
|
description: "Runtime context for dependency injection and contextual information.",
|
|
445
513
|
},
|
|
446
|
-
{
|
|
447
|
-
name: "experimental_generateMessageId",
|
|
448
|
-
type: "IDGenerator",
|
|
449
|
-
isOptional: true,
|
|
450
|
-
description: "Generate a unique ID for each message.",
|
|
451
|
-
},
|
|
452
514
|
{
|
|
453
515
|
name: "stopWhen",
|
|
454
516
|
type: "StopCondition | StopCondition[]",
|
|
@@ -523,6 +585,35 @@ for await (const part of stream.fullStream) {
|
|
|
523
585
|
return stream.toUIMessageStreamResponse();
|
|
524
586
|
```
|
|
525
587
|
|
|
588
|
+
### Using Callbacks
|
|
589
|
+
|
|
590
|
+
All callback functions are now available as top-level properties for a cleaner API experience.
|
|
591
|
+
|
|
592
|
+
```typescript showLineNumbers copy
|
|
593
|
+
const stream = await agent.streamVNext("Tell me a story", {
|
|
594
|
+
onFinish: (result) => {
|
|
595
|
+
console.log('Streaming finished:', result);
|
|
596
|
+
},
|
|
597
|
+
onStepFinish: (step) => {
|
|
598
|
+
console.log('Step completed:', step);
|
|
599
|
+
},
|
|
600
|
+
onChunk: (chunk) => {
|
|
601
|
+
console.log('Received chunk:', chunk);
|
|
602
|
+
},
|
|
603
|
+
onError: ({ error }) => {
|
|
604
|
+
console.error('Streaming error:', error);
|
|
605
|
+
},
|
|
606
|
+
onAbort: (event) => {
|
|
607
|
+
console.log('Stream aborted:', event);
|
|
608
|
+
},
|
|
609
|
+
});
|
|
610
|
+
|
|
611
|
+
// Process the stream
|
|
612
|
+
for await (const chunk of stream.textStream) {
|
|
613
|
+
console.log(chunk);
|
|
614
|
+
}
|
|
615
|
+
```
|
|
616
|
+
|
|
526
617
|
### Advanced Example with Options
|
|
527
618
|
|
|
528
619
|
```typescript showLineNumbers copy
|
|
@@ -93,5 +93,5 @@ The `createTool()` function returns a `Tool` object.
|
|
|
93
93
|
|
|
94
94
|
- [Tools Overview](/docs/tools-mcp/overview.mdx)
|
|
95
95
|
- [Using Tools with Agents](/docs/agents/using-tools-and-mcp.mdx)
|
|
96
|
-
- [
|
|
96
|
+
- [Tool Runtime Context](/docs/tools-mcp/runtime-context.mdx)
|
|
97
97
|
- [Advanced Tool Usage](/docs/tools-mcp/advanced-usage.mdx)
|
|
@@ -253,8 +253,8 @@ const response = await agent.generate(
|
|
|
253
253
|
|
|
254
254
|
For more information on runtime context, please see:
|
|
255
255
|
|
|
256
|
-
- [Runtime
|
|
257
|
-
- [
|
|
256
|
+
- [Agent Runtime Context](../../docs/agents/runtime-context.mdx)
|
|
257
|
+
- [Tool Runtime Context](../../docs/tools-mcp/runtime-context.mdx)
|
|
258
258
|
|
|
259
259
|
## Related
|
|
260
260
|
|
|
@@ -558,8 +558,8 @@ const response = await agent.generate(
|
|
|
558
558
|
|
|
559
559
|
For more information on runtime context, please see:
|
|
560
560
|
|
|
561
|
-
- [Runtime
|
|
562
|
-
- [
|
|
561
|
+
- [Agent Runtime Context](../../docs/agents/runtime-context.mdx)
|
|
562
|
+
- [Tool Runtime Context](../../docs/tools-mcp/runtime-context.mdx)
|
|
563
563
|
|
|
564
564
|
## Usage Without a Mastra Server
|
|
565
565
|
|
|
@@ -0,0 +1,63 @@
|
|
|
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)
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# @mastra/mcp-docs-server
|
|
2
2
|
|
|
3
|
+
## 0.13.19-alpha.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [[`827d876`](https://github.com/mastra-ai/mastra/commit/827d8766f36a900afcaf64a040f7ba76249009b3), [`7eaf1d1`](https://github.com/mastra-ai/mastra/commit/7eaf1d1cec7e828d7a98efc2a748ac395bbdba3b), [`f3427cd`](https://github.com/mastra-ai/mastra/commit/f3427cdaf9eecd63360dfc897a4acbf5f4143a4e), [`05f13b8`](https://github.com/mastra-ai/mastra/commit/05f13b8fb269ccfc4de98e9db58dbe16eae55a5e)]:
|
|
8
|
+
- @mastra/core@0.16.1-alpha.2
|
|
9
|
+
|
|
3
10
|
## 0.13.19-alpha.1
|
|
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.19-alpha.
|
|
3
|
+
"version": "0.13.19-alpha.2",
|
|
4
4
|
"description": "MCP server for accessing Mastra.ai documentation, changelogs, and news.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"uuid": "^11.1.0",
|
|
34
34
|
"zod": "^3.25.76",
|
|
35
35
|
"zod-to-json-schema": "^3.24.6",
|
|
36
|
-
"@mastra/
|
|
37
|
-
"@mastra/
|
|
36
|
+
"@mastra/mcp": "^0.12.0-alpha.1",
|
|
37
|
+
"@mastra/core": "0.16.1-alpha.2"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@hono/node-server": "^1.17.1",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"typescript": "^5.8.3",
|
|
51
51
|
"vitest": "^3.2.4",
|
|
52
52
|
"@internal/lint": "0.0.36",
|
|
53
|
-
"@mastra/core": "0.16.1-alpha.
|
|
53
|
+
"@mastra/core": "0.16.1-alpha.2"
|
|
54
54
|
},
|
|
55
55
|
"homepage": "https://mastra.ai",
|
|
56
56
|
"repository": {
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: "Runtime context | Agents | Mastra Docs"
|
|
3
|
-
description: Learn how to use Mastra's dependency injection system to provide runtime configuration to agents and tools.
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
# Agent Runtime Context
|
|
7
|
-
|
|
8
|
-
Mastra provides runtime context, which is a system based on dependency injection that enables you to configure your agents and tools with runtime variables. If you find yourself creating several different agents that do very similar things, runtime context allows you to combine them into one agent.
|
|
9
|
-
|
|
10
|
-
## Overview
|
|
11
|
-
|
|
12
|
-
The dependency injection system allows you to:
|
|
13
|
-
|
|
14
|
-
1. Pass runtime configuration variables to agents through a type-safe runtimeContext
|
|
15
|
-
2. Access these variables within tool execution contexts
|
|
16
|
-
3. Modify agent behavior without changing the underlying code
|
|
17
|
-
4. Share configuration across multiple tools within the same agent
|
|
18
|
-
|
|
19
|
-
## Basic Usage
|
|
20
|
-
|
|
21
|
-
```typescript
|
|
22
|
-
const agent = mastra.getAgent("weatherAgent");
|
|
23
|
-
|
|
24
|
-
// Define your runtimeContext's type structure
|
|
25
|
-
type WeatherRuntimeContext = {
|
|
26
|
-
"temperature-scale": "celsius" | "fahrenheit";
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const runtimeContext = new RuntimeContext<WeatherRuntimeContext>();
|
|
30
|
-
runtimeContext.set("temperature-scale", "celsius");
|
|
31
|
-
|
|
32
|
-
const response = await agent.generate("What's the weather like today?", {
|
|
33
|
-
runtimeContext,
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
console.log(response.text);
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
## Using with REST API
|
|
40
|
-
|
|
41
|
-
Here's how to dynamically set temperature units based on a user's location using the Cloudflare `CF-IPCountry` header:
|
|
42
|
-
|
|
43
|
-
```typescript filename="src/index.ts"
|
|
44
|
-
import { Mastra } from "@mastra/core";
|
|
45
|
-
import { RuntimeContext } from "@mastra/core/di";
|
|
46
|
-
import { agent as weatherAgent } from "./agents/weather";
|
|
47
|
-
|
|
48
|
-
// Define RuntimeContext type with clear, descriptive types
|
|
49
|
-
type WeatherRuntimeContext = {
|
|
50
|
-
"temperature-scale": "celsius" | "fahrenheit";
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
export const mastra = new Mastra({
|
|
54
|
-
agents: {
|
|
55
|
-
weather: weatherAgent,
|
|
56
|
-
},
|
|
57
|
-
server: {
|
|
58
|
-
middleware: [
|
|
59
|
-
async (c, next) => {
|
|
60
|
-
const country = c.req.header("CF-IPCountry");
|
|
61
|
-
const runtimeContext = c.get<WeatherRuntimeContext>("runtimeContext");
|
|
62
|
-
|
|
63
|
-
// Set temperature scale based on country
|
|
64
|
-
runtimeContext.set(
|
|
65
|
-
"temperature-scale",
|
|
66
|
-
country === "US" ? "fahrenheit" : "celsius",
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
await next(); // Don't forget to call next()
|
|
70
|
-
},
|
|
71
|
-
],
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
## Creating Tools with Variables
|
|
77
|
-
|
|
78
|
-
Tools can access runtimeContext variables and must conform to the agent's runtimeContext type:
|
|
79
|
-
|
|
80
|
-
```typescript
|
|
81
|
-
import { createTool } from "@mastra/core/tools";
|
|
82
|
-
import { z } from "zod";
|
|
83
|
-
|
|
84
|
-
export const weatherTool = createTool({
|
|
85
|
-
id: "getWeather",
|
|
86
|
-
description: "Get the current weather for a location",
|
|
87
|
-
inputSchema: z.object({
|
|
88
|
-
location: z.string().describe("The location to get weather for"),
|
|
89
|
-
}),
|
|
90
|
-
execute: async ({ context, runtimeContext }) => {
|
|
91
|
-
// Type-safe access to runtimeContext variables
|
|
92
|
-
const temperatureUnit = runtimeContext.get("temperature-scale");
|
|
93
|
-
|
|
94
|
-
const weather = await fetchWeather(context.location, {
|
|
95
|
-
temperatureUnit,
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
return { result: weather };
|
|
99
|
-
},
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
async function fetchWeather(
|
|
103
|
-
location: string,
|
|
104
|
-
{ temperatureUnit }: { temperatureUnit: "celsius" | "fahrenheit" },
|
|
105
|
-
): Promise<WeatherResponse> {
|
|
106
|
-
// Implementation of weather API call
|
|
107
|
-
const response = await weatherApi.fetch(location, temperatureUnit);
|
|
108
|
-
|
|
109
|
-
return {
|
|
110
|
-
location,
|
|
111
|
-
temperature: "72°F",
|
|
112
|
-
conditions: "Sunny",
|
|
113
|
-
unit: temperatureUnit,
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
```
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: "Dynamic Tool 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
|
-
# Dynamic Tool Context
|
|
9
|
-
|
|
10
|
-
Mastra provides `RuntimeContext`, a system based on dependency injection, that allows you to pass dynamic, request-specific configuration to your tools during execution. This is useful when a tool's behavior needs to change based on user identity, request headers, or other runtime factors, without altering the tool's core code.
|
|
11
|
-
|
|
12
|
-
<Callout>
|
|
13
|
-
**Note:** `RuntimeContext` is primarily used for passing data *into* tool
|
|
14
|
-
executions. It's distinct from agent memory, which handles conversation
|
|
15
|
-
history and state persistence across multiple calls.
|
|
16
|
-
</Callout>
|
|
17
|
-
|
|
18
|
-
## Basic Usage
|
|
19
|
-
|
|
20
|
-
To use `RuntimeContext`, first define a type structure for your dynamic configuration. Then, create an instance of `RuntimeContext` typed with your definition and set the desired values. Finally, include the `runtimeContext` instance in the options object when calling `agent.generate()` or `agent.stream()`.
|
|
21
|
-
|
|
22
|
-
```typescript
|
|
23
|
-
import { RuntimeContext } from "@mastra/core/di";
|
|
24
|
-
// Assume 'agent' is an already defined Mastra Agent instance
|
|
25
|
-
|
|
26
|
-
// Define the context type
|
|
27
|
-
type WeatherRuntimeContext = {
|
|
28
|
-
"temperature-scale": "celsius" | "fahrenheit";
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
// Instantiate RuntimeContext and set values
|
|
32
|
-
const runtimeContext = new RuntimeContext<WeatherRuntimeContext>();
|
|
33
|
-
runtimeContext.set("temperature-scale", "celsius");
|
|
34
|
-
|
|
35
|
-
// Pass to agent call
|
|
36
|
-
const response = await agent.generate("What's the weather like today?", {
|
|
37
|
-
runtimeContext, // Pass the context here
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
console.log(response.text);
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
## Accessing Context in Tools
|
|
44
|
-
|
|
45
|
-
Tools receive the `runtimeContext` as part of the second argument to their `execute` function. You can then use the `.get()` method to retrieve values.
|
|
46
|
-
|
|
47
|
-
```typescript filename="src/mastra/tools/weather-tool.ts"
|
|
48
|
-
import { createTool } from "@mastra/core/tools";
|
|
49
|
-
import { z } from "zod";
|
|
50
|
-
// Assume WeatherRuntimeContext is defined as above and accessible here
|
|
51
|
-
|
|
52
|
-
// Dummy fetch function
|
|
53
|
-
async function fetchWeather(
|
|
54
|
-
location: string,
|
|
55
|
-
options: { temperatureUnit: "celsius" | "fahrenheit" },
|
|
56
|
-
): Promise<any> {
|
|
57
|
-
console.log(`Fetching weather for ${location} in ${options.temperatureUnit}`);
|
|
58
|
-
// Replace with actual API call
|
|
59
|
-
return { temperature: options.temperatureUnit === "celsius" ? 20 : 68 };
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export const weatherTool = createTool({
|
|
63
|
-
id: "getWeather",
|
|
64
|
-
description: "Get the current weather for a location",
|
|
65
|
-
inputSchema: z.object({
|
|
66
|
-
location: z.string().describe("The location to get weather for"),
|
|
67
|
-
}),
|
|
68
|
-
// The tool's execute function receives runtimeContext
|
|
69
|
-
execute: async ({ context, runtimeContext }) => {
|
|
70
|
-
// Type-safe access to runtimeContext variables
|
|
71
|
-
const temperatureUnit = runtimeContext.get("temperature-scale");
|
|
72
|
-
|
|
73
|
-
// Use the context value in the tool logic
|
|
74
|
-
const weather = await fetchWeather(context.location, {
|
|
75
|
-
temperatureUnit,
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
return {
|
|
79
|
-
result: `The temperature is ${weather.temperature}°${temperatureUnit === "celsius" ? "C" : "F"}`,
|
|
80
|
-
};
|
|
81
|
-
},
|
|
82
|
-
});
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
When the agent uses `weatherTool`, the `temperature-scale` value set in the `runtimeContext` during the `agent.generate()` call will be available inside the tool's `execute` function.
|
|
86
|
-
|
|
87
|
-
## Using with Server Middleware
|
|
88
|
-
|
|
89
|
-
In server environments (like Express or Next.js), you can use middleware to automatically populate `RuntimeContext` based on incoming request data, such as headers or user sessions.
|
|
90
|
-
|
|
91
|
-
Here's an example using Mastra's built-in server middleware support (which uses Hono internally) to set the temperature scale based on the Cloudflare `CF-IPCountry` header:
|
|
92
|
-
|
|
93
|
-
```typescript filename="src/mastra/index.ts"
|
|
94
|
-
import { Mastra } from "@mastra/core";
|
|
95
|
-
import { RuntimeContext } from "@mastra/core/di";
|
|
96
|
-
import { weatherAgent } from "./agents/weather"; // Assume agent is defined elsewhere
|
|
97
|
-
|
|
98
|
-
// Define RuntimeContext type
|
|
99
|
-
type WeatherRuntimeContext = {
|
|
100
|
-
"temperature-scale": "celsius" | "fahrenheit";
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
export const mastra = new Mastra({
|
|
104
|
-
agents: {
|
|
105
|
-
weather: weatherAgent,
|
|
106
|
-
},
|
|
107
|
-
server: {
|
|
108
|
-
middleware: [
|
|
109
|
-
async (c, next) => {
|
|
110
|
-
// Get the RuntimeContext instance
|
|
111
|
-
const runtimeContext =
|
|
112
|
-
c.get<RuntimeContext<WeatherRuntimeContext>>("runtimeContext");
|
|
113
|
-
|
|
114
|
-
// Get country code from request header
|
|
115
|
-
const country = c.req.header("CF-IPCountry");
|
|
116
|
-
|
|
117
|
-
// Set temperature scale based on country
|
|
118
|
-
runtimeContext.set(
|
|
119
|
-
"temperature-scale",
|
|
120
|
-
country === "US" ? "fahrenheit" : "celsius",
|
|
121
|
-
);
|
|
122
|
-
|
|
123
|
-
// Continue request processing
|
|
124
|
-
await next();
|
|
125
|
-
},
|
|
126
|
-
],
|
|
127
|
-
},
|
|
128
|
-
});
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
With this middleware in place, any agent call handled by this Mastra server instance will automatically have the `temperature-scale` set in its `RuntimeContext` based on the user's inferred country, and tools like `weatherTool` will use it accordingly.
|