Skip to main content

Overview

Custom tools are Python functions that extend agent capabilities beyond built-in atomic actions (click, type, swipe). Use cases:
  • External API calls (webhooks, REST services)
  • Data processing and calculations
  • Database operations
  • Domain-specific logic

Quick Start

Basic Example

Simple custom tool without device access:

Tool Structure

All custom tools follow this format:
Function signature:
Key points:
  • List only user parameters in "parameters" (not ctx)
  • ctx (an ActionContext instance) is injected automatically as a keyword argument
  • Access device via ctx.driver, shared state via ctx.shared_state, credentials via ctx.credential_manager
  • Use **kwargs for forward compatibility
  • Return type should be str

Using ActionContext

Access the device and state via the ctx parameter (an ActionContext instance injected automatically):
Available via ctx:
  • ctx.driver - DeviceDriver for raw device I/O (screenshot, tap, swipe, etc.)
  • ctx.state_provider - StateProvider to fetch/parse UI state
  • ctx.ui - Current UIState (refreshed each step)
  • ctx.shared_state - MobileAgentState for agent coordination
  • ctx.credential_manager - CredentialManager for secrets

Accessing Shared State

Access agent state via ctx.shared_state:
MobileAgentState fields:
  • step_number - Current execution step
  • action_history - List of executed actions
  • action_outcomes - Success/failure per action
  • manager_memory - Manager planning notes (append-only string)
  • fast_memory - FastAgent remember() items (list of strings, max 10)
  • custom_variables - User-provided variables
  • visited_packages - Apps visited
  • current_package_name - Current app package
  • plan - Current Manager plan
  • More in mobilerun/agent/droid/state.py

Common Patterns

API Integration

Database Query

Async Operations


Best Practices

1. Clear Descriptions

Write descriptive, specific descriptions:

2. Error Handling

Always catch exceptions:

3. Argument Validation

Validate inputs before processing:

4. Logging

Use Python logging for debugging:

Advanced Example

Combining ActionContext, shared state, and credentials:

See Agent Architecture for understanding shared state and custom tools integration.