Skip to content
QwryAI
Documentation
Pages

Android SDK

Client-Side Tools

Run your agent's client-side actions as Kotlin functions on the device

What are client-side tools?

A client-side tool is a function in your app that your agent can call in the middle of an answer. Register a handler under the action's name; when the agent calls it, the SDK runs your handler, sends what it returns back to the agent, and the agent carries on answering with it.

tool

class QwryClient, Package: com.qwryai.sdk

Kotlin
fun tool(name: String, handler: suspend (input: JSONObject) -> Any?)

Registers a handler for a client-side action. Registering the same name again replaces the handler.

name String required

The action's name, as set on your agent.

handler suspend (JSONObject) -> Any? required

A suspend function that receives the agent's input as an org.json.JSONObject and returns a JSON value: a JSONObject, JSONArray, string, number, boolean or null.

Kotlin
client.tool("get_order_status") { input ->
    val orderNumber = input.getString("order_number")

    // Read from your app's own data, call your API, use a sensor...
    val order = orders.find(orderNumber)

    JSONObject()
        .put("order_number", orderNumber)
        .put("status", order.status)
        .put("eta", order.eta)
}

The agent can now call get_order_status while it answers. Each send, sendNonStreaming or retry allows up to 10 rounds of tool calls by default (set maxToolLoopSteps in Qwry.create to change it). If the agent keeps calling tools beyond that, the call throws a QwryException with the code TOOL_LOOP_LIMIT_EXCEEDED.

If your handler throws, the agent receives {"error": "<the exception's message>"} and decides what to say. If the agent calls a tool your app hasn't registered, it receives an error saying so. Either way, the answer continues.

removeTool

Kotlin
fun removeTool(name: String)

Unregisters a tool handler. If the agent calls that tool later, it's told no tool by that name is registered.

name String required

The name you registered.

Tracking execution

Use the onToolCall and onToolResult handlers of send to show what the agent is doing:

Kotlin
client.send("Where is order 1042?") {
    onToolCall = { call ->
        println("Agent is calling: ${call.name}")
        println("Input: ${call.input}")
    }
    onToolResult = { result ->
        println("Tool result: ${result.output}")
    }
    onTextDelta = { delta ->
        print(delta) // The agent's answer, written after the tool result
    }
}

MessagePart.ToolCall

data class MessagePart.ToolCall, Package: com.qwryai.sdk

Passed to onToolCall before the tool runs.

Kotlin
data class ToolCall(
    val id: String,
    val name: String,
    val input: Any?
) : MessagePart

input is a JSON value as read by org.json, usually a JSONObject.

MessagePart.ToolResult

data class MessagePart.ToolResult, Package: com.qwryai.sdk

Passed to onToolResult after the tool has run.

Kotlin
data class ToolResult(
    val id: String,
    val name: String,
    val output: Any?
) : MessagePart

id matches the ToolCall it answers. output is what your handler returned, or the {"error": …} object the agent received.

Interactive tools

Handlers are suspend functions, so they can wait for the user. For example, ask the user to choose a delivery slot and return their choice:

Kotlin
val slotRequests = MutableStateFlow<CompletableDeferred<String>?>(null)

client.tool("choose_delivery_slot") { _ ->
    val choice = CompletableDeferred<String>()

    // Your UI collects slotRequests and shows a picker
    slotRequests.value = choice

    // Suspend until the user picks a slot
    val slot = choice.await()
    slotRequests.value = null

    JSONObject().put("slot", slot)
}

Explore the documentation