"""MCP Bridge — launches remote MCP server via SSH and bridges stdio

Configure via environment variables:
  NEXUS_SSH_KEY    — path to SSH private key (default: ~/.ssh/id_rsa)
  NEXUS_SSH_HOST   — remote server hostname/IP (required)
  NEXUS_DEPLOY_DIR — remote installation path (default: /opt/nexus)
"""
import os
import sys
import subprocess
from pathlib import Path

ssh_key = os.environ.get("NEXUS_SSH_KEY", str(Path.home() / ".ssh" / "id_rsa"))
ssh_host = os.environ.get("NEXUS_SSH_HOST", "")
deploy_dir = os.environ.get("NEXUS_DEPLOY_DIR", "/opt/nexus")

if not ssh_host:
    print("ERROR: NEXUS_SSH_HOST not set (e.g. root@1.2.3.4)", file=sys.stderr)
    sys.exit(1)

ssh = subprocess.Popen([
    "ssh",
    "-o", "StrictHostKeyChecking=no",
    "-i", ssh_key,
    ssh_host,
    "python3", f"{deploy_dir}/mcp/Nexus_server.py"
], stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)

sys.exit(ssh.wait())
