@mastra/mcp-docs-server 0.13.15 → 0.13.17-alpha.0
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 +15 -15
- package/.docs/organized/changelogs/%40internal%2Ftypes-builder.md +4 -0
- package/.docs/organized/changelogs/%40mastra%2Fastra.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fauth.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Fchroma.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +26 -26
- package/.docs/organized/changelogs/%40mastra%2Fcloud.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +24 -24
- package/.docs/organized/changelogs/%40mastra%2Fcouchbase.md +20 -20
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +30 -0
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloudflare.md +29 -29
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +29 -29
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-vercel.md +29 -29
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +33 -33
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fevals.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Ffastembed.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Ffirecrawl.md +21 -21
- package/.docs/organized/changelogs/%40mastra%2Fgithub.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Flance.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +27 -27
- package/.docs/organized/changelogs/%40mastra%2Fmcp-registry-registry.md +20 -20
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +28 -28
- package/.docs/organized/changelogs/%40mastra%2Fmem0.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +20 -20
- package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fmssql.md +18 -0
- package/.docs/organized/changelogs/%40mastra%2Fopensearch.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +35 -35
- package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +20 -20
- package/.docs/organized/changelogs/%40mastra%2Frag.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fragie.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fschema-compat.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +28 -28
- package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvoice-azure.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvoice-cloudflare.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvoice-deepgram.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvoice-elevenlabs.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvoice-gladia.md +18 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google-gemini-live.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvoice-murf.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai-realtime.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvoice-openai.md +20 -20
- package/.docs/organized/changelogs/%40mastra%2Fvoice-playai.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvoice-sarvam.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fvoice-speechify.md +19 -19
- package/.docs/organized/changelogs/create-mastra.md +23 -23
- package/.docs/organized/changelogs/mastra.md +44 -44
- package/.docs/raw/agents/overview.mdx +29 -47
- package/.docs/raw/deployment/monorepo.mdx +107 -0
- package/.docs/raw/frameworks/web-frameworks/astro.mdx +1 -0
- package/.docs/raw/frameworks/web-frameworks/next-js.mdx +1 -0
- package/.docs/raw/frameworks/web-frameworks/sveltekit.mdx +5 -0
- package/.docs/raw/frameworks/web-frameworks/vite-react.mdx +5 -0
- package/.docs/raw/reference/core/mastra-class.mdx +5 -3
- package/.docs/raw/reference/scorers/context-precision.mdx +130 -0
- package/.docs/raw/reference/scorers/context-relevance.mdx +222 -0
- package/.docs/raw/scorers/off-the-shelf-scorers.mdx +17 -1
- package/.docs/raw/server-db/local-dev-playground.mdx +19 -0
- package/.docs/raw/workflows/control-flow.mdx +0 -46
- package/.docs/raw/workflows/error-handling.mdx +213 -0
- package/package.json +5 -5
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Reference: Context Relevance Scorer | Scorers | Mastra Docs"
|
|
3
|
+
description: Documentation for the Context Relevance Scorer in Mastra. Evaluates the relevance and utility of provided context for generating agent responses using weighted relevance scoring.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import { PropertiesTable } from "@/components/properties-table";
|
|
7
|
+
|
|
8
|
+
# Context Relevance Scorer
|
|
9
|
+
|
|
10
|
+
The `createContextRelevanceScorerLLM()` function creates a scorer that evaluates how relevant and useful provided context was for generating agent responses. It uses weighted relevance levels and applies penalties for unused high-relevance context and missing information.
|
|
11
|
+
|
|
12
|
+
## Parameters
|
|
13
|
+
|
|
14
|
+
<PropertiesTable
|
|
15
|
+
content={[
|
|
16
|
+
{
|
|
17
|
+
name: "model",
|
|
18
|
+
type: "MastraLanguageModel",
|
|
19
|
+
description: "The language model to use for evaluating context relevance",
|
|
20
|
+
required: true,
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
name: "options",
|
|
24
|
+
type: "ContextRelevanceOptions",
|
|
25
|
+
description: "Configuration options for the scorer",
|
|
26
|
+
required: true,
|
|
27
|
+
children: [
|
|
28
|
+
{
|
|
29
|
+
name: "context",
|
|
30
|
+
type: "string[]",
|
|
31
|
+
description: "Array of context pieces to evaluate for relevance",
|
|
32
|
+
required: false,
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
name: "contextExtractor",
|
|
36
|
+
type: "(input, output) => string[]",
|
|
37
|
+
description: "Function to dynamically extract context from the run input and output",
|
|
38
|
+
required: false,
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
name: "scale",
|
|
42
|
+
type: "number",
|
|
43
|
+
description: "Scale factor to multiply the final score (default: 1)",
|
|
44
|
+
required: false,
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: "penalties",
|
|
48
|
+
type: "object",
|
|
49
|
+
description: "Configurable penalty settings for scoring",
|
|
50
|
+
required: false,
|
|
51
|
+
children: [
|
|
52
|
+
{
|
|
53
|
+
name: "unusedHighRelevanceContext",
|
|
54
|
+
type: "number",
|
|
55
|
+
description: "Penalty per unused high-relevance context (default: 0.1)",
|
|
56
|
+
required: false,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: "missingContextPerItem",
|
|
60
|
+
type: "number",
|
|
61
|
+
description: "Penalty per missing context item (default: 0.15)",
|
|
62
|
+
required: false,
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
name: "maxMissingContextPenalty",
|
|
66
|
+
type: "number",
|
|
67
|
+
description: "Maximum total missing context penalty (default: 0.5)",
|
|
68
|
+
required: false,
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
},
|
|
74
|
+
]}
|
|
75
|
+
/>
|
|
76
|
+
|
|
77
|
+
:::note
|
|
78
|
+
Either `context` or `contextExtractor` must be provided. If both are provided, `contextExtractor` takes precedence.
|
|
79
|
+
:::
|
|
80
|
+
|
|
81
|
+
## .run() Returns
|
|
82
|
+
|
|
83
|
+
<PropertiesTable
|
|
84
|
+
content={[
|
|
85
|
+
{
|
|
86
|
+
name: "score",
|
|
87
|
+
type: "number",
|
|
88
|
+
description: "Weighted relevance score between 0 and scale (default 0-1)",
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
name: "reason",
|
|
92
|
+
type: "string",
|
|
93
|
+
description: "Human-readable explanation of the context relevance evaluation",
|
|
94
|
+
},
|
|
95
|
+
]}
|
|
96
|
+
/>
|
|
97
|
+
|
|
98
|
+
## Scoring Details
|
|
99
|
+
|
|
100
|
+
### Weighted Relevance Scoring
|
|
101
|
+
|
|
102
|
+
Context Relevance uses a sophisticated scoring algorithm that considers:
|
|
103
|
+
|
|
104
|
+
1. **Relevance Levels**: Each context piece is classified with weighted values:
|
|
105
|
+
- `high` = 1.0 (directly addresses the query)
|
|
106
|
+
- `medium` = 0.7 (supporting information)
|
|
107
|
+
- `low` = 0.3 (tangentially related)
|
|
108
|
+
- `none` = 0.0 (completely irrelevant)
|
|
109
|
+
|
|
110
|
+
2. **Usage Detection**: Tracks whether relevant context was actually used in the response
|
|
111
|
+
|
|
112
|
+
3. **Penalties Applied** (configurable via `penalties` options):
|
|
113
|
+
- **Unused High-Relevance**: `unusedHighRelevanceContext` penalty per unused high-relevance context (default: 0.1)
|
|
114
|
+
- **Missing Context**: Up to `maxMissingContextPenalty` for identified missing information (default: 0.5)
|
|
115
|
+
|
|
116
|
+
### Scoring Formula
|
|
117
|
+
|
|
118
|
+
```
|
|
119
|
+
Base Score = Σ(relevance_weights) / (num_contexts × 1.0)
|
|
120
|
+
Usage Penalty = count(unused_high_relevance) × unusedHighRelevanceContext
|
|
121
|
+
Missing Penalty = min(count(missing_context) × missingContextPerItem, maxMissingContextPenalty)
|
|
122
|
+
|
|
123
|
+
Final Score = max(0, Base Score - Usage Penalty - Missing Penalty) × scale
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
**Default Values**:
|
|
127
|
+
- `unusedHighRelevanceContext` = 0.1 (10% penalty per unused high-relevance context)
|
|
128
|
+
- `missingContextPerItem` = 0.15 (15% penalty per missing context item)
|
|
129
|
+
- `maxMissingContextPenalty` = 0.5 (maximum 50% penalty for missing context)
|
|
130
|
+
- `scale` = 1
|
|
131
|
+
|
|
132
|
+
### Score Interpretation
|
|
133
|
+
|
|
134
|
+
- **0.9-1.0** = Excellent relevance with minimal gaps
|
|
135
|
+
- **0.7-0.8** = Good relevance with some unused or missing context
|
|
136
|
+
- **0.4-0.6** = Mixed relevance with significant gaps
|
|
137
|
+
- **0.0-0.3** = Poor relevance or mostly irrelevant context
|
|
138
|
+
|
|
139
|
+
### Difference from Context Precision
|
|
140
|
+
|
|
141
|
+
| Aspect | Context Relevance | Context Precision |
|
|
142
|
+
|--------|-------------------|-------------------|
|
|
143
|
+
| **Algorithm** | Weighted levels with penalties | Mean Average Precision (MAP) |
|
|
144
|
+
| **Relevance** | Multiple levels (high/medium/low/none) | Binary (yes/no) |
|
|
145
|
+
| **Position** | Not considered | Critical (rewards early placement) |
|
|
146
|
+
| **Usage** | Tracks and penalizes unused context | Not considered |
|
|
147
|
+
| **Missing** | Identifies and penalizes gaps | Not evaluated |
|
|
148
|
+
|
|
149
|
+
## Usage Examples
|
|
150
|
+
|
|
151
|
+
### Basic Configuration
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
const scorer = createContextRelevanceScorerLLM({
|
|
155
|
+
model: openai('gpt-4o'),
|
|
156
|
+
options: {
|
|
157
|
+
context: ['Einstein won the Nobel Prize for his work on the photoelectric effect'],
|
|
158
|
+
scale: 1,
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Custom Penalty Configuration
|
|
164
|
+
|
|
165
|
+
```typescript
|
|
166
|
+
const scorer = createContextRelevanceScorerLLM({
|
|
167
|
+
model: openai('gpt-4o'),
|
|
168
|
+
options: {
|
|
169
|
+
context: ['Context information...'],
|
|
170
|
+
penalties: {
|
|
171
|
+
unusedHighRelevanceContext: 0.05, // Lower penalty for unused context
|
|
172
|
+
missingContextPerItem: 0.2, // Higher penalty per missing item
|
|
173
|
+
maxMissingContextPenalty: 0.4, // Lower maximum penalty cap
|
|
174
|
+
},
|
|
175
|
+
scale: 2, // Double the final score
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### Dynamic Context Extraction
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
const scorer = createContextRelevanceScorerLLM({
|
|
184
|
+
model: openai('gpt-4o'),
|
|
185
|
+
options: {
|
|
186
|
+
contextExtractor: (input, output) => {
|
|
187
|
+
// Extract context based on the query
|
|
188
|
+
const userQuery = input?.inputMessages?.[0]?.content || '';
|
|
189
|
+
if (userQuery.includes('Einstein')) {
|
|
190
|
+
return [
|
|
191
|
+
'Einstein won the Nobel Prize for the photoelectric effect',
|
|
192
|
+
'He developed the theory of relativity'
|
|
193
|
+
];
|
|
194
|
+
}
|
|
195
|
+
return ['General physics information'];
|
|
196
|
+
},
|
|
197
|
+
penalties: {
|
|
198
|
+
unusedHighRelevanceContext: 0.15,
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Usage Patterns
|
|
205
|
+
|
|
206
|
+
### Content Generation Evaluation
|
|
207
|
+
Best for evaluating context quality in:
|
|
208
|
+
- Chat systems where context usage matters
|
|
209
|
+
- RAG pipelines needing nuanced relevance assessment
|
|
210
|
+
- Systems where missing context affects quality
|
|
211
|
+
|
|
212
|
+
### Context Selection Optimization
|
|
213
|
+
Use when optimizing for:
|
|
214
|
+
- Comprehensive context coverage
|
|
215
|
+
- Effective context utilization
|
|
216
|
+
- Identifying context gaps
|
|
217
|
+
|
|
218
|
+
## Related
|
|
219
|
+
|
|
220
|
+
- [Context Precision Scorer](/reference/scorers/context-precision) - Evaluates context ranking using MAP
|
|
221
|
+
- [Faithfulness Scorer](/reference/scorers/faithfulness) - Measures answer groundedness in context
|
|
222
|
+
- [Custom Scorers](/docs/scorers/custom-scorers) - Creating your own evaluation metrics
|
|
@@ -21,6 +21,22 @@ These scorers evaluate how correct, truthful, and complete your agent's answers
|
|
|
21
21
|
- [`textual-difference`](/reference/scorers/textual-difference): Measures textual differences between strings (`0-1`, higher means more similar)
|
|
22
22
|
- [`tool-call-accuracy`](/reference/scorers/tool-call-accuracy): Evaluates whether the LLM selects the correct tool from available options (`0-1`, higher is better)
|
|
23
23
|
|
|
24
|
+
### Context Quality
|
|
25
|
+
|
|
26
|
+
These scorers evaluate the quality and relevance of context used in generating responses:
|
|
27
|
+
|
|
28
|
+
- [`context-precision`](/reference/scorers/context-precision): Evaluates context relevance and ranking using Mean Average Precision, rewarding early placement of relevant context (`0-1`, higher is better)
|
|
29
|
+
- [`context-relevance`](/reference/scorers/context-relevance): Measures context utility with nuanced relevance levels, usage tracking, and missing context detection (`0-1`, higher is better)
|
|
30
|
+
|
|
31
|
+
:::tip Context Scorer Selection
|
|
32
|
+
- Use **Context Precision** when context ordering matters and you need standard IR metrics (ideal for RAG ranking evaluation)
|
|
33
|
+
- Use **Context Relevance** when you need detailed relevance assessment and want to track context usage and identify gaps
|
|
34
|
+
|
|
35
|
+
Both context scorers support:
|
|
36
|
+
- **Static context**: Pre-defined context arrays
|
|
37
|
+
- **Dynamic context extraction**: Extract context from runs using custom functions (ideal for RAG systems, vector databases, etc.)
|
|
38
|
+
:::
|
|
39
|
+
|
|
24
40
|
### Output Quality
|
|
25
41
|
|
|
26
42
|
These scorers evaluate adherence to format, style, and safety requirements:
|
|
@@ -28,4 +44,4 @@ These scorers evaluate adherence to format, style, and safety requirements:
|
|
|
28
44
|
- [`tone-consistency`](/reference/scorers/tone-consistency): Measures consistency in formality, complexity, and style (`0-1`, higher is better)
|
|
29
45
|
- [`toxicity`](/reference/scorers/toxicity): Detects harmful or inappropriate content (`0-1`, lower is better)
|
|
30
46
|
- [`bias`](/reference/scorers/bias): Detects potential biases in the output (`0-1`, lower is better)
|
|
31
|
-
- [`keyword-coverage`](/reference/scorers/keyword-coverage): Assesses technical terminology usage (`0-1`, higher is better)
|
|
47
|
+
- [`keyword-coverage`](/reference/scorers/keyword-coverage): Assesses technical terminology usage (`0-1`, higher is better)
|
|
@@ -184,6 +184,25 @@ export const mastra = new Mastra({
|
|
|
184
184
|
});
|
|
185
185
|
```
|
|
186
186
|
|
|
187
|
+
## Bundler options
|
|
188
|
+
|
|
189
|
+
Use `transpilePackages` to compile TypeScript packages or libraries. Use `externals` to exclude dependencies resolved at runtime, and `sourcemap` to emit readable stack traces.
|
|
190
|
+
|
|
191
|
+
```typescript filename="src/mastra/index.ts" showLineNumbers copy
|
|
192
|
+
import { Mastra } from "@mastra/core/mastra";
|
|
193
|
+
|
|
194
|
+
export const mastra = new Mastra({
|
|
195
|
+
// ...
|
|
196
|
+
bundler: {
|
|
197
|
+
transpilePackages: ["utils"],
|
|
198
|
+
externals: ["ui"],
|
|
199
|
+
sourcemap: true
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
> See [Mastra Class](../../reference/core/mastra-class.mdx) for more configuration options.
|
|
205
|
+
|
|
187
206
|
|
|
188
207
|
## Next steps
|
|
189
208
|
|
|
@@ -190,52 +190,6 @@ export const testWorkflow = createWorkflow({...})
|
|
|
190
190
|
.commit();
|
|
191
191
|
```
|
|
192
192
|
|
|
193
|
-
## Exiting early with `bail()`
|
|
194
|
-
|
|
195
|
-
Use `bail()` in a step to exit early with a successful result. This returns the provided payload as the step output and ends workflow execution.
|
|
196
|
-
|
|
197
|
-
```typescript {7} filename="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
|
|
198
|
-
import { createWorkflow, createStep } from "@mastra/core/workflows";
|
|
199
|
-
import { z } from "zod";
|
|
200
|
-
|
|
201
|
-
const step1 = createStep({
|
|
202
|
-
id: 'step1',
|
|
203
|
-
execute: async ({ bail }) => {
|
|
204
|
-
return bail({ result: 'bailed' });
|
|
205
|
-
},
|
|
206
|
-
inputSchema: z.object({ value: z.string() }),
|
|
207
|
-
outputSchema: z.object({ result: z.string() }),
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
export const testWorkflow = createWorkflow({...})
|
|
211
|
-
.then(step1)
|
|
212
|
-
.commit();
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
## Exiting early with `Error()`
|
|
216
|
-
|
|
217
|
-
Use `throw new Error()` in a step to exit with an error.
|
|
218
|
-
|
|
219
|
-
```typescript {7} filename="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
|
|
220
|
-
import { createWorkflow, createStep } from "@mastra/core/workflows";
|
|
221
|
-
import { z } from "zod";
|
|
222
|
-
|
|
223
|
-
const step1 = createStep({
|
|
224
|
-
id: 'step1',
|
|
225
|
-
execute: async () => {
|
|
226
|
-
throw new Error('bailed');
|
|
227
|
-
},
|
|
228
|
-
inputSchema: z.object({ value: z.string() }),
|
|
229
|
-
outputSchema: z.object({ result: z.string() }),
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
export const testWorkflow = createWorkflow({...})
|
|
233
|
-
.then(step1)
|
|
234
|
-
.commit();
|
|
235
|
-
```
|
|
236
|
-
|
|
237
|
-
This throws an error from the step and stops workflow execution, returning the error as the result.
|
|
238
|
-
|
|
239
193
|
## Example Run Instance
|
|
240
194
|
|
|
241
195
|
The following example demonstrates how to start a run with multiple inputs. Each input will pass through the `mapStep` sequentially.
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Error Handling in Workflows | 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
|
|
7
|
+
|
|
8
|
+
Mastra provides a built-in retry mechanism for workflows or 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.
|
|
9
|
+
|
|
10
|
+
## Workflow-level using `retryConfig`
|
|
11
|
+
|
|
12
|
+
You can configure retries at the workflow level, which applies to all steps in the workflow:
|
|
13
|
+
|
|
14
|
+
```typescript {8-11} filename="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
|
|
15
|
+
import { createWorkflow, createStep } from "@mastra/core/workflows";
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
|
|
18
|
+
const step1 = createStep({...});
|
|
19
|
+
|
|
20
|
+
export const testWorkflow = createWorkflow({
|
|
21
|
+
// ...
|
|
22
|
+
retryConfig: {
|
|
23
|
+
attempts: 5,
|
|
24
|
+
delay: 2000
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
.then(step1)
|
|
28
|
+
.commit();
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Step-level using `retries`
|
|
32
|
+
|
|
33
|
+
You can configure retries for individual steps using the `retries` property. This overrides the workflow-level retry configuration for that specific step:
|
|
34
|
+
|
|
35
|
+
```typescript {17} filename="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
|
|
36
|
+
import { createWorkflow, createStep } from "@mastra/core/workflows";
|
|
37
|
+
import { z } from "zod";
|
|
38
|
+
|
|
39
|
+
const step1 = createStep({
|
|
40
|
+
// ...
|
|
41
|
+
execute: async () => {
|
|
42
|
+
const response = await // ...
|
|
43
|
+
|
|
44
|
+
if (!response.ok) {
|
|
45
|
+
throw new Error('Error');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
value: ""
|
|
50
|
+
};
|
|
51
|
+
},
|
|
52
|
+
retries: 3
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Conditional branching
|
|
57
|
+
|
|
58
|
+
You can create alternative workflow paths based on the success or failure of previous steps using conditional logic:
|
|
59
|
+
|
|
60
|
+
```typescript {15,19,33-34} filename="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
|
|
61
|
+
import { createWorkflow, createStep } from "@mastra/core/workflows";
|
|
62
|
+
import { z } from "zod";
|
|
63
|
+
|
|
64
|
+
const step1 = createStep({
|
|
65
|
+
// ...
|
|
66
|
+
execute: async () => {
|
|
67
|
+
try {
|
|
68
|
+
const response = await // ...
|
|
69
|
+
|
|
70
|
+
if (!response.ok) {
|
|
71
|
+
throw new Error('error');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
status: "ok"
|
|
76
|
+
};
|
|
77
|
+
} catch (error) {
|
|
78
|
+
return {
|
|
79
|
+
status: "error"
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const step2 = createStep({...});
|
|
86
|
+
const fallback = createStep({...});
|
|
87
|
+
|
|
88
|
+
export const testWorkflow = createWorkflow({
|
|
89
|
+
// ...
|
|
90
|
+
})
|
|
91
|
+
.then(step1)
|
|
92
|
+
.branch([
|
|
93
|
+
[async ({ inputData: { status } }) => status === "ok", step2],
|
|
94
|
+
[async ({ inputData: { status } }) => status === "error", fallback]
|
|
95
|
+
])
|
|
96
|
+
.commit();
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Check previous step results
|
|
100
|
+
|
|
101
|
+
Use `getStepResult()` to inspect a previous step’s results.
|
|
102
|
+
|
|
103
|
+
```typescript {10} filename="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
|
|
104
|
+
import { createStep } from "@mastra/core/workflows";
|
|
105
|
+
import { z } from "zod";
|
|
106
|
+
|
|
107
|
+
const step1 = createStep({...});
|
|
108
|
+
|
|
109
|
+
const step2 = createStep({
|
|
110
|
+
// ...
|
|
111
|
+
execute: async ({ getStepResult }) => {
|
|
112
|
+
|
|
113
|
+
const step1Result = getStepResult(step1);
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
value: ""
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Exiting early with `bail()`
|
|
123
|
+
|
|
124
|
+
Use `bail()` in a step to exit early with a successful result. This returns the provided payload as the step output and ends workflow execution.
|
|
125
|
+
|
|
126
|
+
```typescript {7} filename="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
|
|
127
|
+
import { createWorkflow, createStep } from "@mastra/core/workflows";
|
|
128
|
+
import { z } from "zod";
|
|
129
|
+
|
|
130
|
+
const step1 = createStep({
|
|
131
|
+
id: 'step1',
|
|
132
|
+
execute: async ({ bail }) => {
|
|
133
|
+
return bail({ result: 'bailed' });
|
|
134
|
+
},
|
|
135
|
+
inputSchema: z.object({ value: z.string() }),
|
|
136
|
+
outputSchema: z.object({ result: z.string() }),
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
export const testWorkflow = createWorkflow({...})
|
|
140
|
+
.then(step1)
|
|
141
|
+
.commit();
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Exiting early with `Error()`
|
|
145
|
+
|
|
146
|
+
Use `throw new Error()` in a step to exit with an error.
|
|
147
|
+
|
|
148
|
+
```typescript {7} filename="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
|
|
149
|
+
import { createWorkflow, createStep } from "@mastra/core/workflows";
|
|
150
|
+
import { z } from "zod";
|
|
151
|
+
|
|
152
|
+
const step1 = createStep({
|
|
153
|
+
id: 'step1',
|
|
154
|
+
execute: async () => {
|
|
155
|
+
throw new Error('error');
|
|
156
|
+
},
|
|
157
|
+
inputSchema: z.object({ value: z.string() }),
|
|
158
|
+
outputSchema: z.object({ result: z.string() }),
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
export const testWorkflow = createWorkflow({...})
|
|
162
|
+
.then(step1)
|
|
163
|
+
.commit();
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Monitor errors with `watch()`
|
|
167
|
+
|
|
168
|
+
You can monitor workflows for errors using the `watch` method:
|
|
169
|
+
|
|
170
|
+
```typescript {11} filename="src/test-workflow.ts" showLineNumbers copy
|
|
171
|
+
import { mastra } from "../src/mastra";
|
|
172
|
+
|
|
173
|
+
const workflow = mastra.getWorkflow("testWorkflow");
|
|
174
|
+
const run = await workflow.createRunAsync();
|
|
175
|
+
|
|
176
|
+
run.watch((event) => {
|
|
177
|
+
const {
|
|
178
|
+
payload: { currentStep }
|
|
179
|
+
} = event;
|
|
180
|
+
|
|
181
|
+
console.log(currentStep?.payload?.status);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Monitor errors with `stream()`
|
|
187
|
+
|
|
188
|
+
You can monitor workflows for errors using `stream`:
|
|
189
|
+
|
|
190
|
+
```typescript {11} filename="src/test-workflow.ts" showLineNumbers copy
|
|
191
|
+
import { mastra } from "../src/mastra";
|
|
192
|
+
|
|
193
|
+
const workflow = mastra.getWorkflow("testWorkflow");
|
|
194
|
+
|
|
195
|
+
const run = await workflow.createRunAsync();
|
|
196
|
+
|
|
197
|
+
const stream = await run.stream({
|
|
198
|
+
inputData: {
|
|
199
|
+
value: "initial data"
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
for await (const chunk of stream.stream) {
|
|
204
|
+
console.log(chunk.payload.output.stats);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## Related
|
|
210
|
+
|
|
211
|
+
- [Control Flow](./control-flow.mdx)
|
|
212
|
+
- [Conditional Branching](./control-flow.mdx#conditional-logic-with-branch)
|
|
213
|
+
- [Running Workflows](../../examples/workflows/running-workflows.mdx)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/mcp-docs-server",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.17-alpha.0",
|
|
4
4
|
"description": "MCP server for accessing Mastra.ai documentation, changelogs, and news.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
"uuid": "^11.1.0",
|
|
33
33
|
"zod": "^3.25.67",
|
|
34
34
|
"zod-to-json-schema": "^3.24.5",
|
|
35
|
-
"@mastra/
|
|
36
|
-
"@mastra/
|
|
35
|
+
"@mastra/core": "0.15.3-alpha.0",
|
|
36
|
+
"@mastra/mcp": "^0.11.3-alpha.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@hono/node-server": "^1.17.1",
|
|
@@ -48,8 +48,8 @@
|
|
|
48
48
|
"tsx": "^4.19.4",
|
|
49
49
|
"typescript": "^5.8.3",
|
|
50
50
|
"vitest": "^3.2.4",
|
|
51
|
-
"@mastra/core": "0.15.
|
|
52
|
-
"@internal/lint": "0.0.
|
|
51
|
+
"@mastra/core": "0.15.3-alpha.0",
|
|
52
|
+
"@internal/lint": "0.0.34"
|
|
53
53
|
},
|
|
54
54
|
"scripts": {
|
|
55
55
|
"prepare-docs": "cross-env PREPARE=true node dist/prepare-docs/prepare.js",
|