Files
magpie/internal/cli/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

60 lines
1.8 KiB
Go

package cli
import (
"os"
"path/filepath"
"slices"
"testing"
"github.com/yetone/magpie/internal/library"
)
// magpie library mcp off|on <name> (swang20201 on X): off takes the
// server from its agents and keeps it; on gives it back to them; a flag
// is never taken as a name.
func TestLibraryMCPOffOn(t *testing.T) {
h := t.TempDir()
for k, v := range map[string]string{"HOME": h, "USERPROFILE": h, "XDG_CONFIG_HOME": filepath.Join(h, ".config"), "CLAUDE_CONFIG_DIR": "", "CODEX_HOME": "", "APPDATA": "", "LOCALAPPDATA": ""} {
t.Setenv(k, v)
}
if err := os.MkdirAll(filepath.Join(h, ".claude"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(h, ".claude/settings.json"), nil, 0o644); err != nil {
t.Fatal(err)
}
if _, err := library.SaveServer("", library.Server{Name: "fs", Transport: "stdio", Command: "npx", Env: map[string]string{"K": "v"}, Agents: []string{"claude"}}); err != nil {
t.Fatal(err)
}
server := func() library.ServerView {
t.Helper()
v, err := library.Read(nil)
if err != nil {
t.Fatal(err)
}
for _, s := range v.Servers {
if s.Name == "fs" {
return s
}
}
t.Fatal("fs is gone")
return library.ServerView{}
}
if err := libraryCmd([]string{"library", "mcp", "off", "fs"}); err != nil {
t.Fatal(err)
}
if s := server(); !s.Hidden || len(s.Agents) != 0 || !slices.Equal(s.Paused, []string{"claude"}) || s.Env["K"] != "v" {
t.Fatalf("off: %+v", s.Server)
}
if err := libraryCmd([]string{"library", "mcp", "on", "--help"}); err == nil {
t.Error("a flag was taken as a server's name")
}
if err := libraryCmd([]string{"library", "mcp", "on", "fs"}); err != nil {
t.Fatal(err)
}
if s := server(); s.Hidden || !slices.Equal(s.Agents, []string{"claude"}) || s.Env["K"] != "v" {
t.Fatalf("on: %+v", s.Server)
}
}