Skip to main content

Crate molo_macros

Crate molo_macros 

Source
Expand description

A procedural macro that compiles an async function into a molo::tool::Tool implementation.

The tool attribute macro generates all the boilerplate needed for a tool definition in one shot from the function signature: the unit struct, the parameter JSON Schema, and the call method (including argument parsing and &SharedState injection), so you never have to write impl Tool by hand.

This crate is usually not used as a direct dependency; it is re-exported by molo and invoked as #[molo::tool(...)] (molo::tool is also the name of a module; attribute macros and modules live in different namespaces, so they can share the same name):

#[molo::tool(description = "Evaluate the result of a mathematical expression")]
async fn calculator(expression: String) -> Result<String, molo::tool::ToolError> {
    // Business logic...
    Ok(expression)
}

§Shape constraints

  • Parameters: 0 or 1 business parameter, plus an optional trailing &SharedState (an immutable reference recognized by the type name SharedState, injected automatically; must be the last parameter);
  • The business parameter type must implement schemars::JsonSchema: primitive types come with an implementation out of the box, and custom structs just need #[derive(JsonSchema)];
  • Return: Result<String, ToolError>, Result<ToolOutput, ToolError>, or Result<ToolResult, ToolError> (a type mismatch is caught by the compiler on the generated code);
  • Attributes: description (required), name (optional, defaults to the function name), protected (optional, defaults to false; when true the result is protected and exempt from window trimming), plus optional policy hints: side_effects, risk, requires_confirmation, and timeout_secs.

§Dependency prerequisites

The generated code hard-references the ::molo::, ::serde_json::, and ::schemars:: paths: depend on this framework under the crate name molo (renaming it would make the generated code unresolvable), and add serde_json and schemars as direct dependencies — the generated code references them directly, and the parameter struct’s #[derive(JsonSchema)] also requires schemars.

Attribute Macros§

tool
Compiles an async function into a molo::tool::Tool implementation.