WorkflowsMay 7, 2026·3 min read

Mastra Workflows — TypeScript Agent Orchestration

Mastra Workflows defines multi-step typed agent flows in TypeScript. Build pipelines with .then(), .branch(), .parallel(), .while(). End-to-end type-safe.

Agent ready

This asset can be read and installed directly by agents

TokRepo exposes a universal CLI command, install contract, metadata JSON, adapter-aware plan, and raw content links so agents can judge fit, risk, and next actions.

Needs Confirmation · 52/100Policy: confirm
Agent surface
Any MCP/CLI agent
Kind
Skill
Install
Single
Trust
Trust: New
Entrypoint
Asset
Universal CLI install command
npx tokrepo install 25d28578-b0d5-497a-a626-972c0392849d
Intro

Mastra Workflows is Mastra's typed orchestration primitive — chain .step().then().branch().parallel().while() to build multi-step agent pipelines where TypeScript inferring carries type-safety from the first input to the last output. Best for: production TypeScript agents that need readable, testable, observable workflows. Works with: Mastra 0.5+, Node 20+. Setup time: 5 minutes.


Define a workflow

import { Workflow, Step } from "@mastra/core";
import { z } from "zod";

const fetchData = new Step({
  id: "fetch",
  inputSchema: z.object({ url: z.string().url() }),
  outputSchema: z.object({ markdown: z.string() }),
  execute: async ({ context }) => {
    const res = await fetch(context.inputData.url);
    return { markdown: await res.text() };
  },
});

const summarize = new Step({
  id: "summarize",
  inputSchema: z.object({ markdown: z.string() }),
  outputSchema: z.object({ summary: z.string() }),
  execute: async ({ context, mastra }) => {
    const agent = mastra.getAgent("summarizer");
    const result = await agent.generate(`Summarize: ${context.inputData.markdown}`);
    return { summary: result.text };
  },
});

const workflow = new Workflow({
  name: "fetch-and-summarize",
  triggerSchema: z.object({ url: z.string().url() }),
}).step(fetchData).then(summarize).commit();

Branching

.then(classify)
.branch([
  [async ({ context }) => context.results.classify.kind === "bug",
   fixerStep],
  [async ({ context }) => context.results.classify.kind === "feature",
   plannerStep],
])

Run and stream

const { runId, start } = workflow.createRun();

start({ triggerData: { url: "https://example.com/blog" } });

// Subscribe to step events
workflow.watch(runId, (event) => {
  console.log(event.type, event.payload);
});

Each step's input/output is checkpointed — restart from the last successful step if a downstream step fails.


FAQ

Q: Mastra Workflows vs LangGraph? A: LangGraph is Python-first with a graph DSL. Mastra Workflows is TypeScript-first with a fluent chain API. Pick by language preference + ecosystem; both handle the same shape of problems.

Q: Is Mastra free? A: Yes — Mastra is open-source under Elastic License 2.0. Self-hostable. Mastra Cloud (managed deployment + observability) is paid.

Q: Can workflows call other workflows? A: Yes — step(otherWorkflow) embeds a workflow as a sub-step. Useful for shared sub-flows (e.g. validateAndCleanData reused across pipelines).


Quick Use

  1. npm create mastra@latest (scaffolds project + installs deps)
  2. Define Steps with inputSchema, outputSchema, execute (Zod-typed)
  3. Chain with new Workflow().step().then().commit(), then createRun().start()

Intro

Mastra Workflows is Mastra's typed orchestration primitive — chain .step().then().branch().parallel().while() to build multi-step agent pipelines where TypeScript inferring carries type-safety from the first input to the last output. Best for: production TypeScript agents that need readable, testable, observable workflows. Works with: Mastra 0.5+, Node 20+. Setup time: 5 minutes.


Define a workflow

import { Workflow, Step } from "@mastra/core";
import { z } from "zod";

const fetchData = new Step({
  id: "fetch",
  inputSchema: z.object({ url: z.string().url() }),
  outputSchema: z.object({ markdown: z.string() }),
  execute: async ({ context }) => {
    const res = await fetch(context.inputData.url);
    return { markdown: await res.text() };
  },
});

const summarize = new Step({
  id: "summarize",
  inputSchema: z.object({ markdown: z.string() }),
  outputSchema: z.object({ summary: z.string() }),
  execute: async ({ context, mastra }) => {
    const agent = mastra.getAgent("summarizer");
    const result = await agent.generate(`Summarize: ${context.inputData.markdown}`);
    return { summary: result.text };
  },
});

const workflow = new Workflow({
  name: "fetch-and-summarize",
  triggerSchema: z.object({ url: z.string().url() }),
}).step(fetchData).then(summarize).commit();

Branching

.then(classify)
.branch([
  [async ({ context }) => context.results.classify.kind === "bug",
   fixerStep],
  [async ({ context }) => context.results.classify.kind === "feature",
   plannerStep],
])

Run and stream

const { runId, start } = workflow.createRun();

start({ triggerData: { url: "https://example.com/blog" } });

// Subscribe to step events
workflow.watch(runId, (event) => {
  console.log(event.type, event.payload);
});

Each step's input/output is checkpointed — restart from the last successful step if a downstream step fails.


FAQ

Q: Mastra Workflows vs LangGraph? A: LangGraph is Python-first with a graph DSL. Mastra Workflows is TypeScript-first with a fluent chain API. Pick by language preference + ecosystem; both handle the same shape of problems.

Q: Is Mastra free? A: Yes — Mastra is open-source under Elastic License 2.0. Self-hostable. Mastra Cloud (managed deployment + observability) is paid.

Q: Can workflows call other workflows? A: Yes — step(otherWorkflow) embeds a workflow as a sub-step. Useful for shared sub-flows (e.g. validateAndCleanData reused across pipelines).


Source & Thanks

Built by Mastra. Licensed under Elastic License 2.0.

mastra-ai/mastra — ⭐ 13,000+

🙏

Source & Thanks

Built by Mastra. Licensed under Elastic License 2.0.

mastra-ai/mastra — ⭐ 13,000+

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets