Files
magpie/internal/library/mcp_on_test.go
T
yetone 4869d0c966 library: an MCP server switches off for every agent and app, and on again as it was (swang20201 on X)
An installed server in Library could only be removed. Each server row
now has a switch after its agent chips. Per-agent chips already
existed, but in Docker gateway mode there are none, and the MCP router
gives every server to any app at its address, so nothing could turn a
server off there.

ServerOn (POST /api/library/servers/on, `magpie library mcp off|on
<name>`) reuses the Hidden/Paused pair a plugin's hidden server
already has. Off: Hidden, with its agents kept in Paused. On: those
agents get it back, or none when it had none, so the router gives it
to other apps again. Its command, env, headers, sign-in, DisabledTools
and note stay in library.json. Off, it is also left out of every
project's files (syncProjectMCP) and of subagents' mcpServers
(renderSubagent), which before had only skipped a server gone from
the library. The router's held process ends on the next prune,
because Hidden/Paused are in its hubKey. An off row is dimmed, its
chips take no click, and it is health-checked only on a click.
Strings are in zh, zh-TW, ja and de. docs/subsystems/library.md has
a row for it, and mcp-router.md names it.

Run (mote):
- go test ./internal/library ./internal/cli ./internal/gui: ok.
- go vet ./..., GOOS=linux -tags nogui, GOOS=windows: ok.
- gofmt -l: empty.
- GUI: the 145 files that touch the Library, i18n-keys and gui-*, in
  chromium and webkit. 2798 of 2881 passed.
  - i18n-keys was red because the key "{name} on" also matched core's
    "{name} on magpie". The key is now "Turn {name} on or off", and
    i18n-keys and library-mcp-on are green (26/26).
  - Nine files are red the same way on a clean origin/main 217d77848,
    with the same set failing on this branch: library-hooks,
    library-subagents, library-loading, library-items, list-sort,
    market-have, mobile-web, sidebar-tab-top and skill-where. The Library's
    tab strip shows MCP and has no RTK. Filed as #5850.
- library-mcp-on.test.cjs: en, zh, zh-TW, ja and de, in the app at
  1100px and in gateway mode at 420px, both engines, 22/22.

Broken by hand, each went red:
- project_mcp without the Hidden skip: the project's .mcp.json kept it.
- subagents without it: the subagent still named it.
- ServerOn not giving Paused back: on again, claude's file lacked it.
- the row without its switch, and the CSS without pointer-events:none
  on an off row's chips: library-mcp-on red.
- A Hidden check added to hubPrune stayed green, because hubKey
  already ends the process, so it was left out. The test still checks
  that the process ends.

