anthropic-tools 1.0.1

A Rust library for interacting with the Anthropic API
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
//! Tool definitions for function calling with the Anthropic API.
//!
//! This module provides types for defining tools that Claude can use:
//!
//! - [`Tool`] - Main tool definition with name, description, and input schema
//! - [`JsonSchema`] - JSON Schema for tool input parameters
//! - [`PropertyDef`] - Property definitions within a schema
//! - [`CacheControl`] - Cache control for prompt caching
//!
//! # Example
//!
//! ```rust
//! use anthropic_tools::common::tool::{Tool, PropertyDef};
//!
//! // Create a weather tool
//! let mut tool = Tool::new("get_weather");
//! tool.description("Get the current weather for a location")
//!     .add_string_property("location", Some("City and state, e.g., San Francisco, CA"), true)
//!     .add_enum_property("unit", Some("Temperature unit"), vec!["celsius", "fahrenheit"], false);
//!
//! // Convert to JSON for API request
//! let json = tool.to_value();
//! ```
//!
//! # With Prompt Caching
//!
//! ```rust
//! use anthropic_tools::common::tool::Tool;
//!
//! let mut tool = Tool::new("expensive_tool");
//! tool.description("A tool with many parameters")
//!     .with_cache();  // Enable prompt caching
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Tool definition for the Anthropic API
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Tool {
    /// Name of the tool
    pub name: String,

    /// Description of what the tool does
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// JSON schema for the tool's input parameters
    pub input_schema: JsonSchema,

    /// Cache control for prompt caching
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cache_control: Option<CacheControl>,
}

/// Cache control for prompt caching
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CacheControl {
    #[serde(rename = "type")]
    pub type_name: String,
}

impl CacheControl {
    pub fn ephemeral() -> Self {
        CacheControl {
            type_name: "ephemeral".to_string(),
        }
    }
}

/// JSON Schema for tool input
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct JsonSchema {
    #[serde(rename = "type")]
    pub type_name: String,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub properties: Option<HashMap<String, PropertyDef>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub required: Option<Vec<String>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub additional_properties: Option<bool>,
}

/// Property definition in JSON schema
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PropertyDef {
    #[serde(rename = "type")]
    pub type_name: String,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    #[serde(rename = "enum", skip_serializing_if = "Option::is_none")]
    pub enum_values: Option<Vec<String>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub items: Option<Box<PropertyDef>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub properties: Option<HashMap<String, PropertyDef>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub required: Option<Vec<String>>,

    #[serde(rename = "default", skip_serializing_if = "Option::is_none")]
    pub default_value: Option<serde_json::Value>,
}

impl Tool {
    /// Create a new tool with name only
    pub fn new<S: AsRef<str>>(name: S) -> Self {
        Tool {
            name: name.as_ref().to_string(),
            description: None,
            input_schema: JsonSchema::object(),
            cache_control: None,
        }
    }

    /// Set the tool description
    pub fn description<S: AsRef<str>>(&mut self, desc: S) -> &mut Self {
        self.description = Some(desc.as_ref().to_string());
        self
    }

    /// Add a string property to the input schema
    pub fn add_string_property<S: AsRef<str>>(
        &mut self,
        name: S,
        description: Option<S>,
        required: bool,
    ) -> &mut Self {
        self.add_property(
            name.as_ref(),
            PropertyDef::string(description.map(|s| s.as_ref().to_string())),
            required,
        )
    }

    /// Add a number property to the input schema
    pub fn add_number_property<S: AsRef<str>>(
        &mut self,
        name: S,
        description: Option<S>,
        required: bool,
    ) -> &mut Self {
        self.add_property(
            name.as_ref(),
            PropertyDef::number(description.map(|s| s.as_ref().to_string())),
            required,
        )
    }

    /// Add a boolean property to the input schema
    pub fn add_boolean_property<S: AsRef<str>>(
        &mut self,
        name: S,
        description: Option<S>,
        required: bool,
    ) -> &mut Self {
        self.add_property(
            name.as_ref(),
            PropertyDef::boolean(description.map(|s| s.as_ref().to_string())),
            required,
        )
    }

    /// Add an enum property to the input schema
    pub fn add_enum_property<S: AsRef<str>>(
        &mut self,
        name: S,
        description: Option<S>,
        values: Vec<S>,
        required: bool,
    ) -> &mut Self {
        self.add_property(
            name.as_ref(),
            PropertyDef::enum_type(
                description.map(|s| s.as_ref().to_string()),
                values.into_iter().map(|s| s.as_ref().to_string()).collect(),
            ),
            required,
        )
    }

    /// Add an array property to the input schema
    pub fn add_array_property<S: AsRef<str>>(
        &mut self,
        name: S,
        description: Option<S>,
        items: PropertyDef,
        required: bool,
    ) -> &mut Self {
        self.add_property(
            name.as_ref(),
            PropertyDef::array(description.map(|s| s.as_ref().to_string()), items),
            required,
        )
    }

    /// Add a property with custom PropertyDef
    fn add_property(&mut self, name: &str, prop: PropertyDef, required: bool) -> &mut Self {
        if self.input_schema.properties.is_none() {
            self.input_schema.properties = Some(HashMap::new());
        }

        if let Some(props) = &mut self.input_schema.properties {
            props.insert(name.to_string(), prop);
        }

        if required {
            if self.input_schema.required.is_none() {
                self.input_schema.required = Some(Vec::new());
            }
            if let Some(req) = &mut self.input_schema.required {
                req.push(name.to_string());
            }
        }

        self
    }

