Skip to main content

claude_rust_tools/infrastructure/
web_search_tool.rs

1use claude_rust_errors::{AppError, AppResult};
2use claude_rust_types::{PermissionLevel, SearchReadInfo, 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    fn is_read_only(&self, _input: &Value) -> bool { true }
41    fn is_concurrent_safe(&self, _input: &Value) -> bool { true }
42    fn is_open_world(&self, _input: &Value) -> bool { true }
43
44    fn is_search_or_read_command(&self, _input: &Value) -> SearchReadInfo {
45        SearchReadInfo { is_search: true, is_read: false, is_list: false }
46    }
47
48    async fn execute(&self, input: Value) -> AppResult<String> {
49        let query = input
50            .get("query")
51            .and_then(|q| q.as_str())
52            .ok_or_else(|| AppError::Tool("missing 'query' field".into()))?;
53
54        let max_results = input
55            .get("max_results")
56            .and_then(|m| m.as_u64())
57            .unwrap_or(10) as usize;
58
59        search(query, max_results).await
60    }
61}