haematite-cli 0.21.8

The haematite command: serve a store, its door and Studio, answer MCP, query with jq, and host the store's assistant seat
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
#![forbid(unsafe_code)]
//! Exercise the actual stdio child against HTTP peers, including startup probes.

use std::io::{BufRead, BufReader, Write};
use std::net::TcpListener;
use std::process::{Output, Stdio};
use std::sync::{Arc, Mutex, mpsc};
use std::time::Duration;

use serde_json::{Value, json};

#[path = "mcp_remote/fixtures.rs"]
mod fixtures;
use fixtures::*;

fn call() -> String {
    json!({"jsonrpc":"2.0","id":7,"method":"tools/call","params":{"name":"put_records","arguments":{}}}).to_string() + "\n"
}

fn short_command(url: &str, issuer: &str) -> std::process::Command {
    let command = command(url, issuer);
    // Replace the existing timeout without introducing a repeated flag.
    let args: Vec<_> = command.get_args().map(std::ffi::OsStr::to_owned).collect();
    let mut replacement = std::process::Command::new(command.get_program());
    replacement.args(&args[..args.len() - 1]).arg("200");
    replacement.env("PLUG001_RUN_PASS", KEY);
    replacement
}

#[test]
fn remote_completed_write_outlives_the_send_timeout() -> TestResult {
    let issuer = lys(&[PASS])?;
    let mcp = Peer::script(|_| {
        // The response deliberately crosses the configured send deadline.
        std::thread::sleep(Duration::from_millis(600));
        Ok(Some(reply(
            200,
            "application/json",
            &json!({"jsonrpc":"2.0","id":7,"result":{"committed":true}}).to_string(),
            "",
        )))
    })?;
    let output = run(&mut short_command(&mcp.url(), &issuer.url()), &call())?;
    let seen = mcp.finish()?;
    issuer.finish()?;
    let answers = messages(&output)?;
    assert_eq!(
        answers[0]["result"],
        json!({"committed":true}),
        "completed write was reported as failed: {}",
        answers[0]
    );
    assert_eq!(seen.len(), 1, "a sent write must not be retried");
    Ok(())
}

fn assert_unknown(output: &Output) -> TestResult {
    let answers = messages(output)?;
    assert_eq!(answers[0]["id"], 7);
    assert_eq!(answers[0]["error"]["code"], -32000);
    assert_eq!(
        answers[0]["error"]["data"]["code"], "haematite_mcp_remote_outcome_unknown",
        "sent write was reported as a failure: {}",
        answers[0]
    );
    assert_eq!(
        answers[0]["error"]["message"],
        "the request reached the server; whether it was carried out is not known; read before retrying"
    );
    Ok(())
}

#[test]
fn remote_closed_status_after_send_is_outcome_unknown() -> TestResult {
    let issuer = lys(&[PASS])?;
    let mcp = Peer::script(|_| Ok(None))?;
    let output = run(&mut command(&mcp.url(), &issuer.url()), &call())?;
    let seen = mcp.finish()?;
    issuer.finish()?;
    assert_unknown(&output)?;
    assert_eq!(seen.len(), 1, "a sent write must not be retried");
    Ok(())
}

#[test]
fn remote_closed_body_after_send_is_outcome_unknown() -> TestResult {
    let issuer = lys(&[PASS])?;
    let mcp = Peer::script(|_| {
        Ok(Some("HTTP/1.1 200 Canned\r\nContent-Type: application/json\r\nContent-Length: 100\r\nConnection: close\r\n\r\n{\"jsonrpc\":".into()))
    })?;
    let output = run(&mut command(&mcp.url(), &issuer.url()), &call())?;
    let seen = mcp.finish()?;
    issuer.finish()?;
    assert_unknown(&output)?;
    assert_eq!(seen.len(), 1, "a sent write must not be retried");
    Ok(())
}

