mastra-ai/mastra · error

delete-workflow requires a Mastra context.

Error message

delete-workflow requires a Mastra context.

What it means

delete-workflow's execute requires the Mastra instance from the tool execution context to perform the deletion. If mastra is undefined, this error is thrown before any lookup. It signals the tool ran outside an initialized Mastra runtime.

Source

Thrown at mastracode/sdk/src/tools/workflows/delete-workflow.ts:23

 */
import type { Mastra } from '@mastra/core/mastra';
import { createTool } from '@mastra/core/tools';
import { z } from 'zod';
import { deleteWorkflow } from '../../workflows/service.js';

export const deleteWorkflowTool = createTool({
  id: 'delete-workflow',
  description:
    'Remove a saved workflow from storage and unregister the live in-process Workflow instance. Idempotent. Subsequent save-workflow calls with the same id re-register cleanly.',
  inputSchema: z.object({
    id: z.string().describe('The workflow id to delete.'),
  }),
  outputSchema: z.object({
    ok: z.literal(true),
    id: z.string(),
  }),
  execute: async ({ id }, { mastra }) => {
    if (!mastra) throw new Error('delete-workflow requires a Mastra context.');
    return deleteWorkflow(mastra as Mastra, id);
  },
});

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Execute the tool via the Mastra server/runtime so { mastra } is provided.
  2. In tests, pass { mastra: mastraInstance } (real or mocked) as the second execute argument.
  3. Register delete-workflow on the Mastra instance instead of invoking it standalone.

Example fix

// before
await deleteWorkflowTool.execute({ id }, {} as any);
// after
await deleteWorkflowTool.execute({ id }, { mastra });
Defensive patterns

Strategy: try-catch

Validate before calling

if (!mastra) throw new Error('delete-workflow invoked without a Mastra instance');

Type guard

function hasMastraContext(ctx: unknown): ctx is { mastra: Mastra } { return !!ctx && typeof ctx === 'object' && 'mastra' in ctx && ctx.mastra != null; }

Try / catch

try { await tool.execute({ id }, { mastra }); } catch (e) { if (e.message.includes('requires a Mastra context')) { /* re-run inside the Mastra runtime */ } else throw e; }

Prevention

When it happens

Trigger: Invoking delete-workflow with an execution context lacking mastra — direct execute() calls in tests or scripts, or running outside the Mastra server.

Common situations: Hand-rolled test harnesses with stub contexts; calling tool execute() directly; tool used in a non-Mastra runner.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/849843b0ca5d6aae. Report an issue: GitHub.