Files
magpie/internal/access/mcp_test.go
T
yetone d55881730d gateway: a key can be held to some MCP servers and tools
wakaka on Discord (#help, 11-06) asked for gateway keys limited to some
MCP servers and tools, as keys already are to models (#882).

A key's MCP list (caller-keys.json "mcp") holds entries "server" (every
tool of it) or "server/tool"; "-" alone is no MCP server; an empty list
is every server, as before. A key issued under another is held to its
issuer's list too.

Every MCP route checks it:
- magpie's own servers (web search, vision, image, Magpie Browser, plugin
  tools) through serveMCP: initialize/tools/list/tools/call are refused
  or filtered per server and tool, and /v1/magpie/key-limits names "mcp";
- the Library hub ("magpie"): only the backing servers and tools the key
  may use are listed, and a call by name or by fallback is refused;
- /mcp/<name> and /mcp/run/<name>: the server is refused, tools/list is
  filtered in JSON and SSE replies, tools/call refused; a reply the
  filter can't read (an encoded body) is a 502, never passed through.

GUI: "MCP servers" in the key row's menu opens the app's own picker (no
native select) with All, No MCP server, each server and its tools (GET
/api/caller-keys/mcp lists magpie's own servers and the Library's). A
key held to some shows a badge saying which, that opens the same list;
a free key shows nothing more in its row, on hover either. A hover badge
was tried first: it pushed the row past 560px (gateway-caller-keys) and
squeezed the masked key under a drag at 420px (gateway-key-arrange).
CLI: magpie gateway-key mcp <id> github fs/read_file | none | all.
zh, zh-TW, ja, de strings; docs/reference.md and the gateway-routing and
library subsystem pages.

Not covered: a model's own built-in web search (a vendor tool, not MCP).

GET /api/caller-keys/mcp is named in the CLI parity test's notYet (its
list is what `magpie gateway-key mcp` takes; TestEveryRouteHasACommandOrIsNamed
caught it at the rebased head).

