@mastra/mcp-docs-server 0.0.0-commonjs-20250414101718
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%2Fastra.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fchroma.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +161 -0
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +110 -0
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-vercel.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fevals.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Ffirecrawl.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fgithub.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fmcp-registry-registry.md +26 -0
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fmem0.md +196 -0
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Frag.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fragie.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-azure.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-deepgram.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-elevenlabs.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-google.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-ibm.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-murf.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-openai.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-playai.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-replicate.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fspeech-speechify.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-azure.md +250 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-cloudflare.md +250 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-deepgram.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-elevenlabs.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-murf.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai-realtime.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-playai.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-sarvam.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-speechify.md +302 -0
- package/.docs/organized/changelogs/create-mastra.md +302 -0
- package/.docs/organized/changelogs/mastra.md +302 -0
- package/.docs/organized/code-examples/agent-network.md +282 -0
- package/.docs/organized/code-examples/agent.md +388 -0
- package/.docs/organized/code-examples/ai-sdk-useChat.md +378 -0
- package/.docs/organized/code-examples/assistant-ui.md +37 -0
- package/.docs/organized/code-examples/bird-checker-with-express.md +235 -0
- package/.docs/organized/code-examples/bird-checker-with-nextjs-and-eval.md +360 -0
- package/.docs/organized/code-examples/bird-checker-with-nextjs.md +250 -0
- package/.docs/organized/code-examples/client-side-tools.md +69 -0
- package/.docs/organized/code-examples/crypto-chatbot.md +96 -0
- package/.docs/organized/code-examples/fireworks-r1.md +159 -0
- package/.docs/organized/code-examples/mcp-registry-registry.md +63 -0
- package/.docs/organized/code-examples/memory-todo-agent.md +164 -0
- package/.docs/organized/code-examples/memory-with-context.md +167 -0
- package/.docs/organized/code-examples/memory-with-libsql.md +204 -0
- package/.docs/organized/code-examples/memory-with-mem0.md +121 -0
- package/.docs/organized/code-examples/memory-with-pg.md +224 -0
- package/.docs/organized/code-examples/memory-with-upstash.md +268 -0
- package/.docs/organized/code-examples/quick-start.md +129 -0
- package/.docs/organized/code-examples/stock-price-tool.md +124 -0
- package/.docs/organized/code-examples/weather-agent.md +353 -0
- package/.docs/organized/code-examples/workflow-ai-recruiter.md +159 -0
- package/.docs/organized/code-examples/workflow-with-inline-steps.md +111 -0
- package/.docs/organized/code-examples/workflow-with-memory.md +393 -0
- package/.docs/organized/code-examples/workflow-with-separate-steps.md +131 -0
- package/.docs/raw/agents/adding-tools.mdx +317 -0
- package/.docs/raw/agents/adding-voice.mdx +175 -0
- package/.docs/raw/agents/agent-memory.mdx +62 -0
- package/.docs/raw/agents/mcp-guide.mdx +215 -0
- package/.docs/raw/agents/overview.mdx +303 -0
- package/.docs/raw/community/discord.mdx +12 -0
- package/.docs/raw/community/licensing.mdx +63 -0
- package/.docs/raw/deployment/client.mdx +120 -0
- package/.docs/raw/deployment/deployment.mdx +127 -0
- package/.docs/raw/deployment/server.mdx +282 -0
- package/.docs/raw/evals/custom-eval.mdx +22 -0
- package/.docs/raw/evals/overview.mdx +95 -0
- package/.docs/raw/evals/running-in-ci.mdx +81 -0
- package/.docs/raw/evals/textual-evals.mdx +54 -0
- package/.docs/raw/faq/index.mdx +63 -0
- package/.docs/raw/frameworks/ai-sdk.mdx +296 -0
- package/.docs/raw/frameworks/next-js.mdx +238 -0
- package/.docs/raw/getting-started/installation.mdx +442 -0
- package/.docs/raw/getting-started/mcp-docs-server.mdx +141 -0
- package/.docs/raw/getting-started/project-structure.mdx +80 -0
- package/.docs/raw/index.mdx +22 -0
- package/.docs/raw/integrations/index.mdx +213 -0
- package/.docs/raw/local-dev/add-to-existing-project.mdx +48 -0
- package/.docs/raw/local-dev/creating-a-new-project.mdx +54 -0
- package/.docs/raw/local-dev/mastra-dev.mdx +108 -0
- package/.docs/raw/memory/memory-processors.mdx +131 -0
- package/.docs/raw/memory/overview.mdx +119 -0
- package/.docs/raw/memory/semantic-recall.mdx +122 -0
- package/.docs/raw/memory/working-memory.mdx +87 -0
- package/.docs/raw/observability/logging.mdx +38 -0
- package/.docs/raw/observability/nextjs-tracing.mdx +108 -0
- package/.docs/raw/observability/tracing.mdx +115 -0
- package/.docs/raw/rag/chunking-and-embedding.mdx +156 -0
- package/.docs/raw/rag/overview.mdx +85 -0
- package/.docs/raw/rag/retrieval.mdx +365 -0
- package/.docs/raw/rag/vector-databases.mdx +340 -0
- package/.docs/raw/reference/agents/createTool.mdx +229 -0
- package/.docs/raw/reference/agents/generate.mdx +334 -0
- package/.docs/raw/reference/agents/getAgent.mdx +54 -0
- package/.docs/raw/reference/agents/stream.mdx +369 -0
- package/.docs/raw/reference/cli/build.mdx +55 -0
- package/.docs/raw/reference/cli/dev.mdx +134 -0
- package/.docs/raw/reference/cli/init.mdx +43 -0
- package/.docs/raw/reference/client-js/agents.mdx +107 -0
- package/.docs/raw/reference/client-js/error-handling.mdx +38 -0
- package/.docs/raw/reference/client-js/logs.mdx +24 -0
- package/.docs/raw/reference/client-js/memory.mdx +97 -0
- package/.docs/raw/reference/client-js/telemetry.mdx +20 -0
- package/.docs/raw/reference/client-js/tools.mdx +44 -0
- package/.docs/raw/reference/client-js/vectors.mdx +79 -0
- package/.docs/raw/reference/client-js/workflows.mdx +136 -0
- package/.docs/raw/reference/core/mastra-class.mdx +232 -0
- package/.docs/raw/reference/deployer/cloudflare.mdx +207 -0
- package/.docs/raw/reference/deployer/deployer.mdx +159 -0
- package/.docs/raw/reference/deployer/netlify.mdx +109 -0
- package/.docs/raw/reference/deployer/vercel.mdx +117 -0
- package/.docs/raw/reference/evals/answer-relevancy.mdx +186 -0
- package/.docs/raw/reference/evals/bias.mdx +186 -0
- package/.docs/raw/reference/evals/completeness.mdx +174 -0
- package/.docs/raw/reference/evals/content-similarity.mdx +183 -0
- package/.docs/raw/reference/evals/context-position.mdx +190 -0
- package/.docs/raw/reference/evals/context-precision.mdx +189 -0
- package/.docs/raw/reference/evals/context-relevancy.mdx +188 -0
- package/.docs/raw/reference/evals/contextual-recall.mdx +191 -0
- package/.docs/raw/reference/evals/faithfulness.mdx +193 -0
- package/.docs/raw/reference/evals/hallucination.mdx +219 -0
- package/.docs/raw/reference/evals/keyword-coverage.mdx +176 -0
- package/.docs/raw/reference/evals/prompt-alignment.mdx +238 -0
- package/.docs/raw/reference/evals/summarization.mdx +205 -0
- package/.docs/raw/reference/evals/textual-difference.mdx +161 -0
- package/.docs/raw/reference/evals/tone-consistency.mdx +181 -0
- package/.docs/raw/reference/evals/toxicity.mdx +165 -0
- package/.docs/raw/reference/index.mdx +12 -0
- package/.docs/raw/reference/memory/Memory.mdx +212 -0
- package/.docs/raw/reference/memory/createThread.mdx +95 -0
- package/.docs/raw/reference/memory/getThreadById.mdx +46 -0
- package/.docs/raw/reference/memory/getThreadsByResourceId.mdx +48 -0
- package/.docs/raw/reference/memory/query.mdx +167 -0
- package/.docs/raw/reference/networks/agent-network.mdx +159 -0
- package/.docs/raw/reference/observability/create-logger.mdx +106 -0
- package/.docs/raw/reference/observability/logger.mdx +55 -0
- package/.docs/raw/reference/observability/otel-config.mdx +120 -0
- package/.docs/raw/reference/observability/providers/braintrust.mdx +40 -0
- package/.docs/raw/reference/observability/providers/dash0.mdx +40 -0
- package/.docs/raw/reference/observability/providers/index.mdx +16 -0
- package/.docs/raw/reference/observability/providers/laminar.mdx +41 -0
- package/.docs/raw/reference/observability/providers/langfuse.mdx +51 -0
- package/.docs/raw/reference/observability/providers/langsmith.mdx +48 -0
- package/.docs/raw/reference/observability/providers/langwatch.mdx +45 -0
- package/.docs/raw/reference/observability/providers/new-relic.mdx +40 -0
- package/.docs/raw/reference/observability/providers/signoz.mdx +40 -0
- package/.docs/raw/reference/observability/providers/traceloop.mdx +40 -0
- package/.docs/raw/reference/rag/astra.mdx +258 -0
- package/.docs/raw/reference/rag/chroma.mdx +281 -0
- package/.docs/raw/reference/rag/chunk.mdx +235 -0
- package/.docs/raw/reference/rag/document.mdx +127 -0
- package/.docs/raw/reference/rag/embeddings.mdx +160 -0
- package/.docs/raw/reference/rag/extract-params.mdx +226 -0
- package/.docs/raw/reference/rag/graph-rag.mdx +182 -0
- package/.docs/raw/reference/rag/libsql.mdx +357 -0
- package/.docs/raw/reference/rag/metadata-filters.mdx +298 -0
- package/.docs/raw/reference/rag/pg.mdx +477 -0
- package/.docs/raw/reference/rag/pinecone.mdx +281 -0
- package/.docs/raw/reference/rag/qdrant.mdx +236 -0
- package/.docs/raw/reference/rag/rerank.mdx +212 -0
- package/.docs/raw/reference/rag/turbopuffer.mdx +249 -0
- package/.docs/raw/reference/rag/upstash.mdx +247 -0
- package/.docs/raw/reference/rag/vectorize.mdx +298 -0
- package/.docs/raw/reference/storage/libsql.mdx +74 -0
- package/.docs/raw/reference/storage/postgresql.mdx +48 -0
- package/.docs/raw/reference/storage/upstash.mdx +86 -0
- package/.docs/raw/reference/tools/client.mdx +207 -0
- package/.docs/raw/reference/tools/document-chunker-tool.mdx +141 -0
- package/.docs/raw/reference/tools/graph-rag-tool.mdx +154 -0
- package/.docs/raw/reference/tools/mcp-configuration.mdx +206 -0
- package/.docs/raw/reference/tools/vector-query-tool.mdx +212 -0
- package/.docs/raw/reference/voice/composite-voice.mdx +140 -0
- package/.docs/raw/reference/voice/deepgram.mdx +164 -0
- package/.docs/raw/reference/voice/elevenlabs.mdx +216 -0
- package/.docs/raw/reference/voice/google.mdx +198 -0
- package/.docs/raw/reference/voice/mastra-voice.mdx +394 -0
- package/.docs/raw/reference/voice/murf.mdx +251 -0
- package/.docs/raw/reference/voice/openai-realtime.mdx +431 -0
- package/.docs/raw/reference/voice/openai.mdx +168 -0
- package/.docs/raw/reference/voice/playai.mdx +159 -0
- package/.docs/raw/reference/voice/sarvam.mdx +260 -0
- package/.docs/raw/reference/voice/speechify.mdx +145 -0
- package/.docs/raw/reference/voice/voice.answer.mdx +122 -0
- package/.docs/raw/reference/voice/voice.connect.mdx +124 -0
- package/.docs/raw/reference/voice/voice.listen.mdx +195 -0
- package/.docs/raw/reference/voice/voice.on.mdx +189 -0
- package/.docs/raw/reference/voice/voice.send.mdx +118 -0
- package/.docs/raw/reference/voice/voice.speak.mdx +203 -0
- package/.docs/raw/reference/workflows/after.mdx +88 -0
- package/.docs/raw/reference/workflows/afterEvent.mdx +76 -0
- package/.docs/raw/reference/workflows/commit.mdx +37 -0
- package/.docs/raw/reference/workflows/createRun.mdx +77 -0
- package/.docs/raw/reference/workflows/else.mdx +72 -0
- package/.docs/raw/reference/workflows/events.mdx +305 -0
- package/.docs/raw/reference/workflows/execute.mdx +110 -0
- package/.docs/raw/reference/workflows/if.mdx +107 -0
- package/.docs/raw/reference/workflows/resume.mdx +155 -0
- package/.docs/raw/reference/workflows/resumeWithEvent.mdx +133 -0
- package/.docs/raw/reference/workflows/snapshots.mdx +207 -0
- package/.docs/raw/reference/workflows/start.mdx +84 -0
- package/.docs/raw/reference/workflows/step-class.mdx +100 -0
- package/.docs/raw/reference/workflows/step-condition.mdx +134 -0
- package/.docs/raw/reference/workflows/step-function.mdx +92 -0
- package/.docs/raw/reference/workflows/step-options.mdx +69 -0
- package/.docs/raw/reference/workflows/step-retries.mdx +203 -0
- package/.docs/raw/reference/workflows/suspend.mdx +70 -0
- package/.docs/raw/reference/workflows/then.mdx +74 -0
- package/.docs/raw/reference/workflows/until.mdx +165 -0
- package/.docs/raw/reference/workflows/watch.mdx +118 -0
- package/.docs/raw/reference/workflows/while.mdx +168 -0
- package/.docs/raw/reference/workflows/workflow.mdx +233 -0
- package/.docs/raw/storage/overview.mdx +378 -0
- package/.docs/raw/voice/overview.mdx +135 -0
- package/.docs/raw/voice/speech-to-text.mdx +45 -0
- package/.docs/raw/voice/text-to-speech.mdx +52 -0
- package/.docs/raw/voice/voice-to-voice.mdx +310 -0
- package/.docs/raw/workflows/control-flow.mdx +778 -0
- package/.docs/raw/workflows/dynamic-workflows.mdx +236 -0
- package/.docs/raw/workflows/error-handling.mdx +183 -0
- package/.docs/raw/workflows/nested-workflows.mdx +352 -0
- package/.docs/raw/workflows/overview.mdx +203 -0
- package/.docs/raw/workflows/steps.mdx +108 -0
- package/.docs/raw/workflows/suspend-and-resume.mdx +404 -0
- package/.docs/raw/workflows/variables.mdx +313 -0
- package/LICENSE.md +46 -0
- package/README.md +129 -0
- package/dist/_tsup-dts-rollup.d.ts +149 -0
- package/dist/chunk-QWYMT5LP.js +194 -0
- package/dist/prepare-docs/prepare.d.ts +1 -0
- package/dist/prepare-docs/prepare.js +1 -0
- package/dist/stdio.d.ts +1 -0
- package/dist/stdio.js +518 -0
- package/package.json +60 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# Memory Processors
|
|
2
|
+
|
|
3
|
+
Memory Processors allow you to modify the list of messages retrieved from memory _before_ they are added to the agent's context window and sent to the LLM. This is useful for managing context size, filtering content, and optimizing performance.
|
|
4
|
+
|
|
5
|
+
Processors operate on the messages retrieved based on your memory configuration (e.g., `lastMessages`, `semanticRecall`). They do **not** affect the new incoming user message.
|
|
6
|
+
|
|
7
|
+
## Built-in Processors
|
|
8
|
+
|
|
9
|
+
Mastra provides built-in processors:
|
|
10
|
+
|
|
11
|
+
### `TokenLimiter`
|
|
12
|
+
|
|
13
|
+
This processor is used to prevent errors caused by exceeding the LLM's context window limit. It counts the tokens in the retrieved memory messages and removes the oldest messages until the total count is below the specified `limit`.
|
|
14
|
+
|
|
15
|
+
```typescript copy showLineNumbers {9-12}
|
|
16
|
+
import { Memory } from "@mastra/memory";
|
|
17
|
+
import { TokenLimiter } from "@mastra/memory/processors";
|
|
18
|
+
import { Agent } from "@mastra/core/agent";
|
|
19
|
+
import { openai } from "@ai-sdk/openai";
|
|
20
|
+
|
|
21
|
+
const agent = new Agent({
|
|
22
|
+
model: openai("gpt-4o"),
|
|
23
|
+
memory: new Memory({
|
|
24
|
+
processors: [
|
|
25
|
+
// Ensure the total tokens from memory don't exceed ~127k
|
|
26
|
+
new TokenLimiter(127000),
|
|
27
|
+
],
|
|
28
|
+
}),
|
|
29
|
+
});
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The `TokenLimiter` uses the `o200k_base` encoding by default (suitable for GPT-4o). You can specify other encodings if needed for different models:
|
|
33
|
+
|
|
34
|
+
```typescript copy showLineNumbers {6-9}
|
|
35
|
+
// Import the encoding you need (e.g., for older OpenAI models)
|
|
36
|
+
import cl100k_base from "js-tiktoken/ranks/cl100k_base";
|
|
37
|
+
|
|
38
|
+
const memoryForOlderModel = new Memory({
|
|
39
|
+
processors: [
|
|
40
|
+
new TokenLimiter({
|
|
41
|
+
limit: 16000, // Example limit for a 16k context model
|
|
42
|
+
encoding: cl100k_base,
|
|
43
|
+
}),
|
|
44
|
+
],
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
See the [OpenAI cookbook](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken#encodings) or [`js-tiktoken` repo](https://github.com/dqbd/tiktoken) for more on encodings.
|
|
49
|
+
|
|
50
|
+
### `ToolCallFilter`
|
|
51
|
+
|
|
52
|
+
This processor removes tool calls from the memory messages sent to the LLM. It saves tokens by excluding potentially verbose tool interactions from the context, which is useful if the details aren't needed for future interactions. It's also useful if you always want your agent to call a specific tool again and not rely on previous tool results in memory.
|
|
53
|
+
|
|
54
|
+
```typescript copy showLineNumbers {5-14}
|
|
55
|
+
import { Memory } from "@mastra/memory";
|
|
56
|
+
import { ToolCallFilter, TokenLimiter } from "@mastra/memory/processors";
|
|
57
|
+
|
|
58
|
+
const memoryFilteringTools = new Memory({
|
|
59
|
+
processors: [
|
|
60
|
+
// Example 1: Remove all tool calls/results
|
|
61
|
+
new ToolCallFilter(),
|
|
62
|
+
|
|
63
|
+
// Example 2: Remove only noisy image generation tool calls/results
|
|
64
|
+
new ToolCallFilter({ exclude: ["generateImageTool"] }),
|
|
65
|
+
|
|
66
|
+
// Always place TokenLimiter last
|
|
67
|
+
new TokenLimiter(127000),
|
|
68
|
+
],
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Applying Multiple Processors
|
|
73
|
+
|
|
74
|
+
You can chain multiple processors. They execute in the order they appear in the `processors` array. The output of one processor becomes the input for the next.
|
|
75
|
+
|
|
76
|
+
**Order matters!** It's generally best practice to place `TokenLimiter` **last** in the chain. This ensures it operates on the final set of messages after other filtering has occurred, providing the most accurate token limit enforcement.
|
|
77
|
+
|
|
78
|
+
```typescript copy showLineNumbers {7-14}
|
|
79
|
+
import { Memory } from "@mastra/memory";
|
|
80
|
+
import { ToolCallFilter, TokenLimiter } from "@mastra/memory/processors";
|
|
81
|
+
// Assume a hypothetical 'PIIFilter' custom processor exists
|
|
82
|
+
// import { PIIFilter } from './custom-processors';
|
|
83
|
+
|
|
84
|
+
const memoryWithMultipleProcessors = new Memory({
|
|
85
|
+
processors: [
|
|
86
|
+
// 1. Filter specific tool calls first
|
|
87
|
+
new ToolCallFilter({ exclude: ["verboseDebugTool"] }),
|
|
88
|
+
// 2. Apply custom filtering (e.g., remove hypothetical PII - use with caution)
|
|
89
|
+
// new PIIFilter(),
|
|
90
|
+
// 3. Apply token limiting as the final step
|
|
91
|
+
new TokenLimiter(127000),
|
|
92
|
+
],
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Creating Custom Processors
|
|
97
|
+
|
|
98
|
+
You can create custom logic by extending the base `MemoryProcessor` class.
|
|
99
|
+
|
|
100
|
+
```typescript copy showLineNumbers {4-19,23-26}
|
|
101
|
+
import { Memory, CoreMessage } from "@mastra/memory";
|
|
102
|
+
import { MemoryProcessor, MemoryProcessorOpts } from "@mastra/core/memory";
|
|
103
|
+
|
|
104
|
+
class ConversationOnlyFilter extends MemoryProcessor {
|
|
105
|
+
constructor() {
|
|
106
|
+
// Provide a name for easier debugging if needed
|
|
107
|
+
super({ name: "ConversationOnlyFilter" });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
process(
|
|
111
|
+
messages: CoreMessage[],
|
|
112
|
+
_opts: MemoryProcessorOpts = {}, // Options passed during memory retrieval, rarely needed here
|
|
113
|
+
): CoreMessage[] {
|
|
114
|
+
// Filter messages based on role
|
|
115
|
+
return messages.filter(
|
|
116
|
+
(msg) => msg.role === "user" || msg.role === "assistant",
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Use the custom processor
|
|
122
|
+
const memoryWithCustomFilter = new Memory({
|
|
123
|
+
processors: [
|
|
124
|
+
new ConversationOnlyFilter(),
|
|
125
|
+
new TokenLimiter(127000), // Still apply token limiting
|
|
126
|
+
],
|
|
127
|
+
});
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
When creating custom processors avoid mutating the input `messages` array or its objects directly.
|
|
131
|
+
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
## Overview
|
|
2
|
+
|
|
3
|
+
Memory is how agents manage the context that's available to them, it's a condensation of all chat messages into their context window.
|
|
4
|
+
|
|
5
|
+
## The Context Window
|
|
6
|
+
|
|
7
|
+
The context window is the total information visible to the language model at any given time.
|
|
8
|
+
|
|
9
|
+
In Mastra, context is broken up into three parts: system instructions and information about the user ([working memory](./working-memory.mdx)), recent messages ([message history](#conversation-history)), and older messages that are relevant to the user’s query ([semantic recall](./semantic-recall.mdx)).
|
|
10
|
+
|
|
11
|
+
In addition, we provide [memory processors](./memory-processors.mdx) to trim context or remove information if the context is too long.
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
The fastest way to see memory in action is using the built-in development playground.
|
|
16
|
+
|
|
17
|
+
If you haven't already, create a new Mastra project following the main [Getting Started guide](/docs/getting-started/installation).
|
|
18
|
+
|
|
19
|
+
**1. Install the memory package:**
|
|
20
|
+
|
|
21
|
+
```bash npm2yarn copy
|
|
22
|
+
npm install @mastra/memory
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**2. Create an agent and attach a `Memory` instance:**
|
|
26
|
+
|
|
27
|
+
```typescript filename="src/mastra/agents/index.ts" {10}
|
|
28
|
+
import { Agent } from "@mastra/core/agent";
|
|
29
|
+
import { Memory } from "@mastra/memory";
|
|
30
|
+
import { openai } from "@ai-sdk/openai";
|
|
31
|
+
|
|
32
|
+
export const myMemoryAgent = new Agent({
|
|
33
|
+
name: "MemoryAgent",
|
|
34
|
+
instructions: "...",
|
|
35
|
+
model: openai("gpt-4o"),
|
|
36
|
+
|
|
37
|
+
memory: new Memory(),
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
**3. Start the Development Server:**
|
|
42
|
+
|
|
43
|
+
```bash npm2yarn copy
|
|
44
|
+
npm run dev
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
**4. Open the playground (http://localhost:4111) and select your `MemoryAgent`:**
|
|
48
|
+
|
|
49
|
+
Send a few messages and notice that it remembers information across turns:
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
➡️ You: My favorite color is blue.
|
|
53
|
+
⬅️ Agent: Got it! I'll remember that your favorite color is blue.
|
|
54
|
+
➡️ You: What is my favorite color?
|
|
55
|
+
⬅️ Agent: Your favorite color is blue.
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Memory Threads
|
|
59
|
+
|
|
60
|
+
Mastra organizes memory into threads, which are records that identify specific conversation histories, using two identifiers:
|
|
61
|
+
|
|
62
|
+
1. **`threadId`**: A specific conversation id (e.g., `support_123`).
|
|
63
|
+
2. **`resourceId`**: The user or entity id that owns each thread (e.g., `user_123`, `org_456`).
|
|
64
|
+
|
|
65
|
+
```typescript {2,3}
|
|
66
|
+
const response = await myMemoryAgent.stream("Hello, my name is Alice.", {
|
|
67
|
+
resourceId: "user_alice",
|
|
68
|
+
threadId: "conversation_123",
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
**Important:** without these ID's your agent will not use memory, even if memory is properly configured. The playground handles this for you, but you need to add ID's yourself when using memory in your application.
|
|
73
|
+
|
|
74
|
+
## Conversation History
|
|
75
|
+
|
|
76
|
+
By default, the `Memory` instance includes the [last 40 messages](../../reference/memory/Memory.mdx) from the current Memory thread in each new request. This provides the agent with immediate conversational context.
|
|
77
|
+
|
|
78
|
+
```ts {3}
|
|
79
|
+
const memory = new Memory({
|
|
80
|
+
options: {
|
|
81
|
+
lastMessages: 10,
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
**Important:** Only send the newest user message in each agent call. Mastra handles retrieving and injecting the necessary history. Sending the full history yourself will cause duplication. See the [AI SDK Memory Example](../../examples/memory/use-chat.mdx) for how to handle this with when using the `useChat` frontend hooks.
|
|
87
|
+
|
|
88
|
+
### Storage Configuration
|
|
89
|
+
|
|
90
|
+
Conversation history relies on a [storage adapter](/reference/memory/Memory#parameters) to store messages.
|
|
91
|
+
|
|
92
|
+
```ts {7-12}
|
|
93
|
+
import { Memory } from "@mastra/memory";
|
|
94
|
+
import { Agent } from "@mastra/core/agent";
|
|
95
|
+
import { LibSQLStore } from "@mastra/core/storage/libsql";
|
|
96
|
+
|
|
97
|
+
const agent = new Agent({
|
|
98
|
+
memory: new Memory({
|
|
99
|
+
// this is the default storage db if omitted
|
|
100
|
+
storage: new LibSQLStore({
|
|
101
|
+
config: {
|
|
102
|
+
url: "file:local.db",
|
|
103
|
+
},
|
|
104
|
+
}),
|
|
105
|
+
}),
|
|
106
|
+
});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
**Storage code Examples**:
|
|
110
|
+
|
|
111
|
+
- [LibSQL](/examples/memory/memory-with-libsql)
|
|
112
|
+
- [Postgres](/examples/memory/memory-with-pg)
|
|
113
|
+
- [Upstash](/examples/memory/memory-with-upstash)
|
|
114
|
+
|
|
115
|
+
## Next Steps
|
|
116
|
+
|
|
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.
|
|
118
|
+
|
|
119
|
+
Alternatively you can visit the [configuration reference](../../reference/memory/Memory.mdx) for available options, or browse [usage examples](../../examples/memory/use-chat.mdx).
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# Recalling Relevant History
|
|
2
|
+
|
|
3
|
+
If you ask your friend what they did last weekend, they will search in their memory for events associated with "last weekend" and then tell you what they did. That's sort of like how semantic recall works in Mastra.
|
|
4
|
+
|
|
5
|
+
## How Semantic Recall Works
|
|
6
|
+
|
|
7
|
+
Semantic recall is RAG-based search that helps agents maintain context across longer interactions when messages are no longer within [recent conversation history](./overview.mdx#conversation-history).
|
|
8
|
+
|
|
9
|
+
It uses vector embeddings of messages for similarity search, integrates with various vector stores, and has configurable context windows around retrieved messages.
|
|
10
|
+
|
|
11
|
+
<br />
|
|
12
|
+
<img
|
|
13
|
+
src="/docs/semantic-recall.png"
|
|
14
|
+
alt="Diagram showing Mastra Memory semantic recall"
|
|
15
|
+
width={800}
|
|
16
|
+
/>
|
|
17
|
+
|
|
18
|
+
## Quick Start
|
|
19
|
+
|
|
20
|
+
Semantic recall is enabled by default, so if you give your agent memory it will be included:
|
|
21
|
+
|
|
22
|
+
```typescript {9}
|
|
23
|
+
import { Agent } from "@mastra/core/agent";
|
|
24
|
+
import { Memory } from "@mastra/memory";
|
|
25
|
+
import { openai } from "@ai-sdk/openai";
|
|
26
|
+
|
|
27
|
+
const agent = new Agent({
|
|
28
|
+
name: "SupportAgent",
|
|
29
|
+
instructions: "You are a helpful support agent.",
|
|
30
|
+
model: openai("gpt-4o"),
|
|
31
|
+
memory: new Memory(),
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Recall configuration
|
|
36
|
+
|
|
37
|
+
The two main parameters that control semantic recall behavior are:
|
|
38
|
+
|
|
39
|
+
1. **topK**: How many semantically similar messages to retrieve
|
|
40
|
+
2. **messageRange**: How much surrounding context to include with each match
|
|
41
|
+
|
|
42
|
+
```typescript {5-6}
|
|
43
|
+
const agent = new Agent({
|
|
44
|
+
memory: new Memory({
|
|
45
|
+
options: {
|
|
46
|
+
semanticRecall: {
|
|
47
|
+
topK: 3, // Retrieve 3 most similar messages
|
|
48
|
+
messageRange: 2, // Include 2 messages before and after each match
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
}),
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Storage configuration
|
|
56
|
+
|
|
57
|
+
Semantic recall relies on a [storage and vector db](/reference/memory/Memory#parameters) to store messages and their embeddings.
|
|
58
|
+
|
|
59
|
+
```ts {8-17}
|
|
60
|
+
import { Memory } from "@mastra/memory";
|
|
61
|
+
import { Agent } from "@mastra/core/agent";
|
|
62
|
+
import { LibSQLStore } from "@mastra/core/storage/libsql";
|
|
63
|
+
import { LibSQLVector } from "@mastra/core/vector/libsql";
|
|
64
|
+
|
|
65
|
+
const agent = new Agent({
|
|
66
|
+
memory: new Memory({
|
|
67
|
+
// this is the default storage db if omitted
|
|
68
|
+
storage: new LibSQLStore({
|
|
69
|
+
config: {
|
|
70
|
+
url: "file:local.db",
|
|
71
|
+
},
|
|
72
|
+
}),
|
|
73
|
+
// this is the default vector db if omitted
|
|
74
|
+
vector: new LibSQLVector({
|
|
75
|
+
connectionUrl: "file:local.db",
|
|
76
|
+
}),
|
|
77
|
+
}),
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**Storage/vector code Examples**:
|
|
82
|
+
|
|
83
|
+
- [LibSQL](/examples/memory/memory-with-libsql)
|
|
84
|
+
- [Postgres](/examples/memory/memory-with-pg)
|
|
85
|
+
- [Upstash](/examples/memory/memory-with-upstash)
|
|
86
|
+
|
|
87
|
+
### Embedder configuration
|
|
88
|
+
|
|
89
|
+
Semantic recall relies an [embedding model](/reference/memory/Memory#embedder) to convert messages into embeddings. By default Mastra uses FastEmbed but you can specify another [embedding model](https://sdk.vercel.ai/docs/ai-sdk-core/embeddings).
|
|
90
|
+
|
|
91
|
+
```ts {7}
|
|
92
|
+
import { Memory } from "@mastra/memory";
|
|
93
|
+
import { Agent } from "@mastra/core/agent";
|
|
94
|
+
import { openai } from "@ai-sdk/openai";
|
|
95
|
+
|
|
96
|
+
const agent = new Agent({
|
|
97
|
+
memory: new Memory({
|
|
98
|
+
embedder: openai.embedding("text-embedding-3-small"),
|
|
99
|
+
}),
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Disabling
|
|
104
|
+
|
|
105
|
+
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.
|
|
106
|
+
|
|
107
|
+
Semantic recall is enabled by default but can be disabled when not needed:
|
|
108
|
+
|
|
109
|
+
```typescript {4}
|
|
110
|
+
const agent = new Agent({
|
|
111
|
+
memory: new Memory({
|
|
112
|
+
options: {
|
|
113
|
+
semanticRecall: false,
|
|
114
|
+
},
|
|
115
|
+
}),
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
You might want to disable semantic recall in scenarios like:
|
|
120
|
+
|
|
121
|
+
- When [conversation history](./getting-started.mdx#conversation-history-last-messages) provide sufficient context for the current conversation.
|
|
122
|
+
- In performance-sensitive applications, like realtime two-way audio, where the added latency of creating embeddings and running vector queries is noticeable.
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import YouTube from "../../../components/youtube";
|
|
2
|
+
|
|
3
|
+
# Continuously Relevant Information
|
|
4
|
+
|
|
5
|
+
While [conversation history](/docs/memory/overview#conversation-history) and [semantic recall](./semantic-recall.mdx) help agents remember conversations, working memory allows them to maintain persistent information about users across interactions within a thread.
|
|
6
|
+
|
|
7
|
+
Think of it as the agent's active thoughts or scratchpad – the key information they keep available about the user or task. It's similar to how a person would naturally remember someone's name, preferences, or important details during a conversation.
|
|
8
|
+
|
|
9
|
+
This is useful for maintaining ongoing state that's always relevant and should always be available to the agent.
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
Here's a minimal example of setting up an agent with working memory:
|
|
14
|
+
|
|
15
|
+
```typescript {12-15}
|
|
16
|
+
import { Agent } from "@mastra/core/agent";
|
|
17
|
+
import { Memory } from "@mastra/memory";
|
|
18
|
+
import { openai } from "@ai-sdk/openai";
|
|
19
|
+
|
|
20
|
+
// Create agent with working memory enabled
|
|
21
|
+
const agent = new Agent({
|
|
22
|
+
name: "PersonalAssistant",
|
|
23
|
+
instructions: "You are a helpful personal assistant.",
|
|
24
|
+
model: openai("gpt-4o"),
|
|
25
|
+
memory: new Memory({
|
|
26
|
+
options: {
|
|
27
|
+
workingMemory: {
|
|
28
|
+
enabled: true,
|
|
29
|
+
use: "tool-call", // Recommended setting
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
}),
|
|
33
|
+
});
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## How it Works
|
|
37
|
+
|
|
38
|
+
Working memory is a block of Markdown text that the agent is able to update over time to store continuously relevant information:
|
|
39
|
+
|
|
40
|
+
<YouTube id="ik-ld_XA96s" />
|
|
41
|
+
|
|
42
|
+
## Custom Templates
|
|
43
|
+
|
|
44
|
+
Templates guide the agent on what information to track and update in working memory. While a default template is used if none is provided, you'll typically want to define a custom template tailored to your agent's specific use case to ensure it remembers the most relevant information.
|
|
45
|
+
|
|
46
|
+
Here's an example of a custom template. In this example the agent will store the users name, location, timezone, etc as soon as the user sends a message containing any of the info:
|
|
47
|
+
|
|
48
|
+
```typescript {5-28}
|
|
49
|
+
const memory = new Memory({
|
|
50
|
+
options: {
|
|
51
|
+
workingMemory: {
|
|
52
|
+
enabled: true,
|
|
53
|
+
template: `
|
|
54
|
+
# User Profile
|
|
55
|
+
|
|
56
|
+
## Personal Info
|
|
57
|
+
|
|
58
|
+
- Name:
|
|
59
|
+
- Location:
|
|
60
|
+
- Timezone:
|
|
61
|
+
|
|
62
|
+
## Preferences
|
|
63
|
+
|
|
64
|
+
- Communication Style: [e.g., Formal, Casual]
|
|
65
|
+
- Project Goal:
|
|
66
|
+
- Key Deadlines:
|
|
67
|
+
- [Deadline 1]: [Date]
|
|
68
|
+
- [Deadline 2]: [Date]
|
|
69
|
+
|
|
70
|
+
## Session State
|
|
71
|
+
|
|
72
|
+
- Last Task Discussed:
|
|
73
|
+
- Open Questions:
|
|
74
|
+
- [Question 1]
|
|
75
|
+
- [Question 2]
|
|
76
|
+
`,
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
If your agent is not properly updating working memory when you expect it to, you can add system instructions on _how_ and _when_ to use this template in your agents `instruction` setting.
|
|
83
|
+
|
|
84
|
+
## Examples
|
|
85
|
+
|
|
86
|
+
- [Streaming working memory](/examples/memory/streaming-working-memory)
|
|
87
|
+
- [Using a working memory template](/examples/memory/streaming-working-memory-advanced)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Logging | Mastra Observability Documentation"
|
|
3
|
+
description: Documentation on effective logging in Mastra, crucial for understanding application behavior and improving AI accuracy.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import Image from "next/image";
|
|
7
|
+
|
|
8
|
+
# Logging
|
|
9
|
+
|
|
10
|
+
In Mastra, logs can detail when certain functions run, what input data they receive, and how they respond.
|
|
11
|
+
|
|
12
|
+
## Basic Setup
|
|
13
|
+
|
|
14
|
+
Here's a minimal example that sets up a **console logger** at the `INFO` level. This will print out informational messages and above (i.e., `DEBUG`, `INFO`, `WARN`, `ERROR`) to the console.
|
|
15
|
+
|
|
16
|
+
```typescript filename="mastra.config.ts" showLineNumbers copy
|
|
17
|
+
import { Mastra } from "@mastra/core";
|
|
18
|
+
import { createLogger } from "@mastra/core/logger";
|
|
19
|
+
|
|
20
|
+
export const mastra = new Mastra({
|
|
21
|
+
// Other Mastra configuration...
|
|
22
|
+
logger: createLogger({
|
|
23
|
+
name: "Mastra",
|
|
24
|
+
level: "info",
|
|
25
|
+
}),
|
|
26
|
+
});
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
In this configuration:
|
|
30
|
+
|
|
31
|
+
- `name: "Mastra"` specifies the name to group logs under.
|
|
32
|
+
- `level: "info"` sets the minimum severity of logs to record.
|
|
33
|
+
|
|
34
|
+
## Configuration
|
|
35
|
+
|
|
36
|
+
- For more details on the options you can pass to `createLogger()`, see the [createLogger reference documentation](/reference/observability/create-logger.mdx).
|
|
37
|
+
- Once you have a `Logger` instance, you can call its methods (e.g., `.info()`, `.warn()`, `.error()`) in the [Logger instance reference documentation](/reference/observability/logger.mdx).
|
|
38
|
+
- If you want to send your logs to an external service for centralized collection, analysis, or storage, you can configure other logger types such as Upstash Redis. Consult the [createLogger reference documentation](/reference/observability/create-logger.mdx) for details on parameters like `url`, `token`, and `key` when using the `UPSTASH` logger type.
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Next.js Tracing | Mastra Observability Documentation"
|
|
3
|
+
description: "Set up OpenTelemetry tracing for Next.js applications"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Next.js Tracing
|
|
7
|
+
|
|
8
|
+
Next.js requires additional configuration to enable OpenTelemetry tracing.
|
|
9
|
+
|
|
10
|
+
### Step 1: Next.js Configuration
|
|
11
|
+
|
|
12
|
+
Start by enabling the instrumentation hook in your Next.js config:
|
|
13
|
+
|
|
14
|
+
```ts filename="next.config.ts" showLineNumbers copy
|
|
15
|
+
import type { NextConfig } from "next";
|
|
16
|
+
|
|
17
|
+
const nextConfig: NextConfig = {
|
|
18
|
+
experimental: {
|
|
19
|
+
instrumentationHook: true // Not required in Next.js 15+
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export default nextConfig;
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Step 2: Mastra Configuration
|
|
27
|
+
|
|
28
|
+
Configure your Mastra instance:
|
|
29
|
+
|
|
30
|
+
```typescript filename="mastra.config.ts" copy
|
|
31
|
+
import { Mastra } from "@mastra/core";
|
|
32
|
+
|
|
33
|
+
export const mastra = new Mastra({
|
|
34
|
+
// ... other config
|
|
35
|
+
telemetry: {
|
|
36
|
+
serviceName: "your-project-name",
|
|
37
|
+
enabled: true
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Step 3: Configure your providers
|
|
43
|
+
|
|
44
|
+
If you're using Next.js, you have two options for setting up OpenTelemetry instrumentation:
|
|
45
|
+
|
|
46
|
+
#### Option 1: Using a Custom Exporter
|
|
47
|
+
|
|
48
|
+
The default that will work across providers is to configure a custom exporter:
|
|
49
|
+
|
|
50
|
+
1. Install the required dependencies (example using Langfuse):
|
|
51
|
+
|
|
52
|
+
```bash copy
|
|
53
|
+
npm install @opentelemetry/api langfuse-vercel
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
2. Create an instrumentation file:
|
|
57
|
+
|
|
58
|
+
```ts filename="instrumentation.ts" copy
|
|
59
|
+
import {
|
|
60
|
+
NodeSDK,
|
|
61
|
+
ATTR_SERVICE_NAME,
|
|
62
|
+
Resource,
|
|
63
|
+
} from '@mastra/core/telemetry/otel-vendor';
|
|
64
|
+
import { LangfuseExporter } from 'langfuse-vercel';
|
|
65
|
+
|
|
66
|
+
export function register() {
|
|
67
|
+
const exporter = new LangfuseExporter({
|
|
68
|
+
// ... Langfuse config
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
const sdk = new NodeSDK({
|
|
72
|
+
resource: new Resource({
|
|
73
|
+
[ATTR_SERVICE_NAME]: 'ai',
|
|
74
|
+
}),
|
|
75
|
+
traceExporter: exporter,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
sdk.start();
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
#### Option 2: Using Vercel's Otel Setup
|
|
83
|
+
|
|
84
|
+
If you're deploying to Vercel, you can use their OpenTelemetry setup:
|
|
85
|
+
|
|
86
|
+
1. Install the required dependencies:
|
|
87
|
+
|
|
88
|
+
```bash copy
|
|
89
|
+
npm install @opentelemetry/api @vercel/otel
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
2. Create an instrumentation file at the root of your project (or in the src folder if using one):
|
|
93
|
+
|
|
94
|
+
```ts filename="instrumentation.ts" copy
|
|
95
|
+
import { registerOTel } from '@vercel/otel'
|
|
96
|
+
|
|
97
|
+
export function register() {
|
|
98
|
+
registerOTel({ serviceName: 'your-project-name' })
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Summary
|
|
103
|
+
|
|
104
|
+
This setup will enable OpenTelemetry tracing for your Next.js application and Mastra operations.
|
|
105
|
+
|
|
106
|
+
For more details, see the documentation for:
|
|
107
|
+
- [Next.js Instrumentation](https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation)
|
|
108
|
+
- [Vercel OpenTelemetry](https://vercel.com/docs/observability/otel-overview/quickstart)
|