diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md index b0d8473bd..c77381979 100644 --- a/docs/subsystems/gateway-routing.md +++ b/docs/subsystems/gateway-routing.md @@ -1367,6 +1367,28 @@ Covered by `TestImageMCPDrawsAndServesTheImage`, `TestImageMCPSaysWhenOff`, `TestImageMCPMakesMiniMaxVideos`, `TestImageMCPIsServed` and `image-mcp.test.cjs`. +## Which of magpie's own MCP servers are on + +`GET /v1/magpie/mcp` (`OwnMCPPath`, [`own_mcp.go`](../../internal/gateway/own_mcp.go)) +lists the MCP servers above that are on now, as +`{"servers":[{"name","title","path"}]}`, each named as its `initialize` +names it, with its path on this gateway. It is for an app that gives a +model the Library's servers and wants magpie's own beside them: the +community chat page's "use all of magpie's MCP servers" read only +library.json, so web search, image recognition and image generation never +reached its chats (ferock on Discord). A server is listed while an agent +mounting it would get working tools: `magpie-web-search` while +plugins/search serves and something searches (`canSearch`, so Off in +Settings › Models › Web search with no search API leaves it out), +`magpie-vision` while a model sees (`seer`, not Off), `magpie-image` while +a model draws (`drawer`, not Off), and `magpie-tools` while plugins/tools +serves and a plugin that is on gives a tool (`plugin.ToolsOn`; a note that +can't be read lists it, and its `tools/list` tells). The list holds no +secret, so a read-only gateway key may read it (`readOnlyRoutes`); the route is otherwise guarded as every `/v1/magpie/*` route is, and a caller +mounts each path with the same key it called with. `OwnMCPServers` is the +fixed list `magpie tool` walks; this one follows the switches. Covered by +`TestOwnMCPNoneWhenNothingIsOn` and `TestOwnMCPLists*WhileOn`. + ## Side models magpie picks: allowance first When magpie picks the model itself (`AutoVision` for image recognition, diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index f53d7705e..d7171d09a 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -817,6 +817,7 @@ func (s *Server) routes() *routeMux { mux.HandleFunc("GET /v1/magpie/trace", s.gatewayTrace) mux.HandleFunc("GET /v1/magpie/concurrency", s.concurrency) mux.HandleFunc("GET /v1/magpie/limit", s.keyLimit) + mux.HandleFunc("GET "+OwnMCPPath, s.ownMCPList) // short-lived keys, issued by a key let issue them or from loopback // (issue_keys.go) mux.HandleFunc("POST "+KeysPath, s.issueKey) @@ -918,7 +919,7 @@ var catalogSize stamped[struct{}, int] func (s *Server) info(w http.ResponseWriter, r *http.Request) { out := map[string]any{"name": "magpie", "version": Version, "models": catalogSize.get(struct{}{}, func() int { return len(provider.Catalog()) }), "window": Window, - "apis": []string{"/v1/chat/completions", "/v1/responses", "/v1/messages", "/v1/systemone", "/v1beta/models/{model}:generateContent", "/v1/images/generations", "/v1/images/edits", "/v1/videos", "/v1/embeddings", "/v1/rerank", "/v1/audio/transcriptions", "/v1/audio/translations", "/v1/audio/speech", "/v1/magpie/quotas", "/v1/magpie/quotas/history", "/v1/magpie/usage", "/v1/magpie/usage/requests", "/v1/magpie/route", "/v1/magpie/trace", SearchMCPPath, VisionMCPPath, ImageMCPPath, ToolsMCPPath, HubMCPAll, HubMCPPath + "{agent}"}} + "apis": []string{"/v1/chat/completions", "/v1/responses", "/v1/messages", "/v1/systemone", "/v1beta/models/{model}:generateContent", "/v1/images/generations", "/v1/images/edits", "/v1/videos", "/v1/embeddings", "/v1/rerank", "/v1/audio/transcriptions", "/v1/audio/translations", "/v1/audio/speech", "/v1/magpie/quotas", "/v1/magpie/quotas/history", "/v1/magpie/usage", "/v1/magpie/usage/requests", "/v1/magpie/route", "/v1/magpie/trace", OwnMCPPath, SearchMCPPath, VisionMCPPath, ImageMCPPath, ToolsMCPPath, HubMCPAll, HubMCPPath + "{agent}"}} // which process serves, for another magpie on this computer to name it // on its Routing page (#2606); not told to another machine if local(r) { diff --git a/internal/gateway/own_mcp.go b/internal/gateway/own_mcp.go new file mode 100644 index 000000000..a4c27dfd0 --- /dev/null +++ b/internal/gateway/own_mcp.go @@ -0,0 +1,59 @@ +package gateway + +import ( + "net/http" + + "github.com/yetone/magpie/internal/gwtool" + "github.com/yetone/magpie/internal/plugin" +) + +// OwnMCPPath lists the MCP servers magpie serves itself (web search, +// image recognition, image generation, the plugins' tools) that are on +// now, for an app that gives a model the Library's servers and wants +// magpie's own beside them: the community chat page (ferock on Discord), +// which read only library.json. A server is listed while an agent that +// mounts it would get its tools to work: the search while plugins/search +// serves and something searches (canSearch), image recognition while a +// model sees (seer: not Off in Settings), image generation while a model +// draws (drawer: not Off), the plugin tools while plugins/tools serves +// and a plugin that is on gives a tool. Each is named as its own +// initialize names it, with its path on this gateway; nothing in it is a +// secret. +const OwnMCPPath = "/v1/magpie/mcp" + +// OwnMCP is one of magpie's own MCP servers, as OwnMCPPath lists it. +type OwnMCP struct { + Name string `json:"name"` + Title string `json:"title"` + Path string `json:"path"` // on this gateway, Streamable HTTP +} + +// toolsOn is how many plugins that are on give a tool, from the note the +// host keeps (never starting it); a variable for tests. +var toolsOn = plugin.ToolsOn + +// ownMCP are magpie's own MCP servers that are on now. +func ownMCP() []OwnMCP { + out := []OwnMCP{} + if canSearch() { + out = append(out, OwnMCP{"magpie-web-search", "Magpie Web Search", SearchMCPPath}) + } + if _, ok := seer(); ok { + out = append(out, OwnMCP{"magpie-vision", "Magpie Image Recognition", VisionMCPPath}) + } + if _, ok := drawer(); ok { + out = append(out, OwnMCP{imageServerName, "Magpie Image", ImageMCPPath}) + } + if gwtool.Served(gwtool.PluginTools) { + // a note that can't be read says nothing: the server is listed, + // and its tools/list tells + if n, err := toolsOn(); err != nil || n > 0 { + out = append(out, OwnMCP{"magpie-tools", "Magpie Plugin Tools", ToolsMCPPath}) + } + } + return out +} + +func (s *Server) ownMCPList(w http.ResponseWriter, r *http.Request) { + writeJSON(w, 200, map[string]any{"servers": ownMCP()}) +} diff --git a/internal/gateway/own_mcp_test.go b/internal/gateway/own_mcp_test.go new file mode 100644 index 000000000..c432f4b9b --- /dev/null +++ b/internal/gateway/own_mcp_test.go @@ -0,0 +1,131 @@ +package gateway + +import ( + "encoding/json" + "net/http/httptest" + "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) + } +} diff --git a/internal/gateway/readonly.go b/internal/gateway/readonly.go index b2078053c..655f906b9 100644 --- a/internal/gateway/readonly.go +++ b/internal/gateway/readonly.go @@ -34,6 +34,7 @@ var readOnlyRoutes = map[string]bool{ "GET /v1/magpie/usage/requests": true, "GET /v1/magpie/limit": true, "GET /v1/magpie/concurrency": true, + "GET " + OwnMCPPath: true, "GET " + KeyPagePath: true, "GET " + keyPageData: true, }