Cette page est affichée en anglais. Une traduction française est en cours.
WorkflowsMay 7, 2026·3 min de lecture

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.

Mastra
Mastra · Community
Prêt pour agents

Cet actif peut être lu et installé directement par les agents

TokRepo expose une commande CLI universelle, un contrat d'installation, le metadata JSON, un plan selon l'adaptateur et le contenu raw pour aider les agents à juger l'adaptation, le risque et les prochaines actions.

Needs Confirmation · 52/100Policy : confirmer
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : New
Point d'entrée
Asset
Commande CLI universelle
npx tokrepo install 25d28578-b0d5-497a-a626-972c0392849d
Introduction

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 et remerciements

Built by Mastra. Licensed under Elastic License 2.0.

mastra-ai/mastra — ⭐ 13,000+

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires