setup.ps1โข8.25 kB
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Setup script for MCP implementation
.DESCRIPTION
This script sets up the MCP server environment, installs dependencies,
and configures the development environment for Windows.
.PARAMETER Language
Choose implementation language: 'node' or 'python' (default: 'node')
.PARAMETER Force
Force reinstall of dependencies
.EXAMPLE
.\scripts\setup.ps1
.\scripts\setup.ps1 -Language python
.\scripts\setup.ps1 -Force
#>
param(
[Parameter(Mandatory=$false)]
[ValidateSet('node', 'python')]
[string]$Language = 'node',
[Parameter(Mandatory=$false)]
[switch]$Force
)
# Set error action preference
$ErrorActionPreference = 'Stop'
# Colors for output
$colors = @{
Success = 'Green'
Warning = 'Yellow'
Error = 'Red'
Info = 'Cyan'
Header = 'Magenta'
}
function Write-ColorOutput {
param(
[string]$Message,
[string]$Color = 'White'
)
Write-Host $Message -ForegroundColor $colors[$Color]
}
function Test-CommandExists {
param([string]$Command)
try {
Get-Command $Command -ErrorAction Stop | Out-Null
return $true
} catch {
return $false
}
}
function Install-NodeDependencies {
Write-ColorOutput "๐ฆ Installing Node.js dependencies..." -Color Info
if (-not (Test-CommandExists 'node')) {
Write-ColorOutput "โ Node.js not found. Please install Node.js v18+ from https://nodejs.org/" -Color Error
exit 1
}
$nodeVersion = node --version
Write-ColorOutput "โ
Found Node.js $nodeVersion" -Color Success
if (-not (Test-CommandExists 'npm')) {
Write-ColorOutput "โ npm not found. Please ensure npm is installed with Node.js" -Color Error
exit 1
}
if ($Force) {
Write-ColorOutput "๐๏ธ Cleaning node_modules..." -Color Warning
if (Test-Path "node_modules") {
Remove-Item "node_modules" -Recurse -Force
}
if (Test-Path "package-lock.json") {
Remove-Item "package-lock.json" -Force
}
}
npm install
if ($LASTEXITCODE -ne 0) {
Write-ColorOutput "โ Failed to install Node.js dependencies" -Color Error
exit 1
}
Write-ColorOutput "โ
Node.js dependencies installed successfully" -Color Success
}
function Install-PythonDependencies {
Write-ColorOutput "๐ Installing Python dependencies..." -Color Info
if (-not (Test-CommandExists 'python')) {
Write-ColorOutput "โ Python not found. Please install Python 3.8+ from https://python.org/" -Color Error
exit 1
}
$pythonVersion = python --version
Write-ColorOutput "โ
Found $pythonVersion" -Color Success
if (-not (Test-CommandExists 'pip')) {
Write-ColorOutput "โ pip not found. Please ensure pip is installed with Python" -Color Error
exit 1
}
# Create virtual environment
if (-not (Test-Path "venv")) {
Write-ColorOutput "๐๏ธ Creating Python virtual environment..." -Color Info
python -m venv venv
}
# Activate virtual environment
Write-ColorOutput "๐ Activating virtual environment..." -Color Info
& ".\venv\Scripts\Activate.ps1"
# Upgrade pip
python -m pip install --upgrade pip
# Install dependencies
if ($Force) {
pip install -r requirements.txt --force-reinstall
} else {
pip install -r requirements.txt
}
if ($LASTEXITCODE -ne 0) {
Write-ColorOutput "โ Failed to install Python dependencies" -Color Error
exit 1
}
Write-ColorOutput "โ
Python dependencies installed successfully" -Color Success
}
function Setup-Environment {
Write-ColorOutput "โ๏ธ Setting up environment..." -Color Info
# Copy .env.example to .env if it doesn't exist
if (-not (Test-Path ".env")) {
if (Test-Path ".env.example") {
Copy-Item ".env.example" ".env"
Write-ColorOutput "โ
Created .env file from template" -Color Success
Write-ColorOutput "๐ Please edit .env file with your configuration" -Color Warning
} else {
Write-ColorOutput "โ ๏ธ .env.example not found, skipping .env creation" -Color Warning
}
} else {
Write-ColorOutput "โ
.env file already exists" -Color Success
}
# Create necessary directories
$dirs = @('temp', 'logs')
foreach ($dir in $dirs) {
if (-not (Test-Path $dir)) {
New-Item -ItemType Directory -Path $dir | Out-Null
Write-ColorOutput "โ
Created $dir directory" -Color Success
}
}
}
function Test-Setup {
Write-ColorOutput "๐งช Testing setup..." -Color Info
if ($Language -eq 'node') {
# Test Node.js server
Write-ColorOutput "Testing Node.js server..." -Color Info
$job = Start-Job -ScriptBlock {
Set-Location $using:PWD
node server.js
}
Start-Sleep -Seconds 3
try {
$response = Invoke-RestMethod -Uri "http://localhost:3000/health" -TimeoutSec 5
if ($response.status -eq 'healthy') {
Write-ColorOutput "โ
Node.js server test passed" -Color Success
} else {
Write-ColorOutput "โ Node.js server test failed" -Color Error
}
} catch {
Write-ColorOutput "โ Node.js server test failed: $($_.Exception.Message)" -Color Error
} finally {
Stop-Job $job -Force
Remove-Job $job -Force
}
} else {
# Test Python server
Write-ColorOutput "Testing Python server..." -Color Info
& ".\venv\Scripts\Activate.ps1"
$job = Start-Job -ScriptBlock {
Set-Location $using:PWD
& ".\venv\Scripts\Activate.ps1"
python server.py --http
}
Start-Sleep -Seconds 5
try {
$response = Invoke-RestMethod -Uri "http://localhost:3000/health" -TimeoutSec 5
if ($response.status -eq 'healthy') {
Write-ColorOutput "โ
Python server test passed" -Color Success
} else {
Write-ColorOutput "โ Python server test failed" -Color Error
}
} catch {
Write-ColorOutput "โ Python server test failed: $($_.Exception.Message)" -Color Error
} finally {
Stop-Job $job -Force
Remove-Job $job -Force
}
}
}
# Main execution
try {
Write-ColorOutput "๐ MCP Implementation Setup" -Color Header
Write-ColorOutput "Language: $Language" -Color Info
Write-ColorOutput "Force reinstall: $Force" -Color Info
Write-Host ""
# Check prerequisites
Write-ColorOutput "๐ Checking prerequisites..." -Color Info
if (-not (Test-CommandExists 'git')) {
Write-ColorOutput "โ Git not found. Please install Git from https://git-scm.com/" -Color Error
exit 1
}
Write-ColorOutput "โ
Git is available" -Color Success
# Install dependencies based on language choice
if ($Language -eq 'node') {
Install-NodeDependencies
} else {
Install-PythonDependencies
}
# Setup environment
Setup-Environment
# Test setup
Test-Setup
Write-Host ""
Write-ColorOutput "๐ Setup completed successfully!" -Color Success
Write-ColorOutput "" -Color Info
Write-ColorOutput "Next steps:" -Color Header
Write-ColorOutput "1. Edit .env file with your configuration" -Color Info
Write-ColorOutput "2. Run the development server:" -Color Info
if ($Language -eq 'node') {
Write-ColorOutput " npm run dev" -Color Success
} else {
Write-ColorOutput " .\\scripts\\start-dev.ps1" -Color Success
}
Write-ColorOutput "3. Configure your AI client with the MCP server" -Color Info
Write-ColorOutput "4. Visit http://localhost:3000/health to check server status" -Color Info
} catch {
Write-ColorOutput "โ Setup failed: $($_.Exception.Message)" -Color Error
exit 1
}