|
1 | 1 | package agent |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
4 | 6 | "os" |
5 | 7 | "path/filepath" |
6 | 8 |
|
@@ -34,14 +36,7 @@ func (a *GeminiAgent) Exists() bool { |
34 | 36 | } |
35 | 37 |
|
36 | 38 | func (a *GeminiAgent) HasPlaywright() (bool, error) { |
37 | | - cfg, err := config.ReadConfig(a.configPath) |
38 | | - if err != nil { |
39 | | - if os.IsNotExist(err) { |
40 | | - return false, nil |
41 | | - } |
42 | | - return false, err |
43 | | - } |
44 | | - return config.HasPlaywrightMCP(cfg), nil |
| 39 | + return a.HasMCP("playwright") |
45 | 40 | } |
46 | 41 |
|
47 | 42 | func (a *GeminiAgent) InstallPlaywright() error { |
@@ -74,14 +69,7 @@ func (a *GeminiAgent) RemovePlaywright() error { |
74 | 69 | } |
75 | 70 |
|
76 | 71 | func (a *GeminiAgent) HasContext7() (bool, error) { |
77 | | - cfg, err := config.ReadConfig(a.configPath) |
78 | | - if err != nil { |
79 | | - if os.IsNotExist(err) { |
80 | | - return false, nil |
81 | | - } |
82 | | - return false, err |
83 | | - } |
84 | | - return config.HasContext7MCP(cfg), nil |
| 72 | + return a.HasMCP("context7") |
85 | 73 | } |
86 | 74 |
|
87 | 75 | func (a *GeminiAgent) InstallContext7() error { |
@@ -113,14 +101,7 @@ func (a *GeminiAgent) RemoveContext7() error { |
113 | 101 | } |
114 | 102 |
|
115 | 103 | func (a *GeminiAgent) HasRemixIcon() (bool, error) { |
116 | | - cfg, err := config.ReadConfig(a.configPath) |
117 | | - if err != nil { |
118 | | - if os.IsNotExist(err) { |
119 | | - return false, nil |
120 | | - } |
121 | | - return false, err |
122 | | - } |
123 | | - return config.HasRemixIconMCP(cfg), nil |
| 104 | + return a.HasMCP("remix-icon") |
124 | 105 | } |
125 | 106 |
|
126 | 107 | func (a *GeminiAgent) InstallRemixIcon() error { |
@@ -151,6 +132,160 @@ func (a *GeminiAgent) RemoveRemixIcon() error { |
151 | 132 | return config.WriteConfig(a.configPath, cfg) |
152 | 133 | } |
153 | 134 |
|
| 135 | +func (a *GeminiAgent) HasMCP(name string) (bool, error) { |
| 136 | + cfg, err := config.ReadConfig(a.configPath) |
| 137 | + if err != nil { |
| 138 | + if os.IsNotExist(err) { |
| 139 | + cfg = map[string]interface{}{} |
| 140 | + } else { |
| 141 | + return false, err |
| 142 | + } |
| 143 | + } |
| 144 | + if config.HasMCP(cfg, name) { |
| 145 | + return true, nil |
| 146 | + } |
| 147 | + extensions, err := a.listExtensionMCPs() |
| 148 | + if err != nil { |
| 149 | + return false, err |
| 150 | + } |
| 151 | + _, ok := extensions[name] |
| 152 | + return ok, nil |
| 153 | +} |
| 154 | + |
| 155 | +func (a *GeminiAgent) InstallMCP(name string, mcpConfig map[string]interface{}) error { |
| 156 | + if mcpConfig == nil { |
| 157 | + return fmt.Errorf("missing mcp config") |
| 158 | + } |
| 159 | + cfg, err := config.ReadConfig(a.configPath) |
| 160 | + if err != nil { |
| 161 | + if os.IsNotExist(err) { |
| 162 | + cfg = make(map[string]interface{}) |
| 163 | + if err := os.MkdirAll(filepath.Dir(a.configPath), 0755); err != nil { |
| 164 | + return err |
| 165 | + } |
| 166 | + } else { |
| 167 | + return err |
| 168 | + } |
| 169 | + } |
| 170 | + config.AddMCP(cfg, name, cloneMCPConfig(mcpConfig)) |
| 171 | + return config.WriteConfig(a.configPath, cfg) |
| 172 | +} |
| 173 | + |
| 174 | +func (a *GeminiAgent) RemoveMCP(name string) error { |
| 175 | + cfg, err := config.ReadConfig(a.configPath) |
| 176 | + if err != nil { |
| 177 | + if os.IsNotExist(err) { |
| 178 | + cfg = map[string]interface{}{} |
| 179 | + } else { |
| 180 | + return err |
| 181 | + } |
| 182 | + } |
| 183 | + if !config.HasMCP(cfg, name) { |
| 184 | + extensions, extErr := a.listExtensionMCPs() |
| 185 | + if extErr == nil { |
| 186 | + if _, ok := extensions[name]; ok { |
| 187 | + return fmt.Errorf("mcp %s is managed by a Gemini extension", name) |
| 188 | + } |
| 189 | + } |
| 190 | + return nil |
| 191 | + } |
| 192 | + config.RemoveMCP(cfg, name) |
| 193 | + return config.WriteConfig(a.configPath, cfg) |
| 194 | +} |
| 195 | + |
| 196 | +func (a *GeminiAgent) ListMCPs() (map[string]map[string]interface{}, error) { |
| 197 | + cfg, err := config.ReadConfig(a.configPath) |
| 198 | + if err != nil { |
| 199 | + if os.IsNotExist(err) { |
| 200 | + cfg = map[string]interface{}{} |
| 201 | + } else { |
| 202 | + return nil, err |
| 203 | + } |
| 204 | + } |
| 205 | + result := config.GetMCPServers(cfg) |
| 206 | + extensions, err := a.listExtensionMCPs() |
| 207 | + if err != nil { |
| 208 | + return result, nil |
| 209 | + } |
| 210 | + for name, cfg := range extensions { |
| 211 | + if _, exists := result[name]; exists { |
| 212 | + continue |
| 213 | + } |
| 214 | + result[name] = cfg |
| 215 | + } |
| 216 | + return result, nil |
| 217 | +} |
| 218 | + |
| 219 | +func (a *GeminiAgent) listExtensionMCPs() (map[string]map[string]interface{}, error) { |
| 220 | + extensionsDir := filepath.Join(filepath.Dir(a.configPath), "extensions") |
| 221 | + entries, err := os.ReadDir(extensionsDir) |
| 222 | + if err != nil { |
| 223 | + if os.IsNotExist(err) { |
| 224 | + return map[string]map[string]interface{}{}, nil |
| 225 | + } |
| 226 | + return nil, err |
| 227 | + } |
| 228 | + |
| 229 | + enabled, _ := readGeminiExtensionEnablement(filepath.Join(extensionsDir, "extension-enablement.json")) |
| 230 | + result := make(map[string]map[string]interface{}) |
| 231 | + for _, entry := range entries { |
| 232 | + if !entry.IsDir() { |
| 233 | + continue |
| 234 | + } |
| 235 | + name := entry.Name() |
| 236 | + if enabled != nil { |
| 237 | + if _, ok := enabled[name]; !ok { |
| 238 | + continue |
| 239 | + } |
| 240 | + } |
| 241 | + configPath := filepath.Join(extensionsDir, name, "gemini-extension.json") |
| 242 | + extCfg, err := readGeminiExtensionConfig(configPath) |
| 243 | + if err != nil { |
| 244 | + continue |
| 245 | + } |
| 246 | + for serverName, serverCfg := range extCfg { |
| 247 | + if serverCfg == nil { |
| 248 | + continue |
| 249 | + } |
| 250 | + result[serverName] = serverCfg |
| 251 | + } |
| 252 | + } |
| 253 | + return result, nil |
| 254 | +} |
| 255 | + |
| 256 | +func readGeminiExtensionEnablement(path string) (map[string]struct{}, error) { |
| 257 | + data, err := os.ReadFile(path) |
| 258 | + if err != nil { |
| 259 | + return nil, err |
| 260 | + } |
| 261 | + var raw map[string]interface{} |
| 262 | + if err := json.Unmarshal(data, &raw); err != nil { |
| 263 | + return nil, err |
| 264 | + } |
| 265 | + result := make(map[string]struct{}, len(raw)) |
| 266 | + for key := range raw { |
| 267 | + result[key] = struct{}{} |
| 268 | + } |
| 269 | + return result, nil |
| 270 | +} |
| 271 | + |
| 272 | +func readGeminiExtensionConfig(path string) (map[string]map[string]interface{}, error) { |
| 273 | + data, err := os.ReadFile(path) |
| 274 | + if err != nil { |
| 275 | + return nil, err |
| 276 | + } |
| 277 | + var payload struct { |
| 278 | + MCPServers map[string]map[string]interface{} `json:"mcpServers"` |
| 279 | + } |
| 280 | + if err := json.Unmarshal(data, &payload); err != nil { |
| 281 | + return nil, err |
| 282 | + } |
| 283 | + if payload.MCPServers == nil { |
| 284 | + return map[string]map[string]interface{}{}, nil |
| 285 | + } |
| 286 | + return payload.MCPServers, nil |
| 287 | +} |
| 288 | + |
154 | 289 | func (a *GeminiAgent) SupportsSkills() bool { |
155 | 290 | return false |
156 | 291 | } |
|
0 commit comments