"""Launch the real stdio server and read one public issue; no LLM needed."""

import asyncio
import json
import os
from pathlib import Path
import sys

from mcp import Client, StdioServerParameters


async def main() -> int:
    issue_number = int(sys.argv[1]) if len(sys.argv) > 1 else 3152
    environment = {
        "GITHUB_REPOSITORY": os.environ.get(
            "GITHUB_REPOSITORY", "modelcontextprotocol/python-sdk"
        )
    }
    if os.environ.get("GITHUB_TOKEN"):
        environment["GITHUB_TOKEN"] = os.environ["GITHUB_TOKEN"]
    process = StdioServerParameters(
        command=sys.executable,
        args=[str(Path(__file__).with_name("server.py").resolve())],
        env=environment,
    )
    async with Client(process) as client:
        tools = await client.list_tools()
        print("Tools:", ", ".join(tool.name for tool in tools.tools))
        result = await client.call_tool("get_issue", {"issue_number": issue_number})
        if result.is_error:
            for block in result.content:
                if block.type == "text":
                    print(block.text, file=sys.stderr)
            return 1
        print(json.dumps(result.structured_content, indent=2))
    return 0


if __name__ == "__main__":
    raise SystemExit(asyncio.run(main()))
