Trait for formatting MCP configuration for different agents
| 157 | |
| 158 | /// Trait for formatting MCP configuration for different agents |
| 159 | pub trait McpFormatter: Send + Sync { |
| 160 | /// Format MCP servers into an agent-specific logical `Value` representation. |
| 161 | /// |
| 162 | /// This is used as a common in-memory shape across formatters and tests. |
| 163 | /// On-disk serialization should use `format_to_string()`. |
| 164 | fn format(&self, servers: &BTreeMap<&str, &McpServerConfig>) -> Value; |
| 165 | |
| 166 | /// Format MCP servers into the agent-specific file content. |
| 167 | /// By default this returns pretty JSON for agents that use JSON configs. |
| 168 | fn format_to_string(&self, servers: &BTreeMap<&str, &McpServerConfig>) -> Result<String> { |
| 169 | let output = self.format(servers); |
| 170 | serde_json::to_string_pretty(&output).context("Failed to serialize MCP config") |
| 171 | } |
| 172 | |
| 173 | /// Parse existing configuration file to extract mcpServers |
| 174 | fn parse_existing(&self, content: &str) -> Result<BTreeMap<String, Value>>; |
| 175 | |
| 176 | /// Merge new servers with existing configuration |
| 177 | fn merge( |
| 178 | &self, |
| 179 | existing_content: &str, |
| 180 | new_servers: &BTreeMap<&str, &McpServerConfig>, |
| 181 | ) -> Result<String>; |
| 182 | |
| 183 | /// Remove servers that are no longer in the configuration |
| 184 | /// Default implementation just calls merge, but specific formatters can override |
| 185 | fn cleanup_removed_servers( |
| 186 | &self, |
| 187 | existing_content: &str, |
| 188 | new_servers: &BTreeMap<&str, &McpServerConfig>, |
| 189 | ) -> Result<String> { |
| 190 | self.merge(existing_content, new_servers) |
| 191 | } |
| 192 | |
| 193 | /// Whether this formatter wraps mcpServers in another key |
| 194 | fn wrapper_key(&self) -> Option<&'static str> { |
| 195 | None |
| 196 | } |
| 197 | |
| 198 | /// Whether the formatter should preserve unrelated top-level settings when |
| 199 | /// running in Overwrite mode. Some agents (Gemini, OpenCode) keep other |
| 200 | /// settings in their config files and we shouldn't clobber them. |
| 201 | fn preserve_on_overwrite(&self) -> bool { |
| 202 | false |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // ============================================================================= |
| 207 | // Standard MCP Helper Functions |
nothing calls this directly
no outgoing calls
no test coverage detected