magpie icon indicating copy to clipboard operation
magpie copied to clipboard

cli: magpie tool <memory server> is refused by magpie's own memory endpoint (HTTP 403 'answers the agents on this computer alone'), surfaced as 'isn't JSON-RPC'

Open ArmedHelicopter opened this issue 3 weeks ago • 2 comments

cli: magpie tool <memory server> is refused by magpie's own memory endpoint — HTTP 403 "answers the agents on this computer alone", surfaced as "isn't JSON-RPC"

Environment

  • magpie 0.1.3656 (Windows 11, app window, gateway on 127.0.0.1:3425)
  • Library → MCP: memory (npx -y @modelcontextprotocol/server-memory) ticked for agents including dsh
  • MCP router: ON

Steps to reproduce

  1. magpie tools memory_save — note the endpoint the CLI itself prints: memory_save (memory, /mcp/magpie/memory).
  2. Try to call it from the CLI on the same machine:
    magpie tool memory_save --text "hello" --kind fact --scope global
    

Actual

magpie: what http://127.0.0.1:3425 answered isn't JSON-RPC: is it magpie? (invalid character 'e' looking for beginning of value)

(exit code 1)

The response body the CLI is choking on is the gateway's plain-text refusal — a direct loopback POST shows it:

POST http://127.0.0.1:3425/mcp/magpie/memory
Content-Type: application/json
{"jsonrpc":"2.0","id":1,"method":"initialize",...}

HTTP 403  Content-Type: text/plain
magpie's memory MCP server answers the agents on this computer alone

So magpie tool — magpie's own CLI, running on this computer — is not recognized as an agent of "this computer" by the memory server's access check, and the refusal isn't wrapped as JSON-RPC, so the CLI's error is the generic "isn't JSON-RPC: is it magpie?".

Expected

One of:

  • magpie tool <name> is treated as a local agent call for "answers the agents on this computer alone" servers (the CLI is on this computer), or
  • magpie tool falls back to running the server over stdio for these (magpie mcp memory works fine — see below), or
  • the refusal is JSON-RPC-shaped and says plainly what happened, instead of tripping the CLI's transport sanity check.

Evidence that only the HTTP path is broken

Over stdio the same server answers and writes fine:

magpie mcp memory   (stdio)
tools/list   → memory_search, memory_save, memory_forget
tools/call memory_save {...} → {"result":{"content":[{"text":"Remembered: [m_270011bddb73] reference, everywhere, ..."}]}}

Why it matters

magpie tools advertises the memory tools as CLI-callable, and agents/scripts that only have the CLI (cron, hooks) currently can't use the memory server at all. The two-line error also misleads debugging: it looks like the gateway is down or replaced, not like an access check.

