Overview
Custom tools enable agents to:- Call external APIs (weather, databases, etc.)
- Execute custom business logic
- Integrate with third-party services
- Perform calculations or data transformations
- Access internal systems
tool() function and Zod schemas for type-safe inputs and outputs.
Creating a Custom Tool
Basic Tool Structure
Weather API Example
Frompackages/core/examples/agent-custom-tools.ts:
Using Custom Tools with Agents
Standard Agent (Non-CUA)
Computer Use Agent (CUA)
Tool Integration in CUA Clients
Anthropic CUA
Location:packages/core/lib/v3/agent/AnthropicCUAClient.ts
Converting Tools to Anthropic Format:
Google CUA
Location:packages/core/lib/v3/agent/GoogleCUAClient.ts
Converting Tools to Google Format:
OpenAI CUA
Location:packages/core/lib/v3/agent/OpenAICUAClient.ts
Converting Tools to OpenAI Format:
Built-in Agent Tools
Stagehand provides built-in tools for standard agents. Location:packages/core/lib/v3/agent/tools/
Core Tools
act - Perform actions (click, type):Coordinate-Based Tools (Hybrid Mode)
click - Click at coordinates:Form Tools
fillForm - Fill forms using DOM (DOM mode):Tool Modes
Location:packages/core/lib/v3/agent/tools/index.ts
Stagehand supports two tool modes:
DOM Mode (Default)
act- DOM-based click/typefillForm- DOM-based form fillingextract- Structured data extractionariaTree- Accessibility treegoto,navback,screenshot,scroll,wait,keys,think
click,type,dragAndDrop,clickAndHold,fillFormVision
Hybrid Mode
click,type- Coordinate-based actionsfillFormVision- Vision-based form fillingdragAndDrop,clickAndHold- Advanced interactions- All other standard tools
fillForm(replaced byfillFormVision)
Filtering Tools
Variables in Tools
Tools likeact, type, and fillForm support variables:
Advanced Tool Examples
Database Query Tool
API Integration Tool
Data Transformation Tool
Best Practices
- Clear descriptions: Help the model understand when to use each tool
- Type-safe schemas: Use Zod for input validation
- Error handling: Return meaningful error messages
- Logging: Log tool executions for debugging
- Async execution: Tools can perform async operations
- Return structured data: Return JSON-serializable results
- Enable experimental mode: Required for custom tools in Stagehand
Type Safety
References
- Custom Tools Example:
packages/core/examples/agent-custom-tools.ts - Tool Implementations:
packages/core/lib/v3/agent/tools/ - Anthropic Tool Handler:
packages/core/lib/v3/agent/AnthropicCUAClient.ts:471-500 - Google Tool Handler:
packages/core/lib/v3/agent/utils/googleCustomToolHandler.ts - OpenAI Tool Handler:
packages/core/lib/v3/agent/OpenAICUAClient.ts:444-460