#!/usr/bin/env python3
"""测试 MCP server 能否通过 stdio 正常启动并响应 initialize"""
import subprocess, json

proc = subprocess.Popen(
    ["python", "-m", "szyjiyi", "--project-dir", "."],
    stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)

# 发送 JSON-RPC initialize
msg = json.dumps({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2024-11-05", "capabilities": {}, "clientInfo": {"name": "test", "version": "1.0"}}})
proc.stdin.write((msg + "\n").encode())
proc.stdin.flush()

# 读取响应
import select
ready, _, _ = select.select([proc.stdout], [], [], 5)
if ready:
    line = proc.stdout.readline().decode()
    print(f"response line: {line.strip()}")
    print("MCP server 启动正常")
else:
    stderr = proc.stderr.read().decode()
    print(f"超时，stderr: {stderr[:500]}")
    print("MCP server 启动失败")

proc.terminate()
