Files
magpie/internal/library/mcp_options_test.go
T
yetone 8af7c27fff library: mcp add takes header=, env= and timeout= as settings, mcp set and mcp show (Abandon332 on X, #6111)
`magpie library mcp add` handed env=A=B and timeout=30 to the program as
arguments (exit 0, written into the agent), and refused header= on a URL,
so a token-bearing remote MCP needed a hand edit of library.json.

SplitServerWords takes header=K=V (or K: V), env=K=V, timeout=SECS,
agents= and --local out of the line; words after -- are the program's.
A setting-looking word magpie doesn't know (headers=, envs=) is refused;
header= on a command or env= on a URL is an error. New: mcp set <name>
header=|header-=|env=|env-=|timeout= keeps the rest of the server, and
mcp show <name> prints it as JSON. Header names match without case and
keep the stored spelling.

Run (wsl-run, -tags nogui): TestSplitServerWords, TestSetServerKeepsTheRest,
TestLibraryMCPHeadersEnvTimeout, all of internal/cli and internal/library
ok; GOOS=windows and darwin go vet of both packages; gofmt clean. The new
CLI test fails on the old code by construction (env/timeout in Args). Not
broken by hand separately. Not tried: win-run, the GUI (its save already
sends headers; a stale open editor over a hand edit isn't addressed), the
TUI line (still ServerOf), the other agents' encoders for timeout.
Not run: full suite.
2026-11-11 17:01:36 +08:00

81 lines
2.8 KiB
Go

package library
import (
"strings"
"testing"
)
// #6111 (Abandon332 on X): env=, header= and timeout= on a library mcp line
// were handed to the program as arguments. They are settings now, an
// unknown setting-looking word is refused, and -- hands words to the program.
func TestSplitServerWords(t *testing.T) {
cmd, o, err := SplitServerWords([]string{"uvx", "foo", "env=A=B=C", "timeout=30", "agents=alma", "--local"})
if err != nil || strings.Join(cmd, " ") != "uvx foo" || o.Env["A"] != "B=C" || *o.Timeout != 30 || !o.HasAgent || !*o.Local {
t.Fatalf("%v %+v %v", cmd, o, err)
}
_, o, err = SplitServerWords([]string{"https://x/mcp", "header=Authorization=Bearer abc", "header=Accept: application/json, text/event-stream"})
if err != nil || o.Headers["Authorization"] != "Bearer abc" || o.Headers["Accept"] != "application/json, text/event-stream" {
t.Fatalf("%+v %v", o, err)
}
cmd, o, err = SplitServerWords([]string{"uvx", "foo", "--", "env=A=B", "timeout=3"})
if err != nil || strings.Join(cmd, " ") != "uvx foo env=A=B timeout=3" || o.Env != nil || o.Timeout != nil {
t.Fatalf("after --: %v %+v %v", cmd, o, err)
}
for _, bad := range []string{"headers=A=b", "envs=A=B", "timeout=soon", "env=NOVALUE", "header=nocolon", "timeout=-1"} {
if _, _, err := SplitServerWords([]string{"uvx", bad}); err == nil {
t.Errorf("%s was taken", bad)
}
}
// a program's own key=value that isn't a setting of ours stays an argument
if cmd, _, err := SplitServerWords([]string{"srv", "mode=fast", "--port=3"}); err != nil || len(cmd) != 3 {
t.Fatalf("%v %v", cmd, err)
}
}
func TestSetServerKeepsTheRest(t *testing.T) {
sandbox(t)
s, err := ServerOf("gh", []string{"https://api.githubcopilot.com/mcp/"})
if err != nil {
t.Fatal(err)
}
s.Headers = map[string]string{"Authorization": "Bearer old"}
s.Note = "mine"
if _, err := SaveServer("", s); err != nil {
t.Fatal(err)
}
_, o, _ := SplitServerWords([]string{"header=authorization=Bearer new", "header=Accept=application/json, text/event-stream", "timeout=60"})
if _, err := SetServer("gh", o); err != nil {
t.Fatal(err)
}
got := readServer(t, "gh")
if got.Headers["Authorization"] != "Bearer new" || len(got.Headers) != 2 || got.StartupTimeout != 60 || got.Note != "mine" {
t.Fatalf("%+v", got)
}
_, o, _ = SplitServerWords([]string{"header-=accept"})
if _, err := SetServer("gh", o); err != nil {
t.Fatal(err)
}
if h := readServer(t, "gh").Headers; len(h) != 1 {
t.Fatalf("%v", h)
}
_, o, _ = SplitServerWords([]string{"env=A=B"})
if _, err := SetServer("gh", o); err == nil {
t.Fatal("env on a URL server")
}
}
func readServer(t *testing.T, name string) Server {
t.Helper()
v, err := Read(nil)
if err != nil {
t.Fatal(err)
}
for _, s := range v.Servers {
if s.Name == name {
return *s.Server
}
}
t.Fatalf("no %s", name)
return Server{}
}