tiny-agent-rs 0.1.0

A lightweight, type-safe Rust agent library for LLM tool calling
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# Tool Macro Guide

## Overview

The `tool!` macro simplifies tool creation by eliminating boilerplate and automatically generating JSON schemas from parameter structs. This reduces tool implementation effort by approximately 80%.

## Quick Start

### 1. Define Your Parameters

Use `schemars::JsonSchema` and `serde::Deserialize` derives on your parameter struct:

```rust
use schemars::JsonSchema;
use serde::Deserialize;

#[derive(Debug, Deserialize, JsonSchema)]
struct MyToolParams {
    /// Description for this field (shown in schema)
    required_field: String,
    
    /// An optional parameter
    #[serde(default)]
    optional_field: Option<i32>,
}
```

### 2. Create Your Tool

Use the `tool!` macro with a closure that contains your tool logic:

```rust
tiny_agent_rs::tool!(
    name = "my_tool",
    description = "Brief description of what the tool does",
    params = MyToolParams,
    |params: MyToolParams| async move {
        // Your tool logic here
        let result = do_something(params.required_field);
        
        // Return Ok with JSON value
        Ok(serde_json::json!({
            "result": result
        }))
    }
);
```

### 3. Register and Use

The macro generates a struct named using PascalCase (e.g., `MyTool` for `my_tool`):

```rust
let mut factory = FunctionFactory::new();
factory.register_tool(MyTool);

let agent = Agent::new(api_key, factory);
```

## Macro Syntax

```rust
tool!(
    name = "tool_name",           // Snake case name for the tool
    description = "Tool purpose",  // Description for LLM
    params = ParamStructType,      // Your parameter struct type
    |params: ParamStructType| async move {
        // Async closure with tool logic
        // Must return Result<serde_json::Value, String>
    }
);
```

## What the Macro Generates

The macro automatically generates:

1. **Tool struct** - Named in PascalCase (e.g., `tool_name` → `ToolName`)
2. **Tool trait implementation** - All required methods
3. **JSON schema** - Derived from your `JsonSchema` implementation
4. **Parameter parsing** - With proper error messages
5. **Error handling** - Converts your `String` errors to `AgentError::ToolExecution`

## Parameter Structs

### Required Fields

```rust
#[derive(Debug, Deserialize, JsonSchema)]
struct RequiredParams {
    field1: String,
    field2: i32,
}
```

### Optional Fields

Use `#[serde(default)]` for optional fields:

```rust
#[derive(Debug, Deserialize, JsonSchema)]
struct OptionalParams {
    required: String,
    
    #[serde(default)]
    optional: Option<String>,
}
```

### Nested Structures

Complex nested types are supported:

```rust
#[derive(Debug, Deserialize, JsonSchema)]
struct Address {
    street: String,
    city: String,
}

#[derive(Debug, Deserialize, JsonSchema)]
struct PersonParams {
    name: String,
    
    #[serde(default)]
    address: Option<Address>,
}
```

### Field Descriptions

Use doc comments to add descriptions (appear in generated schema):

```rust
#[derive(Debug, Deserialize, JsonSchema)]
struct DescribedParams {
    /// The user's full name
    name: String,
    
    /// Age in years (must be positive)
    age: u32,
}
```

## Complete Examples

### Text Transformation Tool

```rust
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::json;

#[derive(Debug, Deserialize, JsonSchema)]
struct TextTransformParams {
    /// The text to transform
    text: String,
    
    /// Operation: "uppercase", "lowercase", or "reverse"
    operation: String,
}

tiny_agent_rs::tool!(
    name = "text_transform",
    description = "Transform text by applying uppercase, lowercase, or reverse operations",
    params = TextTransformParams,
    |params: TextTransformParams| async move {
        let result = match params.operation.as_str() {
            "uppercase" => params.text.to_uppercase(),
            "lowercase" => params.text.to_lowercase(),
            "reverse" => params.text.chars().rev().collect(),
            _ => return Err(format!("Unknown operation: {}", params.operation)),
        };
        
        Ok(json!({
            "original": params.text,
            "operation": params.operation,
            "result": result
        }))
    }
);

// Use it:
// factory.register_tool(TextTransform);
```

### Calculator Tool

```rust
#[derive(Debug, Deserialize, JsonSchema)]
struct MathParams {
    /// First number
    a: f64,
    
    /// Second number
    b: f64,
    
    /// Operation: "add", "subtract", "multiply", or "divide"
    operation: String,
}

tiny_agent_rs::tool!(
    name = "math_calculator",
    description = "Perform basic math operations on two numbers",
    params = MathParams,
    |params: MathParams| async move {
        let result = match params.operation.as_str() {
            "add" => params.a + params.b,
            "subtract" => params.a - params.b,
            "multiply" => params.a * params.b,
            "divide" => {
                if params.b == 0.0 {
                    return Err("Cannot divide by zero".to_string());
                }
                params.a / params.b
            }
            _ => return Err(format!("Unknown operation: {}", params.operation)),
        };
        
        Ok(json!({
            "a": params.a,
            "b": params.b,
            "operation": params.operation,
            "result": result
        }))
    }
);

// Use it:
// factory.register_tool(MathCalculator);
```

### API Integration Tool

```rust
#[derive(Debug, Deserialize, JsonSchema)]
struct ApiParams {
    /// The API endpoint to call
    endpoint: String,
    
    /// Optional query parameters
    #[serde(default)]
    params: Option<std::collections::HashMap<String, String>>,
}

tiny_agent_rs::tool!(
    name = "api_call",
    description = "Make HTTP GET requests to external APIs",
    params = ApiParams,
    |params: ApiParams| async move {
        let client = reqwest::Client::new();
        let mut request = client.get(&params.endpoint);
        
        if let Some(query_params) = params.params {
            request = request.query(&query_params);
        }
        
        let response = request
            .send()
            .await
            .map_err(|e| format!("Request failed: {}", e))?;
            
        let body = response
            .text()
            .await
            .map_err(|e| format!("Failed to read response: {}", e))?;
        
        Ok(json!({
            "endpoint": params.endpoint,
            "response": body
        }))
    }
);
```

