With another magpie as a provider (preset remote-magpie), its router's
MCP servers took adding that same magpie again under Library -> MCP.
Now the gateway's info (GET /v1) says "mcp_hub": true to a caller it
serves MCP to while its router is on with a server that isn't itself
another magpie's router. A fetch of a Remote magpie's models asks that
info with the provider's key, only while the library would still give
it a server, and the library then adds one: named for the provider,
http at <root>/mcp/magpie/hub with Authorization: Bearer <key>, kept to
this computer (local), on for the agents given the hub's entry.
- Offered once (bridgesOffered in library.json): removed or switched
off by the user, it stays so until the provider is removed and added
again.
- A server the user already has at that magpie's router (same origin,
the router's path) is the one; none is added beside it.
- The provider removed takes its server out (adoptBridges, at Sync and
the View read); providers.json unreadable takes nothing out; the
provider's address or key changed changes the server's; edited to
another address or key it becomes the user's own (keptBridge).
- No loop: every dial to a URL under /mcp/magpie/hub carries
X-Magpie-Router, and a request with it is given no such server,
neither listed (library Tools, gateway hubServersOf) nor run (Call,
hubToolNamed). A server carried in by sync is never a bridge.
- No GUI change: it is a server row as any other.
Run (mote, at base 1c45645b5):
- gofmt -l clean; go vet ./internal/{gateway,library,provider} on
darwin, GOOS=windows, and GOOS=linux -tags nogui: ok.
- go test ./internal/gateway -run 'TestHubLeavesRemoteRouterToARouter|
TestInfoSaysMCPHub|TestHubMCP'; ./internal/provider -run
'TestRemoteHubNoted|TestRemoteSelf'; ./internal/library -run
'TestRemoteMagpieRouter|TestCarriedServerIsNoBridge|TestPluginTools|
TestHub': ok.
- Real two-magpie run on mote: a built binary, two sandbox HOMEs /
XDG_CONFIG_HOMEs, B on 3512 with a python stub provider and a stub
HTTP MCP server behind its router, A on 3513 with B as a Remote
magpie. After `magpie sync` on A, A's library.json had home-magpie
(http://127.0.0.1:3512/mcp/magpie/hub, Bearer key, bridge home) and
bridgesOffered [home]; A's /mcp/magpie/hub listed
home-magpie__stubmcp__echo and tools/call returned the stub's echo;
A asked with X-Magpie-Router listed nothing (loop guard); B asked
with it still listed its own stubmcp__echo.
Broken by hand, each red: the Tools filter, the Call filter, the router
header in remoteHeaders, the same-origin dedup, offered-once, the
unreadable-providers keep, the key refresh, keptBridge, putServers'
carried reset, hubServerOf's Bridge, the gateway's hubServersOf, the
gateway's header -> ctx, info's non-bridge check, fetch's hook, the
older-magpie check and the this-magpie check.
Not tried / not handled:
- The real run was over loopback; a LAN remote's key check is covered
only by the existing TestHubMCPRemoteEntryNeedsKey path.
- The provider's own extra headers (a proxy's) aren't carried to the
server; a remote with several keys is asked with p.Key.
- A remote whose router is turned off later keeps its server (it lists
no tools).
- A server the user added by hand at another magpie's router now sends
X-Magpie-Router too, so that router leaves out its own Remote
magpies' servers for it.
- Windows and Linux test runs (vet only).
Reported-by: treeman_26897
121 lines
3.9 KiB
Go
121 lines
3.9 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"slices"
|
|
"testing"
|
|
)
|
|
|
|
// bridgeHub is fakeHub whose Tools, as the library's, leaves out a
|
|
// Remote magpie's router for a request of another magpie's router, and
|
|
// says which requests were.
|
|
type bridgeHub struct {
|
|
*fakeHub
|
|
fromRouter []bool
|
|
}
|
|
|
|
func (b *bridgeHub) Tools(ctx context.Context, agent string) []HubTool {
|
|
b.mu.Lock()
|
|
b.fromRouter = append(b.fromRouter, FromRouter(ctx))
|
|
b.mu.Unlock()
|
|
ts := b.fakeHub.Tools(ctx, agent)
|
|
if FromRouter(ctx) {
|
|
ts = slices.DeleteFunc(slices.Clone(ts), func(t HubTool) bool { return t.Server == "home" })
|
|
}
|
|
return ts
|
|
}
|
|
|
|
// A request of another magpie's router (RouterHeader) is one to the
|
|
// library (FromRouter), and isn't given a server that is a Remote
|
|
// magpie's router: neither listed, nor run when named. Two magpies that
|
|
// have each other as Remote magpies don't ask each other round. Without
|
|
// the header, it is given as any server.
|
|
func TestHubLeavesRemoteRouterToARouter(t *testing.T) {
|
|
fresh(t)
|
|
f := &bridgeHub{fakeHub: &fakeHub{on: true,
|
|
tools: map[string][]HubTool{"": {
|
|
{Server: "fs", Tool: "read", Spec: hubSpec("read")},
|
|
{Server: "home", Tool: "search", Spec: hubSpec("search")},
|
|
}},
|
|
servers: map[string][]HubServer{"": {{Name: "fs", Known: true}, {Name: "home", Known: true, Bridge: true}}},
|
|
}}
|
|
old := MCPHub
|
|
MCPHub = f
|
|
t.Cleanup(func() { MCPHub = old })
|
|
h := lanGuard(New().Handler())
|
|
list := `{"jsonrpc":"2.0","id":1,"method":"tools/list"}`
|
|
names := func(m mcpReply) []string {
|
|
var out []string
|
|
for _, x := range m.Result.Tools {
|
|
out = append(out, x.Name)
|
|
}
|
|
slices.Sort(out)
|
|
return out
|
|
}
|
|
|
|
if got := names(readMCP(t, hubPostAt(h, HubMCPAll, here, list))); !slices.Equal(got, []string{"fs__read", "home__search"}) {
|
|
t.Fatalf("an agent's tools %v, want fs's and home's", got)
|
|
}
|
|
if got := names(readMCP(t, hubPostAt(h, HubMCPAll, here, list, RouterHeader, "1"))); !slices.Equal(got, []string{"fs__read"}) {
|
|
t.Fatalf("a router's tools %v, want fs's alone", got)
|
|
}
|
|
if !slices.Equal(f.fromRouter, []bool{false, true}) {
|
|
t.Errorf("the library was told %v of the two requests, want false then true", f.fromRouter)
|
|
}
|
|
// named, home's tool isn't run for a router (hubToolNamed asks
|
|
// Servers), and is for an agent
|
|
call := `{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"home__search","arguments":{"q":"x"}}}`
|
|
if m := readMCP(t, hubPostAt(h, HubMCPAll, here, call, RouterHeader, "1")); m.Error == nil {
|
|
t.Fatalf("home's tool ran for a router: %+v", m.Result)
|
|
}
|
|
if ran := f.ran(); len(ran) != 0 {
|
|
t.Fatalf("ran %v for a router", ran)
|
|
}
|
|
if m := readMCP(t, hubPostAt(h, HubMCPAll, here, call)); m.Error != nil || m.Result.IsError {
|
|
t.Fatalf("home's tool for an agent: %+v", m)
|
|
}
|
|
}
|
|
|
|
// The gateway's info says its router has a server to give ("mcp_hub")
|
|
// while the hub is on with a server that isn't a Remote magpie's router,
|
|
// for another magpie that has this one as a provider to give it a server.
|
|
func TestInfoSaysMCPHub(t *testing.T) {
|
|
fresh(t)
|
|
f := &fakeHub{on: true, servers: map[string][]HubServer{"": {{Name: "home", Bridge: true}}}}
|
|
withHub(t, f)
|
|
h := lanGuard(New().Handler())
|
|
says := func() bool {
|
|
t.Helper()
|
|
r := httptest.NewRequest(http.MethodGet, "/v1", nil)
|
|
r.RemoteAddr, r.Host = here, "127.0.0.1:3425"
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, r)
|
|
var info struct {
|
|
Name string `json:"name"`
|
|
MCPHub bool `json:"mcp_hub"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &info); err != nil || info.Name != "magpie" {
|
|
t.Fatalf("info %d: %s", w.Code, w.Body.String())
|
|
}
|
|
return info.MCPHub
|
|
}
|
|
if says() {
|
|
t.Error("mcp_hub with only a Remote magpie's router behind it")
|
|
}
|
|
f.mu.Lock()
|
|
f.servers[""] = append(f.servers[""], HubServer{Name: "fs"})
|
|
f.mu.Unlock()
|
|
if !says() {
|
|
t.Error("no mcp_hub with fs behind the hub")
|
|
}
|
|
f.mu.Lock()
|
|
f.on = false
|
|
f.mu.Unlock()
|
|
if says() {
|
|
t.Error("mcp_hub with the hub off")
|
|
}
|
|
}
|