| 2267 | } |
| 2268 | |
| 2269 | static int cbm_upsert_json_named_mcp(const char *binary_path, const char *config_path, |
| 2270 | const char *const *object_path, size_t path_len, |
| 2271 | cbm_json_mcp_schema_t schema, const char *entry_name, |
| 2272 | const char *argument) { |
| 2273 | if (!binary_path || !config_path || !object_path || !entry_name || !entry_name[0]) { |
| 2274 | return CLI_ERR; |
| 2275 | } |
| 2276 | char *document = NULL; |
| 2277 | size_t document_length = 0U; |
| 2278 | int read_result = cbm_json_like_read_document(config_path, &document, &document_length); |
| 2279 | if (read_result < 0) { |
| 2280 | return CLI_ERR; |
| 2281 | } |
| 2282 | if (read_result == 0) { |
| 2283 | char *command = NULL; |
| 2284 | int ownership = cbm_json_mcp_snapshot_ownership( |
| 2285 | document, document_length, object_path, path_len, schema, entry_name, argument, |
| 2286 | binary_path, g_previous_managed_mcp_command, &command); |
| 2287 | /* An entry that already says what we would say, but carries extra keys |
| 2288 | * the client added, is ALREADY SATISFIED. Return success without |
| 2289 | * touching the file. |
| 2290 | * |
| 2291 | * We must not rewrite it: the editor replaces an entry wholesale, so |
| 2292 | * writing our canonical shape over it would delete those keys. Doing |
| 2293 | * nothing is both correct and lossless — the entry already points at |
| 2294 | * this binary with the right type, which is the whole content of the |
| 2295 | * install. |
| 2296 | * |
| 2297 | * This is #1630: OpenCode writes `"enabled": true` next to our |
| 2298 | * `command` and `type`, so every user who had toggled a server in the |
| 2299 | * UI hit `op=mcp_install` failure. Confirmed on Linux and Windows with |
| 2300 | * two independent configs. Merging our fields into an annotated entry |
| 2301 | * while preserving the rest is the fuller fix and is tracked there; |
| 2302 | * this makes the common case work without risking anyone's config. */ |
| 2303 | if (ownership == CBM_JSON_LIKE_OBJECT_MATCH_WITH_EXTRAS) { |
| 2304 | /* Owned via the PREVIOUS managed binary during a relocating |
| 2305 | * update: the annotated entry still names the old location, and a |
| 2306 | * wholesale rewrite would drop the client's keys — repair only the |
| 2307 | * command member (#1630's deferred field-merge). An entry already |
| 2308 | * naming the current binary needs nothing. */ |
| 2309 | bool via_previous = command && g_previous_managed_mcp_command && |
| 2310 | strcmp(command, g_previous_managed_mcp_command) == 0 && |
| 2311 | strcmp(command, binary_path) != 0; |
| 2312 | if (!via_previous) { |
| 2313 | free(command); |
| 2314 | free(document); |
| 2315 | return CLI_OK; |
| 2316 | } |
| 2317 | char *value = cbm_json_mcp_render_command_value(binary_path, schema); |
| 2318 | if (!value) { |
| 2319 | free(command); |
| 2320 | free(document); |
| 2321 | return CLI_ERR; |
| 2322 | } |
| 2323 | int repair = cbm_json_like_replace_field_raw_if_unchanged( |
| 2324 | config_path, object_path, path_len, entry_name, "command", value, document, |
| 2325 | document_length); |
| 2326 | free(value); |
no test coverage detected