Skip to main content

tool

Attribute Macro tool 

Source
#[tool]
Expand description

Convert an async function into a struct that implements synaptic_core::Tool.

§Features

  • Function name becomes the tool name
  • Doc comments become the tool description
  • Parameters are mapped to a JSON Schema automatically
  • Option<T> parameters are optional (not in required)
  • #[default = value] sets a default for a parameter
  • #[inject(state)], #[inject(store)], #[inject(tool_call_id)] inject runtime values (the parameter is hidden from the LLM schema); using any inject attribute switches the generated impl to RuntimeAwareTool
  • Parameter doc comments become "description" in the schema

§Example

use synaptic_macros::tool;
use synaptic_core::SynapticError;

/// Search the web for information.
#[tool]
async fn search(
    /// The search query
    query: String,
    /// Maximum number of results
    #[default = 5]
    max_results: i64,
) -> Result<String, SynapticError> {
    Ok(format!("Searching for '{}' (max {})", query, max_results))
}

// `search` is now a function returning `Arc<dyn Tool>`
let tool = search();

§Custom name

#[tool(name = "web_search")]
async fn search(query: String) -> Result<String, SynapticError> {
    Ok(format!("Searching for '{}'", query))
}