magpie icon indicating copy to clipboard operation
magpie copied to clipboard

Feature: per-server Hub mode and project-aware backends for cwd-sensitive MCP servers

Open cuipengcx90 opened this issue 1 month ago • 6 comments

Problem

The MCP Hub currently has one global switch. When enabled, every Library MCP server assigned to an HTTP-capable agent is replaced by the single magpie Hub entry. This works well for stateless global servers, but it prevents mixed setups where some servers should be shared while others must retain project- or session-specific state.

Examples of servers that cannot safely share the current backend:

  • a code indexer that resolves its repository from the process working directory;
  • a memory server that resolves its bank once at startup from the project directory;
  • a goal/task server that binds itself to a project registry at startup;
  • browser automation whose page/session state should remain isolated.

The Hub starts command servers with the user's home directory as cwd and keys the backend pool only by server name. Consequently, these servers either resolve the wrong project or share state across unrelated agents and repositories.

Current workaround

Library Projects can write those servers into project-level MCP files while global, stateless servers remain user-level Hub candidates. This keeps configuration under Magpie, but every repository and worktree must be registered separately. It is difficult for users with many short-lived worktrees and does not help a user-level server that needs per-session roots.

Proposed options

Option A: per-server Hub policy

Add a policy to each Library MCP server, for example:

  • hub: shared — current Hub behavior;
  • hub: direct — keep this server as its own entry in each agent;
  • optionally hub: session or hub: project — relay through Magpie but isolate the backend.

The Library would remain the source of truth; users would not need to hand-maintain each agent's MCP configuration.

Option B: roots-aware backend isolation

For cwd-sensitive command servers, use MCP roots (or equivalent agent/project context) and key the backend pool by (server, roots/project/session) rather than server name alone. The existing special handling for Magpie's own image server, which needs agent roots, may be a useful precedent.

Both options could coexist: a simple direct opt-out first, followed by roots-aware sharing.

Expected behavior

  • Stateless services can still run once and be shared across agents.
  • Project-sensitive services resolve the correct repository or remain direct.
  • Enabling Hub does not force all Library servers for an agent into one lifecycle policy.
  • Library remains the single configuration source.

Related

  • #2679 — MCP Hub
  • #3660 — per-server network proxy settings; its direct means bypassing the network proxy, not bypassing the Hub

I checked the latest released source and main: Library.MCPHub is still a single boolean and inHub currently includes every server except Magpie's own image server. I could not find an existing issue or PR for per-server Hub opt-out or project/session-aware Hub backends.

cuipengcx90 avatar Oct 30 '26 06:10 cuipengcx90

v0.1.2242 (b889ffe81) adds the first step of Option A: a hub policy for each server.

  • With the MCP hub on, open a server in Library → MCP and turn off Through the MCP hub. That server then goes into each agent's MCP config as its own entry, written exactly as with the hub off. The agent starts it itself, in the agent's own folder and session. The other servers still go through magpie's single entry. When every server an agent has is direct, that agent gets no hub entry at all.
  • CLI: magpie library mcp hub <name> direct (or shared, which is the default).
  • Switching rewrites the agents' configs at once and removes the old entry.

Tried: unit tests, which go red on the old code. In a sandbox HOME, real .claude.json and Codex config.toml files were written: a direct entry sits beside magpie, and with every server direct the files are byte-for-byte the same as with the hub off. Also the GUI tests in Chromium and WebKit. Not tried: a real cwd-sensitive server inside a real agent session.

Option B (per-project/roots backends behind the hub) is not built and is left for later. Keeping this open for that and for @cuipengcx90 to confirm the direct switch covers your case.

yetone avatar Oct 30 '26 16:10 yetone

Confirmed on v0.1.2286: the per-server direct switch fixes the cwd problem for Claude and Pi, but not for Codex.

Codex is never given the command. For command servers, magpie writes:

[mcp_servers.cocoindex]
url = "http://127.0.0.1:3425/mcp/run/cocoindex"

ServeRun starts it through runStart, which sets cmd.Dir = home() (internal/library/mcp_run.go). No agent cwd or roots are passed to the run session.

Repro on this machine:

  1. magpie library mcp hub cocoindex direct
  2. Codex config still contains url = "http://127.0.0.1:3425/mcp/run/cocoindex"
  3. POST initialize to /mcp/run/cocoindex returns: couldn't start cocoindex: it ended: Run ccc init in your project root to get started.

So direct only changes which entry the agent gets, not who starts it. This was also true before the new switch (hub off), but the issue's stated guarantee — "The agent starts it itself, in the agent's own folder and session" — does not hold for Codex.

Possible fixes:

  1. Let direct bypass runVia for Codex and write the command itself, accepting the per-thread process cost for servers the user explicitly marked direct.
  2. Pass the agent's cwd/roots to /mcp/run/<name> and key the run session by it (closer to Option B, but at least makes direct honest).

