param( [string]$BundlePath = 'build\Release\publish\winagent-sandbox-mcp', [switch]$StopOnly ) $ErrorActionPreference = 'Stop' $repository = (Resolve-Path (Join-Path $PSScriptRoot '..')).Path $bundle = [IO.Path]::GetFullPath((Join-Path $repository $BundlePath)) $publishRoot = [IO.Path]::GetFullPath((Join-Path $repository 'build\Release\publish')) if (-not $bundle.StartsWith($publishRoot + [IO.Path]::DirectorySeparatorChar, [StringComparison]::OrdinalIgnoreCase)) { throw 'BundlePath must remain inside build\Release\publish.' } $agent = Join-Path $bundle 'agent-mcp.exe' $configuration = Join-Path $bundle 'agent.yaml' $matching = Get-Process -Name 'agent-mcp' -ErrorAction SilentlyContinue | Where-Object { try { [IO.Path]::GetFullPath($_.Path).Equals($agent, [StringComparison]::OrdinalIgnoreCase) } catch { $false } } foreach ($process in $matching) { Stop-Process -Id $process.Id -Force $process.WaitForExit() Write-Output "Stopped published agent-mcp PID $($process.Id)." } if ($StopOnly) { exit 0 } if (-not (Test-Path -LiteralPath $agent) -or -not (Test-Path -LiteralPath $configuration)) { throw 'Published agent-mcp executable or configuration is missing.' } $started = Start-Process -FilePath $agent -ArgumentList @($configuration) -WorkingDirectory $bundle -WindowStyle Hidden -PassThru Write-Output "Started published agent-mcp PID $($started.Id)."