Metadata-Version: 2.4
Name: linux-health-mcp
Version: 0.1.0
Summary: Read-only Linux health tools for AI agents (MCP), with fixture hosts, tests and a sandbox
Keywords: mcp,model-context-protocol,linux,monitoring,health-check,sre,ai-agents
Author: afsarctg
Author-email: afsarctg <84567138+afsarctg@users.noreply.github.com>
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: System Administrators
Classifier: Intended Audience :: Developers
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: System :: Monitoring
Classifier: Topic :: System :: Systems Administration
Requires-Dist: mcp>=2.3.0
Requires-Dist: pydantic>=2.14.4
Requires-Python: >=3.12
Project-URL: Homepage, https://github.com/afsarctg/linux-health-mcp
Project-URL: Repository, https://github.com/afsarctg/linux-health-mcp
Project-URL: Issues, https://github.com/afsarctg/linux-health-mcp/issues
Project-URL: Changelog, https://github.com/afsarctg/linux-health-mcp/releases
Description-Content-Type: text/markdown

# linux-health-mcp

<!-- mcp-name: io.github.afsarctg/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_excerpts` field, 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 by `make_fixtures.py` in 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.py` gives 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_AUDIT` set, 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.sh` runs 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
```bash
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`.

## 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 study `docs/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.sh` 10/10 checks, negative-tested (`docs/kubernetes.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.
