hanzo-mcp 1.1.23

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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//! Cloud-backed code knowledge tools (HIP-0300).
//!
//! Cross-repo search, context bundling, cited RAG answers, and indexing over
//! the live api.hanzo.ai code plane. These complement the local tree-sitter AST
//! engine (`code` tool / `search/ast_search.rs`), which stays for instant
//! single-file symbol/AST ops. Hybrid: local for one file, cloud for the corpus.
//!
//! - `code_search`  → GET  /v1/code/search  (symbol|text|semantic|hybrid)
//! - `code_context` → POST /v1/code/context (token-budgeted span bundle)
//! - `code_ask`     → GET  /v1/code/ask     (cited answer)
//! - `code_index`   → POST /v1/code/index   (index a repo's files)

use anyhow::Result;
use async_trait::async_trait;
use serde::Deserialize;
use serde_json::{json, Value};
use std::path::Path;
use walkdir::WalkDir;

use super::{envelope_err, envelope_ok};
use crate::hanzo_api::HanzoApi;
use crate::{MCPTool, ToolResult};

const SKIP_DIRS: &[&str] = &[
    ".git", "node_modules", "target", "dist", "build", "__pycache__", ".venv", "venv", ".next",
];
const MAX_INDEX_FILES: usize = 5000;
const MAX_FILE_BYTES: u64 = 512 * 1024;

/// Wrap a raw API result (or transport error) in the standard envelope.
fn respond(tool: &'static str, action: &'static str, result: Result<Value>) -> Result<ToolResult> {
    Ok(match result {
        Ok(body) => ToolResult::ok(envelope_ok(tool, action, body)),
        Err(e) => ToolResult::ok(envelope_err(tool, action, "UPSTREAM", e.to_string())),
    })
}

fn require_key(api: &HanzoApi, tool: &'static str, action: &'static str) -> Option<Result<ToolResult>> {
    if api.has_key() {
        None
    } else {
        Some(Ok(ToolResult::ok(envelope_err(
            tool,
            action,
            "NO_API_KEY",
            "no hk- key: set HANZO_API_KEY or ~/.hanzo/config.json .apiKey",
        ))))
    }
}

// ---------------------------------------------------------------------------
// code_search → GET /v1/code/search
// ---------------------------------------------------------------------------

#[derive(Debug, Default, Deserialize)]
struct SearchArgs {
    #[serde(alias = "q")]
    query: Option<String>,
    /// symbol | text | semantic | hybrid (default hybrid)
    #[serde(alias = "type")]
    kind: Option<String>,
    repo: Option<String>,
    limit: Option<u32>,
}

pub struct CodeSearchTool {
    api: HanzoApi,
}

impl CodeSearchTool {
    pub fn new() -> Self {
        Self { api: HanzoApi::from_env() }
    }
    pub fn schema() -> Value {
        json!({
            "name": "code_search",
            "description": "Hybrid cross-repo code search on api.hanzo.ai (symbol|text|semantic|hybrid). Cloud counterpart to the local `code` AST tool.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "query": { "type": "string", "description": "Search query" },
                    "type": { "type": "string", "enum": ["symbol", "text", "semantic", "hybrid"], "default": "hybrid" },
                    "repo": { "type": "string", "description": "Restrict to a repo" },
                    "limit": { "type": "number", "default": 20 }
                },
                "required": ["query"]
            }
        })
    }
}