Not tried: the real Docker image, and a real MCP server with a
sign-in (the test's server is a fake stdio one). The TUI has no
switch (the CLI has one).
2026-11-10 10:23:53 +08:00

150 lines
4.7 KiB
Go

package library
import (
"context"
"encoding/json"
"path/filepath"
"slices"
"strings"
"testing"
"time"
)
// Off, a server is given to no agent, no project and no app at the MCP
// router's address, and a process the router holds for it ends; its
// settings, env and agents are kept. On, it comes back as it was
// (swang20201 on X: a server in Library could only be removed).
func TestServerOnKeepsAndRestores(t *testing.T) {
h := hubSandbox(t)
fake := fakeStdio(t, "ok", map[string]string{"TOKEN": "secret"})
fake.Agents = []string{"claude"}
fake.DisabledTools = []string{"b"}
ok(t)(SaveServer("", *fake))
proj := filepath.Join(t.TempDir(), "app")
write(t, filepath.Join(proj, ".mcp.json"), `{"mcpServers": {"team": {"command": "team-mcp"}}}`)
ok(t)(AddProject(proj))
ok(t)(ProjectServer(proj, fake.Name, []string{"claude"}))
sub := reviewer
sub.MCP, sub.Skills, sub.Agents = []string{fake.Name}, nil, []string{"claude"}
ok(t)(SaveSubagent("", sub))
ok(t)(Sync())
claude := filepath.Join(h, ".claude.json")
mcp := filepath.Join(proj, ".mcp.json")
kept := func(when string) {
t.Helper()
l, err := load()
if err != nil {
t.Fatal(err)
}
s := l.server(fake.Name)
if s == nil || !s.Hidden || len(s.Agents) != 0 || !slices.Equal(s.Paused, []string{"claude"}) ||
s.Env["TOKEN"] != "secret" || !slices.Equal(s.DisabledTools, []string{"b"}) {
t.Fatalf("%s: kept as %+v", when, s)
}
}
subFile := filepath.Join(h, ".claude", "agents", "reviewer.md")
inFiles := func() (user, project bool) {
t.Helper()
user = servers(t, claude, "mcpServers")[fake.Name] != nil
if named := strings.Contains(read(t, subFile), `mcpServers: ["fake"]`); named != user {
t.Fatalf("claude's file has it %v, the subagent names it %v:\n%s", user, named, read(t, subFile))
}
return user, servers(t, mcp, "mcpServers")[fake.Name] != nil
}
if u, p := inFiles(); !u || !p {
t.Fatalf("to begin with: in claude's file %v, the project's %v", u, p)
}
ok(t)(ServerOn(fake.Name, false))
for _, when := range []string{"off", "off twice", "synced"} {
switch when {
case "off twice":
ok(t)(ServerOn(fake.Name, false))
case "synced":
ok(t)(Sync())
}
if u, p := inFiles(); u || p {
t.Fatalf("%s: in claude's file %v, the project's %v", when, u, p)
}
if servers(t, mcp, "mcpServers")["team"] == nil {
t.Fatalf("%s: the project's own server went", when)
}
kept(when)
}
ok(t)(ServerOn(fake.Name, true))
if u, p := inFiles(); !u || !p {
t.Fatalf("on again: in claude's file %v, the project's %v", u, p)
}
env, _ := servers(t, claude, "mcpServers")[fake.Name].(map[string]any)["env"].(map[string]any)
if l, _ := load(); env["TOKEN"] != "secret" || l.server(fake.Name).Hidden || l.server(fake.Name).Paused != nil ||
!slices.Equal(l.server(fake.Name).Agents, []string{"claude"}) {
t.Fatalf("on again: env %v, %+v", env, l.server(fake.Name))
}
// at the MCP router's address, the way Docker's gateway mode gives it
ok(t)(SetMCPHub(true))
ctx := context.Background()
hub := theHub{}
listed := func(agentID string) bool {
return slices.Contains(hubNames(hub.Tools(ctx, agentID)), "fake/a")
}
if !listed("claude") || !listed("") {
t.Fatal("the router doesn't give it to begin with")
}
if _, err := hub.Call(ctx, "", fake.Name, "a", json.RawMessage(`{}`)); err != nil {
t.Fatal(err)
}
hubPool.Lock()
b := hubPool.m[fake.Name]
hubPool.Unlock()
if b == nil {
t.Fatal("no server held after a call")
}
b.mu.Lock()
c := b.conn
b.mu.Unlock()
ok(t)(ServerOn(fake.Name, false))
if listed("claude") || listed("") {
t.Fatal("off, the router still lists it")
}
if _, err := hub.Call(ctx, "", fake.Name, "a", json.RawMessage(`{}`)); err == nil {
t.Fatal("off, an app at the router's address ran it")
}
kept("off at the router")
hubPrune()
ended := false
for end := time.Now().Add(10 * time.Second); time.Now().Before(end); time.Sleep(10 * time.Millisecond) {
if !c.alive() {
ended = true
break
}
}
if !ended {
t.Fatal("the router's process for a server turned off still runs")
}
ok(t)(ServerOn(fake.Name, true))
if !listed("claude") || !listed("") {
t.Fatal("on again, the router doesn't give it")
}
// one ticked for no agent, which the router alone gives other apps
ok(t)(ServerAgents(fake.Name, nil))
if !listed("") {
t.Fatal("a server ticked for no agent isn't given to other apps")
}
ok(t)(ServerOn(fake.Name, false))
if listed("") {
t.Fatal("off, an app at the router's address still has it")
}
ok(t)(ServerOn(fake.Name, true))
if l, _ := load(); !listed("") || len(l.server(fake.Name).Agents) != 0 {
t.Fatalf("on again with no agent: %+v", l.server(fake.Name))
}
if _, err := ServerOn("nope", false); err == nil {
t.Fatal("no error for a server that isn't there")
}
}