"""Verify saved wire data only; never launches an MCP server or contacts network."""
import json, sys, hashlib
from pathlib import Path
p=Path(sys.argv[1] if len(sys.argv)>1 else 'stdio_trace.json')
data=json.loads(p.read_bytes())
assert data['packages']=={'mcp':'2.0.0','mcp-types':'2.0.0'}
assert data['transport']=='stdio' and data['sdk_side']=='server'
expected={
 'modern_discover':['server/discover','tools/list','tools/call','resources/list','resources/read','prompts/list','prompts/get'],
 'modern_direct':['tools/list','tools/call'],
 'legacy':['initialize','notifications/initialized','tools/list','tools/call']}
assert [c['case'] for c in data['cases']]==list(expected)
counts=[0,0,0]; checks=0
for c in data['cases']:
    name=c['case']; modern=name!='legacy'; methods=[]; pending={}; seen=set(); results={}; local=[0,0,0]
    assert c['protocol_version']==('2026-07-28' if modern else '2025-11-25')
    for f in c['frames']:
        line=f['wire_line']; assert '\n' not in line
        m=json.loads(line); assert m['jsonrpc']=='2.0'
        if f['direction']=='client_to_server':
            method=m['method']; methods.append(method)
            if 'id' not in m:
                assert not modern and method=='notifications/initialized' and not pending and results['initialize']['protocolVersion']=='2025-11-25'; local[2]+=1
            else:
                i=m['id']; assert type(i) in (int,str) and i not in seen; seen.add(i); pending[i]=method; local[0]+=1
                if modern:
                    meta=m['params']['_meta']; assert meta['io.modelcontextprotocol/protocolVersion']=='2026-07-28' and meta['io.modelcontextprotocol/clientCapabilities']=={}
                if method=='tools/call': assert m['params']['name']=='lookup_book' and m['params']['arguments']=={'book_id':'mcp-001'}
                if method=='resources/read': assert m['params']['uri']=='catalog://books/mcp-001'
                if method=='prompts/get': assert m['params']['name']=='book_citation'
                if method=='initialize': assert m['params']['protocolVersion']=='2025-11-25' and m['params']['capabilities']=={}
        else:
            assert f['direction']=='server_to_client' and 'id' in m and m['id'] in pending and 'result' in m and 'error' not in m
            method=pending.pop(m['id']); r=m['result']; results[method]=r; local[1]+=1
            if modern: assert r['resultType']=='complete'
            else: assert 'resultType' not in r
    assert not pending and methods==expected[name]
    r=results['tools/call']; assert r['isError'] is False and r['content'][0]['text']=='Local Protocol Notes' and r['structuredContent']=={'result':'Local Protocol Notes'}
    t=results['tools/list']['tools'][0]; assert t['name']=='lookup_book' and t['inputSchema']['required']==['book_id'] and t['inputSchema']['properties']['book_id']['type']=='string'
    if name=='modern_discover':
        assert results['server/discover']['supportedVersions']==['2026-07-28'] and {'tools','resources','prompts'} <= results['server/discover']['capabilities'].keys()
        assert results['resources/list']['resources'][0]['uri']=='catalog://books/mcp-001'
        assert results['resources/read']['contents'][0]=={'mimeType':'text/plain','text':'mcp-001: Local Protocol Notes','uri':'catalog://books/mcp-001'}
        assert results['prompts/list']['prompts'][0]['name']=='book_citation'
        assert results['prompts/get']['messages'][0]=={'role':'user','content':{'type':'text','text':'Cite the supplied catalog record by book ID and title.'}}
    assert local=={'modern_discover':[7,7,0],'modern_direct':[2,2,0],'legacy':[3,3,1]}[name]
    assert c['exit_code']==0 and c['stderr']=='' and c['shutdown']=='close stdin, observe stdout EOF and exit code 0'
    counts=[a+b for a,b in zip(counts,local)]
    print(f'{name}: request/response/notification={local}; saved EOF/0/empty stderr; PASS')
assert counts==[12,12,1]
print(f'Total {sum(counts)} frames; SHA256={hashlib.sha256(p.read_bytes()).hexdigest()}')
print('PASS: saved data only; no live SDK or pipe execution.')
