@mastra/mcp-docs-server 0.0.1-alpha.1
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%2Fclient-js.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fcomposio.md +302 -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 +11 -0
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +302 -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%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%2Fstabilityai.md +302 -0
- package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +59 -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-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 +24 -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 +12 -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.md +385 -0
- package/.docs/organized/code-examples/ai-sdk-useChat.md +377 -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/crypto-chatbot.md +96 -0
- package/.docs/organized/code-examples/fireworks-r1.md +159 -0
- package/.docs/organized/code-examples/integrations.md +184 -0
- package/.docs/organized/code-examples/mcp-configuration.md +341 -0
- package/.docs/organized/code-examples/memory-todo-agent.md +161 -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-pg.md +224 -0
- package/.docs/organized/code-examples/memory-with-upstash.md +268 -0
- package/.docs/organized/code-examples/quick-start.md +127 -0
- package/.docs/organized/code-examples/stock-price-tool.md +124 -0
- package/.docs/organized/code-examples/weather-agent.md +352 -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/00-overview.mdx +185 -0
- package/.docs/raw/agents/01-agent-memory.mdx +610 -0
- package/.docs/raw/agents/02-adding-tools.mdx +224 -0
- package/.docs/raw/agents/03-adding-voice.mdx +170 -0
- package/.docs/raw/deployment/deployment.mdx +156 -0
- package/.docs/raw/deployment/logging-and-tracing.mdx +242 -0
- package/.docs/raw/deployment/server.mdx +114 -0
- package/.docs/raw/evals/00-overview.mdx +106 -0
- package/.docs/raw/evals/01-supported-evals.mdx +31 -0
- package/.docs/raw/evals/02-custom-eval.mdx +187 -0
- package/.docs/raw/faq/index.mdx +63 -0
- package/.docs/raw/frameworks/01-next-js.mdx +238 -0
- package/.docs/raw/frameworks/02-ai-sdk.mdx +218 -0
- package/.docs/raw/getting-started/installation.mdx +436 -0
- package/.docs/raw/getting-started/project-structure.mdx +80 -0
- package/.docs/raw/guides/01-chef-michel.mdx +242 -0
- package/.docs/raw/guides/02-stock-agent.mdx +182 -0
- package/.docs/raw/guides/03-recruiter.mdx +187 -0
- package/.docs/raw/index.mdx +22 -0
- package/.docs/raw/local-dev/creating-projects.mdx +74 -0
- package/.docs/raw/local-dev/integrations.mdx +127 -0
- package/.docs/raw/local-dev/mastra-dev.mdx +65 -0
- package/.docs/raw/rag/chunking-and-embedding.mdx +128 -0
- package/.docs/raw/rag/overview.mdx +85 -0
- package/.docs/raw/rag/retrieval.mdx +362 -0
- package/.docs/raw/rag/vector-databases.mdx +271 -0
- package/.docs/raw/reference/agents/createTool.mdx +190 -0
- package/.docs/raw/reference/agents/generate.mdx +327 -0
- package/.docs/raw/reference/agents/getAgent.mdx +54 -0
- package/.docs/raw/reference/agents/stream.mdx +361 -0
- package/.docs/raw/reference/cli/build.mdx +48 -0
- package/.docs/raw/reference/cli/deploy.mdx +22 -0
- package/.docs/raw/reference/cli/dev.mdx +97 -0
- package/.docs/raw/reference/cli/init.mdx +43 -0
- package/.docs/raw/reference/client-js/agents.mdx +90 -0
- package/.docs/raw/reference/client-js/error-handling.mdx +38 -0
- package/.docs/raw/reference/client-js/index.mdx +127 -0
- package/.docs/raw/reference/client-js/logs.mdx +24 -0
- package/.docs/raw/reference/client-js/memory.mdx +94 -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 +137 -0
- package/.docs/raw/reference/core/mastra-class.mdx +232 -0
- package/.docs/raw/reference/deployer/cloudflare.mdx +176 -0
- package/.docs/raw/reference/deployer/deployer.mdx +159 -0
- package/.docs/raw/reference/deployer/netlify.mdx +88 -0
- package/.docs/raw/reference/deployer/vercel.mdx +97 -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 +8 -0
- package/.docs/raw/reference/memory/Memory.mdx +186 -0
- package/.docs/raw/reference/memory/createThread.mdx +93 -0
- package/.docs/raw/reference/memory/getThreadById.mdx +43 -0
- package/.docs/raw/reference/memory/getThreadsByResourceId.mdx +45 -0
- package/.docs/raw/reference/memory/query.mdx +164 -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/index.mdx +15 -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 +46 -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 +237 -0
- package/.docs/raw/reference/rag/document.mdx +129 -0
- package/.docs/raw/reference/rag/embeddings.mdx +160 -0
- package/.docs/raw/reference/rag/extract-params.mdx +72 -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 +249 -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 +198 -0
- package/.docs/raw/reference/rag/vectorize.mdx +253 -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 +180 -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/speechify.mdx +145 -0
- package/.docs/raw/reference/workflows/after.mdx +88 -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/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/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/suspend.mdx +80 -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/workflows/00-overview.mdx +168 -0
- package/.docs/raw/workflows/control-flow.mdx +712 -0
- package/.docs/raw/workflows/dynamic-workflows.mdx +232 -0
- package/.docs/raw/workflows/steps.mdx +98 -0
- package/.docs/raw/workflows/suspend-and-resume.mdx +196 -0
- package/.docs/raw/workflows/variables.mdx +248 -0
- package/LICENSE +44 -0
- package/README.md +129 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +19 -0
- package/dist/prepare-docs/code-examples.d.ts +4 -0
- package/dist/prepare-docs/code-examples.js +91 -0
- package/dist/prepare-docs/copy-raw.d.ts +1 -0
- package/dist/prepare-docs/copy-raw.js +41 -0
- package/dist/prepare-docs/index.d.ts +1 -0
- package/dist/prepare-docs/index.js +8 -0
- package/dist/prepare-docs/package-changes.d.ts +4 -0
- package/dist/prepare-docs/package-changes.js +92 -0
- package/dist/prepare-docs/prepare.d.ts +1 -0
- package/dist/prepare-docs/prepare.js +13 -0
- package/dist/sse.d.ts +1 -0
- package/dist/sse.js +9 -0
- package/dist/stdio.d.ts +1 -0
- package/dist/stdio.js +8 -0
- package/dist/tools/__tests__/blog.test.d.ts +1 -0
- package/dist/tools/__tests__/blog.test.js +48 -0
- package/dist/tools/__tests__/changes.test.d.ts +1 -0
- package/dist/tools/__tests__/changes.test.js +36 -0
- package/dist/tools/__tests__/docs.test.d.ts +1 -0
- package/dist/tools/__tests__/docs.test.js +46 -0
- package/dist/tools/__tests__/examples.test.d.ts +1 -0
- package/dist/tools/__tests__/examples.test.js +52 -0
- package/dist/tools/blog.d.ts +15 -0
- package/dist/tools/blog.js +73 -0
- package/dist/tools/changes.d.ts +11 -0
- package/dist/tools/changes.js +69 -0
- package/dist/tools/docs.d.ts +11 -0
- package/dist/tools/docs.js +176 -0
- package/dist/tools/examples.d.ts +11 -0
- package/dist/tools/examples.js +61 -0
- package/dist/utils.d.ts +6 -0
- package/dist/utils.js +9 -0
- package/package.json +66 -0
|
@@ -0,0 +1,224 @@
|
|
|
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
|
+
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.
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { Agent } from "@mastra/core/agent";
|
|
79
|
+
import { createTool } from "@mastra/core/tools";
|
|
80
|
+
import { z } from "zod";
|
|
81
|
+
|
|
82
|
+
const agent = new Agent({
|
|
83
|
+
name: "Weather agent",
|
|
84
|
+
tools: {
|
|
85
|
+
weather: createTool({
|
|
86
|
+
id: "Get Weather Information",
|
|
87
|
+
description: 'Get the weather in a location',
|
|
88
|
+
inputSchema: z.object({ location: z.string() }),
|
|
89
|
+
execute: async ({ context: { location } }, { abortSignal }) => {
|
|
90
|
+
return fetch(
|
|
91
|
+
`https://api.weatherapi.com/v1/current.json?q=${location}`,
|
|
92
|
+
{ signal: abortSignal }, // forward the abort signal to fetch
|
|
93
|
+
);
|
|
94
|
+
},
|
|
95
|
+
}),
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
const result = await agent.generate('What is the weather in San Francisco?', {
|
|
100
|
+
abortSignal: myAbortSignal, // signal that will be forwarded to tools
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Debugging Tools
|
|
105
|
+
|
|
106
|
+
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.
|
|
107
|
+
|
|
108
|
+
## Calling an Agent with a Tool
|
|
109
|
+
|
|
110
|
+
Now we can call the agent, and it will use the tool to fetch the weather information.
|
|
111
|
+
|
|
112
|
+
## Example: Interacting with the Agent
|
|
113
|
+
|
|
114
|
+
```typescript filename="src/index.ts"
|
|
115
|
+
import { mastra } from "./index";
|
|
116
|
+
|
|
117
|
+
async function main() {
|
|
118
|
+
const agent = mastra.getAgent("weatherAgent");
|
|
119
|
+
const response = await agent.generate(
|
|
120
|
+
"What's the weather like in New York City today?",
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
console.log(response.text);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
main();
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
The agent will use the `weatherInfo` tool to get the current weather in New York City and respond accordingly.
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
## Vercel AI SDK Tool Format
|
|
133
|
+
|
|
134
|
+
Mastra supports tools created using the Vercel AI SDK format. You can import and use these tools directly:
|
|
135
|
+
|
|
136
|
+
```typescript filename="src/mastra/tools/vercelTool.ts" copy
|
|
137
|
+
import { tool } from 'ai';
|
|
138
|
+
import { z } from 'zod';
|
|
139
|
+
|
|
140
|
+
export const weatherInfo = tool({
|
|
141
|
+
description: "Fetches the current weather information for a given city",
|
|
142
|
+
parameters: z.object({
|
|
143
|
+
city: z.string().describe("The city to get weather for")
|
|
144
|
+
}),
|
|
145
|
+
execute: async ({ city }) => {
|
|
146
|
+
// Replace with actual API call
|
|
147
|
+
const data = await fetch(`https://api.example.com/weather?city=${city}`);
|
|
148
|
+
return data.json();
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
You can use Vercel tools alongside Mastra tools in your agents:
|
|
154
|
+
|
|
155
|
+
```typescript filename="src/mastra/agents/weatherAgent.ts"
|
|
156
|
+
import { Agent } from "@mastra/core/agent";
|
|
157
|
+
import { openai } from "@ai-sdk/openai";
|
|
158
|
+
import { weatherInfo } from "../tools/vercelTool";
|
|
159
|
+
import * as mastraTools from "../tools/mastraTools";
|
|
160
|
+
|
|
161
|
+
export const weatherAgent = new Agent({
|
|
162
|
+
name: "Weather Agent",
|
|
163
|
+
instructions: "You are a helpful assistant that provides weather information.",
|
|
164
|
+
model: openai("gpt-4"),
|
|
165
|
+
tools: {
|
|
166
|
+
weatherInfo, // Vercel tool
|
|
167
|
+
...mastraTools // Mastra tools
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Both tool formats will work seamlessly within your agent's workflow.
|
|
173
|
+
|
|
174
|
+
## Tool Design Best Practices
|
|
175
|
+
|
|
176
|
+
When creating tools for your agents, following these guidelines will help ensure reliable and intuitive tool usage:
|
|
177
|
+
|
|
178
|
+
### Tool Descriptions
|
|
179
|
+
Your tool's main description should focus on its purpose and value:
|
|
180
|
+
- Keep descriptions simple and focused on __what__ the tool does
|
|
181
|
+
- Emphasize the tool's primary use case
|
|
182
|
+
- Avoid implementation details in the main description
|
|
183
|
+
- Focus on helping the agent understand __when__ to use the tool
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
createTool({
|
|
187
|
+
id: "documentSearch",
|
|
188
|
+
description: "Access the knowledge base to find information needed to answer user questions",
|
|
189
|
+
// ... rest of tool configuration
|
|
190
|
+
});
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Parameter Schemas
|
|
194
|
+
Technical details belong in the parameter schemas, where they help the agent use the tool correctly:
|
|
195
|
+
- Make parameters self-documenting with clear descriptions
|
|
196
|
+
- Include default values and their implications
|
|
197
|
+
- Provide examples where helpful
|
|
198
|
+
- Describe the impact of different parameter choices
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
inputSchema: z.object({
|
|
202
|
+
query: z.string().describe("The search query to find relevant information"),
|
|
203
|
+
limit: z.number().describe(
|
|
204
|
+
"Number of results to return. Higher values provide more context, lower values focus on best matches"
|
|
205
|
+
),
|
|
206
|
+
options: z.string().describe(
|
|
207
|
+
"Optional configuration. Example: '{'filter': 'category=news'}'"
|
|
208
|
+
),
|
|
209
|
+
}),
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Agent Interaction Patterns
|
|
213
|
+
Tools are more likely to be used effectively when:
|
|
214
|
+
- Queries or tasks are complex enough to clearly require tool assistance
|
|
215
|
+
- Agent instructions provide clear guidance on tool usage
|
|
216
|
+
- Parameter requirements are well-documented in the schema
|
|
217
|
+
- The tool's purpose aligns with the query's needs
|
|
218
|
+
|
|
219
|
+
### Common Pitfalls
|
|
220
|
+
- Overloading the main description with technical details
|
|
221
|
+
- Mixing implementation details with usage guidance
|
|
222
|
+
- Unclear parameter descriptions or missing examples
|
|
223
|
+
|
|
224
|
+
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).
|
|
@@ -0,0 +1,170 @@
|
|
|
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.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.listen(audioStream, { filetype: "m4a" });
|
|
36
|
+
} catch (error) {
|
|
37
|
+
console.error("Error transcribing audio:", error);
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Using Multiple Providers
|
|
42
|
+
|
|
43
|
+
For more flexibility, you can use different providers for speaking and listening using the CompositeVoice class:
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { Agent } from "@mastra/core/agent";
|
|
47
|
+
import { CompositeVoice } from "@mastra/core/voice";
|
|
48
|
+
import { OpenAIVoice } from "@mastra/voice-openai";
|
|
49
|
+
import { PlayAIVoice } from "@mastra/voice-playai";
|
|
50
|
+
import { openai } from "@ai-sdk/openai";
|
|
51
|
+
|
|
52
|
+
export const agent = new Agent({
|
|
53
|
+
name: "Agent",
|
|
54
|
+
instructions: `You are a helpful assistant with both STT and TTS capabilities.`,
|
|
55
|
+
model: openai("gpt-4o"),
|
|
56
|
+
|
|
57
|
+
// Create a composite voice using OpenAI for listening and PlayAI for speaking
|
|
58
|
+
voice: new CompositeVoice({
|
|
59
|
+
listenProvider: new OpenAIVoice(),
|
|
60
|
+
speakProvider: new PlayAIVoice(),
|
|
61
|
+
}),
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Working with Audio Streams
|
|
66
|
+
|
|
67
|
+
The `speak()` and `listen()` methods work with Node.js streams. Here's how to save and load audio files:
|
|
68
|
+
|
|
69
|
+
### Saving Speech Output
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
import { createWriteStream } from "fs";
|
|
73
|
+
import path from "path";
|
|
74
|
+
|
|
75
|
+
// Generate speech and save to file
|
|
76
|
+
const audio = await agent.speak("Hello, World!");
|
|
77
|
+
const filePath = path.join(process.cwd(), "agent.mp3");
|
|
78
|
+
const writer = createWriteStream(filePath);
|
|
79
|
+
|
|
80
|
+
audio.pipe(writer);
|
|
81
|
+
|
|
82
|
+
await new Promise<void>((resolve, reject) => {
|
|
83
|
+
writer.on("finish", () => resolve());
|
|
84
|
+
writer.on("error", reject);
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Transcribing Audio Input
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
import { createReadStream } from "fs";
|
|
92
|
+
import path from "path";
|
|
93
|
+
|
|
94
|
+
// Read audio file and transcribe
|
|
95
|
+
const audioFilePath = path.join(process.cwd(), "/agent.m4a");
|
|
96
|
+
const audioStream = createReadStream(audioFilePath);
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
console.log("Transcribing audio file...");
|
|
100
|
+
const transcription = await agent.listen(audioStream, { filetype: "m4a" });
|
|
101
|
+
console.log("Transcription:", transcription);
|
|
102
|
+
} catch (error) {
|
|
103
|
+
console.error("Error transcribing audio:", error);
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Real-time Voice Interactions
|
|
108
|
+
|
|
109
|
+
For more dynamic and interactive voice experiences, you can use real-time voice providers that support speech-to-speech capabilities:
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
import { Agent } from "@mastra/core/agent";
|
|
113
|
+
import { OpenAIRealtimeVoice } from "@mastra/voice-openai-realtime";
|
|
114
|
+
import { search, calculate } from "../tools";
|
|
115
|
+
|
|
116
|
+
// Initialize the realtime voice provider
|
|
117
|
+
const voice = new OpenAIRealtimeVoice({
|
|
118
|
+
chatModel: {
|
|
119
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
120
|
+
model: 'gpt-4o-mini-realtime',
|
|
121
|
+
},
|
|
122
|
+
speaker: 'alloy'
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// Create an agent with speech-to-speech voice capabilities
|
|
126
|
+
export const agent = new Agent({
|
|
127
|
+
name: 'Agent',
|
|
128
|
+
instructions: `You are a helpful assistant with speech-to-speech capabilities.`,
|
|
129
|
+
model: openai('gpt-4o'),
|
|
130
|
+
tools: { // Tools configured on Agent are passed to voice provider
|
|
131
|
+
search,
|
|
132
|
+
calculate
|
|
133
|
+
},
|
|
134
|
+
voice
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// Establish a WebSocket connection
|
|
138
|
+
await voice.connect();
|
|
139
|
+
|
|
140
|
+
// Start a conversation
|
|
141
|
+
await voice.speak("Hello, I'm your AI assistant!");
|
|
142
|
+
|
|
143
|
+
// Stream audio from a microphone
|
|
144
|
+
const microphoneStream = getMicrophoneStream();
|
|
145
|
+
await voice.send(microphoneStream);
|
|
146
|
+
|
|
147
|
+
// When done with the conversation
|
|
148
|
+
voice.close();
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Event System
|
|
152
|
+
|
|
153
|
+
The realtime voice provider emits several events you can listen for:
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
// Listen for speech audio data sent from voice provider
|
|
157
|
+
voice.on('speaking', ({ audio }) => {
|
|
158
|
+
// audio contains ReadableStream or Int16Array audio data
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// Listen for transcribed text sent from both voice provider and user
|
|
162
|
+
voice.on('writing', ({ text, role }) => {
|
|
163
|
+
console.log(`${role} said: ${text}`);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// Listen for errors
|
|
167
|
+
voice.on('error', (error) => {
|
|
168
|
+
console.error('Voice error:', error);
|
|
169
|
+
});
|
|
170
|
+
```
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Deployment"
|
|
3
|
+
description: "Build and deploy Mastra applications using platform-specific deployers or standard HTTP servers"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Deploying Mastra Applications
|
|
7
|
+
|
|
8
|
+
Mastra applications can be deployed in two ways:
|
|
9
|
+
|
|
10
|
+
1. **Direct Platform Deployment**: Using platform-specific deployers for Cloudflare Workers, Vercel, or Netlify
|
|
11
|
+
2. **Universal Deployment**: Using `mastra build` to generate a standard Node.js server that can run anywhere
|
|
12
|
+
|
|
13
|
+
## Prerequisites
|
|
14
|
+
|
|
15
|
+
Before you begin, ensure you have:
|
|
16
|
+
|
|
17
|
+
- **Node.js** installed (version 18 or higher is recommended)
|
|
18
|
+
- If using a platform-specific deployer:
|
|
19
|
+
- An account with your chosen platform
|
|
20
|
+
- Required API keys or credentials
|
|
21
|
+
|
|
22
|
+
## Direct Platform Deployment
|
|
23
|
+
|
|
24
|
+
Platform-specific deployers handle configuration and deployment for:
|
|
25
|
+
- **[Cloudflare Workers](/docs/reference/deployer/cloudflare)**
|
|
26
|
+
- **[Vercel](/docs/reference/deployer/vercel)**
|
|
27
|
+
- **[Netlify](/docs/reference/deployer/netlify)**
|
|
28
|
+
|
|
29
|
+
### Installing Deployers
|
|
30
|
+
|
|
31
|
+
```bash copy
|
|
32
|
+
# For Cloudflare
|
|
33
|
+
npm install @mastra/deployer-cloudflare
|
|
34
|
+
|
|
35
|
+
# For Vercel
|
|
36
|
+
npm install @mastra/deployer-vercel
|
|
37
|
+
|
|
38
|
+
# For Netlify
|
|
39
|
+
npm install @mastra/deployer-netlify
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Configuring Deployers
|
|
43
|
+
|
|
44
|
+
Configure the deployer in your entry file:
|
|
45
|
+
|
|
46
|
+
```typescript copy showLineNumbers
|
|
47
|
+
import { Mastra, createLogger } from '@mastra/core';
|
|
48
|
+
import { CloudflareDeployer } from '@mastra/deployer-cloudflare';
|
|
49
|
+
|
|
50
|
+
export const mastra = new Mastra({
|
|
51
|
+
agents: { /* your agents here */ },
|
|
52
|
+
logger: createLogger({ name: 'MyApp', level: 'debug' }),
|
|
53
|
+
deployer: new CloudflareDeployer({
|
|
54
|
+
scope: 'your-cloudflare-scope',
|
|
55
|
+
projectName: 'your-project-name',
|
|
56
|
+
// See complete configuration options in the reference docs
|
|
57
|
+
}),
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Deployer Configuration
|
|
62
|
+
|
|
63
|
+
Each deployer has specific configuration options. Below are basic examples, but refer to the reference documentation for complete details.
|
|
64
|
+
|
|
65
|
+
#### Cloudflare Deployer
|
|
66
|
+
|
|
67
|
+
```typescript copy showLineNumbers
|
|
68
|
+
new CloudflareDeployer({
|
|
69
|
+
scope: 'your-cloudflare-account-id',
|
|
70
|
+
projectName: 'your-project-name',
|
|
71
|
+
// For complete configuration options, see the reference documentation
|
|
72
|
+
})
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
[View Cloudflare Deployer Reference →](/docs/reference/deployer/cloudflare)
|
|
76
|
+
|
|
77
|
+
#### Vercel Deployer
|
|
78
|
+
|
|
79
|
+
```typescript copy showLineNumbers
|
|
80
|
+
new VercelDeployer({
|
|
81
|
+
teamId: 'your-vercel-team-id',
|
|
82
|
+
projectName: 'your-project-name',
|
|
83
|
+
token: 'your-vercel-token'
|
|
84
|
+
// For complete configuration options, see the reference documentation
|
|
85
|
+
})
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
[View Vercel Deployer Reference →](/docs/reference/deployer/vercel)
|
|
89
|
+
|
|
90
|
+
#### Netlify Deployer
|
|
91
|
+
|
|
92
|
+
```typescript copy showLineNumbers
|
|
93
|
+
new NetlifyDeployer({
|
|
94
|
+
scope: 'your-netlify-team-slug',
|
|
95
|
+
projectName: 'your-project-name',
|
|
96
|
+
token: 'your-netlify-token'
|
|
97
|
+
})
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
[View Netlify Deployer Reference →](/docs/reference/deployer/netlify)
|
|
101
|
+
|
|
102
|
+
## Universal Deployment
|
|
103
|
+
|
|
104
|
+
Since Mastra builds to a standard Node.js server, you can deploy to any platform that runs Node.js applications:
|
|
105
|
+
- Cloud VMs (AWS EC2, DigitalOcean Droplets, GCP Compute Engine)
|
|
106
|
+
- Container platforms (Docker, Kubernetes)
|
|
107
|
+
- Platform as a Service (Heroku, Railway)
|
|
108
|
+
- Self-hosted servers
|
|
109
|
+
|
|
110
|
+
### Building
|
|
111
|
+
|
|
112
|
+
Build the application:
|
|
113
|
+
|
|
114
|
+
```bash copy
|
|
115
|
+
# Build from current directory
|
|
116
|
+
mastra build
|
|
117
|
+
|
|
118
|
+
# Or specify a directory
|
|
119
|
+
mastra build --dir ./my-project
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
The build process:
|
|
123
|
+
1. Locates entry file (`src/mastra/index.ts` or `src/mastra/index.js`)
|
|
124
|
+
2. Creates `.mastra` output directory
|
|
125
|
+
3. Bundles code using Rollup with tree shaking and source maps
|
|
126
|
+
4. Generates [Hono](https://hono.dev) HTTP server
|
|
127
|
+
|
|
128
|
+
See [`mastra build`](/docs/reference/cli/build) for all options.
|
|
129
|
+
|
|
130
|
+
### Running the Server
|
|
131
|
+
|
|
132
|
+
Start the HTTP server:
|
|
133
|
+
|
|
134
|
+
```bash copy
|
|
135
|
+
node .mastra/index.js
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Environment Variables
|
|
139
|
+
|
|
140
|
+
Required variables:
|
|
141
|
+
|
|
142
|
+
1. Platform deployer variables (if using platform deployers):
|
|
143
|
+
- Platform credentials
|
|
144
|
+
2. Agent API keys:
|
|
145
|
+
- `OPENAI_API_KEY`
|
|
146
|
+
- `ANTHROPIC_API_KEY`
|
|
147
|
+
3. Server configuration (for universal deployment):
|
|
148
|
+
- `PORT`: HTTP server port (default: 3000)
|
|
149
|
+
- `HOST`: Server host (default: 0.0.0.0)
|
|
150
|
+
|
|
151
|
+
## Platform Documentation
|
|
152
|
+
|
|
153
|
+
Platform deployment references:
|
|
154
|
+
- [Cloudflare Workers](https://developers.cloudflare.com/workers/)
|
|
155
|
+
- [Vercel](https://vercel.com/docs)
|
|
156
|
+
- [Netlify](https://docs.netlify.com/)
|