fast-mcp-ssh 0.4.3

Fast MCP SSH server with persistent PTY sessions, SFTP, and AI-first tool surface
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
//! Host introspection tools: `facts`, `sys`, `svc`.
//!
//! Every probe here runs a machine-readable invocation rather than the
//! human-facing one. `df -PT -B1` instead of `df -h`, `systemctl show
//! --property=...` instead of `systemctl status`, `journalctl -o json`
//! instead of the pager format. Column-aligned human output is what models
//! mis-parse, and re-emitting it costs tokens that carry no information.

use std::collections::BTreeMap;

use rmcp::{
    ErrorData as McpError, RoleServer, handler::server::wrapper::Parameters, model::*, schemars,
    service::RequestContext, tool, tool_router,
};
use serde::Deserialize;

use crate::audit::AuditRecord;
use crate::errors::SshError;
use crate::output::Toon;
use crate::server::SshServer;
use crate::session::exec;
use crate::tools::{clamp_timeout, shell_quote, text};

/// Probe script for `facts`. POSIX sh only: it has to run under dash, ash and
/// busybox, not just bash. Emits `key=value` lines and never fails the call,
/// so a missing tool yields an absent key rather than a non-zero exit.
pub(crate) const FACTS_PROBE: &str = r#"
p() { printf '%s=%s\n' "$1" "$2"; }
p os "$(uname -s 2>/dev/null)"
p kernel "$(uname -r 2>/dev/null)"
p arch "$(uname -m 2>/dev/null)"
p hostname "$(uname -n 2>/dev/null)"
if [ -r /etc/os-release ]; then
  . /etc/os-release 2>/dev/null
  p distro "${ID:-}"
  p distro_version "${VERSION_ID:-}"
fi
[ -r /proc/uptime ] && p uptime_s "$(cut -d' ' -f1 /proc/uptime 2>/dev/null)"
p cpus "$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null)"
if [ -r /proc/meminfo ]; then
  p mem_total_kb "$(awk '/^MemTotal:/{print $2}' /proc/meminfo 2>/dev/null)"
  p mem_avail_kb "$(awk '/^MemAvailable:/{print $2}' /proc/meminfo 2>/dev/null)"
fi
p root_free_kb "$(df -Pk / 2>/dev/null | awk 'NR==2{print $4}')"
if [ -d /run/systemd/system ]; then p init systemd
elif [ -d /run/openrc ]; then p init openrc
elif [ -x /sbin/init ]; then p init sysv
fi
for m in apt-get dnf yum apk pacman zypper brew; do
  command -v "$m" >/dev/null 2>&1 && { p pkg_mgr "$m"; break; }
done
p shell "$(basename "${SHELL:-sh}" 2>/dev/null)"
if [ -f /.dockerenv ]; then p container docker
elif grep -qi microsoft /proc/sys/kernel/osrelease 2>/dev/null; then p container wsl
elif grep -qa 'lxc\|docker\|containerd' /proc/1/cgroup 2>/dev/null; then p container yes
else p container none
fi
if sudo -n true >/dev/null 2>&1; then p sudo_nopasswd yes; else p sudo_nopasswd no; fi
h=
# Every name another tool gates on has to be in this list, or that tool reads
# the absence as "not installed" and refuses on a host that has it: `sys
# what=net` did exactly that for `ss`, on every host, and `shot` never picked
# `spectacle`.
for c in rg rsync tmux screen python3 node jq git docker podman systemctl ss curl wget tar zstd sha256sum grim scrot import gnome-screenshot spectacle; do
  command -v "$c" >/dev/null 2>&1 && h="$h,$c"
done
p has "${h#,}"
"#;

/// Cached per-host profile. Every other tool that has to pick between two
/// backends (ripgrep or grep, docker or podman, which screenshot binary
/// exists) reads this instead of probing again.
#[derive(Debug, Clone, Default)]
pub struct HostFacts {
    pub fields: BTreeMap<String, String>,
}

impl HostFacts {
    fn parse(raw: &str) -> Self {
        let mut fields = BTreeMap::new();
        for line in raw.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }
            if let Some((k, v)) = line.split_once('=') {
                let v = v.trim();
                if !v.is_empty() {
                    fields.insert(k.trim().to_string(), v.to_string());
                }
            }
        }
        Self { fields }
    }

    pub fn get(&self, key: &str) -> Option<&str> {
        self.fields.get(key).map(|s| s.as_str())
    }

    /// True when the remote has the named binary on `PATH`, per the probe.
    pub fn has(&self, tool: &str) -> bool {
        self.get("has")
            .map(|list| list.split(',').any(|t| t == tool))
            .unwrap_or(false)
    }
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct FactsArgs {
    /// Host alias. Omit if a default_host is configured.
    #[serde(default)]
    pub host: Option<String>,
    /// Re-probe instead of returning the cached profile.
    #[serde(default)]
    pub refresh: Option<bool>,
}

#[derive(Debug, Clone, Copy, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum SysView {
    /// Processes by CPU share.
    Ps,
    /// Filesystem usage in bytes.
    Df,
    /// Memory and swap from /proc/meminfo.
    Mem,
    /// Listening TCP and UDP sockets with owning process.
    Net,
    /// Directory sizes under `path`, largest first.
    Du,
    /// Logged-in users.
    Who,
    /// Load average and PSI pressure stalls.
    Load,
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct SysArgs {
    /// Host alias. Omit if a default_host is configured.
    #[serde(default)]
    pub host: Option<String>,
    /// Which view to return.
    pub what: SysView,
    /// Row cap for ps/du/who. Default 15.
    #[serde(default)]
    pub top: Option<u32>,
    /// Directory for `du`. Default "/".
    #[serde(default)]
    pub path: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, schemars::JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum SvcAction {
    Status,
    Logs,
    Start,
    Stop,
    Restart,
    Reload,
    Enable,
    Disable,
    /// List units in a failed state. `unit` is ignored.
    Failed,
}

impl SvcAction {
    fn is_mutating(self) -> bool {
        matches!(
            self,
            SvcAction::Start
                | SvcAction::Stop
                | SvcAction::Restart
                | SvcAction::Reload
                | SvcAction::Enable
                | SvcAction::Disable
        )
    }

    fn verb(self) -> &'static str {
        match self {
            SvcAction::Start => "start",
            SvcAction::Stop => "stop",
            SvcAction::Restart => "restart",
            SvcAction::Reload => "reload",
            SvcAction::Enable => "enable",
            SvcAction::Disable => "disable",
            _ => "",
        }
    }
}

#[derive(Debug, Deserialize, schemars::JsonSchema)]
pub struct SvcArgs {
    /// Host alias. Omit if a default_host is configured.
    #[serde(default)]
    pub host: Option<String>,
    /// systemd unit name. Required for every action except `failed`.
    #[serde(default)]
    pub unit: Option<String>,
    /// What to do. Default `status`.
    #[serde(default)]
    pub action: Option<SvcAction>,
    /// Journal lines for `logs`. Default 50, max 500.
    #[serde(default)]
    pub lines: Option<u32>,
}

/// Properties pulled by `svc status`. `systemctl show --value` returns them
/// one per line in this exact order, with an empty line for anything unset,
/// so the parse is positional and needs no key matching.
const SVC_PROPS: &[&str] = &[
    "LoadState",
    "ActiveState",
    "SubState",
    "UnitFileState",
    "MainPID",
    "MemoryCurrent",
    "TasksCurrent",
    "ActiveEnterTimestamp",
    "Result",
];

/// Rejects unit names that could break out of the shell word they are
/// interpolated into. systemd unit names use a narrow character set, so this
/// costs nothing legitimate.
fn validate_unit(unit: &str) -> Result<(), McpError> {
    if unit.is_empty() || unit.len() > 256 {
        return Err(SshError::Config("unit name empty or too long".into()).into_mcp());
    }
    let ok = unit
        .chars()
        .all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.' | '@' | ':' | '\\'));
    if !ok {
        return Err(SshError::Config(format!(
            "refusing unit name with shell metacharacters: {unit}"
        ))
        .into_mcp());
    }
    Ok(())
}

