#!/usr/bin/env python3
"""Capture real MCP HTTP responses; usage: probe.py BASE_URL COMMIT SIDE OUTPUT_ROOT."""
import datetime, pathlib, subprocess, sys
url, commit, side, root = sys.argv[1:]
out = pathlib.Path(root) / datetime.datetime.now(datetime.timezone.utc).strftime('%Y%m%dT%H%M%S%fZ')
out.mkdir()
cases = [
 ('Disallowed Origin: POST without Authorization', ['-X','POST','-H','Origin: https://untrusted.invalid']),
 ('Disallowed Origin: POST with bogus bearer', ['-X','POST','-H','Origin: https://untrusted.invalid','-H','Authorization: Bearer bogus']),
 ('Disallowed Origin: OPTIONS preflight', ['-X','OPTIONS','-H','Origin: https://untrusted.invalid','-H','Access-Control-Request-Method: POST']),
 ('Control: POST without Origin or Authorization', ['-X','POST']),
]
lines = [f'MCP HTTP runtime evidence — {side}', f'Commit: {commit}', 'Environment: Linux; real T3 dev server; isolated fresh database; T3CODE_DEV_SEED=0.', 'Transport: curl over loopback HTTP to /mcp. No browser or model involved.', '']
for label, args in cases:
    command = ['curl','--silent','--show-error','--include','--max-time','20',*args]
    if 'POST' in args:
        command += ['-H','Content-Type: application/json','-H','Accept: application/json, text/event-stream','--data','{"jsonrpc":"2.0","id":1,"method":"server/discover","params":{}}']
    result = subprocess.run([*command, url+'/mcp'], capture_output=True, text=True, check=True)
    lines += [label, 'Request: '+ ' '.join(command[1:])+' <server>/mcp', result.stdout, '']
path = out / (side+'.txt')
path.write_text('\n'.join(lines))
print(path)
print(path.read_text())
