Skip to main content

claude_rust_tools/infrastructure/
web_search_tool.rs

1use claude_rust_errors::{AppError, AppResult};
2use claude_rust_types::{PermissionLevel, Tool};
3use serde_json::{Value, json};
4
5use super::web_search_client::search;
6
7pub struct WebSearchTool;
8
9#[async_trait::async_trait]
10impl Tool for WebSearchTool {
11    fn name(&self) -> &str {
12        "web_search"
13    }
14
15    fn description(&self) -> &str {
16        "Search the web using DuckDuckGo. Returns a list of results with titles, URLs, and snippets."
17    }
18
19    fn input_schema(&self) -> Value {
20        json!({
21            "type": "object",
22            "properties": {
23                "query": {
24                    "type": "string",
25                    "description": "The search query"
26                },
27                "max_results": {
28                    "type": "number",
29                    "description": "Maximum number of results to return (default: 10)"
30                }
31            },
32            "required": ["query"]
33        })
34    }
35
36    fn permission_level(&self) -> PermissionLevel {
37        PermissionLevel::Dangerous
38    }
39
40    async fn execute(&self, input: Value) -> AppResult<String> {
41        let query = input
42            .get("query")
43            .and_then(|q| q.as_str())
44            .ok_or_else(|| AppError::Tool("missing 'query' field".into()))?;
45
46        let max_results = input
47            .get("max_results")
48            .and_then(|m| m.as_u64())
49            .unwrap_or(10) as usize;
50
51        search(query, max_results).await
52    }
53}