hanzo-mcp 1.1.22

Hanzo MCP server — a hanzo-mcp binary serving 15 hand-written tools (fs, exec, code, git, fetch, workspace, computer, browser, think, memory, plan, tasks, mode, hanzo, search) over JSON-RPC
Documentation
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/// Unified network tool (HIP-0300)
///
/// Handles network operations:
/// - request: Full HTTP request with method/headers/body
/// - fetch: Simplified GET returning text
/// - head: HEAD request for headers only
/// - download: Save URL to file
/// - open: Open URL in browser
/// - search: Web search query
/// - crawl: Recursive site mirror

use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::HashMap;

use crate::hanzo_api::HanzoApi;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum NetAction {
    Request,
    Fetch,
    Head,
    Download,
    Open,
    Search,
    Crawl,
    Help,
}

impl Default for NetAction {
    fn default() -> Self {
        Self::Help
    }
}

impl std::str::FromStr for NetAction {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self> {
        match s.to_lowercase().as_str() {
            "request" | "get" => Ok(Self::Request),
            "fetch" => Ok(Self::Fetch),
            "head" => Ok(Self::Head),
            "download" | "save" => Ok(Self::Download),
            "open" | "browse" => Ok(Self::Open),
            "search" => Ok(Self::Search),
            "crawl" | "mirror" => Ok(Self::Crawl),
            "help" | "" => Ok(Self::Help),
            _ => Err(anyhow!("Unknown action: {}", s)),
        }
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FetchToolArgs {
    pub action: Option<String>,
    pub url: Option<String>,
    pub method: Option<String>,
    pub headers: Option<HashMap<String, String>>,
    pub body: Option<String>,
    pub output: Option<String>,
    pub timeout: Option<u64>,
    pub query: Option<String>,
    pub depth: Option<usize>,
    pub limit: Option<usize>,
}

pub struct FetchToolDefinition;

impl FetchToolDefinition {
    pub fn schema() -> Value {
        json!({
            "name": "fetch",
            "description": "Network operations: request, fetch, head, download, open, search, crawl",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "action": {
                        "type": "string",
                        "enum": ["request", "fetch", "head", "download", "open", "search", "crawl", "help"],
                        "description": "Network action"
                    },
                    "url": { "type": "string", "description": "URL" },
                    "method": { "type": "string", "description": "HTTP method", "default": "GET" },
                    "headers": { "type": "object", "description": "HTTP headers" },
                    "body": { "type": "string", "description": "Request body" },
                    "output": { "type": "string", "description": "Output file/dir for download/crawl" },
                    "timeout": { "type": "number", "description": "Timeout in ms", "default": 30000 },
                    "query": { "type": "string", "description": "Search query" },
                    "depth": { "type": "number", "description": "Crawl depth", "default": 2 },
                    "limit": { "type": "number", "description": "Max results/pages", "default": 10 }
                },
                "required": ["action"]
            }
        })
    }
}

pub struct FetchTool;

impl FetchTool {
    pub fn new() -> Self {
        Self
    }

    fn build_client(&self, timeout: u64) -> Result<reqwest::Client> {
        Ok(reqwest::Client::builder()
            .timeout(std::time::Duration::from_millis(timeout))
            .user_agent("Mozilla/5.0 (compatible; HanzoBot/1.0)")
            .build()?)
    }

    pub async fn execute(&self, args: FetchToolArgs) -> Result<Value> {
        let action: NetAction = args.action.as_deref().unwrap_or("help").parse()?;

        match action {
            NetAction::Request => self.request(&args).await,
            NetAction::Fetch => self.fetch_url(&args).await,
            NetAction::Head => self.head(&args).await,
            NetAction::Download => self.download(&args).await,
            NetAction::Open => self.open(&args).await,
            NetAction::Search => self.search(&args).await,
            NetAction::Crawl => self.crawl(&args).await,
            NetAction::Help => Ok(self.help()),
        }
    }

