Client side tools in the iOS SDK
Register a ClientTool so your agent can run Swift code inside your iOS app, such as opening a screen or reading a value from the device.
Working draft. These docs are illustrative: names, endpoints and examples describe the intended behaviour and can change before launch.
A client tool is a function in your app that the agent can call during a conversation. The agent decides when to call it, the SDK runs your code, and the result goes back to the agent so it can continue its answer. Use client tools for things only the app can do: open an order screen, read the current cart, or turn on a setting.
How a call flows
- The customer sends a message and the agent decides a tool is needed.
- The stream delivers a
.toolCallevent with the tool name and arguments. - The SDK finds your registered
ClientTooland runs its handler. - The SDK sends the handler result back to the agent.
- The agent writes the rest of the answer, using the result.
The same flow exists on the web as client side custom actions, and in the REST API as client actions.
Register a tool
let openOrder = ClientTool(
name: "open_order_screen",
description: "Open the order details screen for an order id.",
parameters: [
"type": "object",
"properties": ["orderId": ["type": "string"]],
"required": ["orderId"],
],
handler: { args in
guard let id = args["orderId"]?.stringValue else {
return ["opened": false]
}
await navigator.openOrder(id)
return ["opened": true]
}
)
octo.registerTool(openOrder)
The parameters value and the handler result are JSONValue values, so dictionary and string literals work as shown.
| Field | Description |
|---|---|
name |
A stable identifier, lowercase with underscores. |
description |
A plain sentence that tells the agent when to use the tool. |
parameters |
A JSON Schema object that describes the arguments. |
handler |
An async closure that receives the arguments as [String: JSONValue] and returns a JSONValue. |
Register tools once, right after you create the client. Registering a second tool with the same name replaces the first.
Write good handlers
- Return quickly. Long work blocks the answer. If you must wait for a user action, return a result that says so.
- Return small JSON. Include only facts the agent can use, such as
["opened": true]. - Do not throw. The handler is not a throwing closure, so catch errors inside it and return
["error": "..."], which lets the agent explain the problem to the customer. - Hop to the main actor for UI work. If
navigatoris marked@MainActor, theawaitin the example does that for you. - Check permissions in the handler. The agent chooses when to call a tool, so treat arguments as untrusted input.
Warning: Never expose a tool that changes money or personal data without a confirmation step in your own UI. The agent can call any tool you register.
Test it
Ask the agent a question that should trigger the tool, then watch for the .toolCall event in your stream. Set up the matching behavior under the agent's Actions tab, and try the full conversation in the Playground before you ship the app.