start.shโข6.96 kB
#!/bin/bash
# AnyDocs MCP Server Unix/Linux Startup Script
# This script provides a convenient way to start the server on Unix-like systems
set -e # Exit on any error
# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to show help
show_help() {
cat << EOF
AnyDocs MCP Server Unix/Linux Startup Script
Usage: $0 [options]
Options:
--mode MODE Server mode: mcp, web, or both (default: both)
--config FILE Configuration file path
--debug Enable debug logging
--venv PATH Path to virtual environment (default: venv)
--no-venv Don't use virtual environment
--install-deps Install dependencies before starting
--kill-ports Kill processes using required ports
--help Show this help message
Examples:
$0 Start both MCP server and web interface
$0 --mode mcp Start only MCP server
$0 --mode web Start only web interface
$0 --debug Start with debug logging
$0 --config custom.yaml Use custom config file
$0 --install-deps Install dependencies and start
$0 --kill-ports Kill port conflicts and start
EOF
}
# Default values
MODE="both"
CONFIG_FILE=""
DEBUG_FLAG=""
VENV_PATH="venv"
USE_VENV=true
INSTALL_DEPS=false
KILL_PORTS=false
EXTRA_ARGS=()
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--mode)
MODE="$2"
shift 2
;;
--config)
CONFIG_FILE="--config $2"
shift 2
;;
--debug)
DEBUG_FLAG="--debug"
shift
;;
--venv)
VENV_PATH="$2"
shift 2
;;
--no-venv)
USE_VENV=false
shift
;;
--install-deps)
INSTALL_DEPS=true
shift
;;
--kill-ports)
KILL_PORTS=true
shift
;;
--help)
show_help
exit 0
;;
*)
EXTRA_ARGS+=("$1")
shift
;;
esac
done
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to kill process on port
kill_port() {
local port=$1
local pid
if command_exists lsof; then
pid=$(lsof -ti :"$port" 2>/dev/null || true)
if [[ -n "$pid" ]]; then
print_info "Killing process $pid on port $port"
kill -9 "$pid" 2>/dev/null || true
sleep 1
fi
elif command_exists netstat; then
pid=$(netstat -tlnp 2>/dev/null | grep ":$port " | awk '{print $7}' | cut -d'/' -f1 || true)
if [[ -n "$pid" && "$pid" != "-" ]]; then
print_info "Killing process $pid on port $port"
kill -9 "$pid" 2>/dev/null || true
sleep 1
fi
fi
}
# Function to setup virtual environment
setup_venv() {
if [[ "$USE_VENV" == true ]]; then
if [[ -d "$VENV_PATH" ]]; then
print_info "Activating virtual environment: $VENV_PATH"
source "$VENV_PATH/bin/activate"
else
print_warning "Virtual environment not found: $VENV_PATH"
print_info "Creating virtual environment..."
python3 -m venv "$VENV_PATH"
source "$VENV_PATH/bin/activate"
INSTALL_DEPS=true
fi
fi
}
# Function to install dependencies
install_dependencies() {
if [[ "$INSTALL_DEPS" == true ]]; then
print_info "Installing dependencies..."
if command_exists uv; then
uv install --dev
elif [[ -f "pyproject.toml" ]]; then
pip install -e .[dev]
else
print_error "No pyproject.toml found and uv not available"
exit 1
fi
fi
}
# Function to check prerequisites
check_prerequisites() {
# Check Python
if ! command_exists python3 && ! command_exists python; then
print_error "Python is not installed or not in PATH"
print_error "Please install Python 3.8+ and try again"
exit 1
fi
# Use python3 if available, otherwise python
if command_exists python3; then
PYTHON_CMD="python3"
else
PYTHON_CMD="python"
fi
# Check Python version
PYTHON_VERSION=$($PYTHON_CMD --version 2>&1 | cut -d' ' -f2)
PYTHON_MAJOR=$(echo "$PYTHON_VERSION" | cut -d'.' -f1)
PYTHON_MINOR=$(echo "$PYTHON_VERSION" | cut -d'.' -f2)
if [[ "$PYTHON_MAJOR" -lt 3 ]] || [[ "$PYTHON_MAJOR" -eq 3 && "$PYTHON_MINOR" -lt 8 ]]; then
print_error "Python 3.8+ is required, found $PYTHON_VERSION"
exit 1
fi
print_success "Python $PYTHON_VERSION found"
# Check if source code exists
if [[ ! -d "src/anydocs_mcp" ]]; then
print_error "Source code not found"
print_error "Please ensure you're in the correct directory"
exit 1
fi
}
# Function to start the server
start_server() {
echo
echo "========================================"
echo " AnyDocs MCP Server Startup"
echo "========================================"
echo "Mode: $MODE"
[[ -n "$CONFIG_FILE" ]] && echo "Config: $CONFIG_FILE"
[[ -n "$DEBUG_FLAG" ]] && echo "Debug: Enabled"
[[ "$USE_VENV" == true ]] && echo "Virtual Environment: $VENV_PATH"
echo
# Kill ports if requested
if [[ "$KILL_PORTS" == true ]]; then
print_info "Killing processes on default ports..."
kill_port 3000 # Default MCP port
kill_port 8000 # Default web port
fi
# Start the server
print_info "Starting AnyDocs MCP Server..."
# Build the command
CMD=("$PYTHON_CMD" "start.py" "--mode" "$MODE")
if [[ -n "$CONFIG_FILE" ]]; then
CMD+=($CONFIG_FILE)
fi
if [[ -n "$DEBUG_FLAG" ]]; then
CMD+=("$DEBUG_FLAG")
fi
# Add extra arguments
CMD+=("${EXTRA_ARGS[@]}")
# Execute the command
exec "${CMD[@]}"
}
# Function to handle cleanup on exit
cleanup() {
print_info "Cleaning up..."
# Kill any child processes
jobs -p | xargs -r kill 2>/dev/null || true
}
# Set up signal handlers
trap cleanup EXIT INT TERM
# Main execution
main() {
print_info "Starting AnyDocs MCP Server setup..."
check_prerequisites
setup_venv
install_dependencies
start_server
}
# Run main function
main "$@"