#!/usr/bin/env python3
# MglennP Radial Switchboard -- MCP Stdio Bridge (Spec Rev 3.8.2) | support@radialswitchboard.com
import json, os, sys, urllib.error, urllib.request
MCP_URL = os.environ.get("RADIAL_MCP_URL", "https://radialswitchboard.com/mcp")
PASS_TOKEN = os.environ.get("RADIAL_PASS_TOKEN", "").strip()
TIMEOUT_S = int(os.environ.get("RADIAL_TIMEOUT_S", "25"))
for line in sys.stdin:
    raw = line.strip()
    if not raw: continue
    req_id = None
    is_notif = False
    try:
        parsed = json.loads(raw)
        if isinstance(parsed, dict):
            req_id = parsed.get("id")
            is_notif = "id" not in parsed
        h = {"Content-Type": "application/json", "Accept": "application/json", "User-Agent": "RadialSwitchboard-StdioBridge/3.8.2", "mcp-protocol-version": "2026-07-28"}
        if PASS_TOKEN: h["X-Radial-Pass"] = PASS_TOKEN
        req = urllib.request.Request(MCP_URL, data=raw.encode("utf-8"), headers=h, method="POST")
        try:
            with urllib.request.urlopen(req, timeout=TIMEOUT_S) as r:
                b = r.read().decode("utf-8", errors="replace").strip()
        except urllib.error.HTTPError as he:
            eb = he.read().decode("utf-8", errors="replace").strip()
            if not eb: raise
            b = eb
        if b and not is_notif:
            try: b = json.dumps(json.loads(b), separators=(",", ":"))
            except Exception: b = b.replace("\r", " ").replace("\n", " ")
            sys.stdout.write(b + "\n"); sys.stdout.flush()
    except Exception as exc:
        if not is_notif:
            err = {"jsonrpc": "2.0", "id": req_id, "error": {"code": -32000, "message": str(exc)}}
            sys.stdout.write(json.dumps(err, separators=(",", ":")) + "\n"); sys.stdout.flush()
