Skip to main content

tool

Attribute Macro tool 

Source
#[tool]
Expand description

Attribute macro that generates a Tool implementation from an async function.

§Requirements

  • The function must be async
  • It must take exactly one argument (the args struct) that implements serde::de::DeserializeOwned and schemars::JsonSchema
  • It must return Result<serde_json::Value, adk_tool::AdkError>
  • Doc comments become the tool description

§Attributes

Optional attributes can be passed to configure tool metadata:

  • read_only — marks the tool as having no side effects (is_read_only() → true, effect() → ToolEffect::ReadOnly)
  • idempotent — marks the tool as safe to repeat (effect() → ToolEffect::Idempotent), so a retry budget may retry it after a retryable error. Ignored alongside read_only.
  • concurrency_safe — marks the tool as safe for concurrent execution (is_concurrency_safe() → true)
  • long_running — marks the tool as long-running (is_long_running() → true)
  • lenient_args — turns off argument validation; see below

§Argument validation

Before the function runs, the call’s arguments are checked against the argument type’s schema, with additionalProperties: false set on every object that declares all of its fields (close_object_schemas). A key the schema does not name, a value outside a #[schemars(range(...))], length(...) or pattern(...) bound, or a missing required field fails the call with an ErrorCategory::InvalidInput error with code TOOL_INVALID_ARGUMENTS_CODE, which lists each violation by JSON pointer. An LlmAgent shows that message to the model so it can correct the call; the function does not run. parameters_schema() is not closed: Gemini’s function declarations reject additionalProperties, and not every path to a provider runs a schema adapter.

A schema that differs from what the type deserializes from also rejects calls serde would accept: a #[serde(alias = "...")] key, or a field with a custom deserialize_with whose schema is narrower than its input. Keep such keys open with #[schemars(extend("additionalProperties" = true))] on the struct, or opt the tool out with #[tool(lenient_args)], which deserializes with serde alone: unknown keys are dropped and schema bounds are advisory.

In both modes an integral float at an integer field, such as 3.0 for a u32, is read as the integer (coerce_integral_floats); some models write every number with a decimal point.

§Examples

ⓘ
/// Search the knowledge base for documents matching a query.
#[tool]
async fn search_docs(args: SearchArgs) -> Result<serde_json::Value, adk_tool::AdkError> {
    // ...
}

/// Look up cached data (read-only, safe for parallel dispatch).
#[tool(read_only, concurrency_safe)]
async fn cache_lookup(args: LookupArgs) -> Result<serde_json::Value, adk_tool::AdkError> {
    // ...
}

/// Set a ticket's status; setting the same status twice changes nothing.
#[tool(idempotent)]
async fn set_ticket_status(args: StatusArgs) -> Result<serde_json::Value, adk_tool::AdkError> {
    // ...
}

/// Accept legacy argument names that only serde knows about.
#[tool(lenient_args)]
async fn legacy_lookup(args: LegacyArgs) -> Result<serde_json::Value, adk_tool::AdkError> {
    // ...
}

// Generated: pub struct SearchDocs; implements Tool
// Use: agent_builder.tool(Arc::new(SearchDocs))