Give agents your app's own diagnostics and actions

Expose focused Android app data and operations so agents can answer questions the built-in tools cannot.

Custom tools let an agent ask your app for a precise answer, such as the current debug user, or run a focused development operation. Your app implements the handler and exposes it through the same guarded remote-tool protocol as the packaged suites.

Register a Tool

import ai.ansight.runtime.AndroidToolResult
import ai.ansight.runtime.AnsightOptions
import ai.ansight.runtime.ToolPolicy
import ai.ansight.runtime.FunctionJsonAndroidTool
import ai.ansight.runtime.ToolDefinition
import ai.ansight.runtime.ToolSchema
import org.json.JSONObject

val tool = FunctionJsonAndroidTool(
    definition = ToolDefinition(
        id = "app.current_user",
        name = "Current User",
        description = "Returns the current debug user id.",
        category = "app",
        policy = ToolPolicy.Read,
        keywords = "app user debug",
        argumentsSchema = ToolSchema.obj(),
        resultSchema = ToolSchema.obj(
            properties = mapOf("userId" to ToolSchema.string()), required = listOf("userId"),
        ),
    ),
) { _, _ ->
    AndroidToolResult.success(
        JSONObject().put("userId", session.currentUserId)
    )
}

val options = AnsightOptions.createBuilder()
    .addTool(tool)
    .withReadOnlyToolAccess()
    .build()

Dynamic Availability

Tools are available by default. When execution depends on current app state, use FunctionJsonAndroidTool or implement AndroidTool.availability(...) so discovery can explain the prerequisite before a call:

import ai.ansight.runtime.FunctionJsonAndroidTool
import ai.ansight.runtime.ToolAvailability
import ai.ansight.runtime.ToolDefinition

val tool = FunctionJsonAndroidTool(
    definition = ToolDefinition(
        id = "app.current_user",
        name = "Current User",
        description = "Returns the current debug user id.",
        category = "app",
        policy = ToolPolicy.Read,
        keywords = "app user debug",
        argumentsSchema = ToolSchema.obj(),
        resultSchema = ToolSchema.obj(
            properties = mapOf("userId" to ToolSchema.string()), required = listOf("userId"),
        ),
    ),
    availabilityHandler = {
        if (session.isSignedIn) {
            ToolAvailability.Available
        } else {
            ToolAvailability.unavailable(
                reasonCode = "user_not_signed_in",
                reason = "No debug user is signed in.",
                requiredState = "Signed-in debug session",
                remediation = "Open the account fixture and sign in.",
                retryable = true,
            )
        }
    },
) { _, _ ->
    AndroidToolResult.success(JSONObject().put("userId", session.currentUserId))
}

The catalog publishes this under runtime and sets executable accordingly. Availability is evaluated again immediately before execution, preventing a stale catalog entry from bypassing the prerequisite.

Registration API

  • addTool(...): adds one tool to options before runtime initialization.
  • addTools(...): adds multiple tools to options before runtime initialization.
  • registerTool(...): registers a tool at runtime.
  • isToolRegistered(...): checks whether a tool id is currently registered.

Specific Concerns

  • Prefer narrow, app-specific operations.
  • Choose the correct policy: ToolPolicy.Read, ToolPolicy.Write, or ToolPolicy.Critical.
  • Do not expose broad scripting, arbitrary file access, or secret access through custom tools.
  • Registered tools are still hidden and unusable until the runtime guard allows their policy.

Tool Definition Checklist

  • Stable tool id, usually under an app-owned prefix such as app.
  • Clear name and description
  • Category and keywords that match the workflow
  • Correct policy
  • Explicit argument validation and failure messages
  • Structured JSON result payloads for callers
  • Dynamic availability with a stable reason code, required state, remediation, and retryability when execution has runtime prerequisites

Authoring and Execution Context

Prefer FunctionJsonAndroidTool(definition, availabilityHandler, handler) when inputs contain booleans, numbers, arrays, or nested objects. It receives a JSONObject and uses the runtime’s argument/result schema validation. FunctionAndroidTool and androidSimpleTool remain supported for flattened string arguments. Keep schemas explicit on ToolDefinition.

Handlers and availability checks run on the transport context, without a main-thread guarantee. Dispatch Android view access to the main looper and engine operations to the engine’s update thread. Keep availability fast and based on thread-safe state. Android handlers are synchronous; a caller timeout does not promise cancellation of a mutation.

Expose a Domain Model

Start with scene.snapshot (read) returning a bounded JSON projection of scene id, selected entity, and transforms. Add scene.set_visible (write) taking one entity id and a boolean. Reject unknown ids and a loading scene with stable error codes, dispatch the change to the scene owner, and return the updated visibility. Read current state at invocation time rather than capturing a stale snapshot at registration.

Keep schemas, policy, and dispatch explicit even when using FunctionJsonAndroidTool. Verify a successful read, a successful mutation, invalid arguments, an unavailable scene, and a denied write under a read-only guard. For a large model or a durable snapshot, use artifact publication from the app instead of embedding it in a tool result.