    async fn request(&self, args: &FetchToolArgs) -> Result<Value> {
        let url = args.url.as_deref().ok_or_else(|| anyhow!("url required"))?;
        let method = args.method.as_deref().unwrap_or("GET");
        let timeout = args.timeout.unwrap_or(30000);
        let client = self.build_client(timeout)?;

        let mut req = match method.to_uppercase().as_str() {
            "GET" => client.get(url),
            "POST" => client.post(url),
            "PUT" => client.put(url),
            "DELETE" => client.delete(url),
            "PATCH" => client.patch(url),
            "HEAD" => client.head(url),
            _ => return Err(anyhow!("Unsupported method: {}", method)),
        };

        if let Some(headers) = &args.headers {
            for (k, v) in headers {
                req = req.header(k.as_str(), v.as_str());
            }
        }
        if let Some(body) = &args.body {
            req = req.body(body.clone());
        }

        let resp = req.send().await?;
        let status = resp.status().as_u16();
        let headers: HashMap<String, String> = resp.headers()
            .iter()
            .map(|(k, v)| (k.to_string(), v.to_str().unwrap_or("").to_string()))
            .collect();

        let content_type = headers.get("content-type").cloned().unwrap_or_default();
        let body_text = resp.text().await?;
        let body_val: Value = if content_type.contains("json") {
            serde_json::from_str(&body_text).unwrap_or(Value::String(body_text.clone()))
        } else {
            Value::String(if body_text.len() > 50000 { body_text[..50000].to_string() } else { body_text })
        };

        Ok(json!({
            "ok": true,
            "data": { "status": status, "headers": headers, "body": body_val },
            "error": null,
            "meta": { "tool": "fetch", "action": "request" }
        }))
    }

    async fn fetch_url(&self, args: &FetchToolArgs) -> Result<Value> {
        let url = args.url.as_deref().ok_or_else(|| anyhow!("url required"))?;
        let timeout = args.timeout.unwrap_or(30000);
        let client = self.build_client(timeout)?;

        let mut req = client.get(url);
        if let Some(headers) = &args.headers {
            for (k, v) in headers {
                req = req.header(k.as_str(), v.as_str());
            }
        }

        let resp = req.send().await?;
        let status = resp.status().as_u16();
        let headers: HashMap<String, String> = resp.headers()
            .iter()
            .map(|(k, v)| (k.to_string(), v.to_str().unwrap_or("").to_string()))
            .collect();
        let text = resp.text().await?;
        let text = if text.len() > 50000 { text[..50000].to_string() } else { text };

        Ok(json!({
            "ok": true,
            "data": { "text": text, "status": status, "headers": headers },
            "error": null,
            "meta": { "tool": "fetch", "action": "fetch" }
        }))
    }

    async fn head(&self, args: &FetchToolArgs) -> Result<Value> {
        let url = args.url.as_deref().ok_or_else(|| anyhow!("url required"))?;
        let timeout = args.timeout.unwrap_or(30000);
        let client = self.build_client(timeout)?;

        let mut req = client.head(url);
        if let Some(headers) = &args.headers {
            for (k, v) in headers {
                req = req.header(k.as_str(), v.as_str());
            }
        }

        let resp = req.send().await?;
        let status = resp.status().as_u16();
        let headers: HashMap<String, String> = resp.headers()
            .iter()
            .map(|(k, v)| (k.to_string(), v.to_str().unwrap_or("").to_string()))
            .collect();

        Ok(json!({
            "ok": true,
            "data": { "status": status, "headers": headers },
            "error": null,
            "meta": { "tool": "fetch", "action": "head" }
        }))
    }

    async fn download(&self, args: &FetchToolArgs) -> Result<Value> {
        let url = args.url.as_deref().ok_or_else(|| anyhow!("url required"))?;
        let output = args.output.as_deref().ok_or_else(|| anyhow!("output path required"))?;
        let timeout = args.timeout.unwrap_or(60000);
        let client = self.build_client(timeout)?;

        let resp = client.get(url).send().await?;
        if !resp.status().is_success() {
            return Ok(json!({
                "ok": false, "data": null,
                "error": { "code": "HTTP_ERROR", "message": format!("{}", resp.status()) },
                "meta": { "tool": "fetch", "action": "download" }
            }));
        }

        let bytes = resp.bytes().await?;
        if let Some(parent) = std::path::Path::new(output).parent() {
            tokio::fs::create_dir_all(parent).await?;
        }
        tokio::fs::write(output, &bytes).await?;

        Ok(json!({
            "ok": true,
            "data": { "url": url, "output": output, "size": bytes.len() },
            "error": null,
            "meta": { "tool": "fetch", "action": "download" }
        }))
    }

