Files
skillshare/internal/mcp/check_live_unix_test.go
T
Willie b7969308bf feat(mcp): probe servers live with mcp check --live
The static check can say a variable is set and a command is on PATH,
but not whether the server actually starts and speaks MCP, which is the
other half of "synced, but the Agent does not see it".

--live runs after the static checks and only for servers without a
static error. A stdio server is started with the current environment
plus its env and fromEnv values, probed with server/discover and, for
servers before 2026-07-28, the initialize handshake, then stopped:
stdin closed, then SIGTERM and SIGKILL to its process group. A remote
server gets a Streamable HTTP POST with its resolved headers; a 401
reports the WWW-Authenticate resource metadata as a warning and never
signs in. Each probe has one timeout (--timeout, default 10s), a few
run at once, and configured values are redacted from every message.

Without --live nothing is started and no request is sent, and the
dashboard API stays static.

Refs #141
2026-09-30 14:35:41 +08:00

57 lines
1.5 KiB
Go

//go:build !windows
package mcp
import (
"fmt"
"os"
"path/filepath"
"strings"
"syscall"
"testing"
"time"
)
// processGone treats a zombie as gone: a container's init may never reap an orphan.
func processGone(pid int) bool {
if err := syscall.Kill(pid, 0); err != nil {
return true
}
stat, err := os.ReadFile(fmt.Sprintf("/proc/%d/stat", pid))
if err != nil {
return os.IsNotExist(err)
}
fields := strings.Fields(string(stat[strings.LastIndexByte(string(stat), ')')+1:]))
return len(fields) > 0 && fields[0] == "Z"
}
func TestLiveStdioTimeoutKillsProcessGroup(t *testing.T) {
pids := filepath.Join(t.TempDir(), "pids")
start := time.Now()
report, err := checkService(t, liveConfig("hang", map[string]string{"MCP_LIVE_PIDS": pids})).Check(liveCheck(1500*time.Millisecond, tokenEnv))
if err != nil {
t.Fatal(err)
}
if _, f := liveOf(t, report); f.Level != "error" || !strings.Contains(f.Message, "no answer within 1.5s") {
t.Fatalf("finding = %+v", f)
}
if elapsed := time.Since(start); elapsed > 5*time.Second {
t.Fatalf("the probe took %s", elapsed)
}
data, err := os.ReadFile(pids)
if err != nil {
t.Fatal(err)
}
var server, child int
if _, err := fmt.Sscanf(string(data), "%d %d", &server, &child); err != nil {
t.Fatal(err)
}
deadline := time.Now().Add(2 * time.Second)
for !(processGone(server) && processGone(child)) {
if time.Now().After(deadline) {
t.Fatalf("server %d or its child %d is still running", server, child)
}
time.Sleep(20 * time.Millisecond)
}
}