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
#![recursion_limit = "256"]
//! Hanzo MCP Server - Rust implementation (HIP-0300)
//!
/// Provides full tool parity with Python hanzo-mcp:
/// - exec: Process execution
/// - fs: File system operations
/// - plan: Plan tracking
/// - think: Reasoning tools (think, critic, review)
/// - memory: Memory and knowledge management
/// - computer: Native OS control
/// - browser: Playwright-based browser automation
/// - mode: Development modes
/// - search: Unified code search

pub mod brain;
pub mod config;
pub mod ffi;
pub mod hanzo_api;
pub mod server;
pub mod protocol;
pub mod tools;
pub mod search;

pub use config::Config;
pub use server::MCPServer;
pub use tools::{
    ExecTool, FsTool, PlanTool, ThinkTool, MemoryTool,
    ComputerTool, BrowserTool, ModeTool,
    CodeTool, GitTool, FetchTool, WorkspaceTool, TasksTool, HanzoTool,
    list_tools, parity_status,
};

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

/// MCP Tool trait that all tools must implement
#[async_trait::async_trait]
pub trait MCPTool: Send + Sync {
    /// Get the tool's name
    fn name(&self) -> &str;

    /// Get the tool's description
    fn description(&self) -> &str;

    /// Get the tool's parameters schema
    fn parameters(&self) -> serde_json::Value;

    /// Execute the tool with given parameters
    async fn execute(&self, params: serde_json::Value) -> Result<ToolResult>;
}

/// Result from tool execution
#[derive(Debug, Serialize, Deserialize)]
pub struct ToolResult {
    pub success: bool,
    pub content: serde_json::Value,
    pub error: Option<String>,
}

impl ToolResult {
    pub fn ok(content: Value) -> Self {
        Self {
            success: true,
            content,
            error: None,
        }
    }

    pub fn err(message: &str) -> Self {
        Self {
            success: false,
            content: json!(null),
            error: Some(message.to_string()),
        }
    }
}

/// Tool wrapper for unified execution
pub struct ToolWrapper<T> {
    pub tool: Arc<RwLock<T>>,
    pub name: String,
    pub description: String,
    pub schema: Value,
}

/// Tool registry for managing all available tools
pub struct ToolRegistry {
    tools: HashMap<String, Box<dyn MCPTool>>,
    exec: Arc<RwLock<ExecTool>>,
    fs: Arc<RwLock<FsTool>>,
    code: Arc<RwLock<CodeTool>>,
    git: Arc<RwLock<GitTool>>,
    fetch: Arc<RwLock<FetchTool>>,
    workspace: Arc<RwLock<WorkspaceTool>>,
    plan: Arc<RwLock<PlanTool>>,
    think: Arc<RwLock<ThinkTool>>,
    memory: Arc<RwLock<MemoryTool>>,
    computer: Arc<RwLock<ComputerTool>>,
    browser: Arc<RwLock<BrowserTool>>,
    mode: Arc<RwLock<ModeTool>>,
    tasks: Arc<RwLock<TasksTool>>,
    hanzo: Arc<RwLock<HanzoTool>>,
}

impl ToolRegistry {
    pub fn new() -> Self {
        Self {
            tools: HashMap::new(),
            exec: Arc::new(RwLock::new(ExecTool::new())),
            fs: Arc::new(RwLock::new(FsTool::new())),
            code: Arc::new(RwLock::new(CodeTool::new())),
            git: Arc::new(RwLock::new(GitTool::new())),
            fetch: Arc::new(RwLock::new(FetchTool::new())),
            workspace: Arc::new(RwLock::new(WorkspaceTool::new())),
            plan: Arc::new(RwLock::new(PlanTool::new())),
            think: Arc::new(RwLock::new(ThinkTool::new())),
            memory: Arc::new(RwLock::new(MemoryTool::new())),
            computer: Arc::new(RwLock::new(ComputerTool::new())),
            browser: Arc::new(RwLock::new(BrowserTool::new())),
            mode: Arc::new(RwLock::new(ModeTool::new())),
            tasks: Arc::new(RwLock::new(TasksTool::new())),
            hanzo: Arc::new(RwLock::new(HanzoTool::new())),
        }
    }

    pub fn register(&mut self, tool: Box<dyn MCPTool>) {
        self.tools.insert(tool.name().to_string(), tool);
    }

    pub fn get(&self, name: &str) -> Option<&Box<dyn MCPTool>> {
        self.tools.get(name)
    }

    pub fn list(&self) -> Vec<String> {
        let mut names: Vec<String> = self.tools.keys().cloned().collect();
        // Add built-in tools (all 13 HIP-0300 canonical + search alias + browser extension)
        names.extend(vec![
            "exec".into(), "fs".into(), "code".into(), "git".into(),
            "fetch".into(), "workspace".into(), "computer".into(),
            "think".into(), "memory".into(), "hanzo".into(),
            "plan".into(), "tasks".into(), "mode".into(),
            "search".into(), "browser".into(),
        ]);
        names.sort();
        names.dedup();
        names
    }

