1use crate::core::Prompt;
7use llm::{LlmModel, ProviderConnectionOverrides, ReasoningEffort, ToolDefinition};
8use mcp_utils::client::McpConfig;
9use std::path::PathBuf;
10
11#[derive(Debug, Clone)]
12pub enum McpConfigSource {
13 File { path: PathBuf, proxy: bool },
14 Json(String),
15 Inline(McpConfig),
16}
17
18impl McpConfigSource {
19 pub fn file(path: PathBuf, proxy: bool) -> Self {
20 Self::File { path, proxy }
21 }
22
23 pub fn direct(path: PathBuf) -> Self {
24 Self::file(path, false)
25 }
26
27 pub fn proxied(path: PathBuf) -> Self {
28 Self::file(path, true)
29 }
30}
31
32#[derive(Debug, Clone)]
37pub struct AgentSpec {
38 pub name: String,
40 pub description: String,
42 pub model: String,
48 pub reasoning_effort: Option<ReasoningEffort>,
50 pub context_window: Option<u32>,
52 pub prompts: Vec<Prompt>,
54 pub provider_connections: ProviderConnectionOverrides,
56 pub mcp_config_sources: Vec<McpConfigSource>,
61 pub exposure: AgentSpecExposure,
63 pub tools: ToolFilter,
65}
66
67impl AgentSpec {
68 pub fn default_spec(model: &LlmModel, reasoning_effort: Option<ReasoningEffort>, prompts: Vec<Prompt>) -> Self {
70 Self {
71 name: "__default__".to_string(),
72 description: "Default agent".to_string(),
73 model: model.to_string(),
74 reasoning_effort,
75 context_window: None,
76 prompts,
77 provider_connections: ProviderConnectionOverrides::default(),
78 mcp_config_sources: Vec::new(),
79 exposure: AgentSpecExposure::none(),
80 tools: ToolFilter::default(),
81 }
82 }
83}
84
85#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
86#[serde(untagged)]
87pub enum ToolMatcher {
88 Name(String),
89 Annotations(ToolAnnotationMatcher),
90}
91
92impl ToolMatcher {
93 pub fn name(pattern: impl Into<String>) -> Self {
94 Self::Name(pattern.into())
95 }
96
97 pub fn read_only() -> Self {
98 Self::Annotations(ToolAnnotationMatcher { read_only: Some(true), ..ToolAnnotationMatcher::default() })
99 }
100
101 pub fn annotations(matcher: ToolAnnotationMatcher) -> Self {
102 Self::Annotations(matcher)
103 }
104
105 pub fn matches(&self, tool: &ToolDefinition) -> bool {
106 match self {
107 Self::Name(pattern) => matches_pattern(pattern, &tool.name),
108 Self::Annotations(matcher) => matcher.matches(tool),
109 }
110 }
111}
112
113#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
114#[serde(rename_all = "camelCase", deny_unknown_fields)]
115pub struct ToolAnnotationMatcher {
116 #[serde(default, skip_serializing_if = "Option::is_none")]
117 pub read_only: Option<bool>,
118 #[serde(default, skip_serializing_if = "Option::is_none")]
119 pub destructive: Option<bool>,
120 #[serde(default, skip_serializing_if = "Option::is_none")]
121 pub idempotent: Option<bool>,
122 #[serde(default, skip_serializing_if = "Option::is_none")]
123 pub open_world: Option<bool>,
124}
125
126impl ToolAnnotationMatcher {
127 pub fn matches(&self, tool: &ToolDefinition) -> bool {
128 let Some(annotations) = tool.annotations.as_ref() else {
129 return false;
130 };
131 let pairs = [
132 (self.read_only, annotations.read_only_hint),
133 (self.destructive, annotations.destructive_hint),
134 (self.idempotent, annotations.idempotent_hint),
135 (self.open_world, annotations.open_world_hint),
136 ];
137 if pairs.iter().all(|(field, _)| field.is_none()) {
138 return false;
139 }
140 pairs.iter().all(|(field, hint)| field.is_none_or(|value| *hint == Some(value)))
141 }
142}
143
144#[doc = ""]
150#[doc = include_str!("docs/tool_filter.md")]
151#[derive(Debug, Clone, Default, PartialEq, Eq, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
152pub struct ToolFilter {
153 #[serde(default, skip_serializing_if = "Vec::is_empty")]
155 pub allow: Vec<ToolMatcher>,
156 #[serde(default, skip_serializing_if = "Vec::is_empty")]
158 pub deny: Vec<ToolMatcher>,
159}
160
161impl ToolFilter {
162 pub fn is_empty(&self) -> bool {
163 self.allow.is_empty() && self.deny.is_empty()
164 }
165
166 pub fn apply(&self, tools: Vec<ToolDefinition>) -> Vec<ToolDefinition> {
168 tools.into_iter().filter(|tool| self.is_tool_allowed(tool)).collect()
169 }
170
171 pub fn is_tool_allowed(&self, tool: &ToolDefinition) -> bool {
172 let allowed = self.allow.is_empty() || self.allow.iter().any(|matcher| matcher.matches(tool));
173 let denied = self.deny.iter().any(|matcher| matcher.matches(tool));
174 allowed && !denied
175 }
176}
177
178fn matches_pattern(pattern: &str, name: &str) -> bool {
180 if let Some(prefix) = pattern.strip_suffix('*') { name.starts_with(prefix) } else { pattern == name }
181}
182
183#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
185pub struct AgentSpecExposure {
186 pub user_invocable: bool,
188 pub agent_invocable: bool,
190}
191
192impl AgentSpecExposure {
193 pub fn none() -> Self {
199 Self { user_invocable: false, agent_invocable: false }
200 }
201
202 pub fn user_only() -> Self {
204 Self { user_invocable: true, agent_invocable: false }
205 }
206
207 pub fn agent_only() -> Self {
209 Self { user_invocable: false, agent_invocable: true }
210 }
211
212 pub fn both() -> Self {
214 Self { user_invocable: true, agent_invocable: true }
215 }
216}
217
218#[cfg(test)]
219mod tests {
220 use super::*;
221 use llm::ToolAnnotations;
222
223 #[test]
224 fn default_spec_has_expected_fields() {
225 let model: LlmModel = "anthropic:claude-sonnet-4-5".parse().unwrap();
226 let prompts = vec![Prompt::file(PathBuf::from("/tmp/BASE.md"), PathBuf::from("/tmp"))];
227 let spec = AgentSpec::default_spec(&model, None, prompts.clone());
228
229 assert_eq!(spec.name, "__default__");
230 assert_eq!(spec.description, "Default agent");
231 assert_eq!(spec.model, model.to_string());
232 assert!(spec.reasoning_effort.is_none());
233 assert_eq!(spec.prompts.len(), 1);
234 assert!(spec.mcp_config_sources.is_empty());
235 assert_eq!(spec.exposure, AgentSpecExposure::none());
236 }
237
238 fn make_tool(name: &str) -> ToolDefinition {
239 ToolDefinition::new(name, "", "")
240 }
241
242 fn make_annotated_tool(name: &str, annotations: ToolAnnotations) -> ToolDefinition {
243 ToolDefinition::new(name, "", "").with_annotations(annotations)
244 }
245
246 #[test]
247 fn empty_filter_allows_all_tools() {
248 let filter = ToolFilter::default();
249 let tools = vec![make_tool("bash"), make_tool("read_file")];
250 let result = filter.apply(tools);
251 assert_eq!(result.len(), 2);
252 }
253
254 #[test]
255 fn allow_keeps_only_matching_tools() {
256 let filter =
257 ToolFilter { allow: vec![ToolMatcher::name("read_file"), ToolMatcher::name("grep")], deny: vec![] };
258 let tools = vec![make_tool("bash"), make_tool("read_file"), make_tool("grep")];
259 let result = filter.apply(tools);
260 let names: Vec<_> = result.iter().map(|t| t.name.as_str()).collect();
261 assert_eq!(names, vec!["read_file", "grep"]);
262 }
263
264 #[test]
265 fn deny_removes_matching_tools() {
266 let filter = ToolFilter { allow: vec![], deny: vec![ToolMatcher::name("bash")] };
267 let tools = vec![make_tool("bash"), make_tool("read_file")];
268 let result = filter.apply(tools);
269 let names: Vec<_> = result.iter().map(|t| t.name.as_str()).collect();
270 assert_eq!(names, vec!["read_file"]);
271 }
272
273 #[test]
274 fn wildcard_matching() {
275 let filter = ToolFilter { allow: vec![ToolMatcher::name("coding__*")], deny: vec![] };
276 let tools = vec![make_tool("coding__grep"), make_tool("coding__read_file"), make_tool("plugins__bash")];
277 let result = filter.apply(tools);
278 let names: Vec<_> = result.iter().map(|t| t.name.as_str()).collect();
279 assert_eq!(names, vec!["coding__grep", "coding__read_file"]);
280 }
281
282 #[test]
283 fn combined_allow_and_deny() {
284 let filter = ToolFilter {
285 allow: vec![ToolMatcher::name("coding__*")],
286 deny: vec![ToolMatcher::name("coding__write_file")],
287 };
288 let tools = vec![
289 make_tool("coding__grep"),
290 make_tool("coding__write_file"),
291 make_tool("coding__read_file"),
292 make_tool("plugins__bash"),
293 ];
294 let result = filter.apply(tools);
295 let names: Vec<_> = result.iter().map(|t| t.name.as_str()).collect();
296 assert_eq!(names, vec!["coding__grep", "coding__read_file"]);
297 }
298
299 #[test]
300 fn annotation_allow_matches_present_values() {
301 let filter = ToolFilter { allow: vec![ToolMatcher::read_only()], deny: vec![] };
302 let tools = vec![
303 make_tool("unknown"),
304 make_annotated_tool("read", ToolAnnotations { read_only_hint: Some(true), ..ToolAnnotations::default() }),
305 make_annotated_tool("write", ToolAnnotations { read_only_hint: Some(false), ..ToolAnnotations::default() }),
306 ];
307 let names: Vec<_> = filter.apply(tools).into_iter().map(|tool| tool.name).collect();
308 assert_eq!(names, vec!["read"]);
309 }
310
311 #[test]
312 fn deny_annotation_removes_destructive_tools() {
313 let filter = ToolFilter {
314 allow: vec![],
315 deny: vec![ToolMatcher::annotations(ToolAnnotationMatcher {
316 destructive: Some(true),
317 ..ToolAnnotationMatcher::default()
318 })],
319 };
320 let tools = vec![
321 make_tool("unknown"),
322 make_annotated_tool(
323 "safe_update",
324 ToolAnnotations {
325 read_only_hint: Some(false),
326 destructive_hint: Some(false),
327 ..ToolAnnotations::default()
328 },
329 ),
330 ];
331 let names: Vec<_> = filter.apply(tools).into_iter().map(|tool| tool.name).collect();
332 assert_eq!(names, vec!["unknown", "safe_update"]);
333 }
334
335 #[test]
336 fn annotation_matchers_do_not_match_missing_fields() {
337 let filter = ToolFilter {
338 allow: vec![],
339 deny: vec![
340 ToolMatcher::annotations(ToolAnnotationMatcher {
341 destructive: Some(true),
342 ..ToolAnnotationMatcher::default()
343 }),
344 ToolMatcher::annotations(ToolAnnotationMatcher {
345 open_world: Some(true),
346 ..ToolAnnotationMatcher::default()
347 }),
348 ToolMatcher::annotations(ToolAnnotationMatcher {
349 idempotent: Some(false),
350 ..ToolAnnotationMatcher::default()
351 }),
352 ToolMatcher::annotations(ToolAnnotationMatcher {
353 read_only: Some(false),
354 ..ToolAnnotationMatcher::default()
355 }),
356 ],
357 };
358 let tools = vec![make_tool("unknown")];
359 let names: Vec<_> = filter.apply(tools).into_iter().map(|tool| tool.name).collect();
360 assert_eq!(names, vec!["unknown"]);
361 }
362
363 #[test]
364 fn annotation_matchers_do_not_infer_fields_from_read_only_hint() {
365 let filter = ToolFilter {
366 allow: vec![ToolMatcher::annotations(ToolAnnotationMatcher {
367 destructive: Some(false),
368 ..ToolAnnotationMatcher::default()
369 })],
370 deny: vec![],
371 };
372 let tools = vec![make_annotated_tool("read", ToolAnnotations::read_only())];
373 assert!(filter.apply(tools).is_empty());
374 }
375
376 #[test]
377 fn deny_wins_over_allow() {
378 let filter =
379 ToolFilter { allow: vec![ToolMatcher::read_only()], deny: vec![ToolMatcher::name("coding__read_file")] };
380 let tools = vec![make_annotated_tool(
381 "coding__read_file",
382 ToolAnnotations { read_only_hint: Some(true), ..ToolAnnotations::default() },
383 )];
384 assert!(filter.apply(tools).is_empty());
385 }
386
387 #[test]
388 fn mixed_allow_entries_are_ored() {
389 let filter = ToolFilter { allow: vec![ToolMatcher::read_only(), ToolMatcher::name("plan__*")], deny: vec![] };
390 let tools = vec![
391 make_annotated_tool(
392 "coding__grep",
393 ToolAnnotations { read_only_hint: Some(true), ..ToolAnnotations::default() },
394 ),
395 make_tool("plan__write_plan"),
396 make_tool("coding__bash"),
397 ];
398 let names: Vec<_> = filter.apply(tools).into_iter().map(|tool| tool.name).collect();
399 assert_eq!(names, vec!["coding__grep", "plan__write_plan"]);
400 }
401
402 #[test]
403 fn empty_annotation_matcher_matches_nothing() {
404 let filter =
405 ToolFilter { allow: vec![ToolMatcher::annotations(ToolAnnotationMatcher::default())], deny: vec![] };
406 let tools = vec![make_annotated_tool(
407 "coding__grep",
408 ToolAnnotations { read_only_hint: Some(true), ..ToolAnnotations::default() },
409 )];
410 assert!(filter.apply(tools).is_empty());
411 }
412
413 #[test]
414 fn exact_name_match_is_not_a_prefix_match() {
415 let filter = ToolFilter { allow: vec![ToolMatcher::name("bash")], deny: vec![] };
416 let names: Vec<_> =
417 filter.apply(vec![make_tool("bash"), make_tool("bash_extended")]).into_iter().map(|t| t.name).collect();
418 assert_eq!(names, vec!["bash"]);
419 }
420
421 #[test]
422 fn matches_pattern_exact_and_wildcard() {
423 assert!(matches_pattern("foo", "foo"));
424 assert!(!matches_pattern("foo", "foobar"));
425 assert!(matches_pattern("foo*", "foobar"));
426 assert!(matches_pattern("foo*", "foo"));
427 assert!(!matches_pattern("bar*", "foo"));
428 }
429}