hematite-cli 0.10.0

Senior SysAdmin, Network Admin, Data Analyst, and Software Engineer living in your terminal. A high-precision local AI agent harness for LM Studio, Ollama, and other local OpenAI-compatible runtimes that runs 100% on your own silicon. Reads repos, edits files, runs builds, inspects full network state and workstation telemetry, and runs real Python/JS for data analysis.
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
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use std::collections::VecDeque;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::sync::{Arc, Mutex};
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
use tokio::process::{Child, ChildStderr, ChildStdin, ChildStdout, Command};
use tokio::task::JoinHandle;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum McpFraming {
    NewlineDelimited,
    ContentLength,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(untagged)]
pub enum JsonRpcId {
    Number(u64),
    String(String),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcRequest<T = JsonValue> {
    pub jsonrpc: String,
    pub id: JsonRpcId,
    pub method: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<T>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcNotification<T = JsonValue> {
    pub jsonrpc: String,
    pub method: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub params: Option<T>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcResponse<T = JsonValue> {
    pub jsonrpc: String,
    pub id: JsonRpcId,
    pub result: Option<T>,
    pub error: Option<JsonRpcError>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcError {
    pub code: i64,
    pub message: String,
    pub data: Option<JsonValue>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct McpTool {
    pub name: String,
    pub description: Option<String>,
    pub input_schema: JsonValue,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct McpListToolsResult {
    pub tools: Vec<McpTool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct McpCallToolResult {
    pub content: Vec<McpContent>,
    pub is_error: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum McpContent {
    #[serde(rename = "text")]
    Text { text: String },
    #[serde(rename = "image")]
    Image { data: String, mime_type: String },
}

pub struct McpProcess {
    _child: Child,
    stdin: ChildStdin,
    stdout: BufReader<ChildStdout>,
    framing: McpFraming,
    stderr_lines: Arc<Mutex<VecDeque<String>>>,
    _stderr_task: JoinHandle<()>,
}

impl McpProcess {
    pub fn spawn(
        command: &str,
        args: &[String],
        env: &std::collections::HashMap<String, String>,
    ) -> Result<Self> {
        Self::spawn_with_framing(command, args, env, McpFraming::NewlineDelimited)
    }

    pub fn spawn_with_framing(
        command: &str,
        args: &[String],
        env: &std::collections::HashMap<String, String>,
        framing: McpFraming,
    ) -> Result<Self> {
        let resolved_command =
            resolve_command_path(command).unwrap_or_else(|| PathBuf::from(command));
        let mut cmd = if is_cmd_wrapper(&resolved_command) {
            let mut wrapper = Command::new("cmd");
            wrapper.arg("/C").arg(&resolved_command);
            wrapper
        } else {
            Command::new(&resolved_command)
        };
        cmd.args(args)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        for (k, v) in env {
            cmd.env(k, v);
        }

        let mut child = cmd.spawn()?;
        let stdin = child
            .stdin
            .take()
            .ok_or_else(|| anyhow!("Failed to capture stdin"))?;
        let stdout = child
            .stdout
            .take()
            .ok_or_else(|| anyhow!("Failed to capture stdout"))?;
        let stderr = child
            .stderr
            .take()
            .ok_or_else(|| anyhow!("Failed to capture stderr"))?;
        let stderr_lines = Arc::new(Mutex::new(VecDeque::with_capacity(16)));
        let stderr_task = spawn_stderr_drain(stderr, Arc::clone(&stderr_lines));

        Ok(Self {
            _child: child,
            stdin,
            stdout: BufReader::new(stdout),
            framing,
            stderr_lines,
            _stderr_task: stderr_task,
        })
    }

    pub async fn request<P: Serialize, R: for<'de> Deserialize<'de>>(
        &mut self,
        id: u64,
        method: &str,
        params: Option<P>,
    ) -> Result<R> {
        let req = JsonRpcRequest {
            jsonrpc: "2.0".to_string(),
            id: JsonRpcId::Number(id),
            method: method.to_string(),
            params: params.map(serde_json::to_value).transpose()?,
        };

        self.write_message(&req).await?;

        loop {
            let payload = self.read_message_payload().await?;
            let value: JsonValue = serde_json::from_slice(&payload).map_err(|e| {
                anyhow!(
                    "Failed to parse MCP response: {}. Raw: {}",
                    e,
                    String::from_utf8_lossy(&payload)
                )
            })?;

            // Ignore notifications or server-initiated events while waiting for a response.
            if value.get("id").is_none() {
                continue;
            }

            let resp: JsonRpcResponse<R> = serde_json::from_value(value)
                .map_err(|e| anyhow!("Failed to decode MCP response: {}", e))?;

            if let Some(error) = resp.error {
                return Err(anyhow!("MCP Error ({}): {}", error.code, error.message));
            }

            return resp
                .result
                .ok_or_else(|| anyhow!("Missing result in MCP response"));
        }
    }

    pub async fn notify<P: Serialize>(&mut self, method: &str, params: Option<P>) -> Result<()> {
        let notification = JsonRpcNotification {
            jsonrpc: "2.0".to_string(),
            method: method.to_string(),
            params: params.map(serde_json::to_value).transpose()?,
        };

        self.write_message(&notification).await
    }

    pub async fn initialize(&mut self, id: u64) -> Result<()> {
        let params = serde_json::json!({
            "protocolVersion": "2024-11-05",
            "capabilities": {},
            "clientInfo": { "name": "hematite", "version": env!("CARGO_PKG_VERSION") }
        });
        let _: JsonValue = self.request(id, "initialize", Some(params)).await?;
        self.notify("notifications/initialized", Some(serde_json::json!({})))
            .await?;
        Ok(())
    }

    pub async fn list_tools(&mut self, id: u64) -> Result<Vec<McpTool>> {
        let res: McpListToolsResult = self.request(id, "tools/list", None::<()>).await?;
        Ok(res.tools)
    }

    pub async fn call_tool(
        &mut self,
        id: u64,
        name: &str,
        arguments: JsonValue,
    ) -> Result<McpCallToolResult> {
        let params = serde_json::json!({
            "name": name,
            "arguments": arguments
        });
        self.request(id, "tools/call", Some(params)).await
    }

    pub async fn shutdown(mut self) {
        let _ = self._child.kill().await;
        self._stderr_task.abort();
    }

    pub fn stderr_summary(&self) -> Option<String> {
        let lines = self.stderr_lines.lock().ok()?;
        if lines.is_empty() {
            None
        } else {
            Some({
                let cap = lines.iter().map(|l| l.len()).sum::<usize>()
                    + lines.len().saturating_sub(1) * 3;
                let mut out = String::with_capacity(cap);
                for (i, line) in lines.iter().enumerate() {
                    if i > 0 {
                        out.push_str(" | ");
                    }
                    out.push_str(line);
                }
                out
            })
        }
    }

    async fn write_message<T: Serialize>(&mut self, message: &T) -> Result<()> {
        let payload = serde_json::to_vec(message)?;
        match self.framing {
            McpFraming::NewlineDelimited => {
                self.stdin.write_all(&payload).await?;
                self.stdin.write_all(b"\n").await?;
            }
            McpFraming::ContentLength => {
                let header = format!("Content-Length: {}\r\n\r\n", payload.len());
                self.stdin.write_all(header.as_bytes()).await?;
                self.stdin.write_all(&payload).await?;
            }
        }
        self.stdin.flush().await?;
        Ok(())
    }

    async fn read_message_payload(&mut self) -> Result<Vec<u8>> {
        match self.framing {
            McpFraming::NewlineDelimited => {
                let mut line = String::new();
                self.stdout.read_line(&mut line).await?;
                if line.is_empty() {
                    return Err(anyhow!("MCP server closed connection unexpectedly"));
                }
                Ok(line.into_bytes())
            }
            McpFraming::ContentLength => {
                let mut first_line = String::new();
                self.stdout.read_line(&mut first_line).await?;
                if first_line.is_empty() {
                    return Err(anyhow!("MCP server closed connection unexpectedly"));
                }

                if !first_line.starts_with("Content-Length:") {
                    return Ok(first_line.into_bytes());
                }

                let content_length = first_line["Content-Length:".len()..]
                    .trim()
                    .parse::<usize>()
                    .map_err(|e| anyhow!("Invalid MCP Content-Length header: {}", e))?;

                loop {
                    let mut header_line = String::new();
                    self.stdout.read_line(&mut header_line).await?;
                    if header_line.is_empty() {
                        return Err(anyhow!(
                            "MCP server closed connection while reading headers"
                        ));
                    }
                    if header_line == "\r\n" || header_line == "\n" {
                        break;
                    }
                }

                let mut payload = vec![0_u8; content_length];
                self.stdout.read_exact(&mut payload).await?;
                Ok(payload)
            }
        }
    }
}

fn spawn_stderr_drain(
    stderr: ChildStderr,
    stderr_lines: Arc<Mutex<VecDeque<String>>>,
) -> JoinHandle<()> {
    tokio::spawn(async move {
        let mut reader = BufReader::new(stderr);

        loop {
            let mut line = String::new();
            match reader.read_line(&mut line).await {
                Ok(0) | Err(_) => break,
                Ok(_) => {
                    let trimmed = line.trim();
                    if trimmed.is_empty() {
                        continue;
                    }

                    if let Ok(mut lines) = stderr_lines.lock() {
                        lines.push_back(trimmed.to_string());
                        while lines.len() > 20 {
                            lines.pop_front();
                        }
                    }
                }
            }
        }
    })
}

#[cfg(windows)]
fn resolve_command_path(command: &str) -> Option<PathBuf> {
    let candidate = PathBuf::from(command);
    let has_extension = Path::new(command).extension().is_some();
    if candidate.is_absolute() || command.contains('\\') || command.contains('/') {
        if !has_extension {
            for ext in [".exe", ".cmd", ".bat", ".com"] {
                let with_ext = PathBuf::from(format!("{command}{ext}"));
                if with_ext.exists() {
                    return Some(with_ext);
                }
            }
        }
        if candidate.exists() {
            return Some(candidate);
        }
        return None;
    }

    let path_var = std::env::var_os("PATH")?;
    for dir in std::env::split_paths(&path_var) {
        if !has_extension {
            for ext in [".exe", ".cmd", ".bat", ".com"] {
                let with_ext = dir.join(format!("{command}{ext}"));
                if with_ext.exists() {
                    return Some(with_ext);
                }
            }
        }
        let direct = dir.join(command);
        if direct.exists() {
            return Some(direct);
        }
    }

    None
}

#[cfg(not(windows))]
fn resolve_command_path(command: &str) -> Option<PathBuf> {
    Some(PathBuf::from(command))
}

#[cfg(windows)]
fn is_cmd_wrapper(path: &Path) -> bool {
    matches!(
        path.extension().and_then(|ext| ext.to_str()).map(|ext| ext.to_ascii_lowercase()),
        Some(ext) if ext == "cmd" || ext == "bat"
    )
}

#[cfg(not(windows))]
fn is_cmd_wrapper(_path: &Path) -> bool {
    false
}