@mastra/mcp-docs-server 0.13.24-alpha.0 → 0.13.24-alpha.3
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 +18 -0
- package/.docs/organized/changelogs/%40mastra%2Fai-sdk.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fastra.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fchroma.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +28 -28
- package/.docs/organized/changelogs/%40mastra%2Fcloud.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +45 -45
- package/.docs/organized/changelogs/%40mastra%2Fcouchbase.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +32 -32
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +21 -21
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +31 -31
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-vercel.md +31 -31
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +35 -35
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fevals.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Flance.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +23 -23
- package/.docs/organized/changelogs/%40mastra%2Fmcp-registry-registry.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +25 -25
- package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fmssql.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fopensearch.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +24 -24
- package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +44 -44
- package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Frag.md +14 -14
- package/.docs/organized/changelogs/%40mastra%2Fs3vectors.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fschema-compat.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +37 -37
- package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fvoice-azure.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fvoice-cloudflare.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fvoice-deepgram.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fvoice-elevenlabs.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fvoice-gladia.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google-gemini-live.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fvoice-murf.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai-realtime.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fvoice-playai.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fvoice-sarvam.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fvoice-speechify.md +10 -10
- package/.docs/organized/changelogs/create-mastra.md +18 -18
- package/.docs/organized/changelogs/mastra.md +42 -42
- package/.docs/raw/agents/dynamic-agents.mdx +19 -0
- package/.docs/raw/agents/overview.mdx +46 -0
- package/.docs/raw/auth/auth0.mdx +235 -0
- package/.docs/raw/auth/index.mdx +2 -7
- package/.docs/raw/auth/workos.mdx +198 -0
- package/.docs/raw/getting-started/installation.mdx +1 -1
- package/.docs/raw/memory/conversation-history.mdx +23 -0
- package/.docs/raw/memory/overview.mdx +35 -150
- package/.docs/raw/memory/semantic-recall.mdx +36 -0
- package/.docs/raw/memory/threads-and-resources.mdx +94 -0
- package/.docs/raw/reference/agents/agent.mdx +82 -5
- package/.docs/raw/reference/agents/getInstructions.mdx +2 -2
- package/.docs/raw/reference/auth/auth0.mdx +117 -0
- package/.docs/raw/reference/auth/workos.mdx +128 -0
- package/.docs/raw/reference/memory/Memory.mdx +41 -0
- package/.docs/raw/reference/rag/pg.mdx +28 -1
- package/.docs/raw/reference/workflows/step.mdx +1 -1
- package/CHANGELOG.md +22 -0
- package/package.json +5 -5
|
@@ -3,54 +3,25 @@ title: "Memory Overview | Memory | Mastra Docs"
|
|
|
3
3
|
description: "Learn how Mastra's memory system works with working memory, conversation history, and semantic recall."
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
-
import { Steps
|
|
6
|
+
import { Steps } from "nextra/components";
|
|
7
7
|
|
|
8
8
|
# Memory overview
|
|
9
9
|
|
|
10
|
-
Memory in Mastra helps agents manage context across conversations by condensing relevant information into the language model
|
|
10
|
+
Memory in Mastra helps agents manage context across conversations by condensing relevant information into the language model's context window.
|
|
11
11
|
|
|
12
|
-
Mastra supports three
|
|
12
|
+
Mastra supports three types of memory: working memory, conversation history, and semantic recall. It uses a two-tier scoping system where memory can be isolated per conversation thread (thread-scoped) or shared across all conversations for the same user (resource-scoped).
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
Supported options include:
|
|
17
|
-
|
|
18
|
-
- [Memory with LibSQL](/examples/memory/memory-with-libsql)
|
|
19
|
-
- [Memory with Postgres](/examples/memory/memory-with-pg)
|
|
20
|
-
- [Memory with Upstash](/examples/memory/memory-with-upstash)
|
|
21
|
-
|
|
22
|
-
## Types of memory
|
|
23
|
-
|
|
24
|
-
All memory types are [thread-scoped](./working-memory.mdx#thread-scoped-memory-default) by default, meaning they apply only to a single conversation. [Resource-scoped](./working-memory.mdx#resource-scoped-memory) configuration allows working memory and semantic recall to persist across all threads that use the same user or entity.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
### Working memory
|
|
28
|
-
|
|
29
|
-
Stores persistent user-specific details such as names, preferences, goals, and other structured data. Uses [Markdown templates](./working-memory.mdx) or [Zod schemas](./working-memory.mdx#structured-working-memory) to define structure.
|
|
30
|
-
|
|
31
|
-
### Conversation history
|
|
32
|
-
|
|
33
|
-
Captures recent messages from the current conversation, providing short-term continuity and maintaining dialogue flow.
|
|
34
|
-
|
|
35
|
-
### Semantic recall
|
|
36
|
-
|
|
37
|
-
Retrieves older messages from past conversations based on semantic relevance. Matches are retrieved using vector search and can include surrounding context for better comprehension.
|
|
38
|
-
|
|
39
|
-
## How memory works together
|
|
40
|
-
|
|
41
|
-
Mastra combines all memory types into a single context window. If the total exceeds the model’s token limit, use [memory processors](./memory-processors.mdx) to trim or filter messages before sending them to the model.
|
|
14
|
+
Mastra's memory system uses [storage providers](#memory-storage-adapters) to persist conversation threads, messages, and working memory across application restarts.
|
|
42
15
|
|
|
43
16
|
## Getting started
|
|
44
17
|
|
|
45
|
-
|
|
18
|
+
First install the required dependencies:
|
|
46
19
|
|
|
47
20
|
```bash copy
|
|
48
21
|
npm install @mastra/core @mastra/memory @mastra/libsql
|
|
49
22
|
```
|
|
50
23
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
To share memory across agents, add a storage adapter to the main Mastra instance. Any agent with memory enabled will use this shared storage to store and recall interactions.
|
|
24
|
+
Then add a storage adapter to the main Mastra instance. Any agent with memory enabled will use this shared storage to store and recall interactions.
|
|
54
25
|
|
|
55
26
|
```typescript {6-8} filename="src/mastra/index.ts" showLineNumbers copy
|
|
56
27
|
import { Mastra } from "@mastra/core/mastra";
|
|
@@ -64,150 +35,65 @@ export const mastra = new Mastra({
|
|
|
64
35
|
});
|
|
65
36
|
```
|
|
66
37
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
Enable working memory by passing a `Memory` instance to the agent's `memory` parameter and setting `workingMemory.enabled` to `true`:
|
|
70
|
-
|
|
71
|
-
```typescript {1,6-12} filename="src/mastra/agents/test-agent.ts" showLineNumbers copy
|
|
72
|
-
import { Memory } from "@mastra/memory";
|
|
73
|
-
import { Agent } from "@mastra/core/agent";
|
|
74
|
-
|
|
75
|
-
export const testAgent = new Agent({
|
|
76
|
-
// ..
|
|
77
|
-
memory: new Memory({
|
|
78
|
-
options: {
|
|
79
|
-
workingMemory: {
|
|
80
|
-
enabled: true
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
})
|
|
84
|
-
})
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
## Dedicated storage
|
|
88
|
-
|
|
89
|
-
Agents can be configured with their own dedicated storage, keeping tasks, conversations, and recalled information separate across agents.
|
|
90
|
-
|
|
91
|
-
### Adding storage to agents
|
|
38
|
+
Now, enable memory by passing a `Memory` instance to the agent's `memory` parameter:
|
|
92
39
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
```typescript {3, 9-11} filename="src/mastra/agents/test-agent.ts" showLineNumbers copy
|
|
40
|
+
```typescript {3-5} filename="src/mastra/agents/test-agent.ts" showLineNumbers copy
|
|
96
41
|
import { Memory } from "@mastra/memory";
|
|
97
42
|
import { Agent } from "@mastra/core/agent";
|
|
98
|
-
import { LibSQLStore } from "@mastra/libsql";
|
|
99
43
|
|
|
100
44
|
export const testAgent = new Agent({
|
|
101
45
|
// ...
|
|
102
|
-
memory: new Memory(
|
|
103
|
-
// ...
|
|
104
|
-
storage: new LibSQLStore({
|
|
105
|
-
url: "file:agent-memory.db"
|
|
106
|
-
})
|
|
107
|
-
// ...
|
|
108
|
-
})
|
|
46
|
+
memory: new Memory()
|
|
109
47
|
});
|
|
110
48
|
```
|
|
111
49
|
|
|
112
|
-
|
|
50
|
+
That memory instance has options you can configure for working memory, conversation history, and semantic recall.
|
|
113
51
|
|
|
114
|
-
|
|
52
|
+
## Different types of memory
|
|
115
53
|
|
|
116
|
-
|
|
117
|
-
2. **`resource`**: The user or entity that owns the thread (e.g., `user_123`, `org_456`).
|
|
54
|
+
Mastra supports three types of memory: working memory, conversation history, and semantic recall.
|
|
118
55
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
```typescript {4} showLineNumbers
|
|
122
|
-
const stream = await agent.stream("message for agent", {
|
|
123
|
-
memory: {
|
|
124
|
-
thread: "user-123",
|
|
125
|
-
resource: "test-123"
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
```
|
|
56
|
+
[**Working memory**](./working-memory.mdx) stores persistent user-specific details such as names, preferences, goals, and other structured data. (Compare this to ChatGPT where you can ask it to tell you about yourself). This is implemented as a block of Markdown text that the agent is able to update over time (or alternately, as a Zod schema)
|
|
129
57
|
|
|
130
|
-
|
|
131
|
-
Even with memory configured, agents won’t store or recall information unless both `thread` and `resource` are provided.
|
|
132
|
-
</Callout>
|
|
58
|
+
[**Conversation history**](./conversation-history.mdx) captures recent messages from the current conversation, providing short-term continuity and maintaining dialogue flow.
|
|
133
59
|
|
|
134
|
-
|
|
60
|
+
[**Semantic recall**](./semantic-recall.mdx) retrieves older messages from past conversations based on semantic relevance. Matches are retrieved using vector search and can include surrounding context for better comprehension.
|
|
135
61
|
|
|
136
|
-
|
|
62
|
+
Mastra combines all memory types into a single context window. If the total exceeds the model’s token limit, use [memory processors](./memory-processors.mdx) to trim or filter messages before sending them to the model.
|
|
137
63
|
|
|
138
|
-
|
|
64
|
+
## Scoping memory with threads and resources
|
|
139
65
|
|
|
140
|
-
|
|
141
|
-
export const testAgent = new Agent({
|
|
142
|
-
memory: new Memory({
|
|
143
|
-
options: {
|
|
144
|
-
threads: {
|
|
145
|
-
generateTitle: true,
|
|
146
|
-
}
|
|
147
|
-
},
|
|
148
|
-
})
|
|
149
|
-
});
|
|
150
|
-
```
|
|
66
|
+
All memory types are [thread-scoped](./working-memory.mdx#thread-scoped-memory-default) by default, meaning they apply only to a single conversation. [Resource-scoped](./working-memory.mdx#resource-scoped-memory) configuration allows working memory and semantic recall to persist across all threads that use the same user or entity.
|
|
151
67
|
|
|
152
|
-
|
|
68
|
+
## Memory Storage Adapters
|
|
153
69
|
|
|
154
|
-
|
|
70
|
+
To persist and recall information between conversations, memory requires a storage adapter.
|
|
155
71
|
|
|
156
|
-
|
|
72
|
+
Supported options include [LibSQL](/examples/memory/memory-with-libsql), [Postgres](/examples/memory/memory-with-pg), and [Upstash](/examples/memory/memory-with-upstash)
|
|
157
73
|
|
|
158
|
-
|
|
159
|
-
export const testAgent = new Agent({
|
|
160
|
-
// ...
|
|
161
|
-
memory: new Memory({
|
|
162
|
-
options: {
|
|
163
|
-
threads: {
|
|
164
|
-
generateTitle: {
|
|
165
|
-
model: openai("gpt-4.1-nano"),
|
|
166
|
-
instructions: "Generate a concise title based on the user's first message",
|
|
167
|
-
},
|
|
168
|
-
},
|
|
169
|
-
}
|
|
170
|
-
})
|
|
171
|
-
});
|
|
172
|
-
```
|
|
74
|
+
We use LibSQL out of the box because it is file-based or in-memory, so it is easy to install and works well with the playground.
|
|
173
75
|
|
|
174
|
-
|
|
76
|
+
## Dedicated storage
|
|
175
77
|
|
|
176
|
-
|
|
78
|
+
Agents can be configured with their own dedicated storage, keeping tasks, conversations, and recalled information separate across agents.
|
|
177
79
|
|
|
178
|
-
|
|
179
|
-
export const testAgent = new Agent({
|
|
180
|
-
// ...
|
|
181
|
-
memory: new Memory({
|
|
182
|
-
options: {
|
|
183
|
-
threads: {
|
|
184
|
-
generateTitle: {
|
|
185
|
-
model: ({ runtimeContext }) => {
|
|
186
|
-
const userTier = runtimeContext.get("userTier");
|
|
187
|
-
return userTier === "premium" ? openai("gpt-4.1") : openai("gpt-4.1-nano");
|
|
188
|
-
},
|
|
189
|
-
instructions: ({ runtimeContext }) => {
|
|
190
|
-
const language = runtimeContext.get("userLanguage") || "English";
|
|
191
|
-
return `Generate a concise, engaging title in ${language} based on the user's first message.`;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
})
|
|
197
|
-
});
|
|
198
|
-
```
|
|
80
|
+
### Adding storage to agents
|
|
199
81
|
|
|
200
|
-
|
|
82
|
+
To assign dedicated storage to an agent, install and import the required dependency and pass a `storage` instance to the `Memory` constructor:
|
|
201
83
|
|
|
202
|
-
|
|
84
|
+
```typescript {3, 9-11} filename="src/mastra/agents/test-agent.ts" showLineNumbers copy
|
|
85
|
+
import { Memory } from "@mastra/memory";
|
|
86
|
+
import { Agent } from "@mastra/core/agent";
|
|
87
|
+
import { LibSQLStore } from "@mastra/libsql";
|
|
203
88
|
|
|
204
|
-
```typescript {3-7} showLineNumbers
|
|
205
89
|
export const testAgent = new Agent({
|
|
206
90
|
// ...
|
|
207
91
|
memory: new Memory({
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
92
|
+
// ...
|
|
93
|
+
storage: new LibSQLStore({
|
|
94
|
+
url: "file:agent-memory.db"
|
|
95
|
+
})
|
|
96
|
+
// ...
|
|
211
97
|
})
|
|
212
98
|
});
|
|
213
99
|
```
|
|
@@ -226,7 +112,6 @@ For local development with `LibSQLStore`, you can inspect stored memory using th
|
|
|
226
112
|
|
|
227
113
|

|
|
228
114
|
|
|
229
|
-
|
|
230
115
|
## Next Steps
|
|
231
116
|
|
|
232
117
|
Now that you understand the core concepts, continue to [semantic recall](./semantic-recall.mdx) to learn how to add RAG memory to your Mastra agents.
|
|
@@ -136,6 +136,42 @@ const agent = new Agent({
|
|
|
136
136
|
});
|
|
137
137
|
```
|
|
138
138
|
|
|
139
|
+
### PostgreSQL Index Optimization
|
|
140
|
+
|
|
141
|
+
When using PostgreSQL as your vector store, you can optimize semantic recall performance by configuring the vector index. This is particularly important for large-scale deployments with thousands of messages.
|
|
142
|
+
|
|
143
|
+
PostgreSQL supports both IVFFlat and HNSW indexes. By default, Mastra creates an IVFFlat index, but HNSW indexes typically provide better performance, especially with OpenAI embeddings which use inner product distance.
|
|
144
|
+
|
|
145
|
+
```typescript {9-18}
|
|
146
|
+
import { Memory } from "@mastra/memory";
|
|
147
|
+
import { PgStore, PgVector } from "@mastra/pg";
|
|
148
|
+
|
|
149
|
+
const agent = new Agent({
|
|
150
|
+
memory: new Memory({
|
|
151
|
+
storage: new PgStore({
|
|
152
|
+
connectionString: process.env.DATABASE_URL,
|
|
153
|
+
}),
|
|
154
|
+
vector: new PgVector({
|
|
155
|
+
connectionString: process.env.DATABASE_URL,
|
|
156
|
+
}),
|
|
157
|
+
options: {
|
|
158
|
+
semanticRecall: {
|
|
159
|
+
topK: 5,
|
|
160
|
+
messageRange: 2,
|
|
161
|
+
indexConfig: {
|
|
162
|
+
type: 'hnsw', // Use HNSW for better performance
|
|
163
|
+
metric: 'dotproduct', // Best for OpenAI embeddings
|
|
164
|
+
m: 16, // Number of bi-directional links (default: 16)
|
|
165
|
+
efConstruction: 64, // Size of candidate list during construction (default: 64)
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
}),
|
|
170
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
For detailed information about index configuration options and performance tuning, see the [PgVector configuration guide](/reference/rag/pg#index-configuration-guide).
|
|
174
|
+
|
|
139
175
|
### Disabling
|
|
140
176
|
|
|
141
177
|
There is a performance impact to using semantic recall. New messages are converted into embeddings and used to query a vector database before new messages are sent to the LLM.
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Memory Threads and Resources | Memory | Mastra Docs"
|
|
3
|
+
description: "Learn how Mastra's memory system works with working memory, conversation history, and semantic recall."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { Callout } from "nextra/components";
|
|
7
|
+
|
|
8
|
+
# Memory threads and resources
|
|
9
|
+
|
|
10
|
+
Mastra organizes memory into threads, which are records that group related interactions, using two identifiers:
|
|
11
|
+
|
|
12
|
+
1. **`thread`**: A globally unique ID representing the conversation (e.g., `support_123`). Must be unique across all resources.
|
|
13
|
+
2. **`resource`**: The user or entity that owns the thread (e.g., `user_123`, `org_456`).
|
|
14
|
+
|
|
15
|
+
The `resource` is especially important for [resource-scoped memory](./working-memory.mdx#resource-scoped-memory), which allows memory to persist across all threads associated with the same user or entity.
|
|
16
|
+
|
|
17
|
+
```typescript {4} showLineNumbers
|
|
18
|
+
const stream = await agent.stream("message for agent", {
|
|
19
|
+
memory: {
|
|
20
|
+
thread: "user-123",
|
|
21
|
+
resource: "test-123"
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
<Callout type="warning">
|
|
27
|
+
Even with memory configured, agents won’t store or recall information unless both `thread` and `resource` are provided.
|
|
28
|
+
</Callout>
|
|
29
|
+
|
|
30
|
+
> Mastra Playground sets `thread` and `resource` IDs automatically. In your own application, you must provide them manually as part of each `.generate()` or `.stream()` call.
|
|
31
|
+
|
|
32
|
+
### Thread title generation
|
|
33
|
+
|
|
34
|
+
Mastra can automatically generate descriptive thread titles based on the user's first message. Enable this by setting `generateTitle` to `true`. This improves organization and makes it easier to display conversations in your UI.
|
|
35
|
+
|
|
36
|
+
```typescript {3-7} showLineNumbers
|
|
37
|
+
export const testAgent = new Agent({
|
|
38
|
+
memory: new Memory({
|
|
39
|
+
options: {
|
|
40
|
+
threads: {
|
|
41
|
+
generateTitle: true,
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
})
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
> Title generation runs asynchronously after the agent responds and does not affect response time. See the [full configuration reference](../../reference/memory/Memory.mdx#thread-title-generation) for details and examples.
|
|
49
|
+
|
|
50
|
+
#### Optimizing title generation
|
|
51
|
+
|
|
52
|
+
Titles are generated using your agent's model by default. To optimize cost or behavior, provide a smaller `model` and custom `instructions`. This keeps title generation separate from main conversation logic.
|
|
53
|
+
|
|
54
|
+
```typescript {5-9} showLineNumbers
|
|
55
|
+
export const testAgent = new Agent({
|
|
56
|
+
// ...
|
|
57
|
+
memory: new Memory({
|
|
58
|
+
options: {
|
|
59
|
+
threads: {
|
|
60
|
+
generateTitle: {
|
|
61
|
+
model: openai("gpt-4.1-nano"),
|
|
62
|
+
instructions: "Generate a concise title based on the user's first message",
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
#### Dynamic model selection and instructions
|
|
71
|
+
|
|
72
|
+
You can configure thread title generation dynamically by passing functions to `model` and `instructions`. These functions receive the `runtimeContext` object, allowing you to adapt title generation based on user-specific values.
|
|
73
|
+
|
|
74
|
+
```typescript {7-16} showLineNumbers
|
|
75
|
+
export const testAgent = new Agent({
|
|
76
|
+
// ...
|
|
77
|
+
memory: new Memory({
|
|
78
|
+
options: {
|
|
79
|
+
threads: {
|
|
80
|
+
generateTitle: {
|
|
81
|
+
model: ({ runtimeContext }) => {
|
|
82
|
+
const userTier = runtimeContext.get("userTier");
|
|
83
|
+
return userTier === "premium" ? openai("gpt-4.1") : openai("gpt-4.1-nano");
|
|
84
|
+
},
|
|
85
|
+
instructions: ({ runtimeContext }) => {
|
|
86
|
+
const language = runtimeContext.get("userLanguage") || "English";
|
|
87
|
+
return `Generate a concise, engaging title in ${language} based on the user's first message.`;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
});
|
|
94
|
+
```
|
|
@@ -7,19 +7,94 @@ description: "Documentation for the `Agent` class in Mastra, which provides the
|
|
|
7
7
|
|
|
8
8
|
The `Agent` class is the foundation for creating AI agents in Mastra. It provides methods for generating responses, streaming interactions, and handling voice capabilities.
|
|
9
9
|
|
|
10
|
-
## Usage
|
|
10
|
+
## Usage examples
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
### Basic string instructions
|
|
13
|
+
|
|
14
|
+
```typescript filename="src/mastra/agents/string-agent.ts" showLineNumbers copy
|
|
13
15
|
import { openai } from "@ai-sdk/openai";
|
|
14
16
|
import { Agent } from "@mastra/core/agent";
|
|
15
17
|
|
|
18
|
+
// String instructions
|
|
16
19
|
export const agent = new Agent({
|
|
17
20
|
name: "test-agent",
|
|
18
|
-
instructions: '
|
|
21
|
+
instructions: 'You are a helpful assistant that provides concise answers.',
|
|
22
|
+
model: openai("gpt-4o")
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// System message object
|
|
26
|
+
export const agent2 = new Agent({
|
|
27
|
+
name: "test-agent-2",
|
|
28
|
+
instructions: {
|
|
29
|
+
role: "system",
|
|
30
|
+
content: "You are an expert programmer"
|
|
31
|
+
},
|
|
32
|
+
model: openai("gpt-4o")
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// Array of system messages
|
|
36
|
+
export const agent3 = new Agent({
|
|
37
|
+
name: "test-agent-3",
|
|
38
|
+
instructions: [
|
|
39
|
+
{ role: "system", content: "You are a helpful assistant" },
|
|
40
|
+
{ role: "system", content: "You have expertise in TypeScript" }
|
|
41
|
+
],
|
|
19
42
|
model: openai("gpt-4o")
|
|
20
43
|
});
|
|
21
44
|
```
|
|
22
45
|
|
|
46
|
+
### Single CoreSystemMessage
|
|
47
|
+
|
|
48
|
+
Use CoreSystemMessage format to access additional properties like `providerOptions` for provider-specific configurations:
|
|
49
|
+
|
|
50
|
+
```typescript filename="src/mastra/agents/core-message-agent.ts" showLineNumbers copy
|
|
51
|
+
import { openai } from "@ai-sdk/openai";
|
|
52
|
+
import { Agent } from "@mastra/core/agent";
|
|
53
|
+
|
|
54
|
+
export const agent = new Agent({
|
|
55
|
+
name: "core-message-agent",
|
|
56
|
+
instructions: {
|
|
57
|
+
role: 'system',
|
|
58
|
+
content: 'You are a helpful assistant specialized in technical documentation.',
|
|
59
|
+
providerOptions: {
|
|
60
|
+
openai: {
|
|
61
|
+
reasoningEffort: 'low'
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
model: openai("gpt-5")
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Multiple CoreSystemMessages
|
|
70
|
+
|
|
71
|
+
```typescript filename="src/mastra/agents/multi-message-agent.ts" showLineNumbers copy
|
|
72
|
+
import { anthropic } from "@ai-sdk/anthropic";
|
|
73
|
+
import { Agent } from "@mastra/core/agent";
|
|
74
|
+
|
|
75
|
+
// This could be customizable based on the user
|
|
76
|
+
const preferredTone = {
|
|
77
|
+
role: 'system',
|
|
78
|
+
content: 'Always maintain a professional and empathetic tone.',
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export const agent = new Agent({
|
|
82
|
+
name: "multi-message-agent",
|
|
83
|
+
instructions: [
|
|
84
|
+
{ role: 'system', content: 'You are a customer service representative.' },
|
|
85
|
+
preferredTone,
|
|
86
|
+
{
|
|
87
|
+
role: 'system',
|
|
88
|
+
content: 'Escalate complex issues to human agents when needed.',
|
|
89
|
+
providerOptions: {
|
|
90
|
+
anthropic: { cacheControl: { type: 'ephemeral' } },
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
model: anthropic('claude-sonnet-4-20250514'),
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
23
98
|
## Constructor parameters
|
|
24
99
|
|
|
25
100
|
<PropertiesTable
|
|
@@ -44,9 +119,11 @@ export const agent = new Agent({
|
|
|
44
119
|
},
|
|
45
120
|
{
|
|
46
121
|
name: "instructions",
|
|
47
|
-
type: "
|
|
122
|
+
type: "SystemMessage | ({ runtimeContext: RuntimeContext }) => SystemMessage | Promise<SystemMessage>",
|
|
48
123
|
isOptional: false,
|
|
49
|
-
description:
|
|
124
|
+
description: `Instructions that guide the agent's behavior. Can be a string, array of strings, system message object,
|
|
125
|
+
array of system messages, or a function that returns any of these types dynamically.
|
|
126
|
+
SystemMessage types: string | string[] | CoreSystemMessage | CoreSystemMessage[] | SystemModelMessage | SystemModelMessage[]`,
|
|
50
127
|
},
|
|
51
128
|
{
|
|
52
129
|
name: "model",
|
|
@@ -33,8 +33,8 @@ await agent.getInstructions();
|
|
|
33
33
|
content={[
|
|
34
34
|
{
|
|
35
35
|
name: "instructions",
|
|
36
|
-
type: "
|
|
37
|
-
description: "The instructions configured for the agent
|
|
36
|
+
type: "SystemMessage | Promise<SystemMessage>",
|
|
37
|
+
description: "The instructions configured for the agent. SystemMessage can be: string | string[] | CoreSystemMessage | CoreSystemMessage[] | SystemModelMessage | SystemModelMessage[]. Returns either directly or as a promise that resolves to the instructions.",
|
|
38
38
|
},
|
|
39
39
|
]}
|
|
40
40
|
/>
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "MastraAuthAuth0 Class"
|
|
3
|
+
description: "API reference for the MastraAuthAuth0 class, which authenticates Mastra applications using Auth0 authentication."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# MastraAuthAuth0 Class
|
|
7
|
+
|
|
8
|
+
The `MastraAuthAuth0` class provides authentication for Mastra using Auth0. It verifies incoming requests using Auth0-issued JWT tokens and integrates with the Mastra server using the `experimental_auth` option.
|
|
9
|
+
|
|
10
|
+
## Usage example
|
|
11
|
+
|
|
12
|
+
```typescript filename="src/mastra/index.ts" showLineNumbers copy
|
|
13
|
+
import { Mastra } from "@mastra/core/mastra";
|
|
14
|
+
import { MastraAuthAuth0 } from '@mastra/auth-auth0';
|
|
15
|
+
|
|
16
|
+
export const mastra = new Mastra({
|
|
17
|
+
// ..
|
|
18
|
+
server: {
|
|
19
|
+
experimental_auth: new MastraAuthAuth0({
|
|
20
|
+
domain: process.env.AUTH0_DOMAIN,
|
|
21
|
+
audience: process.env.AUTH0_AUDIENCE
|
|
22
|
+
}),
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
> **Note:** You can omit the constructor parameters if you have the appropriately named environment variables (`AUTH0_DOMAIN` and `AUTH0_AUDIENCE`) set. In that case, simply use `new MastraAuthAuth0()` without any arguments.
|
|
28
|
+
|
|
29
|
+
## Constructor parameters
|
|
30
|
+
|
|
31
|
+
<PropertiesTable
|
|
32
|
+
content={[
|
|
33
|
+
{
|
|
34
|
+
name: "domain",
|
|
35
|
+
type: "string",
|
|
36
|
+
description: "Your Auth0 domain (e.g., your-tenant.auth0.com). This is used to verify JWT tokens issued by your Auth0 tenant.",
|
|
37
|
+
isOptional: true,
|
|
38
|
+
defaultValue: "process.env.AUTH0_DOMAIN"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
name: "audience",
|
|
42
|
+
type: "string",
|
|
43
|
+
description: "Your Auth0 API identifier/audience. This ensures tokens are intended for your specific API.",
|
|
44
|
+
isOptional: true,
|
|
45
|
+
defaultValue: "process.env.AUTH0_AUDIENCE"
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: "name",
|
|
49
|
+
type: "string",
|
|
50
|
+
description: "Custom name for the auth provider instance.",
|
|
51
|
+
isOptional: true,
|
|
52
|
+
defaultValue: '"auth0"'
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: "authorizeUser",
|
|
56
|
+
type: "(user: Auth0User) => Promise<boolean> | boolean",
|
|
57
|
+
description: "Custom authorization function to determine if a user should be granted access. Called after token verification. By default, allows all authenticated users with valid tokens.",
|
|
58
|
+
isOptional: true,
|
|
59
|
+
},
|
|
60
|
+
]}
|
|
61
|
+
/>
|
|
62
|
+
|
|
63
|
+
## Environment Variables
|
|
64
|
+
|
|
65
|
+
The following environment variables are automatically used when constructor options are not provided:
|
|
66
|
+
|
|
67
|
+
<PropertiesTable
|
|
68
|
+
content={[
|
|
69
|
+
{
|
|
70
|
+
name: "AUTH0_DOMAIN",
|
|
71
|
+
type: "string",
|
|
72
|
+
description: "Your Auth0 domain. Can be found in your Auth0 Dashboard under Applications > Settings.",
|
|
73
|
+
isOptional: true,
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
name: "AUTH0_AUDIENCE",
|
|
77
|
+
type: "string",
|
|
78
|
+
description: "Your Auth0 API identifier. This is the identifier you set when creating an API in your Auth0 Dashboard.",
|
|
79
|
+
isOptional: true,
|
|
80
|
+
},
|
|
81
|
+
]}
|
|
82
|
+
/>
|
|
83
|
+
|
|
84
|
+
## Default Authorization Behavior
|
|
85
|
+
|
|
86
|
+
By default, `MastraAuthAuth0` validates Auth0 JWT tokens and allows access to all authenticated users:
|
|
87
|
+
|
|
88
|
+
1. **Token Verification**: The JWT token is verified using Auth0's public keys (JWKS)
|
|
89
|
+
2. **Signature Validation**: Ensures the token was signed by your Auth0 tenant
|
|
90
|
+
3. **Expiration Check**: Verifies the token has not expired
|
|
91
|
+
4. **Audience Validation**: Confirms the token was issued for your specific API (audience)
|
|
92
|
+
5. **Issuer Validation**: Ensures the token was issued by your Auth0 domain
|
|
93
|
+
|
|
94
|
+
If all validations pass, the user is considered authorized. To implement custom authorization logic (e.g., role-based access control), provide a custom `authorizeUser` function.
|
|
95
|
+
|
|
96
|
+
## Auth0 User Type
|
|
97
|
+
|
|
98
|
+
The `Auth0User` type used in the `authorizeUser` function corresponds to the decoded JWT token payload, which typically includes:
|
|
99
|
+
|
|
100
|
+
- `sub`: The user's unique identifier (subject)
|
|
101
|
+
- `email`: The user's email address (if included in token)
|
|
102
|
+
- `email_verified`: Whether the email is verified
|
|
103
|
+
- `name`: The user's display name (if available)
|
|
104
|
+
- `picture`: URL to the user's profile picture (if available)
|
|
105
|
+
- `iss`: Token issuer (your Auth0 domain)
|
|
106
|
+
- `aud`: Token audience (your API identifier)
|
|
107
|
+
- `iat`: Token issued at timestamp
|
|
108
|
+
- `exp`: Token expiration timestamp
|
|
109
|
+
- `scope`: Granted scopes for the token
|
|
110
|
+
- Custom claims and app metadata configured in your Auth0 tenant
|
|
111
|
+
|
|
112
|
+
The exact properties available depend on your Auth0 configuration, scopes requested, and any custom claims you've configured.
|
|
113
|
+
|
|
114
|
+
## Related
|
|
115
|
+
|
|
116
|
+
[MastraAuthAuth0 Class](/docs/auth/auth0.mdx)
|
|
117
|
+
|