Files
magpie/internal/gateway/mcp_run_test.go
T
yetone 62045475b4 library: Codex on magpie reaches a command's MCP server through the gateway, run only while a thread uses it (#3885)
Codex starts every stdio server in its config once for each thread it
has loaded and keeps them as long as the thread: YPYT1 counted 204 node
processes, 21 GB, beside one Codex. While Codex is on magpie (Wired, not
disconnected, this machine's gateway key, not WSL) the library now gives
it a server run by a command as url = <gateway>/mcp/run/<name>, which
magpie serves over streamable HTTP and runs the command for itself
(internal/library/mcp_run.go):

- a session (Mcp-Session-Id) starts its own child only when it sends
  something past initialize, the first page of tools/prompts/resources
  lists and ping; those are answered from what the first child said for
  the same command, args and env, and a list_changed from a child drops
  that list;
- a (re)started child gets the session's own initialize replayed, so a
  server that keeps state per client still keeps it per thread;
- a child idle for 15 minutes is stopped (a later call starts it again),
  DELETE stops it, and a server changed or removed from the library has
  its children stopped at the next sync;
- server-initiated requests and notifications go to the session's newest
  open stream (GET, or a POST answered as SSE).
The gateway route lets only this machine in: no Origin, a loopback Host,
no forwarded or LAN address (TestRunMCPThisComputerOnly). A server with
an env var magpie hasn't, a remote one, and Codex off magpie, in WSL or
on another machine's key keep the command as before. agent.Rewired
re-syncs the library when Codex is connected or disconnected, so it is
handed the commands back when it leaves magpie.

Tests (internal/library/mcp_run_test.go, a fake server): starts on a
call and not on initialize/lists, idle end and DELETE, a server's own
request round-trips, list_changed, a changed/removed spec, an unknown
session, and the Codex entry in a sandbox (url when wired, the command
back when not, a keyed env var kept as a command). Each was red with its
part broken by hand: the init cache removed, the sweep off, runsFor
forced false, forget a no-op, the Origin/Host guard dropped.

Real runs:
- mote, sandbox HOME, real codex 0.162.1 + @modelcontextprotocol/
  server-memory: config.toml gets url = "http://127.0.0.1:3581/mcp/run/
  mem"; codex app-server's mcpServerStatus/list lists its 9 tools
  through the bridge.
- this Mac, sandbox HOME with only the DeepSeek provider copied, codex
  0.162.1 on deepseek/deepseek-flash: a real turn called mem
  create_entities and read_graph through the bridge and answered with
  the stored observation. Three codex exec turns at once that used no
  tool started 0 memory-server node processes (before: one each).
- Windows 11 (xiaomi-gpu), sandbox profile, codex 0.161.0: url entry
  written, app-server lists the 9 tools, 0 memory-server node.exe left
  after Codex closed.

Run at the pushed head (mote): GOOS=linux/windows/darwin go vet -tags
nogui ./internal/library/ ./internal/gateway/ ./internal/agent/: ok.
go test -count=1 -timeout 45m ./internal/library/ ./internal/gateway/
./internal/agent/ ./internal/gui/: library ok (167s), gateway ok
(596s), agent ok (279s); gui FAIL only TestMainWindowCorners/macOS_26_SDK
("a toolbar doesn't change the corners ... yet the main window's was
taken off"), #3949, red on mote's macOS 27 on main too; no gui or AppKit
file is touched here.
Windows (win-run): go test -run 'TestRun|TestFakeRunServer|TestMCPProxy'
./internal/library/ ./internal/gateway/: ok.

Not tried: the Codex desktop app (the same config.toml), a server that
asks the client for sampling or elicitation in a real agent. Codex picks
up the new entries when it restarts.

Reported-by: YPYT1
2026-10-29 03:06:48 +08:00

60 lines
1.8 KiB
Go

package gateway
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// /mcp/run/<name> starts a command the user gave their agents: only an
// agent on this computer has it run, never a web page (an Origin, a name
// of the page's own pointed at 127.0.0.1), a request a proxy or tunnel
// here passed on, or another machine.
func TestRunMCPThisComputerOnly(t *testing.T) {
fresh(t)
var ran []string
was := RunMCP
RunMCP = func(w http.ResponseWriter, r *http.Request, name string) {
ran = append(ran, name)
w.WriteHeader(http.StatusAccepted)
}
t.Cleanup(func() { RunMCP = was })
h := New().Handler()
for _, c := range []struct {
remote, host, origin, fwd string
}{
{"127.0.0.1:5000", "127.0.0.1:3425", "https://evil.example", ""},
{"127.0.0.1:5000", "127.0.0.1:3425", "http://localhost:3425", ""},
{"127.0.0.1:5000", "evil.example:3425", "", ""},
{"127.0.0.1:5000", "127.0.0.1:3425", "", "203.0.113.9"},
{"192.168.1.20:5000", "192.168.1.10:3425", "", ""},
} {
r := httptest.NewRequest("POST", "/mcp/run/fs", strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"tools/call"}`))
r.RemoteAddr, r.Host = c.remote, c.host
if c.origin != "" {
r.Header.Set("Origin", c.origin)
}
if c.fwd != "" {
r.Header.Set("X-Forwarded-For", c.fwd)
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
if w.Code != http.StatusForbidden || len(ran) != 0 {
t.Fatalf("%+v: got %d, ran %v", c, w.Code, ran)
}
}
for _, host := range []string{"127.0.0.1:3425", "localhost:3425", "[::1]:3425"} {
r := httptest.NewRequest("POST", "/mcp/run/fs", strings.NewReader(`{}`))
r.RemoteAddr, r.Host = "127.0.0.1:5000", host
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
if w.Code != http.StatusAccepted {
t.Fatalf("%s: got %d %s", host, w.Code, w.Body)
}
}
if strings.Join(ran, " ") != "fs fs fs" {
t.Fatalf("ran %v", ran)
}
}