#!/bin/bash
# LinkedIn MCP Client Startup Script
# Starts both the Netlify MCP server and LinkedIn FastAPI client
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
PROCESSES_DIR="$SCRIPT_DIR/.processes"
NETLIFY_PORT=8888
FASTAPI_PORT=8002
# Create processes directory
mkdir -p "$PROCESSES_DIR"
echo -e "${BLUE}π Starting LinkedIn MCP Infrastructure${NC}"
echo "=================================="
# Function to check if port is in use
check_port() {
local port=$1
if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1; then
return 0
else
return 1
fi
}
# Function to start Netlify dev server
start_netlify() {
echo -e "${YELLOW}π‘ Starting Netlify MCP Server...${NC}"
if check_port $NETLIFY_PORT; then
echo -e "${YELLOW}β οΈ Port $NETLIFY_PORT already in use. Checking if it's our server...${NC}"
# Test if it's responding to MCP requests
if curl -s -X POST http://localhost:$NETLIFY_PORT/mcp \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"mcp/init","params":{},"id":"1"}' \
| grep -q "linkedin-mcp-server"; then
echo -e "${GREEN}β
LinkedIn MCP Server already running on port $NETLIFY_PORT${NC}"
return 0
else
echo -e "${RED}β Port $NETLIFY_PORT in use by different service${NC}"
return 1
fi
fi
cd "$PROJECT_ROOT"
# Start Netlify dev server in background
nohup npx netlify dev --port=$NETLIFY_PORT > "$PROCESSES_DIR/netlify-dev.log" 2>&1 &
NETLIFY_PID=$!
echo $NETLIFY_PID > "$PROCESSES_DIR/netlify.pid"
echo -e "${BLUE}π Netlify PID: $NETLIFY_PID${NC}"
# Wait for server to start
echo -e "${YELLOW}β³ Waiting for Netlify server to start...${NC}"
for i in {1..30}; do
if curl -s http://localhost:$NETLIFY_PORT >/dev/null 2>&1; then
echo -e "${GREEN}β
Netlify server started successfully${NC}"
return 0
fi
sleep 1
done
echo -e "${RED}β Netlify server failed to start within 30 seconds${NC}"
return 1
}
# Function to start LinkedIn FastAPI client
start_fastapi() {
echo -e "${YELLOW}π Starting LinkedIn FastAPI Client...${NC}"
if check_port $FASTAPI_PORT; then
echo -e "${YELLOW}β οΈ Port $FASTAPI_PORT already in use. Checking if it's our client...${NC}"
# Test if it's our FastAPI client
if curl -s http://localhost:$FASTAPI_PORT/ | grep -q "LinkedIn MCP Client"; then
echo -e "${GREEN}β
LinkedIn FastAPI Client already running on port $FASTAPI_PORT${NC}"
return 0
else
echo -e "${RED}β Port $FASTAPI_PORT in use by different service${NC}"
return 1
fi
fi
cd "$SCRIPT_DIR"
# Check if virtual environment exists
if [ ! -d ".venv" ]; then
echo -e "${YELLOW}π¦ Creating Python virtual environment...${NC}"
python3 -m venv .venv
fi
# Activate virtual environment and install dependencies
source .venv/bin/activate
echo -e "${YELLOW}π¦ Installing/updating Python dependencies...${NC}"
pip install -q --upgrade pip
pip install -q -r requirements.txt
# Start FastAPI server in background
nohup python -m uvicorn linkedin_client:app --host 0.0.0.0 --port $FASTAPI_PORT --reload > "$PROCESSES_DIR/fastapi.log" 2>&1 &
FASTAPI_PID=$!
echo $FASTAPI_PID > "$PROCESSES_DIR/fastapi.pid"
echo -e "${BLUE}π FastAPI PID: $FASTAPI_PID${NC}"
# Wait for server to start
echo -e "${YELLOW}β³ Waiting for FastAPI server to start...${NC}"
for i in {1..20}; do
if curl -s http://localhost:$FASTAPI_PORT/ >/dev/null 2>&1; then
echo -e "${GREEN}β
FastAPI server started successfully${NC}"
return 0
fi
sleep 1
done
echo -e "${RED}β FastAPI server failed to start within 20 seconds${NC}"
return 1
}
# Function to test the setup
test_setup() {
echo -e "${YELLOW}π§ͺ Testing LinkedIn MCP setup...${NC}"
# Test MCP server
echo -e "${BLUE}Testing MCP server initialization...${NC}"
MCP_RESPONSE=$(curl -s -X POST http://localhost:$NETLIFY_PORT/mcp \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"mcp/init","params":{},"id":"1"}')
if echo "$MCP_RESPONSE" | grep -q "linkedin-mcp-server"; then
echo -e "${GREEN}β
MCP server responding correctly${NC}"
else
echo -e "${RED}β MCP server not responding correctly${NC}"
echo "Response: $MCP_RESPONSE"
return 1
fi
# Test FastAPI client
echo -e "${BLUE}Testing FastAPI client health...${NC}"
HEALTH_RESPONSE=$(curl -s http://localhost:$FASTAPI_PORT/health)
if echo "$HEALTH_RESPONSE" | grep -q "healthy"; then
echo -e "${GREEN}β
FastAPI client healthy${NC}"
else
echo -e "${YELLOW}β οΈ FastAPI client responding but may have issues${NC}"
echo "Response: $HEALTH_RESPONSE"
fi
# Run quick test suite
echo -e "${BLUE}Running quick test suite...${NC}"
cd "$SCRIPT_DIR"
if [ -f ".venv/bin/activate" ]; then
source .venv/bin/activate
python test_linkedin.py
else
echo -e "${YELLOW}β οΈ Virtual environment not found, skipping test suite${NC}"
fi
}
# Main execution
main() {
# Check dependencies
if ! command -v node >/dev/null 2>&1; then
echo -e "${RED}β Node.js is required but not installed${NC}"
exit 1
fi
if ! command -v python3 >/dev/null 2>&1; then
echo -e "${RED}β Python 3 is required but not installed${NC}"
exit 1
fi
if ! command -v npx >/dev/null 2>&1; then
echo -e "${RED}β npx is required but not installed${NC}"
exit 1
fi
# Start services
if start_netlify && start_fastapi; then
echo -e "\n${GREEN}π LinkedIn MCP Infrastructure Started Successfully!${NC}"
echo "=================================="
echo -e "${BLUE}π Service URLs:${NC}"
echo " β’ MCP Server: http://localhost:$NETLIFY_PORT/mcp"
echo " β’ LinkedIn API: http://localhost:$FASTAPI_PORT"
echo " β’ API Documentation: http://localhost:$FASTAPI_PORT/docs"
echo " β’ ReDoc: http://localhost:$FASTAPI_PORT/redoc"
echo ""
echo -e "${BLUE}π Log Files:${NC}"
echo " β’ Netlify: $PROCESSES_DIR/netlify-dev.log"
echo " β’ FastAPI: $PROCESSES_DIR/fastapi.log"
echo ""
echo -e "${BLUE}π§ Management Commands:${NC}"
echo " β’ Check Status: ./check_linkedin_status.sh"
echo " β’ Stop Services: ./stop_linkedin.sh"
echo " β’ Run Tests: python test_linkedin.py"
echo ""
# Test the setup
test_setup
echo -e "\n${GREEN}π Ready for LinkedIn automation!${NC}"
echo -e "${YELLOW}π‘ Set LINKEDIN_ACCESS_TOKEN environment variable for full functionality${NC}"
else
echo -e "\n${RED}π₯ Failed to start LinkedIn MCP Infrastructure${NC}"
exit 1
fi
}
# Handle script arguments
case "${1:-start}" in
"start")
main
;;
"stop")
echo -e "${YELLOW}π Use ./stop_linkedin.sh to stop services${NC}"
;;
"test")
test_setup
;;
"help")
echo "LinkedIn MCP Infrastructure Management"
echo ""
echo "Usage: $0 [command]"
echo ""
echo "Commands:"
echo " start (default) - Start all services"
echo " test - Test running services"
echo " help - Show this help"
echo ""
echo "Other scripts:"
echo " ./stop_linkedin.sh - Stop all services"
echo " ./check_linkedin_status.sh - Check service status"
;;
*)
echo -e "${RED}β Unknown command: $1${NC}"
echo "Use '$0 help' for usage information"
exit 1
;;
esac