claude_code_agent/
middleware.rs1use crate::hooks::HookRegistry;
3use crate::protocol::{HookEventKind, HookOutput};
4
5#[derive(Debug, Clone)]
15pub struct ToolAllowList {
16 pub allowed: Vec<String>,
18}
19
20impl ToolAllowList {
21 pub fn new(tools: impl IntoIterator<Item = impl Into<String>>) -> Self {
23 Self { allowed: tools.into_iter().map(Into::into).collect() }
24 }
25
26 #[must_use]
28 pub fn allows(&self, tool: &str) -> bool {
29 self.allowed.iter().any(|t| t == tool)
30 }
31
32 pub fn install(self, hooks: &mut HookRegistry) {
34 hooks.on(HookEventKind::PreToolUse, None, move |payload| {
35 let verdict = match payload.tool_name.as_deref() {
36 Some(tool) if !self.allows(tool) => Some(format!(
37 "Tool {tool} is not allowed here. Allowed tools: {}",
38 self.allowed.join(", ")
39 )),
40 _ => None,
41 };
42 async move { verdict.map_or_else(HookOutput::allow, HookOutput::block) }
43 });
44 }
45}