k8s-rca-mcp
README.md
# k8s-rca-mcp
Read-only **Model Context Protocol (MCP)** server and automated **15-scenario chaos evaluation harness** for Kubernetes root-cause analysis—enforcing least-privilege RBAC, secret redaction, prompt-injection quarantine, and OpenTelemetry tracing at **$0 cost**.
## Architecture
```mermaid
flowchart LR
Agent[AI Client: Cursor / Ollama / LangGraph] <-->|JSON-RPC 2.0 stdio MCP| Server[k8s-rca-mcp Server]
Server --> Guard[Security Guardrails: Read-Only Allowlist + Secret Redaction + Prompt-Injection Filter]
Guard --> Analyzer[Grounded RCA Correlation Engine]
Analyzer -->|Least-Privilege ClusterRole: NO secrets, NO write verbs| K8s[(Kubernetes API: Pods, Events, Logs, ReplicaSets)]
Server --> OTel[OpenTelemetry Spans]
```
## Security & Guardrail Model
1. **Zero Secret Access (RBAC + Code Enforcement):** Both `deploy/rbac/readonly-clusterrole.yaml` and `guardrails.enforce_read_only_access()` explicitly allow only `get/list/watch` on non-secret diagnostic resources. Reading `secrets` or invoking any mutating verb (`delete`, `patch`, `apply`) raises `GuardrailViolation` and returns `403 Forbidden` in Kubernetes.
2. **Automatic Secret Redaction:** Scrubs PEM private keys, JWTs, AWS access keys (`AKIA...`), `Bearer` tokens, database URI credentials, and `password=/api_key=` patterns from pod logs and Kubernetes events before any text reaches an LLM.
3. **Prompt-Injection Quarantine:** Detects and neutralizes untrusted instructions embedded in pod logs (e.g., `"ignore previous instructions"`, `"kubectl delete ..."`) while flagging `prompt_injection_quarantined: true` in the response audit metadata.
## Exposed MCP Tools (`tools/list`)
| Tool | Read-Only Scope | Purpose |
|---|---|---|
| `get_pod_diagnostics` | `list pods` | Pod phases, restart counts, waiting/termination reasons & exit codes |
| `get_recent_events` | `list events` | Sanitized Kubernetes Warning/Normal events |
| `get_previous_logs` | `get pods/log` | Previous container logs with credential redaction & injection filtering |
| `get_rollout_history` | `list replicasets` | Recent deployment revisions and container images |
| `diagnose_namespace_incident` | Read-only correlation | Structured, evidence-cited RCA report + safe recommended next steps |
## 15-Scenario Chaos & Adversarial Benchmark (`eval/run_eval.py`)
Covers `OOMKilled`, `CrashLoopBackOff`, `ImagePullBackOff`, `CreateContainerConfigError`, `InitContainerFailure`, `ResourceQuotaExceeded`, `FailedScheduling-PVC`, `FailedScheduling-Resources`, `ExpiredTLSCertificate`, `DNSResolutionFailure`, `NetworkPolicyDrop`, `RBACForbidden`, `LivenessProbeKillLoop`, `ReadinessProbeFailure`, and `PromptInjection + JWT Leak`.
## Quick Start ($0 Local)
```bash
# Run unit, RBAC policy, MCP JSON-RPC, and guardrail tests
PYTHONPATH=src:. python -m unittest discover -s tests -v
# Run 15-scenario evaluation harness and print Markdown scorecard
PYTHONPATH=src:. python -m eval.run_eval
```
## Zero-Cost CI Pipeline (`.github/workflows/ci.yml`)
1. **`unit-and-eval-harness`**: Runs `pytest` (guardrails, RBAC YAML audit, MCP JSON-RPC protocol) and executes all 15 chaos scenarios, uploading `eval-scorecard.md`.
2. **`kind-rbac-and-chaos-e2e`**: Spins up a free `kind` cluster in GitHub Actions, applies `readonly-clusterrole.yaml`, proves via `kubectl auth can-i` that `get secrets` and `delete pods` are denied, deploys real failing workloads (`chaos/manifests/sample-failures.yaml`), and runs live RCA diagnosis against the cluster.
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues