@mastra/mcp-docs-server 1.0.0-beta.7 → 1.0.0-beta.9
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/%40internal%2Fchangeset-cli.md +1 -15
- package/.docs/organized/changelogs/%40internal%2Fexternal-types.md +1 -7
- package/.docs/organized/changelogs/%40internal%2Ftypes-builder.md +1 -55
- package/.docs/organized/changelogs/%40mastra%2Fagent-builder.md +12 -12
- package/.docs/organized/changelogs/%40mastra%2Fai-sdk.md +48 -48
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +18 -18
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Fcodemod.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Fconvex.md +16 -0
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +78 -78
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +18 -18
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +23 -23
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Flance.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +65 -65
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +29 -29
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +12 -12
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Fmssql.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +69 -69
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +31 -31
- package/.docs/organized/changelogs/%40mastra%2Freact.md +14 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +56 -56
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +17 -17
- package/.docs/organized/changelogs/create-mastra.md +13 -13
- package/.docs/organized/changelogs/mastra.md +21 -21
- package/.docs/organized/code-examples/mcp-server-adapters.md +1 -2
- package/.docs/organized/code-examples/processors-with-ai-sdk.md +14 -0
- package/.docs/organized/code-examples/server-app-access.md +1 -1
- package/.docs/organized/code-examples/server-hono-adapter.md +1 -1
- package/.docs/raw/getting-started/studio.mdx +4 -2
- package/.docs/raw/guides/agent-frameworks/ai-sdk.mdx +161 -0
- package/.docs/raw/guides/build-your-ui/ai-sdk-ui.mdx +381 -431
- package/.docs/raw/guides/getting-started/quickstart.mdx +11 -0
- package/.docs/raw/guides/migrations/upgrade-to-v1/tools.mdx +3 -3
- package/.docs/raw/guides/migrations/upgrade-to-v1/workflows.mdx +31 -0
- package/.docs/raw/reference/ai-sdk/chat-route.mdx +127 -0
- package/.docs/raw/reference/ai-sdk/handle-chat-stream.mdx +117 -0
- package/.docs/raw/reference/ai-sdk/handle-network-stream.mdx +64 -0
- package/.docs/raw/reference/ai-sdk/handle-workflow-stream.mdx +116 -0
- package/.docs/raw/reference/ai-sdk/network-route.mdx +99 -0
- package/.docs/raw/reference/ai-sdk/to-ai-sdk-stream.mdx +289 -0
- package/.docs/raw/reference/ai-sdk/workflow-route.mdx +110 -0
- package/.docs/raw/reference/client-js/agents.mdx +251 -67
- package/.docs/raw/reference/client-js/mastra-client.mdx +2 -2
- package/.docs/raw/reference/client-js/memory.mdx +4 -1
- package/.docs/raw/reference/core/getMemory.mdx +73 -0
- package/.docs/raw/reference/core/getStoredAgentById.mdx +183 -0
- package/.docs/raw/reference/core/listMemory.mdx +70 -0
- package/.docs/raw/reference/core/listStoredAgents.mdx +151 -0
- package/.docs/raw/reference/core/mastra-class.mdx +8 -0
- package/.docs/raw/reference/server/express-adapter.mdx +52 -0
- package/.docs/raw/reference/server/hono-adapter.mdx +54 -0
- package/.docs/raw/server-db/custom-api-routes.mdx +5 -5
- package/.docs/raw/server-db/server-adapters.mdx +94 -91
- package/.docs/raw/streaming/tool-streaming.mdx +10 -14
- package/.docs/raw/workflows/workflow-state.mdx +4 -5
- package/CHANGELOG.md +15 -0
- package/package.json +4 -4
|
@@ -15,19 +15,26 @@ Retrieve a list of all available agents:
|
|
|
15
15
|
const agents = await mastraClient.listAgents();
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
Returns a record of agent IDs to their serialized agent configurations.
|
|
18
|
+
Returns a record of agent IDs to their serialized agent configurations.
|
|
19
19
|
|
|
20
20
|
## Working with a Specific Agent
|
|
21
21
|
|
|
22
|
-
Get an instance of a specific agent:
|
|
22
|
+
Get an instance of a specific agent by its ID:
|
|
23
|
+
|
|
24
|
+
```typescript title="src/mastra/agents/my-agent.ts"
|
|
25
|
+
export const myAgent = new Agent({
|
|
26
|
+
id: "my-agent",
|
|
27
|
+
// ...
|
|
28
|
+
});
|
|
29
|
+
```
|
|
23
30
|
|
|
24
31
|
```typescript
|
|
25
|
-
const agent = mastraClient.getAgent("agent
|
|
32
|
+
const agent = mastraClient.getAgent("my-agent");
|
|
26
33
|
```
|
|
27
34
|
|
|
28
35
|
## Agent Methods
|
|
29
36
|
|
|
30
|
-
###
|
|
37
|
+
### details()
|
|
31
38
|
|
|
32
39
|
Retrieve detailed information about an agent:
|
|
33
40
|
|
|
@@ -35,9 +42,7 @@ Retrieve detailed information about an agent:
|
|
|
35
42
|
const details = await agent.details();
|
|
36
43
|
```
|
|
37
44
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
### Generate Response
|
|
45
|
+
### generate()
|
|
41
46
|
|
|
42
47
|
Generate a response from the agent:
|
|
43
48
|
|
|
@@ -55,7 +60,16 @@ const response = await agent.generate({
|
|
|
55
60
|
});
|
|
56
61
|
```
|
|
57
62
|
|
|
58
|
-
|
|
63
|
+
You can also use the simplified string format:
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
const response = await agent.generate("Hello, how are you?", {
|
|
67
|
+
threadId: "thread-1",
|
|
68
|
+
resourceId: "resource-1",
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### stream()
|
|
59
73
|
|
|
60
74
|
Stream responses from the agent for real-time interactions:
|
|
61
75
|
|
|
@@ -75,8 +89,28 @@ response.processDataStream({
|
|
|
75
89
|
console.log(chunk);
|
|
76
90
|
},
|
|
77
91
|
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
You can also use the simplified string format:
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
const response = await agent.stream("Tell me a story", {
|
|
98
|
+
threadId: "thread-1",
|
|
99
|
+
clientTools: { colorChangeTool },
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
response.processDataStream({
|
|
103
|
+
onChunk: async (chunk) => {
|
|
104
|
+
if (chunk.type === "text-delta") {
|
|
105
|
+
console.log(chunk.payload.text);
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
You can also read from response body directly:
|
|
78
112
|
|
|
79
|
-
|
|
113
|
+
```typescript
|
|
80
114
|
const reader = response.body.getReader();
|
|
81
115
|
while (true) {
|
|
82
116
|
const { done, value } = await reader.read();
|
|
@@ -85,11 +119,119 @@ while (true) {
|
|
|
85
119
|
}
|
|
86
120
|
```
|
|
87
121
|
|
|
88
|
-
|
|
122
|
+
#### AI SDK compatible format
|
|
89
123
|
|
|
90
|
-
|
|
124
|
+
To stream AI SDK-formatted parts on the client from an `agent.stream(...)` response, wrap `response.processDataStream` into a `ReadableStream<ChunkType>` and use `toAISdkStream`:
|
|
125
|
+
|
|
126
|
+
```typescript title="client-ai-sdk-transform.ts" copy
|
|
127
|
+
import { createUIMessageStream } from "ai";
|
|
128
|
+
import { toAISdkStream } from "@mastra/ai-sdk";
|
|
129
|
+
import type { ChunkType, MastraModelOutput } from "@mastra/core/stream";
|
|
130
|
+
|
|
131
|
+
const response = await agent.stream({ messages: "Tell me a story" });
|
|
132
|
+
|
|
133
|
+
const chunkStream: ReadableStream<ChunkType> = new ReadableStream<ChunkType>({
|
|
134
|
+
start(controller) {
|
|
135
|
+
response
|
|
136
|
+
.processDataStream({
|
|
137
|
+
onChunk: async (chunk) => controller.enqueue(chunk as ChunkType),
|
|
138
|
+
})
|
|
139
|
+
.finally(() => controller.close());
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const uiMessageStream = createUIMessageStream({
|
|
144
|
+
execute: async ({ writer }) => {
|
|
145
|
+
for await (const part of toAISdkStream(
|
|
146
|
+
chunkStream as unknown as MastraModelOutput,
|
|
147
|
+
{ from: "agent" },
|
|
148
|
+
)) {
|
|
149
|
+
writer.write(part);
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
for await (const part of uiMessageStream) {
|
|
155
|
+
console.log(part);
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### getTool()
|
|
160
|
+
|
|
161
|
+
Retrieve information about a specific tool available to the agent:
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
const tool = await agent.getTool("tool-id");
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### executeTool()
|
|
168
|
+
|
|
169
|
+
Execute a specific tool for the agent:
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
const result = await agent.executeTool("tool-id", {
|
|
173
|
+
data: { input: "value" },
|
|
174
|
+
});
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### network()
|
|
178
|
+
|
|
179
|
+
Stream responses from an agent network for multi-agent interactions:
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
const response = await agent.network({
|
|
183
|
+
messages: [
|
|
184
|
+
{
|
|
185
|
+
role: "user",
|
|
186
|
+
content: "Research this topic and write a summary",
|
|
187
|
+
},
|
|
188
|
+
],
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
response.processDataStream({
|
|
192
|
+
onChunk: async (chunk) => {
|
|
193
|
+
console.log(chunk);
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### approveToolCall()
|
|
199
|
+
|
|
200
|
+
Approve a pending tool call that requires human confirmation:
|
|
201
|
+
|
|
202
|
+
```typescript
|
|
203
|
+
const response = await agent.approveToolCall({
|
|
204
|
+
runId: "run-123",
|
|
205
|
+
toolCallId: "tool-call-456",
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
response.processDataStream({
|
|
209
|
+
onChunk: async (chunk) => {
|
|
210
|
+
console.log(chunk);
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### declineToolCall()
|
|
216
|
+
|
|
217
|
+
Decline a pending tool call that requires human confirmation:
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
const response = await agent.declineToolCall({
|
|
221
|
+
runId: "run-123",
|
|
222
|
+
toolCallId: "tool-call-456",
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
response.processDataStream({
|
|
226
|
+
onChunk: async (chunk) => {
|
|
227
|
+
console.log(chunk);
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## Client Tools
|
|
91
233
|
|
|
92
|
-
|
|
234
|
+
Client-side tools allow you to execute custom functions on the client side when the agent requests them.
|
|
93
235
|
|
|
94
236
|
```typescript
|
|
95
237
|
import { createTool } from "@mastra/client-js";
|
|
@@ -132,90 +274,132 @@ response.processDataStream({
|
|
|
132
274
|
});
|
|
133
275
|
```
|
|
134
276
|
|
|
135
|
-
|
|
277
|
+
## Stored Agents
|
|
136
278
|
|
|
137
|
-
|
|
279
|
+
Stored agents are agent configurations stored in a database that can be created, updated, and deleted at runtime. They reference primitives (tools, workflows, other agents, memory, scorers) by key, which are resolved from the Mastra registry when the agent is instantiated.
|
|
280
|
+
|
|
281
|
+
### listStoredAgents()
|
|
282
|
+
|
|
283
|
+
Retrieve a paginated list of all stored agents:
|
|
138
284
|
|
|
139
285
|
```typescript
|
|
140
|
-
const
|
|
286
|
+
const result = await mastraClient.listStoredAgents();
|
|
287
|
+
console.log(result.agents); // Array of stored agents
|
|
288
|
+
console.log(result.total); // Total count
|
|
141
289
|
```
|
|
142
290
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
Get evaluation results for the agent:
|
|
291
|
+
With pagination and ordering:
|
|
146
292
|
|
|
147
293
|
```typescript
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
294
|
+
const result = await mastraClient.listStoredAgents({
|
|
295
|
+
page: 0,
|
|
296
|
+
perPage: 20,
|
|
297
|
+
orderBy: {
|
|
298
|
+
field: "createdAt",
|
|
299
|
+
direction: "DESC",
|
|
300
|
+
},
|
|
301
|
+
});
|
|
153
302
|
```
|
|
154
303
|
|
|
155
|
-
###
|
|
304
|
+
### createStoredAgent()
|
|
156
305
|
|
|
157
|
-
|
|
306
|
+
Create a new stored agent:
|
|
158
307
|
|
|
159
308
|
```typescript
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
|
|
309
|
+
const agent = await mastraClient.createStoredAgent({
|
|
310
|
+
id: "my-agent",
|
|
311
|
+
name: "My Assistant",
|
|
312
|
+
instructions: "You are a helpful assistant.",
|
|
313
|
+
model: {
|
|
314
|
+
provider: "openai",
|
|
315
|
+
name: "gpt-4",
|
|
316
|
+
},
|
|
163
317
|
});
|
|
318
|
+
```
|
|
164
319
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
320
|
+
With all options:
|
|
321
|
+
|
|
322
|
+
```typescript
|
|
323
|
+
const agent = await mastraClient.createStoredAgent({
|
|
324
|
+
id: "full-agent",
|
|
325
|
+
name: "Full Agent",
|
|
326
|
+
description: "A fully configured agent",
|
|
327
|
+
instructions: "You are a helpful assistant.",
|
|
328
|
+
model: {
|
|
329
|
+
provider: "openai",
|
|
330
|
+
name: "gpt-4",
|
|
331
|
+
},
|
|
332
|
+
tools: ["calculator", "weather"],
|
|
333
|
+
workflows: ["data-processing"],
|
|
334
|
+
agents: ["sub-agent-1"],
|
|
335
|
+
memory: "my-memory",
|
|
336
|
+
scorers: {
|
|
337
|
+
"quality-scorer": {
|
|
338
|
+
sampling: { type: "ratio", rate: 0.1 },
|
|
339
|
+
},
|
|
340
|
+
},
|
|
341
|
+
defaultOptions: {
|
|
342
|
+
maxSteps: 10,
|
|
343
|
+
},
|
|
344
|
+
metadata: {
|
|
345
|
+
version: "1.0",
|
|
346
|
+
team: "engineering",
|
|
171
347
|
},
|
|
172
348
|
});
|
|
173
349
|
```
|
|
174
350
|
|
|
175
|
-
|
|
351
|
+
### getStoredAgent()
|
|
176
352
|
|
|
177
|
-
|
|
353
|
+
Get an instance of a specific stored agent:
|
|
178
354
|
|
|
179
|
-
```typescript
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
import type { ChunkType, MastraModelOutput } from "@mastra/core/stream";
|
|
355
|
+
```typescript
|
|
356
|
+
const storedAgent = mastraClient.getStoredAgent("my-agent");
|
|
357
|
+
```
|
|
183
358
|
|
|
184
|
-
|
|
359
|
+
## Stored Agent Methods
|
|
185
360
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
361
|
+
### details()
|
|
362
|
+
|
|
363
|
+
Retrieve the stored agent configuration:
|
|
364
|
+
|
|
365
|
+
```typescript
|
|
366
|
+
const details = await storedAgent.details();
|
|
367
|
+
console.log(details.name);
|
|
368
|
+
console.log(details.instructions);
|
|
369
|
+
console.log(details.model);
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
### update()
|
|
373
|
+
|
|
374
|
+
Update specific fields of a stored agent. All fields are optional:
|
|
375
|
+
|
|
376
|
+
```typescript
|
|
377
|
+
const updated = await storedAgent.update({
|
|
378
|
+
name: "Updated Agent Name",
|
|
379
|
+
instructions: "New instructions for the agent.",
|
|
194
380
|
});
|
|
381
|
+
```
|
|
195
382
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
{ from: "agent" },
|
|
201
|
-
)) {
|
|
202
|
-
writer.write(part);
|
|
203
|
-
}
|
|
204
|
-
},
|
|
383
|
+
```typescript
|
|
384
|
+
// Update just the tools
|
|
385
|
+
await storedAgent.update({
|
|
386
|
+
tools: ["new-tool-1", "new-tool-2"],
|
|
205
387
|
});
|
|
206
388
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
389
|
+
// Update metadata
|
|
390
|
+
await storedAgent.update({
|
|
391
|
+
metadata: {
|
|
392
|
+
version: "2.0",
|
|
393
|
+
lastModifiedBy: "admin",
|
|
394
|
+
},
|
|
395
|
+
});
|
|
210
396
|
```
|
|
211
397
|
|
|
212
|
-
###
|
|
398
|
+
### delete()
|
|
213
399
|
|
|
214
|
-
|
|
400
|
+
Delete a stored agent:
|
|
215
401
|
|
|
216
402
|
```typescript
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
resourceId: "resource-1",
|
|
220
|
-
});
|
|
403
|
+
const result = await storedAgent.delete();
|
|
404
|
+
console.log(result.success); // true
|
|
221
405
|
```
|
|
@@ -101,8 +101,8 @@ export const mastraClient = new MastraClient({
|
|
|
101
101
|
},
|
|
102
102
|
{
|
|
103
103
|
name: "saveMessageToMemory(params)",
|
|
104
|
-
type: "Promise<
|
|
105
|
-
description: "Saves one or more messages to the memory system.",
|
|
104
|
+
type: "Promise<{ messages: (MastraMessageV1 | MastraDBMessage)[] }>",
|
|
105
|
+
description: "Saves one or more messages to the memory system. Returns the saved messages.",
|
|
106
106
|
},
|
|
107
107
|
{
|
|
108
108
|
name: "getMemoryStatus()",
|
|
@@ -76,7 +76,7 @@ await thread.delete();
|
|
|
76
76
|
Save messages to memory:
|
|
77
77
|
|
|
78
78
|
```typescript
|
|
79
|
-
const
|
|
79
|
+
const result = await mastraClient.saveMessageToMemory({
|
|
80
80
|
messages: [
|
|
81
81
|
{
|
|
82
82
|
role: "user",
|
|
@@ -90,6 +90,9 @@ const savedMessages = await mastraClient.saveMessageToMemory({
|
|
|
90
90
|
],
|
|
91
91
|
agentId: "agent-1",
|
|
92
92
|
});
|
|
93
|
+
|
|
94
|
+
// result.messages contains the saved messages
|
|
95
|
+
console.log(result.messages);
|
|
93
96
|
```
|
|
94
97
|
|
|
95
98
|
### Retrieve Thread Messages
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Reference: Mastra.getMemory() | Core"
|
|
3
|
+
description: "Documentation for the `Mastra.getMemory()` method in Mastra, which retrieves a registered memory instance by its registry key."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Mastra.getMemory()
|
|
7
|
+
|
|
8
|
+
The `.getMemory()` method retrieves a memory instance from the Mastra registry by its key. Memory instances are registered in the Mastra constructor and can be referenced by stored agents.
|
|
9
|
+
|
|
10
|
+
## Usage example
|
|
11
|
+
|
|
12
|
+
```typescript copy
|
|
13
|
+
const memory = mastra.getMemory("conversationMemory");
|
|
14
|
+
|
|
15
|
+
// Use the memory instance
|
|
16
|
+
const thread = await memory.createThread({
|
|
17
|
+
resourceId: "user-123",
|
|
18
|
+
title: "New Conversation",
|
|
19
|
+
});
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Parameters
|
|
23
|
+
|
|
24
|
+
<PropertiesTable
|
|
25
|
+
content={[
|
|
26
|
+
{
|
|
27
|
+
name: "key",
|
|
28
|
+
type: "TMemoryKey extends keyof TMemory",
|
|
29
|
+
description:
|
|
30
|
+
"The registry key of the memory instance to retrieve. Must match a key used when registering memory in the Mastra constructor.",
|
|
31
|
+
},
|
|
32
|
+
]}
|
|
33
|
+
/>
|
|
34
|
+
|
|
35
|
+
## Returns
|
|
36
|
+
|
|
37
|
+
<PropertiesTable
|
|
38
|
+
content={[
|
|
39
|
+
{
|
|
40
|
+
name: "memory",
|
|
41
|
+
type: "TMemory[TMemoryKey]",
|
|
42
|
+
description:
|
|
43
|
+
"The memory instance with the specified key. Throws an error if the memory is not found.",
|
|
44
|
+
},
|
|
45
|
+
]}
|
|
46
|
+
/>
|
|
47
|
+
|
|
48
|
+
## Example: Registering and Retrieving Memory
|
|
49
|
+
|
|
50
|
+
```typescript copy
|
|
51
|
+
import { Mastra } from "@mastra/core";
|
|
52
|
+
import { Memory } from "@mastra/memory";
|
|
53
|
+
import { LibSQLStore } from "@mastra/libsql";
|
|
54
|
+
|
|
55
|
+
const conversationMemory = new Memory({
|
|
56
|
+
storage: new LibSQLStore({ url: ":memory:" }),
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const mastra = new Mastra({
|
|
60
|
+
memory: {
|
|
61
|
+
conversationMemory,
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Later, retrieve the memory instance
|
|
66
|
+
const memory = mastra.getMemory("conversationMemory");
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Related
|
|
70
|
+
|
|
71
|
+
- [Mastra.listMemory()](/reference/v1/core/listMemory)
|
|
72
|
+
- [Memory overview](/docs/v1/memory/overview)
|
|
73
|
+
- [Agent Memory](/docs/v1/agents/agent-memory)
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Reference: Mastra.getStoredAgentById() | Core"
|
|
3
|
+
description: "Documentation for the `Mastra.getStoredAgentById()` method in Mastra, which retrieves an agent from storage and creates an executable Agent instance."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Mastra.getStoredAgentById()
|
|
7
|
+
|
|
8
|
+
The `.getStoredAgentById()` method retrieves an agent configuration from storage by its ID and creates an executable `Agent` instance. Stored agents allow you to persist agent configurations in a database and dynamically load them at runtime.
|
|
9
|
+
|
|
10
|
+
## Usage example
|
|
11
|
+
|
|
12
|
+
```typescript copy
|
|
13
|
+
// Get an Agent instance from storage
|
|
14
|
+
const agent = await mastra.getStoredAgentById("my-stored-agent");
|
|
15
|
+
|
|
16
|
+
if (agent) {
|
|
17
|
+
const response = await agent.generate({ messages: "Hello!" });
|
|
18
|
+
console.log(response.text);
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```typescript copy
|
|
23
|
+
// Get the raw storage data instead of an Agent instance
|
|
24
|
+
const storedConfig = await mastra.getStoredAgentById("my-stored-agent", { raw: true });
|
|
25
|
+
|
|
26
|
+
if (storedConfig) {
|
|
27
|
+
console.log(storedConfig.instructions);
|
|
28
|
+
console.log(storedConfig.createdAt);
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Parameters
|
|
33
|
+
|
|
34
|
+
<PropertiesTable
|
|
35
|
+
content={[
|
|
36
|
+
{
|
|
37
|
+
name: "id",
|
|
38
|
+
type: "string",
|
|
39
|
+
description:
|
|
40
|
+
"The unique identifier of the stored agent to retrieve.",
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: "options",
|
|
44
|
+
type: "{ raw?: boolean }",
|
|
45
|
+
description:
|
|
46
|
+
"Optional configuration object.",
|
|
47
|
+
isOptional: true,
|
|
48
|
+
},
|
|
49
|
+
]}
|
|
50
|
+
/>
|
|
51
|
+
|
|
52
|
+
### Options
|
|
53
|
+
|
|
54
|
+
<PropertiesTable
|
|
55
|
+
content={[
|
|
56
|
+
{
|
|
57
|
+
name: "raw",
|
|
58
|
+
type: "boolean",
|
|
59
|
+
description:
|
|
60
|
+
"When `true`, returns the raw `StorageAgentType` object from storage instead of creating an `Agent` instance. Useful for inspecting stored configuration or metadata.",
|
|
61
|
+
isOptional: true,
|
|
62
|
+
defaultValue: "false",
|
|
63
|
+
},
|
|
64
|
+
]}
|
|
65
|
+
/>
|
|
66
|
+
|
|
67
|
+
## Returns
|
|
68
|
+
|
|
69
|
+
<PropertiesTable
|
|
70
|
+
content={[
|
|
71
|
+
{
|
|
72
|
+
name: "result",
|
|
73
|
+
type: "Agent | StorageAgentType | null",
|
|
74
|
+
description:
|
|
75
|
+
"Returns an `Agent` instance by default, or `StorageAgentType` when `raw: true`. Returns `null` if no agent with the given ID exists.",
|
|
76
|
+
},
|
|
77
|
+
]}
|
|
78
|
+
/>
|
|
79
|
+
|
|
80
|
+
## Primitive Resolution
|
|
81
|
+
|
|
82
|
+
When creating an `Agent` instance from stored configuration, the method resolves references to registered primitives:
|
|
83
|
+
|
|
84
|
+
- **Tools**: Resolved from `tools` registered in Mastra config
|
|
85
|
+
- **Workflows**: Resolved from `workflows` registered in Mastra config
|
|
86
|
+
- **Sub-agents**: Resolved from `agents` registered in Mastra config
|
|
87
|
+
- **Memory**: Resolved from `memory` registered in Mastra config
|
|
88
|
+
- **Scorers**: Resolved from `scorers` registered in Mastra config, including sampling configuration
|
|
89
|
+
|
|
90
|
+
If a referenced primitive is not found in the registry, a warning is logged but the agent is still created.
|
|
91
|
+
|
|
92
|
+
## StorageAgentType
|
|
93
|
+
|
|
94
|
+
When using `raw: true`, the returned object has the following structure:
|
|
95
|
+
|
|
96
|
+
<PropertiesTable
|
|
97
|
+
content={[
|
|
98
|
+
{
|
|
99
|
+
name: "id",
|
|
100
|
+
type: "string",
|
|
101
|
+
description: "Unique identifier for the agent.",
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
name: "name",
|
|
105
|
+
type: "string",
|
|
106
|
+
description: "Display name of the agent.",
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
name: "description",
|
|
110
|
+
type: "string",
|
|
111
|
+
description: "Optional description of the agent.",
|
|
112
|
+
isOptional: true,
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
name: "instructions",
|
|
116
|
+
type: "string",
|
|
117
|
+
description: "System instructions for the agent.",
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
name: "model",
|
|
121
|
+
type: "Record<string, unknown>",
|
|
122
|
+
description: "Model configuration with provider and name.",
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
name: "tools",
|
|
126
|
+
type: "Record<string, unknown>",
|
|
127
|
+
description: "Tool references to resolve from registry.",
|
|
128
|
+
isOptional: true,
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
name: "workflows",
|
|
132
|
+
type: "Record<string, unknown>",
|
|
133
|
+
description: "Workflow references to resolve from registry.",
|
|
134
|
+
isOptional: true,
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
name: "agents",
|
|
138
|
+
type: "Record<string, unknown>",
|
|
139
|
+
description: "Sub-agent references to resolve from registry.",
|
|
140
|
+
isOptional: true,
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
name: "memory",
|
|
144
|
+
type: "Record<string, unknown>",
|
|
145
|
+
description: "Memory reference to resolve from registry.",
|
|
146
|
+
isOptional: true,
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
name: "scorers",
|
|
150
|
+
type: "Record<string, unknown>",
|
|
151
|
+
description: "Scorer references with optional sampling config.",
|
|
152
|
+
isOptional: true,
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
name: "defaultOptions",
|
|
156
|
+
type: "Record<string, unknown>",
|
|
157
|
+
description: "Default options passed to agent execution.",
|
|
158
|
+
isOptional: true,
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
name: "metadata",
|
|
162
|
+
type: "Record<string, unknown>",
|
|
163
|
+
description: "Custom metadata stored with the agent.",
|
|
164
|
+
isOptional: true,
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
name: "createdAt",
|
|
168
|
+
type: "Date",
|
|
169
|
+
description: "Timestamp when the agent was created.",
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
name: "updatedAt",
|
|
173
|
+
type: "Date",
|
|
174
|
+
description: "Timestamp when the agent was last updated.",
|
|
175
|
+
},
|
|
176
|
+
]}
|
|
177
|
+
/>
|
|
178
|
+
|
|
179
|
+
## Related
|
|
180
|
+
|
|
181
|
+
- [Mastra.listStoredAgents()](/reference/v1/core/listStoredAgents)
|
|
182
|
+
- [Storage overview](/docs/v1/server-db/storage)
|
|
183
|
+
- [Agents overview](/docs/v1/agents/overview)
|