restart_server.pyโข4.28 kB
#!/usr/bin/env python3
"""
Restart MCP Server Script
Stops existing servers and starts the enhanced MCP harness
"""
import os
import sys
import subprocess
import time
import signal
from pathlib import Path
def find_and_kill_processes():
"""Find and kill Python processes that might be running our servers."""
print("๐ Looking for running MCP server processes...")
try:
# Find Python processes that might be our servers
result = subprocess.run(['netstat', '-ano'], capture_output=True, text=True)
if result.returncode == 0:
lines = result.stdout.split('\n')
python_processes = []
for line in lines:
if '8000' in line or '3001' in line or '3002' in line or '3003' in line:
print(f"๐ Found process using port: {line.strip()}")
# Extract PID from the last column
parts = line.strip().split()
if len(parts) > 4:
try:
pid = int(parts[-1])
python_processes.append(pid)
except ValueError:
pass
# Kill the processes
for pid in python_processes:
try:
print(f"๐ Stopping process {pid}...")
os.kill(pid, signal.SIGTERM)
time.sleep(1)
except ProcessLookupError:
print(f"โ ๏ธ Process {pid} already stopped")
except PermissionError:
print(f"โ ๏ธ No permission to stop process {pid}")
print("โ
Process cleanup completed")
except FileNotFoundError:
print("โ ๏ธ netstat not available, trying alternative method...")
# Alternative: try to kill common Python processes
try:
subprocess.run(['taskkill', '/f', '/im', 'python.exe'],
capture_output=True, text=True)
subprocess.run(['taskkill', '/f', '/im', 'py.exe'],
capture_output=True, text=True)
print("โ
Alternative cleanup completed")
except FileNotFoundError:
print("โ ๏ธ taskkill not available")
def start_enhanced_harness():
"""Start the enhanced MCP harness."""
print("\n๐ Starting Enhanced MCP Harness...")
print("=" * 40)
try:
# Start the enhanced harness
subprocess.Popen([sys.executable, 'enhanced_mcp_harness.py'])
print("โ
Enhanced MCP Harness started on localhost:8000")
# Wait a moment for server to start
time.sleep(2)
# Start the enhanced web interface
print("\n๐ Starting Enhanced Web Interface...")
subprocess.Popen([sys.executable, 'enhanced_web_interface.py'])
print("โ
Enhanced Web Interface started on localhost:3003")
print("\n๐ All servers started successfully!")
print("\n๐ Available URLs:")
print(" โข Enhanced Web Interface: http://localhost:3003")
print(" โข MCP Server: localhost:8000")
print("\n๐ก You can now:")
print(" โข Open http://localhost:3003 in your browser")
print(" โข Test with: py -3 test_client.py")
print(" โข Use any MCP client to connect to localhost:8000")
except Exception as e:
print(f"โ Error starting servers: {e}")
def main():
"""Main function to restart servers."""
print("๐ MCP Server Restart Script")
print("=" * 30)
# Step 1: Stop existing processes
print("\n1๏ธโฃ Stopping existing servers...")
find_and_kill_processes()
# Step 2: Wait a moment
print("\n2๏ธโฃ Waiting for cleanup...")
time.sleep(3)
# Step 3: Start enhanced harness
print("\n3๏ธโฃ Starting enhanced servers...")
start_enhanced_harness()
print("\nโ
Restart completed!")
print("๐ Press Ctrl+C in the server windows to stop them")
if __name__ == "__main__":
main()