@mastra/mcp-docs-server 0.0.0-commonjs-20250414101718

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (254) hide show
  1. package/.docs/organized/changelogs/%40mastra%2Fastra.md +302 -0
  2. package/.docs/organized/changelogs/%40mastra%2Fchroma.md +302 -0
  3. package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +161 -0
  4. package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +302 -0
  5. package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +110 -0
  6. package/.docs/organized/changelogs/%40mastra%2Fcore.md +302 -0
  7. package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +302 -0
  8. package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +302 -0
  9. package/.docs/organized/changelogs/%40mastra%2Fdeployer-vercel.md +302 -0
  10. package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +302 -0
  11. package/.docs/organized/changelogs/%40mastra%2Fevals.md +302 -0
  12. package/.docs/organized/changelogs/%40mastra%2Ffirecrawl.md +302 -0
  13. package/.docs/organized/changelogs/%40mastra%2Fgithub.md +302 -0
  14. package/.docs/organized/changelogs/%40mastra%2Floggers.md +302 -0
  15. package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +302 -0
  16. package/.docs/organized/changelogs/%40mastra%2Fmcp-registry-registry.md +26 -0
  17. package/.docs/organized/changelogs/%40mastra%2Fmcp.md +302 -0
  18. package/.docs/organized/changelogs/%40mastra%2Fmem0.md +196 -0
  19. package/.docs/organized/changelogs/%40mastra%2Fmemory.md +302 -0
  20. package/.docs/organized/changelogs/%40mastra%2Fpg.md +302 -0
  21. package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +302 -0
  22. package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +302 -0
  23. package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +302 -0
  24. package/.docs/organized/changelogs/%40mastra%2Frag.md +302 -0
  25. package/.docs/organized/changelogs/%40mastra%2Fragie.md +302 -0
  26. package/.docs/organized/changelogs/%40mastra%2Fserver.md +302 -0
  27. package/.docs/organized/changelogs/%40mastra%2Fspeech-azure.md +302 -0
  28. package/.docs/organized/changelogs/%40mastra%2Fspeech-deepgram.md +302 -0
  29. package/.docs/organized/changelogs/%40mastra%2Fspeech-elevenlabs.md +302 -0
  30. package/.docs/organized/changelogs/%40mastra%2Fspeech-google.md +302 -0
  31. package/.docs/organized/changelogs/%40mastra%2Fspeech-ibm.md +302 -0
  32. package/.docs/organized/changelogs/%40mastra%2Fspeech-murf.md +302 -0
  33. package/.docs/organized/changelogs/%40mastra%2Fspeech-openai.md +302 -0
  34. package/.docs/organized/changelogs/%40mastra%2Fspeech-playai.md +302 -0
  35. package/.docs/organized/changelogs/%40mastra%2Fspeech-replicate.md +302 -0
  36. package/.docs/organized/changelogs/%40mastra%2Fspeech-speechify.md +302 -0
  37. package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +302 -0
  38. package/.docs/organized/changelogs/%40mastra%2Fupstash.md +302 -0
  39. package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +302 -0
  40. package/.docs/organized/changelogs/%40mastra%2Fvoice-azure.md +250 -0
  41. package/.docs/organized/changelogs/%40mastra%2Fvoice-cloudflare.md +250 -0
  42. package/.docs/organized/changelogs/%40mastra%2Fvoice-deepgram.md +302 -0
  43. package/.docs/organized/changelogs/%40mastra%2Fvoice-elevenlabs.md +302 -0
  44. package/.docs/organized/changelogs/%40mastra%2Fvoice-google.md +302 -0
  45. package/.docs/organized/changelogs/%40mastra%2Fvoice-murf.md +302 -0
  46. package/.docs/organized/changelogs/%40mastra%2Fvoice-openai-realtime.md +302 -0
  47. package/.docs/organized/changelogs/%40mastra%2Fvoice-openai.md +302 -0
  48. package/.docs/organized/changelogs/%40mastra%2Fvoice-playai.md +302 -0
  49. package/.docs/organized/changelogs/%40mastra%2Fvoice-sarvam.md +302 -0
  50. package/.docs/organized/changelogs/%40mastra%2Fvoice-speechify.md +302 -0
  51. package/.docs/organized/changelogs/create-mastra.md +302 -0
  52. package/.docs/organized/changelogs/mastra.md +302 -0
  53. package/.docs/organized/code-examples/agent-network.md +282 -0
  54. package/.docs/organized/code-examples/agent.md +388 -0
  55. package/.docs/organized/code-examples/ai-sdk-useChat.md +378 -0
  56. package/.docs/organized/code-examples/assistant-ui.md +37 -0
  57. package/.docs/organized/code-examples/bird-checker-with-express.md +235 -0
  58. package/.docs/organized/code-examples/bird-checker-with-nextjs-and-eval.md +360 -0
  59. package/.docs/organized/code-examples/bird-checker-with-nextjs.md +250 -0
  60. package/.docs/organized/code-examples/client-side-tools.md +69 -0
  61. package/.docs/organized/code-examples/crypto-chatbot.md +96 -0
  62. package/.docs/organized/code-examples/fireworks-r1.md +159 -0
  63. package/.docs/organized/code-examples/mcp-registry-registry.md +63 -0
  64. package/.docs/organized/code-examples/memory-todo-agent.md +164 -0
  65. package/.docs/organized/code-examples/memory-with-context.md +167 -0
  66. package/.docs/organized/code-examples/memory-with-libsql.md +204 -0
  67. package/.docs/organized/code-examples/memory-with-mem0.md +121 -0
  68. package/.docs/organized/code-examples/memory-with-pg.md +224 -0
  69. package/.docs/organized/code-examples/memory-with-upstash.md +268 -0
  70. package/.docs/organized/code-examples/quick-start.md +129 -0
  71. package/.docs/organized/code-examples/stock-price-tool.md +124 -0
  72. package/.docs/organized/code-examples/weather-agent.md +353 -0
  73. package/.docs/organized/code-examples/workflow-ai-recruiter.md +159 -0
  74. package/.docs/organized/code-examples/workflow-with-inline-steps.md +111 -0
  75. package/.docs/organized/code-examples/workflow-with-memory.md +393 -0
  76. package/.docs/organized/code-examples/workflow-with-separate-steps.md +131 -0
  77. package/.docs/raw/agents/adding-tools.mdx +317 -0
  78. package/.docs/raw/agents/adding-voice.mdx +175 -0
  79. package/.docs/raw/agents/agent-memory.mdx +62 -0
  80. package/.docs/raw/agents/mcp-guide.mdx +215 -0
  81. package/.docs/raw/agents/overview.mdx +303 -0
  82. package/.docs/raw/community/discord.mdx +12 -0
  83. package/.docs/raw/community/licensing.mdx +63 -0
  84. package/.docs/raw/deployment/client.mdx +120 -0
  85. package/.docs/raw/deployment/deployment.mdx +127 -0
  86. package/.docs/raw/deployment/server.mdx +282 -0
  87. package/.docs/raw/evals/custom-eval.mdx +22 -0
  88. package/.docs/raw/evals/overview.mdx +95 -0
  89. package/.docs/raw/evals/running-in-ci.mdx +81 -0
  90. package/.docs/raw/evals/textual-evals.mdx +54 -0
  91. package/.docs/raw/faq/index.mdx +63 -0
  92. package/.docs/raw/frameworks/ai-sdk.mdx +296 -0
  93. package/.docs/raw/frameworks/next-js.mdx +238 -0
  94. package/.docs/raw/getting-started/installation.mdx +442 -0
  95. package/.docs/raw/getting-started/mcp-docs-server.mdx +141 -0
  96. package/.docs/raw/getting-started/project-structure.mdx +80 -0
  97. package/.docs/raw/index.mdx +22 -0
  98. package/.docs/raw/integrations/index.mdx +213 -0
  99. package/.docs/raw/local-dev/add-to-existing-project.mdx +48 -0
  100. package/.docs/raw/local-dev/creating-a-new-project.mdx +54 -0
  101. package/.docs/raw/local-dev/mastra-dev.mdx +108 -0
  102. package/.docs/raw/memory/memory-processors.mdx +131 -0
  103. package/.docs/raw/memory/overview.mdx +119 -0
  104. package/.docs/raw/memory/semantic-recall.mdx +122 -0
  105. package/.docs/raw/memory/working-memory.mdx +87 -0
  106. package/.docs/raw/observability/logging.mdx +38 -0
  107. package/.docs/raw/observability/nextjs-tracing.mdx +108 -0
  108. package/.docs/raw/observability/tracing.mdx +115 -0
  109. package/.docs/raw/rag/chunking-and-embedding.mdx +156 -0
  110. package/.docs/raw/rag/overview.mdx +85 -0
  111. package/.docs/raw/rag/retrieval.mdx +365 -0
  112. package/.docs/raw/rag/vector-databases.mdx +340 -0
  113. package/.docs/raw/reference/agents/createTool.mdx +229 -0
  114. package/.docs/raw/reference/agents/generate.mdx +334 -0
  115. package/.docs/raw/reference/agents/getAgent.mdx +54 -0
  116. package/.docs/raw/reference/agents/stream.mdx +369 -0
  117. package/.docs/raw/reference/cli/build.mdx +55 -0
  118. package/.docs/raw/reference/cli/dev.mdx +134 -0
  119. package/.docs/raw/reference/cli/init.mdx +43 -0
  120. package/.docs/raw/reference/client-js/agents.mdx +107 -0
  121. package/.docs/raw/reference/client-js/error-handling.mdx +38 -0
  122. package/.docs/raw/reference/client-js/logs.mdx +24 -0
  123. package/.docs/raw/reference/client-js/memory.mdx +97 -0
  124. package/.docs/raw/reference/client-js/telemetry.mdx +20 -0
  125. package/.docs/raw/reference/client-js/tools.mdx +44 -0
  126. package/.docs/raw/reference/client-js/vectors.mdx +79 -0
  127. package/.docs/raw/reference/client-js/workflows.mdx +136 -0
  128. package/.docs/raw/reference/core/mastra-class.mdx +232 -0
  129. package/.docs/raw/reference/deployer/cloudflare.mdx +207 -0
  130. package/.docs/raw/reference/deployer/deployer.mdx +159 -0
  131. package/.docs/raw/reference/deployer/netlify.mdx +109 -0
  132. package/.docs/raw/reference/deployer/vercel.mdx +117 -0
  133. package/.docs/raw/reference/evals/answer-relevancy.mdx +186 -0
  134. package/.docs/raw/reference/evals/bias.mdx +186 -0
  135. package/.docs/raw/reference/evals/completeness.mdx +174 -0
  136. package/.docs/raw/reference/evals/content-similarity.mdx +183 -0
  137. package/.docs/raw/reference/evals/context-position.mdx +190 -0
  138. package/.docs/raw/reference/evals/context-precision.mdx +189 -0
  139. package/.docs/raw/reference/evals/context-relevancy.mdx +188 -0
  140. package/.docs/raw/reference/evals/contextual-recall.mdx +191 -0
  141. package/.docs/raw/reference/evals/faithfulness.mdx +193 -0
  142. package/.docs/raw/reference/evals/hallucination.mdx +219 -0
  143. package/.docs/raw/reference/evals/keyword-coverage.mdx +176 -0
  144. package/.docs/raw/reference/evals/prompt-alignment.mdx +238 -0
  145. package/.docs/raw/reference/evals/summarization.mdx +205 -0
  146. package/.docs/raw/reference/evals/textual-difference.mdx +161 -0
  147. package/.docs/raw/reference/evals/tone-consistency.mdx +181 -0
  148. package/.docs/raw/reference/evals/toxicity.mdx +165 -0
  149. package/.docs/raw/reference/index.mdx +12 -0
  150. package/.docs/raw/reference/memory/Memory.mdx +212 -0
  151. package/.docs/raw/reference/memory/createThread.mdx +95 -0
  152. package/.docs/raw/reference/memory/getThreadById.mdx +46 -0
  153. package/.docs/raw/reference/memory/getThreadsByResourceId.mdx +48 -0
  154. package/.docs/raw/reference/memory/query.mdx +167 -0
  155. package/.docs/raw/reference/networks/agent-network.mdx +159 -0
  156. package/.docs/raw/reference/observability/create-logger.mdx +106 -0
  157. package/.docs/raw/reference/observability/logger.mdx +55 -0
  158. package/.docs/raw/reference/observability/otel-config.mdx +120 -0
  159. package/.docs/raw/reference/observability/providers/braintrust.mdx +40 -0
  160. package/.docs/raw/reference/observability/providers/dash0.mdx +40 -0
  161. package/.docs/raw/reference/observability/providers/index.mdx +16 -0
  162. package/.docs/raw/reference/observability/providers/laminar.mdx +41 -0
  163. package/.docs/raw/reference/observability/providers/langfuse.mdx +51 -0
  164. package/.docs/raw/reference/observability/providers/langsmith.mdx +48 -0
  165. package/.docs/raw/reference/observability/providers/langwatch.mdx +45 -0
  166. package/.docs/raw/reference/observability/providers/new-relic.mdx +40 -0
  167. package/.docs/raw/reference/observability/providers/signoz.mdx +40 -0
  168. package/.docs/raw/reference/observability/providers/traceloop.mdx +40 -0
  169. package/.docs/raw/reference/rag/astra.mdx +258 -0
  170. package/.docs/raw/reference/rag/chroma.mdx +281 -0
  171. package/.docs/raw/reference/rag/chunk.mdx +235 -0
  172. package/.docs/raw/reference/rag/document.mdx +127 -0
  173. package/.docs/raw/reference/rag/embeddings.mdx +160 -0
  174. package/.docs/raw/reference/rag/extract-params.mdx +226 -0
  175. package/.docs/raw/reference/rag/graph-rag.mdx +182 -0
  176. package/.docs/raw/reference/rag/libsql.mdx +357 -0
  177. package/.docs/raw/reference/rag/metadata-filters.mdx +298 -0
  178. package/.docs/raw/reference/rag/pg.mdx +477 -0
  179. package/.docs/raw/reference/rag/pinecone.mdx +281 -0
  180. package/.docs/raw/reference/rag/qdrant.mdx +236 -0
  181. package/.docs/raw/reference/rag/rerank.mdx +212 -0
  182. package/.docs/raw/reference/rag/turbopuffer.mdx +249 -0
  183. package/.docs/raw/reference/rag/upstash.mdx +247 -0
  184. package/.docs/raw/reference/rag/vectorize.mdx +298 -0
  185. package/.docs/raw/reference/storage/libsql.mdx +74 -0
  186. package/.docs/raw/reference/storage/postgresql.mdx +48 -0
  187. package/.docs/raw/reference/storage/upstash.mdx +86 -0
  188. package/.docs/raw/reference/tools/client.mdx +207 -0
  189. package/.docs/raw/reference/tools/document-chunker-tool.mdx +141 -0
  190. package/.docs/raw/reference/tools/graph-rag-tool.mdx +154 -0
  191. package/.docs/raw/reference/tools/mcp-configuration.mdx +206 -0
  192. package/.docs/raw/reference/tools/vector-query-tool.mdx +212 -0
  193. package/.docs/raw/reference/voice/composite-voice.mdx +140 -0
  194. package/.docs/raw/reference/voice/deepgram.mdx +164 -0
  195. package/.docs/raw/reference/voice/elevenlabs.mdx +216 -0
  196. package/.docs/raw/reference/voice/google.mdx +198 -0
  197. package/.docs/raw/reference/voice/mastra-voice.mdx +394 -0
  198. package/.docs/raw/reference/voice/murf.mdx +251 -0
  199. package/.docs/raw/reference/voice/openai-realtime.mdx +431 -0
  200. package/.docs/raw/reference/voice/openai.mdx +168 -0
  201. package/.docs/raw/reference/voice/playai.mdx +159 -0
  202. package/.docs/raw/reference/voice/sarvam.mdx +260 -0
  203. package/.docs/raw/reference/voice/speechify.mdx +145 -0
  204. package/.docs/raw/reference/voice/voice.answer.mdx +122 -0
  205. package/.docs/raw/reference/voice/voice.connect.mdx +124 -0
  206. package/.docs/raw/reference/voice/voice.listen.mdx +195 -0
  207. package/.docs/raw/reference/voice/voice.on.mdx +189 -0
  208. package/.docs/raw/reference/voice/voice.send.mdx +118 -0
  209. package/.docs/raw/reference/voice/voice.speak.mdx +203 -0
  210. package/.docs/raw/reference/workflows/after.mdx +88 -0
  211. package/.docs/raw/reference/workflows/afterEvent.mdx +76 -0
  212. package/.docs/raw/reference/workflows/commit.mdx +37 -0
  213. package/.docs/raw/reference/workflows/createRun.mdx +77 -0
  214. package/.docs/raw/reference/workflows/else.mdx +72 -0
  215. package/.docs/raw/reference/workflows/events.mdx +305 -0
  216. package/.docs/raw/reference/workflows/execute.mdx +110 -0
  217. package/.docs/raw/reference/workflows/if.mdx +107 -0
  218. package/.docs/raw/reference/workflows/resume.mdx +155 -0
  219. package/.docs/raw/reference/workflows/resumeWithEvent.mdx +133 -0
  220. package/.docs/raw/reference/workflows/snapshots.mdx +207 -0
  221. package/.docs/raw/reference/workflows/start.mdx +84 -0
  222. package/.docs/raw/reference/workflows/step-class.mdx +100 -0
  223. package/.docs/raw/reference/workflows/step-condition.mdx +134 -0
  224. package/.docs/raw/reference/workflows/step-function.mdx +92 -0
  225. package/.docs/raw/reference/workflows/step-options.mdx +69 -0
  226. package/.docs/raw/reference/workflows/step-retries.mdx +203 -0
  227. package/.docs/raw/reference/workflows/suspend.mdx +70 -0
  228. package/.docs/raw/reference/workflows/then.mdx +74 -0
  229. package/.docs/raw/reference/workflows/until.mdx +165 -0
  230. package/.docs/raw/reference/workflows/watch.mdx +118 -0
  231. package/.docs/raw/reference/workflows/while.mdx +168 -0
  232. package/.docs/raw/reference/workflows/workflow.mdx +233 -0
  233. package/.docs/raw/storage/overview.mdx +378 -0
  234. package/.docs/raw/voice/overview.mdx +135 -0
  235. package/.docs/raw/voice/speech-to-text.mdx +45 -0
  236. package/.docs/raw/voice/text-to-speech.mdx +52 -0
  237. package/.docs/raw/voice/voice-to-voice.mdx +310 -0
  238. package/.docs/raw/workflows/control-flow.mdx +778 -0
  239. package/.docs/raw/workflows/dynamic-workflows.mdx +236 -0
  240. package/.docs/raw/workflows/error-handling.mdx +183 -0
  241. package/.docs/raw/workflows/nested-workflows.mdx +352 -0
  242. package/.docs/raw/workflows/overview.mdx +203 -0
  243. package/.docs/raw/workflows/steps.mdx +108 -0
  244. package/.docs/raw/workflows/suspend-and-resume.mdx +404 -0
  245. package/.docs/raw/workflows/variables.mdx +313 -0
  246. package/LICENSE.md +46 -0
  247. package/README.md +129 -0
  248. package/dist/_tsup-dts-rollup.d.ts +149 -0
  249. package/dist/chunk-QWYMT5LP.js +194 -0
  250. package/dist/prepare-docs/prepare.d.ts +1 -0
  251. package/dist/prepare-docs/prepare.js +1 -0
  252. package/dist/stdio.d.ts +1 -0
  253. package/dist/stdio.js +518 -0
  254. package/package.json +60 -0