#[tool_router(router = ops_router, vis = "pub")]
impl SshServer {
    #[tool(
        description = "Cached host profile: os, kernel, cpus, memory, disk, init, package manager, container kind, sudo, installed tools. Run once instead of a dozen probe execs.",
        annotations(
            title = "Facts",
            read_only_hint = true,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = true
        )
    )]
    async fn facts(
        &self,
        Parameters(args): Parameters<FactsArgs>,
    ) -> Result<CallToolResult, McpError> {
        let host_name = self.resolve_host(args.host)?;
        let refresh = args.refresh.unwrap_or(false);
        let facts = self.host_facts(&host_name, refresh).await?;

        let mut t = Toon::new();
        t.field("host", host_name.as_str());
        for (k, v) in &facts.fields {
            if k == "has" {
                continue;
            }
            t.field(k, v.as_str());
        }
        if let Some(h) = facts.get("has") {
            t.field("has", h);
        }
        Ok(text(t.into_string()))
    }

    #[tool(
        description = "Structured system state: ps, df, mem, net, du, who, load. Parsed tables, not the human output of top/free.",
        annotations(
            title = "Sys",
            read_only_hint = true,
            destructive_hint = false,
            idempotent_hint = true,
            open_world_hint = true
        )
    )]
    async fn sys(&self, Parameters(args): Parameters<SysArgs>) -> Result<CallToolResult, McpError> {
        let host_name = self.resolve_host(args.host)?;
        let top = args.top.unwrap_or(15).clamp(1, 200);
        let session = self
            .pool
            .get_or_connect(&host_name, None)
            .await
            .map_err(|e| e.into_mcp())?;
        let max_capture = self.cfg().defaults.max_capture_bytes;
        let timeout = clamp_timeout(Some(30));

        let cmd = match args.what {
            SysView::Ps => format!(
                "ps -eo pid,ppid,user,pcpu,pmem,rss,etimes,stat,comm --sort=-pcpu --no-headers 2>/dev/null | head -n {top}"
            ),
            SysView::Df => "df -PT -B1 2>/dev/null | tail -n +2".to_string(),
            SysView::Mem => {
                "awk '/^(MemTotal|MemFree|MemAvailable|Buffers|Cached|SwapTotal|SwapFree|Dirty):/{print $1, $2}' /proc/meminfo 2>/dev/null"
                    .to_string()
            }
            SysView::Net => {
                // Alpine ships without iproute2, and `ss` missing looks exactly
                // like "nothing is listening" once stderr is swallowed. Say so
                // instead of returning an empty table.
                if !self.host_facts(&host_name, false).await?.has("ss") {
                    return Err(SshError::Config(format!(
                        "`ss` is not installed on '{host_name}', so the net view has no source \
                         (Alpine: apk add iproute2). Use exec with netstat if that is all the host has."
                    ))
                    .into_mcp());
                }
                "{ ss -ltnpH 2>/dev/null; ss -lunpH 2>/dev/null; } | head -n 200".to_string()
            }
            SysView::Du => {
                let path = args.path.as_deref().unwrap_or("/");
                format!(
                    "du -x -d 1 -B1 {} 2>/dev/null | sort -rn | head -n {top}",
                    shell_quote(path)
                )
            }
            SysView::Who => format!("who -u 2>/dev/null | head -n {top}"),
            SysView::Load => {
                "cat /proc/loadavg 2>/dev/null; for f in cpu io memory; do [ -r /proc/pressure/$f ] && sed \"s/^/$f /\" /proc/pressure/$f; done 2>/dev/null"
                    .to_string()
            }
        };

        let r = exec::exec(&session, &cmd, timeout, max_capture)
            .await
            .map_err(|e| e.into_mcp())?;
        self.audit.write(
            &host_name,
            "sys",
            AuditRecord {
                cmd: Some(&cmd),
                exit_code: Some(r.exit_code),
                bytes_out: Some(r.stdout_bytes),
                ..Default::default()
            },
        );

        let mut t = Toon::new();
        t.field("host", host_name.as_str());
        match args.what {
            SysView::Ps => {
                let rows = split_columns(&r.stdout, 9);
                t.table_strs(
                    "ps",
                    &[
                        "pid", "ppid", "user", "cpu", "mem", "rss", "secs", "stat", "comm",
                    ],
                    &rows,
                );
            }
            SysView::Df => {
                let rows = split_columns(&r.stdout, 7);
                t.table_strs(
                    "df",
                    &["fs", "type", "bytes", "used", "avail", "pct", "mount"],
                    &rows,
                );
            }
            SysView::Mem => {
                for line in r.stdout.lines() {
                    if let Some((k, v)) = line.trim().split_once(' ') {
                        t.field(k.trim_end_matches(':'), v.trim());
                    }
                }
                t.hint("values in kB");
            }
            SysView::Net => {
                let rows = parse_ss(&r.stdout);
                t.table_strs("listening", &["proto", "local", "process"], &rows);
            }
            SysView::Du => {
                let rows = split_columns(&r.stdout, 2);
                t.table_strs("du", &["bytes", "path"], &rows);
            }
            SysView::Who => {
                let rows = split_columns(&r.stdout, 5);
                t.table_strs("who", &["user", "tty", "date", "time", "idle"], &rows);
            }
            SysView::Load => {
                t.block("load", r.stdout.trim());
            }
        }
        if r.stdout.trim().is_empty() && !r.stderr.trim().is_empty() {
            t.field("stderr", r.stderr.trim());
        }
        Ok(text(t.into_string()))
    }

    #[tool(
        description = "systemd units: status, logs, start, stop, restart, reload, enable, disable, failed. Parsed properties and JSON journal, never pager output.",
        annotations(
            title = "Svc",
            read_only_hint = false,
            destructive_hint = true,
            idempotent_hint = false,
            open_world_hint = true
        )
    )]
    async fn svc(
        &self,
        Parameters(args): Parameters<SvcArgs>,
        ctx: RequestContext<RoleServer>,
    ) -> Result<CallToolResult, McpError> {
        let host_name = self.resolve_host(args.host)?;
        let action = args.action.unwrap_or(SvcAction::Status);
        let lines = args.lines.unwrap_or(50).clamp(1, 500);

        let unit = match (&args.unit, action) {
            (_, SvcAction::Failed) => String::new(),
            (Some(u), _) => {
                validate_unit(u)?;
                u.clone()
            }
            (None, _) => {
                return Err(SshError::Config(
                    "unit is required for every action except 'failed'".into(),
                )
                .into_mcp());
            }
        };

        let cmd = match action {
            SvcAction::Failed => {
                "systemctl list-units --state=failed --no-legend --plain 2>/dev/null".to_string()
            }
            SvcAction::Status => format!(
                "systemctl show {} --property={} --value 2>/dev/null",
                shell_quote(&unit),
                SVC_PROPS.join(",")
            ),
            SvcAction::Logs => format!(
                "journalctl -u {} -n {lines} -o json --no-pager 2>/dev/null",
                shell_quote(&unit)
            ),
            a => format!("systemctl {} {}", a.verb(), shell_quote(&unit)),
        };

        // Mutating actions go through the guard chain like any other command,
        // so `systemctl stop` still trips the default confirm pattern.
        if action.is_mutating()
            && let Err(e) = self.run_guards(&host_name, &cmd, &ctx).await
        {
            self.audit.write(
                &host_name,
                "svc",
                AuditRecord::blocked(&cmd, &e.to_string()),
            );
            return Err(e.into_mcp());
        }

        let session = self
            .pool
            .get_or_connect(&host_name, None)
            .await
            .map_err(|e| e.into_mcp())?;
        let max_capture = self.cfg().defaults.max_capture_bytes;
        let timeout = clamp_timeout(Some(60));

        // Capture the pre-state so a restart that lands in `failed` can be
        // reported as such without a second round trip from the caller.
        let before = if action == SvcAction::Restart || action == SvcAction::Start {
            unit_active_state(&session, &unit, max_capture).await
        } else {
            None
        };

        let r = exec::exec(&session, &cmd, timeout, max_capture)
            .await
            .map_err(|e| e.into_mcp())?;
        self.audit.write(
            &host_name,
            "svc",
            AuditRecord {
                cmd: Some(&cmd),
                exit_code: Some(r.exit_code),
                bytes_out: Some(r.stdout_bytes),
                ..Default::default()
            },
        );

        let mut t = Toon::new();
        t.field("host", host_name.as_str());
        match action {
            SvcAction::Failed => {
                let rows = split_columns(&r.stdout, 4);
                if rows.is_empty() {
                    t.field("failed", "none");
                } else {
                    t.table_strs("failed", &["unit", "load", "active", "sub"], &rows);
                }
            }
            SvcAction::Status => {
                t.field("unit", unit.as_str());
                for (name, value) in SVC_PROPS.iter().zip(r.stdout.lines()) {
                    let v = value.trim();
                    if !v.is_empty() && v != "[not set]" {
                        t.field(&camel_to_snake(name), v);
                    }
                }
            }
            SvcAction::Logs => {
                let rows = parse_journal_json(&r.stdout);
                t.table_strs("lines", &["ts", "prio", "msg"], &rows);
            }
            _ => {
                t.field("unit", unit.as_str());
                t.field("action", action.verb());
                t.field("exit", r.exit_code);
                // A restart that succeeds at the systemctl level but lands in
                // `failed` is the most common follow-up question. Answer it
                // here instead of making the caller ask.
                if let Some(state) = unit_active_state(&session, &unit, max_capture).await {
                    t.field("active", state.as_str());
                    if let Some(prev) = before {
                        t.field("active_before", prev.as_str());
                    }
                    if state == "failed" {
                        let tail = format!(
                            "journalctl -u {} -n 30 -o cat --no-pager 2>/dev/null",
                            shell_quote(&unit)
                        );
                        if let Ok(j) = exec::exec(&session, &tail, timeout, max_capture).await {
                            t.block("journal_tail", j.stdout.trim());
                        }
                    }
                }
                if !r.stderr.trim().is_empty() {
                    t.field("stderr", r.stderr.trim());
                }
            }
        }
        Ok(text(t.into_string()))
    }
}

