@mastra/mcp-docs-server 1.1.16 → 1.1.17-alpha.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.docs/docs/evals/built-in-scorers.md +1 -0
- package/.docs/docs/memory/observational-memory.md +7 -5
- package/.docs/docs/observability/tracing/bridges/otel.md +3 -3
- package/.docs/docs/observability/tracing/exporters/sentry.md +1 -1
- package/.docs/docs/server/auth/okta.md +225 -0
- package/.docs/docs/server/auth.md +1 -0
- package/.docs/docs/workspace/lsp.md +116 -0
- package/.docs/docs/workspace/overview.md +15 -1
- package/.docs/guides/agent-frameworks/ai-sdk.md +3 -3
- package/.docs/models/gateways/openrouter.md +2 -1
- package/.docs/models/index.md +1 -1
- package/.docs/models/providers/llmgateway.md +269 -0
- package/.docs/models/providers/poe.md +3 -1
- package/.docs/models/providers/siliconflow.md +2 -1
- package/.docs/models/providers/zai.md +14 -13
- package/.docs/models/providers/zhipuai-coding-plan.md +3 -1
- package/.docs/models/providers/zhipuai.md +13 -12
- package/.docs/models/providers.md +1 -0
- package/.docs/reference/ai-sdk/handle-chat-stream.md +2 -0
- package/.docs/reference/ai-sdk/with-mastra.md +2 -2
- package/.docs/reference/auth/okta.md +162 -0
- package/.docs/reference/client-js/agents.md +2 -2
- package/.docs/reference/client-js/mastra-client.md +1 -1
- package/.docs/reference/client-js/memory.md +1 -1
- package/.docs/reference/deployer/cloudflare.md +31 -1
- package/.docs/reference/evals/noise-sensitivity.md +3 -3
- package/.docs/reference/evals/run-evals.md +78 -3
- package/.docs/reference/evals/scorer-utils.md +184 -0
- package/.docs/reference/evals/trajectory-accuracy.md +613 -0
- package/.docs/reference/harness/harness-class.md +2 -0
- package/.docs/reference/index.md +3 -2
- package/.docs/reference/logging/pino-logger.md +58 -0
- package/.docs/reference/memory/observational-memory.md +2 -2
- package/.docs/reference/observability/tracing/interfaces.md +1 -1
- package/.docs/reference/processors/message-history-processor.md +1 -1
- package/.docs/reference/processors/processor-interface.md +3 -3
- package/.docs/reference/processors/semantic-recall-processor.md +1 -1
- package/.docs/reference/processors/skill-search-processor.md +93 -0
- package/.docs/reference/processors/tool-call-filter.md +2 -2
- package/.docs/reference/processors/working-memory-processor.md +1 -1
- package/.docs/reference/streaming/agents/stream.md +1 -1
- package/.docs/reference/tools/mcp-client.md +1 -1
- package/CHANGELOG.md +42 -0
- package/package.json +4 -4
- package/.docs/reference/core/getStoredAgentById.md +0 -87
- package/.docs/reference/core/listStoredAgents.md +0 -91
|
@@ -344,7 +344,7 @@ const agent = await mastraClient.createStoredAgent({
|
|
|
344
344
|
instructions: 'You are a helpful assistant.',
|
|
345
345
|
model: {
|
|
346
346
|
provider: 'openai',
|
|
347
|
-
name: 'gpt-4',
|
|
347
|
+
name: 'gpt-5.4',
|
|
348
348
|
},
|
|
349
349
|
})
|
|
350
350
|
```
|
|
@@ -359,7 +359,7 @@ const agent = await mastraClient.createStoredAgent({
|
|
|
359
359
|
instructions: 'You are a helpful assistant.',
|
|
360
360
|
model: {
|
|
361
361
|
provider: 'openai',
|
|
362
|
-
name: 'gpt-4',
|
|
362
|
+
name: 'gpt-5.4',
|
|
363
363
|
},
|
|
364
364
|
tools: ['calculator', 'weather'],
|
|
365
365
|
workflows: ['data-processing'],
|
|
@@ -32,7 +32,7 @@ export const mastraClient = new MastraClient({
|
|
|
32
32
|
|
|
33
33
|
**getAgent(agentId)** (`Agent`): Retrieves a specific agent instance by ID.
|
|
34
34
|
|
|
35
|
-
**
|
|
35
|
+
**listMemoryThreads(params)** (`Promise<StorageThreadType[]>`): Retrieves memory threads for the specified resource and agent. Requires a \`resourceId\` and an \`agentId\`.
|
|
36
36
|
|
|
37
37
|
**createMemoryThread(params)** (`Promise<MemoryThread>`): Creates a new memory thread with the given parameters.
|
|
38
38
|
|
|
@@ -7,7 +7,7 @@ The Memory API provides methods to manage conversation threads and message histo
|
|
|
7
7
|
Retrieve all memory threads for a specific resource:
|
|
8
8
|
|
|
9
9
|
```typescript
|
|
10
|
-
const threads = await mastraClient.
|
|
10
|
+
const threads = await mastraClient.listMemoryThreads({
|
|
11
11
|
resourceId: 'resource-1',
|
|
12
12
|
agentId: 'agent-1', // Optional - can be omitted if storage is configured
|
|
13
13
|
})
|
|
@@ -87,4 +87,34 @@ Use `vars` in the `CloudflareDeployer` constructor only for non-sensitive config
|
|
|
87
87
|
|
|
88
88
|
## Build output
|
|
89
89
|
|
|
90
|
-
After running `mastra build`, the deployer generates a `wrangler.jsonc` file conforming to Cloudflare's [wrangler configuration](https://developers.cloudflare.com/workers/wrangler/configuration/). It points to files inside `.mastra/output` so you need to run `mastra build` before deploying with Wrangler.
|
|
90
|
+
After running `mastra build`, the deployer generates a `wrangler.jsonc` file conforming to Cloudflare's [wrangler configuration](https://developers.cloudflare.com/workers/wrangler/configuration/). It points to files inside `.mastra/output` so you need to run `mastra build` before deploying with Wrangler.
|
|
91
|
+
|
|
92
|
+
## Cloudflare bindings
|
|
93
|
+
|
|
94
|
+
When you use the Cloudflare deployer, you can import runtime bindings from `cloudflare:workers` in your Mastra config file. Mastra automatically preserves protocol-based runtime imports like `cloudflare:workers` during `mastra build` without trying to install them as npm dependencies.
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
import { env } from 'cloudflare:workers'
|
|
98
|
+
import { Mastra } from '@mastra/core'
|
|
99
|
+
import { registerApiRoute } from '@mastra/core/server'
|
|
100
|
+
import { CloudflareDeployer } from '@mastra/deployer-cloudflare'
|
|
101
|
+
|
|
102
|
+
export const mastra = new Mastra({
|
|
103
|
+
deployer: new CloudflareDeployer({
|
|
104
|
+
name: 'my-worker',
|
|
105
|
+
kv_namespaces: [{ binding: 'CACHE', id: 'your-kv-namespace-id' }],
|
|
106
|
+
}),
|
|
107
|
+
server: {
|
|
108
|
+
apiRoutes: [
|
|
109
|
+
registerApiRoute('/bindings', {
|
|
110
|
+
method: 'GET',
|
|
111
|
+
requiresAuth: false,
|
|
112
|
+
handler: async c => {
|
|
113
|
+
await env.CACHE.put('status', 'ok')
|
|
114
|
+
return c.json({ status: await env.CACHE.get('status') })
|
|
115
|
+
},
|
|
116
|
+
}),
|
|
117
|
+
],
|
|
118
|
+
},
|
|
119
|
+
})
|
|
120
|
+
```
|
|
@@ -546,9 +546,9 @@ import { createNoiseSensitivityScorerLLM } from '@mastra/evals'
|
|
|
546
546
|
|
|
547
547
|
async function compareModelRobustness() {
|
|
548
548
|
const models = [
|
|
549
|
-
{ name: 'GPT-5.
|
|
550
|
-
{ name: 'GPT-4
|
|
551
|
-
{ name: 'Claude', model: 'anthropic/claude-
|
|
549
|
+
{ name: 'GPT-5.4', model: 'openai/gpt-5.4' },
|
|
550
|
+
{ name: 'GPT-5.4-mini', model: 'openai/gpt-5.4-mini' },
|
|
551
|
+
{ name: 'Claude', model: 'anthropic/claude-opus-4-6' },
|
|
552
552
|
]
|
|
553
553
|
|
|
554
554
|
const testScenario = {
|
|
@@ -35,7 +35,7 @@ console.log(`Processed ${result.summary.totalItems} items`)
|
|
|
35
35
|
|
|
36
36
|
**data** (`RunEvalsDataItem[]`): Array of test cases with input data and optional ground truth.
|
|
37
37
|
|
|
38
|
-
**scorers** (`MastraScorer[] | WorkflowScorerConfig`):
|
|
38
|
+
**scorers** (`MastraScorer[] | AgentScorerConfig | WorkflowScorerConfig`): Scorers to use. A flat array applies all scorers to the raw output. For agents, an \`AgentScorerConfig\` object separates agent-level and trajectory scorers. For workflows, a \`WorkflowScorerConfig\` object specifies scorers for the workflow, individual steps, and trajectory.
|
|
39
39
|
|
|
40
40
|
**targetOptions** (`AgentExecutionOptions | WorkflowRunOptions`): Options forwarded to the target during execution. For agents: options passed to agent.generate() (e.g. maxSteps, modelSettings, instructions). For workflows: options passed to run.start() (e.g. perStep, outputOptions, initialState).
|
|
41
41
|
|
|
@@ -49,20 +49,32 @@ console.log(`Processed ${result.summary.totalItems} items`)
|
|
|
49
49
|
|
|
50
50
|
**groundTruth** (`any`): Expected or reference output for comparison during scoring.
|
|
51
51
|
|
|
52
|
+
**expectedTrajectory** (`TrajectoryExpectation`): Expected trajectory configuration for trajectory scoring. Includes expected steps, ordering, efficiency budgets, blacklists, and tool failure tolerance. Passed to trajectory scorers as \`run.expectedTrajectory\`. Overrides the static defaults in scorer constructors.
|
|
53
|
+
|
|
52
54
|
**requestContext** (`RequestContext`): Request Context to pass to the target during execution.
|
|
53
55
|
|
|
54
56
|
**tracingContext** (`TracingContext`): Tracing context for observability and debugging.
|
|
55
57
|
|
|
56
58
|
**startOptions** (`WorkflowRunOptions`): Per-item workflow run options (e.g. initialState, perStep, outputOptions). Merged on top of targetOptions, so per-item values take precedence. Only applicable when the target is a workflow.
|
|
57
59
|
|
|
60
|
+
## Agent scorer configuration
|
|
61
|
+
|
|
62
|
+
For agents, use `AgentScorerConfig` to separate agent-level scorers from trajectory scorers:
|
|
63
|
+
|
|
64
|
+
**agent** (`MastraScorer[]`): Scorers that receive the raw agent output (MastraDBMessage\[]). Use for evaluating response quality, content, etc.
|
|
65
|
+
|
|
66
|
+
**trajectory** (`MastraScorer[]`): Scorers that receive a pre-extracted Trajectory object. When storage is configured, the pipeline extracts a hierarchical trajectory from observability traces (including nested tool calls and model generations). Otherwise, it falls back to extracting tool calls from agent messages.
|
|
67
|
+
|
|
58
68
|
## Workflow scorer configuration
|
|
59
69
|
|
|
60
|
-
For workflows,
|
|
70
|
+
For workflows, use `WorkflowScorerConfig` to specify scorers at different levels:
|
|
61
71
|
|
|
62
|
-
**workflow** (`MastraScorer[]`):
|
|
72
|
+
**workflow** (`MastraScorer[]`): Scorers to evaluate the entire workflow output.
|
|
63
73
|
|
|
64
74
|
**steps** (`Record<string, MastraScorer[]>`): Object mapping step IDs to arrays of scorers for evaluating individual step outputs.
|
|
65
75
|
|
|
76
|
+
**trajectory** (`MastraScorer[]`): Scorers that receive a pre-extracted Trajectory from the workflow execution. When storage is configured, the pipeline extracts a hierarchical trajectory from observability traces (including nested agent runs and tool calls within workflow steps). Otherwise, it falls back to extracting step results from the workflow output.
|
|
77
|
+
|
|
66
78
|
## Returns
|
|
67
79
|
|
|
68
80
|
**scores** (`Record<string, any>`): Average scores across all test cases, organized by scorer name.
|
|
@@ -105,6 +117,36 @@ const result = await runEvals({
|
|
|
105
117
|
})
|
|
106
118
|
```
|
|
107
119
|
|
|
120
|
+
### Agent trajectory evaluation
|
|
121
|
+
|
|
122
|
+
Use `AgentScorerConfig` to evaluate both the agent response and its tool-calling trajectory:
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
import { runEvals } from '@mastra/core/evals'
|
|
126
|
+
import { createTrajectoryAccuracyScorerCode } from '@mastra/evals/scorers/code/trajectory'
|
|
127
|
+
|
|
128
|
+
const trajectoryScorer = createTrajectoryAccuracyScorerCode()
|
|
129
|
+
|
|
130
|
+
const result = await runEvals({
|
|
131
|
+
target: chatAgent,
|
|
132
|
+
data: [
|
|
133
|
+
{
|
|
134
|
+
input: 'What is the weather in London?',
|
|
135
|
+
expectedTrajectory: {
|
|
136
|
+
steps: [{ stepType: 'tool_call', name: 'weatherTool' }],
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
scorers: {
|
|
141
|
+
// agent: [responseQualityScorer], // Optional: add agent-level scorers
|
|
142
|
+
trajectory: [trajectoryScorer],
|
|
143
|
+
},
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
// result.scores.agent — average agent-level scores
|
|
147
|
+
// result.scores.trajectory — average trajectory scores
|
|
148
|
+
```
|
|
149
|
+
|
|
108
150
|
### Agent with `targetOptions`
|
|
109
151
|
|
|
110
152
|
Pass execution options like `maxSteps` or `modelSettings` to customize agent behavior during evaluation:
|
|
@@ -149,6 +191,37 @@ const workflowResult = await runEvals({
|
|
|
149
191
|
})
|
|
150
192
|
```
|
|
151
193
|
|
|
194
|
+
### Workflow trajectory evaluation
|
|
195
|
+
|
|
196
|
+
Add trajectory scoring to workflow evaluations to validate step execution order:
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
const workflowResult = await runEvals({
|
|
200
|
+
target: myWorkflow,
|
|
201
|
+
data: [
|
|
202
|
+
{
|
|
203
|
+
input: { query: 'Process this data' },
|
|
204
|
+
expectedTrajectory: {
|
|
205
|
+
steps: [
|
|
206
|
+
{ stepType: 'workflow_step', name: 'validate' },
|
|
207
|
+
{ stepType: 'workflow_step', name: 'process' },
|
|
208
|
+
{ stepType: 'workflow_step', name: 'output' },
|
|
209
|
+
],
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
],
|
|
213
|
+
scorers: {
|
|
214
|
+
workflow: [outputQualityScorer],
|
|
215
|
+
steps: {
|
|
216
|
+
validate: [validationScorer],
|
|
217
|
+
},
|
|
218
|
+
trajectory: [trajectoryScorer],
|
|
219
|
+
},
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
// result.scores.trajectory — workflow trajectory scores
|
|
223
|
+
```
|
|
224
|
+
|
|
152
225
|
### Workflow with per-item `startOptions`
|
|
153
226
|
|
|
154
227
|
Use `startOptions` on individual data items to customize each workflow run. Per-item values take precedence over `targetOptions`:
|
|
@@ -175,5 +248,7 @@ const result = await runEvals({
|
|
|
175
248
|
|
|
176
249
|
- [createScorer()](https://mastra.ai/reference/evals/create-scorer) - Create custom scorers for experiments
|
|
177
250
|
- [MastraScorer](https://mastra.ai/reference/evals/mastra-scorer) - Learn about scorer structure and methods
|
|
251
|
+
- [Trajectory Accuracy](https://mastra.ai/reference/evals/trajectory-accuracy) - Built-in trajectory evaluation scorers
|
|
252
|
+
- [Scorer Utilities](https://mastra.ai/reference/evals/scorer-utils) - Helper functions for extracting trajectory data
|
|
178
253
|
- [Custom Scorers](https://mastra.ai/docs/evals/custom-scorers) - Guide to building evaluation logic
|
|
179
254
|
- [Scorers Overview](https://mastra.ai/docs/evals/overview) - Understanding scorer concepts
|
|
@@ -14,9 +14,21 @@ import {
|
|
|
14
14
|
extractToolCalls,
|
|
15
15
|
extractInputMessages,
|
|
16
16
|
extractAgentResponseMessages,
|
|
17
|
+
compareTrajectories,
|
|
18
|
+
createTrajectoryTestRun,
|
|
17
19
|
} from '@mastra/evals/scorers/utils'
|
|
18
20
|
```
|
|
19
21
|
|
|
22
|
+
Trajectory extraction functions are available from `@mastra/core/evals`:
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import {
|
|
26
|
+
extractTrajectory,
|
|
27
|
+
extractWorkflowTrajectory,
|
|
28
|
+
extractTrajectoryFromTrace,
|
|
29
|
+
} from '@mastra/core/evals'
|
|
30
|
+
```
|
|
31
|
+
|
|
20
32
|
## Message extraction
|
|
21
33
|
|
|
22
34
|
### `getAssistantMessageFromRunOutput`
|
|
@@ -266,6 +278,178 @@ const result = await myScorer.run({
|
|
|
266
278
|
})
|
|
267
279
|
```
|
|
268
280
|
|
|
281
|
+
## Trajectory utilities
|
|
282
|
+
|
|
283
|
+
### `extractTrajectory`
|
|
284
|
+
|
|
285
|
+
Extracts a `Trajectory` from agent output messages (`MastraDBMessage[]`). Converts tool invocations into `ToolCallStep` objects. The `runEvals` pipeline calls this automatically for trajectory scorers — you only need it for direct testing.
|
|
286
|
+
|
|
287
|
+
Available from `@mastra/core/evals`.
|
|
288
|
+
|
|
289
|
+
```typescript
|
|
290
|
+
import { extractTrajectory } from '@mastra/core/evals'
|
|
291
|
+
|
|
292
|
+
const trajectory = extractTrajectory(agentOutputMessages)
|
|
293
|
+
// trajectory.steps — ToolCallStep[] extracted from toolInvocations
|
|
294
|
+
// trajectory.rawOutput — the original MastraDBMessage[] array
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
**Returns:** `Trajectory` — Contains `steps: TrajectoryStep[]`, `totalDurationMs`, and `rawOutput`.
|
|
298
|
+
|
|
299
|
+
### `extractWorkflowTrajectory`
|
|
300
|
+
|
|
301
|
+
Extracts a `Trajectory` from workflow step results. Converts `StepResult` records into `WorkflowStepStep` objects, respecting the execution path ordering.
|
|
302
|
+
|
|
303
|
+
Available from `@mastra/core/evals`.
|
|
304
|
+
|
|
305
|
+
```typescript
|
|
306
|
+
import { extractWorkflowTrajectory } from '@mastra/core/evals'
|
|
307
|
+
|
|
308
|
+
const trajectory = extractWorkflowTrajectory(
|
|
309
|
+
workflowResult.steps, // Record<string, StepResult>
|
|
310
|
+
workflowResult.stepExecutionPath, // string[] (optional)
|
|
311
|
+
)
|
|
312
|
+
// trajectory.steps — WorkflowStepStep[] in execution order
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
**Returns:** `Trajectory` — Contains `steps: TrajectoryStep[]`, `totalDurationMs`, and `rawWorkflowResult`.
|
|
316
|
+
|
|
317
|
+
### `extractTrajectoryFromTrace`
|
|
318
|
+
|
|
319
|
+
Builds a hierarchical `Trajectory` from observability trace spans (`SpanRecord[]`). Reconstructs the parent-child span tree and maps each span to the appropriate `TrajectoryStep` discriminated union type with nested `children`.
|
|
320
|
+
|
|
321
|
+
This is the preferred extraction method when storage is available. The `runEvals` pipeline calls this automatically when the target's `Mastra` instance has a configured storage backend. It produces richer trajectories than `extractTrajectory` or `extractWorkflowTrajectory` because it captures the full execution tree, including nested agent runs, tool calls, and model generations.
|
|
322
|
+
|
|
323
|
+
Available from `@mastra/core/evals`.
|
|
324
|
+
|
|
325
|
+
```typescript
|
|
326
|
+
import { extractTrajectoryFromTrace } from '@mastra/core/evals'
|
|
327
|
+
|
|
328
|
+
// After fetching a trace from the observability store
|
|
329
|
+
const traceData = await observabilityStore.getTrace({ traceId })
|
|
330
|
+
const trajectory = extractTrajectoryFromTrace(traceData.spans, rootSpanId)
|
|
331
|
+
// trajectory.steps — hierarchical TrajectoryStep[] with children
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
**Parameters:**
|
|
335
|
+
|
|
336
|
+
- `spans` (`SpanRecord[]`) — Array of span records from a trace query.
|
|
337
|
+
- `rootSpanId` (`string`, optional) — Span ID to use as the starting point. When omitted, uses spans with no parent.
|
|
338
|
+
|
|
339
|
+
**Returns:** `Trajectory` — Contains `steps: TrajectoryStep[]` with recursive `children` and `totalDurationMs`.
|
|
340
|
+
|
|
341
|
+
#### Span type mapping
|
|
342
|
+
|
|
343
|
+
| Span type | Trajectory step type | Key fields extracted |
|
|
344
|
+
| ---------------------- | ---------------------- | ------------------------------------------------------------- |
|
|
345
|
+
| `TOOL_CALL` | `tool_call` | `toolArgs`, `toolResult`, `success` |
|
|
346
|
+
| `MCP_TOOL_CALL` | `mcp_tool_call` | `toolArgs`, `toolResult`, `mcpServer`, `success` |
|
|
347
|
+
| `MODEL_GENERATION` | `model_generation` | `modelId`, `promptTokens`, `completionTokens`, `finishReason` |
|
|
348
|
+
| `AGENT_RUN` | `agent_run` | `agentId` (from entity ID) |
|
|
349
|
+
| `WORKFLOW_RUN` | `workflow_run` | `workflowId` (from entity ID) |
|
|
350
|
+
| `WORKFLOW_STEP` | `workflow_step` | `output` |
|
|
351
|
+
| `WORKFLOW_CONDITIONAL` | `workflow_conditional` | `conditionCount`, `selectedSteps` |
|
|
352
|
+
| `WORKFLOW_PARALLEL` | `workflow_parallel` | `branchCount`, `parallelSteps` |
|
|
353
|
+
| `WORKFLOW_LOOP` | `workflow_loop` | `loopType`, `totalIterations` |
|
|
354
|
+
| `WORKFLOW_SLEEP` | `workflow_sleep` | `sleepDurationMs`, `sleepType` |
|
|
355
|
+
| `WORKFLOW_WAIT_EVENT` | `workflow_wait_event` | `eventName`, `eventReceived` |
|
|
356
|
+
| `PROCESSOR_RUN` | `processor_run` | `processorId` |
|
|
357
|
+
|
|
358
|
+
Spans with types `GENERIC`, `MODEL_STEP`, `MODEL_CHUNK`, and `WORKFLOW_CONDITIONAL_EVAL` are skipped as noise.
|
|
359
|
+
|
|
360
|
+
### `compareTrajectories`
|
|
361
|
+
|
|
362
|
+
Compares an actual trajectory against an expected trajectory and returns a detailed comparison result. Used internally by `createTrajectoryAccuracyScorerCode`.
|
|
363
|
+
|
|
364
|
+
The `expected` parameter accepts either a `Trajectory` (actual trajectory) or `{ steps: ExpectedStep[] }`. When using `ExpectedStep[]`, you can match by name only, name + stepType, or include data for comparison. See [Expected steps](https://mastra.ai/reference/evals/trajectory-accuracy) for details.
|
|
365
|
+
|
|
366
|
+
```typescript
|
|
367
|
+
import { compareTrajectories } from '@mastra/evals/scorers/utils'
|
|
368
|
+
|
|
369
|
+
// Using ExpectedStep[] (recommended for expectations)
|
|
370
|
+
const result = compareTrajectories(
|
|
371
|
+
actualTrajectory,
|
|
372
|
+
{ steps: [{ name: 'search' }, { name: 'summarize', stepType: 'tool_call' }] },
|
|
373
|
+
{ compareStepData: false, allowRepeatedSteps: true },
|
|
374
|
+
)
|
|
375
|
+
// result.score — 0.0 to 1.0
|
|
376
|
+
// result.missingSteps — step names not found
|
|
377
|
+
// result.extraSteps — unexpected step names
|
|
378
|
+
// result.outOfOrderSteps — steps found but in wrong order
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
**Returns:** `TrajectoryComparisonResult`
|
|
382
|
+
|
|
383
|
+
### `createTrajectoryTestRun`
|
|
384
|
+
|
|
385
|
+
Creates a test run object for trajectory scorers. Wraps a `Trajectory` into the expected `ScorerRun` format.
|
|
386
|
+
|
|
387
|
+
```typescript
|
|
388
|
+
import { createTrajectoryTestRun } from '@mastra/evals/scorers/utils'
|
|
389
|
+
|
|
390
|
+
const run = createTrajectoryTestRun({
|
|
391
|
+
steps: [
|
|
392
|
+
{ stepType: 'tool_call', name: 'search', toolArgs: { q: 'test' } },
|
|
393
|
+
{ stepType: 'tool_call', name: 'summarize' },
|
|
394
|
+
],
|
|
395
|
+
})
|
|
396
|
+
|
|
397
|
+
const result = await trajectoryScorer.run(run)
|
|
398
|
+
```
|
|
399
|
+
|
|
400
|
+
### `checkTrajectoryEfficiency`
|
|
401
|
+
|
|
402
|
+
Evaluates trajectory efficiency against step, token, and duration budgets. Also detects redundant calls (same tool with same arguments).
|
|
403
|
+
|
|
404
|
+
```typescript
|
|
405
|
+
import { checkTrajectoryEfficiency } from '@mastra/evals/scorers/utils'
|
|
406
|
+
|
|
407
|
+
const result = checkTrajectoryEfficiency(trajectory, {
|
|
408
|
+
maxSteps: 5,
|
|
409
|
+
maxTotalTokens: 2000,
|
|
410
|
+
maxTotalDurationMs: 5000,
|
|
411
|
+
noRedundantCalls: true,
|
|
412
|
+
})
|
|
413
|
+
// result.score — 1.0 if within all budgets, lower with penalties
|
|
414
|
+
// result.redundantCalls — duplicate tool+args combos
|
|
415
|
+
// result.overBudget — which budgets were exceeded
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
**Returns:** `TrajectoryEfficiencyResult`
|
|
419
|
+
|
|
420
|
+
### `checkTrajectoryBlacklist`
|
|
421
|
+
|
|
422
|
+
Checks whether a trajectory contains forbidden tools or tool call sequences.
|
|
423
|
+
|
|
424
|
+
```typescript
|
|
425
|
+
import { checkTrajectoryBlacklist } from '@mastra/evals/scorers/utils'
|
|
426
|
+
|
|
427
|
+
const result = checkTrajectoryBlacklist(trajectory, {
|
|
428
|
+
blacklistedTools: ['deleteAll', 'admin-override'],
|
|
429
|
+
blacklistedSequences: [['escalate', 'admin-override']],
|
|
430
|
+
})
|
|
431
|
+
// result.passed — true if no violations
|
|
432
|
+
// result.violations — list of violations with type and details
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
**Returns:** `TrajectoryBlacklistResult`
|
|
436
|
+
|
|
437
|
+
### `analyzeToolFailures`
|
|
438
|
+
|
|
439
|
+
Detects tool failure patterns including retries, fallbacks, and argument corrections.
|
|
440
|
+
|
|
441
|
+
```typescript
|
|
442
|
+
import { analyzeToolFailures } from '@mastra/evals/scorers/utils'
|
|
443
|
+
|
|
444
|
+
const result = analyzeToolFailures(trajectory, {
|
|
445
|
+
maxRetriesPerTool: 3,
|
|
446
|
+
})
|
|
447
|
+
// result.score — 1.0 if no failure patterns, lower if patterns detected
|
|
448
|
+
// result.patterns — detected patterns (retry, fallback, arg_correction)
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
**Returns:** `ToolFailureAnalysisResult`
|
|
452
|
+
|
|
269
453
|
## Complete example
|
|
270
454
|
|
|
271
455
|
Here's a complete example showing how to use multiple utilities together:
|