Everything else checks out: Claude/Pi direct stdio entries keep the same command, args and env as with the hub off, and shared servers are served through the hub as expected. No tool loss observed.

cuipengcx90 avatar Oct 31 '26 01:10 cuipengcx90

To make the scope clear: this is not a regression from the Hub or from the per-server switch.

Before either change, Codex's cocoindex was already written as url = "http://127.0.0.1:3425/mcp/run/cocoindex" and already started from home — a pre-change backup confirms it. /mcp/run predates the switch and exists for #3885: Codex starts every server once per loaded thread and keeps them (204 node processes / 21 GB in that report), so magpie runs command servers lazily per thread and stops them after idle.

So the new direct policy simply does not change the Codex path. The fix is to make it honest: either bypass runVia for servers explicitly marked direct, or pass the agent's cwd/roots into /mcp/run. Other agents are unaffected because runsFor requires Format == fmtCodex; they get the raw command and start it in their own cwd.

cuipengcx90 avatar Oct 31 '26 01:10 cuipengcx90

One more data point for the fix: Codex does not currently send cwd or roots, so there is no path for /mcp/run to read.

Captured initialize from codex-mcp-client/0.166.0 against a local HTTP MCP server:

{"protocolVersion":"2025-06-18",
 "capabilities":{"elicitation":{"form":{},"url":{}}},
 "clientInfo":{"name":"codex-mcp-client","title":"Codex","version":"0.166.0"}}

No roots capability, and the HTTP headers carry only protocol/session fields — no cwd. So "pass the path into the run session" needs a carrier first.

Options, smallest first:

  1. Let direct bypass runVia. Codex starts the command itself and uses its own local_process_cwd, as it already does for servers magpie cannot run. Cost: per-thread processes for the few servers explicitly marked direct.
  2. Keep the proxy and add a carrier. Codex supports http_headers_helper, which runs with the project cwd. magpie could write a helper that emits X-Magpie-Cwd, then ServeRun validates it as cmd.Dir and keys the run session by (name, cwd). No Codex change needed, but the helper's header cache must be per runtime context, not global.
  3. Upstream roots. If Codex advertises MCP roots (or a cwd metadata field), /mcp/run can use the first root as cwd, the same way magpie's image MCP already asks its agent for roots.

Option 1 is the smallest honest fix; option 2 is the one that keeps the process benefit.

cuipengcx90 avatar Oct 31 '26 02:10 cuipengcx90

v0.1.2304 (0d5df496a) makes direct hold for Codex too. A server set to direct is no longer handed to Codex as url = ".../mcp/run/<name>". Codex now gets the server's own command, args and env, the same as every other agent, with the router on or off. magpie writes no cwd, so Codex starts the server in the thread's folder: when an entry has no cwd, Codex's LocalStdioServerLauncher uses the session's cwd (codex-rs codex-mcp/src/rmcp_client.rs → stdio_server_launcher.rs). /mcp/run won't start a direct server any more either. Servers left on shared are still run by magpie for Codex, as #3885 needs.

What was tried:

  • A unit test (TestRunDirectServerIsCodexsCommand) that goes red on the old code, showing your exact url = "http://127.0.0.1:3425/mcp/run/cocoindex".
  • A real Codex 0.165.0 in a sandbox CODEX_HOME, with an entry shaped as magpie now writes it, running a tiny stdio server that logs its cwd. Started from another folder with codex exec -C <proj>, the server logged <proj>.

Not tried: real cocoindex, and the Codex desktop app (app-server) with a thread whose folder differs from where the app runs.

Other agents: /mcp/run was the only place a command server became a URL, and only for Codex. Every other agent already got the command and started it itself.

@cuipengcx90, once you're on v0.1.2304, could you check that cocoindex in Codex's config.toml is a command entry and that it finds your project? Keeping this open until you confirm, and for Option B.

yetone avatar Oct 31 '26 02:10 yetone

Confirmed fixed on v0.1.2310 (includes the v0.1.2304 change).

Codex's config now gets the server's own command instead of /mcp/run:

[mcp_servers.cocoindex]
command = "ccc"
args = ["mcp"]

Real check with codex exec -C /Users/cuip/develop/WorkSpaces/idea/smyze/bar-smyze/bar_smyze_opserver: the spawned ccc mcp process has cwd = that project directory. So direct now holds for Codex too.

One scope note for future readers: only Codex was affected. Claude/Pi direct servers were already stdio and started in the agent's cwd; their HTTP entry is the hub, which is for shared/stateless servers and still starts commands in home by design. A cwd-sensitive server must stay direct — putting it in the hub would still be wrong for every agent, not just Codex.

cuipengcx90 avatar Oct 31 '26 02:10 cuipengcx90