/// One `systemctl show` round trip for the single property worth polling
/// after a mutating action. Returns `None` when systemd is not the init.
async fn unit_active_state(
    session: &crate::session::Session,
    unit: &str,
    max_capture: usize,
) -> Option<String> {
    let cmd = format!(
        "systemctl show {} --property=ActiveState --value 2>/dev/null",
        shell_quote(unit)
    );
    let r = exec::exec(
        session,
        &cmd,
        std::time::Duration::from_secs(15),
        max_capture,
    )
    .await
    .ok()?;
    let s = r.stdout.trim();
    if s.is_empty() {
        None
    } else {
        Some(s.to_string())
    }
}

/// Splits whitespace-separated output into rows of `cols` fields, with the
/// last column absorbing the remaining whitespace so paths and command lines
/// survive intact. Short rows are padded so the table stays rectangular.
fn split_columns(out: &str, cols: usize) -> Vec<Vec<String>> {
    out.lines()
        .map(str::trim)
        .filter(|l| !l.is_empty())
        .map(|line| {
            let head: Vec<String> = line
                .split_whitespace()
                .take(cols.saturating_sub(1))
                .map(str::to_string)
                .collect();
            let rest: String = line
                .split_whitespace()
                .skip(head.len())
                .collect::<Vec<_>>()
                .join(" ");
            let mut fields = head;
            if !rest.is_empty() {
                fields.push(rest);
            }
            while fields.len() < cols {
                fields.push("-".to_string());
            }
            fields.truncate(cols);
            fields
        })
        .collect()
}

