@@ -2282,6 +2282,141 @@ static int cbm_remove_openclaw_compaction(const char *config_path) {
22822282 : CLI_ERR;
22832283}
22842284
2285+ /* ── OpenHands settings.json mcp_config (#1826) ───────────────
2286+ * The mcpServers-style config installed above (cbm_install_editor_mcp into
2287+ * ~/.openhands/mcp.json) is not enough: OpenHands only loads a global MCP
2288+ * server it finds registered under settings.json -> mcp_config, in its own
2289+ * shape ({transport, command, enabled} — no args array). Agent profiles then
2290+ * opt in individually via mcp_server_refs (below). */
2291+
2292+ static size_t cbm_openhands_ownership_fields(cbm_json_like_object_field_t fields[3]) {
2293+ fields[0] = (cbm_json_like_object_field_t){
2294+ .key = "transport",
2295+ .shape = CBM_JSON_LIKE_VALUE_STRING,
2296+ .expected_string = "stdio",
2297+ .flags = CBM_JSON_LIKE_FIELD_REQUIRED,
2298+ };
2299+ fields[1] = (cbm_json_like_object_field_t){
2300+ .key = "command",
2301+ .shape = CBM_JSON_LIKE_VALUE_STRING,
2302+ .expected_string = NULL,
2303+ .flags = CBM_JSON_LIKE_FIELD_REQUIRED | CBM_JSON_LIKE_FIELD_CAPTURE_STRING,
2304+ };
2305+ fields[2] = (cbm_json_like_object_field_t){
2306+ .key = "enabled",
2307+ .shape = CBM_JSON_LIKE_VALUE_LITERAL,
2308+ .expected_string = "true",
2309+ .flags = CBM_JSON_LIKE_FIELD_REQUIRED,
2310+ };
2311+ return 3U;
2312+ }
2313+
2314+ static char *cbm_build_openhands_mcp_entry(const char *binary_path) {
2315+ yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
2316+ if (!doc) {
2317+ return NULL;
2318+ }
2319+ yyjson_mut_val *root = yyjson_mut_obj(doc);
2320+ bool ok = root && yyjson_mut_obj_add_strcpy(doc, root, "transport", "stdio") &&
2321+ yyjson_mut_obj_add_strcpy(doc, root, "command", binary_path) &&
2322+ yyjson_mut_obj_add_bool(doc, root, "enabled", true);
2323+ char *json = NULL;
2324+ if (ok) {
2325+ yyjson_mut_doc_set_root(doc, root);
2326+ json = yyjson_mut_write(doc, YYJSON_WRITE_NOFLAG, NULL);
2327+ }
2328+ yyjson_mut_doc_free(doc);
2329+ return json;
2330+ }
2331+
2332+ /* Insert or leave alone: an already-owned entry (exact match, or annotated
2333+ * with extra keys the client added) needs no write. A same-named entry that
2334+ * does not match our shape is left untouched and reported as an error rather
2335+ * than overwritten, matching cbm_upsert_json_named_mcp's fail-closed rule for
2336+ * every other editor client. */
2337+ static int cbm_upsert_openhands_settings_mcp(const char *binary_path, const char *settings_path) {
2338+ if (!binary_path || !settings_path) {
2339+ return CLI_ERR;
2340+ }
2341+ static const char *const path[] = {"mcp_config"};
2342+ char *document = NULL;
2343+ size_t document_length = 0U;
2344+ int read_result = cbm_json_like_read_document(settings_path, &document, &document_length);
2345+ if (read_result < 0) {
2346+ return CLI_ERR;
2347+ }
2348+ if (read_result == 0) {
2349+ cbm_json_like_object_field_t fields[3];
2350+ size_t field_count = cbm_openhands_ownership_fields(fields);
2351+ char *command = NULL;
2352+ int ownership =
2353+ cbm_json_like_match_object_entry(document, document_length, path, 1U,
2354+ CBM_DEFAULT_MCP_SERVER_NAME, fields, field_count,
2355+ &command);
2356+ free(command);
2357+ if (ownership == CBM_JSON_LIKE_OBJECT_MATCH ||
2358+ ownership == CBM_JSON_LIKE_OBJECT_MATCH_WITH_EXTRAS) {
2359+ free(document);
2360+ return CLI_OK;
2361+ }
2362+ if (ownership != CBM_JSON_LIKE_OBJECT_MISSING) {
2363+ free(document);
2364+ return CLI_ERR;
2365+ }
2366+ }
2367+ char *entry = cbm_build_openhands_mcp_entry(binary_path);
2368+ if (!entry) {
2369+ free(document);
2370+ return CLI_ERR;
2371+ }
2372+ int edit_result = cbm_json_like_upsert_entry_if_unchanged(
2373+ settings_path, path, 1U, CBM_DEFAULT_MCP_SERVER_NAME, entry,
2374+ read_result == 1 ? NULL : document, document_length);
2375+ free(entry);
2376+ free(document);
2377+ return edit_result == 0 ? CLI_OK : CLI_ERR;
2378+ }
2379+
2380+ /* Remove only an entry that is still recognisably ours (an annotated entry is
2381+ * left in place and reported, same rule as insertion above); a missing file,
2382+ * path, or entry is a successful no-op. */
2383+ static int cbm_remove_openhands_settings_mcp(const char *settings_path) {
2384+ if (!settings_path) {
2385+ return CLI_ERR;
2386+ }
2387+ static const char *const path[] = {"mcp_config"};
2388+ char *document = NULL;
2389+ size_t document_length = 0U;
2390+ int read_result = cbm_json_like_read_document(settings_path, &document, &document_length);
2391+ if (read_result == 1) {
2392+ free(document);
2393+ return CLI_OK;
2394+ }
2395+ if (read_result < 0) {
2396+ free(document);
2397+ return CLI_ERR;
2398+ }
2399+ cbm_json_like_object_field_t fields[3];
2400+ size_t field_count = cbm_openhands_ownership_fields(fields);
2401+ char *command = NULL;
2402+ int ownership = cbm_json_like_match_object_entry(document, document_length, path, 1U,
2403+ CBM_DEFAULT_MCP_SERVER_NAME, fields,
2404+ field_count, &command);
2405+ free(command);
2406+ if (ownership == CBM_JSON_LIKE_OBJECT_MISSING || ownership == CBM_JSON_LIKE_OBJECT_MISMATCH) {
2407+ free(document);
2408+ return CLI_OK;
2409+ }
2410+ if (ownership != CBM_JSON_LIKE_OBJECT_MATCH) {
2411+ free(document);
2412+ return CLI_ERR;
2413+ }
2414+ int edit_result = cbm_json_like_remove_entry_if_unchanged(
2415+ settings_path, path, 1U, CBM_DEFAULT_MCP_SERVER_NAME, document, document_length);
2416+ free(document);
2417+ return edit_result == 0 ? CLI_OK : CLI_ERR;
2418+ }
2419+
22852420/* ── VS Code MCP (servers key with type:stdio) ────────────────── */
22862421
22872422int cbm_install_vscode_mcp(const char *binary_path, const char *config_path) {
@@ -9228,6 +9363,49 @@ static void uninstall_vscode_profile_configs(const char *code_user, const char *
92289363 cbm_closedir(directory);
92299364}
92309365
9366+ static bool cbm_filename_has_suffix(const char *name, const char *suffix) {
9367+ size_t name_len = strlen(name);
9368+ size_t suffix_len = strlen(suffix);
9369+ return name_len >= suffix_len && strcmp(name + (name_len - suffix_len), suffix) == 0;
9370+ }
9371+
9372+ /* Register or unregister our server against every existing OpenHands agent
9373+ * profile's mcp_server_refs array (#1826). A missing agent-profiles/
9374+ * directory is a silent no-op in both directions — install must never invent
9375+ * it, and uninstall has nothing to undo there. Only *.json entries are
9376+ * touched; a profile directory may hold arbitrary notes alongside profiles. */
9377+ static void openhands_update_profile_refs(const char *profiles_dir, bool installing,
9378+ bool dry_run) {
9379+ cbm_dir_t *d = cbm_opendir(profiles_dir);
9380+ if (!d) {
9381+ return;
9382+ }
9383+ cbm_dirent_t *ent;
9384+ while ((ent = cbm_readdir(d)) != NULL) {
9385+ if (strcmp(ent->name, ".") == 0 || strcmp(ent->name, "..") == 0 ||
9386+ !cbm_filename_has_suffix(ent->name, ".json")) {
9387+ continue;
9388+ }
9389+ char profile_path[CLI_BUF_1K];
9390+ snprintf(profile_path, sizeof(profile_path), "%s/%s", profiles_dir, ent->name);
9391+ struct stat state;
9392+ if (stat(profile_path, &state) != 0 || !S_ISREG(state.st_mode) || dry_run) {
9393+ continue;
9394+ }
9395+ int result = installing ? cbm_json_like_add_unique_string(
9396+ profile_path, "mcp_server_refs", CBM_DEFAULT_MCP_SERVER_NAME)
9397+ : cbm_json_like_remove_string(profile_path, "mcp_server_refs",
9398+ CBM_DEFAULT_MCP_SERVER_NAME);
9399+ if (result != CLI_OK) {
9400+ record_agent_config_error(!installing, "OpenHands",
9401+ installing ? "profile_refs_install"
9402+ : "profile_refs_uninstall",
9403+ profile_path);
9404+ }
9405+ }
9406+ cbm_closedir(d);
9407+ }
9408+
92319409/* Install MCP configs for editor-based agents (Zed, KiloCode, VS Code, OpenClaw). */
92329410static void install_editor_agent_configs(const cbm_detected_agents_t *agents, const char *home,
92339411 const char *binary_path, bool force, bool dry_run) {
@@ -9490,11 +9668,32 @@ static void install_additional_agent_configs(const cbm_detected_agents_t *agents
94909668 if (agents->openhands) {
94919669 char cp[CLI_BUF_1K];
94929670 char skills_dir[CLI_BUF_1K];
9671+ char settings_path[CLI_BUF_1K];
9672+ char profiles_dir[CLI_BUF_1K];
94939673 snprintf(cp, sizeof(cp), "%s/.openhands/mcp.json", home);
94949674 snprintf(skills_dir, sizeof(skills_dir), "%s/.agents/skills", home);
9675+ snprintf(settings_path, sizeof(settings_path), "%s/.openhands/settings.json", home);
9676+ snprintf(profiles_dir, sizeof(profiles_dir), "%s/.openhands/agent-profiles", home);
94959677 install_generic_agent_config("OpenHands", binary_path, cp, NULL, dry_run,
94969678 cbm_install_editor_mcp);
94979679 install_agent_skill("OpenHands", skills_dir, force, dry_run);
9680+ /* #1826: the mcpServers-shaped mcp.json above is not enough — OpenHands
9681+ * only loads a server registered under settings.json -> mcp_config, and
9682+ * only for agent profiles that reference it. agent-profiles/ is never
9683+ * invented; a missing directory means nothing to register into yet. */
9684+ if (g_install_plan) {
9685+ plan_record("OpenHands", "mcp_config", settings_path);
9686+ } else {
9687+ if (!dry_run) {
9688+ if (!prepare_config_parent(settings_path) ||
9689+ cbm_upsert_openhands_settings_mcp(binary_path, settings_path) != CLI_OK) {
9690+ record_agent_config_error(false, "OpenHands", "settings_mcp_install",
9691+ settings_path);
9692+ }
9693+ }
9694+ printf(" settings mcp_config: %s\n", settings_path);
9695+ openhands_update_profile_refs(profiles_dir, true, dry_run);
9696+ }
94989697 }
94999698 if (agents->augment) {
95009699 char cp[CLI_BUF_1K];
@@ -11750,11 +11949,21 @@ static void uninstall_additional_agents(const cbm_detected_agents_t *agents, con
1175011949 if (agents->openhands) {
1175111950 char cp[CLI_BUF_1K];
1175211951 char skills_dir[CLI_BUF_1K];
11952+ char settings_path[CLI_BUF_1K];
11953+ char profiles_dir[CLI_BUF_1K];
1175311954 snprintf(cp, sizeof(cp), "%s/.openhands/mcp.json", home);
1175411955 snprintf(skills_dir, sizeof(skills_dir), "%s/.agents/skills", home);
11956+ snprintf(settings_path, sizeof(settings_path), "%s/.openhands/settings.json", home);
11957+ snprintf(profiles_dir, sizeof(profiles_dir), "%s/.openhands/agent-profiles", home);
1175511958 uninstall_agent_mcp_instr((mcp_uninstall_args_t){"OpenHands", cp, NULL}, dry_run,
1175611959 cbm_remove_editor_mcp_owned);
1175711960 printf(" removed %d skill(s)\n", cbm_remove_skills(skills_dir, dry_run));
11961+ /* #1826 counterpart: undo the settings.json registration and every
11962+ * agent profile's mcp_server_refs entry the install above added. */
11963+ if (!dry_run && cbm_remove_openhands_settings_mcp(settings_path) != CLI_OK) {
11964+ record_agent_config_error(true, "OpenHands", "settings_mcp_uninstall", settings_path);
11965+ }
11966+ openhands_update_profile_refs(profiles_dir, false, dry_run);
1175811967 }
1175911968 if (agents->augment) {
1176011969 char cp[CLI_BUF_1K];
0 commit comments