#[test]
fn remote_connect_failure_before_send_is_request_failed() -> TestResult {
    let listener = TcpListener::bind("127.0.0.1:0")?;
    let url = format!("http://{}", listener.local_addr()?);
    let (closed, wait_closed) = mpsc::channel();
    let probe = std::thread::spawn(move || -> std::io::Result<()> {
        let (stream, _) = listener.accept()?;
        assert!(
            read_request(&stream)?.is_none(),
            "startup must only probe TCP"
        );
        drop(listener);
        closed.send(()).map_err(std::io::Error::other)
    });
    let issuer = Peer::script(move |_| {
        wait_closed.recv().map_err(std::io::Error::other)?;
        Ok(Some(reply(
            200,
            "application/json",
            &json!({"pass":PASS,"expires_at":4_000_000_000_u64,"audience":"haematite"}).to_string(),
            "",
        )))
    })?;
    let output = run(&mut command(&url, &issuer.url()), &call())?;
    probe.join().map_err(|_| "probe server panicked")??;
    issuer.finish()?;
    let answers = messages(&output)?;
    assert_eq!(answers[0]["id"], 7);
    assert_eq!(
        answers[0]["error"]["data"]["code"],
        "haematite_mcp_remote_request_failed"
    );
    Ok(())
}

#[test]
fn remote_cancel_arrives_before_the_held_call_is_released() -> TestResult {
    let issuer = lys(&[PASS])?;
    let (held, held_signal) = mpsc::channel();
    let (cancelled, cancel_signal) = mpsc::channel();
    let (release, release_signal) = mpsc::channel();
    let release_signal = Arc::new(Mutex::new(release_signal));
    let mcp = Peer::parallel(move |(head, body)| {
        let request: Value = serde_json::from_str(body).map_err(std::io::Error::other)?;
        match request["method"].as_str() {
            Some("initialize") => Ok(Some(reply(
                200,
                "application/json",
                &json!({"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2025-11-25"}})
                    .to_string(),
                "Mcp-Session-Id: cancel-session\r\n",
            ))),
            Some("tools/call") => {
                held.send(()).map_err(std::io::Error::other)?;
                release_signal
                    .lock()
                    .map_err(|_| std::io::Error::other("release poisoned"))?
                    .recv()
                    .map_err(std::io::Error::other)?;
                Ok(Some(reply(
                    200,
                    "application/json",
                    &json!({"jsonrpc":"2.0","id":7,"result":{"cancelled":true}}).to_string(),
                    "",
                )))
            }
            Some("notifications/cancelled") => {
                cancelled
                    .send(head.clone())
                    .map_err(std::io::Error::other)?;
                Ok(Some(reply(202, "application/json", "", "")))
            }
            Some("ping") => Ok(Some(reply(
                200,
                "application/json",
                &json!({"jsonrpc":"2.0","id":8,"result":{}}).to_string(),
                "",
            ))),
            _ => Err(std::io::Error::other("unexpected method")),
        }
    })?;
    let mut child = command(&mcp.url(), &issuer.url())
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()?;
    let mut input = child.stdin.take().ok_or("stdin missing")?;
    let mut output = BufReader::new(child.stdout.take().ok_or("stdout missing")?);
    input.write_all(b"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\"}\n")?;
    let mut initialize = String::new();
    output.read_line(&mut initialize)?;
    input.write_all(call().as_bytes())?;
    held_signal.recv()?;
    input.write_all(b"{\"jsonrpc\":\"2.0\",\"id\":8,\"method\":\"ping\"}\n{\"jsonrpc\":\"2.0\",\"method\":\"notifications/cancelled\",\"params\":{\"requestId\":7}}\n")?;
    // A missing cancel must release the fixture so the red run can finish.
    let cancel = cancel_signal.recv_timeout(Duration::from_millis(1000));
    release.send(())?;
    drop(input);
    let mut remaining = String::new();
    std::io::Read::read_to_string(&mut output, &mut remaining)?;
    let result = child.wait_with_output()?;
    let seen = mcp.finish()?;
    issuer.finish()?;
    assert!(result.status.success());
    assert!(
        cancel.is_ok(),
        "cancel did not arrive before releasing the held call"
    );
    let cancel_headers = cancel?;
    assert!(
        cancel_headers
            .to_ascii_lowercase()
            .contains("mcp-session-id: cancel-session\r\n")
    );
    assert!(
        cancel_headers
            .to_ascii_lowercase()
            .contains("mcp-protocol-version: 2025-11-25\r\n")
    );
    let responses: Vec<Value> = (initialize + &remaining)
        .lines()
        .map(serde_json::from_str)
        .collect::<Result<_, _>>()?;
    assert_eq!(
        responses
            .iter()
            .map(|value| value["id"].clone())
            .collect::<Vec<_>>(),
        [json!(1), json!(7), json!(8)]
    );
    assert_eq!(seen.len(), 4);
    Ok(())
}

