riglr-macros 0.3.0

Procedural macros for riglr - reducing boilerplate when creating rig-compatible tools.
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
# riglr-macros

Procedural macros for the riglr ecosystem, providing code generation for tool definitions and error handling with automatic dependency injection.

[![Crates.io](https://img.shields.io/crates/v/riglr-macros.svg)](https://crates.io/crates/riglr-macros)
[![Documentation](https://docs.rs/riglr-macros/badge.svg)](https://docs.rs/riglr-macros)

## Features

- **`#[tool]` macro**: Automatically implement `riglr_core::Tool` for functions and structs
- **Type-based dependency injection**: Automatic detection and injection of ApplicationContext
- **Automatic error mapping**: Convert function errors to `ToolError` with proper retry classification
- **AI-friendly descriptions**: Generate tool descriptions from doc comments or attributes
- **Type safety**: Generate strongly-typed parameter structs with validation
- **Serde integration**: Automatic serialization/deserialization for tool parameters and results

## Architecture

riglr-macros provides compile-time code generation for the riglr ecosystem, transforming annotated functions into fully-featured tools with dependency injection and error handling.

### Design Principles

- **Type-Based Detection**: Dependencies are identified by type signature, not attributes
- **Zero-Cost Abstraction**: All macro expansion happens at compile time
- **Clean Generated Code**: Produces readable, debuggable Rust code
- **Automatic Context Injection**: ApplicationContext is detected and injected automatically
- **Error Preservation**: Original error types are preserved for downcasting
- **rig Framework Compatible**: Generated code integrates seamlessly with rig

### Code Generation Pipeline

1. **AST Analysis**: Parse function signature to identify parameters
2. **Type Detection**: Identify ApplicationContext parameters by type pattern
3. **Args Struct Generation**: Create serde-compatible struct for user parameters
4. **Tool Implementation**: Generate Tool trait implementation with context injection
5. **Error Mapping**: Wrap error handling with proper ToolError conversion
6. **Factory Function**: Create convenience function for tool instantiation

### Generated Components

For each `#[tool]` annotated function, the macro generates:
- **Args Struct**: Serde/JsonSchema struct for parameter validation (excludes context)
- **Tool Struct**: Empty struct implementing the Tool trait
- **Tool Implementation**: Complete Tool trait implementation with execute, name, description
- **Factory Function**: `{name}_tool()` function returning `Arc<dyn Tool>`

## The `#[tool]` Macro

The `#[tool]` macro transforms async functions and structs into riglr tools with automatic dependency injection by generating:

- **Args struct** with serde/schemars for parameter validation
- **Tool trait implementation** with proper error mapping to `ToolError`
- **Description extraction** from doc comments or attributes
- **Automatic context injection** based on parameter type signatures

## Type-Based Dependency Injection

The macro uses **type-based detection** to automatically identify and inject dependencies. Functions with `ApplicationContext` parameters are automatically detected and the context is injected at runtime - no attributes required!

### Basic Function Tool with Context

```rust
use riglr_macros::tool;
use riglr_core::{ToolError, provider::ApplicationContext};

/// Checks the SOL balance for a given Solana address
#[tool]
async fn check_sol_balance(
    context: &ApplicationContext,  // Automatically detected and injected
    address: String,
) -> Result<f64, ToolError> {
    // Access blockchain clients through context
    let solana_client = context.solana_client()?;
    
    // Implementation would check actual balance
    let balance = solana_client.get_balance(&address).await?;
    Ok(balance as f64 / 1_000_000_000.0) // Convert lamports to SOL
}
```

The macro generates:

```rust
// Generated parameter struct (only for user parameters)
#[derive(serde::Deserialize, schemars::JsonSchema)]
struct CheckSolBalanceArgs {
    address: String,  // ApplicationContext is excluded from Args struct
}

// Generated Tool implementation with automatic context injection
#[async_trait::async_trait]
impl riglr_core::Tool for CheckSolBalanceTool {
    async fn execute(
        &self, 
        params: serde_json::Value, 
        context: &ApplicationContext  // Context automatically passed here
    ) -> Result<JobResult, ToolError> {
        let args: CheckSolBalanceArgs = serde_json::from_value(params)?;
        // Call original function with injected context + user params
        let result = check_sol_balance(context, args.address).await?;
        Ok(JobResult::Success { 
            value: serde_json::to_value(result)?, 
            tx_hash: None 
        })
    }

    fn name(&self) -> &str {
        "check_sol_balance"
    }

    fn description(&self) -> &str {
        "Checks the SOL balance for a given Solana address"
    }
}
```

## Generated Code Example

This section shows exactly what code the `#[tool]` macro generates for you. Understanding this helps you debug issues and understand the macro's behavior.

### User-Written Code
```rust
use riglr_macros::tool;
use riglr_core::{ToolError, provider::ApplicationContext};

/// Transfer SOL tokens between accounts
#[tool]
async fn transfer_sol(
    context: &ApplicationContext,  // This will be injected
    to_address: String,           // These become the Args struct
    amount: f64,                  
) -> Result<String, ToolError> {
    let client = context.solana_client()?;
    let tx_hash = client.transfer(&to_address, amount).await?;
    Ok(tx_hash)
}
```

### Generated Code (What the Macro Creates)
```rust
// 1. Args struct for user parameters (context excluded)
#[derive(serde::Deserialize, schemars::JsonSchema)]
struct TransferSolArgs {
    to_address: String,
    amount: f64,
}

// 2. Tool struct
struct TransferSolTool;

// 3. Tool trait implementation
#[async_trait::async_trait]
impl riglr_core::Tool for TransferSolTool {
    async fn execute(
        &self,
        params: serde_json::Value,
        context: &ApplicationContext,  // Context passed by framework
    ) -> Result<riglr_core::JobResult, ToolError> {
        // Deserialize user parameters
        let args: TransferSolArgs = serde_json::from_value(params)
            .map_err(|e| ToolError::invalid_input_with_source(
                e, 
                "Failed to parse parameters"
            ))?;
        
        // Call the original function with injected context
        let result = transfer_sol(
            context,           // Injected from execute
            args.to_address,   // From deserialized args
            args.amount,
        ).await?;
        
        // Package the result
        Ok(riglr_core::JobResult::Success {
            value: serde_json::to_value(result)?,
            tx_hash: None,
        })
    }
    
    fn name(&self) -> &str {
        "transfer_sol"
    }
    
    fn description(&self) -> &str {
        "Transfer SOL tokens between accounts"
    }
}

// 4. Factory function to create tool instances
pub fn transfer_sol_tool() -> Arc<dyn riglr_core::Tool> {
    Arc::new(TransferSolTool)
}
```

### Key Points About Generated Code:
1. **Args Struct**: Only includes user parameters, ApplicationContext is excluded
2. **Tool Implementation**: Handles deserialization, context injection, and result packaging
3. **Error Mapping**: Automatically converts errors to ToolError with proper classification
4. **Factory Function**: Creates Arc-wrapped instances for use with ToolWorker

## How Type-Based Detection Works

The macro automatically identifies parameters by their type signature:

1. **ApplicationContext parameters**: Any parameter of type `&ApplicationContext`, `&riglr_core::provider::ApplicationContext`, or ending in `::ApplicationContext` is automatically detected
2. **User parameters**: All other parameters become fields in the generated Args struct
3. **Automatic injection**: The context is injected from the Tool trait's execute method
4. **Clean signatures**: Your tool functions have clean, explicit signatures showing exactly what dependencies they need

### Supported ApplicationContext Types

The macro recognizes these type patterns:
- `context: &ApplicationContext`
- `ctx: &riglr_core::provider::ApplicationContext`  
- `app_context: &my_crate::provider::ApplicationContext`

### Function Tool with Custom Description

You can override the description with an attribute:

```rust
use riglr_core::{ToolError, provider::ApplicationContext};

#[tool(description = "Gets current ETH price in USD from external API")]
async fn get_eth_price(
    context: &ApplicationContext,
) -> Result<f64, ToolError> {
    let web_client = context.web_client()?;
    let price_data = web_client.get_eth_price().await?;
    Ok(price_data.usd)
}
```

### Struct Tool Implementation

For more complex tools, implement them as structs. The macro handles context injection automatically:

```rust
use riglr_core::{Tool, JobResult, ToolError, provider::ApplicationContext};
use riglr_macros::tool;
use serde::{Serialize, Deserialize};

/// A comprehensive wallet management tool
#[derive(Serialize, Deserialize, schemars::JsonSchema, Clone)]
#[tool(description = "Manages wallet operations across multiple chains")]
struct WalletManager {
    operation: String,
    amount: Option<f64>,
    address: Option<String>,
}

impl WalletManager {
    /// Execute the wallet operation with the provided context
    pub async fn execute(&self, context: &ApplicationContext) -> Result<String, ToolError> {
        match self.operation.as_str() {
            "balance" => {
                let address = self.address.as_ref()
                    .ok_or_else(|| ToolError::invalid_input_string("Address required for balance check"))?;
                
                if let Ok(solana_client) = context.solana_client() {
                    let balance = self.check_solana_balance(context, address).await?;
                    Ok(format!("Solana balance: {} SOL", balance))
                } else if let Ok(evm_client) = context.evm_client() {
                    let balance = self.check_evm_balance(context, address).await?;
                    Ok(format!("EVM balance: {} ETH", balance))
                } else {
                    Err(ToolError::permanent_string("No supported blockchain client available"))
                }
            }
            "transfer" => {
                let amount = self.amount
                    .ok_or_else(|| ToolError::invalid_input_string("Amount required for transfer"))?;
                let to_address = self.address.as_ref()
                    .ok_or_else(|| ToolError::invalid_input_string("Destination address required"))?;
                
                let tx_hash = self.transfer_funds(context, to_address, amount).await?;
                Ok(format!("Transferred {} - tx: {}", amount, tx_hash))
            }
            _ => Err(ToolError::invalid_input_string(format!("Unknown operation: {}", self.operation)))
        }
    }
    
    async fn check_solana_balance(&self, context: &ApplicationContext, address: &str) -> Result<f64, ToolError> {
        let client = context.solana_client()?;
        // Implementation for Solana balance check using context's client
        Ok(1.5)
    }
    
    async fn check_evm_balance(&self, context: &ApplicationContext, address: &str) -> Result<f64, ToolError> {
        let client = context.evm_client()?;
        // Implementation for EVM balance check using context's client
        Ok(0.25)
    }
    
    async fn transfer_funds(&self, context: &ApplicationContext, to_address: &str, amount: f64) -> Result<String, ToolError> {
        if let Ok(solana_client) = context.solana_client() {
            // Perform Solana transfer
            Ok("solana_tx_hash_123".to_string())
        } else if let Ok(evm_client) = context.evm_client() {
            // Perform EVM transfer
            Ok("0xevm_tx_hash_456".to_string())
        } else {
            Err(ToolError::permanent_string("No supported blockchain client available"))
        }
    }
}
```

### Advanced Error Handling

The `#[tool]` macro automatically maps function errors to `ToolError` types. You can use the enhanced error handling:

```rust
use riglr_core::{ToolError, provider::ApplicationContext};

#[tool]
async fn transfer_tokens(
    context: &ApplicationContext,
    to_address: String, 
    amount: f64,
    token_mint: String,
) -> Result<String, ToolError> {
    // Input validation
    if amount <= 0.0 {
        return Err(ToolError::invalid_input_string("Amount must be positive"));
    }
    
    // Access blockchain clients through context
    let solana_client = context.solana_client()
        .map_err(|_| ToolError::permanent_string("Solana client not available for token transfers"))?;
    
    // Simulate network error that should be retried
    if let Err(e) = perform_transfer(context, &to_address, amount, &token_mint).await {
        return Err(ToolError::retriable_with_source(e, "Failed to submit transaction"));
    }
    
    // Simulate rate limiting with proper retry delay
    if is_rate_limited(context).await {
        return Err(ToolError::rate_limited_string_with_delay(
            "API rate limit exceeded",
            Some(std::time::Duration::from_secs(60))
        ));
    }
    
    Ok("transaction_hash_123".to_string())
}

async fn perform_transfer(
    context: &ApplicationContext,
    to: &str, 
    amount: f64, 
    token: &str
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let client = context.solana_client()?;
    // Implementation would perform actual transfer using context's client
    Ok(())
}

async fn is_rate_limited(context: &ApplicationContext) -> bool {
    // Check if we're being rate limited via context's rate limiter
    false
}
```

### Working with ApplicationContext

Tools automatically have access to blockchain clients and other services through ApplicationContext:

```rust
use riglr_core::{ToolError, provider::ApplicationContext};

#[tool]
async fn multi_chain_balance(
    context: &ApplicationContext,
    address: String,
) -> Result<serde_json::Value, ToolError> {
    let mut balances = serde_json::Map::new();
    
    // Try to get Solana balance
    if let Ok(solana_client) = context.solana_client() {
        match get_solana_balance(context, &address).await {
            Ok(sol_balance) => {
                balances.insert("solana".to_string(), serde_json::json!({
                    "balance": sol_balance,
                    "currency": "SOL"
                }));
            }
            Err(e) => {
                // Log error but continue to other chains
                eprintln!("Failed to get Solana balance: {}", e);
            }
        }
    }
    
    // Try to get EVM balance
    if let Ok(evm_client) = context.evm_client() {
        match get_ethereum_balance(context, &address).await {
            Ok(eth_balance) => {
                balances.insert("ethereum".to_string(), serde_json::json!({
                    "balance": eth_balance,
                    "currency": "ETH"
                }));
            }
            Err(e) => {
                eprintln!("Failed to get Ethereum balance: {}", e);
            }
        }
    }
    
    if balances.is_empty() {
        return Err(ToolError::permanent_string("No supported blockchain clients available"));
    }
    
    Ok(serde_json::Value::Object(balances))
}

async fn get_solana_balance(context: &ApplicationContext, address: &str) -> Result<f64, ToolError> {
    let client = context.solana_client()?;
    // Implementation using the Solana client from context
    Ok(1.5)
}

async fn get_ethereum_balance(context: &ApplicationContext, address: &str) -> Result<f64, ToolError> {
    let client = context.evm_client()?;
    // Implementation using the EVM client from context
    Ok(0.25)
}
```

## Benefits of Type-Based Dependency Injection

The new architecture provides several advantages over the previous `#[context]` attribute approach:

### 1. **Clean, Explicit Signatures**
- Function signatures clearly show what dependencies are needed
- No hidden dependencies - everything is explicit in the function signature
- Easy to understand for both humans and AI assistants

### 2. **Automatic Detection** 
- No need to remember special attributes like `#[context]`
- The macro automatically detects ApplicationContext by type
- Reduces boilerplate and potential for errors

### 3. **Better IDE Support**
- IDEs can provide better autocomplete and error checking
- Type information is preserved throughout the process
- Easier refactoring when dependency types change

### 4. **rig-core Compatibility**
- Generated tools work seamlessly with the rig framework
- Standard Tool trait implementation with proper context passing
- Easy integration into existing rig-based applications

### 5. **Simplified Migration**
- Old `_with_context` patterns are no longer needed
- Cleaner, more maintainable code
- Consistent pattern across all tools

## Description Extraction

The macro extracts descriptions in priority order:

1. **Attribute `description = "..."`**: Explicit description override
2. **Rust doc comments**: First line of doc comments on the function/struct
3. **Empty string**: Fallback if no description is found

```rust
/// This is the primary description from doc comments
/// Additional documentation here is ignored for the tool description
#[tool]
async fn documented_tool() -> Result<String, ToolError> {
    Ok("result".to_string())
}

#[tool(description = "This explicit description overrides doc comments")]
/// This doc comment will be ignored for tool description
async fn explicit_description_tool() -> Result<String, ToolError> {
    Ok("result".to_string())
}
```

## Integration with riglr-core

Tools generated by the macro integrate seamlessly with `riglr-core` and the rig framework:

```rust
use riglr_core::{ToolWorker, ExecutionConfig, Job, provider::ApplicationContext};
use riglr_macros::tool;
use std::sync::Arc;

#[tool]
async fn example_tool(
    context: &ApplicationContext,
    param: String,
) -> Result<String, riglr_core::ToolError> {
    // Use context to access services
    let web_client = context.web_client()?;
    Ok(format!("Processed: {}", param))
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create application context with necessary clients
    let context = ApplicationContext::builder()
        .with_web_client()
        .build()?;
    
    // Create worker with context
    let worker = ToolWorker::new(ExecutionConfig::default(), context);
    
    // Register the generated tool using the factory function
    worker.register_tool(example_tool_tool()).await;
    
    // Create and process a job - context is automatically injected
    let job = Job::new(
        "example_tool",
        &serde_json::json!({"param": "test data"}),
        3
    )?;
    
    let result = worker.process_job(job).await?;
    println!("Result: {:?}", result);
    
    Ok(())
}
```

## Parameter Validation

The generated parameter structs support full serde validation. Parameters are automatically validated when the tool is executed:

```rust
use serde::{Deserialize, Serialize};
use riglr_core::{ToolError, provider::ApplicationContext};
use riglr_macros::tool;

#[tool]
async fn transfer_with_validation(
    context: &ApplicationContext,
    #[serde(alias = "to")]
    recipient_address: String,
    
    #[serde(deserialize_with = "validate_positive_amount")]
    amount: f64,
    
    #[serde(default = "default_slippage")]
    slippage_bps: u16,
) -> Result<String, ToolError> {
    // Parameters are already validated by serde before this function is called
    let solana_client = context.solana_client()?;
    
    // Perform transfer with validated parameters
    let tx_hash = solana_client.transfer(
        &recipient_address, 
        amount, 
        slippage_bps
    ).await?;
    
    Ok(format!(
        "Transferred {} to {} with {}bps slippage - tx: {}", 
        amount, 
        recipient_address,
        slippage_bps,
        tx_hash
    ))
}

fn validate_positive_amount<'de, D>(deserializer: D) -> Result<f64, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let amount = f64::deserialize(deserializer)?;
    if amount <= 0.0 {
        return Err(serde::de::Error::custom("Amount must be positive"));
    }
    Ok(amount)
}

fn default_slippage() -> u16 {
    100 // 1% default slippage
}
```

## Best Practices

### 1. Function Design
- Always include `ApplicationContext` as the first parameter for tools that need external services
- Use descriptive parameter names that clearly indicate their purpose
- Provide comprehensive doc comments for each parameter
- Use appropriate default values with `#[serde(default)]` where applicable

### 2. Error Handling

**REQUIRED**: All tool functions must return error types that implement `Into<ToolError>`. The `#[tool]` macro no longer provides automatic conversion for standard library error types like `std::io::Error` or `reqwest::Error`. You must define custom error enums using the `#[derive(IntoToolError)]` macro or manually implement `From<YourError> for ToolError`.

Use the structured error types for better retry logic:

```rust
use riglr_core::{ToolError, provider::ApplicationContext};

#[tool]
async fn robust_tool(
    context: &ApplicationContext,
    param: String,
) -> Result<String, ToolError> {
    // Validate input
    if param.is_empty() {
        return Err(ToolError::invalid_input_string("Parameter cannot be empty"));
    }
    
    // Handle permanent errors (don't retry)
    if !has_required_permissions(context).await {
        return Err(ToolError::permanent_string("Insufficient permissions"));
    }
    
    // Handle retriable errors (retry with backoff)
    match make_network_call(context, &param).await {
        Ok(result) => Ok(result),
        Err(e) if is_network_error(&e) => {
            Err(ToolError::retriable_with_source(e, "Network call failed"))
        }
        Err(e) if is_rate_limited(&e) => {
            Err(ToolError::rate_limited_string_with_delay(
                "API rate limited",
                Some(std::time::Duration::from_secs(30))
            ))
        }
        Err(e) => Err(ToolError::permanent_with_source(e, "Unexpected error"))
    }
}

async fn has_required_permissions(context: &ApplicationContext) -> bool { 
    // Check permissions using context services
    true 
}

async fn make_network_call(
    context: &ApplicationContext, 
    param: &str
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> { 
    let client = context.web_client()?;
    // Make network call using context's HTTP client
    Ok(param.to_string()) 
}

fn is_network_error(_e: &dyn std::error::Error) -> bool { false }
fn is_rate_limited(_e: &dyn std::error::Error) -> bool { false }
```

#### Best Practice: Custom Error Types with IntoToolError

For production applications, define custom error types with the `#[derive(IntoToolError)]` macro when appropriate:

```rust
use riglr_core::ToolError;
use riglr_macros::{tool, IntoToolError};
use thiserror::Error;

#[derive(Debug, Error, IntoToolError)]
enum MyToolError {
    #[error("Invalid input: {reason}")]
    #[permanent]  // Won't be retried
    InvalidInput { reason: String },
    
    #[error("Network timeout after {attempts} attempts")]
    #[retriable]  // Will be retried with exponential backoff
    NetworkTimeout { attempts: u32 },
    
    #[error("API rate limit exceeded")]
    #[rate_limited(retry_after = 60)]  // Retry after 60 seconds
    RateLimited,
    
    #[error("Blockchain error: {0}")]
    #[retriable]
    BlockchainError(String),
}

#[tool]
async fn production_ready_tool(
    input: String,
    retries: u32,
) -> Result<String, MyToolError> {
    // Validation returns permanent errors
    if input.is_empty() {
        return Err(MyToolError::InvalidInput {
            reason: "Input cannot be empty".to_string(),
        });
    }
    
    // Network operations return retriable errors
    for attempt in 1..=retries {
        match perform_operation(&input).await {
            Ok(result) => return Ok(result),
            Err(_) if attempt == retries => {
                return Err(MyToolError::NetworkTimeout { attempts: retries });
            }
            Err(_) => continue,
        }
    }
    
    Err(MyToolError::NetworkTimeout { attempts: retries })
}
```

This approach provides:
- **Type safety**: Compile-time checking of all error paths
- **Clear semantics**: Each error variant explicitly declares its retry behavior
- **Maintainability**: All error handling logic centralized in the error enum
- **Production readiness**: Fine-grained control over retry strategies

#### When to Use Manual Implementation

Some error types require more complex logic than the `IntoToolError` macro can provide. For example, `SolanaToolError` in `riglr-solana-tools` uses a manual `From<SolanaToolError> for ToolError` implementation because it needs:

1. **Dynamic rate-limit detection**: Checking message content at runtime to determine if an error is rate-limited
2. **Source error preservation**: Keeping the original error for downcasting capabilities
3. **Complex classification logic**: Different behavior based on inner error types
4. **Passthrough handling**: Special handling for wrapped `ToolError` variants

Example of when manual implementation is needed:

```rust
// SolanaToolError requires manual implementation due to complex requirements
impl From<SolanaToolError> for ToolError {
    fn from(err: SolanaToolError) -> Self {
        // Passthrough ToolError without re-wrapping
        if let SolanaToolError::ToolError(tool_err) = err {
            return tool_err;
        }

        // Dynamic rate-limit detection based on message content
        if err.is_rate_limited() {
            return ToolError::rate_limited_with_source(err, "Solana operation", err.retry_delay());
        }

        // Complex retriability logic based on error type
        if err.is_retriable() {
            return ToolError::retriable_with_source(err, "Solana operation");
        }

        // Preserve source for downcasting
        ToolError::permanent_with_source(err, "Solana operation")
    }
}
```

Use the `IntoToolError` macro for simpler error enums with static classification. Use manual implementation when you need runtime logic or special handling.

### 3. ApplicationContext Usage

Always check client availability and handle graceful fallbacks:

```rust
#[tool]
async fn chain_specific_tool(
    context: &ApplicationContext,
    operation: String,
) -> Result<String, ToolError> {
    match operation.as_str() {
        "solana_op" => {
            let solana_client = context.solana_client()
                .map_err(|_| ToolError::permanent_string("Solana client not available"))?;
            
            // Perform Solana operation
            let result = solana_client.get_latest_blockhash().await?;
            Ok(format!("Solana operation completed: {}", result))
        }
        "evm_op" => {
            let evm_client = context.evm_client()
                .map_err(|_| ToolError::permanent_string("EVM client not available"))?;
            
            // Perform EVM operation  
            let block_number = evm_client.get_block_number().await?;
            Ok(format!("EVM operation completed at block: {}", block_number))
        }
        "web_op" => {
            let web_client = context.web_client()
                .map_err(|_| ToolError::permanent_string("Web client not available"))?;
                
            // Perform web operation
            let response = web_client.get("https://api.example.com").await?;
            Ok(format!("Web operation completed: {}", response.status()))
        }
        _ => Err(ToolError::invalid_input_string("Unknown operation"))
    }
}
```

### 4. Context-Aware Design

Design tools that gracefully adapt to available services:

```rust
#[tool]
async fn adaptive_balance_check(
    context: &ApplicationContext,
    address: String,
) -> Result<serde_json::Value, ToolError> {
    let mut results = serde_json::Map::new();
    
    // Try each available blockchain client
    if let Ok(solana_client) = context.solana_client() {
        match solana_client.get_balance(&address).await {
            Ok(balance) => {
                results.insert("solana".to_string(), serde_json::json!({
                    "balance": balance,
                    "status": "success"
                }));
            }
            Err(e) => {
                results.insert("solana".to_string(), serde_json::json!({
                    "error": e.to_string(),
                    "status": "error"
                }));
            }
        }
    }
    
    if let Ok(evm_client) = context.evm_client() {
        match evm_client.get_balance(&address).await {
            Ok(balance) => {
                results.insert("ethereum".to_string(), serde_json::json!({
                    "balance": balance.to_string(),
                    "status": "success"
                }));
            }
            Err(e) => {
                results.insert("ethereum".to_string(), serde_json::json!({
                    "error": e.to_string(),
                    "status": "error"
                }));
            }
        }
    }
    
    if results.is_empty() {
        return Err(ToolError::permanent_string("No blockchain clients available"));
    }
    
    Ok(serde_json::Value::Object(results))
}
```

## Migration from Previous Versions

If you're upgrading from a previous version that used `#[context]` attributes or `SignerContext`, here's how to migrate:

### Before (Old Architecture)
```rust
#[tool]
async fn old_transfer(
    #[context] _ctx: &SignerContext,  // ❌ Old way
    to_address: String,
    amount: f64,
) -> Result<String, ToolError> {
    let signer = SignerContext::current().await?;  // ❌ Old way
    // ... implementation
}
```

### After (New Architecture)  
```rust
#[tool]
async fn new_transfer(
    context: &ApplicationContext,  // ✅ New way - automatically detected
    to_address: String,
    amount: f64,
) -> Result<String, ToolError> {
    let client = context.solana_client()?;  // ✅ New way - use context directly
    // ... implementation
}
```

### Key Changes
1. Remove `#[context]` attributes - they're no longer needed
2. Replace `SignerContext::current().await?` with direct context usage  
3. Add `ApplicationContext` parameter to function signatures
4. Access clients through `context.solana_client()`, `context.evm_client()`, etc.

## Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
riglr-macros = "0.3.0"
riglr-core = "0.3.0"
tokio = { version = "1.0", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
async-trait = "0.1"
```

## Quick Start

```rust
use riglr_macros::tool;
use riglr_core::{ToolError, provider::ApplicationContext};

#[tool]
async fn hello_world(
    context: &ApplicationContext,
    name: String,
) -> Result<String, ToolError> {
    Ok(format!("Hello, {}!", name))
}

fn main() {
    // Your tool is ready to use!
    let tool = hello_world_tool();
    println!("Created tool: {}", tool.name());
}
```

## Examples

See the `examples/` directory in the riglr-core crate for complete working examples using the `#[tool]` macro with the new ApplicationContext architecture.

## Requirements

- **Rust 1.70+**: For async trait support and modern language features
- **Function Requirements**: Tools must be async functions returning `Result<T, E>` where `E: Into<ToolError>`. Standard library errors like `std::io::Error` do not implement this automatically - you must wrap them in custom error types.
- **Context Requirements**: Exactly one `ApplicationContext` parameter is required for dependency injection
- **Parameter Requirements**: All user parameters must implement `Serialize + Deserialize + JsonSchema`

## Testing

```bash
cargo test --workspace
```

## License

MIT License - see LICENSE file for details