Skip to content

Open-source Revit MCP server: let Claude and other AI assistants read and edit BIM models, with human approval on every change.

4.9 KB
Raw Blame
param(
    [string]$Mode = "bridge",
    [string]$RevitVersion
)

$ErrorActionPreference = "Stop"
$repoRoot = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot ".."))
$packageRoot = Join-Path $repoRoot "packages\mcp-server-revit"

Write-Host "AEC Model Bridge Server Startup" -ForegroundColor Cyan
Write-Host "=====================`n" -ForegroundColor Cyan

# Check if .env exists
$envFile = Join-Path $repoRoot ".env"
if (-not (Test-Path $envFile)) {
    Write-Host ".env file not found. Creating from example..." -ForegroundColor Yellow
    $envExample = Join-Path $repoRoot ".env.example"
    if (Test-Path $envExample) {
        Copy-Item $envExample $envFile
        Write-Host "Created .env file. Please edit it with your paths!" -ForegroundColor Green
        Write-Host "Opening .env in notepad..." -ForegroundColor Yellow
        Start-Process notepad $envFile
        Write-Host "`nPress Enter after editing .env to continue..." -ForegroundColor Yellow
        Read-Host
    }
    else {
        Write-Error ".env.example not found. Cannot continue."
        exit 1
    }
}

# Check if Python is available
if (-not (Get-Command python -ErrorAction SilentlyContinue)) {
    Write-Error "Python not found. Please install Python 3.11 or later."
    exit 1
}

Write-Host "Python version:" -ForegroundColor Yellow
python --version

# Check if in virtual environment
$inVenv = $env:VIRTUAL_ENV -ne $null
if (-not $inVenv) {
    Write-Host "`nWARNING: Not in a virtual environment." -ForegroundColor Yellow
    Write-Host "It's recommended to use a virtual environment." -ForegroundColor Yellow
    Write-Host "Create one with: python -m venv venv" -ForegroundColor Gray
    Write-Host "Activate with: .\venv\Scripts\Activate.ps1`n" -ForegroundColor Gray
}

# Install/update dependencies
Write-Host "`nInstalling Python MCP server package..." -ForegroundColor Yellow
Push-Location $packageRoot
try {
    python -m pip install -e . --quiet
    if ($LASTEXITCODE -ne 0) {
        Write-Error "Failed to install Python package"
        exit 1
    }
    Write-Host "Package installed successfully" -ForegroundColor Green
}
finally {
    Pop-Location
}

# Display configuration
Write-Host "`nConfiguration:" -ForegroundColor Yellow
Get-Content $envFile | Where-Object { $_ -notmatch "^#" -and $_ -match "\S" } | ForEach-Object {
    Write-Host "  $_" -ForegroundColor Gray
}
$env:MCP_REVIT_MODE = $Mode
if ($RevitVersion) {
    $env:MCP_REVIT_HOST_VERSION = $RevitVersion
}

# Check if Revit is running (if bridge mode)
if ($Mode -eq "bridge") {
    Write-Host "`nChecking Revit bridge..." -ForegroundColor Yellow
    try {
        $registryDir = Join-Path $env:LOCALAPPDATA "AECModelBridge\registry"
        $switches = @()
        if (Test-Path -LiteralPath $registryDir) {
            $switches = Get-ChildItem -LiteralPath $registryDir -Filter "*.json" -File |
                ForEach-Object {
                    try { Get-Content -LiteralPath $_.FullName -Raw | ConvertFrom-Json } catch { $null }
                } |
                Where-Object {
                    $_ -and $_.provider_id -eq "revit" -and
                    (-not $RevitVersion -or $_.host_version -like "$RevitVersion*")
                } |
                Sort-Object started_at -Descending
        }

        if (-not $switches -or $switches.Count -eq 0) {
            $target = if ($RevitVersion) { "Revit $RevitVersion" } else { "a Revit bridge" }
            throw "Could not find $target in $registryDir"
        }

        $switch = $switches | Select-Object -First 1
        $response = Invoke-WebRequest -Uri "$($switch.endpoint)/health" -Method GET -TimeoutSec 2 -UseBasicParsing
        $health = $response.Content | ConvertFrom-Json
        Write-Host "Bridge status: $($health.status)" -ForegroundColor Green
        Write-Host "Revit version: $($health.revit_version)" -ForegroundColor Green
        Write-Host "Bridge endpoint: $($switch.endpoint)" -ForegroundColor Green
        if ($health.active_document) {
            Write-Host "Active document: $($health.active_document)" -ForegroundColor Green
        }
    }
    catch {
        Write-Host "WARNING: Cannot connect to Revit bridge!" -ForegroundColor Red
        Write-Host "Make sure Revit is running with a project open." -ForegroundColor Yellow
        Write-Host "The bridge should be registered under %LOCALAPPDATA%\AECModelBridge\registry." -ForegroundColor Yellow
        Write-Host "`nContinuing anyway... (server will fail on first tool call)`n" -ForegroundColor Gray
    }
}

# Start the server
Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host "Starting MCP Server (mode: $Mode)" -ForegroundColor Green
Write-Host "========================================`n" -ForegroundColor Cyan

Write-Host "Server is now listening for JSON-RPC requests on stdin/stdout" -ForegroundColor Yellow
Write-Host "Press Ctrl+C to stop the server`n" -ForegroundColor Gray

Push-Location $packageRoot
try {
    python -m revit_mcp_server
}
finally {
    Pop-Location
}