    async fn open(&self, args: &FetchToolArgs) -> Result<Value> {
        let url = args.url.as_deref().ok_or_else(|| anyhow!("url required"))?;

        #[cfg(target_os = "macos")]
        let cmd = "open";
        #[cfg(target_os = "linux")]
        let cmd = "xdg-open";
        #[cfg(target_os = "windows")]
        let cmd = "start";

        tokio::process::Command::new(cmd).arg(url).output().await?;

        Ok(json!({
            "ok": true,
            "data": { "url": url, "opened": true },
            "error": null,
            "meta": { "tool": "fetch", "action": "open" }
        }))
    }

    async fn search(&self, args: &FetchToolArgs) -> Result<Value> {
        let query = args.query.as_deref().ok_or_else(|| anyhow!("query required"))?;

        // Canonical path: the platform web search (api.hanzo.ai). Fall back to a
        // local DuckDuckGo scrape only when the cloud is unreachable / unkeyed.
        let api = HanzoApi::from_env();
        if api.has_key() {
            if let Ok(body) = api.get("/v1/websearch/search", &[("q", query.to_string())]).await {
                let limit = args.limit.unwrap_or(10);
                let results: Vec<Value> = body["results"]
                    .as_array()
                    .map(|arr| {
                        arr.iter()
                            .take(limit)
                            .map(|r| json!({
                                "url": r.get("url").cloned().unwrap_or(Value::Null),
                                "title": r.get("title").cloned().unwrap_or(Value::Null),
                                "content": r.get("content").cloned().unwrap_or(Value::Null),
                            }))
                            .collect()
                    })
                    .unwrap_or_default();
                return Ok(json!({
                    "ok": true,
                    "data": { "query": query, "results": results, "count": results.len(), "source": "cloud" },
                    "error": null,
                    "meta": { "tool": "fetch", "action": "search" }
                }));
            }
        }

        self.search_local(args).await
    }

