Overview
Tools are type-safe functions that agents and workflows can call to perform specific actions. They provide automatic input/output validation, suspend/resume capabilities, and integration with the Mastra ecosystem.
createTool < TId , TSchemaIn , TSchemaOut , TSuspend , TResume , TRequestContext , TContext >(
opts : ToolAction
): Tool < TSchemaIn , TSchemaOut , TSuspend , TResume , TContext , TId , TRequestContext >
Creates a type-safe tool with automatic input validation.
Tool configuration Unique identifier for the tool
Description of what the tool does (used by LLMs to decide when to call it)
inputSchema
SchemaWithValidation<TSchemaIn>
Zod schema for validating input parameters
outputSchema
SchemaWithValidation<TSchemaOut>
Zod schema for validating output structure
suspendSchema
SchemaWithValidation<TSuspend>
Zod schema for suspend operation data
resumeSchema
SchemaWithValidation<TResume>
Zod schema for resume operation data
requestContextSchema
SchemaWithValidation<TRequestContext>
Zod schema for validating request context values
Whether the tool requires explicit user approval before execution
providerOptions
Record<string, Record<string, unknown>>
Provider-specific options passed to the model when this tool is used
toModelOutput
(output: TSchemaOut) => unknown
Optional function to transform the tool’s raw output before sending it to the model
Optional MCP-specific properties including annotations and metadata
A type-safe Tool instance with conditional typing based on schemas
Properties
Unique identifier for the tool
Description of what the tool does
inputSchema
SchemaWithValidation | undefined
Schema for validating input parameters
outputSchema
SchemaWithValidation | undefined
Schema for validating output structure
suspendSchema
SchemaWithValidation | undefined
Schema for suspend operation data
resumeSchema
SchemaWithValidation | undefined
Schema for resume operation data
requestContextSchema
SchemaWithValidation | undefined
Schema for validating request context values
Parent Mastra instance for accessing shared resources
Whether the tool requires explicit user approval before execution
providerOptions
Record<string, Record<string, unknown>> | undefined
Provider-specific options passed to the model
toModelOutput
((output: TSchemaOut) => unknown) | undefined
Function to transform output before sending to model
mcp
MCPToolProperties | undefined
MCP-specific properties
Execute Function
The execute function receives validated input and an execution context.
Parameters
context
ToolExecutionContext
required
Execution context Request context for dependency injection
tracingContext
ObservabilityContext | undefined
Tracing context for observability
Agent-specific context (when called from an agent) Current conversation messages
suspend
(data: TSuspend, options?: SuspendOptions) => void
Function to suspend agent execution
Data from resume (if suspended)
writableStream
WritableStream | undefined
Stream for writing data
Workflow-specific context (when called from a workflow) Function to update workflow state
suspend
(data: TSuspend, options?: SuspendOptions) => void
Function to suspend workflow execution
Data from resume (if suspended)
Return Value
The execute function should return:
The tool output (validated against outputSchema)
A Promise that resolves to the output
A ValidationError if validation fails
Examples
import { createTool } from '@mastra/core/tools' ;
import { z } from 'zod' ;
const greetTool = createTool ({
id: 'greet' ,
description: 'Say hello to someone' ,
inputSchema: z . object ({
name: z . string ()
}),
outputSchema: z . object ({
message: z . string ()
}),
execute : async ({ name }) => {
return { message: `Hello, ${ name } !` };
}
});
const calculateTool = createTool ({
id: 'calculate' ,
description: 'Perform calculations' ,
inputSchema: z . object ({
operation: z . enum ([ 'add' , 'subtract' , 'multiply' , 'divide' ]),
a: z . number (),
b: z . number ()
}),
outputSchema: z . object ({
result: z . number ()
}),
execute : async ({ operation , a , b }) => {
let result : number ;
switch ( operation ) {
case 'add' :
result = a + b ;
break ;
case 'subtract' :
result = a - b ;
break ;
case 'multiply' :
result = a * b ;
break ;
case 'divide' :
if ( b === 0 ) throw new Error ( 'Division by zero' );
result = a / b ;
break ;
}
return { result };
}
});
const saveTool = createTool ({
id: 'save-data' ,
description: 'Save data to storage' ,
inputSchema: z . object ({
key: z . string (),
value: z . any ()
}),
outputSchema: z . object ({
saved: z . boolean (),
key: z . string ()
}),
execute : async ({ key , value }, context ) => {
const storage = context ?. mastra ?. getStorage ();
if ( storage ) {
// Save to Mastra storage
await storage . set ( key , value );
}
return { saved: true , key };
}
});
const deleteFileTool = createTool ({
id: 'delete-file' ,
description: 'Delete a file (requires approval)' ,
requireApproval: true ,
inputSchema: z . object ({
filepath: z . string ()
}),
outputSchema: z . object ({
deleted: z . boolean (),
filepath: z . string ()
}),
execute : async ({ filepath }) => {
// Delete the file
await fs . unlink ( filepath );
return { deleted: true , filepath };
}
});
const approvalTool = createTool ({
id: 'approval' ,
description: 'Request user approval' ,
inputSchema: z . object ({
action: z . string (),
details: z . any ()
}),
outputSchema: z . object ({
approved: z . boolean ()
}),
suspendSchema: z . object ({
requestId: z . string (),
timestamp: z . number ()
}),
resumeSchema: z . object ({
approved: z . boolean ()
}),
execute : async ({ action , details }, context ) => {
// Check for resume data
const resumeData = context ?. agent ?. resumeData || context ?. workflow ?. resumeData ;
if ( ! resumeData ) {
// First call - suspend and wait for approval
const requestId = crypto . randomUUID ();
const suspend = context ?. agent ?. suspend || context ?. workflow ?. suspend ;
suspend ?.({ requestId , timestamp: Date . now () });
return { approved: false };
}
// Resumed after approval
return { approved: resumeData . approved };
}
});
const searchTool = createTool ({
id: 'search' ,
description: 'Search the web' ,
inputSchema: z . object ({
query: z . string ()
}),
providerOptions: {
anthropic: {
cacheControl: { type: 'ephemeral' }
}
},
execute : async ({ query }) => {
// Perform search
return { results: [] };
}
});
Tool with Request Context
const userTool = createTool ({
id: 'get-user-data' ,
description: 'Get current user data' ,
requestContextSchema: z . object ({
userId: z . string ()
}),
outputSchema: z . object ({
name: z . string (),
email: z . string ()
}),
execute : async ( inputData , context ) => {
const userId = context ?. requestContext ?. get ( 'userId' );
// Fetch user data using userId
return {
name: 'John Doe' ,
email: 'john@example.com'
};
}
});