Files
magpie/internal/library/hermes_mcp_test.go
T
yetoneandggbdpq e413512d8e library: an agent whose CLI is gone (only its config folder left) is no Library write target, and nothing it already has is removed (#2254, ggbdpq)
After an agent is uninstalled, the Agents page shows "CLI not found"
(d0fcb8bc), but Library still offered it for skills, MCP servers, shared
instructions and rtk, and a sync wrote into a folder nothing reads.

- library.Targets() leaves out an agent whose CLI is missing
  (agent.CLIMissing: false for WSL, plugin and detect agents), so sync,
  the read view, market, instructions, skills and found servers all
  drop it at their one upstream.
- Takes() refuses a gone agent by name, so `library mcp add ...
  agents=hermes` says "Hermes Agent's CLI isn't installed here; nothing
  reads its MCP servers until it is installed again" instead of "every
  agent already has it".
- The guards that must still see a gone agent's files (projects'
  notGlobal for skills and MCP, instructions' own-file check) read
  everyTarget(), so a set kept in the agent's own file is still refused.
- rtk: a gone agent isn't offered rtk; one that still has the hook is
  listed so the hook can be taken out.
- Nothing a user wrote is removed: sync acts per target, so a gone
  agent's skills, servers and AGENTS.md stay as they are, and the client
  keeps agent ids it doesn't show.

Run:
- Sandbox HOME, real binaries: old wrote mcp_servers into a gone
  ~/.hermes/config.yaml; new refuses with the message above, exit 1.
- New tests TestTargetsLeaveGoneAgentsOut, TestGoneAgentKeepsWhatItHas,
  TestRTKLeavesGoneAgentsOut; each broken by hand (Targets check,
  Takes refusal, everyTarget in instructions, rtk gate) goes red.
- go test -tags nogui ./internal/library ./internal/tui ./internal/cli
  ./internal/gui: ok. Fixtures that relied on a gone CLI now place one
  (holdCLI, TestRTKHermesHome, TestRTKOffPath), and so do profile's
  and davsync's sandboxes, whose library syncs to claude and codex
  (TestOldProfiles, TestProfileCarriesLibrary, TestSyncLibrary went red
  in the full run without them).
- gofmt -l clean; go vet -tags nogui darwin/linux/windows clean.
- go test -tags nogui -p 4 ./... at the pushed head: see the last
  commit of this push.

Not tried: the real GUI with an uninstalled agent on a live machine.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-10-15 06:07:43 +08:00

153 lines
5.4 KiB
Go

package library
import (
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/appdir"
"gopkg.in/yaml.v3"
)
// hermesConfig is a Hermes config.yaml as the user keeps it: their model,
// a comment, and a server of their own with settings magpie doesn't write.
const hermesConfig = `# my Hermes settings
model:
default: anthropic/claude-sonnet-4.6 # the one I like
provider: openrouter
mcp_servers:
mine:
command: uvx
args: [mine-mcp]
timeout: 30
toolsets:
- hermes-cli
`
// EUPH on Discord: the Library's MCP servers couldn't be given to Hermes.
// Hermes reads mcp_servers from $HERMES_HOME/config.yaml (tools/mcp_tool.py):
// command/args/env, url/headers, transport: sse for SSE; each is written
// there beside the user's settings, comments and servers, read back, and
// only magpie's taken out again.
func TestHermesMCP(t *testing.T) {
h := sandbox(t)
dir := filepath.Join(h, "hermes-home")
t.Setenv("HERMES_HOME", dir)
p := filepath.Join(dir, "config.yaml")
write(t, p, hermesConfig)
holdCLI(t, h, "hermes") // the config alone is gone (#2173, #2177)
tg := targetByID("hermes")
if tg == nil || tg.MCP == nil || tg.MCP.Path != p {
t.Fatalf("hermes has no MCP target: %+v", tg)
}
if id, err := Takes("hermes-agent", "mcp"); id != "hermes" || err != nil {
t.Errorf("Takes: %q %v", id, err)
}
if tg.Skills != filepath.Join(dir, "skills") {
t.Errorf("skills: %q", tg.Skills)
}
agents := []string{"hermes"}
stdio := Server{Name: "fs", Transport: "stdio", Command: "npx", Args: []string{"-y", "@mcp/fs"}, Env: map[string]string{"K": "V"}, Agents: agents}
web := Server{Name: "web", Transport: "http", URL: "https://example.com/mcp", Headers: map[string]string{"Authorization": "Bearer x"}, Agents: agents}
sse := Server{Name: "old", Transport: "sse", URL: "https://example.com/sse", Agents: agents}
for _, s := range []Server{stdio, web, sse} {
ok(t)(SaveServer("", s))
}
got := read(t, p)
for _, want := range []string{"# my Hermes settings", "# the one I like", "provider: openrouter", "- hermes-cli"} {
if !strings.Contains(got, want) {
t.Errorf("lost %q:\n%s", want, got)
}
}
var f struct {
Servers map[string]map[string]any `yaml:"mcp_servers"`
}
if err := yaml.Unmarshal([]byte(got), &f); err != nil {
t.Fatalf("%v:\n%s", err, got)
}
if fs := f.Servers["fs"]; fs["command"] != "npx" || fs["url"] != nil || fs["transport"] != nil {
t.Errorf("fs: %v", fs)
} else if e, _ := fs["env"].(map[string]any); e["K"] != "V" {
t.Errorf("fs env: %v", fs)
}
if w := f.Servers["web"]; w["url"] != "https://example.com/mcp" || w["transport"] != nil || w["command"] != nil {
t.Errorf("web: %v", w)
} else if hd, _ := w["headers"].(map[string]any); hd["Authorization"] != "Bearer x" {
t.Errorf("web headers: %v", w)
}
if o := f.Servers["old"]; o["url"] != "https://example.com/sse" || o["transport"] != "sse" {
t.Errorf("sse: %v", o)
}
if m := f.Servers["mine"]; m["command"] != "uvx" || m["timeout"] != 30 {
t.Errorf("the user's server: %v", m)
}
// written in Hermes' own order, command before args
if i, j := strings.Index(got, "command: npx"), strings.Index(got, "- '@mcp/fs'"); i < 0 || j < i {
t.Errorf("fs isn't written as Hermes writes it:\n%s", got)
}
read1, err := tg.MCP.read()
if err != nil {
t.Fatal(err)
}
for _, want := range []Server{stdio, web, sse} {
if s := read1[want.Name]; s == nil || !s.same(&want) {
t.Errorf("%s read back as %+v", want.Name, s)
}
}
before := read(t, p)
ok(t)(Sync())
if read(t, p) != before {
t.Errorf("a sync rewrote servers that are as the library has them:\n%s", read(t, p))
}
// a timeout or enabled the user sets on magpie's server stays when it
// is written again
got = strings.Replace(read(t, p), " url: https://example.com/mcp\n", " url: https://example.com/mcp\n enabled: false\n timeout: 90\n", 1)
write(t, p, got)
ok(t)(SaveServer("web", Server{Name: "web", Transport: "http", URL: "https://example.com/v2", Agents: agents}))
yaml.Unmarshal([]byte(read(t, p)), &f)
if w := f.Servers["web"]; w["url"] != "https://example.com/v2" || w["enabled"] != false || w["timeout"] != 90 || w["headers"] != nil {
t.Errorf("web after edit: %v\n%s", w, read(t, p))
}
for _, s := range []string{"fs", "web", "old"} {
ok(t)(RemoveServer(s))
}
after, _ := tg.MCP.read()
if len(after) != 1 || after["mine"] == nil || after["mine"].Command != "uvx" {
t.Errorf("after: %+v\n%s", after, read(t, p))
}
got = read(t, p)
for _, want := range []string{"# my Hermes settings", "# the one I like", "timeout: 30"} {
if !strings.Contains(got, want) {
t.Errorf("lost %q:\n%s", want, got)
}
}
}
// A server Hermes has that the library doesn't is found, to bring in.
func TestHermesMCPFound(t *testing.T) {
h := sandbox(t)
dir := appdir.HermesHome()
write(t, filepath.Join(dir, "config.yaml"), strings.Replace(hermesConfig, "toolsets:", " remote:\n url: https://r.example/sse\n transport: sse\ntoolsets:", 1))
holdCLI(t, h, "hermes") // the config alone is gone (#2173, #2177)
l, err := load()
if err != nil {
t.Fatal(err)
}
found := map[string]*Server{}
for _, f := range foundServers(l) {
found[f.Server.Name] = f.Server
}
if s := found["mine"]; s == nil || s.Command != "uvx" || !strings.Contains(strings.Join(s.Agents, ","), "hermes") {
t.Errorf("mine: %+v", s)
}
if s := found["remote"]; s == nil || s.Transport != "sse" || s.URL != "https://r.example/sse" {
t.Errorf("remote: %+v", s)
}
}