Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

library: Codex on magpie reaches a command's MCP server through the gateway, run only while a thread uses it (#3885)

yetone62045475b434
diff --git a/internal/agent/applied.go b/internal/agent/applied.go
index feffc2701..065c3f2af 100644
--- a/internal/agent/applied.go
+++ b/internal/agent/applied.go
@@ -512,8 +512,19 @@ type Connection struct {
Value string `json:"value,omitempty"`
}
+// Rewired, when set, is told of an agent that Connect or Disconnect
+// changed: the library gives Codex its servers another way on magpie.
+var Rewired func(id string)
+
+func (a *Agent) rewired(err error) {
+ if err == nil && Rewired != nil {
+ Rewired(a.ID)
+ }
+}
+
// ConnectHow is Connect, saying how the model was chosen.
-func (a *Agent) ConnectHow() (Connection, error) {
+func (a *Agent) ConnectHow() (c Connection, err error) {
+ defer func() { a.rewired(err) }()
if a.Native != nil {
return Connection{How: "joined"}, a.Native.Connect()
}
@@ -721,7 +732,8 @@ func connectWas(v string, opts []Option) string {
// still on magpie goes to its default, as picking it does, and so does
// each one magpie set that reads as magpie set it (an effort). What magpie
// remembered setting is forgotten.
-func (a *Agent) Disconnect() error {
+func (a *Agent) Disconnect() (err error) {
+ defer func() { a.rewired(err) }()
if a.Native != nil {
plan, err := a.Native.Disconnect()
if err != nil {
diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go
index a9cad81ac..1e6be71f6 100644
--- a/internal/gateway/gateway.go
+++ b/internal/gateway/gateway.go
@@ -809,6 +809,7 @@ func (s *Server) Handler() http.Handler {
mux.HandleFunc("GET /videos/{id}/content", s.videosContent)
mux.HandleFunc("POST "+claudeMCPPath+"{token}", s.subscription.mcpCall)
mux.HandleFunc("/mcp/{name}", s.mcpProxy)
+ mux.HandleFunc("/mcp/run/{name}", runMCP)
mux.HandleFunc(SearchMCPPath, s.searchMCP)
mux.HandleFunc(VisionMCPPath, s.visionMCP)
mux.HandleFunc(ImageMCPPath, s.imageMCP)
diff --git a/internal/gateway/mcp.go b/internal/gateway/mcp.go
index 4450617c9..cb6a8a8fc 100644
--- a/internal/gateway/mcp.go
+++ b/internal/gateway/mcp.go
@@ -34,6 +34,26 @@ var mcpHop = map[string]bool{
"Transfer-Encoding": true, "Upgrade": true, "Content-Length": true, "Accept-Encoding": true,
}
+// RunMCP serves /mcp/run/<server>: a server magpie runs by its command,
+// for an agent on this computer given it that way (library's ServeRun);
+// nil until the library sets it.
+var RunMCP func(w http.ResponseWriter, r *http.Request, name string)
+
+// runMCP is RunMCP, for this computer's agents alone: it starts a command
+// the user gave their agents, and no other machine, and no web page (one
+// posting a form, or of a name pointed at 127.0.0.1), may have it started.
+func runMCP(w http.ResponseWriter, r *http.Request) {
+ if !local(r) || r.Header.Get("Origin") != "" || !loopbackHost(r.Host) {
+ http.Error(w, "magpie runs the Library's MCP servers for the agents on this computer alone", http.StatusForbidden)
+ return
+ }
+ if RunMCP == nil {
+ http.Error(w, "magpie runs no MCP server here", http.StatusNotFound)
+ return
+ }
+ RunMCP(w, r, r.PathValue("name"))
+}
+
// mcpProxy is /mcp/<server>: a remote MCP server magpie signed in to once,
// for every agent the Library gives it to (#615). The agent speaks to it as
// to the server itself (streamable HTTP: a POST answered with JSON or an
diff --git a/internal/gateway/mcp_run_test.go b/internal/gateway/mcp_run_test.go
new file mode 100644
index 000000000..e084b09e7
--- /dev/null
+++ b/internal/gateway/mcp_run_test.go
@@ -0,0 +1,59 @@
+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)
+ }
+}
diff --git a/internal/library/main_test.go b/internal/library/main_test.go
index a3b97de9a..1bb8e302e 100644
--- a/internal/library/main_test.go
+++ b/internal/library/main_test.go
@@ -29,6 +29,9 @@ func TestMain(m *testing.M) {
ending = func([]*Server) {}
// never the registry (mcp_npx.go)
npxInstall = nil
+ // Codex given its servers by their commands, as the tests expect;
+ // mcp_run_test.go turns it on
+ runOn = false
code := testenv.Run(m)
forgetProbes()
if left := leftDownloads(tmp); len(left) > 0 && code == 0 {
diff --git a/internal/library/mcp_run.go b/internal/library/mcp_run.go
new file mode 100644
index 000000000..c2bdec884
--- /dev/null
+++ b/internal/library/mcp_run.go
@@ -0,0 +1,836 @@
+package library
+
+// A server run by a command is given to Codex, while Codex is on magpie,
+// as the gateway's /mcp/run/<name>, which magpie serves over streamable
+// HTTP by running the command itself. Codex starts every server of its
+// config once for each thread it has loaded, and keeps them for as long
+// as the thread is: 204 node processes, 21 GB, beside one user's Codex
+// (#3885). Here a thread's session starts its server only when the thread
+// calls on it, and ends it once the session has been idle for runIdle;
+// its initialize and its first lists are answered, after the first
+// session's, from what the server said then. A session that is in use
+// has a server of its own, as it had: what a server keeps for its client
+// (a browser, a page) is still kept per thread.
+//
+// Codex not on magpie, in WSL, or reaching the gateway with a key of its
+// own (another machine), and a server with a variable magpie hasn't, are
+// given the command as before.
+
+import (
+ "bufio"
+ "bytes"
+ "crypto/rand"
+ "encoding/hex"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "io"
+ "log"
+ "net/http"
+ "net/url"
+ "os"
+ "runtime"
+ "slices"
+ "strings"
+ "sync"
+ "time"
+
+ "github.com/yetone/magpie/internal/agent"
+ "github.com/yetone/magpie/internal/gateway"
+ "github.com/yetone/magpie/internal/proc"
+)
+
+// RunPath is where the gateway serves a server magpie runs for Codex.
+const RunPath = "/mcp/run/"
+
+// runIdle is how long a session's server is kept with nothing asked of it.
+var runIdle = 15 * time.Minute
+
+// runSpec is the command a server is run with, its variables expanded.
+type runSpec struct {
+ Command string `json:"command"`
+ Args []string `json:"args"`
+ Env map[string]string `json:"env"`
+}
+
+func (r runSpec) key() string { b, _ := json.Marshal(r); return string(b) }
+
+// specOf is the command magpie runs s with, false for a server it doesn't
+// run: a remote one, or one with a variable magpie's environment hasn't.
+func specOf(s *Server) (runSpec, bool) {
+ if s.Remote() || s.Command == "" {
+ return runSpec{}, false
+ }
+ env, unset := expandAll(s.Env)
+ if len(unset) > 0 {
+ return runSpec{}, false
+ }
+ return runSpec{Command: s.Command, Args: slices.Clone(s.Args), Env: env}, true
+}
+
+var runs struct {
+ sync.Mutex
+ sessions map[string]*runSession
+ // given are the servers the last sync gave Codex this way, by name
+ given map[string]runSpec
+ // wired is, by agent, whether the last sync gave it servers this way
+ wired map[string]bool
+ // said is what servers said, by their spec: initialize's result for
+ // each protocol version asked, and the first page of each list
+ said map[string]map[string]json.RawMessage
+ janitor sync.Once
+}
+
+// runsFor says whether t's agent is given servers through the gateway at
+// base: Codex on this machine, on magpie, reaching the gateway as this
+// machine does.
+func runsFor(t *Target, base string) bool {
+ return runOn && t.MCP != nil && t.MCP.Format == fmtCodex && !t.MCP.WSL && t.Agent != nil &&
+ t.Agent.WSL == "" && agent.KeyAt(base) == gateway.Token && runWired(t.Agent)
+}
+
+// runWired says the agent is on magpie, which it is given servers through
+// only while it is; a var for tests.
+var runWired = func(a *agent.Agent) bool { return a.Wired() && !a.Disconnected() }
+
+// runOn, off in the package's tests that aren't about it.
+var runOn = true
+
+func init() {
+ gateway.RunMCP = ServeRun
+ // a Codex connected or disconnected is given its servers the other
+ // way: through the gateway on magpie, by their commands off it
+ agent.Rewired = func(id string) {
+ if !runOn {
+ return
+ }
+ for _, t := range Targets() {
+ if t.Agent == nil || t.Agent.ID != id || t.MCP == nil || t.MCP.Format != fmtCodex {
+ continue
+ }
+ runs.Lock()
+ was, seen := runs.wired[id]
+ runs.Unlock()
+ if !seen || was != runsFor(t, gatewayOf(t)) {
+ go func() {
+ if _, err := Sync(); err != nil {
+ log.Printf("library: syncing %s's MCP servers: %v", id, err)
+ }
+ }()
+ }
+ return
+ }
+ }
+}
+
+// runVia is s as an agent that runsFor is given it, and the spec it is
+// run with; false for one magpie doesn't run.
+func runVia(s *Server, base string) (*Server, runSpec, bool) {
+ spec, ok := specOf(s)
+ if !ok {
+ return s, runSpec{}, false
+ }
+ c := *s
+ c.Transport, c.Command, c.Args, c.Env = "http", "", nil, nil
+ c.URL = strings.TrimRight(base, "/") + RunPath + url.PathEscape(s.Name)
+ return &c, spec, true
+}
+
+// runGiven records the servers a sync gave through the gateway, and ends
+// the servers of any session whose server is given no longer, or with
+// another command: the next call starts it as it is now.
+func runGiven(given map[string]runSpec) {
+ if given == nil {
+ given = map[string]runSpec{}
+ }
+ runs.Lock()
+ runs.given = given
+ var stop []*runSession
+ for _, s := range runs.sessions {
+ stop = append(stop, s)
+ }
+ runs.Unlock()
+ for _, s := range stop {
+ spec, ok := given[s.name]
+ s.mu.Lock()
+ changed := !ok || spec.key() != s.spec.key()
+ if changed {
+ s.spec, s.gone = spec, !ok
+ }
+ s.mu.Unlock()
+ if changed {
+ s.stop()
+ }
+ }
+}
+
+// runSpecOf is the spec of the server called name: as the last sync gave
+// it, or, before a sync has, as the library has it.
+func runSpecOf(name string) (runSpec, bool) {
+ runs.Lock()
+ given := runs.given
+ runs.Unlock()
+ if given != nil {
+ spec, ok := given[name]
+ return spec, ok
+ }
+ mu.Lock()
+ l, err := load()
+ mu.Unlock()
+ if err != nil {
+ return runSpec{}, false
+ }
+ s := l.server(name)
+ if s == nil || !slices.Contains(s.Agents, "codex") {
+ return runSpec{}, false
+ }
+ return specOf(npxDirect(s))
+}
+
+type runSession struct {
+ id, name string
+ start sync.Mutex // held while the server starts
+
+ mu sync.Mutex
+ spec runSpec
+ gone bool // the server is given no longer
+ init json.RawMessage // the session's initialize params
+ child *runChild
+ last time.Time
+ busy int
+ streams []chan []byte // the event streams open, newest last
+}
+
+func newSessionID() string {
+ b := make([]byte, 16)
+ _, _ = rand.Read(b)
+ return hex.EncodeToString(b)
+}
+
+// session is the session sid of name, made with init when there is none:
+// one magpie was restarted under is taken up as it is.
+func session(sid, name string, spec runSpec, init json.RawMessage) *runSession {
+ runs.Lock()
+ defer runs.Unlock()
+ if s := runs.sessions[sid]; s != nil && s.name == name {
+ return s
+ }
+ if runs.sessions == nil {
+ runs.sessions = map[string]*runSession{}
+ }
+ s := &runSession{id: sid, name: name, spec: spec, init: init, last: time.Now()}
+ runs.sessions[sid] = s
+ runs.janitor.Do(func() { go runJanitor() })
+ return s
+}
+
+// runJanitor ends the servers of idle sessions, and forgets sessions a
+// day idle.
+func runJanitor() {
+ for {
+ time.Sleep(runIdle / 15)
+ runSweep(time.Now())
+ }
+}
+
+func runSweep(now time.Time) {
+ runs.Lock()
+ var idle []*runSession
+ for id, s := range runs.sessions {
+ s.mu.Lock()
+ quiet := now.Sub(s.last)
+ if s.child != nil && s.busy == 0 && quiet > runIdle {
+ idle = append(idle, s)
+ }
+ if s.child == nil && s.busy == 0 && len(s.streams) == 0 && quiet > 24*time.Hour {
+ delete(runs.sessions, id)
+ }
+ s.mu.Unlock()
+ }
+ runs.Unlock()
+ for _, s := range idle {
+ s.stop()
+ }
+}
+
+// stop ends the session's server, if it has one running.
+func (s *runSession) stop() {
+ s.mu.Lock()
+ c := s.child
+ s.child = nil
+ s.mu.Unlock()
+ if c != nil {
+ c.end()
+ }
+}
+
+// running is the session's server while it runs.
+func (s *runSession) running() *runChild {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+ if s.child != nil && s.child.ended() {
+ s.child = nil
+ }
+ return s.child
+}
+
+// ensure is the session's server, started if it isn't running, and what
+// it answered the session's initialize with when it was started now.
+func (s *runSession) ensure() (*runChild, json.RawMessage, error) {
+ s.start.Lock()
+ defer s.start.Unlock()
+ if c := s.running(); c != nil {
+ return c, nil, nil
+ }
+ s.mu.Lock()
+ spec, init, gone := s.spec, s.init, s.gone
+ s.mu.Unlock()
+ if gone {
+ return nil, nil, fmt.Errorf("magpie gives Codex no server called %s now", s.name)
+ }
+ c, res, err := runStart(s, spec, init)
+ if err != nil {
+ return nil, nil, err
+ }
+ s.mu.Lock()
+ s.child = c
+ s.mu.Unlock()
+ return c, res, nil
+}
+
+// deliver passes a message of the server's that isn't a reply to the
+// newest stream open; a request with none open is refused, as it would
+// wait for an answer that never comes.
+func (s *runSession) deliver(c *runChild, line []byte, id json.RawMessage, method string) {
+ switch method {
+ case "notifications/tools/list_changed":
+ forget(s.spec, "tools/list")
+ case "notifications/prompts/list_changed":
+ forget(s.spec, "prompts/list")
+ case "notifications/resources/list_changed":
+ forget(s.spec, "resources/list", "resources/templates/list")
+ }
+ s.mu.Lock()
+ var to chan []byte
+ if n := len(s.streams); n > 0 {
+ to = s.streams[n-1]
+ }
+ s.mu.Unlock()
+ if to != nil {
+ select {
+ case to <- line:
+ return
+ default:
+ }
+ }
+ if len(id) > 0 {
+ _ = c.send(rpcError(id, -32603, "the client has no stream open for this request"))
+ }
+}
+
+func (s *runSession) open() chan []byte {
+ ch := make(chan []byte, 64)
+ s.mu.Lock()
+ s.streams = append(s.streams, ch)
+ s.mu.Unlock()
+ return ch
+}
+
+func (s *runSession) close(ch chan []byte) {
+ s.mu.Lock()
+ if i := slices.Index(s.streams, ch); i >= 0 {
+ s.streams = slices.Delete(s.streams, i, i+1)
+ }
+ s.mu.Unlock()
+}
+
+// runChild is a server magpie started for a session.
+type runChild struct {
+ tree *proc.Tree
+ in io.WriteCloser
+ errs *tail
+ wmu sync.Mutex
+ mu sync.Mutex
+ waits map[string]chan json.RawMessage
+ done chan struct{}
+}
+
+func (c *runChild) ended() bool {
+ select {
+ case <-c.done:
+ return true
+ default:
+ return false
+ }
+}
+
+func (c *runChild) end() {
+ c.in.Close()
+ c.tree.Kill()
+}
+
+func (c *runChild) send(b []byte) error {
+ c.wmu.Lock()
+ defer c.wmu.Unlock()
+ _, err := c.in.Write(append(slices.Clip(b), '\n'))
+ return err
+}
+
+// wait is where the reply to id comes; closed with none when the server
+// ends.
+func (c *runChild) wait(id json.RawMessage) chan json.RawMessage {
+ ch := make(chan json.RawMessage, 1)
+ c.mu.Lock()
+ c.waits[idKey(id)] = ch
+ c.mu.Unlock()
+ if c.ended() {
+ c.unwait(id)
+ }
+ return ch
+}
+
+func (c *runChild) unwait(id json.RawMessage) {
+ c.mu.Lock()
+ if ch, ok := c.waits[idKey(id)]; ok {
+ delete(c.waits, idKey(id))
+ close(ch)
+ }
+ c.mu.Unlock()
+}
+
+func idKey(id json.RawMessage) string { return string(bytes.TrimSpace(id)) }
+
+const runInitID = `"magpie-run-init"`
+
+// runStart starts the session's server and initializes it with the
+// session's own params, as Codex would have.
+func runStart(s *runSession, spec runSpec, init json.RawMessage) (*runChild, json.RawMessage, error) {
+ prog, found := stdioProgram(spec.Command)
+ cmd := proc.Command(prog, spec.Args...)
+ // as checkStdioAt starts it: from home, in magpie's environment with
+ // the server's own variables over it
+ cmd.Env = os.Environ()
+ if d := home(); d != "" {
+ cmd.Dir = d
+ if runtime.GOOS != "windows" {
+ cmd.Env = append(cmd.Env, "PWD="+d)
+ }
+ }
+ if found != "" {
+ cmd.Env = withPath(cmd.Env, found)
+ }
+ for k, v := range spec.Env {
+ cmd.Env = append(cmd.Env, k+"="+v)
+ }
+ in, err := cmd.StdinPipe()
+ if err != nil {
+ return nil, nil, err
+ }
+ out, err := cmd.StdoutPipe()
+ if err != nil {
+ return nil, nil, err
+ }
+ errs := &tail{}
+ cmd.Stderr = errs
+ tree, err := proc.StartTree(cmd)
+ if err != nil {
+ return nil, nil, fmt.Errorf("couldn't start %s: %w", spec.Command, err)
+ }
+ c := &runChild{tree: tree, in: in, errs: errs, waits: map[string]chan json.RawMessage{}, done: make(chan struct{})}
+ go func() {
+ err := tree.Wait()
+ if err != nil && !errors.Is(err, os.ErrProcessDone) {
+ if t := errs.last(); t != "" {
+ log.Printf("library: MCP server %s for Codex ended: %v: %s", s.name, err, t)
+ }
+ }
+ }()
+ go func() {
+ defer func() {
+ close(c.done)
+ c.mu.Lock()
+ for k, ch := range c.waits {
+ delete(c.waits, k)
+ close(ch)
+ }
+ c.mu.Unlock()
+ }()
+ sc := bufio.NewScanner(out)
+ sc.Buffer(make([]byte, 64*1024), 64<<20)
+ for sc.Scan() {
+ var m struct {
+ ID json.RawMessage `json:"id"`
+ Method string `json:"method"`
+ }
+ // a line that isn't JSON-RPC is a server logging to the
+ // wrong stream; agents pass over it too
+ if json.Unmarshal(sc.Bytes(), &m) != nil {
+ continue
+ }
+ line := slices.Clone(sc.Bytes())
+ if m.Method == "" && len(m.ID) > 0 {
+ c.mu.Lock()
+ ch, ok := c.waits[idKey(m.ID)]
+ delete(c.waits, idKey(m.ID))
+ c.mu.Unlock()
+ if ok {
+ ch <- line
+ close(ch)
+ }
+ continue
+ }
+ s.deliver(c, line, m.ID, m.Method)
+ }
+ }()
+ id := json.RawMessage(runInitID)
+ ch := c.wait(id)
+ req, _ := json.Marshal(map[string]any{"jsonrpc": "2.0", "id": id, "method": "initialize", "params": init})
+ if err := c.send(req); err != nil {
+ c.end()
+ return nil, nil, fmt.Errorf("couldn't start %s: %w", spec.Command, err)
+ }
+ timer := time.NewTimer(2 * time.Minute)
+ defer timer.Stop()
+ var reply json.RawMessage
+ select {
+ case reply = <-ch:
+ case <-timer.C:
+ }
+ var m struct {
+ Result json.RawMessage `json:"result"`
+ Error *struct {
+ Message string `json:"message"`
+ } `json:"error"`
+ }
+ if reply == nil || json.Unmarshal(reply, &m) != nil || m.Error != nil || len(m.Result) == 0 {
+ c.end()
+ why := "it didn't answer initialize"
+ switch {
+ case m.Error != nil:
+ why = "initialize: " + m.Error.Message
+ case c.ended() && errs.last() != "":
+ why = "it ended: " + errs.last()
+ }
+ return nil, nil, fmt.Errorf("couldn't start %s: %s", s.name, why)
+ }
+ _ = c.send(initializedNote)
+ return c, m.Result, nil
+}
+
+// runLists are the lists whose first page is answered from what a server
+// said before.
+var runLists = []string{"tools/list", "prompts/list", "resources/list", "resources/templates/list"}
+
+func said(spec runSpec, what string) json.RawMessage {
+ runs.Lock()
+ defer runs.Unlock()
+ return runs.said[spec.key()][what]
+}
+
+func remember(spec runSpec, what string, v json.RawMessage) {
+ runs.Lock()
+ defer runs.Unlock()
+ if runs.said == nil {
+ runs.said = map[string]map[string]json.RawMessage{}
+ }
+ m := runs.said[spec.key()]
+ if m == nil {
+ m = map[string]json.RawMessage{}
+ runs.said[spec.key()] = m
+ }
+ m[what] = v
+}
+
+func forget(spec runSpec, what ...string) {
+ runs.Lock()
+ defer runs.Unlock()
+ for _, w := range what {
+ delete(runs.said[spec.key()], w)
+ }
+}
+
+func rpcResult(id, result json.RawMessage) []byte {
+ b, _ := json.Marshal(map[string]any{"jsonrpc": "2.0", "id": id, "result": result})
+ return b
+}
+
+func rpcError(id json.RawMessage, code int, msg string) []byte {
+ b, _ := json.Marshal(map[string]any{"jsonrpc": "2.0", "id": id, "error": map[string]any{"code": code, "message": msg}})
+ return b
+}
+
+// rpcIn is a message from the client.
+type rpcIn struct {
+ ID json.RawMessage `json:"id"`
+ Method string `json:"method"`
+ Params json.RawMessage `json:"params"`
+ Result json.RawMessage `json:"result"`
+ Error json.RawMessage `json:"error"`
+}
+
+// ServeRun is the gateway's RunPath<name>: the server called name, run by
+// magpie, over streamable HTTP. The gateway lets only this machine's
+// agents here.
+func ServeRun(w http.ResponseWriter, r *http.Request, name string) {
+ sid := r.Header.Get("Mcp-Session-Id")
+ switch r.Method {
+ case http.MethodDelete:
+ runs.Lock()
+ s := runs.sessions[sid]
+ delete(runs.sessions, sid)
+ runs.Unlock()
+ if s != nil {
+ s.stop()
+ }
+ w.WriteHeader(http.StatusOK)
+ return
+ case http.MethodGet:
+ runStream(w, r, name, sid)
+ return
+ case http.MethodPost:
+ default:
+ w.Header().Set("Allow", "GET, POST, DELETE")
+ http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
+ return
+ }
+ body, err := io.ReadAll(io.LimitReader(r.Body, 32<<20+1))
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusBadRequest)
+ return
+ }
+ if len(body) > 32<<20 {
+ http.Error(w, "the message is over 32 MB", http.StatusRequestEntityTooLarge)
+ return
+ }
+ var m rpcIn
+ if t := bytes.TrimSpace(body); len(t) > 0 && t[0] == '[' {
+ http.Error(w, "magpie takes one JSON-RPC message a request", http.StatusBadRequest)
+ return
+ }
+ if json.Unmarshal(body, &m) != nil {
+ http.Error(w, "the body isn't a JSON-RPC message", http.StatusBadRequest)
+ return
+ }
+ var line bytes.Buffer
+ if json.Compact(&line, body) != nil {
+ http.Error(w, "the body isn't JSON", http.StatusBadRequest)
+ return
+ }
+ spec, ok := runSpecOf(name)
+ if !ok {
+ http.Error(w, "magpie gives Codex no server called "+name, http.StatusNotFound)
+ return
+ }
+ if m.Method == "initialize" {
+ runInitialize(w, name, spec, m)
+ return
+ }
+ if sid == "" {
+ http.Error(w, "no Mcp-Session-Id: initialize first", http.StatusBadRequest)
+ return
+ }
+ s := session(sid, name, spec, defaultInit())
+ w.Header().Set("Mcp-Session-Id", sid)
+ s.mu.Lock()
+ s.last = time.Now()
+ s.mu.Unlock()
+ if len(m.ID) == 0 || m.Method == "" {
+ // a notification, or the client's answer to the server's request:
+ // for the server, if it runs (it is told it is initialized when
+ // magpie starts it)
+ if c := s.running(); c != nil && m.Method != "notifications/initialized" {
+ _ = c.send(line.Bytes())
+ }
+ w.WriteHeader(http.StatusAccepted)
+ return
+ }
+ if m.Method == "ping" {
+ runReply(w, rpcResult(m.ID, json.RawMessage("{}")))
+ return
+ }
+ first := slices.Contains(runLists, m.Method) && !hasCursor(m.Params)
+ if first {
+ if v := said(spec, m.Method); v != nil {
+ runReply(w, rpcResult(m.ID, v))
+ return
+ }
+ }
+ s.mu.Lock()
+ s.busy++
+ s.mu.Unlock()
+ defer func() {
+ s.mu.Lock()
+ s.busy--
+ s.last = time.Now()
+ s.mu.Unlock()
+ }()
+ c, _, err := s.ensure()
+ if err != nil {
+ runReply(w, rpcError(m.ID, -32603, err.Error()))
+ return
+ }
+ reply := c.wait(m.ID)
+ if err := c.send(line.Bytes()); err != nil {
+ c.unwait(m.ID)
+ runReply(w, rpcError(m.ID, -32603, "the server "+name+" ended"))
+ return
+ }
+ got := func(b json.RawMessage) {
+ if !first {
+ return
+ }
+ var r struct {
+ Result json.RawMessage `json:"result"`
+ }
+ if json.Unmarshal(b, &r) == nil && len(r.Result) > 0 {
+ remember(spec, m.Method, r.Result)
+ }
+ }
+ if !strings.Contains(r.Header.Get("Accept"), "text/event-stream") {
+ select {
+ case b, ok := <-reply:
+ if !ok {
+ b = rpcError(m.ID, -32603, "the server "+name+" ended")
+ }
+ got(b)
+ runReply(w, b)
+ case <-r.Context().Done():
+ c.unwait(m.ID)
+ cancelled(c, m.ID)
+ }
+ return
+ }
+ events := s.open()
+ defer s.close(events)
+ w.Header().Set("Content-Type", "text/event-stream")
+ w.Header().Set("Cache-Control", "no-cache")
+ w.WriteHeader(http.StatusOK)
+ flush(w)
+ for {
+ select {
+ case b := <-events:
+ writeEvent(w, b)
+ case b, ok := <-reply:
+ if !ok {
+ b = rpcError(m.ID, -32603, "the server "+name+" ended")
+ }
+ got(b)
+ writeEvent(w, b)
+ return
+ case <-r.Context().Done():
+ c.unwait(m.ID)
+ cancelled(c, m.ID)
+ return
+ }
+ }
+}
+
+// runInitialize starts a session: answered from what the server said to
+// an initialize of the same protocol version before, else by starting it.
+func runInitialize(w http.ResponseWriter, name string, spec runSpec, m rpcIn) {
+ var p struct {
+ Version string `json:"protocolVersion"`
+ }
+ _ = json.Unmarshal(m.Params, &p)
+ sid := newSessionID()
+ s := session(sid, name, spec, m.Params)
+ w.Header().Set("Mcp-Session-Id", sid)
+ what := "initialize " + p.Version
+ if v := said(spec, what); v != nil {
+ runReply(w, rpcResult(m.ID, v))
+ return
+ }
+ s.mu.Lock()
+ s.busy++
+ s.mu.Unlock()
+ defer func() {
+ s.mu.Lock()
+ s.busy--
+ s.last = time.Now()
+ s.mu.Unlock()
+ }()
+ _, res, err := s.ensure()
+ if err != nil {
+ runReply(w, rpcError(m.ID, -32603, err.Error()))
+ return
+ }
+ if res != nil {
+ remember(spec, what, res)
+ } else if res = said(spec, what); res == nil {
+ runReply(w, rpcError(m.ID, -32603, "the server "+name+" was started already"))
+ return
+ }
+ runReply(w, rpcResult(m.ID, res))
+}
+
+// runStream is a GET's event stream: the server's messages while no
+// request's stream is open.
+func runStream(w http.ResponseWriter, r *http.Request, name, sid string) {
+ if !strings.Contains(r.Header.Get("Accept"), "text/event-stream") {
+ http.Error(w, "a GET here is an event stream", http.StatusNotAcceptable)
+ return
+ }
+ spec, ok := runSpecOf(name)
+ if !ok || sid == "" {
+ http.Error(w, "no session", http.StatusNotFound)
+ return
+ }
+ s := session(sid, name, spec, defaultInit())
+ events := make(chan []byte, 64)
+ s.mu.Lock()
+ // below the requests' streams, which are newer
+ s.streams = slices.Insert(s.streams, 0, events)
+ s.mu.Unlock()
+ defer s.close(events)
+ w.Header().Set("Content-Type", "text/event-stream")
+ w.Header().Set("Cache-Control", "no-cache")
+ w.Header().Set("Mcp-Session-Id", sid)
+ w.WriteHeader(http.StatusOK)
+ flush(w)
+ for {
+ select {
+ case b := <-events:
+ writeEvent(w, b)
+ case <-r.Context().Done():
+ return
+ }
+ }
+}
+
+// defaultInit is the initialize params a session magpie didn't see start
+// (one from before a restart) starts its server with.
+func defaultInit() json.RawMessage {
+ var m struct {
+ Params json.RawMessage `json:"params"`
+ }
+ _ = json.Unmarshal(initializeReq(), &m)
+ return m.Params
+}
+
+func hasCursor(params json.RawMessage) bool {
+ var p struct {
+ Cursor *string `json:"cursor"`
+ }
+ return json.Unmarshal(params, &p) == nil && p.Cursor != nil
+}
+
+func cancelled(c *runChild, id json.RawMessage) {
+ b, _ := json.Marshal(map[string]any{"jsonrpc": "2.0", "method": "notifications/cancelled", "params": map[string]any{"requestId": id, "reason": "the client went"}})
+ _ = c.send(b)
+}
+
+func runReply(w http.ResponseWriter, b []byte) {
+ w.Header().Set("Content-Type", "application/json")
+ _, _ = w.Write(b)
+}
+
+func writeEvent(w http.ResponseWriter, b []byte) {
+ _, _ = fmt.Fprintf(w, "event: message\ndata: %s\n\n", b)
+ flush(w)
+}
+
+func flush(w http.ResponseWriter) {
+ if f, ok := w.(http.Flusher); ok {
+ f.Flush()
+ }
+}
diff --git a/internal/library/mcp_run_test.go b/internal/library/mcp_run_test.go
new file mode 100644
index 000000000..e56f92d28
--- /dev/null
+++ b/internal/library/mcp_run_test.go
@@ -0,0 +1,417 @@
+package library
+
+import (
+ "bufio"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/yetone/magpie/internal/agent"
+)
+
+// The fake server magpie runs for Codex: this test binary again, with
+// MAGPIE_FAKE_RUN naming a log it writes a line to for each start (its
+// pid and the client named in the initialize it was given).
+func TestFakeRunServer(t *testing.T) {
+ logf := os.Getenv("MAGPIE_FAKE_RUN")
+ if logf == "" {
+ t.Skip("run by the run tests")
+ }
+ out := bufio.NewWriter(os.Stdout)
+ say := func(v any) {
+ b, _ := json.Marshal(v)
+ out.Write(append(b, '\n'))
+ out.Flush()
+ }
+ sc := bufio.NewScanner(os.Stdin)
+ var asked json.RawMessage // the tools/call waiting for the client's roots
+ for sc.Scan() {
+ var m struct {
+ ID json.RawMessage `json:"id"`
+ Method string `json:"method"`
+ Params struct {
+ Name string `json:"name"`
+ ClientInfo struct {
+ Name string `json:"name"`
+ } `json:"clientInfo"`
+ } `json:"params"`
+ Result json.RawMessage `json:"result"`
+ }
+ json.Unmarshal(sc.Bytes(), &m)
+ switch {
+ case m.Method == "initialize":
+ l, _ := os.OpenFile(logf, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
+ fmt.Fprintf(l, "%d %s\n", os.Getpid(), m.Params.ClientInfo.Name)
+ l.Close()
+ say(map[string]any{"jsonrpc": "2.0", "id": m.ID, "result": map[string]any{"protocolVersion": "2025-06-18", "capabilities": map[string]any{"tools": map[string]any{"listChanged": true}}, "serverInfo": map[string]any{"name": "fake", "version": "1"}}})
+ case m.Method == "tools/list":
+ say(map[string]any{"jsonrpc": "2.0", "id": m.ID, "result": map[string]any{"tools": []any{map[string]any{"name": "echo"}, map[string]any{"name": fmt.Sprint("pid", os.Getpid())}}}})
+ case m.Method == "tools/call" && m.Params.Name == "ask":
+ asked = m.ID
+ say(map[string]any{"jsonrpc": "2.0", "id": 77, "method": "roots/list"})
+ case m.Method == "tools/call" && m.Params.Name == "change":
+ say(map[string]any{"jsonrpc": "2.0", "method": "notifications/tools/list_changed"})
+ say(map[string]any{"jsonrpc": "2.0", "id": m.ID, "result": map[string]any{"content": []any{}}})
+ case m.Method == "tools/call":
+ say(map[string]any{"jsonrpc": "2.0", "id": m.ID, "result": map[string]any{"content": []any{map[string]any{"type": "text", "text": fmt.Sprint(os.Getpid())}}}})
+ case m.Method == "" && string(m.ID) == "77":
+ say(map[string]any{"jsonrpc": "2.0", "id": asked, "result": map[string]any{"content": []any{map[string]any{"type": "text", "text": "roots " + string(m.Result)}}}})
+ }
+ }
+ os.Exit(0)
+}
+
+// runFixture is the fake server given as "fake", served as the gateway
+// serves it, and the log of its starts.
+func runFixture(t *testing.T) (*httptest.Server, string, runSpec) {
+ t.Helper()
+ self, err := os.Executable()
+ if err != nil {
+ t.Fatal(err)
+ }
+ logf := filepath.Join(t.TempDir(), "starts")
+ spec := runSpec{Command: self, Args: []string{"-test.run=^TestFakeRunServer$"}, Env: map[string]string{"MAGPIE_FAKE_RUN": logf}}
+ resetRuns(t)
+ runGiven(map[string]runSpec{"fake": spec})
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ ServeRun(w, r, strings.TrimPrefix(r.URL.Path, RunPath))
+ }))
+ t.Cleanup(srv.Close)
+ return srv, logf, spec
+}
+
+// resetRuns ends every session's server, before the test and after it.
+func resetRuns(t *testing.T) {
+ end := func() {
+ runs.Lock()
+ all := runs.sessions
+ runs.sessions, runs.given, runs.said, runs.wired = nil, nil, nil, nil
+ runs.Unlock()
+ for _, s := range all {
+ s.mu.Lock()
+ c := s.child
+ s.mu.Unlock()
+ s.stop()
+ if c != nil {
+ <-c.done
+ }
+ }
+ }
+ end()
+ t.Cleanup(end)
+}
+
+func starts(t *testing.T, logf string) []string {
+ t.Helper()
+ b, _ := os.ReadFile(logf)
+ return strings.Fields(strings.ReplaceAll(string(b), "\n", " | "))
+}
+
+func startCount(logf string) int {
+ b, _ := os.ReadFile(logf)
+ return strings.Count(string(b), "\n")
+}
+
+// call posts one message as Codex does, and is the reply's body and the
+// session id it was given.
+func call(t *testing.T, srv *httptest.Server, sid, body string) (string, string) {
+ t.Helper()
+ req, _ := http.NewRequest("POST", srv.URL+RunPath+"fake", strings.NewReader(body))
+ req.Header.Set("Content-Type", "application/json")
+ req.Header.Set("Accept", "application/json, text/event-stream")
+ if sid != "" {
+ req.Header.Set("Mcp-Session-Id", sid)
+ }
+ resp, err := http.DefaultClient.Do(req)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer resp.Body.Close()
+ b, _ := io.ReadAll(resp.Body)
+ if resp.StatusCode != 200 && resp.StatusCode != 202 {
+ t.Fatalf("%s: %d %s", body, resp.StatusCode, b)
+ }
+ got := string(b)
+ // an event stream's last message is the reply
+ if strings.HasPrefix(resp.Header.Get("Content-Type"), "text/event-stream") {
+ var last string
+ for _, l := range strings.Split(got, "\n") {
+ if d, ok := strings.CutPrefix(l, "data: "); ok {
+ last = d
+ }
+ }
+ got = last
+ }
+ return got, resp.Header.Get("Mcp-Session-Id")
+}
+
+const runInit = `{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"%s","version":"1"}}}`
+
+// A second thread's session is initialized and listed from what the first
+// one's server said, with no server of its own; its server starts on its
+// first call, initialized as that thread itself asked.
+func TestRunStartsOnCall(t *testing.T) {
+ srv, logf, _ := runFixture(t)
+ r, a := call(t, srv, "", fmt.Sprintf(runInit, "thread-a"))
+ if a == "" || !strings.Contains(r, `"serverInfo"`) || !strings.Contains(r, `"id":0`) {
+ t.Fatalf("initialize: %s (session %q)", r, a)
+ }
+ call(t, srv, a, `{"jsonrpc":"2.0","method":"notifications/initialized"}`)
+ tools, _ := call(t, srv, a, `{"jsonrpc":"2.0","id":1,"method":"tools/list"}`)
+ if !strings.Contains(tools, `"echo"`) || !strings.Contains(tools, `"id":1`) {
+ t.Fatalf("tools/list: %s", tools)
+ }
+ if n := startCount(logf); n != 1 {
+ t.Fatalf("%d starts for the first session: %v", n, starts(t, logf))
+ }
+ for i := range 5 {
+ r, b := call(t, srv, "", fmt.Sprintf(runInit, fmt.Sprint("idle-", i)))
+ if b == "" || b == a || !strings.Contains(r, `"serverInfo"`) {
+ t.Fatalf("initialize %d: %s (session %q)", i, r, b)
+ }
+ call(t, srv, b, `{"jsonrpc":"2.0","method":"notifications/initialized"}`)
+ if got, _ := call(t, srv, b, `{"jsonrpc":"2.0","id":"x","method":"tools/list"}`); !strings.Contains(got, `"id":"x"`) || !strings.Contains(got, `"echo"`) {
+ t.Fatalf("tools/list %d: %s", i, got)
+ }
+ if got, _ := call(t, srv, b, `{"jsonrpc":"2.0","id":2,"method":"ping"}`); !strings.Contains(got, `"result":{}`) {
+ t.Fatalf("ping: %s", got)
+ }
+ }
+ if n := startCount(logf); n != 1 {
+ t.Fatalf("idle sessions started servers: %v", starts(t, logf))
+ }
+ r, b := call(t, srv, "", fmt.Sprintf(runInit, "thread-b"))
+ call(t, srv, b, `{"jsonrpc":"2.0","method":"notifications/initialized"}`)
+ got, _ := call(t, srv, b, `{"jsonrpc":"2.0","id":9,"method":"tools/call","params":{"name":"echo","arguments":{}}}`)
+ if !strings.Contains(got, `"id":9`) || !strings.Contains(got, `"text"`) {
+ t.Fatalf("tools/call: %s (init %s)", got, r)
+ }
+ if s := starts(t, logf); startCount(logf) != 2 || !strings.Contains(strings.Join(s, " "), "thread-b") {
+ t.Fatalf("starts: %v", s)
+ }
+ // the first session keeps its own server
+ got2, _ := call(t, srv, a, `{"jsonrpc":"2.0","id":10,"method":"tools/call","params":{"name":"echo","arguments":{}}}`)
+ if startCount(logf) != 2 || got2 == got {
+ t.Fatalf("session a: %s, b: %s, starts %v", got2, got, starts(t, logf))
+ }
+}
+
+// A session's server ends once it has been idle runIdle, and the next call
+// starts it again, initialized as the session was.
+func TestRunIdleEnds(t *testing.T) {
+ srv, logf, _ := runFixture(t)
+ _, a := call(t, srv, "", fmt.Sprintf(runInit, "thread-a"))
+ first, _ := call(t, srv, a, `{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"echo"}}`)
+ runs.Lock()
+ s := runs.sessions[a]
+ runs.Unlock()
+ c := s.running()
+ if c == nil {
+ t.Fatal("no server after a call")
+ }
+ runSweep(time.Now().Add(runIdle / 2))
+ if s.running() == nil {
+ t.Fatal("ended before it was idle")
+ }
+ runSweep(time.Now().Add(runIdle + time.Minute))
+ select {
+ case <-c.done:
+ case <-time.After(30 * time.Second):
+ t.Fatal("an idle server wasn't ended")
+ }
+ again, _ := call(t, srv, a, `{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"echo"}}`)
+ if !strings.Contains(again, `"id":2`) || again == first {
+ t.Fatalf("after idle: %s (was %s)", again, first)
+ }
+ if got := starts(t, logf); startCount(logf) != 2 || strings.Count(strings.Join(got, " "), "thread-a") != 2 {
+ t.Fatalf("starts: %v", got)
+ }
+ // DELETE ends the session and its server
+ c = s.running()
+ req, _ := http.NewRequest("DELETE", srv.URL+RunPath+"fake", nil)
+ req.Header.Set("Mcp-Session-Id", a)
+ resp, err := http.DefaultClient.Do(req)
+ if err != nil {
+ t.Fatal(err)
+ }
+ resp.Body.Close()
+ select {
+ case <-c.done:
+ case <-time.After(30 * time.Second):
+ t.Fatal("DELETE left the server running")
+ }
+}
+
+// What the server asks the client mid-call comes on the call's stream, and
+// the client's answer goes to the server.
+func TestRunServerAsks(t *testing.T) {
+ srv, _, _ := runFixture(t)
+ _, a := call(t, srv, "", fmt.Sprintf(runInit, "thread-a"))
+ req, _ := http.NewRequest("POST", srv.URL+RunPath+"fake", strings.NewReader(`{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"ask"}}`))
+ req.Header.Set("Accept", "application/json, text/event-stream")
+ req.Header.Set("Mcp-Session-Id", a)
+ resp, err := http.DefaultClient.Do(req)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer resp.Body.Close()
+ rd := bufio.NewReader(resp.Body)
+ next := func() string {
+ for {
+ l, err := rd.ReadString('\n')
+ if err != nil {
+ t.Fatalf("stream: %v", err)
+ }
+ if d, ok := strings.CutPrefix(strings.TrimSpace(l), "data: "); ok {
+ return d
+ }
+ }
+ }
+ if q := next(); !strings.Contains(q, `"roots/list"`) || !strings.Contains(q, `"id":77`) {
+ t.Fatalf("asked: %s", q)
+ }
+ call(t, srv, a, `{"jsonrpc":"2.0","id":77,"result":{"roots":[]}}`)
+ if r := next(); !strings.Contains(r, `"id":5`) || !strings.Contains(r, `roots {\"roots\":[]}`) {
+ t.Fatalf("reply: %s", r)
+ }
+}
+
+// A server saying its tools changed has the next session's tools/list go
+// to a server again.
+func TestRunListChanged(t *testing.T) {
+ srv, _, _ := runFixture(t)
+ _, a := call(t, srv, "", fmt.Sprintf(runInit, "a"))
+ before, _ := call(t, srv, a, `{"jsonrpc":"2.0","id":1,"method":"tools/list"}`)
+ _, b := call(t, srv, "", fmt.Sprintf(runInit, "b"))
+ call(t, srv, b, `{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"change"}}`)
+ // the notification is read before the reply after it
+ after, _ := call(t, srv, b, `{"jsonrpc":"2.0","id":1,"method":"tools/list"}`)
+ if before == after {
+ t.Fatalf("tools/list answered from before the change: %s", after)
+ }
+}
+
+// A server given no longer, or with another command, has its sessions'
+// servers ended; a new session of one gone is refused.
+func TestRunGivenChanged(t *testing.T) {
+ srv, logf, spec := runFixture(t)
+ _, a := call(t, srv, "", fmt.Sprintf(runInit, "a"))
+ call(t, srv, a, `{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"echo"}}`)
+ runs.Lock()
+ s := runs.sessions[a]
+ runs.Unlock()
+ c := s.running()
+ runGiven(map[string]runSpec{"fake": spec}) // the same: kept
+ if s.running() != c {
+ t.Fatal("a sync giving the same server ended it")
+ }
+ changed := spec
+ changed.Args = append([]string{"-test.v"}, spec.Args...)
+ runGiven(map[string]runSpec{"fake": changed})
+ <-c.done
+ call(t, srv, a, `{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"echo"}}`)
+ if startCount(logf) != 2 {
+ t.Fatalf("starts after the change: %v", starts(t, logf))
+ }
+ c = s.running()
+ runGiven(map[string]runSpec{})
+ <-c.done
+ req, _ := http.NewRequest("POST", srv.URL+RunPath+"fake", strings.NewReader(fmt.Sprintf(runInit, "c")))
+ resp, err := http.DefaultClient.Do(req)
+ if err != nil {
+ t.Fatal(err)
+ }
+ resp.Body.Close()
+ if resp.StatusCode != http.StatusNotFound {
+ t.Fatalf("a server given no longer: %d", resp.StatusCode)
+ }
+ req, _ = http.NewRequest("POST", srv.URL+RunPath+"fake", strings.NewReader(`{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"echo"}}`))
+ req.Header.Set("Mcp-Session-Id", a)
+ resp, err = http.DefaultClient.Do(req)
+ if err != nil {
+ t.Fatal(err)
+ }
+ resp.Body.Close()
+ if resp.StatusCode != http.StatusNotFound || s.running() != nil {
+ t.Fatalf("a session of a server given no longer: %d", resp.StatusCode)
+ }
+}
+
+// A session magpie didn't see start (Codex kept it over a restart of
+// magpie's) is taken up as it is.
+func TestRunUnknownSession(t *testing.T) {
+ srv, logf, _ := runFixture(t)
+ got, sid := call(t, srv, "old-session", `{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"echo"}}`)
+ if sid != "old-session" || !strings.Contains(got, `"id":4`) || strings.Contains(got, `"error"`) {
+ t.Fatalf("%s (session %q)", got, sid)
+ }
+ if startCount(logf) != 1 {
+ t.Fatalf("starts: %v", starts(t, logf))
+ }
+}
+
+// Codex on magpie is given a command's server as the gateway's, and a
+// sync after reads it back as magpie's own; off magpie it gets the
+// command again.
+func TestRunCodexEntry(t *testing.T) {
+ h := sandbox(t)
+ resetRuns(t)
+ runOn = true
+ wired := true
+ was := runWired
+ runWired = func(a *agent.Agent) bool { return wired }
+ t.Cleanup(func() { runOn, runWired = false, was })
+ if _, err := SaveServer("", Server{Name: "mem", Transport: "stdio", Command: "node", Args: []string{"server.js"}, Agents: []string{"codex", "claude"}}); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := SaveServer("", Server{Name: "keyed", Transport: "stdio", Command: "node", Env: map[string]string{"MAGPIE_TEST_UNSET_VAR": "${MAGPIE_TEST_UNSET_VAR}"}, Agents: []string{"codex"}}); err != nil {
+ t.Fatal(err)
+ }
+ codex := read(t, filepath.Join(h, ".codex/config.toml"))
+ if !strings.Contains(codex, RunPath+"mem") || strings.Contains(codex, `"server.js"`) {
+ t.Fatalf("codex on magpie:\n%s", codex)
+ }
+ if !strings.Contains(codex, `MAGPIE_TEST_UNSET_VAR`) || strings.Contains(codex, RunPath+"keyed") {
+ t.Fatalf("a server with a variable magpie hasn't:\n%s", codex)
+ }
+ if claude := read(t, filepath.Join(h, ".claude.json")); !strings.Contains(claude, `"server.js"`) || strings.Contains(claude, RunPath) {
+ t.Fatalf("claude:\n%s", claude)
+ }
+ runs.Lock()
+ _, given := runs.given["mem"]
+ runs.Unlock()
+ if !given {
+ t.Fatal("the sync didn't record the server it gave")
+ }
+ if r, err := Sync(); err != nil || len(r.Changed) != 0 || len(r.Problems) != 0 {
+ t.Fatalf("a second sync: %+v %v", r, err)
+ }
+ if v, err := Read(nil); err != nil {
+ t.Fatal(err)
+ } else {
+ for _, f := range v.FoundServers {
+ t.Errorf("found as the user's: %+v", f)
+ }
+ }
+ wired = false
+ if _, err := Sync(); err != nil {
+ t.Fatal(err)
+ }
+ codex = read(t, filepath.Join(h, ".codex/config.toml"))
+ if strings.Contains(codex, RunPath) || !strings.Contains(codex, `"server.js"`) {
+ t.Fatalf("codex off magpie:\n%s", codex)
+ }
+ runs.Lock()
+ _, given = runs.given["mem"]
+ runs.Unlock()
+ if given {
+ t.Fatal("a server given by its command again is still served")
+ }
+}
diff --git a/internal/library/sync.go b/internal/library/sync.go
index f7bd5f494..a375110f6 100644
--- a/internal/library/sync.go
+++ b/internal/library/sync.go
@@ -40,6 +40,9 @@ type Result struct {
// dropped are the servers taken out of an agent's config, as it had
// them, whose processes go once no agent runs them (mcp_reap.go)
dropped []*Server
+ // run are the servers given Codex as the gateway's, which magpie runs
+ // for it (mcp_run.go), by name
+ run map[string]runSpec
}
// Problem is one thing that couldn't be given to an agent.
@@ -77,6 +80,7 @@ func (l *Library) sync() *Result {
if b.dir != "" {
pruneBackups()
}
+ runGiven(res.run)
return res
}
@@ -173,6 +177,7 @@ func Sync() (*Result, error) {
ending(res.dropped)
}
npxPrune(npxDirs)
+ runGiven(res.run)
return res, nil
}
@@ -181,6 +186,16 @@ func (l *Library) syncMCP(t *Target, b *backups, res *Result) {
return
}
id := t.Agent.ID
+ base := gatewayOf(t)
+ run := runsFor(t, base)
+ if t.MCP.Format == fmtCodex && t.Agent != nil {
+ runs.Lock()
+ if runs.wired == nil {
+ runs.wired = map[string]bool{}
+ }
+ runs.wired[id] = run
+ runs.Unlock()
+ }
a := l.applied(id)
// an agent given no server, with none of magpie's in it, isn't read:
// a file of its that can't be read is nothing the library did
@@ -226,7 +241,6 @@ func (l *Library) syncMCP(t *Target, b *backups, res *Result) {
res.dropped = append(res.dropped, had)
}
}
- base := gatewayOf(t)
for _, s := range l.MCP {
if !slices.Contains(s.Agents, id) {
continue
@@ -250,6 +264,14 @@ func (l *Library) syncMCP(t *Target, b *backups, res *Result) {
if !t.MCP.WSL {
s = npxDirect(s)
}
+ if run {
+ if via, spec, ok := runVia(s, base); ok {
+ if res.run == nil {
+ res.run = map[string]runSpec{}
+ }
+ s, res.run[s.Name] = via, spec
+ }
+ }
s = wroteStartup(s, a.Startup[s.Name])
old := entries[s.Name]
if err := t.MCP.foreign(old); err != nil {