magpie icon indicating copy to clipboard operation
magpie copied to clipboard

MCP hub lists itself: a Library server at this magpie's own /mcp/magpie/hub/<agent> nests the hub inside the hub (4000+ tool names in Claude Code)

Open ShumRain opened this issue 2 weeks ago • 1 comments

What happens

The Library has an HTTP server named magpie whose URL is this computer's own hub:

{"name": "magpie", "transport": "http", "url": "http://127.0.0.1:3425/mcp/magpie/hub/agy",
 "agents": ["agy", "antigravity", "claude", "codex", "copilot", "dsh", "kimi", "qoder"]}

With the MCP hub on, a Claude Code session starts with its context window close to the limit. After a few turns, autocompact runs and reports "Autocompact is thrashing: the context refilled to the limit within 3 turns of the previous compact, 3 times in a row."

The session's tool list shows the hub nested in itself:

  • Names like mcp__magpie__magpie__magpie__…, about 8 levels deep.
  • Past the length limit, the names are cut to numbered stubs (mag_1000–mag_4135, magp_100–magp_999, magpi_10–99). That is more than 4000 tool names in all.
  • Every other server, dida365 for example, shows up once at each level.

Why

  1. hub/agy lists the Library servers ticked for agy. This magpie entry is one of them, so the hub calls tools/list on itself. Each level adds another magpie__ prefix.
  2. The loop guard misses this case:
    • mcp_check.go sends X-Magpie-Router on any call to a /mcp/magpie/hub URL, and the receiving hub sees FromRouter.
    • But FromRouter only drops servers that have Bridge set (hubServersOf in gateway/hub_mcp.go, theHub.Tools in library/mcp_hub.go).
    • A plain Library entry pointing at the hub has no Bridge, so it is listed and called again.
  3. The Library server is named magpie, so hubFor returns hubTaken. Agents then get each Library server written directly, the self-pointing magpie entry included, in place of the hub's own entry. That is how Claude Code (claude is in agents) reaches the nested hub.

I don't know how the entry got into the Library. The /agy suffix suggests it was taken in from agy's MCP file, where magpie had written its hub entry, but I haven't confirmed that.

Suggested fix

  • In the hub, leave out any server whose URL resolves to this magpie's own gateway at HubMCPAll, with or without the agent suffix, whatever its name and whether or not it has Bridge.
  • If a Library entry can be created from an agent file (import or detection), skip entries that point at this magpie's own hub.

Workaround

Remove the magpie server from Library → MCP, or untick every agent on it, then apply. The hub's own entry is written again and the nesting goes away.

Environment

  • magpie 0.1.4574 on macOS, gateway at 127.0.0.1:3425, MCP hub on
  • Agent: Claude Code
  • Code read at 848e3b1 (main)

No keys or tokens are included.

ShumRain avatar Nov 20 '26 06:11 ShumRain

I have a fix ready on a fork branch (630c21ec, based on main at f65fe92): https://github.com/yetone/magpie/compare/main...eisen0419:magpie:fix/7305-mcp-hub-self-loop

Root cause & fix

  • hubServers and foundServersOf did not filter out Library entries pointing at this process's own /mcp/magpie/hub endpoint, and the X-Magpie-Router loop guard only dropped servers marked Bridge.
  • Added ownHub (internal/library/mcp_hub.go, using gateway.Here in internal/gateway/lan.go) to skip this hub's own URLs in hubServers and foundServersOf across loopback, unspecified, interface addresses, pinned MAGPIE_ADDR, and portless / path-prefixed MAGPIE_PUBLIC_URL or LAZYCAT_APP_DOMAIN (comparing cleaned paths with path.Clean, while keeping hubs on other hosts/ports and leaving hubTaken unchanged).
  • Stored the request Host with X-Magpie-Router (gateway.WithRouterHost) so routerLoop / sameHub also drops a plain self-pointing hub URL for hostnames ownHub does not know (including portless Host headers on default scheme ports), and remoteHeaders (internal/library/mcp_check.go) sends X-Magpie-Router for path-prefixed public hub URLs.
  • Updated docs/subsystems/mcp-router.md.

Verification

  • go test -race -count=1 ./internal/library/... (pass)
  • go test -race -count=1 -run 'TestHub|TestOwnHub|TestMCP|TestRouter' ./internal/gateway/... (pass)
  • gofmt -l, go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./..., and macOS/Linux(nogui)/Windows go build all clean.

eisen0419 avatar Nov 20 '26 10:11 eisen0419