magpie-image was the one MCP server the hub left out, because it saves what it makes on disk and needs to know whose project that is. The hub now serves its tools like any other server's and tells it where the caller works: 1. the hub answers a tools/call as SSE and asks the agent roots/list on that stream (3s), as Claude Code answers; 2. else Codex's _meta x-codex-turn-metadata.workspaces (git repos only); 3. else the Codex thread's cwd from its session file (sessions.CodexThreadCwd), only with session consent; 4. else none, and the image goes to ~/Pictures/Magpie as before. The hub passes `magpie/roots` and `magpie/client` in the call's _meta; imagemcp saves into the first root and names the agent on its usage row. Its result carries `magpie/model`, which the hub's own usage row takes, so the row says which image model drew it. A WSL agent keeps magpie-image as its own entry (the hub can't reach a path inside the distro). docs/subsystems/mcp-hub.md (mcp-router.md after the next commit), gateway-routing.md and gateway-sessions.md say so. It is rebased on #4382 (b889ffe81), which lets the user keep a server out of the hub (Server.Hub = direct). inHub now asks both: a server kept direct is out, and magpie-image is in unless the agent is in WSL, so the user can keep magpie-image direct too; the editor's switch shows for it like any server, and the hub's line counts it with the others (mcp-hub-direct.test.cjs follows). Run at e2583df31 (this stack on86fef947e), on mote: gofmt -l clean; go vet for darwin, linux (-tags nogui) and windows; go build ./...; go test -tags nogui ./... -timeout 45m, 81 packages ok, 0 FAIL; library, gateway, imagemcp, sessions, cli and gui again alone, ok; site's node --test, 49 of 49. GUI on xiaomi-gpu (Windows; mote's browsers crashed under other sessions' suites): library-*, image*, mcp-hub-direct, i18n-keys, list-sort and plugin-tools, both engines and every language, 1055 of 1056: i18n-keys found "{name} is given to each agent directly" dropped from the library's strings in the rebase. Put back in the first commit; at the pushed head i18n-keys, mcp-hub-direct and library-mcp-hub are 48 of 48. That is the only difference from e2583df31. Broken by hand at e2583df31: imagemcp ignoring magpie/roots fails TestHubCallSavesInTheCallersProject; the router asking anything but roots/list fails TestHubAsksTheAgentItsRoots and TestHubRootsUnasked; inHub leaving magpie-image out fails TestHubGivesMagpieImage. Real use, sandbox HOME and gateway, at 62421e570 (before the rebase): Claude Code saved into its project via roots/list; Codex in a git repo via its workspaces _meta; Codex outside a repo with session consent via the thread's cwd. At 62421e570 the whole GUI suite ran on WSL magpie-desktop at load 116 to 135 from other runs: 504 of 15757 red in 118 files, WebKit timeouts, none in a file this change touches; filed as #4471 with the list. Rebased once more onto2570efb5f, which changed library-mcp-hub, list-sort, mcp-hub-direct and gateway-routing.md. Run at the pushed head: go build; gateway, library, cli and imagemcp tests ok; the GUI budget tests ok; on xiaomi-gpu i18n-keys, mcp-hub-direct, library-mcp-hub and list-sort, 51 of 54. The three reds were WebKit 420px library-mcp-hub cases with no Connect sheet after a reload. origin/maind77da5bffgoes red the same way (de 420px, 9 of 11). In a second run of library-mcp-hub on WebKit at this head, 11 of 11 passed. Filed as #4483. Not tried: Codex outside a git repo without consent (it saves to ~/Pictures/Magpie, #4435); agents on Windows and in WSL with real use.
87 lines
2.4 KiB
Go
87 lines
2.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// A request magpie's own MCP server makes of its client while it answers
|
|
// one of the client's (MCP Streamable HTTP): the POST is answered with an
|
|
// event stream instead of JSON, the request is one event on it, the client
|
|
// POSTs its answer, and the reply ends the stream. The hub asks so for an
|
|
// agent's roots (hubRoots).
|
|
|
|
type mcpExchangeKey struct{}
|
|
|
|
// mcpExchange is one POST's answer: canStream when the client takes an
|
|
// event stream back for it, streamed once the stream has begun.
|
|
type mcpExchange struct {
|
|
w http.ResponseWriter
|
|
canStream bool
|
|
mu sync.Mutex
|
|
streamed bool
|
|
}
|
|
|
|
// send writes m as one event of the stream, beginning it.
|
|
func (x *mcpExchange) send(m any) {
|
|
b, _ := json.Marshal(m)
|
|
x.mu.Lock()
|
|
defer x.mu.Unlock()
|
|
if !x.streamed {
|
|
h := x.w.Header()
|
|
h.Set("Content-Type", "text/event-stream")
|
|
h.Set("Cache-Control", "no-cache")
|
|
x.w.WriteHeader(http.StatusOK)
|
|
x.streamed = true
|
|
}
|
|
fmt.Fprintf(x.w, "event: message\ndata: %s\n\n", b)
|
|
http.NewResponseController(x.w).Flush()
|
|
}
|
|
|
|
// clientAsked are the requests made of clients, by id, each waiting for its answer.
|
|
var clientAsked sync.Map // string(id) -> chan mcpMessage
|
|
|
|
// clientAnswered hands a client's answer to the request it is of; one of
|
|
// nothing asked is let be.
|
|
func clientAnswered(m mcpMessage) {
|
|
if ch, ok := clientAsked.LoadAndDelete(string(m.ID)); ok {
|
|
ch.(chan mcpMessage) <- m
|
|
}
|
|
}
|
|
|
|
var errCantAsk = errors.New("the client takes no event stream back")
|
|
|
|
// askClient makes a request of the client whose call ctx is of, and is its
|
|
// answer, given within wait.
|
|
func askClient(ctx context.Context, method string, params any, wait time.Duration) (mcpMessage, error) {
|
|
x, _ := ctx.Value(mcpExchangeKey{}).(*mcpExchange)
|
|
if x == nil || !x.canStream {
|
|
return mcpMessage{}, errCantAsk
|
|
}
|
|
id, _ := json.Marshal("magpie-" + rand.Text())
|
|
ch := make(chan mcpMessage, 1)
|
|
clientAsked.Store(string(id), ch)
|
|
defer clientAsked.Delete(string(id))
|
|
req := map[string]any{"jsonrpc": "2.0", "id": json.RawMessage(id), "method": method}
|
|
if params != nil {
|
|
req["params"] = params
|
|
}
|
|
x.send(req)
|
|
t := time.NewTimer(wait)
|
|
defer t.Stop()
|
|
select {
|
|
case m := <-ch:
|
|
return m, nil
|
|
case <-t.C:
|
|
return mcpMessage{}, fmt.Errorf("the client didn't answer %s in %s", method, wait)
|
|
case <-ctx.Done():
|
|
return mcpMessage{}, ctx.Err()
|
|
}
|
|
}
|