@mastra/mcp-docs-server 1.2.14-alpha.3 → 1.2.14
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/agents/processors.md +25 -1
- package/.docs/docs/workflows/agents-and-tools.md +29 -0
- package/.docs/docs/workflows/stored-workflows.md +146 -0
- package/.docs/models/environment-variables.md +1 -1
- package/.docs/models/gateways/neon.md +71 -0
- package/.docs/models/gateways/vercel.md +1 -1
- package/.docs/models/gateways.md +1 -0
- package/.docs/models/index.md +1 -1
- package/.docs/models/providers/ambient.md +1 -1
- package/.docs/models/providers/cortecs.md +112 -66
- package/.docs/models/providers/hyper.md +6 -6
- package/.docs/models/providers/kilo.md +1 -1
- package/.docs/models/providers/llmgateway.md +1 -1
- package/.docs/models/providers/minimax.md +23 -25
- package/.docs/models/providers/nano-gpt.md +1 -1
- package/.docs/models/providers/openai.md +26 -28
- package/.docs/models/providers/perplexity-agent.md +24 -24
- package/.docs/models/providers.md +0 -1
- package/.docs/reference/client-js/workflows.md +92 -0
- package/.docs/reference/core/addStoredWorkflow.md +62 -0
- package/.docs/reference/core/addStoredWorkflows.md +40 -0
- package/.docs/reference/index.md +5 -0
- package/.docs/reference/processors/processor-interface.md +121 -10
- package/.docs/reference/server/routes.md +13 -0
- package/.docs/reference/storage/overview.md +9 -8
- package/.docs/reference/streaming/workflows/observeStream.md +1 -1
- package/.docs/reference/streaming/workflows/resumeStream.md +1 -1
- package/.docs/reference/streaming/workflows/stream.md +1 -1
- package/.docs/reference/workflows/stored-workflow-definition.md +292 -0
- package/.docs/reference/workflows/workflow-methods/agent.md +62 -0
- package/.docs/reference/workflows/workflow-methods/tool.md +43 -0
- package/CHANGELOG.md +14 -0
- package/package.json +5 -5
- package/.docs/models/providers/neon.md +0 -109
|
@@ -58,6 +58,11 @@ Processor methods run at different points in the agent execution lifecycle:
|
|
|
58
58
|
│ │ ▼ │ │
|
|
59
59
|
│ │ Tool Execution (if needed) │ │
|
|
60
60
|
│ │ │ │ │
|
|
61
|
+
│ │ ▼ │ │
|
|
62
|
+
│ │ ┌────────────────────────┐ │ │
|
|
63
|
+
│ │ │ processToolResult │ ← Runs per tool, after each │ │
|
|
64
|
+
│ │ └───────────┬────────────┘ tool.execute() returns │ │
|
|
65
|
+
│ │ │ │ │
|
|
61
66
|
│ │ └──────── Loop back if tools called ────────────│ │
|
|
62
67
|
│ │ │ │
|
|
63
68
|
│ └──────────────────────────────────────────────────────────────┘ │
|
|
@@ -73,16 +78,17 @@ Processor methods run at different points in the agent execution lifecycle:
|
|
|
73
78
|
└────────────────────────────────────────────────────────────────────┘
|
|
74
79
|
```
|
|
75
80
|
|
|
76
|
-
| Method | When it runs
|
|
77
|
-
| --------------------- |
|
|
78
|
-
| `processInput` | Once at the start, before the agentic loop
|
|
79
|
-
| `processInputStep` | At each step of the agentic loop, before each LLM call
|
|
80
|
-
| `processLLMRequest` | After LLM request conversion, before the provider call
|
|
81
|
-
| `processAPIError` | When an LLM API call fails
|
|
82
|
-
| `processOutputStream` | On each streaming chunk during LLM response
|
|
83
|
-
| `processLLMResponse` | After the LLM step completes and stream chunks are collected
|
|
84
|
-
| `processOutputStep` | After each LLM response, before tool execution
|
|
85
|
-
| `
|
|
81
|
+
| Method | When it runs | Use case |
|
|
82
|
+
| --------------------- | ------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------- |
|
|
83
|
+
| `processInput` | Once at the start, before the agentic loop | Validate/transform initial user input, add context |
|
|
84
|
+
| `processInputStep` | At each step of the agentic loop, before each LLM call | Transform messages between steps, handle tool results |
|
|
85
|
+
| `processLLMRequest` | After LLM request conversion, before the provider call | Rewrite the outbound `LanguageModelV2Prompt` for the current call without persisting changes |
|
|
86
|
+
| `processAPIError` | When an LLM API call fails | Inspect API rejections, optionally mutate state/messages, and request a retry |
|
|
87
|
+
| `processOutputStream` | On each streaming chunk during LLM response | Filter/modify streaming content, detect patterns in real-time |
|
|
88
|
+
| `processLLMResponse` | After the LLM step completes and stream chunks are collected | Capture or cache the full response, run post-call side effects paired with `processLLMRequest` |
|
|
89
|
+
| `processOutputStep` | After each LLM response, before tool execution | Validate output quality, implement guardrails with retry |
|
|
90
|
+
| `processToolResult` | Per tool, after `tool.execute()` returns and before the result is added to the message list | Scan tool output for prompt injection, redact sensitive fields, abort on policy violations |
|
|
91
|
+
| `processOutputResult` | Once after generation completes | Post-process final response, log results |
|
|
86
92
|
|
|
87
93
|
## Interface definition
|
|
88
94
|
|
|
@@ -131,6 +137,8 @@ interface Processor<TId extends string = string, TTripwireMetadata = unknown> {
|
|
|
131
137
|
|
|
132
138
|
processOutputStep?(args: ProcessOutputStepArgs<TTripwireMetadata>): ProcessorMessageResult
|
|
133
139
|
|
|
140
|
+
processToolResult?(args: ProcessToolResultArgs<TTripwireMetadata>): ProcessorMessageResult
|
|
141
|
+
|
|
134
142
|
processOutputResult?(args: ProcessOutputResultArgs<TTripwireMetadata>): ProcessorMessageResult
|
|
135
143
|
}
|
|
136
144
|
```
|
|
@@ -698,6 +706,109 @@ export class QualityGuardrail implements Processor {
|
|
|
698
706
|
}
|
|
699
707
|
```
|
|
700
708
|
|
|
709
|
+
### `processToolResult`
|
|
710
|
+
|
|
711
|
+
Processes a tool's result after `tool.execute()` returns and before the result is added to the message list or fed to the next LLM call. Symmetric with `processOutputStep`, which fires before tool execution. Use this method to scan tool output for prompt injection, redact sensitive fields, or abort the run with `abort('reason', { retry: true })`.
|
|
712
|
+
|
|
713
|
+
To replace the tool's result, mutate `messageList` in place via `messageList.updateToolInvocation`. The runtime re-reads the post-processor result from the message list and overwrites the downstream tool-result stream chunk before it is enqueued, so streaming clients see the processed value.
|
|
714
|
+
|
|
715
|
+
This method does not fire when `tool.execute()` throws; it is called only for successful tool executions where a result is available.
|
|
716
|
+
|
|
717
|
+
```typescript
|
|
718
|
+
processToolResult?(args: ProcessToolResultArgs): ProcessorMessageResult;
|
|
719
|
+
```
|
|
720
|
+
|
|
721
|
+
#### `ProcessToolResultArgs`
|
|
722
|
+
|
|
723
|
+
**messages** (`MastraDBMessage[]`): All messages including the current assistant message with the tool call.
|
|
724
|
+
|
|
725
|
+
**messageList** (`MessageList`): MessageList instance for managing messages. Call updateToolInvocation to replace the tool result with a redacted or transformed value.
|
|
726
|
+
|
|
727
|
+
**stepNumber** (`number`): Current step number (0-indexed).
|
|
728
|
+
|
|
729
|
+
**toolName** (`string`): Name of the tool that was executed.
|
|
730
|
+
|
|
731
|
+
**toolCallId** (`string`): Unique identifier for this specific tool call.
|
|
732
|
+
|
|
733
|
+
**args** (`unknown`): Arguments the LLM passed to the tool.
|
|
734
|
+
|
|
735
|
+
**result** (`unknown`): Value returned by the tool. For client-executed tools this is the output of tool.execute() after it has passed through ensureSerializable. For provider-executed tools (e.g. Anthropic web\_search) it is the raw result from the provider stream, which is not run through ensureSerializable.
|
|
736
|
+
|
|
737
|
+
**providerExecuted** (`boolean`): Whether this result came from a provider-executed tool such as Anthropic web\_search. Defaults to undefined for client-executed tools.
|
|
738
|
+
|
|
739
|
+
**systemMessages** (`CoreMessage[]`): All system messages for read access.
|
|
740
|
+
|
|
741
|
+
**steps** (`StepResult[]`): All completed steps so far.
|
|
742
|
+
|
|
743
|
+
**state** (`Record<string, unknown>`): Per-processor state that persists across all method calls within this request. Shared with other processor methods on the same processor.
|
|
744
|
+
|
|
745
|
+
**abort** (`(reason?: string, options?: { retry?: boolean; metadata?: unknown }) => never`): Function to abort the run. Pass retry: true to request the LLM retry the step with the abort reason as feedback.
|
|
746
|
+
|
|
747
|
+
**retryCount** (`number`): Number of times processors have triggered retry. Starts at 0.
|
|
748
|
+
|
|
749
|
+
**tracingContext** (`TracingContext`): Tracing context for observability.
|
|
750
|
+
|
|
751
|
+
**requestContext** (`RequestContext`): Request-scoped context with execution metadata.
|
|
752
|
+
|
|
753
|
+
#### Use cases
|
|
754
|
+
|
|
755
|
+
- Scanning tool output for prompt injection before the LLM sees it.
|
|
756
|
+
- Redacting sensitive fields from tool returns (PII, secrets, credentials).
|
|
757
|
+
- Aborting the run when a tool returns content that violates policy.
|
|
758
|
+
- Logging or instrumenting tool returns for compliance or audit.
|
|
759
|
+
|
|
760
|
+
#### Example: Redact sensitive fields
|
|
761
|
+
|
|
762
|
+
```typescript
|
|
763
|
+
import type { Processor } from '@mastra/core/processors'
|
|
764
|
+
|
|
765
|
+
export class RedactToolResult implements Processor {
|
|
766
|
+
id = 'redact-tool-result'
|
|
767
|
+
|
|
768
|
+
async processToolResult({ toolName, toolCallId, args, result, messageList }) {
|
|
769
|
+
if (toolName !== 'lookup-customer') return
|
|
770
|
+
|
|
771
|
+
const redacted = {
|
|
772
|
+
...(result as Record<string, unknown>),
|
|
773
|
+
ssn: '[REDACTED]',
|
|
774
|
+
email: '[REDACTED]',
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
messageList.updateToolInvocation({
|
|
778
|
+
type: 'tool-invocation',
|
|
779
|
+
toolInvocation: {
|
|
780
|
+
state: 'result',
|
|
781
|
+
toolCallId,
|
|
782
|
+
toolName,
|
|
783
|
+
args,
|
|
784
|
+
result: redacted,
|
|
785
|
+
},
|
|
786
|
+
})
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
```
|
|
790
|
+
|
|
791
|
+
#### Example: Block prompt injection in tool output
|
|
792
|
+
|
|
793
|
+
```typescript
|
|
794
|
+
import type { Processor } from '@mastra/core/processors'
|
|
795
|
+
|
|
796
|
+
export class ScanToolResult implements Processor {
|
|
797
|
+
id = 'scan-tool-result'
|
|
798
|
+
|
|
799
|
+
async processToolResult({ result, abort }) {
|
|
800
|
+
const text = typeof result === 'string' ? result : JSON.stringify(result)
|
|
801
|
+
if (containsPromptInjection(text)) {
|
|
802
|
+
abort('blocked by scan-tool-result: suspected prompt injection')
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
function containsPromptInjection(text: string): boolean {
|
|
808
|
+
return /ignore (all )?(previous|prior) instructions/i.test(text)
|
|
809
|
+
}
|
|
810
|
+
```
|
|
811
|
+
|
|
701
812
|
## Processor types
|
|
702
813
|
|
|
703
814
|
Mastra provides type aliases to ensure processors implement the required methods:
|
|
@@ -183,6 +183,19 @@ The route returns:
|
|
|
183
183
|
| `GET` | `/api/workflows/:workflowId/runs` | List workflow runs |
|
|
184
184
|
| `GET` | `/api/workflows/:workflowId/runs/:runId` | Get specific run |
|
|
185
185
|
|
|
186
|
+
### Stored workflows
|
|
187
|
+
|
|
188
|
+
Stored workflow definitions (beta) are workflows expressed as JSON, persisted through the `workflowDefinitions` storage domain, and live-registered on the running instance. See [Stored workflows](https://mastra.ai/docs/workflows/stored-workflows).
|
|
189
|
+
|
|
190
|
+
| Method | Path | Description |
|
|
191
|
+
| -------- | ----------------------------------------- | ------------------------------------------------------------------------------ |
|
|
192
|
+
| `GET` | `/api/stored/workflows` | List stored workflow definitions, filterable by `status` and `authorId` |
|
|
193
|
+
| `GET` | `/api/stored/workflows/:storedWorkflowId` | Get a stored workflow definition by ID |
|
|
194
|
+
| `POST` | `/api/stored/workflows` | Upsert a definition (plus optional helper `dependencies`) and live-register it |
|
|
195
|
+
| `DELETE` | `/api/stored/workflows/:storedWorkflowId` | Delete a stored definition and unregister the live workflow |
|
|
196
|
+
|
|
197
|
+
On authenticated servers, the read routes require the `stored-workflows:read` permission and the write routes require `stored-workflows:write`. Registered stored workflows are executed through the ordinary `/api/workflows/:workflowId` routes above.
|
|
198
|
+
|
|
186
199
|
### Create run request body
|
|
187
200
|
|
|
188
201
|
```typescript
|
|
@@ -10,14 +10,15 @@ Mastra storage is organized into domains. Each domain owns a set of tables or co
|
|
|
10
10
|
|
|
11
11
|
Not every storage adapter implements every domain. Composite storage lets you mix adapters per domain when the adapter packages export the corresponding domain classes.
|
|
12
12
|
|
|
13
|
-
| Domain
|
|
14
|
-
|
|
|
15
|
-
| `memory`
|
|
16
|
-
| `workflows`
|
|
17
|
-
| `
|
|
18
|
-
| `
|
|
19
|
-
| `
|
|
20
|
-
| `
|
|
13
|
+
| Domain | Description |
|
|
14
|
+
| --------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
|
|
15
|
+
| `memory` | Conversation persistence: messages, threads, and resources (including working memory). |
|
|
16
|
+
| `workflows` | Workflow run snapshots used for suspend and resume. |
|
|
17
|
+
| `workflowDefinitions` | Persisted [stored workflow](https://mastra.ai/docs/workflows/stored-workflows) definitions (beta). Loaded and live-registered on boot. |
|
|
18
|
+
| `scores` | Evaluation score records from eval runs. |
|
|
19
|
+
| `observability` | Traces and spans used by observability exporters and Studio. |
|
|
20
|
+
| `datasets` | Dataset records, versioned items, and dataset versions used by experiments. |
|
|
21
|
+
| `experiments` | Experiment runs and per-item experiment results. |
|
|
21
22
|
|
|
22
23
|
The schema definitions below cover the built-in database-backed tables documented for `memory`, `workflows`, `scores`, and `observability`. Other domains, and non-database adapters, use implementation-specific storage structures.
|
|
23
24
|
|
|
@@ -34,7 +34,7 @@ The stream emits event types during workflow execution. Each event has a `type`
|
|
|
34
34
|
- **`workflow-step-start`**: A step begins execution
|
|
35
35
|
- **`workflow-step-output`**: Custom output from a step
|
|
36
36
|
- **`workflow-step-result`**: A step completes with results
|
|
37
|
-
- **`workflow-finish`**: Workflow execution completes with usage statistics
|
|
37
|
+
- **`workflow-finish`**: Workflow execution completes with usage statistics. For successful runs, `payload.finalWorkflowResult` carries the workflow's final result
|
|
38
38
|
|
|
39
39
|
## Related
|
|
40
40
|
|
|
@@ -66,7 +66,7 @@ The stream emits event types during workflow execution. Each event has a `type`
|
|
|
66
66
|
- **`workflow-step-start`**: A step begins execution
|
|
67
67
|
- **`workflow-step-output`**: Custom output from a step
|
|
68
68
|
- **`workflow-step-result`**: A step completes with results
|
|
69
|
-
- **`workflow-finish`**: Workflow execution completes with usage statistics
|
|
69
|
+
- **`workflow-finish`**: Workflow execution completes with usage statistics. For successful runs, `payload.finalWorkflowResult` carries the workflow's final result
|
|
70
70
|
|
|
71
71
|
## Related
|
|
72
72
|
|
|
@@ -93,7 +93,7 @@ The stream emits event types during workflow execution. Each event has a `type`
|
|
|
93
93
|
- **`workflow-step-output`**: Custom output from a step
|
|
94
94
|
- **`workflow-step-progress`**: A foreach step reports per-iteration progress (includes `completedCount`, `totalCount`, `currentIndex`, `iterationStatus`, and optional `iterationOutput`)
|
|
95
95
|
- **`workflow-step-result`**: A step completes with results
|
|
96
|
-
- **`workflow-finish`**: Workflow execution completes with usage statistics
|
|
96
|
+
- **`workflow-finish`**: Workflow execution completes with usage statistics. For successful runs, `payload.finalWorkflowResult` carries the workflow's final result so stream consumers don't need a follow-up fetch
|
|
97
97
|
|
|
98
98
|
## Related
|
|
99
99
|
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Stored workflow definition
|
|
4
|
+
|
|
5
|
+
> **Beta:** Stored workflows are in beta. Breaking changes may occur without a major version bump until the API is stable.
|
|
6
|
+
|
|
7
|
+
A stored workflow definition is a JSON-compatible `StoredWorkflowGraph` accepted by [`Mastra.addStoredWorkflow()`](https://mastra.ai/reference/core/addStoredWorkflow), the stored-workflow server routes, and the Client SDK workflows API.
|
|
8
|
+
|
|
9
|
+
See [Stored workflows](https://mastra.ai/docs/workflows/stored-workflows) for a complete setup and usage example.
|
|
10
|
+
|
|
11
|
+
## Definition fields
|
|
12
|
+
|
|
13
|
+
| Field | Type | Required | Description |
|
|
14
|
+
| ---------------------- | --------------------------- | -------- | ------------------------------------------------------------------------------ |
|
|
15
|
+
| `id` | `string` | Yes | Unique workflow ID. This is also the ID used to retrieve and run the workflow. |
|
|
16
|
+
| `description` | `string` | No | Human-readable description |
|
|
17
|
+
| `inputSchema` | `JsonSchema` | Yes | JSON Schema for the workflow input |
|
|
18
|
+
| `outputSchema` | `JsonSchema` | Yes | JSON Schema for the workflow output |
|
|
19
|
+
| `stateSchema` | `JsonSchema` | No | JSON Schema for shared workflow state |
|
|
20
|
+
| `requestContextSchema` | `JsonSchema` | No | JSON Schema for values read from the request context |
|
|
21
|
+
| `metadata` | `Record<string, unknown>` | No | Arbitrary JSON metadata preserved through storage |
|
|
22
|
+
| `graph` | `SerializedStepFlowEntry[]` | Yes | Step entries that make up the workflow |
|
|
23
|
+
|
|
24
|
+
Schemas use JSON Schema rather than Zod so the definition can round-trip through JSON. Mastra converts each schema to Zod when it registers the workflow.
|
|
25
|
+
|
|
26
|
+
```json
|
|
27
|
+
{
|
|
28
|
+
"id": "greeting-workflow",
|
|
29
|
+
"description": "Returns a greeting for the supplied name",
|
|
30
|
+
"inputSchema": {
|
|
31
|
+
"type": "object",
|
|
32
|
+
"properties": { "name": { "type": "string" } },
|
|
33
|
+
"required": ["name"]
|
|
34
|
+
},
|
|
35
|
+
"outputSchema": {
|
|
36
|
+
"type": "object",
|
|
37
|
+
"properties": { "message": { "type": "string" } },
|
|
38
|
+
"required": ["message"]
|
|
39
|
+
},
|
|
40
|
+
"graph": [
|
|
41
|
+
{
|
|
42
|
+
"type": "mapping",
|
|
43
|
+
"id": "create-greeting",
|
|
44
|
+
"mapConfig": "{\"message\":{\"template\":\"Hello, ${initData.name}!\"}}"
|
|
45
|
+
}
|
|
46
|
+
]
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Graph entries
|
|
51
|
+
|
|
52
|
+
Entries in the `graph` run in order. Each entry receives the previous entry's output, and the first entry receives the workflow input.
|
|
53
|
+
|
|
54
|
+
| Entry type | Description |
|
|
55
|
+
| ------------- | ------------------------------------------------------ |
|
|
56
|
+
| `agent` | Invoke a registered agent |
|
|
57
|
+
| `tool` | Invoke a registered tool |
|
|
58
|
+
| `mapping` | Reshape data between steps |
|
|
59
|
+
| `workflow` | Invoke a registered workflow as a nested step |
|
|
60
|
+
| `parallel` | Run several steps concurrently and merge their outputs |
|
|
61
|
+
| `conditional` | Run every branch whose predicate is true, concurrently |
|
|
62
|
+
| `foreach` | Run one step per item of an array input |
|
|
63
|
+
| `loop` | Repeat a step while or until a predicate holds |
|
|
64
|
+
| `sleep` | Pause for a fixed duration |
|
|
65
|
+
| `sleepUntil` | Pause until a fixed date |
|
|
66
|
+
|
|
67
|
+
Code-defined workflows that use [`.agent()`](https://mastra.ai/reference/workflows/workflow-methods/agent) and [`.tool()`](https://mastra.ai/reference/workflows/workflow-methods/tool) produce the same declarative entries when serialized.
|
|
68
|
+
|
|
69
|
+
### Agent steps
|
|
70
|
+
|
|
71
|
+
An `agent` entry invokes a registered agent by ID. Agent steps accept `{ prompt: string }` as input and return `{ text: string }` by default.
|
|
72
|
+
|
|
73
|
+
```json
|
|
74
|
+
{
|
|
75
|
+
"type": "agent",
|
|
76
|
+
"id": "summarize",
|
|
77
|
+
"agentId": "support-agent"
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
The `id` identifies this call site within the workflow. Later steps address the result as `stepResults.summarize`, regardless of the agent's own ID.
|
|
82
|
+
|
|
83
|
+
Add an `outputSchema` to request structured output from the agent:
|
|
84
|
+
|
|
85
|
+
```json
|
|
86
|
+
{
|
|
87
|
+
"type": "agent",
|
|
88
|
+
"id": "extract-subtopics",
|
|
89
|
+
"agentId": "support-agent",
|
|
90
|
+
"outputSchema": {
|
|
91
|
+
"type": "array",
|
|
92
|
+
"items": {
|
|
93
|
+
"type": "object",
|
|
94
|
+
"properties": { "title": { "type": "string" } },
|
|
95
|
+
"required": ["title"]
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Use a `mapping` entry before an agent to build its `{ prompt }` input from workflow data.
|
|
102
|
+
|
|
103
|
+
Agent entries accept an optional `description` and an `options` object:
|
|
104
|
+
|
|
105
|
+
```json
|
|
106
|
+
{
|
|
107
|
+
"type": "agent",
|
|
108
|
+
"id": "summarize",
|
|
109
|
+
"agentId": "support-agent",
|
|
110
|
+
"description": "Summarize the incoming request",
|
|
111
|
+
"options": { "retries": 2, "metadata": { "team": "support" } }
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Only `retries` and `metadata` persist. Function-valued options such as `onFinish` and function-valued `toolChoice` are rejected when a code-defined workflow is stored. Other agent call options don't persist.
|
|
116
|
+
|
|
117
|
+
### Tool steps
|
|
118
|
+
|
|
119
|
+
A `tool` entry invokes a tool by its registration key from the `Mastra` `tools` object. Mastra resolves the tool's input and output schemas from the registry when it registers the workflow.
|
|
120
|
+
|
|
121
|
+
```json
|
|
122
|
+
{
|
|
123
|
+
"type": "tool",
|
|
124
|
+
"id": "lookup",
|
|
125
|
+
"toolId": "lookup-customer"
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Tool entries accept the same optional `description` and `options` fields as agent entries. Only `retries` and `metadata` persist.
|
|
130
|
+
|
|
131
|
+
### Mapping steps
|
|
132
|
+
|
|
133
|
+
A `mapping` entry reshapes data. Its `mapConfig` is a JSON string that encodes an object. Each key becomes a key in the step output, and each descriptor defines one source.
|
|
134
|
+
|
|
135
|
+
| Descriptor | Description |
|
|
136
|
+
| -------------------------------------- | ----------------------------------------- |
|
|
137
|
+
| `{ "value": ... }` | A constant JSON value |
|
|
138
|
+
| `{ "template": "..." }` | A string built from `${...}` placeholders |
|
|
139
|
+
| `{ "initData": true, "path": "a.b" }` | A value from the workflow input |
|
|
140
|
+
| `{ "step": "step-id", "path": "a.b" }` | A value from a preceding step's output |
|
|
141
|
+
| `{ "requestContextPath": "a.b" }` | A value from the request context |
|
|
142
|
+
|
|
143
|
+
The `step` source also accepts an array of step IDs:
|
|
144
|
+
|
|
145
|
+
```json
|
|
146
|
+
{ "step": ["escalate", "auto-reply"], "path": "text" }
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
The first listed step with a non-empty result supplies the value. This can select the branch that ran after a `conditional` entry.
|
|
150
|
+
|
|
151
|
+
Templates resolve placeholders against `initData`, `inputData`, `state`, `requestContext`, and `stepResults.<step-id>`:
|
|
152
|
+
|
|
153
|
+
```json
|
|
154
|
+
{
|
|
155
|
+
"type": "mapping",
|
|
156
|
+
"id": "build-prompt",
|
|
157
|
+
"mapConfig": "{\"prompt\":{\"template\":\"Summarize this request: ${initData.request}\"}}"
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Objects and arrays resolved by a template are stringified as JSON. A `null` value inside a present result renders as an empty string. A template that references a step without a successful output fails the run.
|
|
162
|
+
|
|
163
|
+
Mapping entries must be top-level graph entries. They can't be placed inside `parallel`, `conditional`, `foreach`, or `loop` containers.
|
|
164
|
+
|
|
165
|
+
### Nested workflow steps
|
|
166
|
+
|
|
167
|
+
A `workflow` entry invokes another registered workflow. The target can be code-defined or stored.
|
|
168
|
+
|
|
169
|
+
```json
|
|
170
|
+
{
|
|
171
|
+
"type": "workflow",
|
|
172
|
+
"id": "lookup-first",
|
|
173
|
+
"workflowId": "lookup-customer-workflow"
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
The `id` identifies the call site. The same nested workflow can appear several times under different call-site IDs, and later steps address each result as `stepResults.<id>`. A `workflow` entry also accepts an optional `description`.
|
|
178
|
+
|
|
179
|
+
### Parallel entries
|
|
180
|
+
|
|
181
|
+
A `parallel` entry runs several single steps concurrently and merges their outputs into an object keyed by step ID.
|
|
182
|
+
|
|
183
|
+
```json
|
|
184
|
+
{
|
|
185
|
+
"type": "parallel",
|
|
186
|
+
"steps": [
|
|
187
|
+
{ "type": "tool", "id": "first", "toolId": "lookup-customer" },
|
|
188
|
+
{ "type": "tool", "id": "second", "toolId": "lookup-customer" }
|
|
189
|
+
]
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Each child must be an `agent`, `tool`, or `workflow` entry. All children receive the parallel entry's input directly.
|
|
194
|
+
|
|
195
|
+
### Conditional entries
|
|
196
|
+
|
|
197
|
+
A `conditional` entry pairs each step with a declarative predicate and runs every branch whose predicate is true.
|
|
198
|
+
|
|
199
|
+
```json
|
|
200
|
+
{
|
|
201
|
+
"type": "conditional",
|
|
202
|
+
"steps": [
|
|
203
|
+
{ "type": "agent", "id": "escalate", "agentId": "support-agent" },
|
|
204
|
+
{ "type": "agent", "id": "auto-reply", "agentId": "support-agent" }
|
|
205
|
+
],
|
|
206
|
+
"predicates": [
|
|
207
|
+
{ "op": "eq", "left": { "path": "inputData.priority" }, "right": { "literal": "urgent" } },
|
|
208
|
+
{ "op": "ne", "left": { "path": "inputData.priority" }, "right": { "literal": "urgent" } }
|
|
209
|
+
]
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
Each child must be an `agent`, `tool`, or `workflow` entry, and each child needs a predicate. All children receive the conditional entry's input directly.
|
|
214
|
+
|
|
215
|
+
### Predicates
|
|
216
|
+
|
|
217
|
+
Conditional entries and loops use a JSON predicate DSL. Operands are `{ "path": "..." }` references or `{ "literal": ... }` values. Paths resolve against `initData`, `inputData`, `stepResults`, and `state`.
|
|
218
|
+
|
|
219
|
+
| Operator | Shape |
|
|
220
|
+
| ------------------------------------ | -------------------------------------------- |
|
|
221
|
+
| `eq`, `ne`, `lt`, `lte`, `gt`, `gte` | `{ "op": "eq", "left": ..., "right": ... }` |
|
|
222
|
+
| `in`, `notIn` | `{ "op": "in", "value": ..., "set": [...] }` |
|
|
223
|
+
| `exists`, `notExists` | `{ "op": "exists", "path": "..." }` |
|
|
224
|
+
| `truthy`, `falsy` | `{ "op": "truthy", "value": ... }` |
|
|
225
|
+
| `and`, `or` | `{ "op": "and", "args": [...] }` |
|
|
226
|
+
| `not` | `{ "op": "not", "arg": ... }` |
|
|
227
|
+
|
|
228
|
+
Missing paths don't throw. Path-based operators return `false` when the path can't be resolved. Use `exists` or `notExists` to distinguish a missing value from a falsy value.
|
|
229
|
+
|
|
230
|
+
### Foreach entries
|
|
231
|
+
|
|
232
|
+
A `foreach` entry runs its body once for each item in an array input. The preceding entry must produce a raw array. Results preserve input order, and concurrency defaults to `1`.
|
|
233
|
+
|
|
234
|
+
```json
|
|
235
|
+
{
|
|
236
|
+
"type": "foreach",
|
|
237
|
+
"step": { "type": "workflow", "id": "write-blurb", "workflowId": "blurb-workflow" },
|
|
238
|
+
"opts": { "concurrency": 3 }
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
The body can be an `agent`, `tool`, or `workflow` entry, but not a `mapping` entry.
|
|
243
|
+
|
|
244
|
+
### Loop entries
|
|
245
|
+
|
|
246
|
+
A `loop` repeats one step while (`dowhile`) or until (`dountil`) a predicate holds.
|
|
247
|
+
|
|
248
|
+
```json
|
|
249
|
+
{
|
|
250
|
+
"type": "loop",
|
|
251
|
+
"loopType": "dountil",
|
|
252
|
+
"step": { "type": "tool", "id": "poll", "toolId": "check-status" },
|
|
253
|
+
"predicate": {
|
|
254
|
+
"op": "eq",
|
|
255
|
+
"left": { "path": "inputData.status" },
|
|
256
|
+
"right": { "literal": "done" }
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
The loop body must be a single step, and stored loops require a declarative predicate.
|
|
262
|
+
|
|
263
|
+
### Sleep entries
|
|
264
|
+
|
|
265
|
+
A `sleep` entry pauses for a fixed number of milliseconds. A `sleepUntil` entry pauses until a fixed date represented by an ISO date string. Stored definitions require literal values.
|
|
266
|
+
|
|
267
|
+
```json
|
|
268
|
+
{ "type": "sleep", "id": "wait", "duration": 5000 }
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
```json
|
|
272
|
+
{ "type": "sleepUntil", "id": "wait-for-launch", "date": "2027-01-01T00:00:00.000Z" }
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
Use a code-defined workflow when the duration or date must be calculated at runtime.
|
|
276
|
+
|
|
277
|
+
## Validation
|
|
278
|
+
|
|
279
|
+
Mastra validates definitions before it persists or registers them:
|
|
280
|
+
|
|
281
|
+
- Structure: Entry shapes and required fields, including placement rules such as top-level-only mappings.
|
|
282
|
+
- References: Each `agentId` and `workflowId` must resolve against the live registries or the same bundle. A `toolId` must match a tool registration key.
|
|
283
|
+
- Schema flow: Each entry's input must be compatible with the preceding output, including inferred mapping outputs.
|
|
284
|
+
|
|
285
|
+
Validation errors include a dotted path, such as `graph.2.steps.0`, that identifies the invalid entry.
|
|
286
|
+
|
|
287
|
+
## Related
|
|
288
|
+
|
|
289
|
+
- [Use stored workflows](https://mastra.ai/docs/workflows/stored-workflows)
|
|
290
|
+
- [`Mastra.addStoredWorkflow()`](https://mastra.ai/reference/core/addStoredWorkflow)
|
|
291
|
+
- [`Mastra.addStoredWorkflows()`](https://mastra.ai/reference/core/addStoredWorkflows)
|
|
292
|
+
- [Client SDK workflows API](https://mastra.ai/reference/client-js/workflows)
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Workflow\.agent()
|
|
4
|
+
|
|
5
|
+
The `.agent()` method adds an agent as a declarative step. The step accepts `{ prompt: string }` as input and returns `{ text: string }` by default. Use `.map()` before the agent to build the prompt from workflow data.
|
|
6
|
+
|
|
7
|
+
Unlike wrapping an agent with `createStep()`, `.agent()` records a declarative entry in the workflow graph. This makes the workflow portable: the same graph can be serialized and persisted as a [stored workflow](https://mastra.ai/docs/workflows/stored-workflows).
|
|
8
|
+
|
|
9
|
+
## Usage example
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
workflow
|
|
13
|
+
.map({ prompt: mapVariable({ initData: workflow, path: "topic" }) })
|
|
14
|
+
.agent(testAgent)
|
|
15
|
+
.commit();
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Parameters
|
|
19
|
+
|
|
20
|
+
**agentOrId** (`Agent | string`): An agent instance, or the ID of an agent registered on the Mastra instance. When passing an ID, the agent is resolved from the registry at execution time.
|
|
21
|
+
|
|
22
|
+
**options** (`AgentStepOptions & { structuredOutput?: { schema }, retries?: number, scorers?: DynamicArgument<MastraScorers>, metadata?: StepMetadata }`): Agent call options such as maxSteps, modelSettings, memory, and providerOptions, plus step-level retries, scorers, and metadata. Per-request fields such as requestContext, resourceId, threadId, and onStepFinish are managed by the workflow engine and excluded.
|
|
23
|
+
|
|
24
|
+
**stepOptions** (`{ id?: string }`): The step's call-site ID within the workflow. Defaults to the agent's ID. Set this when the same agent appears more than once in one workflow.
|
|
25
|
+
|
|
26
|
+
## Returns
|
|
27
|
+
|
|
28
|
+
**workflow** (`Workflow`): The workflow instance for method chaining
|
|
29
|
+
|
|
30
|
+
## Structured output
|
|
31
|
+
|
|
32
|
+
By default the step's output is `{ text: string }`. Pass `structuredOutput.schema` to make the step return that shape instead. The schema becomes the step's output schema, so later steps chain against it with full type safety:
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
workflow
|
|
36
|
+
.agent(testAgent, {
|
|
37
|
+
structuredOutput: {
|
|
38
|
+
schema: z.object({
|
|
39
|
+
subtopics: z.array(z.string()),
|
|
40
|
+
}),
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
.commit();
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Referencing an agent by ID
|
|
47
|
+
|
|
48
|
+
Pass a string to reference a registered agent without importing it. The agent must be registered on the Mastra instance when the workflow runs:
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
workflow.agent("test-agent", { maxSteps: 3 }).commit();
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Persisting agent steps
|
|
55
|
+
|
|
56
|
+
Workflows built with `.agent()` serialize to the same declarative entries that [stored workflows](https://mastra.ai/docs/workflows/stored-workflows) use. Only `retries` and `metadata` round-trip through storage. Options that hold functions, such as `onFinish` or a function-valued `toolChoice`, throw an error when the workflow is stored.
|
|
57
|
+
|
|
58
|
+
## Related
|
|
59
|
+
|
|
60
|
+
- [Agents and Tools](https://mastra.ai/docs/workflows/agents-and-tools)
|
|
61
|
+
- [Stored workflows](https://mastra.ai/docs/workflows/stored-workflows)
|
|
62
|
+
- [Workflow.tool()](https://mastra.ai/reference/workflows/workflow-methods/tool)
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# Workflow\.tool()
|
|
4
|
+
|
|
5
|
+
The `.tool()` method adds a tool as a declarative step. The tool's own input and output schemas apply, so the previous step's output must satisfy the tool's input schema. Use `.map()` to transform the data if they don't match.
|
|
6
|
+
|
|
7
|
+
Unlike wrapping a tool with `createStep()`, `.tool()` records a declarative entry in the workflow graph. This makes the workflow portable: the same graph can be serialized and persisted as a [stored workflow](https://mastra.ai/docs/workflows/stored-workflows).
|
|
8
|
+
|
|
9
|
+
## Usage example
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
workflow.tool(testTool).commit();
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Parameters
|
|
16
|
+
|
|
17
|
+
**toolOrId** (`Tool | string`): A tool instance, or the ID of a tool registered on the Mastra instance. When passing an ID, the tool is resolved from the registry at execution time.
|
|
18
|
+
|
|
19
|
+
**options** (`{ retries?: number, scorers?: DynamicArgument<MastraScorers>, metadata?: StepMetadata }`): Step-level retry count, scorers, and metadata for the tool step.
|
|
20
|
+
|
|
21
|
+
**stepOptions** (`{ id?: string }`): The step's call-site ID within the workflow. Defaults to the tool's ID. Set this when the same tool appears more than once in one workflow.
|
|
22
|
+
|
|
23
|
+
## Returns
|
|
24
|
+
|
|
25
|
+
**workflow** (`Workflow`): The workflow instance for method chaining
|
|
26
|
+
|
|
27
|
+
## Referencing a tool by ID
|
|
28
|
+
|
|
29
|
+
Pass a string to reference a registered tool without importing it. The tool must be registered on the Mastra instance when the workflow runs:
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
workflow.tool("lookup-customer", { retries: 2 }).commit();
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Persisting tool steps
|
|
36
|
+
|
|
37
|
+
Workflows built with `.tool()` serialize to the same declarative entries that [stored workflows](https://mastra.ai/docs/workflows/stored-workflows) use. Only `retries` and `metadata` round-trip through storage. A function-valued `scorers` option throws an error when the workflow is stored.
|
|
38
|
+
|
|
39
|
+
## Related
|
|
40
|
+
|
|
41
|
+
- [Agents and Tools](https://mastra.ai/docs/workflows/agents-and-tools)
|
|
42
|
+
- [Stored workflows](https://mastra.ai/docs/workflows/stored-workflows)
|
|
43
|
+
- [Workflow.agent()](https://mastra.ai/reference/workflows/workflow-methods/agent)
|