createTool()

Tools are typed functions that can be executed by agents or workflows, with built-in integration access and parameter validation. Each tool has a schema that defines its inputs, an executor function that implements its logic, and access to configured integrations.

src/mastra/tools/index.ts
import { createTool } from "@mastra/core";
import { z } from "zod";
 
const getStockPrice = async (symbol: string) => {
  const data = await fetch(
    `https://mastra-stock-data.vercel.app/api/stock-data?symbol=${symbol}`,
  ).then((r) => r.json());
  return data.prices["4. close"];
};
 
export const stockPrices = createTool({
  id: "Get Stock Price",
  inputSchema: z.object({
    symbol: z.string(),
  }),
  description: `Fetches the last day's closing stock price for a given symbol`,
  execute: async ({ context }) => {
    console.log("Using tool to fetch stock price for", context.symbol);
    return {
      symbol: context.symbol,
      currentPrice: await getStockPrice(context.symbol),
    };
  },
});

API Signature

Parameters

label:

string
Name of the tool (e.g., "Get Stock Prices")

schema:

ZodSchema
Zod schema for validating inputs

description:

string
Clear explanation of what market data the tool provides

executor:

(params: ExecutorParams) => Promise<any>
Async function that fetches the requested market data
ExecutorParams

data:

object
The validated input data (in this case, symbol)

integrationsRegistry:

function
Function to get connected integrations

runId?:

string
The runId of the current run

agents:

Map<string, Agent<any>>
Map of registered agents

engine?:

MastraEngine
Mastra engine instance

llm:

LLM
LLM instance

outputSchema?:

ZodSchema
Zod schema for validating outputs

Returns

ToolApi:

object
The tool API object that includes the schema, label, description, and executor function.
ToolApi

schema:

ZodSchema<IN>
Zod schema for validating inputs.

label:

string
Name of the tool.

description:

string
Description of the tool's functionality.

outputSchema?:

ZodSchema<OUT>
Zod schema for validating outputs.

executor:

(params: IntegrationApiExcutorParams<IN>) => Promise<OUT>
Async function that executes the tool's logic.

MIT 2025 © Nextra.