@mastra/mcp-docs-server 1.2.15-alpha.19 → 1.2.15-alpha.21
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/docs/agents/code-mode.md +1 -1
- package/.docs/docs/agents/networks.md +2 -2
- package/.docs/docs/agents/overview.md +2 -2
- package/.docs/docs/agents/using-tools.md +1 -1
- package/.docs/docs/browser/overview.md +8 -8
- package/.docs/docs/browser/recording.md +2 -2
- package/.docs/docs/capabilities/{channels/overview.md → channels.md} +70 -7
- package/.docs/docs/capabilities/subagents.md +2 -2
- package/.docs/docs/deployment/cloud-providers.md +9 -9
- package/.docs/docs/deployment/overview.md +9 -9
- package/.docs/docs/deployment/sandbox.md +3 -3
- package/.docs/docs/deployment/web-framework.md +6 -6
- package/.docs/docs/deployment/workflow-runners.md +2 -2
- package/.docs/docs/harness/agent-controller.md +2 -2
- package/.docs/docs/index.md +8 -8
- package/.docs/docs/long-running-agents/durable-agents.md +2 -2
- package/.docs/docs/long-running-agents/signal-providers.md +2 -15
- package/.docs/docs/long-running-agents/signals.md +1 -1
- package/.docs/docs/mastra-platform/database.md +2 -2
- package/.docs/docs/mastra-platform/deploy.md +27 -0
- package/.docs/docs/mastra-platform/server.md +1 -1
- package/.docs/docs/mastra-platform/trace-intelligence.md +9 -13
- package/.docs/docs/mcp/overview.md +42 -0
- package/.docs/docs/memory/memory-processors.md +5 -5
- package/.docs/docs/memory/message-history.md +1 -1
- package/.docs/docs/memory/observational-memory.md +1 -1
- package/.docs/docs/observability/integrations/exporters/mastra-storage.md +16 -16
- package/.docs/docs/observability/integrations/overview.md +3 -3
- package/.docs/docs/server/auth/fga.md +1 -1
- package/.docs/docs/server/auth/simple-auth.md +1 -1
- package/.docs/docs/server/auth.md +8 -8
- package/.docs/docs/server/custom-api-routes.md +35 -0
- package/.docs/docs/storage/overview.md +14 -13
- package/.docs/docs/studio/observability.md +1 -1
- package/.docs/docs/workflows/overview.md +1 -1
- package/.docs/docs/workflows/snapshots.md +11 -11
- package/.docs/docs/workflows/time-travel.md +2 -0
- package/.docs/docs/workspace/filesystem.md +15 -15
- package/.docs/docs/workspace/sandbox.md +17 -17
- package/.docs/docs/workspace/search.md +1 -1
- package/.docs/guides/agent-frameworks/ai-sdk.md +2 -2
- package/.docs/guides/deployment/mastra-workers.md +1 -1
- package/.docs/guides/getting-started/quickstart.md +3 -3
- package/.docs/guides/index.md +8 -8
- package/.docs/guides/voice/overview.md +55 -106
- package/.docs/guides/voice/speech-to-text.md +7 -7
- package/.docs/guides/voice/text-to-speech.md +9 -10
- package/.docs/{guides/build-your-ui → integrations/agentic-ui}/ai-sdk-ui.md +2 -2
- package/.docs/{guides/build-your-ui/copilotkit/overview.md → integrations/agentic-ui/copilotkit.md} +256 -3
- package/.docs/{docs/browser → integrations/browsers}/agent-browser.md +2 -2
- package/.docs/{docs/browser → integrations/browsers}/browser-viewer.md +3 -3
- package/.docs/{docs/browser → integrations/browsers}/firecrawl.md +2 -2
- package/.docs/{docs/browser → integrations/browsers}/stagehand.md +1 -1
- package/.docs/{docs/capabilities → integrations}/channels/discord.md +2 -2
- package/.docs/integrations/channels/github.md +103 -0
- package/.docs/{docs/capabilities → integrations}/channels/imessage.md +7 -5
- package/.docs/{docs/capabilities → integrations}/channels/slack.md +4 -4
- package/.docs/{docs/capabilities → integrations}/channels/teams.md +2 -2
- package/.docs/{docs/capabilities → integrations}/channels/telegram.md +2 -2
- package/.docs/{docs/capabilities → integrations}/channels/whatsapp.md +2 -2
- package/.docs/{reference/storage/dsql.md → integrations/databases/aurora-dsql.md} +1 -1
- package/.docs/{reference/storage → integrations/databases}/clickhouse.md +2 -2
- package/.docs/{reference/storage → integrations/databases}/cloudflare-d1.md +1 -1
- package/.docs/{reference/storage/cloudflare.md → integrations/databases/cloudflare-kv.md} +1 -1
- package/.docs/{reference/storage → integrations/databases}/convex.md +1 -1
- package/.docs/{reference/storage → integrations/databases}/duckdb.md +5 -5
- package/.docs/{reference/storage → integrations/databases}/dynamodb.md +1 -1
- package/.docs/{reference/storage/lance.md → integrations/databases/lancedb.md} +1 -1
- package/.docs/{reference/storage → integrations/databases}/libsql.md +2 -2
- package/.docs/{reference/storage → integrations/databases}/mongodb.md +1 -1
- package/.docs/{reference/storage → integrations/databases}/mssql.md +1 -1
- package/.docs/integrations/databases/neon.md +220 -0
- package/.docs/{reference/storage → integrations/databases}/oracledb.md +1 -1
- package/.docs/{reference/storage → integrations/databases}/postgresql.md +1 -1
- package/.docs/{reference/storage → integrations/databases}/redis.md +1 -1
- package/.docs/{reference/storage → integrations/databases}/spanner.md +1 -1
- package/.docs/{reference/storage → integrations/databases}/upstash.md +1 -1
- package/.docs/{guides/deployment → integrations/deploy}/aws-bedrock-agentcore.md +1 -1
- package/.docs/{guides/deployment → integrations/deploy}/aws-lambda.md +1 -1
- package/.docs/{guides/deployment → integrations/deploy}/azure-app-services.md +1 -1
- package/.docs/{guides/deployment → integrations/deploy}/cloudflare.md +1 -1
- package/.docs/{guides/deployment → integrations/deploy}/digital-ocean.md +1 -1
- package/.docs/{guides/deployment → integrations/deploy}/netlify.md +1 -1
- package/.docs/{guides/deployment → integrations/deploy}/vercel.md +2 -2
- package/.docs/{reference/workspace/s3-filesystem.md → integrations/file-storage/amazon-s3.md} +5 -5
- package/.docs/{reference/workspace/archil-filesystem.md → integrations/file-storage/archil.md} +3 -3
- package/.docs/{reference/workspace/azure-blob-filesystem.md → integrations/file-storage/azure-blob.md} +2 -2
- package/.docs/{reference/workspace/gcs-filesystem.md → integrations/file-storage/google-cloud-storage.md} +5 -5
- package/.docs/{reference/workspace/mesa-filesystem.md → integrations/file-storage/mesa.md} +1 -1
- package/.docs/{reference/workspace/files-sdk-filesystem.md → integrations/file-storage/vercel-files.md} +5 -5
- package/.docs/{guides/getting-started → integrations/frameworks}/astro.md +1 -1
- package/.docs/{guides/getting-started → integrations/frameworks}/electron.md +1 -1
- package/.docs/{guides/getting-started → integrations/frameworks}/next-js.md +2 -2
- package/.docs/{guides/getting-started → integrations/frameworks}/nuxt.md +1 -1
- package/.docs/{guides/getting-started → integrations/frameworks}/sveltekit.md +1 -1
- package/.docs/{guides/getting-started → integrations/frameworks}/vite-react.md +1 -1
- package/.docs/{docs/observability/integrations/exporters → integrations/observability}/arize.md +1 -1
- package/.docs/{docs/observability/integrations/exporters → integrations/observability}/arthur.md +1 -1
- package/.docs/{docs/observability/integrations/exporters → integrations/observability}/braintrust.md +1 -1
- package/.docs/{docs/observability/integrations/exporters → integrations/observability}/confident-ai.md +1 -1
- package/.docs/integrations/observability/datadog.md +538 -0
- package/.docs/{docs/observability/integrations/exporters → integrations/observability}/laminar.md +1 -1
- package/.docs/{docs/observability/integrations/exporters → integrations/observability}/langfuse.md +1 -1
- package/.docs/{docs/observability/integrations/exporters → integrations/observability}/langsmith.md +1 -1
- package/.docs/{docs/observability/integrations/exporters/otel.md → integrations/observability/opentelemetry.md} +276 -44
- package/.docs/{docs/observability/integrations/exporters → integrations/observability}/posthog.md +1 -1
- package/.docs/{docs/observability/integrations/exporters → integrations/observability}/sentry.md +1 -1
- package/.docs/{reference/workspace/apple-container-sandbox.md → integrations/sandboxes/apple-container.md} +1 -1
- package/.docs/{reference/workspace/daytona-sandbox.md → integrations/sandboxes/daytona.md} +2 -2
- package/.docs/{reference/workspace/docker-sandbox.md → integrations/sandboxes/docker.md} +1 -1
- package/.docs/{reference/workspace/e2b-sandbox.md → integrations/sandboxes/e2b.md} +3 -3
- package/.docs/{reference/workspace/modal-sandbox.md → integrations/sandboxes/modal.md} +2 -2
- package/.docs/{reference/workspace/railway-sandbox.md → integrations/sandboxes/railway.md} +8 -0
- package/.docs/{reference/workspace/vercel-sandbox.md → integrations/sandboxes/vercel.md} +141 -12
- package/.docs/{reference → integrations}/tools/brightdata.md +1 -1
- package/.docs/{reference → integrations}/tools/perplexity.md +1 -1
- package/.docs/{reference → integrations}/tools/tavily.md +1 -1
- package/.docs/{reference → integrations}/voice/aws-nova-sonic.md +1 -1
- package/.docs/{reference → integrations}/voice/cloudflare.md +3 -3
- package/.docs/{reference/voice/google-gemini-live.md → integrations/voice/google.md} +305 -30
- package/.docs/{reference/voice/inworld-realtime.md → integrations/voice/inworld.md} +163 -25
- package/.docs/{reference → integrations}/voice/livekit.md +437 -34
- package/.docs/{reference/voice/openai-realtime.md → integrations/voice/openai.md} +117 -20
- package/.docs/integrations.md +147 -0
- package/.docs/models/embeddings.md +2 -2
- package/.docs/models/environment-variables.md +1 -0
- package/.docs/models/gateways/openrouter.md +4 -2
- package/.docs/models/gateways/vercel.md +2 -1
- package/.docs/models/index.md +1 -1
- package/.docs/models/providers/coralbricks.md +75 -0
- package/.docs/models/providers/evroc.md +2 -3
- package/.docs/models/providers/hyper.md +6 -6
- package/.docs/models/providers/kilo.md +7 -5
- package/.docs/models/providers/llmgateway.md +1 -2
- package/.docs/models/providers/nano-gpt.md +1 -3
- package/.docs/models/providers/opencode.md +0 -1
- package/.docs/models/providers/privatemode-ai.md +5 -4
- package/.docs/models/providers/snowflake-cortex.md +5 -1
- package/.docs/models/providers/wandb.md +2 -1
- package/.docs/models/providers/zenmux.md +0 -15
- package/.docs/models/providers.md +1 -0
- package/.docs/reference/agent-controller/agent-controller-class.md +32 -1
- package/.docs/reference/agents/channels.md +2 -2
- package/.docs/reference/agents/generateLegacy.md +2 -2
- package/.docs/reference/agents/getDefaultOptions.md +1 -1
- package/.docs/reference/agents/getDefaultStreamOptions.md +1 -1
- package/.docs/reference/agents/inngest-agent.md +1 -1
- package/.docs/reference/agents/network.md +1 -1
- package/.docs/reference/ai-sdk/handle-network-stream.md +1 -1
- package/.docs/reference/ai-sdk/network-route.md +1 -1
- package/.docs/reference/auth/auth0.md +1 -1
- package/.docs/reference/auth/better-auth.md +1 -1
- package/.docs/reference/auth/clerk.md +1 -1
- package/.docs/reference/auth/firebase.md +1 -1
- package/.docs/reference/auth/google.md +1 -1
- package/.docs/reference/auth/supabase.md +1 -1
- package/.docs/reference/auth/workos.md +1 -1
- package/.docs/reference/browser/agent-browser.md +1 -1
- package/.docs/reference/browser/browser-viewer.md +1 -1
- package/.docs/reference/browser/firecrawl-browser.md +1 -1
- package/.docs/reference/browser/stagehand-browser.md +1 -1
- package/.docs/reference/channels/channel-provider.md +1 -1
- package/.docs/reference/channels/slack-provider.md +2 -2
- package/.docs/reference/cli/mastra.md +2 -2
- package/.docs/reference/configuration.md +1 -26
- package/.docs/reference/core/getStorage.md +1 -1
- package/.docs/reference/core/getVector.md +2 -2
- package/.docs/reference/core/listVectors.md +2 -2
- package/.docs/reference/core/setStorage.md +1 -1
- package/.docs/reference/file-based-agents/storage.md +1 -1
- package/.docs/reference/file-based-agents/studio.md +1 -1
- package/.docs/reference/index.md +27 -55
- package/.docs/{guides → reference}/migrations/ai-sdk-v4-to-v5.md +1 -1
- package/.docs/{guides → reference}/migrations/mastra-cloud.md +1 -1
- package/.docs/{guides → reference}/migrations/upgrade-to-v1/cli.md +1 -1
- package/.docs/{guides → reference}/migrations/upgrade-to-v1/memory.md +1 -1
- package/.docs/{guides → reference}/migrations/upgrade-to-v1/overview.md +41 -41
- package/.docs/{guides → reference}/migrations/upgrade-to-v1/tools.md +1 -1
- package/.docs/{guides → reference}/migrations/upgrade-to-v1/tracing.md +10 -10
- package/.docs/reference/observability/tracing/bridges/datadog.md +2 -2
- package/.docs/reference/observability/tracing/bridges/otel.md +2 -2
- package/.docs/reference/observability/tracing/exporters/arize.md +1 -1
- package/.docs/reference/observability/tracing/exporters/arthur.md +1 -1
- package/.docs/reference/observability/tracing/exporters/confident-ai.md +1 -1
- package/.docs/reference/observability/tracing/exporters/mastra-platform-exporter.md +12 -0
- package/.docs/reference/observability/tracing/exporters/otel.md +2 -2
- package/.docs/reference/pubsub/lease-provider.md +1 -1
- package/.docs/{guides → reference}/rag/chunking-and-embedding.md +16 -19
- package/.docs/reference/rag/database-config.md +1 -1
- package/.docs/{guides/rag/graph-rag.md → reference/rag/graph-rag-guide.md} +1 -1
- package/.docs/{guides → reference}/rag/overview.md +2 -2
- package/.docs/{guides → reference}/rag/retrieval.md +1 -1
- package/.docs/{guides → reference}/rag/vector-databases.md +1 -1
- package/.docs/reference/server/create-route.md +27 -1
- package/.docs/reference/storage/composite.md +3 -3
- package/.docs/reference/storage/retention.md +4 -4
- package/.docs/reference/streaming/ChunkType.md +3 -3
- package/.docs/reference/streaming/agents/streamLegacy.md +3 -3
- package/.docs/reference/tools/mcp-server.md +1 -1
- package/.docs/reference/vectors/convex.md +2 -2
- package/.docs/reference/vectors/oracledb.md +2 -2
- package/.docs/reference/workers/overview.md +8 -8
- package/.docs/reference/workflows/run-methods/timeTravel.md +1 -0
- package/.docs/reference/workspace/platform-filesystem.md +2 -2
- package/.docs/reference/workspace/platform-sandbox.md +10 -2
- package/.docs/reference/workspace/process-manager.md +3 -3
- package/.docs/reference/workspace/sandbox.md +10 -0
- package/CHANGELOG.md +16 -0
- package/dist/index.js +1 -1
- package/dist/{src-BZcgzbk9.js → src-D-W-bx5t.js} +2 -2
- package/dist/{src-BZcgzbk9.js.map → src-D-W-bx5t.js.map} +1 -1
- package/dist/stdio.js +1 -1
- package/package.json +5 -5
- package/.docs/docs/capabilities/channels/other-adapters.md +0 -68
- package/.docs/docs/observability/integrations/bridges/datadog.md +0 -219
- package/.docs/docs/observability/integrations/bridges/otel.md +0 -234
- package/.docs/docs/observability/integrations/exporters/datadog.md +0 -321
- package/.docs/guides/build-your-ui/copilotkit/channels.md +0 -86
- package/.docs/guides/build-your-ui/copilotkit/generative-ui.md +0 -174
- package/.docs/guides/guide/chef-michel.md +0 -211
- package/.docs/guides/guide/publishing-mcp-server.md +0 -137
- package/.docs/guides/guide/slack-assistant.md +0 -193
- package/.docs/guides/guide/stock-agent.md +0 -132
- package/.docs/guides/guide/web-search.md +0 -322
- package/.docs/guides/guide/whatsapp-chat-bot.md +0 -407
- package/.docs/guides/voice/realtime-voice.md +0 -430
- package/.docs/reference/voice/google.md +0 -290
- package/.docs/reference/voice/inworld.md +0 -137
- package/.docs/reference/voice/openai.md +0 -96
- package/.docs/reference/voice/playai.md +0 -82
- package/.docs/reference/workspace/vercel-serverless.md +0 -128
- /package/.docs/{guides/concepts → docs/guides}/multi-agent-systems.md +0 -0
- /package/.docs/{guides/concepts → docs/guides}/streaming.md +0 -0
- /package/.docs/{guides/build-your-ui → integrations/agentic-ui}/assistant-ui.md +0 -0
- /package/.docs/{guides/build-your-ui → integrations/agentic-ui}/openui.md +0 -0
- /package/.docs/{docs/server → integrations}/auth/auth0.md +0 -0
- /package/.docs/{docs/server → integrations}/auth/better-auth.md +0 -0
- /package/.docs/{docs/server → integrations}/auth/clerk.md +0 -0
- /package/.docs/{docs/server → integrations}/auth/firebase.md +0 -0
- /package/.docs/{docs/server → integrations}/auth/google.md +0 -0
- /package/.docs/{docs/server → integrations}/auth/okta.md +0 -0
- /package/.docs/{docs/server → integrations}/auth/supabase.md +0 -0
- /package/.docs/{docs/server → integrations}/auth/workos.md +0 -0
- /package/.docs/{guides/deployment → integrations/deploy}/amazon-ec2.md +0 -0
- /package/.docs/{guides/deployment → integrations/deploy}/inngest.md +0 -0
- /package/.docs/{guides/deployment → integrations/deploy}/kubernetes.md +0 -0
- /package/.docs/{guides/deployment → integrations/deploy}/temporal.md +0 -0
- /package/.docs/{reference/workspace/agentfs-filesystem.md → integrations/file-storage/agentfs.md} +0 -0
- /package/.docs/{reference/workspace/google-drive-filesystem.md → integrations/file-storage/google-drive.md} +0 -0
- /package/.docs/{guides/getting-started → integrations/frameworks}/express.md +0 -0
- /package/.docs/{guides/getting-started → integrations/frameworks}/hono.md +0 -0
- /package/.docs/{guides/getting-started → integrations/frameworks}/nestjs.md +0 -0
- /package/.docs/{reference/workspace/agentcore-runtime-sandbox.md → integrations/sandboxes/agentcore.md} +0 -0
- /package/.docs/{reference/workspace/blaxel-sandbox.md → integrations/sandboxes/blaxel.md} +0 -0
- /package/.docs/{guides/guide → integrations/tools}/firecrawl.md +0 -0
- /package/.docs/{reference → integrations}/voice/azure.md +0 -0
- /package/.docs/{reference → integrations}/voice/deepgram.md +0 -0
- /package/.docs/{reference → integrations}/voice/elevenlabs.md +0 -0
- /package/.docs/{reference → integrations}/voice/mistral.md +0 -0
- /package/.docs/{reference → integrations}/voice/murf.md +0 -0
- /package/.docs/{reference → integrations}/voice/sarvam.md +0 -0
- /package/.docs/{reference → integrations}/voice/speechify.md +0 -0
- /package/.docs/{reference/voice/xai-realtime.md → integrations/voice/xai.md} +0 -0
- /package/.docs/{guides/getting-started → reference}/manual-install.md +0 -0
- /package/.docs/{guides → reference}/migrations/agentnetwork.md +0 -0
- /package/.docs/{guides → reference}/migrations/network-to-supervisor.md +0 -0
- /package/.docs/{guides → reference}/migrations/upgrade-to-v1/agent.md +0 -0
- /package/.docs/{guides → reference}/migrations/upgrade-to-v1/client.md +0 -0
- /package/.docs/{guides → reference}/migrations/upgrade-to-v1/deployment.md +0 -0
- /package/.docs/{guides → reference}/migrations/upgrade-to-v1/evals.md +0 -0
- /package/.docs/{guides → reference}/migrations/upgrade-to-v1/mastra.md +0 -0
- /package/.docs/{guides → reference}/migrations/upgrade-to-v1/mcp.md +0 -0
- /package/.docs/{guides → reference}/migrations/upgrade-to-v1/processors.md +0 -0
- /package/.docs/{guides → reference}/migrations/upgrade-to-v1/rag.md +0 -0
- /package/.docs/{guides → reference}/migrations/upgrade-to-v1/storage.md +0 -0
- /package/.docs/{guides → reference}/migrations/upgrade-to-v1/vectors.md +0 -0
- /package/.docs/{guides → reference}/migrations/upgrade-to-v1/voice.md +0 -0
- /package/.docs/{guides → reference}/migrations/upgrade-to-v1/workflows.md +0 -0
- /package/.docs/{guides → reference}/migrations/vnext-to-standard-apis.md +0 -0
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
-
|
|
3
|
-
# Building an AI chef assistant
|
|
4
|
-
|
|
5
|
-
In this guide, you'll create a "Chef Assistant" agent that helps users cook meals with available ingredients.
|
|
6
|
-
|
|
7
|
-
You'll learn how to create the agent and register it with Mastra. Next, you'll interact with the agent through your terminal and get to know different response formats. You'll then access the agent through Mastra's local API endpoints.
|
|
8
|
-
|
|
9
|
-
## Prerequisites
|
|
10
|
-
|
|
11
|
-
- Node.js `v22.13.0` or later installed
|
|
12
|
-
- An API key from a supported [Model Provider](https://mastra.ai/models)
|
|
13
|
-
- An existing Mastra project (Follow the [installation guide](https://mastra.ai/guides/getting-started/quickstart) to set up a new project)
|
|
14
|
-
|
|
15
|
-
## Create the agent
|
|
16
|
-
|
|
17
|
-
To create an agent in Mastra use the `Agent` class to define it and then register it with Mastra.
|
|
18
|
-
|
|
19
|
-
1. Create a new file `src/mastra/agents/chefAgent.ts` and define your agent:
|
|
20
|
-
|
|
21
|
-
```ts
|
|
22
|
-
import { Agent } from '@mastra/core/agent'
|
|
23
|
-
|
|
24
|
-
export const chefAgent = new Agent({
|
|
25
|
-
id: 'chef-agent',
|
|
26
|
-
name: 'chef-agent',
|
|
27
|
-
instructions:
|
|
28
|
-
'You are Michel, a practical and experienced home chef' +
|
|
29
|
-
'You help people cook with whatever ingredients they have available.',
|
|
30
|
-
model: 'openai/gpt-5.6-sol',
|
|
31
|
-
})
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
2. In your `src/mastra/index.ts` file, register the agent:
|
|
35
|
-
|
|
36
|
-
```ts
|
|
37
|
-
import { Mastra } from '@mastra/core'
|
|
38
|
-
import { chefAgent } from './agents/chefAgent'
|
|
39
|
-
|
|
40
|
-
export const mastra = new Mastra({
|
|
41
|
-
agents: { chefAgent },
|
|
42
|
-
})
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
## Interact with the agent
|
|
46
|
-
|
|
47
|
-
Depending on your requirements you can interact and get responses from the agent in different formats. In the following steps you'll learn how to generate, stream, and get structured output.
|
|
48
|
-
|
|
49
|
-
1. Create a new file `src/index.ts` and add a `main()` function to it. Inside, craft a query to ask the agent and log its response.
|
|
50
|
-
|
|
51
|
-
```ts
|
|
52
|
-
import { chefAgent } from './mastra/agents/chefAgent'
|
|
53
|
-
|
|
54
|
-
async function main() {
|
|
55
|
-
const query =
|
|
56
|
-
'In my kitchen I have: pasta, canned tomatoes, garlic, olive oil, and some dried herbs (basil and oregano). What can I make?'
|
|
57
|
-
console.log(`Query: ${query}`)
|
|
58
|
-
|
|
59
|
-
const response = await chefAgent.generate([{ role: 'user', content: query }])
|
|
60
|
-
console.log('\n👨🍳 Chef Michel:', response.text)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
main()
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
Afterwards, run the script:
|
|
67
|
-
|
|
68
|
-
```bash
|
|
69
|
-
npx bun src/index.ts
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
You should get an output similar to this:
|
|
73
|
-
|
|
74
|
-
```text
|
|
75
|
-
Query: In my kitchen I have: pasta, canned tomatoes, garlic, olive oil, and some dried herbs (basil and oregano). What can I make?
|
|
76
|
-
|
|
77
|
-
👨🍳 Chef Michel: You can make a delicious pasta al pomodoro! Here's how...
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
2. In the previous example you might have waited a bit for the response without any sign of progress. To show the agent's output as it creates it you should instead stream its response to the terminal.
|
|
81
|
-
|
|
82
|
-
```ts
|
|
83
|
-
import { chefAgent } from './mastra/agents/chefAgent'
|
|
84
|
-
|
|
85
|
-
async function main() {
|
|
86
|
-
const query =
|
|
87
|
-
"Now I'm over at my friend's house, and they have: chicken thighs, coconut milk, sweet potatoes, and some curry powder."
|
|
88
|
-
console.log(`Query: ${query}`)
|
|
89
|
-
|
|
90
|
-
const stream = await chefAgent.stream([{ role: 'user', content: query }])
|
|
91
|
-
|
|
92
|
-
console.log('\n Chef Michel: ')
|
|
93
|
-
|
|
94
|
-
for await (const chunk of stream.textStream) {
|
|
95
|
-
process.stdout.write(chunk)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
console.log('\n\n✅ Recipe complete!')
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
main()
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
Afterwards, run the script again:
|
|
105
|
-
|
|
106
|
-
```bash
|
|
107
|
-
npx bun src/index.ts
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
You should get an output similar to the one below. This time though you can read it line by line instead of one large block.
|
|
111
|
-
|
|
112
|
-
```text
|
|
113
|
-
Query: Now I'm over at my friend's house, and they have: chicken thighs, coconut milk, sweet potatoes, and some curry powder.
|
|
114
|
-
|
|
115
|
-
👨🍳 Chef Michel:
|
|
116
|
-
Great! You can make a comforting chicken curry...
|
|
117
|
-
|
|
118
|
-
✅ Recipe complete!
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
3. Instead of showing the agent's response to a human you might want to pass it along to another part of your code. For these instances your agent should return [structured output](https://mastra.ai/docs/agents/structured-output).
|
|
122
|
-
|
|
123
|
-
Change your `src/index.ts` to the following:
|
|
124
|
-
|
|
125
|
-
```ts
|
|
126
|
-
import { chefAgent } from './mastra/agents/chefAgent'
|
|
127
|
-
import { z } from 'zod'
|
|
128
|
-
|
|
129
|
-
async function main() {
|
|
130
|
-
const query = 'I want to make lasagna, can you generate a lasagna recipe for me?'
|
|
131
|
-
console.log(`Query: ${query}`)
|
|
132
|
-
|
|
133
|
-
// Define the Zod schema
|
|
134
|
-
const schema = z.object({
|
|
135
|
-
ingredients: z.array(
|
|
136
|
-
z.object({
|
|
137
|
-
name: z.string(),
|
|
138
|
-
amount: z.string(),
|
|
139
|
-
}),
|
|
140
|
-
),
|
|
141
|
-
steps: z.array(z.string()),
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
const response = await chefAgent.generate([{ role: 'user', content: query }], {
|
|
145
|
-
structuredOutput: {
|
|
146
|
-
schema,
|
|
147
|
-
},
|
|
148
|
-
})
|
|
149
|
-
console.log('\n👨🍳 Chef Michel:', response.object)
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
main()
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
After running the script again you should get an output similar to this:
|
|
156
|
-
|
|
157
|
-
```text
|
|
158
|
-
Query: I want to make lasagna, can you generate a lasagna recipe for me?
|
|
159
|
-
|
|
160
|
-
👨🍳 Chef Michel: {
|
|
161
|
-
ingredients: [
|
|
162
|
-
{ name: "Lasagna noodles", amount: "12 sheets" },
|
|
163
|
-
{ name: "Ground beef", amount: "1 pound" },
|
|
164
|
-
],
|
|
165
|
-
steps: [
|
|
166
|
-
"Preheat oven to 375°F (190°C).",
|
|
167
|
-
"Cook the lasagna noodles according to package instructions.",
|
|
168
|
-
]
|
|
169
|
-
}
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
## Run the agent server
|
|
173
|
-
|
|
174
|
-
Learn how to interact with your agent through Mastra's API.
|
|
175
|
-
|
|
176
|
-
1. You can run your agent as a service using the `mastra dev` command:
|
|
177
|
-
|
|
178
|
-
```bash
|
|
179
|
-
mastra dev
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
This will start a server exposing endpoints to interact with your registered agents. Within [Studio](https://mastra.ai/docs/studio/overview) you can test your agent through a UI.
|
|
183
|
-
|
|
184
|
-
2. By default, `mastra dev` runs on `http://localhost:4111`. Your Chef Assistant agent will be available at:
|
|
185
|
-
|
|
186
|
-
```text
|
|
187
|
-
POST http://localhost:4111/api/agents/chefAgent/generate
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
3. You can interact with the agent using `curl` from the command line:
|
|
191
|
-
|
|
192
|
-
```bash
|
|
193
|
-
curl -X POST http://localhost:4111/api/agents/chefAgent/generate \
|
|
194
|
-
-H "Content-Type: application/json" \
|
|
195
|
-
-d '{
|
|
196
|
-
"messages": [
|
|
197
|
-
{
|
|
198
|
-
"role": "user",
|
|
199
|
-
"content": "I have eggs, flour, and milk. What can I make?"
|
|
200
|
-
}
|
|
201
|
-
]
|
|
202
|
-
}'
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
**Sample Response:**
|
|
206
|
-
|
|
207
|
-
```json
|
|
208
|
-
{
|
|
209
|
-
"text": "You can make delicious pancakes! Here's a simple recipe..."
|
|
210
|
-
}
|
|
211
|
-
```
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
-
|
|
3
|
-
# Publishing an MCP server
|
|
4
|
-
|
|
5
|
-
This example guides you through setting up a basic Mastra MCPServer using the stdio transport, building it, and preparing it for publishing to NPM.
|
|
6
|
-
|
|
7
|
-
## Install dependencies
|
|
8
|
-
|
|
9
|
-
Install the necessary packages:
|
|
10
|
-
|
|
11
|
-
**npm**:
|
|
12
|
-
|
|
13
|
-
```bash
|
|
14
|
-
npm install @mastra/mcp @mastra/core tsup
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
**pnpm**:
|
|
18
|
-
|
|
19
|
-
```bash
|
|
20
|
-
pnpm add @mastra/mcp @mastra/core tsup
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
**Yarn**:
|
|
24
|
-
|
|
25
|
-
```bash
|
|
26
|
-
yarn add @mastra/mcp @mastra/core tsup
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
**Bun**:
|
|
30
|
-
|
|
31
|
-
```bash
|
|
32
|
-
bun add @mastra/mcp @mastra/core tsup
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
## Setting up an MCP server
|
|
36
|
-
|
|
37
|
-
1. Create a file for your stdio server, for example, `src/mastra/stdio.ts`.
|
|
38
|
-
|
|
39
|
-
2. Add the following code to the file. Remember to import your actual Mastra tools and name the server appropriately.
|
|
40
|
-
|
|
41
|
-
```typescript
|
|
42
|
-
#!/usr/bin/env node
|
|
43
|
-
import { MCPServer } from '@mastra/mcp'
|
|
44
|
-
import { weatherTool } from './tools'
|
|
45
|
-
|
|
46
|
-
const server = new MCPServer({
|
|
47
|
-
name: 'my-mcp-server',
|
|
48
|
-
version: '1.0.0',
|
|
49
|
-
tools: { weatherTool },
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
server.startStdio().catch(error => {
|
|
53
|
-
console.error('Error running MCP server:', error)
|
|
54
|
-
process.exit(1)
|
|
55
|
-
})
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
3. Update your `package.json` to include the `bin` entry pointing to your built server file and a script to build the server with both ESM and CJS outputs.
|
|
59
|
-
|
|
60
|
-
```json
|
|
61
|
-
{
|
|
62
|
-
"bin": "dist/stdio.mjs",
|
|
63
|
-
"scripts": {
|
|
64
|
-
"build:mcp": "tsup src/mastra/stdio.ts --format esm,cjs --no-splitting --dts && echo '#!/usr/bin/env node' | cat - dist/stdio.mjs > temp && mv temp dist/stdio.mjs && chmod +x dist/stdio.mjs"
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
The build command generates both ESM (`.mjs`) and CJS (`.cjs`) outputs for maximum compatibility. The shebang (`#!/usr/bin/env node`) is prepended to the ESM artifact to make it directly executable, and the `bin` entry points to this file.
|
|
70
|
-
|
|
71
|
-
4. Run the build command:
|
|
72
|
-
|
|
73
|
-
**npm**:
|
|
74
|
-
|
|
75
|
-
```bash
|
|
76
|
-
npm run build:mcp
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
**pnpm**:
|
|
80
|
-
|
|
81
|
-
```bash
|
|
82
|
-
pnpm run build:mcp
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
**Yarn**:
|
|
86
|
-
|
|
87
|
-
```bash
|
|
88
|
-
yarn build:mcp
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
**Bun**:
|
|
92
|
-
|
|
93
|
-
```bash
|
|
94
|
-
bun run build:mcp
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
This will compile your server code into both ESM and CJS formats and make the ESM output file executable. On Unix-like systems, the `chmod +x` step makes the file directly executable. Windows users may need to use WSL or handle execution through Node.js directly.
|
|
98
|
-
|
|
99
|
-
## Publishing to NPM
|
|
100
|
-
|
|
101
|
-
To make your MCP server available for others (or yourself) to use via `npx` or as a dependency, you can publish it to NPM.
|
|
102
|
-
|
|
103
|
-
1. Ensure you have an NPM account and are logged in (`npm login`).
|
|
104
|
-
|
|
105
|
-
2. Make sure your package name in `package.json` is unique and available.
|
|
106
|
-
|
|
107
|
-
3. Run the publish command from your project root after building:
|
|
108
|
-
|
|
109
|
-
```bash
|
|
110
|
-
npm publish --access public
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
For more details on publishing packages, refer to the [NPM documentation](https://docs.npmjs.com/creating-and-publishing-scoped-public-packages).
|
|
114
|
-
|
|
115
|
-
## Using a published MCP server
|
|
116
|
-
|
|
117
|
-
Once published, your MCP server can be used by an `MCPClient` by specifying the command to run your package. You can also use any other MCP client like Claude desktop, Cursor, or Windsurf.
|
|
118
|
-
|
|
119
|
-
```typescript
|
|
120
|
-
import { MCPClient } from '@mastra/mcp'
|
|
121
|
-
|
|
122
|
-
const mcp = new MCPClient({
|
|
123
|
-
servers: {
|
|
124
|
-
// Give this MCP server instance a name
|
|
125
|
-
yourServerName: {
|
|
126
|
-
command: 'npx',
|
|
127
|
-
args: ['-y', '@your-org-name/your-package-name@latest'], // Replace with your package name
|
|
128
|
-
},
|
|
129
|
-
},
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
// You can then get tools or toolsets from this configuration to use in your agent
|
|
133
|
-
const tools = await mcp.listTools()
|
|
134
|
-
const toolsets = await mcp.listToolsets()
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
Note: If you published without an organization scope, the `args` might be `["-y", "your-package-name@latest"]`.
|
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
-
|
|
3
|
-
# Building a Slack assistant
|
|
4
|
-
|
|
5
|
-
In this guide, you'll build a Mastra agent that responds to messages and mentions on Slack. You'll learn how to configure a channel adapter, set up a Slack app with the right permissions, connect it to your agent via a webhook, and test the interaction.
|
|
6
|
-
|
|
7
|
-
## Prerequisites
|
|
8
|
-
|
|
9
|
-
- Node.js `v22.13.0` or later installed
|
|
10
|
-
- An API key from a supported [Model Provider](https://mastra.ai/models)
|
|
11
|
-
- An existing Mastra project. Follow the [installation guide](https://mastra.ai/guides/getting-started/quickstart) if needed.
|
|
12
|
-
- A [Slack workspace](https://slack.com/) where you can create apps
|
|
13
|
-
|
|
14
|
-
## Create the agent
|
|
15
|
-
|
|
16
|
-
Install the Slack adapter:
|
|
17
|
-
|
|
18
|
-
**npm**:
|
|
19
|
-
|
|
20
|
-
```bash
|
|
21
|
-
npm install @chat-adapter/slack
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
**pnpm**:
|
|
25
|
-
|
|
26
|
-
```bash
|
|
27
|
-
pnpm add @chat-adapter/slack
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
**Yarn**:
|
|
31
|
-
|
|
32
|
-
```bash
|
|
33
|
-
yarn add @chat-adapter/slack
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
**Bun**:
|
|
37
|
-
|
|
38
|
-
```bash
|
|
39
|
-
bun add @chat-adapter/slack
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
Create a new file `src/mastra/agents/slack-agent.ts` and define your agent:
|
|
43
|
-
|
|
44
|
-
```ts
|
|
45
|
-
import { Agent } from '@mastra/core/agent'
|
|
46
|
-
import { createSlackAdapter } from '@chat-adapter/slack'
|
|
47
|
-
|
|
48
|
-
export const slackAgent = new Agent({
|
|
49
|
-
id: 'slack-agent',
|
|
50
|
-
name: 'Slack Agent',
|
|
51
|
-
instructions:
|
|
52
|
-
'You are a helpful assistant. Answer questions, help with tasks, and have natural conversations.',
|
|
53
|
-
model: 'anthropic/claude-opus-4-7',
|
|
54
|
-
channels: {
|
|
55
|
-
adapters: {
|
|
56
|
-
slack: createSlackAdapter(),
|
|
57
|
-
},
|
|
58
|
-
},
|
|
59
|
-
})
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
The `channels` property tells Mastra to generate a webhook endpoint for each adapter. In this case, the Slack adapter handles event verification, signature validation, and message formatting automatically.
|
|
63
|
-
|
|
64
|
-
Register the agent in your `src/mastra/index.ts` file:
|
|
65
|
-
|
|
66
|
-
```ts
|
|
67
|
-
import { Mastra } from '@mastra/core'
|
|
68
|
-
import { slackAgent } from './agents/slack-agent'
|
|
69
|
-
|
|
70
|
-
export const mastra = new Mastra({
|
|
71
|
-
agents: { slackAgent },
|
|
72
|
-
})
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
## Create a Slack app
|
|
76
|
-
|
|
77
|
-
You need a Slack app to connect your agent to a workspace.
|
|
78
|
-
|
|
79
|
-
Go to <https://api.slack.com/apps> and select **Create New App** > **From scratch**. Give it a name and select your workspace.
|
|
80
|
-
|
|
81
|
-
Navigate to **OAuth & Permissions** and scroll to **Bot Token Scopes**. Add the following scopes:
|
|
82
|
-
|
|
83
|
-
- `app_mentions:read`
|
|
84
|
-
- `channels:history`
|
|
85
|
-
- `channels:read`
|
|
86
|
-
- `chat:write`
|
|
87
|
-
- `users:read`
|
|
88
|
-
|
|
89
|
-
At the top of **OAuth & Permissions**, select **Install to Workspace**. Copy the **Bot User OAuth Token** (`xoxb-...`).
|
|
90
|
-
|
|
91
|
-
Go to **Basic Information** > **App Credentials** and copy the **Signing Secret** (e.g. `c3a4...`).
|
|
92
|
-
|
|
93
|
-
Ensure **Socket Mode** is turned **off** under **Settings** > **Socket Mode**.
|
|
94
|
-
|
|
95
|
-
Add the credentials to your `.env` file:
|
|
96
|
-
|
|
97
|
-
```bash
|
|
98
|
-
SLACK_SIGNING_SECRET=your-signing-secret
|
|
99
|
-
SLACK_BOT_TOKEN=xoxb-your-bot-token
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
The adapter reads these environment variables by default. See the [Chat SDK Slack adapter docs](https://chat-sdk.dev/adapters/slack) for more details.
|
|
103
|
-
|
|
104
|
-
## Connect the webhook
|
|
105
|
-
|
|
106
|
-
Slack delivers events to your agent via a webhook. Mastra generates this endpoint automatically for each channel adapter.
|
|
107
|
-
|
|
108
|
-
Start the dev server:
|
|
109
|
-
|
|
110
|
-
**npm**:
|
|
111
|
-
|
|
112
|
-
```bash
|
|
113
|
-
npm run dev
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
**pnpm**:
|
|
117
|
-
|
|
118
|
-
```bash
|
|
119
|
-
pnpm run dev
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
**Yarn**:
|
|
123
|
-
|
|
124
|
-
```bash
|
|
125
|
-
yarn dev
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
**Bun**:
|
|
129
|
-
|
|
130
|
-
```bash
|
|
131
|
-
bun run dev
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
During local development, Slack needs a public URL to reach your local server. Open a new terminal and start a tunnel:
|
|
135
|
-
|
|
136
|
-
```bash
|
|
137
|
-
npx cloudflared tunnel --url http://localhost:4111
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
This outputs a temporary public URL like `https://triple-arms-solutions-kit.trycloudflare.com`. The URL changes each time you restart the tunnel.
|
|
141
|
-
|
|
142
|
-
In your Slack app settings, go to **Event Subscriptions** and toggle **Enable Events** to **On**.
|
|
143
|
-
|
|
144
|
-
Set the **Request URL** to your tunnel URL with the agent webhook path:
|
|
145
|
-
|
|
146
|
-
```text
|
|
147
|
-
https://<your-tunnel-url>/api/agents/slack-agent/channels/slack/webhook
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
Slack sends a verification request. If your dev server is running, it responds with a green checkmark.
|
|
151
|
-
|
|
152
|
-
Under **Subscribe to bot events**, add:
|
|
153
|
-
|
|
154
|
-
- `app_mention`
|
|
155
|
-
- `message.channels`
|
|
156
|
-
|
|
157
|
-
Select **Save Changes**. Slack requires you to reinstall the app after changing event subscriptions. Go to **OAuth & Permissions** and select **Reinstall to Workspace** to apply the updated permissions.
|
|
158
|
-
|
|
159
|
-
> **Note:** The tunnel URL is for local development only. When you [deploy your application](https://mastra.ai/docs/deployment/overview), update the **Request URL** in your Slack app's **Event Subscriptions** to your production URL (e.g. `https://your-app.example.com/api/agents/slack-agent/channels/slack/webhook`).
|
|
160
|
-
|
|
161
|
-
## Test the agent
|
|
162
|
-
|
|
163
|
-
Before testing with Slack, you can refine your agent's behavior in [Studio](https://mastra.ai/docs/studio/overview). Open <http://localhost:4111/> to chat with your agent and adjust its instructions. Inspect traces to see how responses are generated.
|
|
164
|
-
|
|
165
|
-
Once you're happy with the agent's responses, test the Slack integration. Invite the bot to a channel in Slack:
|
|
166
|
-
|
|
167
|
-
```text
|
|
168
|
-
/invite @your-bot-name
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
Mention the bot in the channel:
|
|
172
|
-
|
|
173
|
-
```text
|
|
174
|
-
@your-bot-name What can you help me with?
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
The agent responds in the thread. Output may vary depending on the model and instructions.
|
|
178
|
-
|
|
179
|
-
## Next steps
|
|
180
|
-
|
|
181
|
-
You can extend this agent to:
|
|
182
|
-
|
|
183
|
-
- [Deploy your application](https://mastra.ai/docs/deployment/overview) and update the Slack **Request URL** to your production endpoint
|
|
184
|
-
- Add more adapters (Discord, Telegram) to the same agent so it responds on multiple platforms
|
|
185
|
-
- Configure [multimodal content](https://mastra.ai/docs/capabilities/channels/overview) to let your agent process images, video, and audio shared in chat
|
|
186
|
-
- Add [tools](https://mastra.ai/docs/agents/using-tools) to give the agent access to external APIs and data
|
|
187
|
-
|
|
188
|
-
Learn more:
|
|
189
|
-
|
|
190
|
-
- [Channels overview](https://mastra.ai/docs/capabilities/channels/overview)
|
|
191
|
-
- [Studio](https://mastra.ai/docs/studio/overview)
|
|
192
|
-
- [Deployment overview](https://mastra.ai/docs/deployment/overview)
|
|
193
|
-
- [Chat SDK adapter docs](https://chat-sdk.dev)
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
-
|
|
3
|
-
# Building an AI stock agent
|
|
4
|
-
|
|
5
|
-
In this guide, you'll create an agent that fetches the previous day's closing stock price for a symbol. You'll create a tool and add it to an agent, then use the agent to fetch stock prices.
|
|
6
|
-
|
|
7
|
-
## Prerequisites
|
|
8
|
-
|
|
9
|
-
- Node.js `v22.13.0` or later installed
|
|
10
|
-
- An API key from a supported [Model Provider](https://mastra.ai/models)
|
|
11
|
-
- An existing Mastra project (Follow the [installation guide](https://mastra.ai/guides/getting-started/quickstart) to set up a new project)
|
|
12
|
-
|
|
13
|
-
## Creating the agent
|
|
14
|
-
|
|
15
|
-
To create an agent in Mastra use the `Agent` class to define it and then register it with Mastra.
|
|
16
|
-
|
|
17
|
-
1. Create a new file `src/mastra/agents/stockAgent.ts` and define your agent:
|
|
18
|
-
|
|
19
|
-
```ts
|
|
20
|
-
import { Agent } from '@mastra/core/agent'
|
|
21
|
-
|
|
22
|
-
export const stockAgent = new Agent({
|
|
23
|
-
id: 'stock-agent',
|
|
24
|
-
name: 'Stock Agent',
|
|
25
|
-
instructions:
|
|
26
|
-
'You are a helpful assistant that provides current stock prices. When asked about a stock, use the stock price tool to fetch the stock price.',
|
|
27
|
-
model: 'openai/gpt-5.6-sol',
|
|
28
|
-
})
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
2. In your `src/mastra/index.ts` file, register the agent:
|
|
32
|
-
|
|
33
|
-
```ts
|
|
34
|
-
import { Mastra } from '@mastra/core'
|
|
35
|
-
import { stockAgent } from './agents/stockAgent'
|
|
36
|
-
|
|
37
|
-
export const mastra = new Mastra({
|
|
38
|
-
agents: { stockAgent },
|
|
39
|
-
})
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
## Creating the stock price tool
|
|
43
|
-
|
|
44
|
-
The Stock Agent doesn't yet know anything about current stock prices. To change this, create a tool and add it to the agent.
|
|
45
|
-
|
|
46
|
-
1. Create a new file `src/mastra/tools/stockPrices.ts`. Inside, add a `stockPrices` tool that will fetch the last day's closing stock price for a given symbol:
|
|
47
|
-
|
|
48
|
-
```ts
|
|
49
|
-
import { createTool } from '@mastra/core/tools'
|
|
50
|
-
import { z } from 'zod'
|
|
51
|
-
|
|
52
|
-
const getStockPrice = async (symbol: string) => {
|
|
53
|
-
const data = await fetch(
|
|
54
|
-
`https://mastra-stock-data.vercel.app/api/stock-data?symbol=${symbol}`,
|
|
55
|
-
).then(r => r.json())
|
|
56
|
-
return data.prices['4. close']
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export const stockPrices = createTool({
|
|
60
|
-
id: 'Get Stock Price',
|
|
61
|
-
inputSchema: z.object({
|
|
62
|
-
symbol: z.string(),
|
|
63
|
-
}),
|
|
64
|
-
description: `Fetches the last day's closing stock price for a given symbol`,
|
|
65
|
-
execute: async inputData => {
|
|
66
|
-
console.log('Using tool to fetch stock price for', inputData.symbol)
|
|
67
|
-
return {
|
|
68
|
-
symbol: inputData.symbol,
|
|
69
|
-
currentPrice: await getStockPrice(inputData.symbol),
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
})
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
2. Inside `src/mastra/agents/stockAgent.ts` import your newly created `stockPrices` tool and add it to the agent.
|
|
76
|
-
|
|
77
|
-
```ts
|
|
78
|
-
import { Agent } from '@mastra/core/agent'
|
|
79
|
-
import { stockPrices } from '../tools/stockPrices'
|
|
80
|
-
|
|
81
|
-
export const stockAgent = new Agent({
|
|
82
|
-
id: 'stock-agent',
|
|
83
|
-
name: 'Stock Agent',
|
|
84
|
-
instructions:
|
|
85
|
-
'You are a helpful assistant that provides current stock prices. When asked about a stock, use the stock price tool to fetch the stock price.',
|
|
86
|
-
model: 'openai/gpt-5.6-sol',
|
|
87
|
-
tools: {
|
|
88
|
-
stockPrices,
|
|
89
|
-
},
|
|
90
|
-
})
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
## Running the agent server
|
|
94
|
-
|
|
95
|
-
Learn how to interact with your agent through Mastra's API.
|
|
96
|
-
|
|
97
|
-
1. You can run your agent as a service using the `mastra dev` command:
|
|
98
|
-
|
|
99
|
-
```bash
|
|
100
|
-
mastra dev
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
This will start a server exposing endpoints to interact with your registered agents. Within [Studio](https://mastra.ai/docs/studio/overview) you can test your `stockAgent` and `stockPrices` tool through a UI.
|
|
104
|
-
|
|
105
|
-
2. By default, `mastra dev` runs on `http://localhost:4111`. Your Stock agent will be available at:
|
|
106
|
-
|
|
107
|
-
```text
|
|
108
|
-
POST http://localhost:4111/api/agents/stockAgent/generate
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
3. You can interact with the agent using `curl` from the command line:
|
|
112
|
-
|
|
113
|
-
```bash
|
|
114
|
-
curl -X POST http://localhost:4111/api/agents/stockAgent/generate \
|
|
115
|
-
-H "Content-Type: application/json" \
|
|
116
|
-
-d '{
|
|
117
|
-
"messages": [
|
|
118
|
-
{ "role": "user", "content": "What is the current stock price of Apple (AAPL)?" }
|
|
119
|
-
]
|
|
120
|
-
}'
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
You should receive a JSON response similar to:
|
|
124
|
-
|
|
125
|
-
```json
|
|
126
|
-
{
|
|
127
|
-
"text": "The current price of Apple (AAPL) is $174.55.",
|
|
128
|
-
"agent": "Stock Agent"
|
|
129
|
-
}
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
This indicates that your agent successfully processed the request, used the `stockPrices` tool to fetch the stock price, and returned the result.
|