Not verified

  • Whether other "answers the agents on this computer alone" servers (e.g. magpie mcp browser's HTTP route, which 403s loopback without a gateway key the same way) have the same CLI gap.

ArmedHelicopter avatar Nov 11 '26 02:11 ArmedHelicopter

The 403 is a side track. magpie tool itself is not refused: it gets a 200, but the answer is an event stream (text/event-stream), and the CLI parses it as plain JSON. The 'e' in the error is the e of event: message.

Why it streams. toolGateway.do() sends Accept: application/json, text/event-stream (internal/cli/tool_cli.go:104) and no X-Magpie-Cwd. With no folder, memoryCallerOf falls back to hubRoots (internal/gateway/memory_mcp.go:253), which asks the client roots/list over the call's own stream (hub_mcp.go:838). Because the request accepts event streams (search_mcp.go:157), that question starts an SSE reply. The CLI never answers it, so the server waits hubRootsWait (3s), does the save, and sends the result as a second SSE event. Then rpc() runs json.Unmarshal on the whole body (tool_cli.go:141) and fails.

The direct POST got a 403 for a different reason. PowerShell's Invoke-RestMethod/Invoke-WebRequest send a User-Agent that starts with Mozilla/5.0, and fromBrowser() (internal/gateway/lan.go:746) refuses that as a web page. That refusal is by design and is not what the CLI hits.

Side effect to know about: the memory is saved even though the CLI prints an error and exits 1. Running it again merges ("Already remembered, merged"), so you get no duplicates. But a script that treats exit 1 as "not saved" is wrong.

What I ran. I added a test in internal/cli that runs the real gateway.New().Handler() behind httptest and calls runOut("tool", "memory_save", ...), followed by raw POSTs with different headers. Tested on main 6d15f63, go1.27.3, CGO_ENABLED=0 go test -tags nogui ./internal/cli -run TestRepro6049 -v (Linux, not Windows):

=== RUN   TestRepro6049
    repro6049_test.go:29: 1) magpie tool memory_save ...
           err=what http://127.0.0.1:36341 answered isn't JSON-RPC: is it magpie? (invalid character 'e' looking for beginning of value)
           out=""
    repro6049_test.go:32:    memories kept after 1): [m_852b941e1bb9] "hello"
    repro6049_test.go:55: 2) the CLI's own headers (tool_cli.go do())
           200 OK  Content-Type=text/event-stream
           "event: message\ndata: {\"id\":\"magpie-J47K7P6N2ZJHKRUEWLRKO6H32D\",\"jsonrpc\":\"2.0\",\"method\":\"roots/list\"}\n\nevent: message\ndata: {\"id\":1,\"jsonrpc\":\"2.0\",\"result\":{\"c..."
    repro6049_test.go:55: 3) same, Accept: application/json only
           200 OK  Content-Type=application/json
           "{\"id\":1,\"jsonrpc\":\"2.0\",\"result\":{\"content\":[{\"text\":\"Already remembered, merged: [m_852b941e1bb9] fact, everywhere, 2026-11-11: hello\",\"type\":\"text\"}],\"isError..."
    repro6049_test.go:55: 4) same as 2, plus X-Magpie-Cwd
           200 OK  Content-Type=application/json
           "{\"id\":1,\"jsonrpc\":\"2.0\",\"result\":{\"content\":[{\"text\":\"Already remembered, merged: [m_852b941e1bb9] fact, everywhere, 2026-11-11: hello\",\"type\":\"text\"}],\"isError..."
    repro6049_test.go:55: 5) PowerShell 7 Invoke-RestMethod User-Agent
           403 Forbidden  Content-Type=text/plain; charset=utf-8
           "magpie's memory MCP server answers the agents on this computer alone\n"
--- PASS: TestRepro6049 (6.02s)
PASS
ok  	github.com/yetone/magpie/internal/cli	6.023s

Fix. The CLI should name its folder, as the stdio path does (internal/browsermcp/browsermcp.go:129 sets MemoryCwdHeader). This also gives --scope project a project to use, which the CLI currently never has:

@@ -105,6 +105,9 @@
 	}
 	r.Header.Set("Authorization", "Bearer "+gateway.Token)
 	r.Header.Set("User-Agent", toolAgent)
+	if d, err := os.Getwd(); err == nil {
+		r.Header.Set(gateway.MemoryCwdHeader, d)
+	}
 	res, err := g.client.Do(r)
 	if err != nil {
 		return nil, errNoMagpie{g.base, err}

After that change, the same test:

=== RUN   TestRepro6049
    repro6049_test.go:29: 1) magpie tool memory_save ...
           err=<nil>
           out="Remembered: [m_463ea0e2ec13] fact, everywhere, 2026-11-11: hello\n"
    repro6049_test.go:32:    memories kept after 1): [m_463ea0e2ec13] "hello"

The rest of ./internal/cli gives the same result with and without the patch. Only TestEveryRouteHasACommandOrIsNamed fails, and it fails on unpatched main too. For robustness, rpc() should also either read text/event-stream (take the data: event whose id matches, and answer or decline server requests like roots/list) or stop advertising it. Otherwise any server-to-client request on any server will break the CLI the same way.

Workaround until then: use magpie mcp memory over stdio as you did, or any non-browser client that sends Accept: application/json only (case 3 above). PowerShell's cmdlets won't work here because of their User-Agent.

The CLI's error pointed at the wrong cause ("is it magpie?"), and only the raw body showed it. We keep a note on that pattern in our own repo (ours): https://github.com/aron-intframe/agent-guards#8-read-the-raw-session-not-the-first-line-of-the-log

aron-intframe avatar Nov 11 '26 03:11 aron-intframe

Fixed in v0.1.3988 (e39b9d5df), as aron-intframe described. magpie tool now sends the folder it runs in (X-Magpie-Cwd), so the memory server no longer asks it for roots. It also reads an answer sent as text/event-stream: it answers roots/list with its folder and refuses any other server request right away. The save no longer exits 1 after succeeding. The 403 for PowerShell's browser User-Agent is unchanged, since that refusal is by design. Details are in #6392.

yetone avatar Nov 13 '26 04:11 yetone