    async fn search_local(&self, args: &FetchToolArgs) -> Result<Value> {
        let query = args.query.as_deref().ok_or_else(|| anyhow!("query required"))?;
        let timeout = args.timeout.unwrap_or(15000);
        let client = self.build_client(timeout)?;

        // Manual URL encoding for the query
        let encoded_query: String = query.chars().map(|c| {
            if c.is_alphanumeric() || c == '-' || c == '_' || c == '.' || c == '~' {
                c.to_string()
            } else if c == ' ' {
                "+".to_string()
            } else {
                format!("%{:02X}", c as u32)
            }
        }).collect();
        let url = format!("https://html.duckduckgo.com/html/?q={}", encoded_query);
        let resp = client.get(&url).send().await?;
        let html = resp.text().await?;

        // Simple regex extraction of search results
        let re = regex::Regex::new(r#"class="result__title"[\s\S]*?href="([^"]*)"[^>]*>([\s\S]*?)</a>"#).unwrap();
        let limit = args.limit.unwrap_or(10);
        let mut results = Vec::new();

        for cap in re.captures_iter(&html) {
            if results.len() >= limit { break; }
            let href = cap.get(1).map(|m| m.as_str()).unwrap_or("").replace("&amp;", "&");
            let title = cap.get(2).map(|m| m.as_str()).unwrap_or("");
            // Strip HTML tags from title
            let tag_re = regex::Regex::new(r"<[^>]+>").unwrap();
            let clean_title = tag_re.replace_all(title, "").trim().to_string();
            results.push(json!({ "url": href, "title": clean_title }));
        }

        Ok(json!({
            "ok": true,
            "data": { "query": query, "results": results, "count": results.len(), "source": "local" },
            "error": null,
            "meta": { "tool": "fetch", "action": "search" }
        }))
    }

    async fn crawl(&self, args: &FetchToolArgs) -> Result<Value> {
        let url = args.url.as_deref().ok_or_else(|| anyhow!("url required"))?;
        let output = args.output.as_deref().ok_or_else(|| anyhow!("output directory required"))?;
        let max_depth = args.depth.unwrap_or(2);
        let max_pages = args.limit.unwrap_or(100);
        let timeout = args.timeout.unwrap_or(10000);
        let client = self.build_client(timeout)?;

        let start_host = reqwest::Url::parse(url)?.host_str().unwrap_or("").to_string();
        let mut visited = std::collections::HashSet::new();
        let mut pages = Vec::new();
        let mut queue = vec![(url.to_string(), 0usize)];

        tokio::fs::create_dir_all(output).await?;

        while let Some((current_url, depth)) = queue.first().cloned() {
            queue.remove(0);
            if visited.contains(&current_url) || depth > max_depth || pages.len() >= max_pages {
                continue;
            }
            if let Ok(parsed) = reqwest::Url::parse(&current_url) {
                if parsed.host_str().unwrap_or("") != start_host { continue; }
            } else { continue; }

            visited.insert(current_url.clone());

            let resp = match client.get(&current_url).send().await {
                Ok(r) => r,
                Err(_) => continue,
            };
            let body = match resp.text().await {
                Ok(b) => b,
                Err(_) => continue,
            };

            // Determine filename
            if let Ok(parsed) = reqwest::Url::parse(&current_url) {
                let path = parsed.path().trim_start_matches('/');
                let file_name = if path.is_empty() || path.ends_with('/') {
                    format!("{}index.html", path)
                } else if !path.contains('.') {
                    format!("{}.html", path)
                } else {
                    path.to_string()
                };
                let full_path = std::path::Path::new(output).join(&file_name);
                if let Some(parent) = full_path.parent() {
                    let _ = tokio::fs::create_dir_all(parent).await;
                }
                let _ = tokio::fs::write(&full_path, &body).await;
                pages.push(full_path.display().to_string());
            }

            // Extract links for further crawling
            let link_re = regex::Regex::new(r#"href=["']([^"']+)["']"#).unwrap();
            for cap in link_re.captures_iter(&body) {
                if let Some(href) = cap.get(1) {
                    if let Ok(abs) = reqwest::Url::parse(&current_url).and_then(|base| base.join(href.as_str())) {
                        let abs_str = abs.to_string();
                        if !visited.contains(&abs_str) {
                            queue.push((abs_str, depth + 1));
                        }
                    }
                }
            }
        }

        Ok(json!({
            "ok": true,
            "data": { "pages": pages, "count": pages.len(), "dest": output, "depth": max_depth },
            "error": null,
            "meta": { "tool": "fetch", "action": "crawl" }
        }))
    }

    fn help(&self) -> Value {
        json!({
            "ok": true,
            "data": {
                "tool": "fetch",
                "actions": {
                    "request": "Full HTTP request (requires url, optional method/headers/body)",
                    "fetch": "Simplified GET returning text (requires url)",
                    "head": "HEAD request for headers only (requires url)",
                    "download": "Save URL to file (requires url, output)",
                    "open": "Open URL in browser (requires url)",
                    "search": "Web search (requires query)",
                    "crawl": "Recursive site mirror (requires url, output)"
                }
            },
            "error": null,
            "meta": { "tool": "fetch", "action": "help" }
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_net_action_parse() {
        assert_eq!("request".parse::<NetAction>().unwrap(), NetAction::Request);
        assert_eq!("fetch".parse::<NetAction>().unwrap(), NetAction::Fetch);
        assert_eq!("head".parse::<NetAction>().unwrap(), NetAction::Head);
        assert_eq!("download".parse::<NetAction>().unwrap(), NetAction::Download);
        assert_eq!("search".parse::<NetAction>().unwrap(), NetAction::Search);
        assert_eq!("crawl".parse::<NetAction>().unwrap(), NetAction::Crawl);
    }
}