@mastra/mcp-docs-server 1.0.0-beta.4 → 1.0.0-beta.6
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/%40internal%2Fstorage-test-utils.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fagent-builder.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fai-sdk.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fastra.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fchroma.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fconvex.md +29 -0
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +411 -211
- package/.docs/organized/changelogs/%40mastra%2Fcouchbase.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-vercel.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fduckdb.md +42 -0
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Felasticsearch.md +52 -0
- package/.docs/organized/changelogs/%40mastra%2Fevals.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Flance.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fmcp-registry-registry.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fmssql.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fopensearch.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Frag.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Freact.md +89 -1
- package/.docs/organized/changelogs/%40mastra%2Fs3vectors.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fschema-compat.md +42 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-azure.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-cloudflare.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-deepgram.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-elevenlabs.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-gladia.md +92 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google-gemini-live.md +67 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-murf.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai-realtime.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-playai.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-sarvam.md +201 -1
- package/.docs/organized/changelogs/%40mastra%2Fvoice-speechify.md +201 -1
- package/.docs/organized/changelogs/create-mastra.md +201 -1
- package/.docs/organized/changelogs/mastra.md +201 -1
- package/.docs/organized/code-examples/agui.md +1 -0
- package/.docs/organized/code-examples/ai-sdk-v5.md +1 -0
- package/.docs/organized/code-examples/mcp-server-adapters.md +721 -0
- package/.docs/organized/code-examples/memory-with-processors.md +1 -1
- package/.docs/organized/code-examples/quick-start.md +1 -1
- package/.docs/organized/code-examples/server-app-access.md +342 -0
- package/.docs/raw/agents/adding-voice.mdx +7 -10
- package/.docs/raw/agents/agent-approval.mdx +189 -0
- package/.docs/raw/agents/guardrails.mdx +26 -23
- package/.docs/raw/agents/networks.mdx +2 -2
- package/.docs/raw/agents/overview.mdx +27 -62
- package/.docs/raw/agents/processors.mdx +279 -0
- package/.docs/raw/agents/using-tools.mdx +4 -5
- package/.docs/raw/course/01-first-agent/05-running-playground.md +5 -5
- package/.docs/raw/course/01-first-agent/09-testing-your-agent.md +3 -3
- package/.docs/raw/course/01-first-agent/13-testing-your-tool.md +3 -3
- package/.docs/raw/course/01-first-agent/17-testing-memory.md +2 -2
- package/.docs/raw/course/04-workflows/07-using-playground.md +1 -1
- package/.docs/raw/deployment/building-mastra.mdx +1 -1
- package/.docs/raw/deployment/cloud-providers/amazon-ec2.mdx +1 -1
- package/.docs/raw/deployment/cloud-providers/aws-lambda.mdx +1 -1
- package/.docs/raw/deployment/cloud-providers/azure-app-services.mdx +1 -1
- package/.docs/raw/deployment/cloud-providers/digital-ocean.mdx +1 -1
- package/.docs/raw/deployment/cloud-providers/index.mdx +20 -27
- package/.docs/raw/deployment/cloud-providers/netlify-deployer.mdx +44 -13
- package/.docs/raw/deployment/mastra-cloud/observability.mdx +19 -17
- package/.docs/raw/deployment/mastra-cloud/setting-up.mdx +1 -1
- package/.docs/raw/deployment/overview.mdx +2 -2
- package/.docs/raw/deployment/web-framework.mdx +5 -5
- package/.docs/raw/evals/custom-scorers.mdx +3 -5
- package/.docs/raw/evals/overview.mdx +2 -3
- package/.docs/raw/evals/running-in-ci.mdx +0 -2
- package/.docs/raw/{guides/guide → getting-started}/manual-install.mdx +2 -2
- package/.docs/raw/getting-started/project-structure.mdx +1 -1
- package/.docs/raw/getting-started/start.mdx +72 -0
- package/.docs/raw/getting-started/studio.mdx +1 -1
- package/.docs/raw/{frameworks/agentic-uis/ai-sdk.mdx → guides/build-your-ui/ai-sdk-ui.mdx} +113 -11
- package/.docs/raw/{frameworks/web-frameworks → guides/getting-started}/astro.mdx +23 -25
- package/.docs/raw/{frameworks/servers → guides/getting-started}/express.mdx +3 -4
- package/.docs/raw/guides/{quickstarts/nextjs.mdx → getting-started/next-js.mdx} +11 -11
- package/.docs/raw/guides/{quickstarts/standalone-server.mdx → getting-started/quickstart.mdx} +7 -7
- package/.docs/raw/{frameworks/web-frameworks → guides/getting-started}/sveltekit.mdx +23 -25
- package/.docs/raw/{frameworks/web-frameworks → guides/getting-started}/vite-react.mdx +7 -7
- package/.docs/raw/guides/guide/ai-recruiter.mdx +2 -3
- package/.docs/raw/guides/guide/chef-michel.mdx +2 -3
- package/.docs/raw/guides/guide/notes-mcp-server.mdx +2 -2
- package/.docs/raw/guides/guide/research-assistant.mdx +7 -8
- package/.docs/raw/guides/guide/stock-agent.mdx +4 -6
- package/.docs/raw/guides/guide/web-search.mdx +12 -10
- package/.docs/raw/guides/guide/whatsapp-chat-bot.mdx +421 -0
- package/.docs/raw/guides/index.mdx +3 -35
- package/.docs/raw/guides/migrations/agentnetwork.mdx +4 -4
- package/.docs/raw/guides/migrations/ai-sdk-v4-to-v5.mdx +1 -1
- package/.docs/raw/guides/migrations/upgrade-to-v1/agent.mdx +40 -0
- package/.docs/raw/guides/migrations/upgrade-to-v1/tools.mdx +5 -0
- package/.docs/raw/guides/migrations/upgrade-to-v1/workflows.mdx +51 -0
- package/.docs/raw/guides/migrations/vnext-to-standard-apis.mdx +2 -2
- package/.docs/raw/index.mdx +2 -2
- package/.docs/raw/mcp/overview.mdx +3 -5
- package/.docs/raw/memory/memory-processors.mdx +264 -79
- package/.docs/raw/memory/semantic-recall.mdx +7 -7
- package/.docs/raw/memory/storage/memory-with-libsql.mdx +2 -4
- package/.docs/raw/memory/storage/memory-with-mongodb.mdx +2 -4
- package/.docs/raw/memory/storage/memory-with-pg.mdx +2 -4
- package/.docs/raw/memory/storage/memory-with-upstash.mdx +2 -4
- package/.docs/raw/memory/threads-and-resources.mdx +3 -3
- package/.docs/raw/memory/working-memory.mdx +14 -7
- package/.docs/raw/{logging.mdx → observability/logging.mdx} +1 -1
- package/.docs/raw/observability/overview.mdx +2 -3
- package/.docs/raw/observability/tracing/bridges/otel.mdx +176 -0
- package/.docs/raw/observability/tracing/exporters/arize.mdx +17 -0
- package/.docs/raw/observability/tracing/exporters/braintrust.mdx +19 -0
- package/.docs/raw/observability/tracing/exporters/langfuse.mdx +20 -0
- package/.docs/raw/observability/tracing/exporters/langsmith.mdx +12 -0
- package/.docs/raw/observability/tracing/exporters/otel.mdx +25 -5
- package/.docs/raw/observability/tracing/exporters/posthog.mdx +107 -0
- package/.docs/raw/observability/tracing/overview.mdx +74 -8
- package/.docs/raw/observability/tracing/processors/sensitive-data-filter.mdx +0 -1
- package/.docs/raw/rag/chunking-and-embedding.mdx +16 -17
- package/.docs/raw/rag/overview.mdx +3 -2
- package/.docs/raw/rag/retrieval.mdx +43 -38
- package/.docs/raw/rag/vector-databases.mdx +93 -2
- package/.docs/raw/reference/agents/agent.mdx +7 -10
- package/.docs/raw/reference/agents/generate.mdx +55 -6
- package/.docs/raw/reference/agents/generateLegacy.mdx +2 -2
- package/.docs/raw/reference/agents/getLLM.mdx +1 -1
- package/.docs/raw/reference/agents/network.mdx +46 -3
- package/.docs/raw/reference/cli/mastra.mdx +2 -1
- package/.docs/raw/reference/client-js/agents.mdx +3 -3
- package/.docs/raw/reference/client-js/memory.mdx +43 -0
- package/.docs/raw/reference/client-js/workflows.mdx +92 -63
- package/.docs/raw/reference/core/getLogger.mdx +1 -1
- package/.docs/raw/reference/core/listLogs.mdx +1 -1
- package/.docs/raw/reference/core/listLogsByRunId.mdx +1 -1
- package/.docs/raw/reference/core/mastra-model-gateway.mdx +5 -19
- package/.docs/raw/reference/core/setLogger.mdx +1 -1
- package/.docs/raw/reference/core/setTelemetry.mdx +1 -1
- package/.docs/raw/reference/deployer/netlify.mdx +1 -2
- package/.docs/raw/reference/evals/answer-relevancy.mdx +28 -98
- package/.docs/raw/reference/evals/answer-similarity.mdx +12 -258
- package/.docs/raw/reference/evals/bias.mdx +29 -87
- package/.docs/raw/reference/evals/completeness.mdx +31 -90
- package/.docs/raw/reference/evals/content-similarity.mdx +28 -88
- package/.docs/raw/reference/evals/context-precision.mdx +28 -130
- package/.docs/raw/reference/evals/context-relevance.mdx +11 -11
- package/.docs/raw/reference/evals/faithfulness.mdx +28 -101
- package/.docs/raw/reference/evals/hallucination.mdx +28 -103
- package/.docs/raw/reference/evals/keyword-coverage.mdx +28 -107
- package/.docs/raw/reference/evals/noise-sensitivity.mdx +11 -11
- package/.docs/raw/reference/evals/prompt-alignment.mdx +15 -15
- package/.docs/raw/reference/evals/scorer-utils.mdx +362 -0
- package/.docs/raw/reference/evals/textual-difference.mdx +27 -100
- package/.docs/raw/reference/evals/tone-consistency.mdx +25 -98
- package/.docs/raw/reference/evals/tool-call-accuracy.mdx +7 -7
- package/.docs/raw/reference/evals/toxicity.mdx +29 -92
- package/.docs/raw/reference/index.mdx +1 -0
- package/.docs/raw/reference/memory/memory-class.mdx +5 -7
- package/.docs/raw/reference/observability/tracing/bridges/otel.mdx +150 -0
- package/.docs/raw/reference/observability/tracing/configuration.mdx +0 -4
- package/.docs/raw/reference/observability/tracing/exporters/arize.mdx +4 -0
- package/.docs/raw/reference/observability/tracing/exporters/langsmith.mdx +17 -1
- package/.docs/raw/reference/observability/tracing/exporters/otel.mdx +6 -0
- package/.docs/raw/reference/observability/tracing/exporters/posthog.mdx +132 -0
- package/.docs/raw/reference/observability/tracing/instances.mdx +0 -4
- package/.docs/raw/reference/observability/tracing/interfaces.mdx +29 -4
- package/.docs/raw/reference/observability/tracing/spans.mdx +0 -4
- package/.docs/raw/reference/processors/batch-parts-processor.mdx +1 -1
- package/.docs/raw/reference/processors/language-detector.mdx +10 -3
- package/.docs/raw/reference/processors/message-history-processor.mdx +131 -0
- package/.docs/raw/reference/processors/moderation-processor.mdx +12 -5
- package/.docs/raw/reference/processors/pii-detector.mdx +12 -5
- package/.docs/raw/reference/processors/processor-interface.mdx +502 -0
- package/.docs/raw/reference/processors/prompt-injection-detector.mdx +10 -3
- package/.docs/raw/reference/processors/semantic-recall-processor.mdx +197 -0
- package/.docs/raw/reference/processors/system-prompt-scrubber.mdx +3 -4
- package/.docs/raw/reference/processors/token-limiter-processor.mdx +2 -2
- package/.docs/raw/reference/processors/tool-call-filter.mdx +125 -0
- package/.docs/raw/reference/processors/unicode-normalizer.mdx +1 -1
- package/.docs/raw/reference/processors/working-memory-processor.mdx +221 -0
- package/.docs/raw/reference/rag/embeddings.mdx +5 -5
- package/.docs/raw/reference/rag/rerank.mdx +1 -2
- package/.docs/raw/reference/rag/rerankWithScorer.mdx +0 -1
- package/.docs/raw/reference/storage/cloudflare-d1.mdx +37 -0
- package/.docs/raw/reference/storage/convex.mdx +164 -0
- package/.docs/raw/reference/storage/lance.mdx +33 -0
- package/.docs/raw/reference/storage/libsql.mdx +37 -0
- package/.docs/raw/reference/storage/mongodb.mdx +39 -0
- package/.docs/raw/reference/storage/mssql.mdx +37 -0
- package/.docs/raw/reference/storage/postgresql.mdx +37 -0
- package/.docs/raw/reference/streaming/ChunkType.mdx +1 -1
- package/.docs/raw/reference/streaming/agents/stream.mdx +64 -2
- package/.docs/raw/reference/streaming/workflows/observeStream.mdx +7 -9
- package/.docs/raw/reference/streaming/workflows/{resumeStreamVNext.mdx → resumeStream.mdx} +51 -11
- package/.docs/raw/reference/streaming/workflows/stream.mdx +83 -24
- package/.docs/raw/reference/templates/overview.mdx +1 -4
- package/.docs/raw/reference/tools/client.mdx +1 -2
- package/.docs/raw/reference/tools/create-tool.mdx +132 -0
- package/.docs/raw/reference/tools/graph-rag-tool.mdx +5 -5
- package/.docs/raw/reference/tools/mcp-client.mdx +76 -21
- package/.docs/raw/reference/tools/mcp-server.mdx +1 -2
- package/.docs/raw/reference/tools/vector-query-tool.mdx +14 -15
- package/.docs/raw/reference/vectors/chroma.mdx +81 -1
- package/.docs/raw/reference/vectors/convex.mdx +429 -0
- package/.docs/raw/reference/vectors/couchbase.mdx +24 -17
- package/.docs/raw/reference/vectors/duckdb.mdx +462 -0
- package/.docs/raw/reference/vectors/elasticsearch.mdx +310 -0
- package/.docs/raw/reference/vectors/lance.mdx +38 -22
- package/.docs/raw/reference/vectors/libsql.mdx +35 -2
- package/.docs/raw/reference/vectors/mongodb.mdx +35 -2
- package/.docs/raw/reference/vectors/opensearch.mdx +37 -16
- package/.docs/raw/reference/vectors/pg.mdx +43 -36
- package/.docs/raw/reference/vectors/pinecone.mdx +48 -1
- package/.docs/raw/reference/vectors/qdrant.mdx +36 -1
- package/.docs/raw/reference/vectors/turbopuffer.mdx +74 -0
- package/.docs/raw/reference/voice/google.mdx +159 -20
- package/.docs/raw/reference/voice/openai-realtime.mdx +2 -2
- package/.docs/raw/reference/voice/voice.addInstructions.mdx +2 -3
- package/.docs/raw/reference/voice/voice.addTools.mdx +1 -1
- package/.docs/raw/reference/voice/voice.answer.mdx +1 -1
- package/.docs/raw/reference/voice/voice.close.mdx +1 -1
- package/.docs/raw/reference/voice/voice.connect.mdx +1 -1
- package/.docs/raw/reference/voice/voice.off.mdx +1 -1
- package/.docs/raw/reference/voice/voice.on.mdx +1 -1
- package/.docs/raw/reference/voice/voice.send.mdx +1 -1
- package/.docs/raw/reference/voice/voice.updateConfig.mdx +1 -1
- package/.docs/raw/reference/workflows/run-methods/restart.mdx +142 -0
- package/.docs/raw/reference/workflows/run-methods/resume.mdx +44 -0
- package/.docs/raw/reference/workflows/run-methods/start.mdx +44 -0
- package/.docs/raw/reference/workflows/run.mdx +13 -5
- package/.docs/raw/reference/workflows/step.mdx +13 -0
- package/.docs/raw/reference/workflows/workflow.mdx +19 -0
- package/.docs/raw/server-db/mastra-client.mdx +1 -2
- package/.docs/raw/server-db/mastra-server.mdx +30 -1
- package/.docs/raw/server-db/request-context.mdx +0 -1
- package/.docs/raw/server-db/storage.mdx +11 -0
- package/.docs/raw/streaming/overview.mdx +26 -15
- package/.docs/raw/streaming/tool-streaming.mdx +48 -5
- package/.docs/raw/streaming/workflow-streaming.mdx +5 -11
- package/.docs/raw/tools-mcp/advanced-usage.mdx +1 -2
- package/.docs/raw/tools-mcp/mcp-overview.mdx +3 -5
- package/.docs/raw/voice/overview.mdx +21 -41
- package/.docs/raw/voice/speech-to-speech.mdx +4 -4
- package/.docs/raw/voice/speech-to-text.mdx +1 -2
- package/.docs/raw/voice/text-to-speech.mdx +1 -2
- package/.docs/raw/workflows/control-flow.mdx +180 -0
- package/.docs/raw/workflows/error-handling.mdx +1 -0
- package/.docs/raw/workflows/human-in-the-loop.mdx +4 -4
- package/.docs/raw/workflows/overview.mdx +56 -44
- package/.docs/raw/workflows/snapshots.mdx +1 -0
- package/.docs/raw/workflows/suspend-and-resume.mdx +85 -16
- package/.docs/raw/workflows/time-travel.mdx +313 -0
- package/.docs/raw/workflows/workflow-state.mdx +191 -0
- package/CHANGELOG.md +18 -0
- package/dist/{chunk-5NJC7NRO.js → chunk-4CM2BQNP.js} +24 -4
- package/dist/prepare-docs/package-changes.d.ts.map +1 -1
- package/dist/prepare-docs/prepare.js +1 -1
- package/dist/stdio.js +1 -1
- package/package.json +7 -7
- package/.docs/raw/agents/human-in-the-loop-with-tools.mdx +0 -90
- package/.docs/raw/frameworks/agentic-uis/cedar-os.mdx +0 -102
- package/.docs/raw/frameworks/agentic-uis/openrouter.mdx +0 -179
- package/.docs/raw/frameworks/web-frameworks/next-js.mdx +0 -379
- package/.docs/raw/getting-started/quickstart.mdx +0 -27
- package/.docs/raw/getting-started/templates.mdx +0 -73
- package/.docs/raw/reference/streaming/workflows/observeStreamVNext.mdx +0 -47
- package/.docs/raw/reference/streaming/workflows/streamVNext.mdx +0 -153
- /package/.docs/raw/{frameworks/agentic-uis → guides/build-your-ui}/assistant-ui.mdx +0 -0
- /package/.docs/raw/{frameworks/agentic-uis → guides/build-your-ui}/copilotkit.mdx +0 -0
|
@@ -19,7 +19,7 @@ You'll learn how to create the agent and register it with Mastra. Next, you'll i
|
|
|
19
19
|
|
|
20
20
|
- Node.js `v22.13.0` or later installed
|
|
21
21
|
- An API key from a supported [Model Provider](/models/v1)
|
|
22
|
-
- An existing Mastra project (Follow the [installation guide](/guides/v1/
|
|
22
|
+
- An existing Mastra project (Follow the [installation guide](/guides/v1/getting-started/quickstart) to set up a new project)
|
|
23
23
|
|
|
24
24
|
## Creating the Agent
|
|
25
25
|
|
|
@@ -32,7 +32,6 @@ To create an agent in Mastra use the `Agent` class to define it and then registe
|
|
|
32
32
|
Create a new file `src/mastra/agents/chefAgent.ts` and define your agent:
|
|
33
33
|
|
|
34
34
|
```ts copy title="src/mastra/agents/chefAgent.ts"
|
|
35
|
-
import { openai } from "@ai-sdk/openai";
|
|
36
35
|
import { Agent } from "@mastra/core/agent";
|
|
37
36
|
|
|
38
37
|
export const chefAgent = new Agent({
|
|
@@ -41,7 +40,7 @@ export const chefAgent = new Agent({
|
|
|
41
40
|
instructions:
|
|
42
41
|
"You are Michel, a practical and experienced home chef" +
|
|
43
42
|
"You help people cook with whatever ingredients they have available.",
|
|
44
|
-
model: openai
|
|
43
|
+
model: "openai/gpt-5.1",
|
|
45
44
|
});
|
|
46
45
|
```
|
|
47
46
|
|
|
@@ -18,7 +18,7 @@ In this guide, you'll learn how to build a complete MCP (Model Context Protocol)
|
|
|
18
18
|
|
|
19
19
|
- Node.js `v22.13.0` or later installed
|
|
20
20
|
- An API key from a supported [Model Provider](/models/v1)
|
|
21
|
-
- An existing Mastra project (Follow the [installation guide](/guides/v1/
|
|
21
|
+
- An existing Mastra project (Follow the [installation guide](/guides/v1/getting-started/quickstart) to set up a new project)
|
|
22
22
|
|
|
23
23
|
## Adding necessary dependencies & files
|
|
24
24
|
|
|
@@ -38,7 +38,7 @@ npm install @mastra/mcp@beta
|
|
|
38
38
|
|
|
39
39
|
<StepItem>
|
|
40
40
|
|
|
41
|
-
After following the default [installation guide](/guides/v1/
|
|
41
|
+
After following the default [installation guide](/guides/v1/getting-started/quickstart) your project will include files that are not relevant for this guide. You can safely remove them:
|
|
42
42
|
|
|
43
43
|
```bash copy
|
|
44
44
|
rm -rf src/mastra/agents src/mastra/workflows src/mastra/tools/weather-tool.ts
|
|
@@ -16,7 +16,7 @@ You'll use the foundational Transformer paper ["Attention Is All You Need"](http
|
|
|
16
16
|
|
|
17
17
|
- Node.js `v22.13.0` or later installed
|
|
18
18
|
- An API key from a supported [Model Provider](/models/v1)
|
|
19
|
-
- An existing Mastra project (Follow the [installation guide](/guides/v1/
|
|
19
|
+
- An existing Mastra project (Follow the [installation guide](/guides/v1/getting-started/quickstart) to set up a new project)
|
|
20
20
|
|
|
21
21
|
## How RAG works
|
|
22
22
|
|
|
@@ -57,7 +57,7 @@ Let's define the agent's behavior, connect it to your Mastra project, and create
|
|
|
57
57
|
|
|
58
58
|
Install additional dependencies
|
|
59
59
|
|
|
60
|
-
After running the [installation guide](/guides/v1/
|
|
60
|
+
After running the [installation guide](/guides/v1/getting-started/quickstart) you'll need to install additional dependencies:
|
|
61
61
|
|
|
62
62
|
```bash copy
|
|
63
63
|
npm install @mastra/rag@beta ai@^4.0.0
|
|
@@ -76,14 +76,14 @@ Create a new file `src/mastra/agents/researchAgent.ts` and define your agent:
|
|
|
76
76
|
|
|
77
77
|
```ts copy title="src/mastra/agents/researchAgent.ts"
|
|
78
78
|
import { Agent } from "@mastra/core/agent";
|
|
79
|
-
import {
|
|
79
|
+
import { ModelRouterEmbeddingModel } from "@mastra/core/llm";
|
|
80
80
|
import { createVectorQueryTool } from "@mastra/rag";
|
|
81
81
|
|
|
82
82
|
// Create a tool for semantic search over the paper embeddings
|
|
83
83
|
const vectorQueryTool = createVectorQueryTool({
|
|
84
84
|
vectorStoreName: "libSqlVector",
|
|
85
85
|
indexName: "papers",
|
|
86
|
-
model:
|
|
86
|
+
model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
|
|
87
87
|
});
|
|
88
88
|
|
|
89
89
|
export const researchAgent = new Agent({
|
|
@@ -94,7 +94,7 @@ export const researchAgent = new Agent({
|
|
|
94
94
|
and provide accurate, well-supported answers based on the retrieved content.
|
|
95
95
|
Focus on the specific content available in the tool and acknowledge if you cannot find sufficient information to answer a question.
|
|
96
96
|
Base your responses only on the content provided, not on general knowledge.`,
|
|
97
|
-
model: openai
|
|
97
|
+
model: "openai/gpt-5.1",
|
|
98
98
|
tools: {
|
|
99
99
|
vectorQueryTool,
|
|
100
100
|
},
|
|
@@ -229,9 +229,8 @@ This allows the agent to efficiently search and retrieve relevant information.
|
|
|
229
229
|
|
|
230
230
|
Open the `src/store.ts` file and add the following:
|
|
231
231
|
|
|
232
|
-
```ts copy title="src/store.ts"
|
|
232
|
+
```ts copy title="src/store.ts"
|
|
233
233
|
import { MDocument } from "@mastra/rag";
|
|
234
|
-
import { openai } from "@ai-sdk/openai";
|
|
235
234
|
import { embedMany } from "ai";
|
|
236
235
|
import { mastra } from "./mastra";
|
|
237
236
|
|
|
@@ -251,7 +250,7 @@ const chunks = await doc.chunk({
|
|
|
251
250
|
|
|
252
251
|
// Generate embeddings
|
|
253
252
|
const { embeddings } = await embedMany({
|
|
254
|
-
model:
|
|
253
|
+
model: new ModelRouterEmbeddingModel("openai/text-embedding-3-small"),
|
|
255
254
|
values: chunks.map((chunk) => chunk.text),
|
|
256
255
|
});
|
|
257
256
|
|
|
@@ -17,7 +17,7 @@ In this guide, you're going to create a simple agent that fetches the last day's
|
|
|
17
17
|
|
|
18
18
|
- Node.js `v22.13.0` or later installed
|
|
19
19
|
- An API key from a supported [Model Provider](/models/v1)
|
|
20
|
-
- An existing Mastra project (Follow the [installation guide](/guides/v1/
|
|
20
|
+
- An existing Mastra project (Follow the [installation guide](/guides/v1/getting-started/quickstart) to set up a new project)
|
|
21
21
|
|
|
22
22
|
## Creating the Agent
|
|
23
23
|
|
|
@@ -30,7 +30,6 @@ To create an agent in Mastra use the `Agent` class to define it and then registe
|
|
|
30
30
|
Create a new file `src/mastra/agents/stockAgent.ts` and define your agent:
|
|
31
31
|
|
|
32
32
|
```ts copy title="src/mastra/agents/stockAgent.ts"
|
|
33
|
-
import { openai } from "@ai-sdk/openai";
|
|
34
33
|
import { Agent } from "@mastra/core/agent";
|
|
35
34
|
|
|
36
35
|
export const stockAgent = new Agent({
|
|
@@ -38,7 +37,7 @@ export const stockAgent = new Agent({
|
|
|
38
37
|
name: "Stock Agent",
|
|
39
38
|
instructions:
|
|
40
39
|
"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.",
|
|
41
|
-
model: openai
|
|
40
|
+
model: "openai/gpt-5.1",
|
|
42
41
|
});
|
|
43
42
|
```
|
|
44
43
|
|
|
@@ -104,8 +103,7 @@ export const stockPrices = createTool({
|
|
|
104
103
|
|
|
105
104
|
Inside `src/mastra/agents/stockAgent.ts` import your newly created `stockPrices` tool and add it to the agent.
|
|
106
105
|
|
|
107
|
-
```ts copy title="src/mastra/agents/stockAgent.ts" {
|
|
108
|
-
import { openai } from "@ai-sdk/openai";
|
|
106
|
+
```ts copy title="src/mastra/agents/stockAgent.ts" {9-11}
|
|
109
107
|
import { Agent } from "@mastra/core/agent";
|
|
110
108
|
import { stockPrices } from "../tools/stockPrices";
|
|
111
109
|
|
|
@@ -114,7 +112,7 @@ export const stockAgent = new Agent({
|
|
|
114
112
|
name: "Stock Agent",
|
|
115
113
|
instructions:
|
|
116
114
|
"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.",
|
|
117
|
-
model: openai
|
|
115
|
+
model: "openai/gpt-5.1",
|
|
118
116
|
tools: {
|
|
119
117
|
stockPrices,
|
|
120
118
|
},
|
|
@@ -19,7 +19,7 @@ When building a web search agent, you have two main strategies to consider:
|
|
|
19
19
|
|
|
20
20
|
- Node.js `v22.13.0` or later installed
|
|
21
21
|
- An API key from a supported [Model Provider](/models/v1)
|
|
22
|
-
- An existing Mastra project (Follow the [installation guide](/guides/v1/
|
|
22
|
+
- An existing Mastra project (Follow the [installation guide](/guides/v1/getting-started/quickstart) to set up a new project)
|
|
23
23
|
|
|
24
24
|
## Using native search tools
|
|
25
25
|
|
|
@@ -61,7 +61,7 @@ Create a new file `src/mastra/agents/searchAgent.ts` and define your agent:
|
|
|
61
61
|
name: "Search Agent",
|
|
62
62
|
instructions:
|
|
63
63
|
"You are a search agent that can search the web for information.",
|
|
64
|
-
model:
|
|
64
|
+
model: "openai/gpt-5.1",
|
|
65
65
|
});
|
|
66
66
|
```
|
|
67
67
|
|
|
@@ -76,7 +76,7 @@ Create a new file `src/mastra/agents/searchAgent.ts` and define your agent:
|
|
|
76
76
|
name: "Search Agent",
|
|
77
77
|
instructions:
|
|
78
78
|
"You are a search agent that can search the web for information.",
|
|
79
|
-
model:
|
|
79
|
+
model: "google/gemini-2.5-flash",
|
|
80
80
|
});
|
|
81
81
|
```
|
|
82
82
|
|
|
@@ -93,7 +93,7 @@ Setup the tool:
|
|
|
93
93
|
<Tabs>
|
|
94
94
|
<TabItem value="openai" label="Open AI">
|
|
95
95
|
|
|
96
|
-
```ts copy title="src/mastra/agents/searchAgent.ts" {9-
|
|
96
|
+
```ts copy title="src/mastra/agents/searchAgent.ts" {9-12}
|
|
97
97
|
import { openai } from "@ai-sdk/openai";
|
|
98
98
|
import { Agent } from "@mastra/core/agent";
|
|
99
99
|
|
|
@@ -102,7 +102,7 @@ Setup the tool:
|
|
|
102
102
|
name: "Search Agent",
|
|
103
103
|
instructions:
|
|
104
104
|
"You are a search agent that can search the web for information.",
|
|
105
|
-
model:
|
|
105
|
+
model: "openai/gpt-5.1",
|
|
106
106
|
tools: {
|
|
107
107
|
webSearch: openai.tools.webSearch()
|
|
108
108
|
}
|
|
@@ -112,7 +112,7 @@ Setup the tool:
|
|
|
112
112
|
</TabItem>
|
|
113
113
|
<TabItem value="gemini" label="Gemini">
|
|
114
114
|
|
|
115
|
-
```ts copy title="src/mastra/agents/searchAgent.ts" {9-
|
|
115
|
+
```ts copy title="src/mastra/agents/searchAgent.ts" {9-14}
|
|
116
116
|
import { google } from "@ai-sdk/google";
|
|
117
117
|
import { Agent } from "@mastra/core/agent";
|
|
118
118
|
|
|
@@ -121,9 +121,11 @@ Setup the tool:
|
|
|
121
121
|
name: "Search Agent",
|
|
122
122
|
instructions:
|
|
123
123
|
"You are a search agent that can search the web for information.",
|
|
124
|
-
model:
|
|
124
|
+
model: "google/gemini-2.5-flash",
|
|
125
125
|
tools: {
|
|
126
|
-
|
|
126
|
+
webSearch: google.tools.googleSearch({
|
|
127
|
+
mode: "MODE_DYNAMIC"
|
|
128
|
+
})
|
|
127
129
|
}
|
|
128
130
|
});
|
|
129
131
|
```
|
|
@@ -191,7 +193,7 @@ export const searchAgent = new Agent({
|
|
|
191
193
|
name: "Search Agent",
|
|
192
194
|
instructions:
|
|
193
195
|
"You are a search agent that can search the web for information.",
|
|
194
|
-
model: "openai/gpt-
|
|
196
|
+
model: "openai/gpt-5.1",
|
|
195
197
|
});
|
|
196
198
|
```
|
|
197
199
|
|
|
@@ -252,7 +254,7 @@ export const searchAgent = new Agent({
|
|
|
252
254
|
name: "Search Agent",
|
|
253
255
|
instructions:
|
|
254
256
|
"You are a search agent that can search the web for information.",
|
|
255
|
-
model: "openai/gpt-
|
|
257
|
+
model: "openai/gpt-5.1",
|
|
256
258
|
tools: {
|
|
257
259
|
webSearch,
|
|
258
260
|
},
|
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "WhatsApp Chat Bot"
|
|
3
|
+
description: Create a WhatsApp chat bot using Mastra agents and workflows to handle incoming messages and respond naturally via text messages.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# WhatsApp Chat Bot
|
|
7
|
+
|
|
8
|
+
This guide demonstrates how to create a WhatsApp chat bot using Mastra agents and workflows. The bot receives incoming WhatsApp messages via webhook, processes them through an AI agent, breaks responses into natural text messages, and sends them back via the WhatsApp Business API.
|
|
9
|
+
|
|
10
|
+
## Prerequisites
|
|
11
|
+
|
|
12
|
+
This example requires a WhatsApp Business API setup and uses the `anthropic` model. Add these environment variables to your `.env` file:
|
|
13
|
+
|
|
14
|
+
```bash title=".env" copy
|
|
15
|
+
ANTHROPIC_API_KEY=<your-anthropic-api-key>
|
|
16
|
+
WHATSAPP_VERIFY_TOKEN=<your-verify-token>
|
|
17
|
+
WHATSAPP_ACCESS_TOKEN=<your-whatsapp-access-token>
|
|
18
|
+
WHATSAPP_BUSINESS_PHONE_NUMBER_ID=<your-phone-number-id>
|
|
19
|
+
WHATSAPP_API_VERSION=v22.0
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Creating the WhatsApp client
|
|
23
|
+
|
|
24
|
+
This client handles sending messages to users via the WhatsApp Business API.
|
|
25
|
+
|
|
26
|
+
```typescript title="src/whatsapp-client.ts" showLineNumbers copy
|
|
27
|
+
// Simple WhatsApp Business API client for sending messages
|
|
28
|
+
|
|
29
|
+
interface SendMessageParams {
|
|
30
|
+
to: string;
|
|
31
|
+
message: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function sendWhatsAppMessage({ to, message }: SendMessageParams) {
|
|
35
|
+
// Get environment variables for WhatsApp API
|
|
36
|
+
const apiVersion = process.env.WHATSAPP_API_VERSION || "v22.0";
|
|
37
|
+
const phoneNumberId = process.env.WHATSAPP_BUSINESS_PHONE_NUMBER_ID;
|
|
38
|
+
const accessToken = process.env.WHATSAPP_ACCESS_TOKEN;
|
|
39
|
+
|
|
40
|
+
// Check if required environment variables are set
|
|
41
|
+
if (!phoneNumberId || !accessToken) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// WhatsApp Business API endpoint
|
|
46
|
+
const url = `https://graph.facebook.com/${apiVersion}/${phoneNumberId}/messages`;
|
|
47
|
+
|
|
48
|
+
// Message payload following WhatsApp API format
|
|
49
|
+
const payload = {
|
|
50
|
+
messaging_product: "whatsapp",
|
|
51
|
+
recipient_type: "individual",
|
|
52
|
+
to: to,
|
|
53
|
+
type: "text",
|
|
54
|
+
text: {
|
|
55
|
+
body: message,
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
// Send message via WhatsApp Business API
|
|
61
|
+
const response = await fetch(url, {
|
|
62
|
+
method: "POST",
|
|
63
|
+
headers: {
|
|
64
|
+
"Content-Type": "application/json",
|
|
65
|
+
Authorization: `Bearer ${accessToken}`,
|
|
66
|
+
},
|
|
67
|
+
body: JSON.stringify(payload),
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const result = await response.json();
|
|
71
|
+
|
|
72
|
+
if (response.ok) {
|
|
73
|
+
console.log(`✅ WhatsApp message sent to ${to}: "${message}"`);
|
|
74
|
+
return true;
|
|
75
|
+
} else {
|
|
76
|
+
console.error("❌ Failed to send WhatsApp message:", result);
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
} catch (error) {
|
|
80
|
+
console.error("❌ Error sending WhatsApp message:", error);
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Creating the chat agent
|
|
87
|
+
|
|
88
|
+
This agent handles the main conversation logic with a friendly, conversational personality.
|
|
89
|
+
|
|
90
|
+
```typescript title="src/mastra/agents/chat-agent.ts" showLineNumbers copy
|
|
91
|
+
import { Agent } from "@mastra/core/agent";
|
|
92
|
+
import { Memory } from "@mastra/memory";
|
|
93
|
+
import { LibSQLStore } from "@mastra/libsql";
|
|
94
|
+
|
|
95
|
+
export const chatAgent = new Agent({
|
|
96
|
+
id: "chat-agent",
|
|
97
|
+
name: "Chat Agent",
|
|
98
|
+
instructions: `
|
|
99
|
+
You are a helpful, friendly, and knowledgeable AI assistant that loves to chat with users via WhatsApp.
|
|
100
|
+
|
|
101
|
+
Your personality:
|
|
102
|
+
- Warm, approachable, and conversational
|
|
103
|
+
- Enthusiastic about helping with any topic
|
|
104
|
+
- Use a casual, friendly tone like you're chatting with a friend
|
|
105
|
+
- Be concise but informative
|
|
106
|
+
- Show genuine interest in the user's questions
|
|
107
|
+
|
|
108
|
+
Your capabilities:
|
|
109
|
+
- Answer questions on a wide variety of topics
|
|
110
|
+
- Provide helpful advice and suggestions
|
|
111
|
+
- Engage in casual conversation
|
|
112
|
+
- Help with problem-solving and creative tasks
|
|
113
|
+
- Explain complex topics in simple terms
|
|
114
|
+
|
|
115
|
+
Guidelines:
|
|
116
|
+
- Keep responses informative but not overwhelming
|
|
117
|
+
- Ask follow-up questions when appropriate
|
|
118
|
+
- Be encouraging and positive
|
|
119
|
+
- If you don't know something, admit it honestly
|
|
120
|
+
- Adapt your communication style to match the user's tone
|
|
121
|
+
- Remember this is WhatsApp, so keep it conversational and natural
|
|
122
|
+
|
|
123
|
+
Always aim to be helpful while maintaining a friendly, approachable conversation style.
|
|
124
|
+
`,
|
|
125
|
+
model: "anthropic/claude-4-sonnet-20250514",
|
|
126
|
+
memory: new Memory({
|
|
127
|
+
storage: new LibSQLStore({
|
|
128
|
+
id: 'agent-storage',
|
|
129
|
+
url: "file:../mastra.db",
|
|
130
|
+
}),
|
|
131
|
+
}),
|
|
132
|
+
});
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Creating the text message agent
|
|
136
|
+
|
|
137
|
+
This agent converts longer responses into natural, bite-sized text messages suitable for WhatsApp.
|
|
138
|
+
|
|
139
|
+
```typescript title="src/mastra/agents/text-message-agent.ts" showLineNumbers copy
|
|
140
|
+
import { Agent } from "@mastra/core/agent";
|
|
141
|
+
import { Memory } from "@mastra/memory";
|
|
142
|
+
import { LibSQLStore } from "@mastra/libsql";
|
|
143
|
+
|
|
144
|
+
export const textMessageAgent = new Agent({
|
|
145
|
+
id: "text-message-agent",
|
|
146
|
+
name: "Text Message Agent",
|
|
147
|
+
instructions: `
|
|
148
|
+
You are a text message converter that takes formal or lengthy text and breaks it down into natural, casual text messages.
|
|
149
|
+
|
|
150
|
+
Your job is to:
|
|
151
|
+
- Convert any input text into 5-8 short, casual text messages
|
|
152
|
+
- Each message should be 1-2 sentences maximum
|
|
153
|
+
- Use natural, friendly texting language (contractions, casual tone)
|
|
154
|
+
- Maintain all the important information from the original text
|
|
155
|
+
- Make it feel like you're texting a friend
|
|
156
|
+
- Use appropriate emojis sparingly to add personality
|
|
157
|
+
- Keep the conversational flow logical and easy to follow
|
|
158
|
+
|
|
159
|
+
Think of it like you're explaining something exciting to a friend via text - break it into bite-sized, engaging messages that don't overwhelm them with a long paragraph.
|
|
160
|
+
|
|
161
|
+
Always return exactly 5-8 messages in the messages array.
|
|
162
|
+
`,
|
|
163
|
+
model: "anthropic/claude-4-sonnet-20250514",
|
|
164
|
+
memory: new Memory({
|
|
165
|
+
storage: new LibSQLStore({
|
|
166
|
+
id: 'agent-storage',
|
|
167
|
+
url: "file:../mastra.db",
|
|
168
|
+
}),
|
|
169
|
+
}),
|
|
170
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Creating the chat workflow
|
|
174
|
+
|
|
175
|
+
This workflow orchestrates the entire chat process: generating a response, breaking it into messages, and sending them via WhatsApp.
|
|
176
|
+
|
|
177
|
+
```typescript title="src/mastra/workflows/chat-workflow.ts" showLineNumbers copy
|
|
178
|
+
import { createStep, createWorkflow } from "@mastra/core/workflows";
|
|
179
|
+
import { z } from "zod";
|
|
180
|
+
import { sendWhatsAppMessage } from "../../whatsapp-client";
|
|
181
|
+
|
|
182
|
+
const respondToMessage = createStep({
|
|
183
|
+
id: "respond-to-message",
|
|
184
|
+
description: "Generate response to user message",
|
|
185
|
+
inputSchema: z.object({ userMessage: z.string() }),
|
|
186
|
+
outputSchema: z.object({ response: z.string() }),
|
|
187
|
+
execute: async ({ inputData, mastra }) => {
|
|
188
|
+
const agent = mastra?.getAgent("chatAgent");
|
|
189
|
+
if (!agent) {
|
|
190
|
+
throw new Error("Chat agent not found");
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const response = await agent.generate([
|
|
194
|
+
{ role: "user", content: inputData.userMessage },
|
|
195
|
+
]);
|
|
196
|
+
|
|
197
|
+
return { response: response.text };
|
|
198
|
+
},
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
const breakIntoMessages = createStep({
|
|
202
|
+
id: "break-into-messages",
|
|
203
|
+
description: "Breaks response into text messages",
|
|
204
|
+
inputSchema: z.object({ prompt: z.string() }),
|
|
205
|
+
outputSchema: z.object({ messages: z.array(z.string()) }),
|
|
206
|
+
execute: async ({ inputData, mastra }) => {
|
|
207
|
+
const agent = mastra?.getAgent("textMessageAgent");
|
|
208
|
+
if (!agent) {
|
|
209
|
+
throw new Error("Text Message agent not found");
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const response = await agent.generate(
|
|
213
|
+
[{ role: "user", content: inputData.prompt }],
|
|
214
|
+
{
|
|
215
|
+
structuredOutput: {
|
|
216
|
+
schema: z.object({
|
|
217
|
+
messages: z.array(z.string()),
|
|
218
|
+
}),
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
if (!response.object) throw new Error("Error generating messages");
|
|
224
|
+
|
|
225
|
+
return response.object;
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
const sendMessages = createStep({
|
|
230
|
+
id: "send-messages",
|
|
231
|
+
description: "Sends text messages via WhatsApp",
|
|
232
|
+
inputSchema: z.object({
|
|
233
|
+
messages: z.array(z.string()),
|
|
234
|
+
userPhone: z.string(),
|
|
235
|
+
}),
|
|
236
|
+
outputSchema: z.object({ sentCount: z.number() }),
|
|
237
|
+
execute: async ({ inputData }) => {
|
|
238
|
+
const { messages, userPhone } = inputData;
|
|
239
|
+
|
|
240
|
+
console.log(
|
|
241
|
+
`\n🔥 Sending ${messages.length} WhatsApp messages to ${userPhone}...`,
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
let sentCount = 0;
|
|
245
|
+
|
|
246
|
+
// Send each message with a small delay for natural flow
|
|
247
|
+
for (let i = 0; i < messages.length; i++) {
|
|
248
|
+
const success = await sendWhatsAppMessage({
|
|
249
|
+
to: userPhone,
|
|
250
|
+
message: messages[i],
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
if (success) {
|
|
254
|
+
sentCount++;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Add delay between messages for natural texting rhythm
|
|
258
|
+
if (i < messages.length - 1) {
|
|
259
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
console.log(
|
|
264
|
+
`\n✅ Successfully sent ${sentCount}/${messages.length} WhatsApp messages\n`,
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
return { sentCount };
|
|
268
|
+
},
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
export const chatWorkflow = createWorkflow({
|
|
272
|
+
id: "chat-workflow",
|
|
273
|
+
inputSchema: z.object({ userMessage: z.string() }),
|
|
274
|
+
outputSchema: z.object({ sentCount: z.number() }),
|
|
275
|
+
})
|
|
276
|
+
.then(respondToMessage)
|
|
277
|
+
.map(async ({ inputData }) => ({
|
|
278
|
+
prompt: `Break this AI response into 3-8 casual, friendly text messages that feel natural for WhatsApp conversation:\n\n${inputData.response}`,
|
|
279
|
+
}))
|
|
280
|
+
.then(breakIntoMessages)
|
|
281
|
+
.map(async ({ inputData, getInitData }) => {
|
|
282
|
+
// Parse the original stringified input to get user phone
|
|
283
|
+
const initData = getInitData();
|
|
284
|
+
const webhookData = JSON.parse(initData.userMessage);
|
|
285
|
+
const userPhone =
|
|
286
|
+
webhookData.entry?.[0]?.changes?.[0]?.value?.messages?.[0]?.from ||
|
|
287
|
+
"unknown";
|
|
288
|
+
|
|
289
|
+
return {
|
|
290
|
+
messages: inputData.messages,
|
|
291
|
+
userPhone,
|
|
292
|
+
};
|
|
293
|
+
})
|
|
294
|
+
.then(sendMessages);
|
|
295
|
+
|
|
296
|
+
chatWorkflow.commit();
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
## Setting up Mastra configuration
|
|
300
|
+
|
|
301
|
+
Configure your Mastra instance with the agents, workflow, and WhatsApp webhook endpoints.
|
|
302
|
+
|
|
303
|
+
```typescript title="src/mastra/index.ts" showLineNumbers copy
|
|
304
|
+
import { Mastra } from "@mastra/core";
|
|
305
|
+
import { registerApiRoute } from "@mastra/core/server";
|
|
306
|
+
import { PinoLogger } from "@mastra/loggers";
|
|
307
|
+
import { LibSQLStore } from "@mastra/libsql";
|
|
308
|
+
|
|
309
|
+
import { chatWorkflow } from "./workflows/chat-workflow";
|
|
310
|
+
import { textMessageAgent } from "./agents/text-message-agent";
|
|
311
|
+
import { chatAgent } from "./agents/chat-agent";
|
|
312
|
+
|
|
313
|
+
export const mastra = new Mastra({
|
|
314
|
+
workflows: { chatWorkflow },
|
|
315
|
+
agents: { textMessageAgent, chatAgent },
|
|
316
|
+
storage: new LibSQLStore({
|
|
317
|
+
id: 'agent-storage',
|
|
318
|
+
url: ":memory:",
|
|
319
|
+
}),
|
|
320
|
+
logger: new PinoLogger({
|
|
321
|
+
name: "Mastra",
|
|
322
|
+
level: "info",
|
|
323
|
+
}),
|
|
324
|
+
server: {
|
|
325
|
+
apiRoutes: [
|
|
326
|
+
registerApiRoute("/whatsapp", {
|
|
327
|
+
method: "GET",
|
|
328
|
+
handler: async (c) => {
|
|
329
|
+
const verifyToken = process.env.WHATSAPP_VERIFY_TOKEN;
|
|
330
|
+
const {
|
|
331
|
+
"hub.mode": mode,
|
|
332
|
+
"hub.challenge": challenge,
|
|
333
|
+
"hub.verify_token": token,
|
|
334
|
+
} = c.req.query();
|
|
335
|
+
|
|
336
|
+
if (mode === "subscribe" && token === verifyToken) {
|
|
337
|
+
return c.text(challenge, 200);
|
|
338
|
+
} else {
|
|
339
|
+
return c.status(403);
|
|
340
|
+
}
|
|
341
|
+
},
|
|
342
|
+
}),
|
|
343
|
+
registerApiRoute("/whatsapp", {
|
|
344
|
+
method: "POST",
|
|
345
|
+
handler: async (c) => {
|
|
346
|
+
const mastra = c.get("mastra");
|
|
347
|
+
const chatWorkflow = mastra.getWorkflow("chatWorkflow");
|
|
348
|
+
|
|
349
|
+
const body = await c.req.json();
|
|
350
|
+
|
|
351
|
+
const workflowRun = await chatWorkflow.createRun();
|
|
352
|
+
const runResult = await workflowRun.start({
|
|
353
|
+
inputData: { userMessage: JSON.stringify(body) },
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
return c.json(runResult);
|
|
357
|
+
},
|
|
358
|
+
}),
|
|
359
|
+
],
|
|
360
|
+
},
|
|
361
|
+
});
|
|
362
|
+
```
|
|
363
|
+
|
|
364
|
+
## Testing the chat bot
|
|
365
|
+
|
|
366
|
+
You can test the chat bot locally by simulating a WhatsApp webhook payload.
|
|
367
|
+
|
|
368
|
+
```typescript title="src/test-whatsapp-bot.ts" showLineNumbers copy
|
|
369
|
+
import "dotenv/config";
|
|
370
|
+
|
|
371
|
+
import { mastra } from "./mastra";
|
|
372
|
+
|
|
373
|
+
// Simulate a WhatsApp webhook payload
|
|
374
|
+
const mockWebhookData = {
|
|
375
|
+
entry: [
|
|
376
|
+
{
|
|
377
|
+
changes: [
|
|
378
|
+
{
|
|
379
|
+
value: {
|
|
380
|
+
messages: [
|
|
381
|
+
{
|
|
382
|
+
from: "1234567890", // Test phone number
|
|
383
|
+
text: {
|
|
384
|
+
body: "Hello! How are you today?",
|
|
385
|
+
},
|
|
386
|
+
},
|
|
387
|
+
],
|
|
388
|
+
},
|
|
389
|
+
},
|
|
390
|
+
],
|
|
391
|
+
},
|
|
392
|
+
],
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
const workflow = mastra.getWorkflow("chatWorkflow");
|
|
396
|
+
const workflowRun = await workflow.createRun();
|
|
397
|
+
|
|
398
|
+
const result = await workflowRun.start({
|
|
399
|
+
inputData: { userMessage: JSON.stringify(mockWebhookData) },
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
console.log("Workflow completed:", result);
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
## Example output
|
|
406
|
+
|
|
407
|
+
When a user sends "Hello! How are you today?" to your WhatsApp bot, it might respond with multiple messages like:
|
|
408
|
+
|
|
409
|
+
```text
|
|
410
|
+
Hey there! 👋 I'm doing great, thanks for asking!
|
|
411
|
+
|
|
412
|
+
How's your day going so far?
|
|
413
|
+
|
|
414
|
+
I'm here and ready to chat about whatever's on your mind
|
|
415
|
+
|
|
416
|
+
Whether you need help with something or just want to talk, I'm all ears! 😊
|
|
417
|
+
|
|
418
|
+
What's new with you?
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
The bot maintains conversation context through memory and delivers responses that feel natural for WhatsApp messaging.
|