Skip to content

docs: add Mastra integration cookbook page - #3150

Draft
pranaygp wants to merge 10 commits into
mainfrom
pgp-mastra-integration-docs
Draft

docs: add Mastra integration cookbook page#3150
pranaygp wants to merge 10 commits into
mainfrom
pgp-mastra-integration-docs

Conversation

@pranaygp

@pranaygp pranaygp commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Adds docs/content/docs/v5/cookbook/integrations/mastra.mdx, a cookbook integration guide for running Mastra workflows on Workflow SDK via the new (experimental) @mastra/workflow package. Registered in the integrations meta.json and linked from the cookbook index.

What the page covers

  • What the integration gives you — Mastra's createWorkflow() / createStep() authoring API and run API stay the same; the execution engine underneath becomes Workflow SDK. No worker process to operate, zero-infra local dev, managed in production.
  • How it works@mastra/workflow ships a generic "use workflow" runner that deterministically walks the serialized step graph in the workflow sandbox. Each step execute, branch condition, and loop predicate runs in a "use step" function. Includes a mapping table from Mastra concepts to Workflow SDK primitives (suspend() → hooks, .sleep() → durable sleep(), run watching → namespaced run streams, cancel → run.cancel()).
  • Next.js setup walkthrough — install, withWorkflow, the consumer re-export file, authoring a workflow with init({ runner: mastraRunner }), registering on new Mastra(...), starting a run from a route handler, inspecting with npx workflow web.
  • Why the re-export file is required — it makes the package's "use workflow" / "use step" files reachable from the consumer's source tree so the compiler transforms them, and its side-effect import loads the Mastra workflow definitions into the flow-handler process. Cross-linked to the Publishing Libraries cookbook page whose pattern this follows.
  • Upgrade behavior — step IDs for package-shipped code embed the package version, so upgrading @mastra/workflow renames every step it ships. The page frames this as safe rather than hazardous, because runs are pinned to the deployment that recorded their IDs: resume re-enqueues against workflowRun.deploymentId (packages/core/src/runtime/resume-hook.ts:215-227, runs.ts:168) and only an explicit deploymentId: 'latest' moves a run to newer code (start.ts:279-306). New runs get the new version; in-flight runs finish on the old one. No drain step. Pinning is per-world, so the callout says "on worlds with deployment pinning, such as Vercel" rather than stating it flatly: world-vercel preserves deploymentId across re-enqueue (packages/world-vercel/src/queue.ts:406,417-418,490), world-local pins only per SDK version, and world-postgres returns a constant (packages/world-postgres/src/queue.ts:133-135). Note the version-embedded ID holds re-export or not — an export-reachable package file keeps its name@version ID either way (packages/builders/src/module-specifier.ts:370-402), which is publishing-libraries.mdx overstates the consumer re-export: package step IDs still embed name@version #3152. The page deliberately does not repeat the Publishing Libraries page's claim that re-exporting relocates the IDs.
  • Human-in-the-loop — a step calling suspend({ reason }), resumed from a route with resumeHook() on the mastra:${runId}:${stepId} token. run.resume() works from any process as well, including one that never saw the run start, provided the Mastra instance has storage configured — the package mirrors the Workflow SDK run id onto the Mastra snapshot, and storage is where the two ids are mapped. run.watch() and run.cancel() work cross-process on the same condition. The page frames the choice around what each call returns (resumeHook() returns when the hook is released; run.resume() waits for the next stopping point and hands back the WorkflowResult) rather than around a process limitation. Two specifics verified against the package source by its author: the .foreach() token suffix is the item's index, zero-based and counted across the whole input array rather than restarting per concurrency batch (suspendToken in src/workflows/walker.ts:106-110, offsets at 450-455); and the store must be a hosted database, since a file: URL gives each serverless invocation its own empty copy and reproduces the very Cannot resume run ... error storage prevents.
  • Watching a runrun.watch() / run.stream(), plus the underlying mastra:events namespaced run stream and the Mastra event types it carries.
  • Limitations callout — experimental status, plus nested workflows, time travel, streamLegacy() / perStep streaming unsupported and .map() untested.

Related PRs

Part of a series adding Workflow SDK support to Mastra:

This PR should merge once the package is published — until then the install instructions point at something that isn't on npm yet, so it stays in draft.

Docs Preview

Page Preview
Mastra integration (v5) https://workflow-docs-git-pgp-mastra-integration-docs.vercel.sh/v5/docs/cookbook/integrations/mastra
Cookbook index (v5) https://workflow-docs-git-pgp-mastra-integration-docs.vercel.sh/v5/cookbook

(Behind deployment protection — requires Vercel team access.)

