"""Reference client for TokConnect Streamable HTTP MCP.

Install the pinned requirements file, then run:
TOKCONNECT_KEY=... python pillar-mcp-python-client.py

Status: imports and transport/session signatures were smoke-tested on
18 September 2026 with mcp==2.2.0 and httpx2==2.13.0 in an isolated venv.
This file has not been run against a hosted TokConnect account key.
"""
import asyncio
import os

import httpx2
from mcp import ClientSession
from mcp.client.streamable_http import streamable_http_client

ENDPOINT = "https://mcp.tokconnect.com/mcp"


async def main() -> None:
    key = os.environ["TOKCONNECT_KEY"]
    headers = {"Authorization": f"Bearer {key}"}
    async with httpx2.AsyncClient(headers=headers) as http_client:
        async with streamable_http_client(ENDPOINT, http_client=http_client) as (read, write):
            async with ClientSession(read, write) as session:
                await session.initialize()
                tools = await session.list_tools()
                print("Tools:", ", ".join(tool.name for tool in tools.tools))
                result = await session.call_tool(
                    "search_topics",
                    {"keyword": "language learning app", "limit": 5, "language": "en"},
                )
                for block in result.content:
                    if hasattr(block, "text"):
                        print(block.text)


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