impl Default for CodeSearchTool {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl MCPTool for CodeSearchTool {
    fn name(&self) -> &str {
        "code_search"
    }
    fn description(&self) -> &str {
        "Hybrid cross-repo code search on api.hanzo.ai (symbol|text|semantic|hybrid)"
    }
    fn parameters(&self) -> Value {
        Self::schema()["inputSchema"].clone()
    }
    async fn execute(&self, params: Value) -> Result<ToolResult> {
        if let Some(r) = require_key(&self.api, "code_search", "search") {
            return r;
        }
        let args: SearchArgs = serde_json::from_value(params).unwrap_or_default();
        let query = match args.query.filter(|q| !q.trim().is_empty()) {
            Some(q) => q,
            None => return Ok(ToolResult::ok(envelope_err("code_search", "search", "INVALID_ARGS", "query required"))),
        };

        let mut q: Vec<(&str, String)> = vec![
            ("q", query),
            ("type", args.kind.unwrap_or_else(|| "hybrid".to_string())),
            ("limit", args.limit.unwrap_or(20).to_string()),
        ];
        if let Some(repo) = args.repo.filter(|r| !r.is_empty()) {
            q.push(("repo", repo));
        }
        respond("code_search", "search", self.api.get("/v1/code/search", &q).await)
    }
}

// ---------------------------------------------------------------------------
// code_context → POST /v1/code/context
// ---------------------------------------------------------------------------

#[derive(Debug, Default, Deserialize)]
struct ContextArgs {
    #[serde(alias = "q")]
    query: Option<String>,
    #[serde(alias = "budget_tokens", alias = "budget")]
    budget_tokens: Option<u32>,
    repo: Option<String>,
}

pub struct CodeContextTool {
    api: HanzoApi,
}

impl CodeContextTool {
    pub fn new() -> Self {
        Self { api: HanzoApi::from_env() }
    }
    pub fn schema() -> Value {
        json!({
            "name": "code_context",
            "description": "Token-budgeted code context bundle for a query from api.hanzo.ai.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "query": { "type": "string", "description": "What to gather context for" },
                    "budgetTokens": { "type": "number", "default": 2000 },
                    "repo": { "type": "string" }
                },
                "required": ["query"]
            }
        })
    }
}

impl Default for CodeContextTool {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl MCPTool for CodeContextTool {
    fn name(&self) -> &str {
        "code_context"
    }
    fn description(&self) -> &str {
        "Token-budgeted code context bundle for a query from api.hanzo.ai"
    }
    fn parameters(&self) -> Value {
        Self::schema()["inputSchema"].clone()
    }
    async fn execute(&self, params: Value) -> Result<ToolResult> {
        if let Some(r) = require_key(&self.api, "code_context", "context") {
            return r;
        }
        let args: ContextArgs = serde_json::from_value(params).unwrap_or_default();
        let query = match args.query.filter(|q| !q.trim().is_empty()) {
            Some(q) => q,
            None => return Ok(ToolResult::ok(envelope_err("code_context", "context", "INVALID_ARGS", "query required"))),
        };
        let mut body = json!({
            "query": query,
            "budgetTokens": args.budget_tokens.unwrap_or(2000),
        });
        if let Some(repo) = args.repo.filter(|r| !r.is_empty()) {
            body["repo"] = json!(repo);
        }
        respond("code_context", "context", self.api.post("/v1/code/context", body).await)
    }
}

// ---------------------------------------------------------------------------
// code_ask → GET /v1/code/ask
// ---------------------------------------------------------------------------

#[derive(Debug, Default, Deserialize)]
struct AskArgs {
    #[serde(alias = "q", alias = "question")]
    query: Option<String>,
    repo: Option<String>,
}

pub struct CodeAskTool {
    api: HanzoApi,
}

impl CodeAskTool {
    pub fn new() -> Self {
        Self { api: HanzoApi::from_env() }
    }
    pub fn schema() -> Value {
        json!({
            "name": "code_ask",
            "description": "Ask a question about indexed code; returns a cited RAG answer from api.hanzo.ai.",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "query": { "type": "string", "description": "Question about the code" },
                    "repo": { "type": "string" }
                },
                "required": ["query"]
            }
        })
    }
}

