"""Print `status://overview` from the running nostr-admin MCP server.

Talks to the streamable-HTTP endpoint set up by `mcp_admin.py` (defaults to
`http://localhost:8100/mcp/`). Override with NOSTR_MCP_URL env if needed.

Output is whatever the server returns, pretty-printed JSON — no field is
hand-rendered here, so new keys added on the server side show up
automatically without touching this script.
"""

import asyncio
import json
import os
import sys

from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client


DEFAULT_URL = "http://localhost:8100/mcp/"


async def _fetch(url: str) -> dict:
    async with streamablehttp_client(url) as (read, write, _get_session_id):
        async with ClientSession(read, write) as session:
            await session.initialize()
            result = await session.read_resource("status://overview")
            for content in result.contents:
                text = getattr(content, "text", None)
                if text:
                    return json.loads(text)
            raise RuntimeError("status://overview returned no text content")


def main() -> int:
    url = os.environ.get("NOSTR_MCP_URL", DEFAULT_URL)
    try:
        payload = asyncio.run(_fetch(url))
    except Exception as e:
        print(f"error fetching {url}: {e}", file=sys.stderr)
        return 1
    print(json.dumps(payload, indent=2, ensure_ascii=False, sort_keys=True))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
