|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/BackendStack21/kode/internal/mcpclient" |
| 10 | +) |
| 11 | + |
| 12 | +// ── E2E: MCP Client with a real MCP server subprocess ────────────────── |
| 13 | +// |
| 14 | +// These tests start a real MCP server subprocess, discover tools, call |
| 15 | +// them, and verify the full ToolAdapter round-trip. Gated by KODE_E2E=true |
| 16 | +// since they spawn external processes. |
| 17 | + |
| 18 | +// fakeMCPPath returns the path to the pre-compiled fake MCP server binary. |
| 19 | +// The test runs inside cmd/kode/, so we go up two levels to reach the repo root. |
| 20 | +func fakeMCPPath(t *testing.T) string { |
| 21 | + t.Helper() |
| 22 | + return filepath.Join("..", "..", "internal", "mcpclient", "testdata", "fakeserver") |
| 23 | +} |
| 24 | + |
| 25 | +func skipIfNoMCPE2E(t *testing.T) { |
| 26 | + t.Helper() |
| 27 | + if os.Getenv("KODE_E2E") == "" { |
| 28 | + t.Skip("KODE_E2E not set — skipping MCP E2E test") |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestMCPClientE2E_DiscoverTools(t *testing.T) { |
| 33 | + skipIfNoMCPE2E(t) |
| 34 | + |
| 35 | + client, err := mcpclient.New("e2e-test", mcpclient.ServerConfig{ |
| 36 | + Command: fakeMCPPath(t), |
| 37 | + Env: map[string]string{ |
| 38 | + "FAKE_TOOLS": `[{"name":"fetch","description":"Fetch a URL"},{"name":"search","description":"Search the web"}]`, |
| 39 | + }, |
| 40 | + }) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("New: %v", err) |
| 43 | + } |
| 44 | + defer client.Close() |
| 45 | + |
| 46 | + tools, err := client.Discover(context.Background()) |
| 47 | + if err != nil { |
| 48 | + t.Fatalf("Discover: %v", err) |
| 49 | + } |
| 50 | + if len(tools) != 2 { |
| 51 | + t.Fatalf("expected 2 tools, got %d", len(tools)) |
| 52 | + } |
| 53 | + if tools[0].Name != "fetch" { |
| 54 | + t.Errorf("tool[0].Name = %q, want %q", tools[0].Name, "fetch") |
| 55 | + } |
| 56 | + if tools[1].Name != "search" { |
| 57 | + t.Errorf("tool[1].Name = %q, want %q", tools[1].Name, "search") |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestMCPClientE2E_CallTool(t *testing.T) { |
| 62 | + skipIfNoMCPE2E(t) |
| 63 | + |
| 64 | + client, err := mcpclient.New("e2e-call", mcpclient.ServerConfig{ |
| 65 | + Command: fakeMCPPath(t), |
| 66 | + Env: map[string]string{"FAKE_TOOLS": `[{"name":"echo","description":"Echo"}]`}, |
| 67 | + }) |
| 68 | + if err != nil { |
| 69 | + t.Fatalf("New: %v", err) |
| 70 | + } |
| 71 | + defer client.Close() |
| 72 | + |
| 73 | + if _, err := client.Discover(context.Background()); err != nil { |
| 74 | + t.Fatalf("Discover: %v", err) |
| 75 | + } |
| 76 | + |
| 77 | + result, err := client.CallTool(context.Background(), "echo", `{"text":"hello"}`) |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("CallTool: %v", err) |
| 80 | + } |
| 81 | + if result != "ok" { |
| 82 | + t.Errorf("result = %q, want %q", result, "ok") |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestMCPClientE2E_ToolAdapter(t *testing.T) { |
| 87 | + skipIfNoMCPE2E(t) |
| 88 | + |
| 89 | + client, err := mcpclient.New("e2e-adapter", mcpclient.ServerConfig{ |
| 90 | + Command: fakeMCPPath(t), |
| 91 | + Env: map[string]string{"FAKE_TOOLS": `[{"name":"my_tool","description":"Test tool"}]`}, |
| 92 | + }) |
| 93 | + if err != nil { |
| 94 | + t.Fatalf("New: %v", err) |
| 95 | + } |
| 96 | + defer client.Close() |
| 97 | + |
| 98 | + if _, err := client.Discover(context.Background()); err != nil { |
| 99 | + t.Fatalf("Discover: %v", err) |
| 100 | + } |
| 101 | + |
| 102 | + // Verify ToolAdapter implements kode.Tool-compatible interface |
| 103 | + adapter := &mcpclient.ToolAdapter{ |
| 104 | + Client: client, |
| 105 | + ToolName: "my_tool", |
| 106 | + Desc: "Test tool", |
| 107 | + ParamSchema: map[string]any{ |
| 108 | + "type": "object", |
| 109 | + "properties": map[string]any{ |
| 110 | + "input": map[string]any{"type": "string"}, |
| 111 | + }, |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + if adapter.Name() != "e2e-adapter__my_tool" { |
| 116 | + t.Errorf("Name = %q, want %q", adapter.Name(), "e2e-adapter__my_tool") |
| 117 | + } |
| 118 | + |
| 119 | + // Verify Schema returns proper JSON |
| 120 | + schema, ok := adapter.Schema().(map[string]any) |
| 121 | + if !ok { |
| 122 | + t.Fatal("Schema() did not return map") |
| 123 | + } |
| 124 | + if schema["type"] != "object" { |
| 125 | + t.Errorf("schema type = %v, want 'object'", schema["type"]) |
| 126 | + } |
| 127 | + |
| 128 | + // Verify Call works |
| 129 | + result, err := adapter.Call(`{"input":"world"}`) |
| 130 | + if err != nil { |
| 131 | + t.Fatalf("Call: %v", err) |
| 132 | + } |
| 133 | + if result != "ok" { |
| 134 | + t.Errorf("Call result = %q, want %q", result, "ok") |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func TestMCPClientE2E_LoadMCPToolsIntegration(t *testing.T) { |
| 139 | + skipIfNoMCPE2E(t) |
| 140 | + |
| 141 | + // Test the full loadMCPTools → ToolAdapter flow that main.go uses |
| 142 | + servers := map[string]mcpclient.ServerConfig{ |
| 143 | + "test-server": { |
| 144 | + Command: fakeMCPPath(t), |
| 145 | + Env: map[string]string{ |
| 146 | + "FAKE_TOOLS": `[{"name":"fetch","description":"Fetch tool"},{"name":"query","description":"Query tool"}]`, |
| 147 | + }, |
| 148 | + }, |
| 149 | + } |
| 150 | + |
| 151 | + var tools []interface { |
| 152 | + Name() string |
| 153 | + Description() string |
| 154 | + Schema() any |
| 155 | + Call(args string) (string, error) |
| 156 | + } |
| 157 | + |
| 158 | + // We can't call loadMCPTools directly because it takes []kode.Tool, |
| 159 | + // so we test at the mcpclient level instead |
| 160 | + client, err := mcpclient.New("test-server", servers["test-server"]) |
| 161 | + if err != nil { |
| 162 | + t.Fatalf("New: %v", err) |
| 163 | + } |
| 164 | + defer client.Close() |
| 165 | + |
| 166 | + defs, err := client.Discover(context.Background()) |
| 167 | + if err != nil { |
| 168 | + t.Fatalf("Discover: %v", err) |
| 169 | + } |
| 170 | + |
| 171 | + // Create ToolAdapters (same as loadMCPTools does) |
| 172 | + for _, def := range defs { |
| 173 | + tools = append(tools, &mcpclient.ToolAdapter{ |
| 174 | + Client: client, |
| 175 | + ToolName: def.Name, |
| 176 | + Desc: def.Description, |
| 177 | + ParamSchema: def.InputSchema, |
| 178 | + }) |
| 179 | + } |
| 180 | + |
| 181 | + if len(tools) != 2 { |
| 182 | + t.Fatalf("expected 2 tools, got %d", len(tools)) |
| 183 | + } |
| 184 | + if tools[0].Name() != "test-server__fetch" { |
| 185 | + t.Errorf("tools[0].Name() = %q, want %q", tools[0].Name(), "test-server__fetch") |
| 186 | + } |
| 187 | + if tools[1].Name() != "test-server__query" { |
| 188 | + t.Errorf("tools[1].Name() = %q, want %q", tools[1].Name(), "test-server__query") |
| 189 | + } |
| 190 | + |
| 191 | + // Verify we can call both tools |
| 192 | + result, err := tools[0].Call(`{}`) |
| 193 | + if err != nil { |
| 194 | + t.Fatalf("Call fetch: %v", err) |
| 195 | + } |
| 196 | + if result != "ok" { |
| 197 | + t.Errorf("fetch result = %q, want %q", result, "ok") |
| 198 | + } |
| 199 | + |
| 200 | + result, err = tools[1].Call(`{}`) |
| 201 | + if err != nil { |
| 202 | + t.Fatalf("Call query: %v", err) |
| 203 | + } |
| 204 | + if result != "ok" { |
| 205 | + t.Errorf("query result = %q, want %q", result, "ok") |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +func TestMCPClientE2E_ServerError(t *testing.T) { |
| 210 | + skipIfNoMCPE2E(t) |
| 211 | + |
| 212 | + client, err := mcpclient.New("e2e-error", mcpclient.ServerConfig{ |
| 213 | + Command: fakeMCPPath(t), |
| 214 | + Env: map[string]string{ |
| 215 | + "FAKE_TOOLS": `[{"name":"borked","description":"Broken tool"}]`, |
| 216 | + "FAKE_ERROR_ON_CALL": "internal failure", |
| 217 | + }, |
| 218 | + }) |
| 219 | + if err != nil { |
| 220 | + t.Fatalf("New: %v", err) |
| 221 | + } |
| 222 | + defer client.Close() |
| 223 | + |
| 224 | + if _, err := client.Discover(context.Background()); err != nil { |
| 225 | + t.Fatalf("Discover: %v", err) |
| 226 | + } |
| 227 | + |
| 228 | + _, err = client.CallTool(context.Background(), "borked", `{}`) |
| 229 | + if err == nil { |
| 230 | + t.Fatal("expected error for error-returning tool") |
| 231 | + } |
| 232 | +} |
0 commit comments