Mozaar
feat: add Gradio mock demo - full MCP pipeline simulation
3d898cc
Raw History Blame Contribute Delete
11.9 kB
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()