# 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. ## Install Copy the content below into your project: ## 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 ```typescript 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 ```typescript .then(classify) .branch([ [async ({ context }) => context.results.classify.kind === "bug", fixerStep], [async ({ context }) => context.results.classify.kind === "feature", plannerStep], ]) ``` ### Run and stream ```typescript 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](https://github.com/mastra-ai). Licensed under Elastic License 2.0. > > [mastra-ai/mastra](https://github.com/mastra-ai/mastra) — ⭐ 13,000+ --- ## 快速使用 1. `npm create mastra@latest`(搭项目 + 装依赖) 2. 定义 Step 带 `inputSchema` / `outputSchema` / `execute`(Zod 类型化) 3. `new Workflow().step().then().commit()` 串起来,然后 `createRun().start()` --- ## 简介 Mastra Workflows 是 Mastra 的类型化编排原语 —— 用 `.step().then().branch().parallel().while()` 串多步 agent 流水线,TypeScript 从第一个输入推断到最后一个输出,整条链类型安全。适合需要可读、可测、可观测工作流的生产 TypeScript agent。需要 Mastra 0.5+ 和 Node 20+。装机时间 5 分钟。 --- ### 定义 workflow ```typescript 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(); ``` ### 分支 ```typescript .then(classify) .branch([ [async ({ context }) => context.results.classify.kind === "bug", fixerStep], [async ({ context }) => context.results.classify.kind === "feature", plannerStep], ]) ``` ### 跑 + 流式 ```typescript const { runId, start } = workflow.createRun(); start({ triggerData: { url: "https://example.com/blog" } }); // 订阅 step 事件 workflow.watch(runId, (event) => { console.log(event.type, event.payload); }); ``` 每个 step 的输入输出会 checkpoint —— 下游 step 失败时从最后一个成功 step 重启。 --- ### FAQ **Q: Mastra Workflows 跟 LangGraph 比?** A: LangGraph 是 Python 优先 + 图 DSL。Mastra Workflows 是 TypeScript 优先 + 流式链 API。按语言偏好和生态选;解决同一类问题。 **Q: Mastra 免费吗?** A: 免费 —— Elastic License 2.0 开源。可自托管。Mastra Cloud(托管部署 + 可观测)付费。 **Q: workflow 能调 workflow 吗?** A: 能 —— `step(otherWorkflow)` 把一个 workflow 当成子 step。共享子流程(比如 `validateAndCleanData` 在多个流水线复用)有用。 --- ## 来源与感谢 > Built by [Mastra](https://github.com/mastra-ai). Licensed under Elastic License 2.0. > > [mastra-ai/mastra](https://github.com/mastra-ai/mastra) — ⭐ 13,000+ --- Source: https://tokrepo.com/en/workflows/mastra-workflows-typescript-agent-orchestration Author: Mastra