    /// Execute a tool by name
    pub async fn execute(&self, name: &str, params: Value) -> Result<ToolResult> {
        match name {
            "exec" => {
                let args: tools::ExecToolArgs = serde_json::from_value(params)?;
                let result = self.exec.read().await.execute(args).await?;
                Ok(ToolResult::ok(serde_json::from_str(&result)?))
            }
            "fs" => {
                let args: tools::FsToolArgs = serde_json::from_value(params)?;
                let result = self.fs.read().await.execute(args).await?;
                Ok(ToolResult::ok(serde_json::from_str(&result)?))
            }
            "search" => {
                let mut args: tools::FsToolArgs = serde_json::from_value(params)?;
                if args.action.is_empty() {
                    args.action = "search".to_string();
                }
                let result = self.fs.read().await.execute(args).await?;
                Ok(ToolResult::ok(serde_json::from_str(&result)?))
            }
            "plan" => {
                let args: tools::PlanToolArgs = serde_json::from_value(params)?;
                let result = self.plan.read().await.execute(args).await?;
                Ok(ToolResult::ok(serde_json::from_str(&result)?))
            }
            "think" => {
                let args: tools::ThinkToolArgs = serde_json::from_value(params)?;
                let result = self.think.read().await.execute(args).await?;
                Ok(ToolResult::ok(result))
            }
            "memory" => {
                let args: tools::MemoryToolArgs = serde_json::from_value(params)?;
                let result = self.memory.read().await.execute(args).await?;
                Ok(ToolResult::ok(serde_json::from_str(&result)?))
            }
            "computer" => {
                let args: tools::ComputerToolArgs = serde_json::from_value(params)?;
                let mut computer = self.computer.write().await;
                let result = computer.execute(args).await?;
                Ok(ToolResult::ok(serde_json::from_str(&result)?))
            }
            "browser" => {
                let args: tools::BrowserToolArgs = serde_json::from_value(params)?;
                let result = self.browser.read().await.execute(args).await?;
                Ok(ToolResult::ok(serde_json::from_str(&result)?))
            }
            "mode" => {
                let args: tools::ModeToolArgs = serde_json::from_value(params)?;
                let result = self.mode.read().await.execute(args).await?;
                Ok(ToolResult::ok(serde_json::from_str(&result)?))
            }
            "code" => {
                let args: tools::CodeToolArgs = serde_json::from_value(params)?;
                let result = self.code.read().await.execute(args).await?;
                Ok(ToolResult::ok(result))
            }
            "git" => {
                let args: tools::GitToolArgs = serde_json::from_value(params)?;
                let result = self.git.read().await.execute(args).await?;
                Ok(ToolResult::ok(result))
            }
            "fetch" => {
                let args: tools::FetchToolArgs = serde_json::from_value(params)?;
                let result = self.fetch.read().await.execute(args).await?;
                Ok(ToolResult::ok(result))
            }
            "workspace" => {
                let args: tools::WorkspaceToolArgs = serde_json::from_value(params)?;
                let result = self.workspace.read().await.execute(args).await?;
                Ok(ToolResult::ok(result))
            }
            "tasks" => {
                let args: tools::TasksToolArgs = serde_json::from_value(params)?;
                let result = self.tasks.read().await.execute(args).await?;
                Ok(ToolResult::ok(result))
            }
            "hanzo" => {
                let args: tools::HanzoToolArgs = serde_json::from_value(params)?;
                let result = self.hanzo.read().await.execute(args).await?;
                Ok(ToolResult::ok(result))
            }
            _ => {
                if let Some(tool) = self.tools.get(name) {
                    tool.execute(params).await
                } else {
                    Ok(ToolResult::err(&format!("Unknown tool: {}", name)))
                }
            }
        }
    }

    /// Get tool definitions for MCP protocol
    pub fn get_definitions(&self) -> Vec<Value> {
        let mut definitions = vec![
            json!({
                "name": "exec",
                "description": tools::ExecToolDefinition::new().description,
                "inputSchema": tools::ExecToolDefinition::new().input_schema
            }),
            json!({
                "name": "fs",
                "description": tools::FsToolDefinition::new().description,
                "inputSchema": tools::FsToolDefinition::new().input_schema
            }),
            json!({
                "name": "search",
                "description": "Search file contents (alias of fs with action=search)",
                "inputSchema": tools::FsToolDefinition::new().input_schema
            }),
            json!({
                "name": "plan",
                "description": tools::PlanToolDefinition::new().description,
                "inputSchema": tools::PlanToolDefinition::new().input_schema
            }),
            json!({
                "name": "think",
                "description": tools::ThinkToolDefinition::new().description,
                "inputSchema": tools::ThinkToolDefinition::new().input_schema
            }),
            json!({
                "name": "memory",
                "description": tools::MemoryToolDefinition::new().description,
                "inputSchema": tools::MemoryToolDefinition::new().input_schema
            }),
            json!({
                "name": "computer",
                "description": tools::ComputerToolDefinition::new().description,
                "inputSchema": tools::ComputerToolDefinition::new().input_schema
            }),
            json!({
                "name": "browser",
                "description": tools::BrowserToolDefinition::new().description,
                "inputSchema": tools::BrowserToolDefinition::new().input_schema
            }),
            json!({
                "name": "mode",
                "description": tools::ModeToolDefinition::new().description,
                "inputSchema": tools::ModeToolDefinition::new().input_schema
            }),
            tools::CodeToolDefinition::schema(),
            tools::GitToolDefinition::schema(),
            tools::FetchToolDefinition::schema(),
            tools::WorkspaceToolDefinition::schema(),
            tools::TasksToolDefinition::schema(),
            tools::HanzoToolDefinition::schema(),
        ];

        // Add custom registered tools
        for tool in self.tools.values() {
            definitions.push(json!({
                "name": tool.name(),
                "description": tool.description(),
                "inputSchema": tool.parameters()
            }));
        }

        definitions
    }

