#!/usr/bin/env python3
"""Second read-only check: curogram_list_conversations (take=1). No sends."""
import json, urllib.request, urllib.error
URL = "https://claude-cloud.tail053faf.ts.net/curogram/mcp"
BEARER = "f10221f209ad50494fd1fd86e62bda9236449b3b35b0a2d97b1ca7bfb86208b7"
def post(payload, session=None):
    headers = {"Content-Type": "application/json",
               "Accept": "application/json, text/event-stream",
               "Authorization": f"Bearer {BEARER}"}
    if session: headers["Mcp-Session-Id"] = session
    req = urllib.request.Request(URL, data=json.dumps(payload).encode(), headers=headers, method="POST")
    try:
        r = urllib.request.urlopen(req, timeout=40)
        return r.status, r.headers.get("Mcp-Session-Id"), r.read().decode("utf-8", "replace")
    except urllib.error.HTTPError as e:
        return e.code, None, e.read().decode("utf-8", "replace")
def parse(raw):
    for line in raw.splitlines():
        line = line.strip()
        if line.startswith("data:"):
            try: return json.loads(line[5:].strip())
            except: pass
    try: return json.loads(raw)
    except: return {"raw": raw[:300]}
s, sid, raw = post({"jsonrpc": "2.0", "id": 1, "method": "initialize",
    "params": {"protocolVersion": "2024-11-05", "capabilities": {},
               "clientInfo": {"name": "s", "version": "0.1"}}})
post({"jsonrpc": "2.0", "method": "notifications/initialized", "params": {}}, sid)
s2, _, raw2 = post({"jsonrpc": "2.0", "id": 2, "method": "tools/call",
    "params": {"name": "curogram_list_conversations", "arguments": {"take": 1}}}, sid)
cr = parse(raw2)
if "error" in cr:
    print("TOOL_ERROR", json.dumps(cr["error"])[:300]); raise SystemExit(2)
res = cr.get("result", {})
content = res.get("content", [])
text = " ".join(c.get("text", "") for c in content if isinstance(c, dict))
print("status", s2, "isError", res.get("isError"))
# Print only structural shape, not patient content: count items / keys
try:
    data = json.loads(text)
    items = data.get("items") if isinstance(data, dict) else None
    print("items_count", len(items) if isinstance(items, list) else "n/a",
          "top_keys", list(data.keys()) if isinstance(data, dict) else type(data).__name__)
except Exception:
    print("text_len", len(text))
print("AUTH_OK", not res.get("isError"))