## Error Handling

Return errors as `String`:

```rust
tiny_agent_rs::tool!(
    name = "divide",
    description = "Divide two numbers",
    params = DivideParams,
    |params: DivideParams| async move {
        if params.divisor == 0.0 {
            return Err("Division by zero".to_string());
        }
        
        Ok(json!({ "result": params.dividend / params.divisor }))
    }
);
```

The macro automatically converts your `String` errors to `AgentError::ToolExecution`.

## Schema Generation

The generated schema is OpenAI-compatible and includes:

- **Field types** - Automatically inferred from Rust types
- **Required fields** - Non-optional fields marked as required
- **Descriptions** - From doc comments
- **Nested objects** - Complex types properly structured

Example generated schema:

```json
{
  "type": "object",
  "properties": {
    "text": {
      "type": "string",
      "description": "The text to transform"
    },
    "operation": {
      "type": "string",
      "description": "Operation: \"uppercase\", \"lowercase\", or \"reverse\""
    }
  },
  "required": ["text", "operation"],
  "title": "TextTransformParams"
}
```

## Best Practices

### 1. Clear Descriptions

Always document your parameters and tool purpose:

```rust
/// The user's email address (must be valid format)
email: String,
```

### 2. Validate Input

Validate parameters in your tool logic:

```rust
|params: EmailParams| async move {
    if !params.email.contains('@') {
        return Err("Invalid email format".to_string());
    }
    // ...
}
```

### 3. Structured Output

Return structured JSON for better parsing:

```rust
Ok(json!({
    "status": "success",
    "data": result,
    "timestamp": chrono::Utc::now().to_rfc3339()
}))
```

### 4. Use Enums for Choices

For limited options, consider using enums:

```rust
#[derive(Debug, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
enum Operation {
    Add,
    Subtract,
    Multiply,
    Divide,
}

#[derive(Debug, Deserialize, JsonSchema)]
struct MathParams {
    a: f64,
    b: f64,
    operation: Operation,
}
```

## Comparison: Before vs After

### Before (Manual Implementation)

```rust
#[derive(Debug)]
struct MyTool;

#[derive(Debug, Deserialize)]
struct MyToolParams {
    value: i32,
}

impl Tool for MyTool {
    fn name(&self) -> &'static str {
        "my_tool"
    }

    fn description(&self) -> &'static str {
        "Does something useful"
    }

    fn parameters_schema(&self) -> serde_json::Value {
        json!({
            "type": "object",
            "properties": {
                "value": {
                    "type": "integer",
                    "description": "A value"
                }
            },
            "required": ["value"],
            "additionalProperties": false
        })
    }

    fn execute(
        &self,
        parameters: serde_json::Value,
    ) -> std::pin::Pin<
        Box<
            dyn std::future::Future<Output = Result<serde_json::Value, AgentError>>
                + Send
                + '_,
        >,
    > {
        Box::pin(async move {
            let params: MyToolParams = serde_json::from_value(parameters)
                .map_err(|e| AgentError::ToolExecution(format!("Invalid params: {}", e)))?;
            
            Ok(json!({ "result": params.value * 2 }))
        })
    }
}

// ~70 lines of code
```

### After (With Macro)

```rust
#[derive(Debug, Deserialize, JsonSchema)]
struct MyToolParams {
    /// A value
    value: i32,
}

tiny_agent_rs::tool!(
    name = "my_tool",
    description = "Does something useful",
    params = MyToolParams,
    |params: MyToolParams| async move {
        Ok(json!({ "result": params.value * 2 }))
    }
);

// ~15 lines of code - 80% reduction!
```

## Testing Your Tools

### Unit Tests

Test tools directly by calling execute:

```rust
#[tokio::test]
async fn test_my_tool() {
    let tool = MyTool;
    
    let result = tool.execute(json!({
        "value": 21
    })).await.unwrap();
    
    assert_eq!(result["result"], 42);
}
```

### Schema Validation

Verify schema generation:

```rust
#[test]
fn test_schema() {
    let tool = MyTool;
    let schema = tool.parameters_schema();
    
    assert_eq!(schema["type"], "object");
    assert!(schema["properties"].get("value").is_some());
}
```

## Troubleshooting

### Struct Name Not Found

The macro generates a struct in PascalCase. For `my_tool`, use `MyTool`:

```rust
factory.register_tool(MyTool);  // ✅ Correct
factory.register_tool(my_tool);  // ❌ Wrong
```

### Type Annotations Needed

Always annotate the closure parameter type:

```rust
|params: MyToolParams| async move { ... }  // ✅ Correct
|params| async move { ... }                 // ❌ Wrong - type needed
```

### Schema Not Generated

Ensure `JsonSchema` derive is present:

```rust
#[derive(Debug, Deserialize, JsonSchema)]  // ✅ Correct
#[derive(Debug, Deserialize)]              // ❌ Missing JsonSchema
struct MyParams { ... }
```

## See Also

- [Example: macro_tool_example.rs]../examples/macro_tool_example.rs
- [Tests: tests/macro_test.rs]../tests/macro_test.rs
- [Schema validation: tests/schema_validation_test.rs]../tests/schema_validation_test.rs
- [Macro implementation: tinyagent_macros/src/lib.rs]../tinyagent_macros/src/lib.rs