#[test]
fn remote_json_negotiates_protocol_and_echoes_session() -> TestResult {
    let issuer = lys(&[PASS])?;
    let mcp = Peer::new(vec![
        reply(
            200,
            "application/json",
            "{\n\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{\"protocolVersion\":\"2025-11-25\"}}",
            "Mcp-Session-Id: fixture-session\r\n",
        ),
        reply(
            200,
            "application/json",
            "{\"jsonrpc\":\"2.0\",\"id\":7,\"result\":{\"tools\":[]}}",
            "",
        ),
    ])?;
    let input = format!(
        "{{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{{}}}}\n{REQUEST}"
    );
    let output = run(&mut command(&mcp.url(), &issuer.url()), &input)?;
    let seen = mcp.finish()?;
    let minted = issuer.finish()?;
    let answers = messages(&output)?;
    assert_eq!(answers.len(), 2);
    assert_eq!(answers[1]["result"]["tools"], json!([]));
    assert_eq!(minted.len(), 1);
    assert_eq!(seen.len(), 2);
    let head = seen[1].0.to_ascii_lowercase();
    assert!(head.contains("accept: application/json, text/event-stream\r\n"));
    assert!(head.contains("mcp-protocol-version: 2025-11-25\r\n"));
    assert!(head.contains("mcp-session-id: fixture-session\r\n"));
    assert!(seen[0].0.contains(&format!("Bearer {PASS}")));
    Ok(())
}

#[test]
fn remote_sse_emits_each_data_message() -> TestResult {
    let issuer = lys(&[PASS])?;
    let mcp = Peer::new(vec![reply(
        200,
        "text/event-stream; charset=utf-8",
        ": keepalive\r\nevent: message\r\ndata: {\"jsonrpc\":\"2.0\",\"method\":\"notifications/progress\"}\r\n\r\ndata: {\"jsonrpc\":\"2.0\",\"id\":7,\r\ndata: \"result\":{\"tools\":[]}}\r\n\r\n",
        "",
    )])?;
    let output = run(&mut command(&mcp.url(), &issuer.url()), REQUEST)?;
    mcp.finish()?;
    issuer.finish()?;
    let answers = messages(&output)?;
    assert_eq!(answers.len(), 2);
    assert_eq!(answers[1]["id"], 7);
    Ok(())
}

#[test]
fn remote_notification_202_writes_nothing() -> TestResult {
    let issuer = lys(&[PASS])?;
    let mcp = Peer::new(vec![reply(202, "application/json", "", "")])?;
    let output = run(
        &mut command(&mcp.url(), &issuer.url()),
        "{\"jsonrpc\":\"2.0\",\"method\":\"notifications/initialized\"}\n",
    )?;
    let seen = mcp.finish()?;
    issuer.finish()?;
    assert!(messages(&output)?.is_empty());
    assert_eq!(seen.len(), 1);
    Ok(())
}

#[test]
fn remote_401_mints_fresh_pass_and_retries_once() -> TestResult {
    let issuer = lys(&[PASS, "fixture-fresh-pass"])?;
    let mcp = Peer::new(vec![
        reply(
            401,
            "application/json",
            "{\"error\":\"mcp_pass_refused\"}",
            "",
        ),
        reply(
            200,
            "application/json",
            "{\"jsonrpc\":\"2.0\",\"id\":7,\"result\":{}}",
            "",
        ),
    ])?;
    let output = run(&mut command(&mcp.url(), &issuer.url()), REQUEST)?;
    let seen = mcp.finish()?;
    let minted = issuer.finish()?;
    assert_eq!(messages(&output)?[0]["id"], 7);
    assert_eq!(minted.len(), 2);
    assert_eq!(seen.len(), 2);
    assert_eq!(seen[0].1, seen[1].1);
    assert!(seen[1].0.contains("Bearer fixture-fresh-pass"));
    Ok(())
}

