adk-rust-macros
Proc macros for ADK-Rust — #[tool] attribute for zero-boilerplate tool registration.
Overview
This crate provides the #[tool] attribute macro that turns an async function into a full adk_core::Tool implementation. No manual struct definitions, no trait boilerplate, no JSON schema wiring.
Installation
[]
= "0.5.0"
= "0.5.0"
= "1.0"
= { = "1.0", = ["derive"] }
= "1.0"
Quick Start
use tool;
use JsonSchema;
use Deserialize;
/// Add two numbers together.
async
// Generated: pub struct Add; implements adk_core::Tool
// Register: agent_builder.tool(Arc::new(Add))
What the Macro Generates
For a function get_weather, the macro generates:
| Input | Output |
|---|---|
Function name get_weather |
Tool name "get_weather" |
Doc comment /// Get the current weather. |
Tool description "Get the current weather." |
Arg type WeatherArgs |
JSON schema via schemars::schema_for!(WeatherArgs) |
| — | Struct GetWeather implementing Tool trait |
The generated struct is zero-sized and implements adk_core::Tool with:
name()→ snake_case function namedescription()→ doc comment textparameters_schema()→ JSON schema from the args typeexecute()→ deserializes args, calls your function
Usage Patterns
Simple tool (args only)
/// Search the knowledge base for documents matching a query.
async
// Use: Arc::new(SearchDocs)
Tool with context access
use Arc;
use ToolContext;
/// Read the current session state.
async
No-args tool
/// Get the current server time.
async
// Use: Arc::new(GetTime)
Schema Cleaning
The macro automatically cleans the generated JSON schema for LLM API compatibility:
- Strips
$schemaandtitlefields (rejected by Gemini) - Simplifies nullable types:
{"type": ["string", "null"]}→{"type": "string"} - Unwraps
anyOfwrappers for simpleOption<T>fields
Naming Convention
| Function | Generated Struct |
|---|---|
get_weather |
GetWeather |
search_docs |
SearchDocs |
add |
Add |
send_email_notification |
SendEmailNotification |
The function name (snake_case) becomes the tool name string. The struct name (PascalCase) is what you pass to Arc::new().
Requirements
- Function must be
async - Args type must implement
serde::Deserializeandschemars::JsonSchema - Return type must be
Result<serde_json::Value, adk_tool::AdkError> - Doc comments are used as the tool description (falls back to function name with underscores replaced by spaces)
License
Apache-2.0