    /// Initialize with all default tools
    pub fn with_defaults() -> Self {
        let mut registry = Self::new();

        // Cloud-backed tools (api.hanzo.ai) via the generic MCPTool seam.
        // These complement the local tools: code_* is cross-repo RAG next to the
        // local tree-sitter `code` tool; web_* and vision reach the platform.
        registry.register(Box::new(tools::CodeSearchTool::new()));
        registry.register(Box::new(tools::CodeContextTool::new()));
        registry.register(Box::new(tools::CodeAskTool::new()));
        registry.register(Box::new(tools::CodeIndexTool::new()));
        registry.register(Box::new(tools::WebSearchTool::new()));
        registry.register(Box::new(tools::WebReadTool::new()));
        registry.register(Box::new(tools::ResearchTool::new()));
        registry.register(Box::new(tools::VisionTool::new()));

        // Ported tool surface (HIP-0300 parity with python-sdk): config, llm, ui,
        // agent, lsp, refactor, system — all dyn-trait tools via the MCPTool seam.
        registry.register(Box::new(tools::ConfigTool::new()));
        registry.register(Box::new(tools::LlmTool::new()));
        registry.register(Box::new(tools::UiTool::new()));
        registry.register(Box::new(tools::AgentTool::new()));
        registry.register(Box::new(tools::LspTool::new()));
        registry.register(Box::new(tools::RefactorTool::new()));
        registry.register(Box::new(tools::SystemTool::new()));

        #[cfg(feature = "computer-control")]
        {
            // Additional computer control features
        }

        #[cfg(feature = "vector-store")]
        {
            // Vector store integration
        }

        registry
    }
}

impl Default for ToolRegistry {
    fn default() -> Self {
        Self::with_defaults()
    }
}

/// Get version information
pub fn version() -> Value {
    json!({
        "name": "hanzo-mcp",
        "version": env!("CARGO_PKG_VERSION"),
        "rust_version": "1.75+",
        "tools": list_tools().len(),
        "parity": parity_status()
    })
}

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

    #[test]
    fn test_tool_registry() {
        let registry = ToolRegistry::new();
        let tools = registry.list();
        assert!(tools.contains(&"exec".to_string()));
        assert!(tools.contains(&"fs".to_string()));
        assert!(tools.contains(&"search".to_string()));
        assert!(tools.contains(&"plan".to_string()));
        assert!(tools.contains(&"think".to_string()));
        assert!(tools.contains(&"memory".to_string()));
        assert!(tools.contains(&"computer".to_string()));
        assert!(tools.contains(&"browser".to_string()));
        assert!(tools.contains(&"mode".to_string()));
    }

    #[test]
    fn test_tool_definitions() {
        let registry = ToolRegistry::new();
        let definitions = registry.get_definitions();
        assert!(definitions.len() >= 9);
    }

    #[test]
    fn test_cloud_tools_registered_and_discoverable() {
        let registry = ToolRegistry::with_defaults();
        let names = registry.list();
        for t in ["code_search", "code_context", "code_ask", "code_index", "web_search", "web_read", "research", "vision"] {
            assert!(names.contains(&t.to_string()), "{t} missing from registry.list()");
        }
        // Each cloud tool must also expose a definition (name + inputSchema) for MCP tools/list.
        let defs = registry.get_definitions();
        for t in ["code_search", "code_context", "code_ask", "code_index", "web_search", "web_read", "research", "vision"] {
            assert!(
                defs.iter().any(|d| d["name"] == t && d["inputSchema"].is_object()),
                "{t} missing a definition"
            );
        }
    }

    #[tokio::test]
    async fn test_proc_execute() {
        let registry = ToolRegistry::new();
        let result = registry.execute("exec", json!({
            "action": "help"
        })).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_fs_execute() {
        let registry = ToolRegistry::new();
        let result = registry.execute("fs", json!({
            "action": "help"
        })).await;
        assert!(result.is_ok());
    }

    #[test]
    fn test_version() {
        let v = version();
        assert!(v.get("name").is_some());
        assert!(v.get("version").is_some());
        assert!(v.get("tools").is_some());
    }
}