Agent Tools
Tools extend agent capabilities by allowing them to perform actions, access external systems, and manipulate data. This guide covers creating tools and assigning them to agents.What are Tools?
Tools are type-safe functions that agents can call during conversations. Each tool has:- Unique ID - Identifier for the tool
- Description - What the tool does (used by LLM for selection)
- Input Schema - Zod schema for parameter validation
- Execute Function - The actual logic to run
- Optional Output Schema - Validate tool results
Creating Tools
Basic Tool
From:packages/core/src/tools/tool.ts
Tool with Output Validation
Tool with Approval Required
For sensitive operations, require user approval:Tool with Mastra Integration
Access Mastra resources in tools:Assigning Tools to Agents
Static Tool Assignment
Assign tools when creating the agent:Dynamic Tool Assignment
From:examples/agent/src/mastra/agents/dynamic-tools-agent.ts
Load tools based on request context:
Tool Discovery Pattern
From:examples/agent/src/mastra/agents/dynamic-tools-agent.ts
Let agents discover and load tools on demand:
Runtime Tool Assignment
Override agent tools for specific calls:Tool Execution Control
Tool Choice Strategies
Control when and how tools are called:Active Tools Filter
Limit which tools are available:Tool Approval
Require approval for sensitive operations:Tool Examples from Source
Cooking Tool
From:examples/agent/src/mastra/agents/model-v2-agent.ts
Calculator Tool
From:examples/agent/src/mastra/agents/dynamic-tools-agent.ts
Database Search Tool
Workspace Tools
When you attach a workspace to an agent, file operation tools are automatically added:Tool Best Practices
Write clear descriptions
Write clear descriptions
Tool descriptions help the LLM decide when to use each tool:
Use descriptive parameter names
Use descriptive parameter names
Add
.describe() to schema fields:Validate both input and output
Validate both input and output
Use schemas to catch errors early:
Handle errors gracefully
Handle errors gracefully
Return user-friendly error messages:
Use requireApproval for sensitive operations
Use requireApproval for sensitive operations
Protect destructive actions:
Next Steps
Streaming
Learn about streaming agent responses
Structured Output
Get typed, validated output from agents
Workflows
Execute multi-step workflows from agents
Tool API Reference
Complete API documentation for tools