Tool as a Workflow step (Legacy)
This example demonstrates how to create and integrate a custom tool as a workflow step, showing how to define input/output schemas and implement the tool’s execution logic.
import { createTool } from "@mastra/core/tools";
import { LegacyWorkflow } from "@mastra/core/workflows/legacy";
import { z } from "zod";
const crawlWebpage = createTool({
id: "Crawl Webpage",
description: "Crawls a webpage and extracts the text content",
inputSchema: z.object({
url: z.string().url(),
}),
outputSchema: z.object({
rawText: z.string(),
}),
execute: async ({ context }) => {
const response = await fetch(context.triggerData.url);
const text = await response.text();
return { rawText: "This is the text content of the webpage: " + text };
},
});
const contentWorkflow = new LegacyWorkflow({ name: "content-review" });
contentWorkflow.step(crawlWebpage).commit();
const { start } = contentWorkflow.createRun();
const res = await start({ triggerData: { url: "https://example.com" } });
console.log(res.results);
View Example on GitHub
Workflows (Legacy)
The following links provide example documentation for legacy workflows:
- Creating a Simple Workflow (Legacy)
- Workflow (Legacy) with Sequential Steps
- Parallel Execution with Steps
- Branching Paths
- Workflow (Legacy) with Conditional Branching (experimental)
- Calling an Agent From a Workflow (Legacy)
- Workflow (Legacy) with Cyclical dependencies
- Data Mapping with Workflow Variables (Legacy)
- Human in the Loop Workflow (Legacy)
- Workflow (Legacy) with Suspend and Resume