#!/usr/bin/env bash set -euo pipefail PORT=${PORT:-8506} APP_MODULE=${APP_MODULE:-news_mcp.mcp_server_fastmcp:app} LOGFILE=${LOGFILE:-logs/server.log} PIDFILE=${PIDFILE:-logs/server.pid} mkdir -p "$(dirname "$LOGFILE")" mkdir -p "$(dirname "$PIDFILE")" if [ -f "$PIDFILE" ]; then PID="$(cat "$PIDFILE" 2>/dev/null || true)" if [ -n "$PID" ] && ps -p "$PID" > /dev/null 2>&1; then echo "Server already running (PID $PID)" exit 0 fi rm -f "$PIDFILE" fi # If another process already binds the configured port, fail fast rather than # launching a second server and hiding the collision. if command -v curl >/dev/null 2>&1; then if curl -sf "http://127.0.0.1:${PORT}/health" >/dev/null 2>&1; then echo "A server is already responding on port $PORT; refusing to start a second instance" exit 1 fi fi UVICORN_BIN="${UVICORN_BIN:-}" if [ -z "$UVICORN_BIN" ]; then if [ -x ".venv/bin/uvicorn" ]; then UVICORN_BIN=".venv/bin/uvicorn" else UVICORN_BIN="uvicorn" fi fi export PYTHONPATH="$(pwd):${PYTHONPATH:-}" nohup "$UVICORN_BIN" "$APP_MODULE" --host 0.0.0.0 --port "$PORT" > "$LOGFILE" 2>&1 & echo $! > "$PIDFILE" echo "Uvicorn started on port $PORT (PID $(cat "$PIDFILE"))"