Skip to main content

createTool()

The createTool() function is used to define custom tools that your Mastra agents can execute. Tools extend an agent's capabilities by allowing it to interact with external systems, perform calculations, or access specific data.

Usage example
Direct link to Usage example

src/mastra/tools/reverse-tool.ts
import { createTool } from "@mastra/core/tools";
import { z } from "zod";

export const tool = createTool({
id: "test-tool",
description: "Reverse the input string",
inputSchema: z.object({
input: z.string(),
}),
outputSchema: z.object({
output: z.string(),
}),
execute: async (inputData) => {
const reversed = inputData.input.split("").reverse().join("");

return {
output: reversed,
};
},
});

Parameters
Direct link to Parameters

id:

string
A unique identifier for the tool.

description:

string
A description of what the tool does. This is used by the agent to decide when to use the tool.

inputSchema?:

Zod schema
A Zod schema defining the expected input parameters for the tool's `execute` function.

outputSchema?:

Zod schema
A Zod schema defining the expected output structure of the tool's `execute` function.

execute:

function
The function that contains the tool's logic. It receives two parameters: the validated input data (first parameter) and an optional execution context object (second parameter) containing `requestContext`, `tracingContext`, `abortSignal`, and other execution metadata.

input:

z.infer<TInput>
The validated input data based on inputSchema

context?:

ToolExecutionContext
Optional execution context containing metadata

onInputStart?:

function
Optional callback invoked when the tool call input streaming begins. Receives `toolCallId`, `messages`, and `abortSignal`.

onInputDelta?:

function
Optional callback invoked for each incremental chunk of input text as it streams in. Receives `inputTextDelta`, `toolCallId`, `messages`, and `abortSignal`.

onInputAvailable?:

function
Optional callback invoked when the complete tool input is available and parsed. Receives the validated `input` object, `toolCallId`, `messages`, and `abortSignal`.

onOutput?:

function
Optional callback invoked after the tool has successfully executed and returned output. Receives the tool's `output`, `toolCallId`, `messages`, and `abortSignal`.

Returns
Direct link to Returns

The createTool() function returns a Tool object.

Tool:

object
An object representing the defined tool, ready to be added to an agent.

Tool Lifecycle Hooks
Direct link to Tool Lifecycle Hooks

Tools support lifecycle hooks that allow you to monitor and react to different stages of tool execution. These hooks are particularly useful for logging, analytics, validation, and real-time updates during streaming.

Available Hooks
Direct link to Available Hooks

onInputStart
Direct link to onInputStart

Called when tool call input streaming begins, before any input data is received.

export const tool = createTool({
id: "example-tool",
description: "Example tool with hooks",
onInputStart: ({ toolCallId, messages, abortSignal }) => {
console.log(`Tool ${toolCallId} input streaming started`);
},
// ... other properties
});

onInputDelta
Direct link to onInputDelta

Called for each incremental chunk of input text as it streams in. Useful for showing real-time progress or parsing partial JSON.

export const tool = createTool({
id: "example-tool",
description: "Example tool with hooks",
onInputDelta: ({ inputTextDelta, toolCallId, messages, abortSignal }) => {
console.log(`Received input chunk: ${inputTextDelta}`);
},
// ... other properties
});

onInputAvailable
Direct link to onInputAvailable

Called when the complete tool input is available and has been parsed and validated against the inputSchema.

export const tool = createTool({
id: "example-tool",
description: "Example tool with hooks",
inputSchema: z.object({
city: z.string(),
}),
onInputAvailable: ({ input, toolCallId, messages, abortSignal }) => {
console.log(`Tool received complete input:`, input);
// input is fully typed based on inputSchema
},
// ... other properties
});

onOutput
Direct link to onOutput

Called after the tool has successfully executed and returned output. Useful for logging results, triggering follow-up actions, or analytics.

export const tool = createTool({
id: "example-tool",
description: "Example tool with hooks",
outputSchema: z.object({
result: z.string(),
}),
execute: async (input) => {
return { result: "Success" };
},
onOutput: ({ output, toolCallId, toolName, abortSignal }) => {
console.log(`${toolName} execution completed:`, output);
// output is fully typed based on outputSchema
},
});

Hook Execution Order
Direct link to Hook Execution Order

For a typical streaming tool call, the hooks are invoked in this order:

  1. onInputStart - Input streaming begins
  2. onInputDelta - Called multiple times as chunks arrive
  3. onInputAvailable - Complete input is parsed and validated
  4. Tool's execute function runs
  5. onOutput - Tool has completed successfully

Hook Parameters
Direct link to Hook Parameters

All hooks receive a parameter object with these common properties:

  • toolCallId (string): Unique identifier for this specific tool call
  • abortSignal (AbortSignal): Signal for detecting if the operation should be cancelled

Additionally:

  • onInputStart, onInputDelta, and onInputAvailable receive messages (array): The conversation messages at the time of the tool call
  • onInputDelta receives inputTextDelta (string): The incremental text chunk
  • onInputAvailable receives input: The validated input data (typed according to inputSchema)
  • onOutput receives output: The tool's return value (typed according to outputSchema) and toolName (string): The name of the tool

Error Handling
Direct link to Error Handling

Hook errors are caught and logged automatically, but do not prevent tool execution from continuing. If a hook throws an error, it will be logged to the console but will not fail the tool call.