|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// TestRewriteMCPConfigForGateway_PreservesNonProxiedServers tests that |
| 11 | +// servers not being proxied (like safeinputs/safeoutputs) are preserved unchanged |
| 12 | +func TestRewriteMCPConfigForGateway_PreservesNonProxiedServers(t *testing.T) { |
| 13 | + // Create a temporary config file |
| 14 | + tmpDir := t.TempDir() |
| 15 | + configFile := filepath.Join(tmpDir, "test-config.json") |
| 16 | + |
| 17 | + // Initial config with both proxied and non-proxied servers |
| 18 | + initialConfig := map[string]any{ |
| 19 | + "mcpServers": map[string]any{ |
| 20 | + "safeinputs": map[string]any{ |
| 21 | + "command": "gh", |
| 22 | + "args": []string{"aw", "mcp-server", "--mode", "safe-inputs"}, |
| 23 | + }, |
| 24 | + "safeoutputs": map[string]any{ |
| 25 | + "command": "gh", |
| 26 | + "args": []string{"aw", "mcp-server", "--mode", "safe-outputs"}, |
| 27 | + }, |
| 28 | + "github": map[string]any{ |
| 29 | + "command": "docker", |
| 30 | + "args": []string{"run", "-i", "--rm", "ghcr.io/github-mcp-server"}, |
| 31 | + }, |
| 32 | + }, |
| 33 | + "gateway": map[string]any{ |
| 34 | + "port": 8080, |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + initialJSON, _ := json.Marshal(initialConfig) |
| 39 | + if err := os.WriteFile(configFile, initialJSON, 0644); err != nil { |
| 40 | + t.Fatalf("Failed to write config file: %v", err) |
| 41 | + } |
| 42 | + |
| 43 | + // Gateway config only includes external server (github), not internal servers |
| 44 | + gatewayConfig := &MCPGatewayConfig{ |
| 45 | + MCPServers: map[string]MCPServerConfig{ |
| 46 | + "github": { |
| 47 | + Command: "docker", |
| 48 | + Args: []string{"run", "-i", "--rm", "ghcr.io/github-mcp-server"}, |
| 49 | + }, |
| 50 | + }, |
| 51 | + Gateway: GatewaySettings{ |
| 52 | + Port: 8080, |
| 53 | + }, |
| 54 | + } |
| 55 | + |
| 56 | + // Rewrite the config |
| 57 | + if err := rewriteMCPConfigForGateway(configFile, gatewayConfig); err != nil { |
| 58 | + t.Fatalf("rewriteMCPConfigForGateway failed: %v", err) |
| 59 | + } |
| 60 | + |
| 61 | + // Read back the rewritten config |
| 62 | + rewrittenData, err := os.ReadFile(configFile) |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("Failed to read rewritten config: %v", err) |
| 65 | + } |
| 66 | + |
| 67 | + var rewrittenConfig map[string]any |
| 68 | + if err := json.Unmarshal(rewrittenData, &rewrittenConfig); err != nil { |
| 69 | + t.Fatalf("Failed to parse rewritten config: %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + // Verify structure |
| 73 | + mcpServers, ok := rewrittenConfig["mcpServers"].(map[string]any) |
| 74 | + if !ok { |
| 75 | + t.Fatal("mcpServers not found or wrong type") |
| 76 | + } |
| 77 | + |
| 78 | + // Should have all 3 servers: 2 preserved + 1 rewritten |
| 79 | + if len(mcpServers) != 3 { |
| 80 | + t.Errorf("Expected 3 servers in rewritten config, got %d", len(mcpServers)) |
| 81 | + } |
| 82 | + |
| 83 | + // Verify safeinputs is preserved with original command/args |
| 84 | + safeinputs, ok := mcpServers["safeinputs"].(map[string]any) |
| 85 | + if !ok { |
| 86 | + t.Fatal("safeinputs server not found") |
| 87 | + } |
| 88 | + |
| 89 | + safeinputsCommand, ok := safeinputs["command"].(string) |
| 90 | + if !ok || safeinputsCommand != "gh" { |
| 91 | + t.Errorf("Expected safeinputs to preserve original command 'gh', got '%v'", safeinputsCommand) |
| 92 | + } |
| 93 | + |
| 94 | + safeinputsArgs, ok := safeinputs["args"].([]any) |
| 95 | + if !ok { |
| 96 | + t.Error("Expected safeinputs to have args array") |
| 97 | + } else if len(safeinputsArgs) < 3 { |
| 98 | + t.Errorf("Expected safeinputs to have at least 3 args, got %d", len(safeinputsArgs)) |
| 99 | + } |
| 100 | + |
| 101 | + // Verify safeoutputs is preserved with original command/args |
| 102 | + safeoutputs, ok := mcpServers["safeoutputs"].(map[string]any) |
| 103 | + if !ok { |
| 104 | + t.Fatal("safeoutputs server not found") |
| 105 | + } |
| 106 | + |
| 107 | + safeoutputsCommand, ok := safeoutputs["command"].(string) |
| 108 | + if !ok || safeoutputsCommand != "gh" { |
| 109 | + t.Errorf("Expected safeoutputs to preserve original command 'gh', got '%v'", safeoutputsCommand) |
| 110 | + } |
| 111 | + |
| 112 | + safeoutputsArgs, ok := safeoutputs["args"].([]any) |
| 113 | + if !ok { |
| 114 | + t.Error("Expected safeoutputs to have args array") |
| 115 | + } else if len(safeoutputsArgs) < 3 { |
| 116 | + t.Errorf("Expected safeoutputs to have at least 3 args, got %d", len(safeoutputsArgs)) |
| 117 | + } |
| 118 | + |
| 119 | + // Verify github server points to gateway (was rewritten) |
| 120 | + github, ok := mcpServers["github"].(map[string]any) |
| 121 | + if !ok { |
| 122 | + t.Fatal("github server not found") |
| 123 | + } |
| 124 | + |
| 125 | + githubURL, ok := github["url"].(string) |
| 126 | + if !ok { |
| 127 | + t.Fatal("github server should have url (rewritten)") |
| 128 | + } |
| 129 | + |
| 130 | + expectedURL := "http://localhost:8080/mcp/github" |
| 131 | + if githubURL != expectedURL { |
| 132 | + t.Errorf("Expected github URL %s, got %s", expectedURL, githubURL) |
| 133 | + } |
| 134 | + |
| 135 | + // Verify github server does NOT have command/args (was rewritten) |
| 136 | + if _, hasCommand := github["command"]; hasCommand { |
| 137 | + t.Error("Rewritten github server should not have 'command' field") |
| 138 | + } |
| 139 | + |
| 140 | + // Verify gateway settings are NOT included in rewritten config |
| 141 | + _, hasGateway := rewrittenConfig["gateway"] |
| 142 | + if hasGateway { |
| 143 | + t.Error("Gateway section should not be included in rewritten config") |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +// TestRewriteMCPConfigForGateway_NoGatewaySection tests that gateway section is removed |
| 148 | +func TestRewriteMCPConfigForGateway_NoGatewaySection(t *testing.T) { |
| 149 | + // Create a temporary config file |
| 150 | + tmpDir := t.TempDir() |
| 151 | + configFile := filepath.Join(tmpDir, "test-config.json") |
| 152 | + |
| 153 | + initialConfig := map[string]any{ |
| 154 | + "mcpServers": map[string]any{ |
| 155 | + "github": map[string]any{ |
| 156 | + "command": "gh", |
| 157 | + "args": []string{"aw", "mcp-server"}, |
| 158 | + }, |
| 159 | + }, |
| 160 | + "gateway": map[string]any{ |
| 161 | + "port": 8080, |
| 162 | + "apiKey": "test-key", |
| 163 | + }, |
| 164 | + } |
| 165 | + |
| 166 | + initialJSON, _ := json.Marshal(initialConfig) |
| 167 | + if err := os.WriteFile(configFile, initialJSON, 0644); err != nil { |
| 168 | + t.Fatalf("Failed to write config file: %v", err) |
| 169 | + } |
| 170 | + |
| 171 | + gatewayConfig := &MCPGatewayConfig{ |
| 172 | + MCPServers: map[string]MCPServerConfig{ |
| 173 | + "github": { |
| 174 | + Command: "gh", |
| 175 | + Args: []string{"aw", "mcp-server"}, |
| 176 | + }, |
| 177 | + }, |
| 178 | + Gateway: GatewaySettings{ |
| 179 | + Port: 8080, |
| 180 | + APIKey: "test-key", |
| 181 | + }, |
| 182 | + } |
| 183 | + |
| 184 | + // Rewrite the config |
| 185 | + if err := rewriteMCPConfigForGateway(configFile, gatewayConfig); err != nil { |
| 186 | + t.Fatalf("rewriteMCPConfigForGateway failed: %v", err) |
| 187 | + } |
| 188 | + |
| 189 | + // Read back the rewritten config |
| 190 | + rewrittenData, err := os.ReadFile(configFile) |
| 191 | + if err != nil { |
| 192 | + t.Fatalf("Failed to read rewritten config: %v", err) |
| 193 | + } |
| 194 | + |
| 195 | + var rewrittenConfig map[string]any |
| 196 | + if err := json.Unmarshal(rewrittenData, &rewrittenConfig); err != nil { |
| 197 | + t.Fatalf("Failed to parse rewritten config: %v", err) |
| 198 | + } |
| 199 | + |
| 200 | + // Verify gateway settings are NOT included in rewritten config |
| 201 | + _, hasGateway := rewrittenConfig["gateway"] |
| 202 | + if hasGateway { |
| 203 | + t.Error("Gateway section should not be included in rewritten config") |
| 204 | + } |
| 205 | + |
| 206 | + // Verify mcpServers still exists |
| 207 | + _, hasMCPServers := rewrittenConfig["mcpServers"] |
| 208 | + if !hasMCPServers { |
| 209 | + t.Error("mcpServers section should be present in rewritten config") |
| 210 | + } |
| 211 | +} |
0 commit comments