impl Default for CodeAskTool {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl MCPTool for CodeAskTool {
    fn name(&self) -> &str {
        "code_ask"
    }
    fn description(&self) -> &str {
        "Ask a question about indexed code; returns a cited RAG answer from api.hanzo.ai"
    }
    fn parameters(&self) -> Value {
        Self::schema()["inputSchema"].clone()
    }
    async fn execute(&self, params: Value) -> Result<ToolResult> {
        if let Some(r) = require_key(&self.api, "code_ask", "ask") {
            return r;
        }
        let args: AskArgs = serde_json::from_value(params).unwrap_or_default();
        let query = match args.query.filter(|q| !q.trim().is_empty()) {
            Some(q) => q,
            None => return Ok(ToolResult::ok(envelope_err("code_ask", "ask", "INVALID_ARGS", "query required"))),
        };
        let mut q: Vec<(&str, String)> = vec![("q", query)];
        if let Some(repo) = args.repo.filter(|r| !r.is_empty()) {
            q.push(("repo", repo));
        }
        respond("code_ask", "ask", self.api.get("/v1/code/ask", &q).await)
    }
}

// ---------------------------------------------------------------------------
// code_index → POST /v1/code/index
// ---------------------------------------------------------------------------

#[derive(Debug, Deserialize)]
struct IndexFile {
    path: String,
    content: String,
}

#[derive(Debug, Default, Deserialize)]
struct IndexArgs {
    repo: Option<String>,
    /// Local directory to walk into `files` when `files` is omitted.
    path: Option<String>,
    files: Option<Vec<IndexFile>>,
}

pub struct CodeIndexTool {
    api: HanzoApi,
}

impl CodeIndexTool {
    pub fn new() -> Self {
        Self { api: HanzoApi::from_env() }
    }
    pub fn schema() -> Value {
        json!({
            "name": "code_index",
            "description": "Index a repo into api.hanzo.ai. Pass explicit `files`, or a local `path` to walk (text files, bounded).",
            "inputSchema": {
                "type": "object",
                "properties": {
                    "repo": { "type": "string", "description": "Repo name (defaults to the directory basename)" },
                    "path": { "type": "string", "description": "Local directory to walk and index" },
                    "files": {
                        "type": "array",
                        "description": "Explicit files to index",
                        "items": {
                            "type": "object",
                            "properties": {
                                "path": { "type": "string" },
                                "content": { "type": "string" }
                            },
                            "required": ["path", "content"]
                        }
                    }
                },
                "required": []
            }
        })
    }
}

impl Default for CodeIndexTool {
    fn default() -> Self {
        Self::new()
    }
}

/// Walk a directory into indexable `{path, content}` files (bounded, text only).
fn collect_files(dir: &Path) -> (Vec<Value>, usize) {
    let mut files = Vec::new();
    let mut skipped = 0usize;
    for entry in WalkDir::new(dir)
        .follow_links(false)
        .into_iter()
        .filter_entry(|e| {
            !(e.file_type().is_dir()
                && e.file_name().to_str().map_or(false, |n| SKIP_DIRS.contains(&n)))
        })
        .filter_map(|e| e.ok())
        .filter(|e| e.file_type().is_file())
    {
        if files.len() >= MAX_INDEX_FILES {
            break;
        }
        let path = entry.path();
        match entry.metadata() {
            Ok(m) if m.len() > MAX_FILE_BYTES => {
                skipped += 1;
                continue;
            }
            Ok(_) => {}
            Err(_) => {
                skipped += 1;
                continue;
            }
        }
        match std::fs::read_to_string(path) {
            Ok(content) => {
                let rel = path.strip_prefix(dir).unwrap_or(path).to_string_lossy().to_string();
                files.push(json!({ "path": rel, "content": content }));
            }
            Err(_) => skipped += 1, // binary / non-utf8
        }
    }
    (files, skipped)
}

#[async_trait]
impl MCPTool for CodeIndexTool {
    fn name(&self) -> &str {
        "code_index"
    }
    fn description(&self) -> &str {
        "Index a repo into api.hanzo.ai (explicit files or a local path to walk)"
    }
    fn parameters(&self) -> Value {
        Self::schema()["inputSchema"].clone()
    }
    async fn execute(&self, params: Value) -> Result<ToolResult> {
        if let Some(r) = require_key(&self.api, "code_index", "index") {
            return r;
        }
        let args: IndexArgs = serde_json::from_value(params).unwrap_or_default();

        // Resolve files: explicit list wins; else walk the local path.
        let (files, repo, walked_skipped) = if let Some(list) = args.files {
            let files: Vec<Value> = list
                .into_iter()
                .map(|f| json!({ "path": f.path, "content": f.content }))
                .collect();
            (files, args.repo.clone(), 0usize)
        } else if let Some(path) = args.path.as_deref().filter(|p| !p.is_empty()) {
            let dir = Path::new(path);
            if !dir.is_dir() {
                return Ok(ToolResult::ok(envelope_err("code_index", "index", "INVALID_ARGS", format!("not a directory: {}", path))));
            }
            let repo = args.repo.clone().or_else(|| {
                dir.canonicalize()
                    .ok()
                    .and_then(|c| c.file_name().map(|n| n.to_string_lossy().to_string()))
            });
            let (files, skipped) = collect_files(dir);
            (files, repo, skipped)
        } else {
            return Ok(ToolResult::ok(envelope_err("code_index", "index", "INVALID_ARGS", "provide `files` or a local `path`")));
        };

        if files.is_empty() {
            return Ok(ToolResult::ok(envelope_err("code_index", "index", "EMPTY", "no indexable files found")));
        }
        let repo = match repo.filter(|r| !r.is_empty()) {
            Some(r) => r,
            None => return Ok(ToolResult::ok(envelope_err("code_index", "index", "INVALID_ARGS", "repo required"))),
        };

        let sent = files.len();
        let body = json!({ "repo": repo, "files": files });
        match self.api.post("/v1/code/index", body).await {
            Ok(mut result) => {
                if let Some(obj) = result.as_object_mut() {
                    obj.insert("sent".into(), json!(sent));
                    if walked_skipped > 0 {
                        obj.insert("walkedSkipped".into(), json!(walked_skipped));
                    }
                }
                Ok(ToolResult::ok(envelope_ok("code_index", "index", result)))
            }
            Err(e) => Ok(ToolResult::ok(envelope_err("code_index", "index", "UPSTREAM", e.to_string()))),
        }
    }
}

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