#[test]
fn remote_second_401_returns_server_code_for_original_id_without_secrets() -> TestResult {
    let issuer = lys(&[PASS, "fixture-fresh-pass"])?;
    let mcp = Peer::new(vec![
        reply(
            401,
            "application/json",
            "{\"error\":\"mcp_pass_refused\"}",
            "",
        ),
        reply(
            401,
            "application/json",
            &json!({"error":"mcp_sign_in_required", "message":PASS}).to_string(),
            "",
        ),
    ])?;
    let output = run(&mut command(&mcp.url(), &issuer.url()), REQUEST)?;
    let seen = mcp.finish()?;
    let minted = issuer.finish()?;
    let answers = messages(&output)?;
    assert_eq!(answers[0]["id"], 7);
    assert_eq!(answers[0]["error"]["data"]["code"], "mcp_sign_in_required");
    assert!(!String::from_utf8_lossy(&output.stdout).contains(PASS));
    assert_eq!(minted.len(), 2);
    assert_eq!(seen.len(), 2);
    Ok(())
}

fn unanswered() -> std::io::Result<String> {
    let listener = TcpListener::bind("127.0.0.1:0")?;
    Ok(format!("http://{}", listener.local_addr()?))
}

fn startup_refusal(url: &str, issuer: &str, code: &str, address: &str) -> TestResult {
    let mut child = command(url, issuer)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()?;
    let held_stdin = child.stdin.take().ok_or("stdin missing")?;
    let output = child.wait_with_output()?;
    drop(held_stdin);
    assert_eq!(
        output.status.code(),
        Some(78),
        "{}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(output.stdout.is_empty());
    let error = String::from_utf8(output.stderr)?;
    assert!(
        error.contains(&format!("{code}: {address} did not answer")),
        "{error}"
    );
    Ok(())
}

#[test]
fn remote_unanswered_loopback_refuses_before_reading_stdin() -> TestResult {
    let url = unanswered()?;
    startup_refusal(
        &url,
        "https://lys.ablative.com.au",
        "haematite_mcp_remote_unanswered",
        &url,
    )
}

#[test]
fn remote_unanswered_loopback_lys_refuses_before_reading_stdin() -> TestResult {
    let issuer = unanswered()?;
    startup_refusal(
        "https://haematite.ablative.com.au/mcp",
        &issuer,
        "haematite_mcp_lys_unanswered",
        &issuer,
    )
}

#[test]
fn remote_non_loopback_http_is_refused_by_name() -> TestResult {
    let output = run(
        &mut command(
            "http://haematite.ablative.com.au/mcp",
            "https://lys.ablative.com.au",
        ),
        "",
    )?;
    assert_eq!(output.status.code(), Some(78));
    assert!(String::from_utf8(output.stderr)?.contains("haematite_mcp_remote_url_invalid"));
    Ok(())
}

/// Fails while every pass failure is answered `request_failed`: Lys refusing
/// the agent's pass is said by name, as before round 2, so a person can tell
/// "Lys said no" from "the server could not be reached", and the MCP server is
/// never sent the request.
#[test]
fn remote_lys_refusing_the_pass_is_named_and_sends_nothing() -> TestResult {
    let remote = Peer::new(Vec::new())?;
    let issuer = Peer::new(vec![reply(
        403,
        "application/json",
        &json!({"refusal":"agent_retired"}).to_string(),
        "",
    )])?;
    let output = run(&mut command(&remote.url(), &issuer.url()), &call())?;
    issuer.finish()?;
    let sent = remote.finish()?;
    let answers = messages(&output)?;
    assert_eq!(answers[0]["id"], 7);
    assert_eq!(answers[0]["error"]["data"]["code"], "mcp_lys_pass_refused");
    assert!(sent.is_empty(), "the MCP server was sent {sent:?}");
    Ok(())
}