#!/bin/sh
# minis-mcp-cli — MinisApp MCP (Model Context Protocol) client for iSH/PRoot.
#
# Thin sh wrapper: ensures the python3 + httpx runtime is present, then execs
# the shared Python implementation. The Python under
# /usr/local/lib/minis-mcp-cli is platform-neutral and is shipped identically
# on iOS (iSH) and Android (PRoot/Ubuntu) — keep it POSIX + dependency-light.
#
# Data file: /var/minis/mcp-servers/servers.json (mcpServers object format,
# Claude-Desktop compatible). Logs: /var/minis/mcp-servers/mcp-cli.log.
# Diagnostics go to the log, never to stdout (stdout is reserved for the
# JSON the agent parses).

LOG="/var/minis/mcp-servers/mcp-cli.log"
mkdir -p /var/minis/mcp-servers 2>/dev/null
_log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >> "$LOG" 2>/dev/null; }

# Ensure python3 is available.
if ! command -v python3 > /dev/null 2>&1; then
  _log "Installing python3..."
  export DEBIAN_FRONTEND=noninteractive
  apt-get update -qq >> "$LOG" 2>&1
  apt-get install -y -qq python3 python3-pip >> "$LOG" 2>&1
fi

# Ensure httpx is importable (used by the HTTP transport).
if ! python3 -c "import httpx" 2>/dev/null; then
  _log "Installing httpx..."
  pip install --quiet httpx >> "$LOG" 2>&1 || \
    pip install --quiet --break-system-packages httpx >> "$LOG" 2>&1
fi

exec python3 /usr/local/lib/minis-mcp-cli/main.py "$@"