Run on mote (mote-run), at the pushed head:
- go test ./internal/access ./internal/cli ./internal/gui
  ./internal/library ./internal/gateway at a9c40186d (this change on
  2ce44dc28): ok except internal/gui's TestMainWindowCorners/macOS_26_SDK
  (#3949, the same message on macOS 27). After the rebase onto a9667daad
  (no file in common): TestCoreScriptBudget, TestCallerKey*,
  TestEveryRoute*, TestGatewayKey*, TestKeyMCP*, internal/access and
  go vet ./internal/... at d36f89224's tree: ok.
- gateway-caller-keys chromium en reads wider than 560px 4 of 6 runs
  alone on this branch and 4 of 6 on origin/main a9667daad: #3797,
  recorded there.
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go
  vet ./...: clean; darwin, linux (nogui) and windows builds ok; gofmt -l
  clean.
- GUI: gateway-key-mcp.test.cjs in chromium and webkit, widths 1100 and
  420, en/zh/zh-TW/ja/de: 20/20. Every gateway-key*, gateway-keys* and
  i18n* file in both engines: ok except gateway-key-models en (red on
  origin/main ecc26a339 too, #4081/#5233) and i18n-keys "a plugin's words
  are its own" (red on main, #5332).
- The whole GUI suite (both engines, every language) with the hover
  badge: 91 reds in 25 files. Rerun alone on this branch and on
  origin/main 54dc257f1: only gateway-caller-keys (560px sideways),
  gateway-key-readonly, models-fold and models-select-all-api were red
  on the branch alone; the badge moved into the row menu for the first
  two. After it, every gateway-key*, gateway-caller-keys, gateway-card,
  gateway-connect-keys, api-key-theme and i18n* file passes in both
  engines, except i18n-keys "a plugin's words are its own" (red on main,
  #5332, none of the three strings are this change's) and 1 of 20
  models-select-all-api "the click scrolled" (#4389; models-fold is
  #4071). The rest are red on origin/main too and each has an issue:
  account-weight #4644, codex-auto-reset #2360, header-centre #3479,
  images-codex-one #4618, key-replace #4126, menu-input-scroll #3402,
  mobile-web #3952, model-chip-steady #4388, model-copy-id #4621,
  models-works-only #4391, nav-timeout #4787, omarchy #4624,
  page-width-steady #5013, phone-connect-menu #4706/#4707,
  plugin-move-unserved #4392.

Broken by hand, each went red: the serveMCP server gate, call check and
list filter, the own-server list filter and key-limits "mcp"
(TestKeyMCPOwnServer); hubTools, hubServersOf, the hubToolNamed
fallback and hubMCPRun (TestKeyMCPHub); /mcp/run (TestKeyMCPRun); the
/mcp/<name> guard and filter (TestKeyMCPProxy); the GUI's "No MCP
server" being exclusive (gateway-key-mcp).
gateway-key-readonly chromium 420 ja was red with the hover badge (the
sibling run, the whole suite and its rerun; green on origin/main) and
passes with the badge in the row menu.

Not tried: a real Library server through a live gateway, the hub with a
real agent, and running on Windows or Linux (vet and build only).

Reported-by: wakaka
2026-11-07 03:21:57 +08:00

133 lines
4.7 KiB
Go

package access
import (
"slices"
"strings"
"testing"
"time"
)
// A key's MCP list is servers whole and <server>/<tool> entries, kept as
// written; "-" alone holds it to none, and a bad entry is refused.
func TestCleanMCP(t *testing.T) {
got, err := CleanMCP([]string{" github ", "", "fs/read", "github/search_code", "fs/read", "magpie-web-search/web_fetch"})
if err != nil || !slices.Equal(got, []string{"github", "fs/read", "magpie-web-search/web_fetch"}) {
t.Fatal(got, err)
}
if got, err := CleanMCP([]string{NoMCP}); err != nil || !slices.Equal(got, []string{NoMCP}) {
t.Fatal("none:", got, err)
}
if got, err := CleanMCP([]string{NoMCP, "fs"}); err != nil || !slices.Equal(got, []string{"fs"}) {
t.Fatal("none beside a server:", got, err)
}
if got, err := CleanMCP(nil); err != nil || len(got) != 0 {
t.Fatal("all:", got, err)
}
for _, bad := range []string{"fs/", "/read", "-fs", "a b", "fs/ read", "fs/" + strings.Repeat("x", 129), strings.Repeat("s", 65)} {
if _, err := CleanMCP([]string{bad}); err == nil {
t.Error("accepted", bad)
}
}
many := make([]string, 501)
for i := range many {
many[i] = "fs/t" + strings.Repeat("x", i%7) + string(rune('a'+i%26)) + strings.Repeat("y", i/26)
}
if _, err := CleanMCP(many); err == nil {
t.Error("took 501 entries")
}
}
// What a key may use: everything with no list, a server listed whole
// with every tool, a listed tool alone of its server, nothing with "-";
// an issued key is held to its issuer's list too.
func TestKeyMCPAllows(t *testing.T) {
all := Identity{}
if all.MCPHeld() || !all.MCPAllows("fs", "read") {
t.Fatal("a key with no list is held")
}
if ok, every := all.MCPServer("fs"); !ok || !every {
t.Fatal("a key with no list: fs", ok, every)
}
who := Identity{MCP: []string{"github", "fs/read"}}
for _, c := range []struct {
server, tool string
want bool
}{
{"github", "create_issue", true},
{"fs", "read", true},
{"fs", "write", false},
{"magpie-web-search", "web_search", false},
{"fs/read", "", false},
} {
if got := who.MCPAllows(c.server, c.tool); got != c.want {
t.Errorf("%s %s: %v", c.server, c.tool, got)
}
}
if ok, every := who.MCPServer("fs"); !ok || every {
t.Error("fs is some tools:", ok, every)
}
if ok, every := who.MCPServer("github"); !ok || !every {
t.Error("github is whole:", ok, every)
}
if ok, _ := who.MCPServer("linear"); ok {
t.Error("linear isn't listed")
}
none := Identity{MCP: []string{NoMCP}}
if ok, _ := none.MCPServer("fs"); ok || none.MCPAllows("fs", "read") || !none.MCPHeld() {
t.Error("a key held to none")
}
// issued under a key held to github: its own wider list doesn't widen it
child := Identity{MCP: []string{"github", "fs"}, Issuer: &Identity{MCP: []string{"github/search_code"}}}
if !child.MCPHeld() || child.MCPAllows("fs", "read") || child.MCPAllows("github", "create_issue") || !child.MCPAllows("github", "search_code") {
t.Error("an issued key past its issuer")
}
if ok, every := child.MCPServer("github"); !ok || every {
t.Error("github under the issuer:", ok, every)
}
free := Identity{Issuer: &Identity{MCP: []string{"fs"}}}
if !free.MCPHeld() || free.MCPAllows("github", "x") || !free.MCPAllows("fs", "x") {
t.Error("an issued key with no list of its own")
}
}
// A key's MCP list is kept in caller-keys.json, reaches the gateway with
// the key and with every key issued under it, and is cleared by an empty
// list; a bad one is refused and leaves the list as it was.
func TestKeyMCPKept(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
clockAt(t, time.Date(2026, 11, 6, 9, 0, 0, 0, time.UTC))
id, secret := addKey(t, "Phone")
if who, ok := Authenticate(secret); !ok || who.MCPHeld() {
t.Fatal("a new key is held", who)
}
if _, err := Update("mcp-key", Change{Key: id, MCP: []string{"github", "fs/read"}}); err != nil {
t.Fatal(err)
}
if _, err := Update("mcp-key", Change{Key: id, MCP: []string{"fs/"}}); err == nil {
t.Fatal("took a bad entry")
}
who, ok := Authenticate(secret)
if !ok || !slices.Equal(who.MCP, []string{"github", "fs/read"}) || who.MCPAllows("fs", "write") {
t.Fatal(who, ok)
}
if keys, _ := List(); !slices.Equal(keys[0].MCP, []string{"github", "fs/read"}) {
t.Fatal("listed", keys[0].MCP)
}
if _, err := Update("issuer-key", Change{Key: id, Issues: true}); err != nil {
t.Fatal(err)
}
child, err := IssueKey(Issue{TTL: time.Hour, Issuer: id})
if err != nil {
t.Fatal(err)
}
if cw, ok := Authenticate(child.Key); !ok || !cw.MCPHeld() || cw.MCPAllows("fs", "write") || !cw.MCPAllows("github", "x") {
t.Fatal("issued:", cw, ok)
}
if _, err := Update("mcp-key", Change{Key: id}); err != nil {
t.Fatal(err)
}
if who, _ := Authenticate(secret); who.MCPHeld() {
t.Fatal("an empty list still holds", who.MCP)
}
}