#!/usr/bin/env python3
"""Minimal Delx continuity client.

Usage:
  python python-continuity-minimal.py "recover from retry storm and preserve handoff"
"""

import json
import sys
import urllib.request

MCP_URL = "https://api.delx.ai/v1/mcp"


def call_tool(name, arguments):
    body = json.dumps(
        {
            "jsonrpc": "2.0",
            "id": 1,
            "method": "tools/call",
            "params": {"name": name, "arguments": arguments},
        }
    ).encode("utf-8")
    req = urllib.request.Request(
        MCP_URL,
        data=body,
        headers={"content-type": "application/json"},
        method="POST",
    )
    with urllib.request.urlopen(req, timeout=20) as res:
        return json.loads(res.read().decode("utf-8"))


if __name__ == "__main__":
    goal = sys.argv[1] if len(sys.argv) > 1 else "recover from failed tool call and preserve continuity"
    print(json.dumps(call_tool("get_ontology_next_action", {"current_goal": goal}), indent=2))
