Files
magpie/internal/gateway/hub_describe_test.go
T
yetone 58a833f254 mcp router: initialize says what each server behind it is and does
packinggoodsllc_07668 (Discord) runs magpies that only share MCP servers,
and an agent given several routers knew each by its name alone. A
router's initialize instructions now describe what it holds, built from
what the servers themselves say; no model writes any of it.

- After the "Tools of …" first line (and the router's name sentence when
  it has one), a line per server: "- name (its serverInfo.title): what it
  is for. Tools: tool (first clause of its description), …". The tools
  the Library turned off are left out; another router's "[name] " prefix
  is dropped. A line too long for its share says tools by name, then as
  many as fit and "and N more". The whole is at most 2000 characters
  (hubSayMax), the closing text kept whole.
- What a server is for: the market's words as before, else its
  serverInfo.description, else its instructions' first line (saysOf).
  Another magpie's "Tools of a (…), b." is said with "Tools of" once.
- What a server said and listed is kept in settings.Dir()/
  mcp-hub-servers.json (not the synced library folder), keyed by name and
  a hash of hubKey, written via temp+rename only on change, pruned of
  servers gone. A router started again describes its servers without
  starting any; one added or edited since is listed by initialize, which
  waits up to 5s for it.
- Pi's hub description (hubAbouts) uses the server's own words too.

Agents, from their installed source (research, not run): Claude Code
shows instructions (4096 chars), Codex as the namespace description,
OpenCode, Gemini CLI (trusted folders only), Qwen Code, Pi (first line in
the prompt); Kimi CLI ignores them. None shows serverInfo.title.

Run at this tree (mote):
- go test -tags nogui ./internal/library: ok (116s)
- go test ./internal/gateway -run 'Hub|MCP|Roots|OversizedBody|
  ReadOnlyKeyEveryRoute|FileURI|ImageModel': ok
- GOOS=linux and GOOS=windows go vet internal/gateway internal/library;
  go vet internal/gui internal/cli; go build ./...; gofmt -l: clean
- New: TestHubInstructionsDescribeServers, TestHubDescriptionsOutlive
  ARestart (library); TestHubAboutBounded, TestHubServerLine,
  TestHubClauseBrackets, TestHubLineOfARouter (gateway).
- Red on the old code: TestHubInstructionsDescribeServers at c9af34446
  fails ("Tools of la, notes." and nothing more).
- Broken by hand, each red: noteCard not writing the file (restart test),
  the DisabledTools filter removed, saysOf returning nothing.
- Real use: two magpie serve builds of this tree in sandbox HOMEs on
  mote (127.0.0.1:3561, :3562), A serving a stdio notes server, B with A's
  /mcp/magpie/hub as server "la". B's initialize said
  "- la (Magpie MCP hub): Tools of notes (…). Tools: notes__add_note (Add
  a note to the notebook), notes__search_notes (…)."; B restarted with A
  down said the same from the file.

Not tried: a real agent reading the new text (Claude Code, Codex,
OpenCode etc. were read in source only); a router with dozens of real
servers; Windows and Linux runs (the change has no OS-specific code).
2026-11-02 18:37:57 +08:00

82 lines
3.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gateway
import (
"fmt"
"strings"
"testing"
)
// However many servers and tools there are, the instructions stay within
// hubSayMax with the tail whole, and every server magpie knows anything
// of is named on a line of its own.
func TestHubAboutBounded(t *testing.T) {
const tail = "The tools of every MCP server in magpie's Library, each named <server>__<tool>."
for _, n := range []int{1, 3, 12, 40} {
for _, tools := range []int{0, 2, 30, 200} {
ss := make([]HubServer, n)
for i := range ss {
ss[i] = HubServer{Name: fmt.Sprintf("srv%02d", i), About: strings.Repeat("Reads and writes things for you. ", 6), Known: true}
for j := range tools {
ss[i].Tools = append(ss[i].Tools, HubToolNote{Name: fmt.Sprintf("tool_%03d", j), About: strings.Repeat("word ", 60)})
}
}
say := hubAbout(ss, "This router: Los Angeles.", tail)
if l := runeLen(say); l > hubSayMax {
t.Errorf("%d servers × %d tools: %d characters", n, tools, l)
}
if !strings.HasSuffix(say, tail) {
t.Errorf("%d servers × %d tools: the tail is cut:\n%s", n, tools, say)
}
if n <= 12 {
for _, s := range ss {
if !strings.Contains(say, "\n- "+s.Name) {
t.Errorf("%d servers × %d tools: %s has no line:\n%s", n, tools, s.Name, say)
}
}
}
}
}
}
// A server's line names its tools with the first clause of each; a
// router's "[name] " before a description is left out, and a server
// magpie knows nothing of has no line.
func TestHubServerLine(t *testing.T) {
s := HubServer{Name: "la", Title: "Magpie MCP hub · Los Angeles", Tools: []HubToolNote{
{Name: "fetch__fetch", About: "[Los Angeles] Fetches a URL, and more."},
{Name: "x", About: ""},
}}
if got, want := hubServerLine(s, 1000), "- la (Magpie MCP hub · Los Angeles): Tools: fetch__fetch (Fetches a URL), x."; got != want {
t.Errorf("got %q, want %q", got, want)
}
if got := hubServerLine(HubServer{Name: "bare"}, 1000); got != "" {
t.Errorf("a server known by name only has a line: %q", got)
}
if got := hubServerLine(s, 30); runeLen(got) > 30 {
t.Errorf("%q is over 30", got)
}
}
// Another magpie's first line keeps each server's words in brackets, so
// its clause isn't cut inside them.
func TestHubClauseBrackets(t *testing.T) {
for in, want := range map[string]string{
"Tools of fetch (Fetch a page, as Markdown), time.": "Tools of fetch (Fetch a page, as Markdown)",
"Search [docs: all of them]. Then more.": "Search [docs: all of them]",
"Plain words, then more": "Plain words",
} {
if got := hubClause(in); got != want {
t.Errorf("hubClause(%q) = %q, want %q", in, got, want)
}
}
}
// Another magpie's router says "Tools of a (…), b." of itself; the first
// line names it with what follows, not "Tools of" twice.
func TestHubLineOfARouter(t *testing.T) {
got := HubLine([]HubServer{{Name: "la", About: "Tools of notes (Notes the team keeps), time."}})
if want := "Tools of la (notes (Notes the team keeps))."; got != want {
t.Errorf("got %q, want %q", got, want)
}
}