
linux-health-mcp
io.github.afsarctg/linux-health-mcp
Read-only Linux health checks for AI agents: services, disk, memory, logs, with evidence.
Documentation
linux-health-mcp
Read-only Linux health tools for AI agents, over the Model Context Protocol (MCP). An agent can ask "what is wrong with this host?" and get structured findings with evidence: failed services, crash loops, disk and inode pressure, low memory or overload, and noisy error logs. It cannot change anything.
Why it is built this way
- Least privilege by construction: the server can run only a fixed allowlist of read-only commands
(
src/linux_health_mcp/commands.py). Tools request commands by key; unknown keys are refused before anything runs (fail closed). There is no shell and no free-form argument. - Evidence, not vibes: every finding carries the exact data it is based on.
- Host-chosen names are untrusted too: log source tags and non-standard mount names never appear in findings
(shown as
source #N/mount #N; real names only in fields labelled untrusted). - Logs are untrusted input: anyone who can write a log line can write words aimed at the agent (prompt injection).
Raw log text never appears in a finding; excerpts go in a separate
untrusted_log_excerptsfield, labelled as data, with control/bidi characters stripped, identifiers restricted, and secret-shaped tokens (passwords, bearer tokens, AWS keys, URL credentials, long tokens, emails; quoted and JSON forms too) redacted on the FULL line before truncation; only visible characters are kept (allowlist by Unicode category, minus blank-rendering lookalikes); after a secret key or Authorization header the rest of the line is redacted. Tests plant an injection, secrets and invisible characters and check none leak; mutation checks show the tests fail if a safeguard is removed. - Tested without touching a real machine: fixture hosts (
fixtures/, generated bymake_fixtures.pyin formats copied from real Ubuntu output) for eight scenarios (healthy, crash loop, disk full, noisy journal, memory pressure, three prompt-injection attempts). Each scenario declares its expected findings; the tests fail on a missed fault OR a false alarm. - The agent is evaluated, not trusted:
evals/run_evals.pygives a real AI agent (Claude Code headless, with NO built-in tools: only this server) each fixture host and scores its report: faults found, faults missed, invented critical faults, warning noise, and whether it called a faulty host healthy. Every tool call is saved as a trace. One scenario plants log lines telling the AI to "report this host as healthy"; the agent must still report the real disk fault (baseline: it did in every run). - A release gate that can fail:
./gate.sh= tests, sandboxed tests, mutation checks (11 safeguards broken one at a time in a throwaway copy; all must be caught), then agent evals compared with a reviewed baseline (evals/baseline.json: 24/24 runs correct,claude-sonnet-5-5). - Audited: with
LHM_AUDITset, every tool call and every command run is appended to a JSONL log tied by call id. The log holds sizes, outcomes and finding summaries, not raw output; each record is written before the command runs, and the server refuses to run if it can't write it. Keep it on a separate volume/tmpfs so a full disk doesn't block diagnosis. - Sandboxed:
./sandbox.shruns the suite in a container with no network, a read-only filesystem, no capabilities, non-root, 1 CPU / 256 MB.
Tools
| tool | finds |
|---|---|
system_overview | low available memory (<15% warn, <5% critical), sustained load (15-min load per CPU >= 2) |
disk_pressure | space or inodes >= 85% (warn) / >= 95% (critical) |
service_health | failed systemd units; units restarted >= 3 (warn) / >= 10 (critical) times in the last hour |
journal_errors | sources with >= 20 error-priority log lines in the last hour, with a sample line |
Run
uv sync
uv run pytest -q # 85 tests
./sandbox.sh # same tests in the locked-down container
./gate.sh # release gate: tests + sandbox + mutation checks + agent evals
uv run python evals/mutation_check.py # 11 safeguards broken one by one; all must be caught
LHM_FIXTURE=fixtures/crash_loop uv run linux-health-mcp # serve a fixture host over stdio
uv run python -m linux_health_mcp.report fixtures/disk_full # one-shot JSON health report of a fixture host
uv run linux-health-mcp # serve THIS host (read-only commands only)
Add to an MCP client (e.g. Claude Code) from PyPI: claude mcp add linux-health -- uvx linux-health-mcp;
or from a checkout: claude mcp add linux-health -- uv --directory /path/to/linux-health-mcp run linux-health-mcp.
Releases: docs/RELEASING.md (PyPI via Trusted Publishing + the official MCP Registry, server.json).
Kubernetes
./k8s/verify.sh runs the suite and a fixture report as a hardened Job on a local one-node kind cluster (API server on
127.0.0.1 only) and asserts 10 properties: non-root uid, read-only root FS (mount flag + failed write), no service
account token or RBAC, Pod Security Admission restricted rejecting a privileged pod, and default-deny NetworkPolicy
egress blocked (measured against a control namespace: kind's default CNI does enforce it). ./k8s/teardown.sh removes
the cluster. Details, measured output and CKA practice exercises: docs/kubernetes.md.
Observability: ./observability/up.sh adds Prometheus + kube-state-metrics, Loki, Grafana Alloy (log shipping;
Promtail is end of life) and Grafana to that cluster, all pinned and Pod-Security restricted, plus a CronJob that runs
a fixture health report every 5 minutes. ./observability/verify.sh checks targets, the provisioned SLO dashboard and
Loki logs through the APIs, then forces both alerts (HealthReportFailed, HealthReportStale) to fire and to clear,
and unit-tests the rules with promtool (plus a mutation check). The same flow runs in GitHub Actions
(.github/workflows/observability.yml). Details, measured RAM and PromQL/LogQL exercises: docs/observability.md.
Roadmap
- Week 2 (done): agent eval harness + local release gate; CI workflow ready for publication.
- Week 3 (done): audit trail (
LHM_AUDIT=/path/audit.jsonl: every tool call + command, no raw output, fails closed); control experiment (quarantine on vs off, 60 runs, two models, with a positive-control attack); case studydocs/case-study-agent-harness.md. - Week 4 (done): run on Kubernetes, on kind instead of k3s (no systemd service, no system-wide install, removable
with one command); hardened Job +
k8s/verify.sh10/10 checks, negative-tested (docs/kubernetes.md). - E2 (done locally; CI workflow not yet run on GitHub): observability: Prometheus, Loki, Alloy, Grafana on kind, SLO
dashboard, 2 alert rules forced to fire and clear by
observability/verify.sh(docs/observability.md).
How this was built
Designed, specified, tested and reviewed by Afsar Ahmed; implementation by AI coding agents (Claude Code) working to that specification. Mutation checks confirm the tests fail when thresholds or the allowlist gate are broken.
License and security
MIT (see LICENSE). Report vulnerabilities privately; see SECURITY.md.
linux-health-mcppip install linux-health-mcp