setup_dev.pyā¢4.22 kB
#!/usr/bin/env python3
"""
Development setup script for MCP Server Demo
This script helps set up the development environment for the MCP server project.
"""
import subprocess
import sys
import os
from pathlib import Path
def run_command(command: str, description: str) -> bool:
"""Run a command and return True if successful"""
print(f"š {description}...")
try:
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
print(f"ā
{description} completed successfully")
return True
except subprocess.CalledProcessError as e:
print(f"ā {description} failed: {e}")
if e.stdout:
print(f"Output: {e.stdout}")
if e.stderr:
print(f"Error: {e.stderr}")
return False
def check_python_version():
"""Check if Python version is >= 3.10"""
version = sys.version_info
if version.major < 3 or (version.major == 3 and version.minor < 10):
print(f"ā Python 3.10+ required, found {version.major}.{version.minor}")
print("Please upgrade Python and try again.")
return False
print(f"ā
Python {version.major}.{version.minor}.{version.micro} is compatible")
return True
def setup_virtual_environment():
"""Create and activate virtual environment"""
venv_path = Path("venv")
if venv_path.exists():
print("ā
Virtual environment already exists")
return True
return run_command("python3 -m venv venv", "Creating virtual environment")
def install_dependencies():
"""Install project dependencies"""
# Determine the correct pip command based on OS
if os.name == 'nt': # Windows
pip_cmd = "venv\\Scripts\\pip"
else: # Unix-like systems
pip_cmd = "venv/bin/pip"
# Upgrade pip first
upgrade_pip = run_command(f"{pip_cmd} install --upgrade pip", "Upgrading pip")
if not upgrade_pip:
return False
# Install requirements
return run_command(f"{pip_cmd} install -r requirements.txt", "Installing dependencies")
def create_run_scripts():
"""Create convenient run scripts"""
# Windows batch script
windows_script = """@echo off
call venv\\Scripts\\activate
echo Starting MCP Server in development mode...
mcp dev server.py
pause
"""
# Unix shell script
unix_script = """#!/bin/bash
source venv/bin/activate
echo "Starting MCP Server in development mode..."
mcp dev server.py
"""
try:
with open("run_dev.bat", "w") as f:
f.write(windows_script)
with open("run_dev.sh", "w") as f:
f.write(unix_script)
# Make shell script executable
os.chmod("run_dev.sh", 0o755)
print("ā
Created run_dev.bat and run_dev.sh scripts")
return True
except Exception as e:
print(f"ā Failed to create run scripts: {e}")
return False
def main():
"""Main setup function"""
print("š Setting up MCP Server Demo development environment...\n")
# Check Python version
if not check_python_version():
sys.exit(1)
# Setup virtual environment
if not setup_virtual_environment():
print("ā Failed to setup virtual environment")
sys.exit(1)
# Install dependencies
if not install_dependencies():
print("ā Failed to install dependencies")
sys.exit(1)
# Create run scripts
if not create_run_scripts():
print("ā ļø Warning: Failed to create run scripts, but setup is otherwise complete")
print("\nš Development environment setup complete!")
print("\nNext steps:")
print("1. Activate the virtual environment:")
if os.name == 'nt':
print(" venv\\Scripts\\activate")
else:
print(" source venv/bin/activate")
print("\n2. Run the server in development mode:")
print(" mcp dev server.py")
print("\n3. Or use the convenience script:")
if os.name == 'nt':
print(" run_dev.bat")
else:
print(" ./run_dev.sh")
print("\n4. To install in Claude Desktop:")
print(" mcp install server.py")
if __name__ == "__main__":
main()