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
204 lines
6.5 KiB
Go
204 lines
6.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"io"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/mcpauth"
|
|
"github.com/yetone/magpie/internal/netproxy"
|
|
)
|
|
|
|
// mcpClient relays the agents' requests to a remote MCP server magpie is
|
|
// signed in to. It has no timeout: a GET's event stream stays open for as
|
|
// long as the agent keeps it.
|
|
var mcpClient = &http.Client{Transport: mcpTransport()}
|
|
|
|
func mcpTransport() http.RoundTripper {
|
|
t := http.DefaultTransport.(*http.Transport).Clone()
|
|
t.Proxy = netproxy.Func
|
|
t.ResponseHeaderTimeout = 2 * time.Minute
|
|
return netproxy.Dispatch(t)
|
|
}
|
|
|
|
// mcpHop are the headers a relay doesn't pass on.
|
|
var mcpHop = map[string]bool{
|
|
"Authorization": true, "Host": true, "Connection": true, "Keep-Alive": true,
|
|
"Proxy-Authorization": true, "Proxy-Connection": true, "Te": true, "Trailer": true,
|
|
"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
|
|
// event stream, a GET's event stream, a DELETE that ends the session, with
|
|
// Mcp-Session-Id), and magpie sends it on with its own token, renewing it
|
|
// when it runs out or the server says it has.
|
|
func (s *Server) mcpProxy(w http.ResponseWriter, r *http.Request) {
|
|
name := r.PathValue("name")
|
|
if !local(r) && !sharedWith(r) {
|
|
http.Error(w, "magpie lends its MCP sign-ins to another machine only when magpie is shared on the local network and the request carries its API key", http.StatusForbidden)
|
|
return
|
|
}
|
|
// a web page can make the browser send a request here too (a form, or
|
|
// a hostname of its own pointed at 127.0.0.1), and magpie would carry
|
|
// it to the server with the user's sign-in; agents send no Origin and
|
|
// name this computer, as the MCP spec has servers check
|
|
if r.Header.Get("Origin") != "" || !sharedWith(r) && !loopbackHost(r.Host) {
|
|
http.Error(w, "magpie lends its MCP sign-ins to agents, not to web pages", http.StatusForbidden)
|
|
return
|
|
}
|
|
rec, ok := mcpauth.Get(name)
|
|
if !ok {
|
|
http.Error(w, "magpie isn't signed in to an MCP server called "+name+": sign in to it in magpie's Library", http.StatusNotFound)
|
|
return
|
|
}
|
|
// a body cut at the limit would go to the server as a shorter message
|
|
body, ok := readMCPBody(w, r, 32<<20)
|
|
if !ok {
|
|
return
|
|
}
|
|
target, err := url.Parse(rec.URL)
|
|
if err != nil {
|
|
http.Error(w, "the server's URL isn't one: "+err.Error(), http.StatusBadGateway)
|
|
return
|
|
}
|
|
if r.URL.RawQuery != "" {
|
|
q := target.Query()
|
|
for k, v := range r.URL.Query() {
|
|
q[k] = v
|
|
}
|
|
target.RawQuery = q.Encode()
|
|
}
|
|
token, err := mcpauth.Token(r.Context(), name)
|
|
if err != nil {
|
|
mcpRefused(w, name, err)
|
|
return
|
|
}
|
|
send := func(token string) (*http.Response, error) {
|
|
req, err := http.NewRequestWithContext(r.Context(), r.Method, target.String(), bytes.NewReader(body))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for k, v := range r.Header {
|
|
if mcpHop[http.CanonicalHeaderKey(k)] {
|
|
continue
|
|
}
|
|
// the gateway's own key, which the caller check put there
|
|
if strings.EqualFold(k, "x-api-key") && len(v) == 1 && v[0] == Token {
|
|
continue
|
|
}
|
|
req.Header[k] = v
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
if body == nil {
|
|
req.ContentLength = 0
|
|
}
|
|
return mcpClient.Do(req)
|
|
}
|
|
resp, err := send(token)
|
|
if err == nil && resp.StatusCode == http.StatusUnauthorized {
|
|
io.Copy(io.Discard, io.LimitReader(resp.Body, 1<<16))
|
|
resp.Body.Close()
|
|
fresh, rerr := mcpauth.Renew(r.Context(), name, token)
|
|
if rerr != nil {
|
|
mcpRefused(w, name, rerr)
|
|
return
|
|
}
|
|
resp, err = send(fresh)
|
|
if err == nil && resp.StatusCode == http.StatusUnauthorized {
|
|
// a token just renewed and refused all the same: the sign-in
|
|
// has run out, as the Library then says (新v学员 on Discord)
|
|
mcpauth.Rejected(name, fresh)
|
|
}
|
|
}
|
|
if err != nil {
|
|
if r.Context().Err() == nil {
|
|
log.Printf("mcp %s: %v", name, err)
|
|
}
|
|
http.Error(w, "couldn't reach the MCP server "+name+": "+err.Error(), http.StatusBadGateway)
|
|
return
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode == http.StatusUnauthorized {
|
|
// the agent would start a sign-in of its own, at magpie's address
|
|
io.Copy(io.Discard, io.LimitReader(resp.Body, 1<<16))
|
|
mcpRefused(w, name, mcpauth.ErrExpired)
|
|
return
|
|
}
|
|
for k, v := range resp.Header {
|
|
if mcpHop[http.CanonicalHeaderKey(k)] || strings.EqualFold(k, "WWW-Authenticate") {
|
|
continue
|
|
}
|
|
w.Header()[k] = v
|
|
}
|
|
w.WriteHeader(resp.StatusCode)
|
|
flusher, _ := w.(http.Flusher)
|
|
buf := make([]byte, 32<<10)
|
|
for {
|
|
n, err := resp.Body.Read(buf)
|
|
if n > 0 {
|
|
if _, werr := w.Write(buf[:n]); werr != nil {
|
|
return
|
|
}
|
|
if flusher != nil {
|
|
flusher.Flush()
|
|
}
|
|
}
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// mcpRefused answers an agent whose request magpie has no token for. It is
|
|
// not a 401, which would set the agent signing in itself.
|
|
func mcpRefused(w http.ResponseWriter, name string, err error) {
|
|
status := http.StatusBadGateway
|
|
msg := err.Error()
|
|
if errors.Is(err, mcpauth.ErrExpired) || errors.Is(err, mcpauth.ErrNotSignedIn) {
|
|
msg = "magpie's sign-in to the MCP server " + name + " has run out: sign in to it again in magpie's Library"
|
|
}
|
|
http.Error(w, msg, status)
|
|
}
|
|
|
|
// loopbackHost: a Host header naming this computer, localhost or a loopback
|
|
// address, with or without a port.
|
|
func loopbackHost(host string) bool {
|
|
if h, _, err := net.SplitHostPort(host); err == nil {
|
|
host = h
|
|
}
|
|
host = strings.Trim(host, "[]")
|
|
if strings.EqualFold(host, "localhost") {
|
|
return true
|
|
}
|
|
ip := net.ParseIP(host)
|
|
return ip != nil && ip.IsLoopback()
|
|
}
|