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
use serde::{Deserialize, Serialize};
use crate::types::CacheControlEphemeral;
/// Common parameters for a custom tool.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ToolParam {
/// [JSON schema](https://json-schema.org/draft/2020-12) for this tool's input.
///
/// This defines the shape of the `input` that your tool accepts and that the model
/// will produce.
pub input_schema: serde_json::Value,
/// Name of the tool.
///
/// This is how the tool will be called by the model and in `tool_use` blocks.
pub name: String,
/// Create a cache control breakpoint at this content block.
#[serde(skip_serializing_if = "Option::is_none")]
pub cache_control: Option<CacheControlEphemeral>,
/// Description of what this tool does.
///
/// Tool descriptions should be as detailed as possible. The more information that
/// the model has about what the tool is and how to use it, the better it will
/// perform. You can use natural language descriptions to reinforce important
/// aspects of the tool input JSON schema.
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// Enable strict mode for tool parameter validation.
///
/// When enabled, Claude will guarantee that tool parameters exactly match the
/// `input_schema`. This ensures type-safe function calls with correctly-typed
/// arguments.
///
/// This feature requires the beta header `structured-outputs-2025-11-13`.
///
/// See
/// [structured outputs](https://docs.anthropic.com/en/docs/build-with-claude/structured-outputs)
/// for details.
#[serde(skip_serializing_if = "Option::is_none")]
pub strict: Option<bool>,
}
impl ToolParam {
/// Create a new `ToolParam` with the required fields.
pub fn new(name: String, input_schema: serde_json::Value) -> Self {
Self { name, input_schema, cache_control: None, description: None, strict: None }
}
/// Add a description to the tool.
pub fn with_description(mut self, description: String) -> Self {
self.description = Some(description);
self
}
/// Add cache control to the tool.
pub fn with_cache_control(mut self, cache_control: CacheControlEphemeral) -> Self {
self.cache_control = Some(cache_control);
self
}
/// Enable strict mode for tool parameter validation.
///
/// When enabled, Claude will guarantee that tool parameters exactly match the
/// `input_schema`. This ensures type-safe function calls with correctly-typed
/// arguments.
///
/// This feature requires the beta header `structured-outputs-2025-11-13`.
///
/// # Example
///
/// ```
/// use serde_json::json;
/// use adk_anthropic::ToolParam;
///
/// let tool = ToolParam::new(
/// "get_weather".to_string(),
/// json!({
/// "type": "object",
/// "properties": {
/// "location": { "type": "string" },
/// "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] }
/// },
/// "required": ["location"],
/// "additionalProperties": false
/// })
/// )
/// .with_strict(true);
/// ```
pub fn with_strict(mut self, strict: bool) -> Self {
self.strict = Some(strict);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::{json, to_value};
#[test]
fn tool_param_complete() {
let input_schema = json!({
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query"
}
}
});
let cache_control = CacheControlEphemeral::new();
let tool = ToolParam::new("search".to_string(), input_schema)
.with_description("Search for information".to_string())
.with_cache_control(cache_control);
let json = to_value(&tool).unwrap();
assert_eq!(
json,
json!({
"input_schema": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query"
}
}
},
"name": "search",
"cache_control": {
"type": "ephemeral"
},
"description": "Search for information"
})
);
}
#[test]
fn tool_param_with_strict_mode() {
let input_schema = json!({
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"],
"additionalProperties": false
});
let tool = ToolParam::new("get_weather".to_string(), input_schema)
.with_description("Get the current weather".to_string())
.with_strict(true);
let json = to_value(&tool).unwrap();
assert_eq!(
json,
json!({
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"],
"additionalProperties": false
},
"name": "get_weather",
"description": "Get the current weather",
"strict": true
})
);
// Verify strict is Some(true)
assert_eq!(tool.strict, Some(true));
}
#[test]
fn tool_param_strict_mode_false_not_serialized() {
// When strict is explicitly set to false, it should be serialized
let input_schema = json!({
"type": "object",
"properties": {
"query": { "type": "string" }
}
});
let tool = ToolParam::new("search".to_string(), input_schema).with_strict(false);
let json = to_value(&tool).unwrap();
// strict: false should be serialized
assert_eq!(json["strict"], json!(false));
}
#[test]
fn tool_param_strict_mode_none_not_serialized() {
// When strict is None (default), it should not be serialized
let input_schema = json!({
"type": "object",
"properties": {
"query": { "type": "string" }
}
});
let tool = ToolParam::new("search".to_string(), input_schema);
let json = to_value(&tool).unwrap();
// strict should not be present when None
assert!(json.get("strict").is_none(), "strict should not be serialized when None");
}
}