| 1 | package plugin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "sync" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | // TestHostAddRemove exercises the hot add/remove path behind `/mcp add` and |
| 11 | // `/mcp remove`: a server connects live into an existing host, its namespaced |
| 12 | // tools surface, a duplicate name is rejected, and removal disconnects it and |
| 13 | // reports the tool prefix to unregister. |
| 14 | func TestHostAddRemove(t *testing.T) { |
| 15 | srv := mcpHTTPServer(t, false) |
| 16 | defer srv.Close() |
| 17 | |
| 18 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 19 | defer cancel() |
| 20 | |
| 21 | h := NewHost() |
| 22 | defer h.Close() |
| 23 | |
| 24 | spec := Spec{Name: "h", Type: "http", URL: srv.URL, Headers: map[string]string{"Authorization": "Bearer secret"}} |
| 25 | tools, err := h.Add(ctx, spec) |
| 26 | if err != nil { |
| 27 | t.Fatalf("Add: %v", err) |
| 28 | } |
| 29 | if len(tools) != 1 || tools[0].Name() != "mcp__h__greet" { |
| 30 | t.Fatalf("tools = %v, want [mcp__h__greet]", names(tools)) |
| 31 | } |
| 32 | if got := h.Servers(); len(got) != 1 || got[0].Name != "h" || got[0].Tools != 1 { |
| 33 | t.Fatalf("Servers() = %+v, want one server 'h' with 1 tool", got) |
| 34 | } |
| 35 | |
| 36 | // A second add under the same name is rejected (no duplicate connection). |
| 37 | if _, err := h.Add(ctx, spec); err == nil { |
| 38 | t.Error("Add of an already-connected name should error") |
| 39 | } |
| 40 | |
| 41 | prefix, found := h.Remove("h") |
| 42 | if !found || prefix != "mcp__h__" { |
| 43 | t.Fatalf("Remove = (%q, %v), want (\"mcp__h__\", true)", prefix, found) |
| 44 | } |
| 45 | if len(h.Servers()) != 0 { |
| 46 | t.Errorf("server should be gone after Remove, got %+v", h.Servers()) |
| 47 | } |
| 48 | if _, found := h.Remove("h"); found { |
| 49 | t.Error("removing an absent server should report not found") |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestHostAddConnectedRejectsLateDuplicate(t *testing.T) { |
| 54 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 55 | defer cancel() |
| 56 | |
| 57 | h := NewHost() |
| 58 | defer h.Close() |
| 59 | |
| 60 | spec := helperSpec() |
| 61 | if _, err := h.addConnected(ctx, spec); err != nil { |
| 62 | t.Fatalf("first addConnected: %v", err) |
| 63 | } |
| 64 | if _, err := h.addConnected(ctx, spec); !IsServerAlreadyConnected(err) { |
| 65 | t.Fatalf("second addConnected error = %v, want ErrServerAlreadyConnected", err) |
| 66 | } |
| 67 | if got := h.ServerNames(); len(got) != 1 || got[0] != spec.Name { |
| 68 | t.Fatalf("ServerNames() = %v, want exactly one %q", got, spec.Name) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestHostAddConcurrentSameServerReusesSingleClient(t *testing.T) { |
| 73 | ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) |
| 74 | defer cancel() |
| 75 | |
| 76 | h := NewHost() |
| 77 | defer h.Close() |
| 78 | |
| 79 | spec := helperSpec() |
| 80 | spec.Env["GO_WANT_HELPER_INIT_MS"] = "100" |
| 81 | |
| 82 | const callers = 5 |
| 83 | var wg sync.WaitGroup |
| 84 | errs := make([]error, callers) |
| 85 | counts := make([]int, callers) |
| 86 | wg.Add(callers) |
| 87 | for i := 0; i < callers; i++ { |
| 88 | go func(i int) { |
| 89 | defer wg.Done() |
| 90 | tools, err := h.Add(ctx, spec) |
| 91 | errs[i] = err |
| 92 | counts[i] = len(tools) |
| 93 | }(i) |
| 94 | } |
| 95 | wg.Wait() |
| 96 | |
| 97 | for i, err := range errs { |
| 98 | if err != nil { |
| 99 | t.Fatalf("caller %d Add: %v", i, err) |
| 100 | } |
| 101 | if counts[i] != 2 { |
| 102 | t.Fatalf("caller %d got %d tools, want 2", i, counts[i]) |
| 103 | } |
| 104 | } |
| 105 | if got := h.ServerNames(); len(got) != 1 || got[0] != spec.Name { |
| 106 | t.Fatalf("ServerNames() = %v, want exactly one %q", got, spec.Name) |
| 107 | } |
| 108 | } |
| 109 |