    /// Enable cache control for this tool
    pub fn with_cache(&mut self) -> &mut Self {
        self.cache_control = Some(CacheControl::ephemeral());
        self
    }

    /// Build the tool and return ownership
    pub fn build(self) -> Self {
        self
    }

    /// Convert to serde_json::Value
    pub fn to_value(&self) -> serde_json::Value {
        serde_json::to_value(self).unwrap()
    }
}

impl JsonSchema {
    /// Create an object schema
    pub fn object() -> Self {
        JsonSchema {
            type_name: "object".to_string(),
            properties: Some(HashMap::new()),
            required: None,
            additional_properties: None,
        }
    }

    /// Create an empty object schema (no properties)
    pub fn empty_object() -> Self {
        JsonSchema {
            type_name: "object".to_string(),
            properties: None,
            required: None,
            additional_properties: None,
        }
    }
}

impl PropertyDef {
    /// Create a string property
    pub fn string(description: Option<String>) -> Self {
        PropertyDef {
            type_name: "string".to_string(),
            description,
            enum_values: None,
            items: None,
            properties: None,
            required: None,
            default_value: None,
        }
    }

    /// Create a number property
    pub fn number(description: Option<String>) -> Self {
        PropertyDef {
            type_name: "number".to_string(),
            description,
            enum_values: None,
            items: None,
            properties: None,
            required: None,
            default_value: None,
        }
    }

    /// Create an integer property
    pub fn integer(description: Option<String>) -> Self {
        PropertyDef {
            type_name: "integer".to_string(),
            description,
            enum_values: None,
            items: None,
            properties: None,
            required: None,
            default_value: None,
        }
    }

    /// Create a boolean property
    pub fn boolean(description: Option<String>) -> Self {
        PropertyDef {
            type_name: "boolean".to_string(),
            description,
            enum_values: None,
            items: None,
            properties: None,
            required: None,
            default_value: None,
        }
    }

    /// Create an enum (string with allowed values) property
    pub fn enum_type(description: Option<String>, values: Vec<String>) -> Self {
        PropertyDef {
            type_name: "string".to_string(),
            description,
            enum_values: Some(values),
            items: None,
            properties: None,
            required: None,
            default_value: None,
        }
    }

    /// Create an array property
    pub fn array(description: Option<String>, items: PropertyDef) -> Self {
        PropertyDef {
            type_name: "array".to_string(),
            description,
            enum_values: None,
            items: Some(Box::new(items)),
            properties: None,
            required: None,
            default_value: None,
        }
    }

    /// Create an object property
    pub fn object(description: Option<String>, properties: HashMap<String, PropertyDef>) -> Self {
        PropertyDef {
            type_name: "object".to_string(),
            description,
            enum_values: None,
            items: None,
            properties: Some(properties),
            required: None,
            default_value: None,
        }
    }

    /// Set a default value
    pub fn with_default(&mut self, value: serde_json::Value) -> &mut Self {
        self.default_value = Some(value);
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tool_builder() {
        let mut tool = Tool::new("search");
        tool.description("Search the web for information")
            .add_string_property("query", Some("The search query"), true)
            .add_number_property("limit", Some("Maximum results to return"), false);

        assert_eq!(tool.name, "search");
        assert!(tool.description.is_some());
        assert!(tool.input_schema.properties.is_some());

        let props = tool.input_schema.properties.as_ref().unwrap();
        assert!(props.contains_key("query"));
        assert!(props.contains_key("limit"));

        let required = tool.input_schema.required.as_ref().unwrap();
        assert!(required.contains(&"query".to_string()));
        assert!(!required.contains(&"limit".to_string()));
    }

    #[test]
    fn test_tool_serialize() {
        let mut tool = Tool::new("get_weather");
        tool.description("Get the current weather in a given location")
            .add_string_property(
                "location",
                Some("The city and state, e.g. San Francisco, CA"),
                true,
            )
            .add_enum_property(
                "unit",
                Some("Temperature unit"),
                vec!["celsius", "fahrenheit"],
                false,
            );

        let json = serde_json::to_string_pretty(&tool).unwrap();
        assert!(json.contains("\"name\": \"get_weather\""));
        assert!(json.contains("\"type\": \"object\""));
        assert!(json.contains("\"location\""));
        assert!(json.contains("\"required\""));
    }

    #[test]
    fn test_property_def_string() {
        let prop = PropertyDef::string(Some("A test property".to_string()));
        assert_eq!(prop.type_name, "string");
        assert_eq!(prop.description, Some("A test property".to_string()));
    }

    #[test]
    fn test_property_def_enum() {
        let prop = PropertyDef::enum_type(
            Some("Choose a color".to_string()),
            vec!["red".to_string(), "green".to_string(), "blue".to_string()],
        );
        assert_eq!(prop.type_name, "string");
        assert!(prop.enum_values.is_some());
        assert_eq!(prop.enum_values.unwrap().len(), 3);
    }

    #[test]
    fn test_property_def_array() {
        let items = PropertyDef::string(Some("Item in array".to_string()));
        let prop = PropertyDef::array(Some("An array of strings".to_string()), items);
        assert_eq!(prop.type_name, "array");
        assert!(prop.items.is_some());
    }

    #[test]
    fn test_tool_with_cache() {
        let mut tool = Tool::new("cached_tool");
        tool.with_cache();
        assert!(tool.cache_control.is_some());
    }

    #[test]
    fn test_tool_to_value() {
        let mut tool = Tool::new("test");
        tool.description("A test tool");
        let value = tool.to_value();
        assert!(value.is_object());
        assert_eq!(value["name"], "test");
    }
}