Files
magpie/internal/gateway/mcp_test.go
T
yetone 778dc8dc41 gui, mcp: Sign in in an MCP server's editor signs in to the server the form shows, saving it first, and a server that needs no sign-in is said to work as it is (lc on Discord: exa mcp 无法OAuth使用,x-api-key + key也无法使用 agent使用的是omp) — Exa signs in only at mcp.exa.ai/mcp?login (401 with resource_metadata, auth.exa.ai, S256, dynamic registration); plain /mcp answers without one. Sign in sent only the name, so the backend probed the saved URL: a URL just changed to ?login, unsaved, got "this server answers without a sign-in: there is nothing to sign in to". Now Sign in saves the form first when its name, transport, URL or headers aren't the saved server's, leaves the dialog open, and signs in to it; a server being added, or one switched to HTTP, has Sign in too, and a later Save is of the server as saved. mcpauth.ErrNoSignIn says the server already works without signing in, so the agents can use it as it is, a key goes in Headers and a server that signs in at another address (Exa's ?login) needs that URL; the GUI shows it in Chinese. The gateway's /mcp/<name> relay already kept the signed-in URL's query (?login reaches the server) and passed the Library's headers (x-api-key) on, dropping only the gateway's own key: TestMCPProxyKeepsQueryAndHeaders holds it (the fake now records the query). omp reads magpie's mcp.json entry as written (type/url/headers, discovery/builtin.ts) and sends headers on every request (mcp/transports/http.ts): with omp 18.4.9's own client under a temp HOME, mcp.exa.ai/mcp with x-api-key listed Exa's tools and ?login without a sign-in got the 401, so the encoder is unchanged. mcp-signin.test.cjs (webkit, en and zh): an unchanged Exa says it works as it is, with nothing saved; its URL changed to ?login, Sign in saves it then signs in, the dialog stays and no click moves the page; old code started the sign-in without saving. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass but TestProvidersAnswerWhileListsComeIn, a timing test that passes on rerun; library and mcp GUI tests pass in webkit (chromium didn't launch here). Not tried: a real Exa OAuth sign-in in magpie, or omp through magpie's relay with a real account.
2026-10-04 01:52:46 +08:00

211 lines
7.5 KiB
Go

