package http import ( "context" "net/http" "net/http/cookiejar" "net/http/httptest" "strings" "testing" mcpsdk "github.com/modelcontextprotocol/go-sdk/mcp" ) // fakeMCPServer serves a one-tool MCP server over streamable HTTP. When // wantAuth is non-empty, requests must carry it as a Bearer credential. func fakeMCPServer(t *testing.T, wantAuth string) *httptest.Server { t.Helper() srv := mcpsdk.NewServer(&mcpsdk.Implementation{Name: "fake", Version: "1"}, nil) mcpsdk.AddTool(srv, &mcpsdk.Tool{Name: "echo", Description: "Echoes its input"}, func(ctx context.Context, req *mcpsdk.CallToolRequest, in map[string]any) (*mcpsdk.CallToolResult, map[string]any, error) { return &mcpsdk.CallToolResult{}, map[string]any{"ok": true}, nil }) handler := mcpsdk.NewStreamableHTTPHandler(func(*http.Request) *mcpsdk.Server { return srv }, nil) ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if wantAuth != "" && r.Header.Get("Authorization") != "Bearer "+wantAuth { http.Error(w, "unauthorized", http.StatusUnauthorized) return } handler.ServeHTTP(w, r) })) t.Cleanup(ts.Close) return ts } func TestMcpConnections(t *testing.T) { srv := testServer(t) fake := fakeMCPServer(t, "") secured := fakeMCPServer(t, "s3cret-token") newClient := func() apiClient { jar, _ := cookiejar.New(nil); return apiClient{t, &http.Client{Jar: jar}, srv.URL} } alice := newClient() alice.request("POST", "/api/signup", map[string]any{"email": "alice@superbrain.test", "password": "a-long-test-password"}, 201) alice.request("POST", "/api/auth/login", map[string]any{"email": "alice@superbrain.test", "password": "a-long-test-password"}, 200) // Validation. alice.request("POST", "/api/mcp/connections", map[string]any{"name": "x", "url": "ftp://nope/x", "transport": "streamable-http", "authKind": "none"}, 400) alice.request("POST", "/api/mcp/connections", map[string]any{"name": "x", "url": fake.URL, "transport": "carrier-pigeon", "authKind": "none"}, 400) alice.request("POST", "/api/mcp/connections", map[string]any{"name": "x", "url": fake.URL, "transport": "streamable-http", "authKind": "bearer"}, 400) // secret required alice.request("POST", "/api/mcp/connections", map[string]any{"name": "x", "url": fake.URL, "transport": "streamable-http", "authKind": "header"}, 400) // headerName + secret // Create + masked projection + live tool listing. created := alice.request("POST", "/api/mcp/connections", map[string]any{ "name": "Local fake", "url": fake.URL, "transport": "streamable-http", "authKind": "none", }, 201) id, _ := created["id"].(string) if created["secret"] != "" { t.Fatal("secret echoed on a none-auth connection") } list := alice.request("GET", "/api/mcp/connections", nil, 200)["connections"].([]any) if len(list) != 1 { t.Fatalf("expected 1 connection, got %d", len(list)) } test := alice.request("POST", "/api/mcp/connections/"+id+"/test", map[string]any{}, 200) if test["ok"] != true || test["count"] != float64(1) { t.Fatalf("unexpected test result: %v", test) } tools := test["tools"].([]any) if tools[0].(map[string]any)["name"] != "echo" { t.Fatalf("unexpected tool list: %v", tools) } // Bearer credential: stored encrypted, masked in responses, sent on dial. sec := alice.request("POST", "/api/mcp/connections", map[string]any{ "name": "Secured", "url": secured.URL, "transport": "streamable-http", "authKind": "bearer", "secret": "s3cret-token", }, 201) secID, _ := sec["id"].(string) if sec["secret"] != "••••••••" { t.Fatalf("secret not masked: %v", sec["secret"]) } if r := alice.request("POST", "/api/mcp/connections/"+secID+"/test", map[string]any{}, 200); r["ok"] != true { t.Fatalf("bearer connection failed against the secured fake: %v", r) } // Version contract + write-only secret. alice.request("PATCH", "/api/mcp/connections/"+secID, map[string]any{"name": "Renamed", "version": 99}, 409) patched := alice.request("PATCH", "/api/mcp/connections/"+secID, map[string]any{"name": "Renamed", "version": 1}, 200) if patched["version"] != float64(2) || patched["name"] != "Renamed" { t.Fatalf("unexpected patch result: %v", patched) } // Mask keeps the stored secret: the secured fake still accepts the dial. // Mask-only PATCH is a no-op: 200, current projection, version NOT bumped. noop := alice.request("PATCH", "/api/mcp/connections/"+secID, map[string]any{"secret": "••••••••", "version": 2}, 200) if noop["version"] != float64(2) { t.Fatalf("mask-only PATCH bumped the version: %v", noop) } if r := alice.request("POST", "/api/mcp/connections/"+secID+"/test", map[string]any{}, 200); r["ok"] != true { t.Fatalf("mask echo lost the stored secret: %v", r) } // Switching to a wrong secret surfaces as a dial failure, not a silent ok. alice.request("PATCH", "/api/mcp/connections/"+secID, map[string]any{"secret": "wrong", "version": 2}, 200) alice.request("POST", "/api/mcp/connections/"+secID+"/test", map[string]any{}, 502) // authKind none clears the secret. alice.request("PATCH", "/api/mcp/connections/"+secID, map[string]any{"authKind": "none", "version": 3}, 200) // Unreachable server: 502 with an error class, never a silent ok. dead := alice.request("POST", "/api/mcp/connections", map[string]any{ "name": "Dead", "url": "https://localhost:1/mcp", "transport": "streamable-http", "authKind": "none", }, 201) deadID, _ := dead["id"].(string) if r := alice.request("POST", "/api/mcp/connections/"+deadID+"/test", map[string]any{}, 502); r["ok"] == true { t.Fatal("dead server reported ok") } // Owner scoping + deletion. bob := newClient() bob.request("POST", "/api/signup", map[string]any{"email": "bob@superbrain.test", "password": "a-long-test-password"}, 201) bob.request("POST", "/api/auth/login", map[string]any{"email": "bob@superbrain.test", "password": "a-long-test-password"}, 200) bob.request("PATCH", "/api/mcp/connections/"+id, map[string]any{"name": "stolen", "version": 1}, 404) bob.request("DELETE", "/api/mcp/connections/"+id, nil, 404) alice.request("DELETE", "/api/mcp/connections/"+id, nil, 200) alice.request("POST", "/api/mcp/connections/"+id+"/test", map[string]any{}, 404) // Cap (clear the survivors first so the count starts at zero). alice.request("DELETE", "/api/mcp/connections/"+secID, nil, 200) alice.request("DELETE", "/api/mcp/connections/"+deadID, nil, 200) for i := 0; i < maxTestMcpConns; i++ { alice.request("POST", "/api/mcp/connections", map[string]any{ "name": "cap", "url": fake.URL, "transport": "sse", "authKind": "none", }, 201) } alice.request("POST", "/api/mcp/connections", map[string]any{ "name": "one too many", "url": fake.URL, "transport": "sse", "authKind": "none", }, 400) } // maxTestMcpConns mirrors handlers.maxMcpConns. const maxTestMcpConns = 10 // The suggestion catalog ships with the list: GitHub and Make are // token-connectable (bearer), HubSpot is honestly flagged unavailable // because its remote MCP requires OAuth 2.1 + PKCE, which the client // (none/bearer/header) cannot do. func TestMcpSuggestions(t *testing.T) { srv := testServer(t) c, _ := accountClient(t, srv.URL, "mcp-sugg@superbrain.test") list := c.request("GET", "/api/mcp/connections", nil, 200) sugg, _ := list["suggestions"].([]any) byKind := map[string]map[string]any{} for _, s := range sugg { m := s.(map[string]any) byKind[m["kind"].(string)] = m } if len(byKind) != 3 { t.Fatalf("want github/hubspot/make suggestions, got %v", byKind) } gh := byKind["github"] if gh["url"] != "https://api.githubcopilot.com/mcp/" || gh["authKind"] != "bearer" || gh["available"] != true { t.Fatalf("github suggestion wrong: %v", gh) } mk := byKind["make"] if mk["authKind"] != "bearer" || mk["available"] != true || !strings.Contains(mk["url"].(string), "make.com/mcp/v2") { t.Fatalf("make suggestion wrong: %v", mk) } hs := byKind["hubspot"] if hs["available"] != false || hs["url"] != "https://mcp.hubspot.com" { t.Fatalf("hubspot must be flagged OAuth-only: %v", hs) } }