Step Class
The Step class defines individual units of work within a workflow, encapsulating execution logic, data validation, and input/output handling. It can take either a tool or an agent as a parameter to automatically create a step from them.
Usage example
src/mastra/workflows/test-workflow.ts
import { createWorkflow, createStep } from "@mastra/core/workflows";
import { z } from "zod";
const step1 = createStep({
id: "step-1",
description: "passes value from input to output",
inputSchema: z.object({
value: z.number()
}),
outputSchema: z.object({
value: z.number()
}),
execute: async ({ inputData }) => {
const { value } = inputData;
return {
value
};
}
});
Constructor Parameters
id:
string
Unique identifier for the step
description:
string
Optional description of what the step does
inputSchema:
z.ZodType<any>
Zod schema defining the input structure
outputSchema:
z.ZodType<any>
Zod schema defining the output structure
resumeSchema:
z.ZodType<any>
Optional Zod schema for resuming the step
suspendSchema:
z.ZodType<any>
Optional Zod schema for suspending the step
execute:
(params: ExecuteParams) => Promise<any>
Async function containing step logic
ExecuteParams
inputData:
z.infer<TStepInput>
The input data matching the inputSchema
resumeData:
z.infer<TResumeSchema>
The resume data matching the resumeSchema, when resuming the step from a suspended state. Only exists if the step is being resumed.
mastra:
Mastra
Access to Mastra services (agents, tools, etc.)
getStepResult:
(stepId: string) => any
Function to access results from other steps
getInitData:
() => any
Function to access the initial input data of the workflow in any step
suspend:
() => Promise<void>
Function to pause workflow execution
runId:
string
Current run id
runtimeContext?:
RuntimeContext
Runtime context for dependency injection and contextual information.
runCount?:
number
The run count for this specific step, it automatically increases each time the step runs