package gateway
import (
"bufio"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/mcpauth"
"github.com/yetone/magpie/internal/mcpauth/mcpauthtest"
)
// /mcp/<server> is a remote MCP server magpie signed in to (#615), as the
// agents reach it: a POST's JSON answer and its event stream, a GET's
// stream and a DELETE go to the server with magpie's token in place of the
// gateway's, and the session's headers both ways. A token the server
// refuses is renewed once; one it won't renew is said to need signing in
// again, never with a 401 that would set the agent signing in itself.
func TestMCPProxy(t *testing.T) {
fresh(t)
f := mcpauthtest.New(t)
f.SignIn(t, "neon")
gw := httptest.NewServer(New().Handler())
defer gw.Close()
call := func(method, body string, hdr ...string) *http.Response {
t.Helper()
req, _ := http.NewRequest(method, gw.URL+"/mcp/neon", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json, text/event-stream")
req.Header.Set("Authorization", "Bearer "+Token)
for i := 0; i+1 < len(hdr); i += 2 {
req.Header.Set(hdr[i], hdr[i+1])
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
return resp
}
token, _ := mcpauth.Token(t.Context(), "neon")
resp := call("POST", `{"jsonrpc":"2.0","id":1,"method":"initialize"}`, "X-Team", "acme", "MCP-Protocol-Version", "2025-06-18")
b, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != 200 || !strings.Contains(string(b), `"method":"initialize"`) || resp.Header.Get("Mcp-Session-Id") != "sess-1" {
t.Fatalf("initialize: %d %s %v", resp.StatusCode, b, resp.Header)
}
if got := f.Seen.Get("Authorization"); got != "Bearer "+token {
t.Fatalf("the server got Authorization %q, not magpie's token", got)
}
if f.Seen.Get("X-Team") != "acme" || f.Seen.Get("Mcp-Protocol-Version") != "2025-06-18" {
t.Fatalf("headers not passed on: %v", f.Seen)
}
// a call answered as an event stream comes through as one
resp = call("POST", `{"jsonrpc":"2.0","id":2,"method":"tools/call"}`, "Mcp-Session-Id", "sess-1")
b, _ = io.ReadAll(resp.Body)
resp.Body.Close()
if !strings.HasPrefix(resp.Header.Get("Content-Type"), "text/event-stream") || !strings.Contains(string(b), "notifications/progress") || !strings.Contains(string(b), `"id":2,"result"`) {
t.Fatalf("tools/call: %v %s", resp.Header, b)
}
// a GET's stream is passed on as it comes, an event at a time
resp = call("GET", "", "Mcp-Session-Id", "sess-1")
rd := bufio.NewReader(resp.Body)
first, err := rd.ReadString('\n')
if err != nil || first != "id: 1\n" {
t.Fatalf("stream: %q %v", first, err)
}
rest, _ := io.ReadAll(rd)
resp.Body.Close()
if !strings.Contains(string(rest), `"n":2`) {
t.Fatalf("stream ended early: %s", rest)
}
resp = call("DELETE", "", "Mcp-Session-Id", "sess-1")
resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
t.Fatalf("DELETE: %d", resp.StatusCode)
}
if want := "DELETE sess-1"; f.Calls[len(f.Calls)-1] != want {
t.Fatalf("calls %q", f.Calls)
}
// the server ends the token early: renewed once, and the call goes through
f.Revoke()
resp = call("POST", `{"jsonrpc":"2.0","id":3,"method":"tools/list"}`)
b, _ = io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != 200 || !strings.Contains(string(b), "tools/list") || f.Refreshed != 1 {
t.Fatalf("after the server refused the token: %d %s, renewed %d", resp.StatusCode, b, f.Refreshed)
}
// it won't renew it: sign in again, and no 401 for the agent to act on
f.Revoke()
f.ForgetRefresh()
resp = call("POST", `{"jsonrpc":"2.0","id":4,"method":"tools/list"}`)
b, _ = io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != http.StatusBadGateway || resp.Header.Get("WWW-Authenticate") != "" || !strings.Contains(string(b), "sign in to it again") {
t.Fatalf("a sign-in run out: %d %v %s", resp.StatusCode, resp.Header, b)
}
// a server magpie isn't signed in to
req, _ := http.NewRequest("POST", gw.URL+"/mcp/notion", strings.NewReader("{}"))
r2, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
r2.Body.Close()
if r2.StatusCode != http.StatusNotFound {
t.Fatalf("unknown server: %d", r2.StatusCode)
}
}
// Another machine is lent magpie's sign-ins only through the gateway shared
// on the local network, with its key.
func TestMCPProxyStaysHere(t *testing.T) {
fresh(t)
t.Setenv("MAGPIE_ADDR", "0.0.0.0:3425")
f := mcpauthtest.New(t)
f.SignIn(t, "neon")
h := lanGuard(New().Handler())
r := httptest.NewRequest("POST", "/mcp/neon", strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"initialize"}`))
r.RemoteAddr = "192.168.1.9:5000"
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
if w.Code != http.StatusForbidden || len(f.Calls) != 0 {
t.Fatalf("another machine got %d, calls %v", w.Code, f.Calls)
}
}
// A web page can't borrow magpie's sign-ins through the browser: a request
// with an Origin, or one to a hostname of the page's own pointed at
// 127.0.0.1, is refused before it reaches the server.
func TestMCPProxyNotForWebPages(t *testing.T) {
fresh(t)
f := mcpauthtest.New(t)
f.SignIn(t, "neon")
h := New().Handler()
for _, c := range []struct{ host, origin string }{
{"127.0.0.1:3425", "https://evil.example"},
{"evil.example:3425", ""},
} {
r := httptest.NewRequest("POST", "/mcp/neon", strings.NewReader(`{"jsonrpc":"2.0","id":1,"method":"tools/call"}`))
r.RemoteAddr = "127.0.0.1:5000"
r.Host = c.host
if c.origin != "" {
r.Header.Set("Origin", c.origin)
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
if w.Code != http.StatusForbidden || len(f.Calls) != 0 {
t.Fatalf("host %s origin %q: got %d, calls %v", c.host, c.origin, w.Code, f.Calls)
}
}
for _, host := range []string{"localhost:3425", "[::1]:3425", "127.0.0.1"} {
if !loopbackHost(host) {
t.Fatalf("%s is this computer", host)
}
}
}
// A server signed in to at an address with a query (Exa's
// mcp.exa.ai/mcp?login, where its sign-in is) is reached at that address,
// the query as it was, and the headers the Library gives it (a key) go
// with magpie's token; only the gateway's own key stays here.
func TestMCPProxyKeepsQueryAndHeaders(t *testing.T) {
fresh(t)
f := mcpauthtest.New(t)
mcpauthtest.SignIn(t, "exa", f.URL+"?login")
gw := httptest.NewServer(New().Handler())
defer gw.Close()
call := func(body string, hdr ...string) {
t.Helper()
req, _ := http.NewRequest("POST", gw.URL+"/mcp/exa", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
for i := 0; i+1 < len(hdr); i += 2 {
req.Header.Set(hdr[i], hdr[i+1])
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
b, _ := io.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatalf("%s: %d %s", body, resp.StatusCode, b)
}
}
call(`{"jsonrpc":"2.0","id":1,"method":"initialize"}`, "X-Api-Key", "exa-key", "X-Exa-Source", "magpie")
if f.SeenQuery != "login" {
t.Fatalf("the server got the query %q, not login", f.SeenQuery)
}
if f.Seen.Get("X-Api-Key") != "exa-key" || f.Seen.Get("X-Exa-Source") != "magpie" {
t.Fatalf("headers not passed on: %v", f.Seen)
}
token, _ := mcpauth.Token(t.Context(), "exa")
if f.Seen.Get("Authorization") != "Bearer "+token {
t.Fatalf("Authorization %q", f.Seen.Get("Authorization"))
}
// the gateway's own key isn't the server's
call(`{"jsonrpc":"2.0","id":2,"method":"tools/list"}`, "X-Api-Key", Token)
if f.Seen.Get("X-Api-Key") != "" {
t.Fatalf("the server got the gateway's key %q", f.Seen.Get("X-Api-Key"))
}
}