import json import os import time from mitmproxy import http class CaptureAddon: def __init__(self): self.output_dir = os.environ.get("CAPTURE_DIR", "/captures") os.makedirs(self.output_dir, exist_ok=True) self.counter = 0 def request(self, flow: http.HTTPFlow): if "api.anthropic.com" not in (flow.request.host or ""): return self.counter += 1 flow.metadata["capture_id"] = self.counter body = None try: body = json.loads(flow.request.get_text()) except Exception: body = flow.request.get_text() source = "unknown" ua = flow.request.headers.get("user-agent", "") if "claude-cli" in ua or "claude-code" in ua: source = "claude-code" elif "python" in ua.lower() or "httpx" in ua.lower(): source = "claude-py" data = { "capture_id": self.counter, "source": source, "timestamp": time.time(), "method": flow.request.method, "url": flow.request.pretty_url, "headers": dict(flow.request.headers), "body": body, } path = os.path.join(self.output_dir, f"req_{self.counter:04d}.json") with open(path, "w") as f: json.dump(data, f, indent=2, default=str) print(f"[capture] #{self.counter} {source} {flow.request.method} {flow.request.pretty_url}") addons = [CaptureAddon()]