import gradio as gr import json import time from mock_engine import run_tool, get_audit_log, MOCK_PODS def fmt_json(data: dict) -> str: return json.dumps(data, indent=2, ensure_ascii=False) # ── Tool functions ──────────────────────────────────────────────────────────── def tool_list_pods(namespace, token): if not token: return "ERROR: Token required", "" result = run_tool("list_pods", {"namespace": namespace}) summary = ( "Total: " + str(result["total"]) + " | Healthy: " + str(result["healthy"]) + " | Crashed: " + str(result["crashed"]) ) if result["attention"]: summary += "\n\nATTENTION: " + ", ".join(result["attention"]) return fmt_json(result), summary def tool_get_pod_status(pod_name, namespace, token): if not token: return "ERROR: Token required" result = run_tool("get_pod_status", { "pod_name": pod_name, "namespace": namespace, }) return fmt_json(result) def tool_read_logs(pod_name, namespace, token, tail_lines): if not token: return "ERROR: Token required" result = run_tool("read_logs", { "pod_name": pod_name, "namespace": namespace, "tail_lines": int(tail_lines), }) return result["logs"] def tool_describe_deployment(deployment_name, namespace, token): if not token: return "ERROR: Token required" result = run_tool("describe_deployment", { "deployment_name": deployment_name, "namespace": namespace, }) output = fmt_json(result) if result.get("diagnosis") == "INTENTIONAL_CRASH": output += ( "\n\n" + "=" * 50 + "\nDIAGNOSIS: INTENTIONAL_CRASH" + "\nRoot cause: 'exit 1' detected in container command." + "\nRestarting alone will NOT fix this." + "\n" + "=" * 50 ) return output def tool_restart_service(pod_name, namespace, token, confirm): if not token: return "ERROR: Token required" result = run_tool("restart_service", { "pod_name": pod_name, "namespace": namespace, "confirm": confirm, }) return fmt_json(result) def run_full_pipeline(namespace, token): if not token: empty = "ERROR: Token required" return empty, empty, empty, empty, empty, empty r1 = run_tool("list_pods", {"namespace": namespace}) time.sleep(0.2) web_pod = next( (p["name"] for p in r1["pods"] if "web-app" in p["name"]), "web-app-f6c49bc8-cxjpf" ) r2 = run_tool("read_logs", { "pod_name": web_pod, "namespace": namespace, "tail_lines": 50, }) time.sleep(0.2) r3 = run_tool("get_pod_status", { "pod_name": web_pod, "namespace": namespace, }) time.sleep(0.2) r4 = run_tool("describe_deployment", { "deployment_name": "web-app", "namespace": namespace, }) time.sleep(0.2) r5 = run_tool("restart_service", { "pod_name": web_pod, "namespace": namespace, "confirm": True, }) logs = get_audit_log() audit = "\n".join([ e["timestamp"] + " | " + e["tool"] + " | success=" + str(e["success"]) for e in logs[-10:] ]) return ( fmt_json(r1), r2["logs"], fmt_json(r3), fmt_json(r4), fmt_json(r5), audit, ) def refresh_audit(): logs = get_audit_log() if not logs: return "No tool calls yet. Run a tool first." return "\n".join([ e["timestamp"] + " | " + e["tool"] + " | ns=" + e["namespace"] + " | success=" + str(e["success"]) for e in logs ]) # ── Gradio UI ───────────────────────────────────────────────────────────────── THEME = gr.themes.Soft( primary_hue="orange", secondary_hue="blue", ) with gr.Blocks(theme=THEME, title="MCP Infra Operator") as demo: gr.Markdown(""" # 🔧 MCP Infra Operator ### Autonomous Kubernetes Remediation via Model Context Protocol > **Mock demo** — Full pipeline simulation without a real cluster or API credits. > Simulates a `CrashLoopBackOff` incident and demonstrates autonomous root cause analysis. [![GitHub](https://img.shields.io/badge/GitHub-MozaarAI--Agentic-181717?logo=github)](https://github.com/MozaarAI-Agentic/Project3-MCP-Infra-Operator) [![MCP](https://img.shields.io/badge/MCP-Protocol-D97757?logo=anthropic)](https://modelcontextprotocol.io) [![Kubernetes](https://img.shields.io/badge/Kubernetes-1.35+-326CE5?logo=kubernetes)](https://kubernetes.io) [![Tests](https://img.shields.io/badge/Tests-40_passed-22C55E)](https://github.com/MozaarAI-Agentic/Project3-MCP-Infra-Operator) """) # ── Auth + Cluster state ────────────────────────────────────────────────── with gr.Row(): with gr.Column(scale=1): gr.Markdown("### 🔑 Authentication") token_input = gr.Textbox( label="MCP Token", placeholder="Enter: mcp-dev-2026", type="password", value="mcp-dev-2026", ) namespace_input = gr.Textbox( label="Kubernetes Namespace", value="default", ) with gr.Column(scale=2): gr.Markdown("### 🖥️ Mock Cluster State") gr.Markdown(""" | Pod | Phase | Ready | Restarts | Status | |-----|-------|-------|----------|--------| | web-app-f6c49bc8-cxjpf | Running | ❌ | **99** | 🔴 CRASH | | worker-777bbb8cb9-b844r | Running | ✅ | 0 | 🟢 OK | | cache-69689c8c-hq55x | Running | ✅ | 0 | 🟢 OK | > `web-app` is in **CrashLoopBackOff** — command: `sh -c "echo '...'; sleep 10; exit 1"` """) gr.Markdown("---") # ── Tabs ────────────────────────────────────────────────────────────────── with gr.Tabs(): # Full Pipeline with gr.Tab("🚀 Full Pipeline"): gr.Markdown(""" ### Autonomous Remediation Pipeline Runs all 5 MCP tools in sequence: `list_pods` → `read_logs` → `get_pod_status` → `describe_deployment` → `restart_service` """) run_btn = gr.Button( "▶ Run Full Remediation Pipeline", variant="primary", size="lg", ) with gr.Row(): out_list = gr.Textbox(label="Step 1 — list_pods", lines=10) out_logs = gr.Textbox(label="Step 2 — read_logs", lines=10) with gr.Row(): out_status = gr.Textbox(label="Step 3 — get_pod_status", lines=10) out_desc = gr.Textbox(label="Step 4 — describe_deployment", lines=10) with gr.Row(): out_restart = gr.Textbox(label="Step 5 — restart_service", lines=10) out_audit_p = gr.Textbox(label="Audit Log", lines=10) run_btn.click( run_full_pipeline, inputs=[namespace_input, token_input], outputs=[out_list, out_logs, out_status, out_desc, out_restart, out_audit_p], ) # list_pods with gr.Tab("📋 list_pods"): gr.Markdown("Lists all pods in a namespace. Auto-identifies CrashLoopBackOff. **Scope: READ**") btn1 = gr.Button("Call list_pods", variant="primary") with gr.Row(): out1 = gr.Textbox(label="Result (JSON)", lines=20) sum1 = gr.Textbox(label="Summary", lines=5) btn1.click( tool_list_pods, inputs=[namespace_input, token_input], outputs=[out1, sum1], ) # get_pod_status with gr.Tab("🔍 get_pod_status"): gr.Markdown("Returns detailed pod status with auto-diagnosis. **Scope: READ**") pod_input2 = gr.Textbox(label="Pod Name", value="web-app-f6c49bc8-cxjpf") btn2 = gr.Button("Call get_pod_status", variant="primary") out2 = gr.Textbox(label="Result (JSON)", lines=20) btn2.click( tool_get_pod_status, inputs=[pod_input2, namespace_input, token_input], outputs=[out2], ) # read_logs with gr.Tab("📄 read_logs"): gr.Markdown("Reads pod logs. Handles crashed containers via previous container support. **Scope: READ**") pod_input3 = gr.Textbox(label="Pod Name", value="web-app-f6c49bc8-cxjpf") tail_input = gr.Slider(label="Tail Lines", minimum=10, maximum=200, value=50, step=10) btn3 = gr.Button("Call read_logs", variant="primary") out3 = gr.Textbox(label="Logs", lines=10) btn3.click( tool_read_logs, inputs=[pod_input3, namespace_input, token_input, tail_input], outputs=[out3], ) # describe_deployment with gr.Tab("🏗️ describe_deployment"): gr.Markdown("Reads full deployment spec. **Detects `exit 1` as INTENTIONAL_CRASH.** Scope: READ**") deploy_input = gr.Textbox(label="Deployment Name", value="web-app") btn4 = gr.Button("Call describe_deployment", variant="primary") out4 = gr.Textbox(label="Result (JSON) + Diagnosis", lines=25) btn4.click( tool_describe_deployment, inputs=[deploy_input, namespace_input, token_input], outputs=[out4], ) # restart_service with gr.Tab("🔄 restart_service"): gr.Markdown(""" **DESTRUCTIVE ACTION** — Deletes pod. Kubernetes recreates via ReplicaSet. Requires `confirm=True`. **Scope: WRITE** """) pod_input5 = gr.Textbox(label="Pod Name", value="web-app-f6c49bc8-cxjpf") confirm_check = gr.Checkbox(label="confirm=True (required to execute)", value=False) btn5 = gr.Button("Call restart_service", variant="primary") out5 = gr.Textbox(label="Result (JSON)", lines=15) btn5.click( tool_restart_service, inputs=[pod_input5, namespace_input, token_input, confirm_check], outputs=[out5], ) # Audit Log with gr.Tab("📊 Audit Log"): gr.Markdown(""" ### Immutable Append-Only Audit Log Every tool call is permanently recorded with timestamp, tool name, namespace, and result. No modification. No deletion. Full traceability. """) btn_audit = gr.Button("Refresh Audit Log", variant="secondary") out_audit_tab = gr.Textbox(label="Audit Log", lines=20) btn_audit.click(refresh_audit, outputs=[out_audit_tab]) # ── Footer ──────────────────────────────────────────────────────────────── gr.Markdown(""" --- **Built by [Bobby Mozaar](mailto:smozaar@gmail.com)** — AI Systems Architect · BigIcks Consulting · Cameroon 🇨🇲 [📂 GitHub Repository](https://github.com/MozaarAI-Agentic/Project3-MCP-Infra-Operator) | [📖 MCP Protocol](https://modelcontextprotocol.io) | [🤗 HuggingFace Space](https://huggingface.co/spaces/Mozaar/Project3-MCP-Infra-Operator) """) if __name__ == "__main__": demo.launch()