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)
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,
dida365for example, shows up once at each level.
Why
hub/agylists the Library servers ticked foragy. Thismagpieentry is one of them, so the hub callstools/liston itself. Each level adds anothermagpie__prefix.- The loop guard misses this case:
mcp_check.gosendsX-Magpie-Routeron any call to a/mcp/magpie/hubURL, and the receiving hub seesFromRouter.- But
FromRouteronly drops servers that haveBridgeset (hubServersOfingateway/hub_mcp.go,theHub.Toolsinlibrary/mcp_hub.go). - A plain Library entry pointing at the hub has no
Bridge, so it is listed and called again.
- The Library server is named
magpie, sohubForreturnshubTaken. Agents then get each Library server written directly, the self-pointingmagpieentry included, in place of the hub's own entry. That is how Claude Code (claudeis inagents) 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 hasBridge. - 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.
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
hubServersandfoundServersOfdid not filter out Library entries pointing at this process's own/mcp/magpie/hubendpoint, and theX-Magpie-Routerloop guard only dropped servers markedBridge.- Added
ownHub(internal/library/mcp_hub.go, usinggateway.Hereininternal/gateway/lan.go) to skip this hub's own URLs inhubServersandfoundServersOfacross loopback, unspecified, interface addresses, pinnedMAGPIE_ADDR, and portless / path-prefixedMAGPIE_PUBLIC_URLorLAZYCAT_APP_DOMAIN(comparing cleaned paths withpath.Clean, while keeping hubs on other hosts/ports and leavinghubTakenunchanged). - Stored the request
HostwithX-Magpie-Router(gateway.WithRouterHost) sorouterLoop/sameHubalso drops a plain self-pointing hub URL for hostnamesownHubdoes not know (including portlessHostheaders on default scheme ports), andremoteHeaders(internal/library/mcp_check.go) sendsX-Magpie-Routerfor 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)/Windowsgo buildall clean.