gateway: GET /v1/magpie/mcp lists magpie's own MCP servers that are on (ferock on Discord)

ferock on Discord: the community chat page's "use all of magpie's MCP
servers" gave a chat only the servers in library.json, never magpie's own
web search, image recognition or image generation. Nothing told an app
which of magpie's own servers are on, so the page couldn't add them.

GET /v1/magpie/mcp (OwnMCPPath, own_mcp.go) answers
{"servers":[{"name","title","path"}]} for the ones on now, each named
as its initialize names it: magpie-web-search while plugins/search serves
and something searches (canSearch), magpie-vision while a model sees
(seer: not Off), magpie-image while a model draws (drawer: not Off),
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). It holds no
secret, so a read-only gateway key may read it (readOnlyRoutes); it is
otherwise guarded as every /v1/magpie/* route is. It is in / "apis".
page-chat 0.1.7 (magpie-community/plugins) reads it in every route the
page takes its MCP list from (readMcp: the list, a chat's tools, a tool
call, the connection check). Documented in gateway-routing.md.

Run (mote, at e2a65cbc9 plus this change, before the rebase onto
2987f5e2b, which adds three commits in gateway/gui/provider that this
doesn't touch):
- gofmt -l: clean. go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: ok.
- go test -tags nogui -timeout 45m ./... in a sandbox HOME: 81 packages
  ok; internal/gateway ok. Two reds, neither in code this touches, both
  filed: internal/gui TestSettingsSaveKeepsWhatItDoesNotSend (SearchAll,
  #4514) and internal/update TestRelaunchOpenTriedAgain (#3881).
  The first run, at d6e111638, also had TestReadOnlyKeyEveryRoute red on
  the new route; it is in readOnlyRoutes now.
- At the pushed head (on 2987f5e2b): go vet ./internal/gateway and
  -run 'OwnMCP|ReadOnlyKey|ClientPath|ServingGates' ok.
- TestOwnMCP* (5 tests) red on the old code by hand: the route taken
  out, each answers 404.
- Real binary (go build -tags nogui, sandbox HOME, MAGPIE_ADDR
  127.0.0.1:3591, a fake vendor listing gpt-image-1 and gpt-4o): the list
  was magpie-image; tools/list on its path answered generate_image,
  generate_video, generation_result; with vision and imageGen "off" in
  settings.json the list was empty; a request with a foreign Origin got
  403.

Not tried: image recognition and web search listed by the real binary
(the fake vendor's gpt-4o wasn't known to see offline, and no search API
was set up; the unit tests cover both); a real vendor; the GUI (no GUI
change); Windows and Linux runs (vet only).
This commit is contained in:
yetone
2026-10-31 20:37:19 +08:00
parent 2987f5e2b7
commit cac655c14f
5 changed files with 215 additions and 1 deletions
+22
View File
@@ -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,
+2 -1
View File
@@ -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) {
+59
View File
@@ -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()})
}
+131
View File
@@ -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)
}
}
+1
View File
@@ -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,
}