Give agents your app's own diagnostics and actions
Expose focused Flutter app data and operations so agents can answer questions the built-in tools cannot.
Custom tools let an agent ask your Dart app code for a precise answer, such as the current user or an app-specific state snapshot. Expose small, structured results and only the development actions your workflow needs.
final registration = await Ansight.instance.registerTool(
const AnsightToolDefinition(
id: 'app.current_user',
name: 'Current user',
category: 'app',
policy: AnsightToolPolicy.read,
),
(arguments, context) async => const AnsightToolResult.success(
result: <String, Object?>{'signedIn': true},
),
);
await registration.unregister();
Use stable ids, narrow structured results, an explicit read, write, or
critical policy, and the least-permissive tool guard. Reserve critical for
destructive, secret-bearing, or arbitrary code-invoking operations.
Native tool entries report dynamic runtime availability and remediation through the shared catalog. Dart-backed custom tools are advertised as available while registered; when one becomes unusable, unregister it or return a stable failure errorCode from the handler. The Dart registration API does not currently expose a pre-invocation availability callback.
Authoring and Execution Context
registerTool(definition, handler) already supplies the concise form. Define argumentsSchema and resultSchema on AnsightToolDefinition, and choose an explicit policy. The Dart bridge currently supplies string argument values: parse and validate them explicitly; do not assume typed JSON values.
Handlers run on the Dart isolate receiving the native callback. Await mutations before returning; schedule widget changes at a valid point in the Flutter frame lifecycle. Native engine or UI operations must dispatch in the platform implementation. Keep expensive work off the UI isolate. A bridge timeout does not guarantee native cancellation. Dart tools have no pre-invocation availability callback: unregister unavailable tools or return a stable scene_not_ready failure.
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 registerTool. 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.