Files
magpie/internal/gateway/own_mcp_test.go
T
yetone 788057340b library: Magpie Image Recognition in Discover beside Magpie Image and Web Search (matrix0487 on Discord, #4653)
matrix0487 asked on Discord (#feedback) for all three of magpie's own MCP
servers, image recognition, image generation and search, in the Library's
Discover. Image generation and search were there; image recognition was
an address to copy in Settings only (#4653).

- `magpie mcp vision` (browsermcp.RunVision) relays stdio to the
  gateway's /mcp/magpie/vision, as `mcp search` does. analyze_image's
  relative image path is made one in the agent's folder on the way
  (imageFrom), since the gateway can't know where the agent runs; an
  absolute path, ~, a URL or a drive goes as written.
- library: magpie-vision is a featured, opt-in server (no agents until
  picked), any binary's `mcp vision` is one server (serverKey), and it
  gets the 120s tool timeout (selfTimeout).
- Settings › Models › Images: the Image recognition row has Give agents
  Magpie Image Recognition… / Turn on the Library, none with recognition
  off or in gateway mode. The three links are one helper, libWay, used by
  Image generation and Web search too (no budget raised: core 1,533,011
  of 1,533,105).
- Never a dead tool: Discover lists all three whatever is set up, and one
  that can't answer yet says why (gateway.OwnUnready, set by
  /api/library/market/servers as `unready`): a "Not set up yet" badge on
  its card with the reason as its title, and the reason in its sheet
  (nothing set up, or turned off in Settings / Plugins). It can still be
  added; its tools already answer with the same reason until then.
- Strings in en, zh, zh-TW, ja, de; docs/subsystems/library.md.

Run, all on mote (macOS 27) through mote-run, at this head's tree
(rebased on 21882a68d):
- gofmt -l internal/: clean. go vet darwin; GOOS=linux -tags nogui;
  GOOS=windows: browsermcp, cli, library, gateway, gui, imagemcp ok.
- go test -timeout 45m: internal/library, browsermcp, cli, imagemcp ok;
  internal/gateway ok (whole package, 556s, before the rebase; OwnMCP,
  OwnUnready, VisionMCP, SearchMCP after it). internal/gui: three reds,
  none from this change, each red on origin/main 21882a68d too:
  TestSettingsSaveKeepsWhatItDoesNotSend (BusyLoad, from 8d8df0639,
  filed #4739), TestMainWindowCorners/macOS_26_SDK (#3949),
  TestColdTurnsTaken (#4510). Budget tests ok.
- New: TestMagpieImageRecognitionIsOptIn, TestVisionBridgePassesLines,
  TestImageFromLeavesTheRest, TestOwnUnreadyWhileNotListed.
- New GUI test vision-mcp-library.test.cjs: chromium + webkit, en, zh,
  zh-TW, ja, de, 1100 and 420px (link opens the sheet with its hint and
  unready line; Library off → Turn on the Library; one added opens
  Discover with all three, only the unready one badged, inside its card;
  gateway mode and recognition off show no link; no horizontal overflow,
  no page errors): 206/206 with search-mcp-library and
  images-library-way. On e84870f17 (old code), chromium: every link and
  Discover case red.
- i18n-keys, gui-de, gui-zh-tw ok; gui-ja's one red is #4580, the same
  on origin/main 21882a68d.
- Broken by hand: serverKey without "vision" → TestMagpieImageRecognitionIsOptIn
  red; imageFrom not joining the folder → both browsermcp tests red.
- Real use, sandbox HOME on mote, nogui build, `magpie serve` on
  127.0.0.1:3591: `magpie mcp vision` initialize / tools/list answered;
  analyze_image of shots/a.png says "magpie has no model that sees
  images…"; missing/b.png names /…/work/missing/b.png (relative path
  resolved); an extra argument exits 1; gateway down answers id 9 with
  "magpie isn't running, so Magpie Image Recognition can't be reached".

Not tried: analyze_image against a real seeing model through the relay
(the sandbox had no provider; the gateway path is the one /mcp/magpie/vision
already serves); a real agent adding it from Discover; WSL (unchanged,
the MCP router serves it there); the whole GUI suite, queued after
mote's running one.

Reported-by: matrix0487 (Discord)
Closes #4653
2026-11-01 14:12:42 +08:00

173 lines
5.1 KiB
Go

package gateway
import (
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/gwtool"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// ownMCPNames asks the gateway for magpie's own MCP servers, as the chat
// page plugin does, and gives each one's name and path.
func ownMCPNames(t *testing.T, s *Server) map[string]string {
t.Helper()
r := httptest.NewRequest("GET", OwnMCPPath, nil)
r.RemoteAddr = "127.0.0.1:50000"
w := httptest.NewRecorder()
lanGuard(s.Handler()).ServeHTTP(w, r)
if w.Code != 200 {
t.Fatalf("%s: %d %s", OwnMCPPath, w.Code, w.Body)
}
var got struct {
Servers []OwnMCP `json:"servers"`
}
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil || got.Servers == nil {
t.Fatalf("%s: %v %s", OwnMCPPath, err, w.Body)
}
out := map[string]string{}
for _, m := range got.Servers {
out[m.Name] = m.Path
}
return out
}
func toolsAre(t *testing.T, n int) {
t.Helper()
was := toolsOn
toolsOn = func() (int, error) { return n, nil }
t.Cleanup(func() { toolsOn = was })
}
// With nothing set up, none of magpie's own servers is listed, and the
// list is empty, not null.
func TestOwnMCPNoneWhenNothingIsOn(t *testing.T) {
fresh(t)
toolsAre(t, 0)
if got := ownMCPNames(t, New()); len(got) != 0 {
t.Fatalf("listed %v", got)
}
}
// Image recognition is listed while a model sees, and not with it Off in
// Settings (ferock on Discord: the chat page gave a model the Library's
// servers only).
func TestOwnMCPListsVisionWhileOn(t *testing.T) {
s, _ := eyed(t)
toolsAre(t, 0)
if got := ownMCPNames(t, s); got["magpie-vision"] != VisionMCPPath {
t.Fatalf("vision on: %v", got)
}
noVision(t)
if got := ownMCPNames(t, s); got["magpie-vision"] != "" {
t.Fatalf("vision off: %v", got)
}
}
// Image generation is listed while a model draws, and not with it Off.
func TestOwnMCPListsImageWhileOn(t *testing.T) {
s, _ := easeled(t)
toolsAre(t, 0)
if got := ownMCPNames(t, s); got[imageServerName] != ImageMCPPath {
t.Fatalf("image on: %v", got)
}
st := settings.Load()
st.ImageGen = "off"
if err := settings.Save(st); err != nil {
t.Fatal(err)
}
if got := ownMCPNames(t, s); got[imageServerName] != "" {
t.Fatalf("image off: %v", got)
}
}
// Web search is listed while something searches and plugins/search
// serves, and not with the plugin off.
func TestOwnMCPListsSearchWhileOn(t *testing.T) {
fresh(t)
toolsAre(t, 0)
apis := newSearchAPIs(t)
if err := provider.SetSearchAPI(apis.api("tavily", "tvly-k")); err != nil {
t.Fatal(err)
}
s := New()
if got := ownMCPNames(t, s); got["magpie-web-search"] != SearchMCPPath {
t.Fatalf("search on: %v", got)
}
stop := gwtool.Gate() // plugins/search off: nothing serves web_search
defer stop()
searchOn() // TestMain's
defer func() { searchOn = gwtool.Serve(gwtool.WebSearch) }()
if got := ownMCPNames(t, s); got["magpie-web-search"] != "" {
t.Fatalf("plugins/search off: %v", got)
}
}
// The plugin tools server is listed while plugins/tools serves and a
// plugin that is on gives a tool.
func TestOwnMCPListsPluginToolsWhileOn(t *testing.T) {
fresh(t)
s := New()
toolsAre(t, 2)
if got := ownMCPNames(t, s); got["magpie-tools"] != ToolsMCPPath {
t.Fatalf("tools on: %v", got)
}
toolsAre(t, 0)
if got := ownMCPNames(t, s); got["magpie-tools"] != "" {
t.Fatalf("no plugin gives a tool: %v", got)
}
toolsAre(t, 2)
stop := gwtool.Gate() // plugins/tools off
defer stop()
if got := ownMCPNames(t, s); got["magpie-tools"] != "" {
t.Fatalf("plugins/tools off: %v", got)
}
defer gwtool.Serve(gwtool.PluginTools)()
if got := ownMCPNames(t, s); got["magpie-tools"] != ToolsMCPPath {
t.Fatalf("plugins/tools on: %v", got)
}
}
// Each of magpie's own servers the Library's Discover lists says why it
// can't answer yet exactly while ownMCP leaves it out, and tells nothing
// set up from turned off in Settings (matrix0487 on Discord: all three in
// Discover, never a dead tool).
func TestOwnUnreadyWhileNotListed(t *testing.T) {
fresh(t)
toolsAre(t, 0)
for _, n := range []string{"magpie-web-search", "magpie-vision", imageServerName} {
if why := OwnUnready(n); !strings.Contains(why, "yet") {
t.Errorf("%s with nothing set up: %q", n, why)
}
}
if why := OwnUnready("magpie-browser"); why != "" {
t.Errorf("magpie-browser: %q", why)
}
s, _ := eyed(t)
if got := ownMCPNames(t, s); got["magpie-vision"] == "" || OwnUnready("magpie-vision") != "" {
t.Fatalf("a model sees: listed %v, unready %q", got, OwnUnready("magpie-vision"))
}
noVision(t)
if why := OwnUnready("magpie-vision"); !strings.Contains(why, "off in Settings") {
t.Fatalf("vision off: %q", why)
}
st := settings.Load()
st.ImageGen = "off"
if err := settings.Save(st); err != nil {
t.Fatal(err)
}
if why := OwnUnready(imageServerName); !strings.Contains(why, "off in Settings") {
t.Fatalf("image generation off: %q", why)
}
stop := gwtool.Gate() // plugins/search off
defer stop()
searchOn()
defer func() { searchOn = gwtool.Serve(gwtool.WebSearch) }()
if why := OwnUnready("magpie-web-search"); !strings.Contains(why, "Plugins") {
t.Fatalf("plugins/search off: %q", why)
}
}