Files
magpie/internal/imagemcp/hub_test.go
T
yetone b78b1de003 library: magpie-image is behind the MCP hub, saving into the calling agent's project
magpie-image was the one MCP server the hub left out, because it saves
what it makes on disk and needs to know whose project that is. The hub
now serves its tools like any other server's and tells it where the
caller works:

1. the hub answers a tools/call as SSE and asks the agent roots/list on
   that stream (3s), as Claude Code answers;
2. else Codex's _meta x-codex-turn-metadata.workspaces (git repos only);
3. else the Codex thread's cwd from its session file
   (sessions.CodexThreadCwd), only with session consent;
4. else none, and the image goes to ~/Pictures/Magpie as before.

The hub passes `magpie/roots` and `magpie/client` in the call's _meta;
imagemcp saves into the first root and names the agent on its usage
row. Its result carries `magpie/model`, which the hub's own usage row
takes, so the row says which image model drew it. A WSL agent keeps
magpie-image as its own entry (the hub can't reach a path inside the
distro). docs/subsystems/mcp-hub.md (mcp-router.md after the next commit), gateway-routing.md and
gateway-sessions.md say so.

It is rebased on #4382 (b889ffe81), which lets the user keep a server
out of the hub (Server.Hub = direct). inHub now asks both: a server
kept direct is out, and magpie-image is in unless the agent is in WSL,
so the user can keep magpie-image direct too; the editor's switch
shows for it like any server, and the hub's line counts it with the
others (mcp-hub-direct.test.cjs follows).

Run at e2583df31 (this stack on 86fef947e), on mote: gofmt -l clean;
go vet for darwin, linux (-tags nogui) and windows; go build ./...;
go test -tags nogui ./... -timeout 45m, 81 packages ok, 0 FAIL;
library, gateway, imagemcp, sessions, cli and gui again alone, ok;
site's node --test, 49 of 49. GUI on xiaomi-gpu (Windows; mote's
browsers crashed under other sessions' suites): library-*, image*,
mcp-hub-direct, i18n-keys, list-sort and plugin-tools, both engines and
every language, 1055 of 1056: i18n-keys found "{name} is given to each
agent directly" dropped from the library's strings in the rebase. Put
back in the first commit; at the pushed head i18n-keys, mcp-hub-direct
and library-mcp-hub are 48 of 48. That is the only difference from
e2583df31.
Broken by hand at e2583df31: imagemcp ignoring magpie/roots fails
TestHubCallSavesInTheCallersProject; the router asking anything but
roots/list fails TestHubAsksTheAgentItsRoots and TestHubRootsUnasked;
inHub leaving magpie-image out fails TestHubGivesMagpieImage.
Real use, sandbox HOME and gateway, at 62421e570 (before the rebase): Claude Code saved into its project
via roots/list; Codex in a git repo via its workspaces _meta; Codex
outside a repo with session consent via the thread's cwd.
At 62421e570 the whole GUI suite ran on WSL magpie-desktop at load 116 to 135 from other
runs: 504 of 15757 red in 118 files, WebKit timeouts, none in a file
this change touches; filed as #4471 with the list.

Rebased once more onto 2570efb5f, which changed library-mcp-hub,
list-sort, mcp-hub-direct and gateway-routing.md. Run at the pushed
head: go build; gateway, library, cli and imagemcp tests ok; the GUI
budget tests ok; on xiaomi-gpu i18n-keys, mcp-hub-direct,
library-mcp-hub and list-sort, 51 of 54. The three reds were WebKit
420px library-mcp-hub cases with no Connect sheet after a reload.
origin/main d77da5bff goes red the same way (de 420px, 9 of 11). In a
second run of library-mcp-hub on WebKit at this head, 11 of 11 passed.
Filed as #4483.
Not tried: Codex outside a git repo without consent (it saves to
~/Pictures/Magpie, #4435); agents on Windows and in WSL with real use.
2026-10-31 02:49:08 +08:00

71 lines
3.0 KiB
Go

package imagemcp
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/gateway"
)
// Behind magpie's MCP hub the server is started once for every agent, in
// the home folder: a call the hub makes carries its agent's roots and
// name in _meta, and the images go into that agent's project, asked for
// under its name. With no roots (an agent that says none) they go into
// ~/Pictures/Magpie, never the folder the server was started in. The hub
// keeps the call's row in the request log, so the server reports none.
func TestHubCallSavesInTheCallersProject(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
g := &reportGW{}
mine := t.TempDir() // what this server's own client says is its root
c := start(t, g, mine)
c.call("initialize", map[string]any{"protocolVersion": "2025-06-18", "capabilities": map[string]any{"roots": map[string]any{}}, "clientInfo": map[string]any{"name": "magpie", "version": "1"}})
project := t.TempDir()
r := c.call("tools/call", map[string]any{"name": "generate_image", "arguments": map[string]any{"prompt": "a magpie"},
"_meta": map[string]any{gateway.HubRootsMeta: []string{"https://example.com/x", fileURI(project)}, gateway.HubClientMeta: "claude-code/2.1.314"}})
if r["isError"] == true || !strings.Contains(text(r), filepath.Join(project, Folder)) {
t.Fatalf("%v", r)
}
if files, _ := filepath.Glob(filepath.Join(project, Folder, "a-magpie-*.png")); len(files) != 1 {
t.Fatalf("saved %v", files)
}
if got := g.ua[len(g.ua)-1]; got != "claude-code/2.1.314 magpie-image/1" {
t.Errorf("asked as %q", got)
}
// the hub's row names the model, from the result's _meta
if m, _ := r["_meta"].(map[string]any); m == nil || !strings.Contains(fmt.Sprint(m[gateway.HubModelMeta]), "/") {
t.Errorf("no model in the result's _meta: %v", r)
}
r = c.call("tools/call", map[string]any{"name": "generate_image", "arguments": map[string]any{"prompt": "a magpie"},
"_meta": map[string]any{gateway.HubRootsMeta: []string{}}})
pictures := filepath.Join(home, "Pictures", "Magpie")
if r["isError"] == true || !strings.Contains(text(r), pictures) {
t.Fatalf("no roots: %v", r)
}
if files, _ := filepath.Glob(filepath.Join(mine, Folder, "*")); len(files) != 0 {
t.Errorf("saved into the server's own root: %v", files)
}
if _, err := os.Stat(pictures); err != nil {
t.Errorf("nothing in %s: %v", pictures, err)
}
// a call of its own client's is reported; the hub's two were not
if r = c.call("tools/call", map[string]any{"name": "generation_result", "arguments": map[string]any{"id": "img_nothing"}}); r["_meta"] != nil {
t.Errorf("its own client's call has the hub's _meta: %v", r)
}
var got []string
for end := time.Now().Add(10 * time.Second); time.Now().Before(end) && !strings.Contains(strings.Join(got, " "), "generation_result"); time.Sleep(5 * time.Millisecond) {
got = g.reported(1)
}
if len(got) != 1 || !strings.Contains(got[0], `"generation_result"`) {
t.Fatalf("reported %v", got)
}
}