/// `ss -ltnpH` emits `State Recv-Q Send-Q Local Peer Process`. Only the
/// address and the owning process carry information for a caller asking what
/// is listening.
fn parse_ss(out: &str) -> Vec<Vec<String>> {
    out.lines()
        .map(str::trim)
        .filter(|l| !l.is_empty())
        .filter_map(|line| {
            let f: Vec<&str> = line.split_whitespace().collect();
            let local = f.get(3)?;
            let proto = if line.contains("UNCONN") {
                "udp"
            } else {
                "tcp"
            };
            let process = f
                .get(5)
                .copied()
                .unwrap_or("-")
                .trim_start_matches("users:((")
                .trim_end_matches("))");
            Some(vec![
                proto.to_string(),
                (*local).to_string(),
                process.to_string(),
            ])
        })
        .collect()
}

/// journald emits about thirty fields per entry. Three of them answer the
/// question the caller actually asked.
fn parse_journal_json(out: &str) -> Vec<Vec<String>> {
    out.lines()
        .filter(|l| !l.trim().is_empty())
        .filter_map(|line| {
            let v: serde_json::Value = serde_json::from_str(line).ok()?;
            let ts = v
                .get("__REALTIME_TIMESTAMP")
                .and_then(|t| t.as_str())
                .and_then(|t| t.parse::<i64>().ok())
                .map(format_epoch_us)
                .unwrap_or_else(|| "-".into());
            let prio = v
                .get("PRIORITY")
                .and_then(|p| p.as_str())
                .unwrap_or("-")
                .to_string();
            let msg = match v.get("MESSAGE") {
                Some(serde_json::Value::String(s)) => s.clone(),
                // journald returns a byte array when the message is not valid
                // UTF-8. Rendering it lossily beats dropping the line.
                Some(serde_json::Value::Array(a)) => String::from_utf8_lossy(
                    &a.iter()
                        .filter_map(|b| b.as_u64().map(|n| n as u8))
                        .collect::<Vec<u8>>(),
                )
                .into_owned(),
                _ => return None,
            };
            Some(vec![ts, prio, msg])
        })
        .collect()
}

