File size: 11,936 Bytes
3d898cc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | 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.
[](https://github.com/MozaarAI-Agentic/Project3-MCP-Infra-Operator)
[](https://modelcontextprotocol.io)
[](https://kubernetes.io)
[](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() |