#!/usr/bin/env python3 """Sample hx extension (stdio JSON-RPC). Blocks bash commands matching a pattern and adds a `now` tool. ~/.hx/config.toml: [[extensions]] command = ["python3", "/path/to/ext_guard.py", "rm -rf"] """ import json, sys, time deny = sys.argv[1] if len(sys.argv) > 1 else "rm -rf" for line in sys.stdin: m = json.loads(line) p = m.get("params") or {} meth = m.get("method") if meth == "initialize": r = {"subscribe": ["tool_call"], "tools": [{"name": "now", "description": "Current unix time", "inputSchema": {"type": "object", "properties": {}}}]} elif meth == "event": e = p["event"] cmd = e["args"].get("command") or e["args"].get("cmd") or "" if e["name"].lower() == "bash" and deny in cmd: r = {"action": "block", "reason": f"ext_guard: `{deny}` is not allowed"} else: r = {"action": "continue"} elif meth == "tool/call": r = {"content": str(int(time.time())), "is_error": False} else: print(json.dumps({"jsonrpc": "2.0", "id": m.get("id"), "error": {"code": -32601, "message": "unknown"}}), flush=True) continue print(json.dumps({"jsonrpc": "2.0", "id": m["id"], "result": r}), flush=True)