Notes for reviewers

  • v5 only. Because the page has no v4 counterpart, the cookbook index links it as /v5/cookbook/integrations/mastra; the unprefixed /cookbook/... form used by its siblings resolves against v4 and fails the link lint. Happy to add a v4 copy instead if you'd rather it ship on the default docs version.
  • Every API in the samples has been checked against a working @mastra/workflow build with passing end-to-end tests, and the Workflow SDK side (resumeHook, getRun().getReadable()) against this repo's own API reference pages. The package is not published yet, so the install instructions are the one thing that can't be exercised.
  • No changeset: docs-only change under docs/content/, matching the majority of prior docs-only PRs.

🤖 Generated with Claude Code

Documents running Mastra workflows on Workflow SDK via the experimental
@mastra/workflow package: how the generic runner walks the serialized step
graph, the Mastra-to-Workflow-SDK primitive mapping, Next.js setup, why the
consumer re-export file is required, human-in-the-loop with suspend/resume,
and current limitations.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Pranay Prakash <pranay.gp@gmail.com>
@changeset-bot

changeset-bot Bot commented Jul 28, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 4318a26

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel

vercel Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
example-nextjs-workflow-turbopack Ready Ready Preview, Comment Jul 28, 2026 1:08pm
example-nextjs-workflow-webpack Ready Ready Preview, Comment Jul 28, 2026 1:08pm
example-workflow Ready Ready Preview, Comment Jul 28, 2026 1:08pm
workbench-astro-workflow Ready Ready Preview, Comment Jul 28, 2026 1:08pm
workbench-express-workflow Ready Ready Preview, Comment Jul 28, 2026 1:08pm
workbench-fastify-workflow Ready Ready Preview, Comment Jul 28, 2026 1:08pm
workbench-hono-workflow Ready Ready Preview, Comment Jul 28, 2026 1:08pm
workbench-nestjs-workflow Ready Ready Preview, Comment Jul 28, 2026 1:08pm
workbench-nitro-workflow Ready Ready Preview, Comment Jul 28, 2026 1:08pm
workbench-nuxt-workflow Ready Ready Preview, Comment Jul 28, 2026 1:08pm
workbench-sveltekit-workflow Ready Ready Preview, Comment Jul 28, 2026 1:08pm
workbench-tanstack-start-workflow Ready Ready Preview, Comment Jul 28, 2026 1:08pm
workbench-vite-workflow Ready Ready Preview, Comment Jul 28, 2026 1:08pm
workflow-docs Building Building Preview, Comment, Open in v0 Jul 28, 2026 1:08pm
workflow-swc-playground Ready Ready Preview, Comment Jul 28, 2026 1:08pm
workflow-tarballs Ready Ready Preview, Comment Jul 28, 2026 1:08pm
workflow-web Ready Ready Preview, Comment Jul 28, 2026 1:08pm

The page only exists in the v5 content tree, so an unprefixed
/cookbook/... link resolves against v4 and fails the link lint.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Pranay Prakash <pranay.gp@gmail.com>
Verified against the @mastra/workflow implementation:

- Drop the claim that workflow and step IDs must be static string
  literals. Mastra step ids are ordinary strings and can be anything;
  the real requirement is the re-export file, already documented.
- Describe what run.start() actually resolves to — a full Mastra
  WorkflowResult with status, input, steps, and the success, failure,
  and suspended payloads — instead of implying a two-field object.
- Resume from a route with resumeHook() on the mastra:${runId}:${stepId}
  token. run.resume() works, but awaiting its result only does so in the
  process that started the run, which is rarely the resuming route.
- Document run watching, including the mastra:events run stream and the
  Mastra event types it carries.
- Add the peer version ranges and the rest of what init() returns, and
  correct the unsupported list.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Pranay Prakash <pranay.gp@gmail.com>
The re-export file does not pin step IDs to the consumer's source tree —
export-reachable package files keep a name@version ID either way
(#3152). State its real purpose (compiler discovery plus
loading definitions into the flow handler) and note that upgrading the
package changes its shipped step IDs, so in-flight runs must drain first.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Pranay Prakash <pranay.gp@gmail.com>
pranaygp and others added 3 commits July 27, 2026 23:03
Runs are pinned to the deployment that recorded their step IDs, so a
package version bump is intended invalidation rather than a hazard.
Removes the incorrect drain-before-upgrade advice.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Pranay Prakash <pranay.gp@gmail.com>
The Mastra page said awaiting run.resume() only worked in the process
that started the run. The package now mirrors the Workflow SDK run id
onto the Mastra snapshot, so run.resume(), run.watch(), and run.cancel()
work from any process when the Mastra instance has storage configured.

Reframe the choice between resumeHook() and run.resume() around what
each returns rather than around a process limitation, and note the item
index appended to hook tokens inside .foreach().

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Pranay Prakash <pranay.gp@gmail.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Pranay Prakash <pranay.gp@gmail.com>
pranaygp and others added 2 commits July 27, 2026 23:40
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Pranay Prakash <pranay.gp@gmail.com>
The resume, watch, and cancel failures share one message template, so
anchoring the quote on the invariant tail lets readers searching a
cancel or watch failure find the page.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: Pranay Prakash <pranay.gp@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant