@@ -18,7 +18,15 @@ if (Get-Command bash -ErrorAction SilentlyContinue) { exit 0 }
1818
1919$Port = if ($env: MONK_AGENT_PORT ) { $env: MONK_AGENT_PORT } else { " 7419" }
2020$AgentHost = if ($env: MONK_AGENT_HOST ) { $env: MONK_AGENT_HOST } else { " 127.0.0.1" }
21- $HealthUrl = " http://${AgentHost} :$Port /.well-known/oauth-protected-resource"
21+ # IPv6 loopback hosts (e.g. ::1, an explicit MONK_AGENT_HOST override) must be
22+ # bracketed in a URL authority or the port separator is ambiguous.
23+ $UrlHost = if ($AgentHost.Contains (" :" ) -and -not ($AgentHost.StartsWith (" [" ) -and $AgentHost.EndsWith (" ]" ))) {
24+ " [$AgentHost ]"
25+ } else {
26+ $AgentHost
27+ }
28+ $HealthUrl = " http://${UrlHost} :$Port /.well-known/oauth-protected-resource"
29+ $HealthResource = " http://${UrlHost} :$Port /mcp"
2230
2331function Write-Json {
2432 param ([object ]$Object )
@@ -27,8 +35,12 @@ function Write-Json {
2735
2836function Test-AgentRunning {
2937 try {
30- $Response = Invoke-WebRequest - Uri $HealthUrl - UseBasicParsing - TimeoutSec 2
31- return $Response.Content -match ' "resource"'
38+ $Response = Invoke-WebRequest - Uri $HealthUrl - UseBasicParsing - TimeoutSec 2 - NoProxy
39+ # Require the resource field to equal our own MCP endpoint, not merely be
40+ # present — an unrelated service on the same port could otherwise be
41+ # mistaken for monk-agent.
42+ $Document = $Response.Content | ConvertFrom-Json - ErrorAction Stop
43+ return [string ]$Document.resource -ceq $HealthResource
3244 } catch {
3345 return $false
3446 }
@@ -70,10 +82,16 @@ if (-not (Test-Path $AgentPath)) {
7082
7183# Binary present but not running - start it directly (do not wait).
7284$MonkHome = if ($env: MONK_AGENT_HOME ) { $env: MONK_AGENT_HOME } else { Join-Path $HOME " .monk" }
73- $LogDir = Join-Path $MonkHome " agent\launcher\logs"
74- New-Item - ItemType Directory - Force - Path $LogDir | Out-Null
85+ $AgentDataDir = Join-Path $MonkHome " agent\launcher"
86+ $LogDir = Join-Path $AgentDataDir " logs"
87+ $RunDir = Join-Path $AgentDataDir " run"
88+ New-Item - ItemType Directory - Force - Path $LogDir , $RunDir | Out-Null
7589$LogOut = Join-Path $LogDir " monk-agent.out.log"
7690$LogErr = Join-Path $LogDir " monk-agent.err.log"
91+ # Write the PID file the official uninstaller looks for (Stop-ManagedAgent in
92+ # scripts/uninstall-monk-agent.ps1) so a companion this hook starts in the
93+ # background is found and stopped on uninstall instead of surviving it.
94+ $PidFile = Join-Path $RunDir " monk-agent.pid"
7795
7896$env: MONK_AUTH_URL = if ($env: MONK_AUTH_URL ) { $env: MONK_AUTH_URL } else { " https://auth.monk.io" }
7997$env: MONK_AGENT_AUTH_CLIENT_ID = if ($env: MONK_AGENT_AUTH_CLIENT_ID ) { $env: MONK_AGENT_AUTH_CLIENT_ID } else { " UW84YWcJME3buMSLfqLX8IbBsYdNWi47" }
@@ -92,6 +110,10 @@ try {
92110} catch {
93111}
94112
113+ if ($Process ) {
114+ Set-Content - Path $PidFile - Value $Process.Id - NoNewline:$false - ErrorAction SilentlyContinue
115+ }
116+
95117# Wait briefly for the agent to become reachable. If the process exits early or
96118# the health endpoint never responds, report an attempted start with a pointer
97119# to the logs instead of a false "has been started".
0 commit comments