@@ -0,0 +1,236 @@
1
+ ---
2
+ title: "Dynamic Workflows | Mastra Docs"
3
+ description: "Learn how to create dynamic workflows within workflow steps, allowing for flexible workflow creation based on runtime conditions."
4
+ ---
5
+
6
+ # Dynamic Workflows
7
+
8
+ This guide demonstrates how to create dynamic workflows within a workflow step. This advanced pattern allows you to create and execute workflows on the fly based on runtime conditions.
9
+
10
+ ## Overview
11
+
12
+ Dynamic workflows are useful when you need to create workflows based on runtime data.
13
+
14
+ ## Implementation
15
+
16
+ The key to creating dynamic workflows is accessing the Mastra instance from within a step's `execute` function and using it to create and run a new workflow.
17
+
18
+ ### Basic Example
19
+
20
+ ```typescript
21
+ import { Mastra, Step, Workflow } from '@mastra/core';
22
+ import { z } from 'zod';
23
+
24
+ const isMastra = (mastra: any): mastra is Mastra => {
25
+ return mastra && typeof mastra === 'object' && mastra instanceof Mastra;
26
+ };
27
+
28
+ // Step that creates and runs a dynamic workflow
29
+ const createDynamicWorkflow = new Step({
30
+ id: 'createDynamicWorkflow',
31
+ outputSchema: z.object({
32
+ dynamicWorkflowResult: z.any(),
33
+ }),
34
+ execute: async ({ context, mastra }) => {
35
+ if (!mastra) {
36
+ throw new Error('Mastra instance not available');
37
+ }
38
+
39
+ if (!isMastra(mastra)) {
40
+ throw new Error('Invalid Mastra instance');
41
+ }
42
+
43
+ const inputData = context.triggerData.inputData;
44
+
45
+ // Create a new dynamic workflow
46
+ const dynamicWorkflow = new Workflow({
47
+ name: 'dynamic-workflow',
48
+ mastra, // Pass the mastra instance to the new workflow
49
+ triggerSchema: z.object({
50
+ dynamicInput: z.string(),
51
+ }),
52
+ });
53
+
54
+ // Define steps for the dynamic workflow
55
+ const dynamicStep = new Step({
56
+ id: 'dynamicStep',
57
+ execute: async ({ context }) => {
58
+ const dynamicInput = context.triggerData.dynamicInput;
59
+ return {
60
+ processedValue: `Processed: ${dynamicInput}`,
61
+ };
62
+ },
63
+ });
64
+
65
+ // Build and commit the dynamic workflow
66
+ dynamicWorkflow.step(dynamicStep).commit();
67
+
68
+ // Create a run and execute the dynamic workflow
69
+ const run = dynamicWorkflow.createRun();
70
+ const result = await run.start({
71
+ triggerData: {
72
+ dynamicInput: inputData,
73
+ },
74
+ });
75
+
76
+ let dynamicWorkflowResult;
77
+
78
+ if (result.results['dynamicStep']?.status === 'success') {
79
+ dynamicWorkflowResult = result.results['dynamicStep']?.output.processedValue;
80
+ } else {
81
+ throw new Error('Dynamic workflow failed');
82
+ }
83
+
84
+ // Return the result from the dynamic workflow
85
+ return {
86
+ dynamicWorkflowResult,
87
+ };
88
+ },
89
+ });
90
+
91
+ // Main workflow that uses the dynamic workflow creator
92
+ const mainWorkflow = new Workflow({
93
+ name: 'main-workflow',
94
+ triggerSchema: z.object({
95
+ inputData: z.string(),
96
+ }),
97
+ mastra: new Mastra(),
98
+ });
99
+
100
+ mainWorkflow.step(createDynamicWorkflow).commit();
101
+
102
+ // Register the workflow with Mastra
103
+ export const mastra = new Mastra({
104
+ workflows: { mainWorkflow },
105
+ });
106
+
107
+ const run = mainWorkflow.createRun();
108
+ const result = await run.start({
109
+ triggerData: {
110
+ inputData: 'test',
111
+ },
112
+ });
113
+ ```
114
+
115
+ ## Advanced Example: Workflow Factory
116
+
117
+ You can create a workflow factory that generates different workflows based on input parameters:
118
+
119
+ ```typescript
120
+
121
+ const isMastra = (mastra: any): mastra is Mastra => {
122
+ return mastra && typeof mastra === 'object' && mastra instanceof Mastra;
123
+ };
124
+
125
+ const workflowFactory = new Step({
126
+ id: 'workflowFactory',
127
+ inputSchema: z.object({
128
+ workflowType: z.enum(['simple', 'complex']),
129
+ inputData: z.string(),
130
+ }),
131
+ outputSchema: z.object({
132
+ result: z.any(),
133
+ }),
134
+ execute: async ({ context, mastra }) => {
135
+ if (!mastra) {
136
+ throw new Error('Mastra instance not available');
137
+ }
138
+
139
+ if (!isMastra(mastra)) {
140
+ throw new Error('Invalid Mastra instance');
141
+ }
142
+
143
+ // Create a new dynamic workflow based on the type
144
+ const dynamicWorkflow = new Workflow({
145
+ name: `dynamic-${context.workflowType}-workflow`,
146
+ mastra,
147
+ triggerSchema: z.object({
148
+ input: z.string(),
149
+ }),
150
+ });
151
+
152
+ if (context.workflowType === 'simple') {
153
+ // Simple workflow with a single step
154
+ const simpleStep = new Step({
155
+ id: 'simpleStep',
156
+ execute: async ({ context }) => {
157
+ return {
158
+ result: `Simple processing: ${context.triggerData.input}`,
159
+ };
160
+ },
161
+ });
162
+
163
+ dynamicWorkflow.step(simpleStep).commit();
164
+ } else {
165
+ // Complex workflow with multiple steps
166
+ const step1 = new Step({
167
+ id: 'step1',
168
+ outputSchema: z.object({
169
+ intermediateResult: z.string(),
170
+ }),
171
+ execute: async ({ context }) => {
172
+ return {
173
+ intermediateResult: `First processing: ${context.triggerData.input}`,
174
+ };
175
+ },
176
+ });
177
+
178
+ const step2 = new Step({
179
+ id: 'step2',
180
+ execute: async ({ context }) => {
181
+ const intermediate = context.getStepResult(step1).intermediateResult;
182
+ return {
183
+ finalResult: `Second processing: ${intermediate}`,
184
+ };
185
+ },
186
+ });
187
+
188
+ dynamicWorkflow.step(step1).then(step2).commit();
189
+ }
190
+
191
+ // Execute the dynamic workflow
192
+ const run = dynamicWorkflow.createRun();
193
+ const result = await run.start({
194
+ triggerData: {
195
+ input: context.inputData,
196
+ },
197
+ });
198
+
199
+ // Return the appropriate result based on workflow type
200
+ if (context.workflowType === 'simple') {
201
+ return {
202
+ // @ts-ignore
203
+ result: result.results['simpleStep']?.output,
204
+ };
205
+ } else {
206
+ return {
207
+ // @ts-ignore
208
+ result: result.results['step2']?.output,
209
+ };
210
+ }
211
+ },
212
+ });
213
+ ```
214
+
215
+ ## Important Considerations
216
+
217
+ 1. **Mastra Instance**: The `mastra` parameter in the `execute` function provides access to the Mastra instance, which is essential for creating dynamic workflows.
218
+
219
+ 2. **Error Handling**: Always check if the Mastra instance is available before attempting to create a dynamic workflow.
220
+
221
+ 3. **Resource Management**: Dynamic workflows consume resources, so be mindful of creating too many workflows in a single execution.
222
+
223
+ 4. **Workflow Lifecycle**: Dynamic workflows are not automatically registered with the main Mastra instance. They exist only for the duration of the step execution unless you explicitly register them.
224
+
225
+ 5. **Debugging**: Debugging dynamic workflows can be challenging. Consider adding detailed logging to track their creation and execution.
226
+
227
+ ## Use Cases
228
+
229
+ - **Conditional Workflow Selection**: Choose different workflow patterns based on input data
230
+ - **Parameterized Workflows**: Create workflows with dynamic configurations
231
+ - **Workflow Templates**: Use templates to generate specialized workflows
232
+ - **Multi-tenant Applications**: Create isolated workflows for different tenants
233
+
234
+ ## Conclusion
235
+
236
+ Dynamic workflows provide a powerful way to create flexible, adaptable workflow systems. By leveraging the Mastra instance within step execution, you can create workflows that respond to runtime conditions and requirements.
@@ -0,0 +1,183 @@
1
+ ---
2
+ title: "Error Handling in Workflows | Mastra Docs"
3
+ description: "Learn how to handle errors in Mastra workflows using step retries, conditional branching, and monitoring."
4
+ ---
5
+
6
+ # Error Handling in Workflows
7
+
8
+ Robust error handling is essential for production workflows. Mastra provides several mechanisms to handle errors gracefully, allowing your workflows to recover from failures or gracefully degrade when necessary.
9
+
10
+ ## Overview
11
+
12
+ Error handling in Mastra workflows can be implemented using:
13
+
14
+ 1. **Step Retries** - Automatically retry failed steps
15
+ 2. **Conditional Branching** - Create alternative paths based on step success or failure
16
+ 3. **Error Monitoring** - Watch workflows for errors and handle them programmatically
17
+ 4. **Result Status Checks** - Check the status of previous steps in subsequent steps
18
+
19
+ ## Step Retries
20
+
21
+ Mastra provides a built-in retry mechanism for steps that fail due to transient errors. This is particularly useful for steps that interact with external services or resources that might experience temporary unavailability.
22
+
23
+ ### Basic Retry Configuration
24
+
25
+ You can configure retries at the workflow level or for individual steps:
26
+
27
+ ```typescript
28
+ // Workflow-level retry configuration
29
+ const workflow = new Workflow({
30
+ name: 'my-workflow',
31
+ retryConfig: {
32
+ attempts: 3, // Number of retry attempts
33
+ delay: 1000, // Delay between retries in milliseconds
34
+ },
35
+ });
36
+
37
+ // Step-level retry configuration (overrides workflow-level)
38
+ const apiStep = new Step({
39
+ id: 'callApi',
40
+ execute: async () => {
41
+ // API call that might fail
42
+ },
43
+ retryConfig: {
44
+ attempts: 5, // This step will retry up to 5 times
45
+ delay: 2000, // With a 2-second delay between retries
46
+ },
47
+ });
48
+ ```
49
+
50
+ For more details about step retries, see the [Step Retries](../reference/workflows/step-retries.mdx) reference.
51
+
52
+ ## Conditional Branching
53
+
54
+ You can create alternative workflow paths based on the success or failure of previous steps using conditional logic:
55
+
56
+ ```typescript
57
+ // Create a workflow with conditional branching
58
+ const workflow = new Workflow({
59
+ name: 'error-handling-workflow',
60
+ });
61
+
62
+ workflow
63
+ .step(fetchDataStep)
64
+ .then(processDataStep, {
65
+ // Only execute processDataStep if fetchDataStep was successful
66
+ when: ({ context }) => {
67
+ return context.steps.fetchDataStep?.status === 'success';
68
+ },
69
+ })
70
+ .then(fallbackStep, {
71
+ // Execute fallbackStep if fetchDataStep failed
72
+ when: ({ context }) => {
73
+ return context.steps.fetchDataStep?.status === 'failed';
74
+ },
75
+ })
76
+ .commit();
77
+ ```
78
+
79
+ ## Error Monitoring
80
+
81
+ You can monitor workflows for errors using the `watch` method:
82
+
83
+ ```typescript
84
+ const { start, watch } = workflow.createRun();
85
+
86
+ watch(async ({ results }) => {
87
+ // Check for any failed steps
88
+ const failedSteps = Object.entries(results)
89
+ .filter(([_, step]) => step.status === "failed")
90
+ .map(([stepId]) => stepId);
91
+
92
+ if (failedSteps.length > 0) {
93
+ console.error(`Workflow has failed steps: ${failedSteps.join(', ')}`);
94
+ // Take remedial action, such as alerting or logging
95
+ }
96
+ });
97
+
98
+ await start();
99
+ ```
100
+
101
+ ## Handling Errors in Steps
102
+
103
+ Within a step's execution function, you can handle errors programmatically:
104
+
105
+ ```typescript
106
+ const robustStep = new Step({
107
+ id: 'robustStep',
108
+ execute: async ({ context }) => {
109
+ try {
110
+ // Attempt the primary operation
111
+ const result = await someRiskyOperation();
112
+ return { success: true, data: result };
113
+ } catch (error) {
114
+ // Log the error
115
+ console.error('Operation failed:', error);
116
+
117
+ // Return a graceful fallback result instead of throwing
118
+ return {
119
+ success: false,
120
+ error: error.message,
121
+ fallbackData: 'Default value'
122
+ };
123
+ }
124
+ },
125
+ });
126
+ ```
127
+
128
+ ## Checking Previous Step Results
129
+
130
+ You can make decisions based on the results of previous steps:
131
+
132
+ ```typescript
133
+ const finalStep = new Step({
134
+ id: 'finalStep',
135
+ execute: async ({ context }) => {
136
+ // Check results of previous steps
137
+ const step1Success = context.steps.step1?.status === 'success';
138
+ const step2Success = context.steps.step2?.status === 'success';
139
+
140
+ if (step1Success && step2Success) {
141
+ // All steps succeeded
142
+ return { status: 'complete', result: 'All operations succeeded' };
143
+ } else if (step1Success) {
144
+ // Only step1 succeeded
145
+ return { status: 'partial', result: 'Partial completion' };
146
+ } else {
147
+ // Critical failure
148
+ return { status: 'failed', result: 'Critical steps failed' };
149
+ }
150
+ },
151
+ });
152
+ ```
153
+
154
+ ## Best Practices for Error Handling
155
+
156
+ 1. **Use retries for transient failures**: Configure retry policies for steps that might experience temporary issues.
157
+
158
+ 2. **Provide fallback paths**: Design workflows with alternative paths for when critical steps fail.
159
+
160
+ 3. **Be specific about error scenarios**: Use different handling strategies for different types of errors.
161
+
162
+ 4. **Log errors comprehensively**: Include context information when logging errors to aid in debugging.
163
+
164
+ 5. **Return meaningful data on failure**: When a step fails, return structured data about the failure to help downstream steps make decisions.
165
+
166
+ 6. **Consider idempotency**: Ensure steps can be safely retried without causing duplicate side effects.
167
+
168
+ 7. **Monitor workflow execution**: Use the `watch` method to actively monitor workflow execution and detect errors early.
169
+
170
+ ## Advanced Error Handling
171
+
172
+ For more complex error handling scenarios, consider:
173
+
174
+ - **Implementing circuit breakers**: If a step fails repeatedly, stop retrying and use a fallback strategy
175
+ - **Adding timeout handling**: Set time limits for steps to prevent workflows from hanging indefinitely
176
+ - **Creating dedicated error recovery workflows**: For critical workflows, create separate recovery workflows that can be triggered when the main workflow fails
177
+
178
+ ## Related
179
+
180
+ - [Step Retries Reference](../../reference/workflows/step-retries.mdx)
181
+ - [Watch Method Reference](../../reference/workflows/watch.mdx)
182
+ - [Step Conditions](../../reference/workflows/step-condition.mdx)
183
+ - [Control Flow](./control-flow.mdx)