    #[test]
    fn search_schema_shape() {
        let s = CodeSearchTool::schema();
        assert_eq!(s["name"], "code_search");
        assert_eq!(s["inputSchema"]["required"][0], "query");
        let props = &s["inputSchema"]["properties"];
        assert!(props.get("type").is_some());
        assert!(props.get("repo").is_some());
    }

    #[test]
    fn tool_names_are_stable() {
        assert_eq!(CodeSearchTool::new().name(), "code_search");
        assert_eq!(CodeContextTool::new().name(), "code_context");
        assert_eq!(CodeAskTool::new().name(), "code_ask");
        assert_eq!(CodeIndexTool::new().name(), "code_index");
    }

    #[test]
    fn search_args_accept_q_and_type_aliases() {
        let a: SearchArgs = serde_json::from_value(json!({ "q": "foo", "type": "symbol", "limit": 5 })).unwrap();
        assert_eq!(a.query.as_deref(), Some("foo"));
        assert_eq!(a.kind.as_deref(), Some("symbol"));
        assert_eq!(a.limit, Some(5));
    }

    #[test]
    fn collect_files_walks_and_skips_noise() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let mut f = std::fs::File::create(root.join("a.rs")).unwrap();
        writeln!(f, "fn main() {{}}").unwrap();
        std::fs::create_dir_all(root.join("node_modules")).unwrap();
        std::fs::File::create(root.join("node_modules").join("junk.js")).unwrap();

        let (files, _skipped) = collect_files(root);
        assert_eq!(files.len(), 1, "node_modules must be pruned");
        assert_eq!(files[0]["path"], "a.rs");
    }

    /// Live end-to-end via the registry: index a repo, then find a symbol in it.
    /// Requires a live api.hanzo.ai + hk- key. Run: `cargo test -- --ignored`.
    #[tokio::test]
    #[ignore]
    async fn live_index_then_search_via_registry() {
        let registry = crate::ToolRegistry::with_defaults();
        let repo = format!("mcp-rust-live-{}", std::process::id());

        let indexed = registry
            .execute("code_index", json!({
                "repo": repo,
                "files": [{ "path": "greet.rs", "content": "pub fn hanzo_greet() -> &'static str { \"hi\" }" }]
            }))
            .await
            .unwrap();
        assert_eq!(indexed.content["ok"], true, "index envelope: {}", indexed.content);
        assert!(indexed.content["data"]["indexed"].as_u64().unwrap_or(0) >= 1);

        let found = registry
            .execute("code_search", json!({ "q": "hanzo_greet", "type": "hybrid", "repo": repo }))
            .await
            .unwrap();
        assert_eq!(found.content["ok"], true, "search envelope: {}", found.content);
        println!("code_search data: {}", found.content["data"]);
    }
}