"""Python MCP SDK 2.1.1 + httpx2; install explicitly in your project environment."""
import asyncio
from contextlib import asynccontextmanager
import json
import os

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


@asynccontextmanager
async def transport():
    async with httpx2.AsyncClient(headers={"X-API-Key": os.environ["RADAR_API_KEY"]}, timeout=30) as http:
        async with streamable_http_client("https://api.revenirdata.com/mcp", http_client=http) as streams:
            yield streams


async def main():
    async with Client(transport()) as client:
        tools = (await client.list_tools()).tools
        result = await client.call_tool("search_opportunities", {"job_family": ["data"], "limit": 1})
        if result.is_error:
            raise RuntimeError("Radar MCP tool failed; inspect account access and safe error code.")
        body = result.structured_content
        print(json.dumps({"protocol": client.protocol_version, "tools": [tool.name for tool in tools],
                          "request_id": body["meta"]["request_id"], "returned": body["pagination"]["returned"]}))


if __name__ == "__main__":
    asyncio.run(asyncio.wait_for(main(), timeout=45))
