@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,317 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Agent Tool Selection | Agent Documentation | Mastra"
|
|
3
|
+
description: Tools are typed functions that can be executed by agents or workflows, with built-in integration access and parameter validation. Each tool has a schema that defines its inputs, an executor function that implements its logic, and access to configured integrations.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Agent Tool Selection
|
|
7
|
+
|
|
8
|
+
Tools are typed functions that can be executed by agents or workflows, with built-in integration access and parameter validation. Each tool has a schema that defines its inputs, an executor function that implements its logic, and access to configured integrations.
|
|
9
|
+
|
|
10
|
+
## Creating Tools
|
|
11
|
+
|
|
12
|
+
In this section, we'll walk through the process of creating a tool that can be used by your agents. Let's create a simple tool that fetches current weather information for a given city.
|
|
13
|
+
|
|
14
|
+
```typescript filename="src/mastra/tools/weatherInfo.ts" copy
|
|
15
|
+
import { createTool } from "@mastra/core/tools";
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
|
|
18
|
+
const getWeatherInfo = async (city: string) => {
|
|
19
|
+
// Replace with an actual API call to a weather service
|
|
20
|
+
const data = await fetch(`https://api.example.com/weather?city=${city}`).then(
|
|
21
|
+
(r) => r.json(),
|
|
22
|
+
);
|
|
23
|
+
return data;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const weatherInfo = createTool({
|
|
27
|
+
id: "Get Weather Information",
|
|
28
|
+
inputSchema: z.object({
|
|
29
|
+
city: z.string(),
|
|
30
|
+
}),
|
|
31
|
+
description: `Fetches the current weather information for a given city`,
|
|
32
|
+
execute: async ({ context: { city } }) => {
|
|
33
|
+
console.log("Using tool to fetch weather information for", city);
|
|
34
|
+
return await getWeatherInfo(city);
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Adding Tools to an Agent
|
|
40
|
+
|
|
41
|
+
Now we'll add the tool to an agent. We'll create an agent that can answer questions about the weather and configure it to use our `weatherInfo` tool.
|
|
42
|
+
|
|
43
|
+
```typescript filename="src/mastra/agents/weatherAgent.ts"
|
|
44
|
+
import { Agent } from "@mastra/core/agent";
|
|
45
|
+
import { openai } from "@ai-sdk/openai";
|
|
46
|
+
import * as tools from "../tools/weatherInfo";
|
|
47
|
+
|
|
48
|
+
export const weatherAgent = new Agent<typeof tools>({
|
|
49
|
+
name: "Weather Agent",
|
|
50
|
+
instructions:
|
|
51
|
+
"You are a helpful assistant that provides current weather information. When asked about the weather, use the weather information tool to fetch the data.",
|
|
52
|
+
model: openai("gpt-4o-mini"),
|
|
53
|
+
tools: {
|
|
54
|
+
weatherInfo: tools.weatherInfo,
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Registering the Agent
|
|
60
|
+
|
|
61
|
+
We need to initialize Mastra with our agent.
|
|
62
|
+
|
|
63
|
+
```typescript filename="src/index.ts"
|
|
64
|
+
import { Mastra } from "@mastra/core";
|
|
65
|
+
import { weatherAgent } from "./agents/weatherAgent";
|
|
66
|
+
|
|
67
|
+
export const mastra = new Mastra({
|
|
68
|
+
agents: { weatherAgent },
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
This registers your agent with Mastra, making it available for use.
|
|
73
|
+
|
|
74
|
+
## Abort Signals
|
|
75
|
+
|
|
76
|
+
The abort signals from `generate` and `stream` (text generation) are forwarded to the tool execution. You can access them in the second parameter of the execute function and e.g. abort long-running computations or forward them to fetch calls inside tools.
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import { Agent } from "@mastra/core/agent";
|
|
80
|
+
import { createTool } from "@mastra/core/tools";
|
|
81
|
+
import { z } from "zod";
|
|
82
|
+
|
|
83
|
+
const agent = new Agent({
|
|
84
|
+
name: "Weather agent",
|
|
85
|
+
tools: {
|
|
86
|
+
weather: createTool({
|
|
87
|
+
id: "Get Weather Information",
|
|
88
|
+
description: "Get the weather in a location",
|
|
89
|
+
inputSchema: z.object({ location: z.string() }),
|
|
90
|
+
execute: async ({ context: { location } }, { abortSignal }) => {
|
|
91
|
+
return fetch(
|
|
92
|
+
`https://api.weatherapi.com/v1/current.json?q=${location}`,
|
|
93
|
+
{ signal: abortSignal }, // forward the abort signal to fetch
|
|
94
|
+
);
|
|
95
|
+
},
|
|
96
|
+
}),
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const result = await agent.generate("What is the weather in San Francisco?", {
|
|
101
|
+
abortSignal: myAbortSignal, // signal that will be forwarded to tools
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Injecting Request/User-Specific Variables
|
|
106
|
+
|
|
107
|
+
We support dependency injection for tools and workflows. You can pass a container directly to your `generate` or `stream` function calls, or inject it using [server middleware](/docs/deployment/server#Middleware).
|
|
108
|
+
|
|
109
|
+
Here's an example where we change the temperature scale between Fahrenheit and Celsius:
|
|
110
|
+
|
|
111
|
+
```typescript filename="src/agents/weather-agent.ts"
|
|
112
|
+
import { Agent } from "@mastra/core/agent";
|
|
113
|
+
import { createTool } from "@mastra/core/tools";
|
|
114
|
+
import { z } from "zod";
|
|
115
|
+
|
|
116
|
+
export const agent = new Agent({
|
|
117
|
+
name: "Weather agent",
|
|
118
|
+
tools: {
|
|
119
|
+
weather: createTool({
|
|
120
|
+
id: "Get Weather Information",
|
|
121
|
+
description: "Get the weather in a location",
|
|
122
|
+
inputSchema: z.object({ location: z.string() }),
|
|
123
|
+
execute: async ({ context: { location }, container }) => {
|
|
124
|
+
const scale = container.get("temperature-scale");
|
|
125
|
+
|
|
126
|
+
const result = await fetch(
|
|
127
|
+
`https://api.weatherapi.com/v1/current.json?q=${location}`,
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
const json = await result.json();
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
temperature: scale === "celsius" ? json.temp_c : json.temp_f,
|
|
134
|
+
};
|
|
135
|
+
},
|
|
136
|
+
}),
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
import { agent } from "./agents/weather";
|
|
143
|
+
|
|
144
|
+
type MyContainer = {"temperature-scale", "celsius" | "farenheit"}
|
|
145
|
+
|
|
146
|
+
const container = new Container<MyContainer>();
|
|
147
|
+
container.set("temperature-scale", "celsius");
|
|
148
|
+
|
|
149
|
+
const result = await agent.generate("What is the weather in San Francisco?", {
|
|
150
|
+
container,
|
|
151
|
+
});
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Let's derive temperature from the user's country using cloudflare ` CF-IPCountry` header. We're keeping the same agent code as the example above
|
|
155
|
+
|
|
156
|
+
```typescript filename="src/index.ts"
|
|
157
|
+
import { Mastra } from "@mastra/core";
|
|
158
|
+
import { Container } from "@mastra/core/di";
|
|
159
|
+
import { agent as weatherAgent } from "./agents/weather";
|
|
160
|
+
|
|
161
|
+
type MyContainer = {"temperature-scale", "celsius" | "farenheit"}
|
|
162
|
+
|
|
163
|
+
export const mastra = new Mastra({
|
|
164
|
+
agents: {
|
|
165
|
+
weather: weatherAgent,
|
|
166
|
+
},
|
|
167
|
+
server: {
|
|
168
|
+
middleware: [
|
|
169
|
+
(c, next) => {
|
|
170
|
+
const country = c.req.header("CF-IPCountry");
|
|
171
|
+
const container: MyContainer = c.get("container");
|
|
172
|
+
|
|
173
|
+
container.set(
|
|
174
|
+
"temperature-scale",
|
|
175
|
+
country === "US" ? "farenheit" : "celsius",
|
|
176
|
+
);
|
|
177
|
+
},
|
|
178
|
+
],
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Debugging Tools
|
|
184
|
+
|
|
185
|
+
You can test tools using Vitest or any other testing framework. Writing unit tests for your tools ensures they behave as expected and helps catch errors early.
|
|
186
|
+
|
|
187
|
+
## Calling an Agent with a Tool
|
|
188
|
+
|
|
189
|
+
Now we can call the agent, and it will use the tool to fetch the weather information.
|
|
190
|
+
|
|
191
|
+
## Example: Interacting with the Agent
|
|
192
|
+
|
|
193
|
+
```typescript filename="src/index.ts"
|
|
194
|
+
import { mastra } from "./index";
|
|
195
|
+
|
|
196
|
+
async function main() {
|
|
197
|
+
const agent = mastra.getAgent("weatherAgent");
|
|
198
|
+
const response = await agent.generate(
|
|
199
|
+
"What's the weather like in New York City today?",
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
console.log(response.text);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
main();
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
The agent will use the `weatherInfo` tool to get the current weather in New York City and respond accordingly.
|
|
209
|
+
|
|
210
|
+
## Vercel AI SDK Tool Format
|
|
211
|
+
|
|
212
|
+
Mastra supports tools created using the Vercel AI SDK format. You can import and use these tools directly:
|
|
213
|
+
|
|
214
|
+
```typescript filename="src/mastra/tools/vercelTool.ts" copy
|
|
215
|
+
import { tool } from "ai";
|
|
216
|
+
import { z } from "zod";
|
|
217
|
+
|
|
218
|
+
export const weatherInfo = tool({
|
|
219
|
+
description: "Fetches the current weather information for a given city",
|
|
220
|
+
parameters: z.object({
|
|
221
|
+
city: z.string().describe("The city to get weather for"),
|
|
222
|
+
}),
|
|
223
|
+
execute: async ({ city }) => {
|
|
224
|
+
// Replace with actual API call
|
|
225
|
+
const data = await fetch(`https://api.example.com/weather?city=${city}`);
|
|
226
|
+
return data.json();
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
You can use Vercel tools alongside Mastra tools in your agents:
|
|
232
|
+
|
|
233
|
+
```typescript filename="src/mastra/agents/weatherAgent.ts"
|
|
234
|
+
import { Agent } from "@mastra/core/agent";
|
|
235
|
+
import { openai } from "@ai-sdk/openai";
|
|
236
|
+
import { weatherInfo } from "../tools/vercelTool";
|
|
237
|
+
import * as mastraTools from "../tools/mastraTools";
|
|
238
|
+
|
|
239
|
+
export const weatherAgent = new Agent({
|
|
240
|
+
name: "Weather Agent",
|
|
241
|
+
instructions:
|
|
242
|
+
"You are a helpful assistant that provides weather information.",
|
|
243
|
+
model: openai("gpt-4"),
|
|
244
|
+
tools: {
|
|
245
|
+
weatherInfo, // Vercel tool
|
|
246
|
+
...mastraTools, // Mastra tools
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
Both tool formats will work seamlessly within your agent's workflow.
|
|
252
|
+
|
|
253
|
+
## Tool Design Best Practices
|
|
254
|
+
|
|
255
|
+
When creating tools for your agents, following these guidelines will help ensure reliable and intuitive tool usage:
|
|
256
|
+
|
|
257
|
+
### Tool Descriptions
|
|
258
|
+
|
|
259
|
+
Your tool's main description should focus on its purpose and value:
|
|
260
|
+
|
|
261
|
+
- Keep descriptions simple and focused on **what** the tool does
|
|
262
|
+
- Emphasize the tool's primary use case
|
|
263
|
+
- Avoid implementation details in the main description
|
|
264
|
+
- Focus on helping the agent understand **when** to use the tool
|
|
265
|
+
|
|
266
|
+
```typescript
|
|
267
|
+
createTool({
|
|
268
|
+
id: "documentSearch",
|
|
269
|
+
description:
|
|
270
|
+
"Access the knowledge base to find information needed to answer user questions",
|
|
271
|
+
// ... rest of tool configuration
|
|
272
|
+
});
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
### Parameter Schemas
|
|
276
|
+
|
|
277
|
+
Technical details belong in the parameter schemas, where they help the agent use the tool correctly:
|
|
278
|
+
|
|
279
|
+
- Make parameters self-documenting with clear descriptions
|
|
280
|
+
- Include default values and their implications
|
|
281
|
+
- Provide examples where helpful
|
|
282
|
+
- Describe the impact of different parameter choices
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
inputSchema: z.object({
|
|
286
|
+
query: z.string().describe("The search query to find relevant information"),
|
|
287
|
+
limit: z.number().describe(
|
|
288
|
+
"Number of results to return. Higher values provide more context, lower values focus on best matches"
|
|
289
|
+
),
|
|
290
|
+
options: z.string().describe(
|
|
291
|
+
"Optional configuration. Example: '{'filter': 'category=news'}'"
|
|
292
|
+
),
|
|
293
|
+
}),
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
### Agent Interaction Patterns
|
|
297
|
+
|
|
298
|
+
Tools are more likely to be used effectively when:
|
|
299
|
+
|
|
300
|
+
- Queries or tasks are complex enough to clearly require tool assistance
|
|
301
|
+
- Agent instructions provide clear guidance on tool usage
|
|
302
|
+
- Parameter requirements are well-documented in the schema
|
|
303
|
+
- The tool's purpose aligns with the query's needs
|
|
304
|
+
|
|
305
|
+
### Common Pitfalls
|
|
306
|
+
|
|
307
|
+
- Overloading the main description with technical details
|
|
308
|
+
- Mixing implementation details with usage guidance
|
|
309
|
+
- Unclear parameter descriptions or missing examples
|
|
310
|
+
|
|
311
|
+
Following these practices helps ensure your tools are discoverable and usable by agents while maintaining clean separation between purpose (main description) and implementation details (parameter schemas).
|
|
312
|
+
|
|
313
|
+
## Model Context Protocol (MCP) Tools
|
|
314
|
+
|
|
315
|
+
Mastra also supports tools from MCP-compatible servers through the `@mastra/mcp` package. MCP provides a standardized way for AI models to discover and interact with external tools and resources. This makes it easy to integrate third-party tools into your agents without writing custom integrations.
|
|
316
|
+
|
|
317
|
+
For detailed information about using MCP tools, including configuration options and best practices, see our [MCP guide](/docs/agents/mcp-guide).
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# Adding Voice to Agents
|
|
2
|
+
|
|
3
|
+
Mastra agents can be enhanced with voice capabilities, allowing them to speak responses and listen to user input. You can configure an agent to use either a single voice provider or combine multiple providers for different operations.
|
|
4
|
+
|
|
5
|
+
## Using a Single Provider
|
|
6
|
+
|
|
7
|
+
The simplest way to add voice to an agent is to use a single provider for both speaking and listening:
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
import { createReadStream } from "fs";
|
|
11
|
+
import path from "path";
|
|
12
|
+
import { Agent } from "@mastra/core/agent";
|
|
13
|
+
import { OpenAIVoice } from "@mastra/voice-openai";
|
|
14
|
+
import { openai } from "@ai-sdk/openai";
|
|
15
|
+
|
|
16
|
+
// Initialize the voice provider with default settings
|
|
17
|
+
const voice = new OpenAIVoice();
|
|
18
|
+
|
|
19
|
+
// Create an agent with voice capabilities
|
|
20
|
+
export const agent = new Agent({
|
|
21
|
+
name: "Agent",
|
|
22
|
+
instructions: `You are a helpful assistant with both STT and TTS capabilities.`,
|
|
23
|
+
model: openai("gpt-4o"),
|
|
24
|
+
voice,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// The agent can now use voice for interaction
|
|
28
|
+
await agent.voice.speak("Hello, I'm your AI assistant!");
|
|
29
|
+
|
|
30
|
+
// Read audio file and transcribe
|
|
31
|
+
const audioFilePath = path.join(process.cwd(), "/audio.m4a");
|
|
32
|
+
const audioStream = createReadStream(audioFilePath);
|
|
33
|
+
|
|
34
|
+
try {
|
|
35
|
+
const transcription = await agent.voice.listen(audioStream, {
|
|
36
|
+
filetype: "m4a",
|
|
37
|
+
});
|
|
38
|
+
} catch (error) {
|
|
39
|
+
console.error("Error transcribing audio:", error);
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Using Multiple Providers
|
|
44
|
+
|
|
45
|
+
For more flexibility, you can use different providers for speaking and listening using the CompositeVoice class:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { Agent } from "@mastra/core/agent";
|
|
49
|
+
import { CompositeVoice } from "@mastra/core/voice";
|
|
50
|
+
import { OpenAIVoice } from "@mastra/voice-openai";
|
|
51
|
+
import { PlayAIVoice } from "@mastra/voice-playai";
|
|
52
|
+
import { openai } from "@ai-sdk/openai";
|
|
53
|
+
|
|
54
|
+
export const agent = new Agent({
|
|
55
|
+
name: "Agent",
|
|
56
|
+
instructions: `You are a helpful assistant with both STT and TTS capabilities.`,
|
|
57
|
+
model: openai("gpt-4o"),
|
|
58
|
+
|
|
59
|
+
// Create a composite voice using OpenAI for listening and PlayAI for speaking
|
|
60
|
+
voice: new CompositeVoice({
|
|
61
|
+
input: new OpenAIVoice(),
|
|
62
|
+
output: new PlayAIVoice(),
|
|
63
|
+
}),
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Working with Audio Streams
|
|
68
|
+
|
|
69
|
+
The `speak()` and `listen()` methods work with Node.js streams. Here's how to save and load audio files:
|
|
70
|
+
|
|
71
|
+
### Saving Speech Output
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
import { createWriteStream } from "fs";
|
|
75
|
+
import path from "path";
|
|
76
|
+
|
|
77
|
+
// Generate speech and save to file
|
|
78
|
+
const audio = await agent.voice.speak("Hello, World!");
|
|
79
|
+
const filePath = path.join(process.cwd(), "agent.mp3");
|
|
80
|
+
const writer = createWriteStream(filePath);
|
|
81
|
+
|
|
82
|
+
audio.pipe(writer);
|
|
83
|
+
|
|
84
|
+
await new Promise<void>((resolve, reject) => {
|
|
85
|
+
writer.on("finish", () => resolve());
|
|
86
|
+
writer.on("error", reject);
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Transcribing Audio Input
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { createReadStream } from "fs";
|
|
94
|
+
import path from "path";
|
|
95
|
+
|
|
96
|
+
// Read audio file and transcribe
|
|
97
|
+
const audioFilePath = path.join(process.cwd(), "/agent.m4a");
|
|
98
|
+
const audioStream = createReadStream(audioFilePath);
|
|
99
|
+
|
|
100
|
+
try {
|
|
101
|
+
console.log("Transcribing audio file...");
|
|
102
|
+
const transcription = await agent.voice.listen(audioStream, {
|
|
103
|
+
filetype: "m4a",
|
|
104
|
+
});
|
|
105
|
+
console.log("Transcription:", transcription);
|
|
106
|
+
} catch (error) {
|
|
107
|
+
console.error("Error transcribing audio:", error);
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Real-time Voice Interactions
|
|
112
|
+
|
|
113
|
+
For more dynamic and interactive voice experiences, you can use real-time voice providers that support speech-to-speech capabilities:
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
import { Agent } from "@mastra/core/agent";
|
|
117
|
+
import { OpenAIRealtimeVoice } from "@mastra/voice-openai-realtime";
|
|
118
|
+
import { search, calculate } from "../tools";
|
|
119
|
+
|
|
120
|
+
// Initialize the realtime voice provider
|
|
121
|
+
const voice = new OpenAIRealtimeVoice({
|
|
122
|
+
chatModel: {
|
|
123
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
124
|
+
model: "gpt-4o-mini-realtime",
|
|
125
|
+
},
|
|
126
|
+
speaker: "alloy",
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// Create an agent with speech-to-speech voice capabilities
|
|
130
|
+
export const agent = new Agent({
|
|
131
|
+
name: "Agent",
|
|
132
|
+
instructions: `You are a helpful assistant with speech-to-speech capabilities.`,
|
|
133
|
+
model: openai("gpt-4o"),
|
|
134
|
+
tools: {
|
|
135
|
+
// Tools configured on Agent are passed to voice provider
|
|
136
|
+
search,
|
|
137
|
+
calculate,
|
|
138
|
+
},
|
|
139
|
+
voice,
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// Establish a WebSocket connection
|
|
143
|
+
await agent.voice.connect();
|
|
144
|
+
|
|
145
|
+
// Start a conversation
|
|
146
|
+
agent.voice.speak("Hello, I'm your AI assistant!");
|
|
147
|
+
|
|
148
|
+
// Stream audio from a microphone
|
|
149
|
+
const microphoneStream = getMicrophoneStream();
|
|
150
|
+
agent.voice.send(microphoneStream);
|
|
151
|
+
|
|
152
|
+
// When done with the conversation
|
|
153
|
+
agent.voice.close();
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Event System
|
|
157
|
+
|
|
158
|
+
The realtime voice provider emits several events you can listen for:
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
// Listen for speech audio data sent from voice provider
|
|
162
|
+
agent.voice.on("speaking", ({ audio }) => {
|
|
163
|
+
// audio contains ReadableStream or Int16Array audio data
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// Listen for transcribed text sent from both voice provider and user
|
|
167
|
+
agent.voice.on("writing", ({ text, role }) => {
|
|
168
|
+
console.log(`${role} said: ${text}`);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
// Listen for errors
|
|
172
|
+
agent.voice.on("error", (error) => {
|
|
173
|
+
console.error("Voice error:", error);
|
|
174
|
+
});
|
|
175
|
+
```
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Using Agent Memory | Agents | Mastra Docs"
|
|
3
|
+
description: Documentation on how agents in Mastra use memory to store conversation history and contextual information.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Agent Memory
|
|
7
|
+
|
|
8
|
+
Agents in Mastra can leverage a powerful memory system to store conversation history, recall relevant information, and maintain persistent context across interactions. This allows agents to have more natural, stateful conversations.
|
|
9
|
+
|
|
10
|
+
## Enabling Memory for an Agent
|
|
11
|
+
|
|
12
|
+
To enable memory, simply instantiate the `Memory` class and pass it to your agent's configuration. You also need to install the memory package:
|
|
13
|
+
|
|
14
|
+
```bash npm2yarn copy
|
|
15
|
+
npm install @mastra/memory
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import { Agent } from "@mastra/core/agent";
|
|
20
|
+
import { Memory } from "@mastra/memory";
|
|
21
|
+
import { openai } from "@ai-sdk/openai";
|
|
22
|
+
|
|
23
|
+
// Basic memory setup
|
|
24
|
+
const memory = new Memory();
|
|
25
|
+
|
|
26
|
+
const agent = new Agent({
|
|
27
|
+
name: "MyMemoryAgent",
|
|
28
|
+
instructions: "You are a helpful assistant with memory.",
|
|
29
|
+
model: openai("gpt-4o"),
|
|
30
|
+
memory: memory, // Attach the memory instance
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
This basic setup uses default settings, including LibSQL for storage and FastEmbed for embeddings. For detailed setup instructions, see [Memory](/docs/memory/overview).
|
|
35
|
+
|
|
36
|
+
## Using Memory in Agent Calls
|
|
37
|
+
|
|
38
|
+
To utilize memory during interactions, you **must** provide `resourceId` and `threadId` when calling the agent's `stream()` or `generate()` methods.
|
|
39
|
+
|
|
40
|
+
- `resourceId`: Typically identifies the user or entity (e.g., `user_123`).
|
|
41
|
+
- `threadId`: Identifies a specific conversation thread (e.g., `support_chat_456`).
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
// Example agent call using memory
|
|
45
|
+
await agent.stream("Remember my favorite color is blue.", {
|
|
46
|
+
resourceId: "user_alice",
|
|
47
|
+
threadId: "preferences_thread",
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// Later in the same thread...
|
|
51
|
+
const response = await agent.stream("What's my favorite color?", {
|
|
52
|
+
resourceId: "user_alice",
|
|
53
|
+
threadId: "preferences_thread",
|
|
54
|
+
});
|
|
55
|
+
// Agent will use memory to recall the favorite color.
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
These IDs ensure that conversation history and context are correctly stored and retrieved for the appropriate user and conversation.
|
|
59
|
+
|
|
60
|
+
## Next Steps
|
|
61
|
+
|
|
62
|
+
Keep exploring Mastra's [memory capabilities](/docs/memory/overview) like threads, conversation history, semantic recall, and working memory.
|