Core Concept
Tools in Mastra:- Type-safe: Input/output validation with Zod schemas
- Composable: Combine tools from multiple sources
- Context-aware: Access Mastra resources and request context
- Suspendable: Support async operations requiring approval
- Observable: Automatic tracing and logging
Tool Sources
Agents compose tools from multiple sources:- Assigned Tools: Explicitly configured tools
- Memory Tools: Tools from memory instances (e.g., working memory)
- Workspace Tools: File operations and code execution
- MCP Tools: Tools from Model Context Protocol servers
- Toolsets: Collections of related tools
Creating Tools
Basic Tool
Tool with Output Schema
Validate tool outputs:Tool Execution Context
Tools receive an execution context with metadata:Agent Context
When called from an agent:Workflow Context
When called from a workflow:Request Context Schema
Validate request context values:Tool Approval
Require explicit user approval for sensitive operations:Suspend and Resume
Tools can suspend execution for async operations:Output Transformation
Transform tool output before sending to the model:Provider-Specific Options
Configure provider-specific behavior:MCP Tools
Define tools for Model Context Protocol:Toolsets
Group related tools into toolsets:Dynamic Tools
Create tools dynamically based on request context:Integration Tools
Use pre-built integration tools:Memory Tools
Memory instances provide working memory tools automatically:Workspace Tools
Workspace provides file and code execution tools:Best Practices
Provide clear descriptions
Provide clear descriptions
Write descriptions that explain when to use the tool:
Use Zod for validation
Use Zod for validation
Always define input schemas with descriptions:
Handle errors gracefully
Handle errors gracefully
Return meaningful errors instead of throwing:
Use requireApproval for destructive operations
Use requireApproval for destructive operations
Protect sensitive operations:
Related Resources
- Agents - Autonomous AI systems
- Workflows - Step-based execution
- Memory - Conversation persistence
- Mastra Class - Central orchestrator