Files
magpie/internal/library/mcp_local.go
T
yetone cd9c52c16e library: a Remote magpie's MCP router is a Library server of its own
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
2026-11-06 14:33:18 +08:00

96 lines
2.9 KiB
Go

package library
import (
"fmt"
"slices"
)
// A server can be kept to this computer (Server.Local; huhuy72219405 on
// X: "有些mcp属于本地的,同步之后其他设备不应该读取到"). It is for a server
// that runs a program, or reads a folder, only this computer has.
//
// - Collect leaves it out, so a backup or a sync carries it to no other
// computer. Turning it on takes the server out of what is carried, so
// the other computers let go of theirs at their next sync, as of any
// server taken out; the flag itself is never carried.
// - Put keeps it, whatever comes in: a bundle without it doesn't take it
// away, and another computer's server by its name doesn't take its
// place. That one is kept in Away, given to no agent here, and carried
// on as it came, so a sync from here doesn't take it from the computers
// that have it.
// - Once no server here by that name is kept to this computer (it was
// removed, renamed or shared again), settleAway lets go of Away's: the
// server here shared again goes in its place, and with none here the
// other computers' comes in, as a sync would bring it.
// carriedServers are the servers a carried library has: the library's,
// but those kept to this computer, and the other computers' by their
// names.
func (l *Library) carriedServers() []*Server {
out := []*Server{}
for _, s := range l.MCP {
if !s.Local {
out = append(out, s)
}
}
return append(out, l.Away...)
}
// putServers makes the library's servers those a carried library has,
// but for the ones kept to this computer, which stay as they are.
func (l *Library) putServers(in []*Server) {
own := map[string]bool{}
servers := []*Server{}
for _, s := range l.MCP {
if s.Local {
own[s.Name] = true
servers = append(servers, s)
}
}
l.Away = nil
for _, s := range in {
c := *s
c.Local = false // only this computer says what is its own
c.Bridge = "" // and which of its providers a server is
c.Agents = slices.Sorted(slices.Values(orNone(s.Agents)))
if own[c.Name] {
l.Away = append(l.Away, &c)
} else {
servers = append(servers, &c)
}
}
l.MCP = servers
}
// settleAway lets go of each server in Away whose name no server kept to
// this computer has any more. One shared here by that name is the one
// carried from now on; with none, the other computers' comes in.
func (l *Library) settleAway() {
away := l.Away[:0:0]
for _, a := range l.Away {
switch s := l.server(a.Name); {
case s != nil && s.Local:
away = append(away, a)
case s == nil:
l.MCP = append(l.MCP, a)
}
}
l.Away = away
if len(l.Away) == 0 {
l.Away = nil
}
}
// ServerLocal keeps a server to this computer, or lets a sync carry it
// again.
func ServerLocal(name string, on bool) (*Result, error) {
return change(func(l *Library) error {
s := l.server(name)
if s == nil {
return fmt.Errorf("no server called %s", name)
}
s.Local = on
return nil
})
}