ExamplesWorkflowsUsing a Tool as a Step

Tool as a Workflow step

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 { Workflow } from '@mastra/core/workflows';
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 Workflow({ 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