Skip to main content
The Agent class is the primary entry point for the mobile-use SDK, responsible for managing device interaction and executing tasks.

Import

Constructor

Parameters

AgentConfig
Custom agent configuration. If not provided, default configuration is used.

Example

Methods

init

Initialize the agent by connecting to a device and starting required servers.

Parameters

str
Minitap API key for Platform features (Platform tasks, cloud mobiles). Can also be set via MINITAP_API_KEY environment variable.
int
default:3
Maximum number of attempts to start servers if they fail
int
default:5
Number of retries for API calls
int
default:5
Seconds to wait between retries

Returns

bool
True if initialization succeeded, False otherwise

Example

Example with API Key

Always check the return value of init() before running tasks. Note that init() is now an async method and must be awaited.

run_task

Execute a mobile automation task asynchronously.

Parameters

str
Natural language description of what to accomplish
type[TOutput] | str
Type of output:
  • Pydantic model class for structured output
  • String description for output format
str | AgentProfile
Agent profile to use (name or instance)
str
Optional name for the task (for logging/debugging)
TaskRequest[TOutput] | PlatformTaskRequest[TOutput]
Pre-built TaskRequest or PlatformTaskRequest (alternative to individual parameters)

Returns

str | dict | TOutput | None
Task result:
  • str: Simple text output
  • dict: Unstructured dictionary
  • TOutput: Instance of specified Pydantic model
  • None: Task failed or no output

Examples

For platform tasks, the Locked App Package is configured on the platform task itself, not in PlatformTaskRequest. For local tasks using TaskRequest, use .with_locked_app_package() in the builder.

new_task

Create a new task request builder for fluent task configuration.

Parameters

str
required
Natural language description of what to accomplish

Returns

TaskRequestBuilder[None]
TaskRequestBuilder instance for fluent configuration

Example


clean

Clean up resources, stop servers, and reset the agent state.

Parameters

bool
default:false
Set to True to clean zombie/pre-existing mobile-use servers

Example

Use force=True if you have zombie servers from previous runs:
Note that clean() is now an async method and must be awaited.

install_apk

Install an APK on the connected Android device.
This method works with both local devices and cloud mobiles:
  • For local devices: Uses ADB to install the APK directly
  • For cloud mobiles: Installs it on the cloud mobile via the API

Parameters

str | Path
required
Path to the local APK file to install

Raises

  • FileNotFoundError: If the APK file does not exist
  • AgentNotInitializedError: If the agent is not initialized (local mode)
  • AgentError: If the device is not an Android device or ADB client is not initialized
  • CloudMobileServiceUninitializedError: If cloud mobile service is not initialized (cloud mode)
  • AgentTaskRequestError: If cloud mobile ID is not configured (cloud mode)
APK installation is only supported on Android devices. Attempting to install an APK on an iOS device will raise an AgentError.
For cloud mobiles, the APK must be x86_64 compatible.
For cloud mobiles, the install_apk method automatically starts the cloud mobile if it’s not already running, so you don’t need to manually start it before installing.

get_screenshot

Capture a screenshot from the mobile device.
This method works with both local devices and cloud mobiles:
  • For local devices: Uses ADB (Android) or xcrun (iOS) to capture screenshots directly
  • For cloud mobiles: Retrieves screenshots from the cloud mobile via the Platform API

Returns

Image.Image
Screenshot as a PIL Image object

Raises

  • AgentNotInitializedError: If the agent is not initialized
  • CloudMobileServiceUninitializedError: If using cloud mobile without proper initialization
  • Exception: If screenshot capture fails

Example

Example with Cloud Mobile

Use get_screenshot() for debugging, monitoring, or extracting visual data from your mobile device during automation workflows.

Complete Example

Exception Handling

The Agent may raise the following exceptions:
  • AgentNotInitializedError: Agent methods called before initialization
  • DeviceNotFoundError: No device found or device disconnected
  • AgentProfileNotFoundError: Specified profile not found
  • ServerStartupError: Failed to start required servers
  • ExecutableNotFoundError: Required executable (adb, idb, xcrun) not found
  • AgentTaskRequestError: Invalid task request configuration
  • PlatformServiceUninitializedError: Platform service not initialized (missing API key)
  • CloudMobileServiceUninitializedError: Cloud mobile service not initialized (missing API key or cloud mobile not configured)
  • AgentInvalidApiKeyError: Invalid Minitap API key
See Exceptions for details.

Next Steps

Task Request Builder

Configure tasks with the builder pattern

Agent Config Builder

Configure agent behavior