@mastra/mcp-docs-server 1.0.0-beta.5 → 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/%40mastra%2Fagent-builder.md +9 -9
- package/.docs/organized/changelogs/%40mastra%2Fai-sdk.md +67 -67
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +53 -53
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +27 -27
- package/.docs/organized/changelogs/%40mastra%2Fconvex.md +29 -0
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +274 -274
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +15 -15
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +12 -12
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +65 -65
- package/.docs/organized/changelogs/%40mastra%2Fduckdb.md +42 -0
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Felasticsearch.md +52 -0
- package/.docs/organized/changelogs/%40mastra%2Fevals.md +12 -12
- package/.docs/organized/changelogs/%40mastra%2Flance.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +24 -24
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +9 -9
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +84 -84
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +36 -36
- package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fmssql.md +27 -27
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +28 -28
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +47 -47
- package/.docs/organized/changelogs/%40mastra%2Frag.md +43 -43
- package/.docs/organized/changelogs/%40mastra%2Freact.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fschema-compat.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +56 -56
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google.md +19 -19
- package/.docs/organized/changelogs/create-mastra.md +9 -9
- package/.docs/organized/changelogs/mastra.md +17 -17
- 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/server-app-access.md +342 -0
- package/.docs/raw/agents/agent-approval.mdx +189 -0
- package/.docs/raw/agents/guardrails.mdx +13 -9
- package/.docs/raw/agents/networks.mdx +1 -0
- package/.docs/raw/agents/overview.mdx +23 -58
- package/.docs/raw/agents/processors.mdx +279 -0
- package/.docs/raw/deployment/cloud-providers/index.mdx +19 -26
- package/.docs/raw/deployment/cloud-providers/netlify-deployer.mdx +44 -13
- package/.docs/raw/evals/running-in-ci.mdx +0 -2
- package/.docs/raw/{guides/getting-started → getting-started}/manual-install.mdx +2 -2
- package/.docs/raw/getting-started/start.mdx +1 -1
- package/.docs/raw/guides/build-your-ui/ai-sdk-ui.mdx +8 -0
- package/.docs/raw/guides/getting-started/quickstart.mdx +1 -1
- package/.docs/raw/guides/guide/whatsapp-chat-bot.mdx +421 -0
- package/.docs/raw/guides/index.mdx +3 -35
- package/.docs/raw/guides/migrations/upgrade-to-v1/agent.mdx +11 -0
- package/.docs/raw/guides/migrations/upgrade-to-v1/workflows.mdx +29 -0
- package/.docs/raw/index.mdx +1 -1
- package/.docs/raw/memory/memory-processors.mdx +265 -79
- package/.docs/raw/memory/working-memory.mdx +10 -2
- package/.docs/raw/observability/overview.mdx +0 -1
- 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 +5 -4
- package/.docs/raw/observability/tracing/overview.mdx +71 -6
- package/.docs/raw/observability/tracing/processors/sensitive-data-filter.mdx +0 -1
- package/.docs/raw/rag/retrieval.mdx +23 -6
- package/.docs/raw/rag/vector-databases.mdx +93 -2
- package/.docs/raw/reference/agents/generate.mdx +55 -6
- package/.docs/raw/reference/agents/network.mdx +44 -0
- package/.docs/raw/reference/client-js/memory.mdx +43 -0
- package/.docs/raw/reference/client-js/workflows.mdx +92 -63
- package/.docs/raw/reference/deployer/netlify.mdx +1 -2
- package/.docs/raw/reference/evals/scorer-utils.mdx +362 -0
- package/.docs/raw/reference/index.mdx +1 -0
- 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/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/language-detector.mdx +9 -2
- package/.docs/raw/reference/processors/message-history-processor.mdx +131 -0
- package/.docs/raw/reference/processors/moderation-processor.mdx +10 -3
- package/.docs/raw/reference/processors/pii-detector.mdx +10 -3
- package/.docs/raw/reference/processors/processor-interface.mdx +502 -0
- package/.docs/raw/reference/processors/prompt-injection-detector.mdx +9 -2
- package/.docs/raw/reference/processors/semantic-recall-processor.mdx +197 -0
- package/.docs/raw/reference/processors/system-prompt-scrubber.mdx +2 -2
- package/.docs/raw/reference/processors/tool-call-filter.mdx +125 -0
- package/.docs/raw/reference/processors/working-memory-processor.mdx +221 -0
- 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 +56 -1
- 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/tools/mcp-client.mdx +74 -17
- package/.docs/raw/reference/vectors/convex.mdx +429 -0
- package/.docs/raw/reference/vectors/duckdb.mdx +462 -0
- package/.docs/raw/reference/vectors/elasticsearch.mdx +310 -0
- package/.docs/raw/reference/voice/google.mdx +159 -20
- 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-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 +6 -6
- package/.docs/raw/streaming/tool-streaming.mdx +2 -2
- package/.docs/raw/streaming/workflow-streaming.mdx +5 -11
- 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 +8 -0
- package/package.json +4 -4
- package/.docs/raw/agents/human-in-the-loop-with-tools.mdx +0 -91
- package/.docs/raw/reference/streaming/workflows/observeStreamVNext.mdx +0 -47
- package/.docs/raw/reference/streaming/workflows/streamVNext.mdx +0 -153
|
@@ -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.
|
|
@@ -1,43 +1,11 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: "Overview"
|
|
3
3
|
description: "Guides on building with Mastra"
|
|
4
|
+
showCopyButton: false
|
|
4
5
|
---
|
|
5
6
|
|
|
6
7
|
import { CardGrid, CardGridItem } from "@site/src/components/CardGrid";
|
|
7
8
|
|
|
8
|
-
# Guides
|
|
9
|
+
# Mastra Guides
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
<CardGrid>
|
|
13
|
-
<CardGridItem
|
|
14
|
-
title="AI Recruiter"
|
|
15
|
-
description="Create a workflow that processes candidate resumes and conducts interviews, demonstrating branching logic and LLM integration in Mastra workflows."
|
|
16
|
-
href="/guides/v1/guide/ai-recruiter"
|
|
17
|
-
/>
|
|
18
|
-
<CardGridItem
|
|
19
|
-
title="Chef Assistant"
|
|
20
|
-
description="Build an AI chef agent that helps users cook meals with available ingredients, showing how to create interactive agents with custom tools."
|
|
21
|
-
href="/guides/v1/guide/chef-michel"
|
|
22
|
-
/>
|
|
23
|
-
<CardGridItem
|
|
24
|
-
title="Research Assistant"
|
|
25
|
-
description="Develop an AI research assistant that analyzes academic papers using Retrieval Augmented Generation (RAG), demonstrating document processing and question answering."
|
|
26
|
-
href="/guides/v1/guide/research-assistant"
|
|
27
|
-
/>
|
|
28
|
-
<CardGridItem
|
|
29
|
-
title="Stock Agent"
|
|
30
|
-
description="Implement a simple agent that fetches stock prices, illustrating the basics of creating tools and integrating them with Mastra agents."
|
|
31
|
-
href="/guides/v1/guide/stock-agent"
|
|
32
|
-
/>
|
|
33
|
-
<CardGridItem
|
|
34
|
-
title="Notes MCP Server"
|
|
35
|
-
description="Build an AI notes assistant that helps users manage their notes, showing how to create interactive agents with custom tools."
|
|
36
|
-
href="/guides/v1/guide/notes-mcp-server"
|
|
37
|
-
/>
|
|
38
|
-
<CardGridItem
|
|
39
|
-
title="Web Search"
|
|
40
|
-
description="A step-by-step guide to creating an agent that can search the web."
|
|
41
|
-
href="/guides/v1/guide/web-search"
|
|
42
|
-
/>
|
|
43
|
-
</CardGrid>
|
|
11
|
+
Mastra offers a variety of guides to help you build and work with Mastra, from building agents and workflows to using the Mastra SDK and API, and implementing different UI frameworks. To find what you need, use the left-hand side navigation to browse by topic or use the search bar to look up specific implementation details, frameworks, or features.
|
|
@@ -231,6 +231,17 @@ const mastra = new Mastra({
|
|
|
231
231
|
const agent = mastra.getAgentById('my-agent');
|
|
232
232
|
```
|
|
233
233
|
|
|
234
|
+
### Stream API responses now redact sensitive data
|
|
235
|
+
|
|
236
|
+
The Mastra server now automatically redacts sensitive information from agent stream responses. This prevents accidental exposure of system prompts, tool definitions, and API keys in `step-start`, `step-finish`, and `finish` stream chunks.
|
|
237
|
+
|
|
238
|
+
**What's redacted:**
|
|
239
|
+
- `request.body` containing LLM request payloads (system prompts, tool schemas)
|
|
240
|
+
- `metadata.request` in step results
|
|
241
|
+
- `output.steps[].request` in nested step data
|
|
242
|
+
|
|
243
|
+
This behavior is enabled by default. If you need access to the full request data (e.g., for debugging or internal services), you can disable redaction when using server adapters directly (e.g., `@mastra/hono` or `@mastra/express`).
|
|
244
|
+
|
|
234
245
|
## Removed
|
|
235
246
|
|
|
236
247
|
### `generateVNext` and `streamVNext` methods
|
|
@@ -158,8 +158,37 @@ Step `suspendPayload` is now validated for steps that have a `suspendSchema` def
|
|
|
158
158
|
});
|
|
159
159
|
```
|
|
160
160
|
|
|
161
|
+
### Branch result fields are now optional
|
|
162
|
+
|
|
163
|
+
The `.branch()` method now returns a schema where all branch output fields are optional. This reflects the runtime behavior where each branch only executes if its condition is truthy, so outputs from any branch may be undefined.
|
|
164
|
+
|
|
165
|
+
To migrate, update any code that consumes branch outputs to handle optional values.
|
|
166
|
+
|
|
167
|
+
```diff
|
|
168
|
+
const workflow = createWorkflow({...})
|
|
169
|
+
.branch([
|
|
170
|
+
[condition1, stepA], // outputSchema: { result: z.string() }
|
|
171
|
+
[condition2, stepB], // outputSchema: { data: z.number() }
|
|
172
|
+
])
|
|
173
|
+
- // Previously: stepA.result typed as string, stepB.data typed as number
|
|
174
|
+
+ // Now: stepA.result typed as string | undefined, stepB.data typed as number | undefined
|
|
175
|
+
.then(nextStep);
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
If your code depends on non-optional types, add runtime checks or provide default values when accessing branch outputs.
|
|
179
|
+
|
|
180
|
+
See [Run.resumeStream()](/reference/v1/streaming/workflows/resumeStream) for details.
|
|
181
|
+
|
|
161
182
|
## Removed
|
|
162
183
|
|
|
184
|
+
### `streamVNext`, `resumeStreamVNext`, and `observeStreamVNext` methods
|
|
185
|
+
|
|
186
|
+
The experimental `streamVNext()`, `resumeStreamVNext()`, and `observeStreamVNext()` methods have been removed. These methods are now the standard implementation with updated event structures and return types.
|
|
187
|
+
|
|
188
|
+
To migrate, use the standard `stream()`, `resumeStream()`, and `observeStream()` methods. Update event type checks to use workflow-prefixed names and access stream properties directly.
|
|
189
|
+
|
|
190
|
+
See [`Run.stream()`](/reference/v1/streaming/workflows/stream), [`Run.resumeStream()`](/reference/v1/streaming/workflows/resumeStream), and [`Run.observeStream()`](/reference/v1/streaming/workflows/observeStream) for details.
|
|
191
|
+
|
|
163
192
|
### Legacy workflows export
|
|
164
193
|
|
|
165
194
|
The `./workflows/legacy` export path has been removed from `@mastra/core`. Legacy workflows are no longer supported.
|
package/.docs/raw/index.mdx
CHANGED
|
@@ -51,7 +51,7 @@ Explore real-world examples in our [community showcase](/showcase).
|
|
|
51
51
|
|
|
52
52
|
## Get started
|
|
53
53
|
|
|
54
|
-
Choose a [getting started guide](/docs/v1/getting-started/start) to get started, or see the [manual installation guide](/
|
|
54
|
+
Choose a [getting started guide](/docs/v1/getting-started/start) to get started, or see the [manual installation guide](/docs/v1/getting-started/manual-install) if you need more control over your setup.
|
|
55
55
|
|
|
56
56
|
If you're new to AI agents, check out our [templates](https://mastra.ai/templates), [course](https://mastra.ai/course), and [YouTube videos](https://youtube.com/@mastra-ai). You can also join our [Discord](https://discord.gg/BTYqqHKUrf) community to get help and share your projects.
|
|
57
57
|
|