import json, urllib.request, time
URL="https://claude-cloud.tail053faf.ts.net/advancedmd/mcp"
AUTH="Bearer f10221f209ad50494fd1fd86e62bda9236449b3b35b0a2d97b1ca7bfb86208b7"
def sess():
    def post(b,s=None):
        d=json.dumps(b).encode(); r=urllib.request.Request(URL,data=d,method="POST")
        r.add_header("Authorization",AUTH); r.add_header("Content-Type","application/json")
        r.add_header("Accept","application/json, text/event-stream")
        if s: r.add_header("Mcp-Session-Id",s)
        resp=urllib.request.urlopen(r,timeout=60); sid=resp.headers.get("Mcp-Session-Id"); raw=resp.read().decode()
        o=None
        if raw.lstrip().startswith("{"): o=json.loads(raw)
        else:
            for ln in raw.splitlines():
                if ln.startswith("data:"):
                    try:o=json.loads(ln[5:].strip())
                    except:pass
        return sid,o
    sid,_=post({"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"b","version":"1"}}})
    dd=json.dumps({"jsonrpc":"2.0","method":"notifications/initialized"}).encode()
    r=urllib.request.Request(URL,data=dd,method="POST"); r.add_header("Authorization",AUTH)
    r.add_header("Content-Type","application/json"); r.add_header("Accept","application/json, text/event-stream"); r.add_header("Mcp-Session-Id",sid)
    urllib.request.urlopen(r,timeout=30).read(); return sid
def call(sid,name,args):
    b={"jsonrpc":"2.0","id":7,"method":"tools/call","params":{"name":name,"arguments":args}}
    d=json.dumps(b).encode(); r=urllib.request.Request(URL,data=d,method="POST")
    r.add_header("Authorization",AUTH); r.add_header("Content-Type","application/json")
    r.add_header("Accept","application/json, text/event-stream"); r.add_header("Mcp-Session-Id",sid)
    resp=urllib.request.urlopen(r,timeout=90); raw=resp.read().decode(); o=None
    if raw.lstrip().startswith("{"): o=json.loads(raw)
    else:
        for ln in raw.splitlines():
            if ln.startswith("data:"):
                try:o=json.loads(ln[5:].strip())
                except:pass
    return o['result']['content'][0]['text']
sid=sess()
for i in range(75):
    txt=call(sid,"auth_status",{})
    st=json.loads(txt)
    cds=st.get("cooldowns",{})
    if not cds:
        print("READY at iter",i); break
    mins=max((v.get("minsLeft",0) for v in cds.values()), default=0)
    print(f"iter {i}: cooldown minsLeft={mins}", flush=True)
    time.sleep(60)
else:
    print("still cooling after wait")