/// Microseconds since the epoch to RFC 3339.
fn format_epoch_us(us: i64) -> String {
    let secs = us / 1_000_000;
    time::OffsetDateTime::from_unix_timestamp(secs)
        .ok()
        .and_then(|d| {
            d.format(&time::format_description::well_known::Rfc3339)
                .ok()
        })
        .unwrap_or_else(|| secs.to_string())
}

/// `ActiveState` to `active_state`, `MainPID` to `main_pid`. An underscore
/// goes in only at a lower-to-upper boundary or before the final capital of
/// an acronym run, so `PID` does not become `p_i_d`.
fn camel_to_snake(s: &str) -> String {
    let chars: Vec<char> = s.chars().collect();
    let mut out = String::with_capacity(s.len() + 4);
    for (i, &c) in chars.iter().enumerate() {
        if c.is_ascii_uppercase() && i != 0 {
            let prev_lower = chars[i - 1].is_ascii_lowercase() || chars[i - 1].is_ascii_digit();
            let next_lower = chars.get(i + 1).is_some_and(|n| n.is_ascii_lowercase());
            if prev_lower || next_lower {
                out.push('_');
            }
        }
        out.push(c.to_ascii_lowercase());
    }
    out
}

impl SshServer {
    /// Returns the cached profile for `host`, probing once when absent or
    /// when `refresh` is set. Shared by every tool that has to choose a
    /// backend.
    pub(crate) async fn host_facts(
        &self,
        host: &str,
        refresh: bool,
    ) -> Result<HostFacts, McpError> {
        if !refresh && let Some(f) = self.facts_cache.get(host) {
            return Ok(f.clone());
        }
        let session = self
            .pool
            .get_or_connect(host, None)
            .await
            .map_err(|e| e.into_mcp())?;
        let max_capture = self.cfg().defaults.max_capture_bytes;
        let r = exec::exec(
            &session,
            FACTS_PROBE,
            std::time::Duration::from_secs(30),
            max_capture,
        )
        .await
        .map_err(|e| e.into_mcp())?;
        let facts = HostFacts::parse(&r.stdout);
        if facts.fields.is_empty() {
            return Err(SshError::Other(format!(
                "host profile probe returned nothing (exit {}): {}",
                r.exit_code,
                r.stderr.trim()
            ))
            .into_mcp());
        }
        self.facts_cache.insert(host.to_string(), facts.clone());
        Ok(facts)
    }
}

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

    #[test]
    fn parses_probe_output() {
        let f = HostFacts::parse("os=Linux\nkernel=6.1.0\nhas=rg,jq\nempty=\n\ndistro=debian\n");
        assert_eq!(f.get("os"), Some("Linux"));
        assert_eq!(f.get("distro"), Some("debian"));
        assert_eq!(f.get("empty"), None);
        assert!(f.has("rg"));
        assert!(f.has("jq"));
        assert!(!f.has("r"));
        assert!(!f.has("docker"));
    }

    #[test]
    fn probe_value_may_contain_equals() {
        let f = HostFacts::parse("cmdline=a=b=c\n");
        assert_eq!(f.get("cmdline"), Some("a=b=c"));
    }

    #[test]
    fn splits_columns_keeping_tail() {
        let rows = split_columns("1 2 root 0.5 0.1 100 20 S my command here\n", 9);
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].len(), 9);
        assert_eq!(rows[0][8], "my command here");
    }

    #[test]
    fn splits_columns_pads_short_rows() {
        let rows = split_columns("only two\n", 5);
        assert_eq!(rows[0], vec!["only", "two", "-", "-", "-"]);
    }

    #[test]
    fn splits_columns_handles_runs_of_spaces() {
        let rows = split_columns("a    b     c\n", 3);
        assert_eq!(rows[0], vec!["a", "b", "c"]);
    }

    #[test]
    fn parses_ss_listening() {
        let out = "LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:((\"sshd\",pid=1,fd=3))\n\
                   UNCONN 0 0 0.0.0.0:68 0.0.0.0:* users:((\"dhclient\",pid=2,fd=6))\n";
        let rows = parse_ss(out);
        assert_eq!(rows.len(), 2);
        assert_eq!(rows[0][0], "tcp");
        assert_eq!(rows[0][1], "0.0.0.0:22");
        assert!(rows[0][2].contains("sshd"));
        assert_eq!(rows[1][0], "udp");
    }

    #[test]
    fn parses_journal_json_lines() {
        let out = "{\"__REALTIME_TIMESTAMP\":\"1700000000000000\",\"PRIORITY\":\"6\",\"MESSAGE\":\"started\"}\n\
                   {\"PRIORITY\":\"3\",\"MESSAGE\":\"boom\"}\n\
                   not json\n";
        let rows = parse_journal_json(out);
        assert_eq!(rows.len(), 2);
        assert!(rows[0][0].starts_with("2023-11-14"));
        assert_eq!(rows[0][2], "started");
        assert_eq!(rows[1][0], "-");
        assert_eq!(rows[1][2], "boom");
    }

    #[test]
    fn parses_journal_binary_message() {
        let out = r#"{"PRIORITY":"6","MESSAGE":[104,105]}"#;
        let rows = parse_journal_json(out);
        assert_eq!(rows[0][2], "hi");
    }

    #[test]
    fn rejects_unit_with_metacharacters() {
        assert!(validate_unit("nginx.service").is_ok());
        assert!(validate_unit("getty@tty1.service").is_ok());
        assert!(validate_unit("a; rm -rf /").is_err());
        assert!(validate_unit("$(id)").is_err());
        assert!(validate_unit("").is_err());
    }

    /// Extract the binary names the probe actually looks for, so a test can
    /// assert a gate and the probe agree.
    fn probed_binaries() -> Vec<&'static str> {
        FACTS_PROBE
            .lines()
            .find(|l| l.trim_start().starts_with("for c in "))
            .expect("probe has a `for c in` loop")
            .trim()
            .trim_start_matches("for c in ")
            .trim_end_matches("; do")
            .split_whitespace()
            .collect()
    }

    #[test]
    fn every_gated_binary_is_actually_probed() {
        // `sys what=net` gates on `has("ss")`. `ss` was missing from the probe,
        // so the view refused on every host, installed or not.
        let probed = probed_binaries();
        assert!(probed.contains(&"ss"), "probed: {probed:?}");
    }

    #[test]
    fn probe_reports_a_binary_it_looks_for() {
        let f = HostFacts::parse("has=ss,jq\n");
        assert!(f.has("ss"));
        assert!(!f.has("spectacle"));
    }

    #[test]
    fn camel_to_snake_property_names() {
        assert_eq!(camel_to_snake("ActiveState"), "active_state");
        assert_eq!(camel_to_snake("MainPID"), "main_pid");
        assert_eq!(camel_to_snake("SubState"), "sub_state");
        assert_eq!(camel_to_snake("Result"), "result");
    }
}