import argparse, json, urllib.request, pathlib, datetime
p=argparse.ArgumentParser()
p.add_argument('--port',type=int,required=True)
p.add_argument('--token-file',required=True)
p.add_argument('--sha',required=True)
p.add_argument('--out',required=True)
a=p.parse_args()
token=pathlib.Path(a.token_file).read_text().strip()
headers={'Content-Type':'application/json','Accept':'application/json, text/event-stream','Authorization':'Bearer '+token}
endpoint=f'http://127.0.0.1:{a.port}/mcp'
transcript={'commit':a.sha,'surface':'Real T3 server, MCP over HTTP, Linux; fresh isolated database; no seeded projects','requests':[]}
def request(method,params,ident):
    body={'jsonrpc':'2.0','method':method,'params':params}
    if ident is not None: body['id']=ident
    req=urllib.request.Request(endpoint,data=json.dumps(body).encode(),headers=headers)
    with urllib.request.urlopen(req,timeout=120) as response:
        if response.headers.get('mcp-session-id'): headers['mcp-session-id']=response.headers['mcp-session-id']
        raw=response.read().decode()
        try: result=json.loads(raw)
        except ValueError:
            lines=[line[6:] for line in raw.splitlines() if line.startswith('data: ')]
            result=json.loads(lines[-1]) if lines else raw
    transcript['requests'].append({'request':body,'response':result})
    return result
request('initialize',{'protocolVersion':'2025-06-18','capabilities':{},'clientInfo':{'name':'project-create-proof','version':'1.0.0'}},1)
request('notifications/initialized',{},None)
for ident,args in enumerate([{'title':'control-title-only'},{'title':'empty-scripts-demo','scripts':[]},{'title':'empty-folders-demo','trackedFolders':[]}],3):
    request('tools/call',{'name':'t3_project_create','arguments':args},ident)
request('tools/call',{'name':'t3_project_list','arguments':{}},6)
# Omit host filesystem locations from public captures; leave request/results otherwise intact.
def redact(v):
    if isinstance(v,dict): return {k:('[isolated project directory]' if k in ('workspaceRoot','cwd','path','rootPath','worktreePath','faviconPath') and isinstance(x,str) and x.startswith('/') else redact(x)) for k,x in v.items()}
    if isinstance(v,list): return [redact(x) for x in v]
    if isinstance(v,str):
        try: return json.dumps(redact(json.loads(v)),indent=2)
        except (ValueError,TypeError): return v
    return v
out=pathlib.Path(a.out)/datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%SZ')
out.mkdir(parents=True)
file=out/'transcript.json'
file.write_text(json.dumps(redact(transcript),indent=2)+'\n')
print(file)
print(file.read_text())
