@@ -2286,6 +2286,140 @@ static int cbm_remove_openclaw_compaction(const char *config_path) {
22862286 : CLI_ERR;
22872287}
22882288
2289+ /* ── OpenHands settings.json mcp_config (#1826) ───────────────
2290+ * The mcpServers-style config installed above (cbm_install_editor_mcp into
2291+ * ~/.openhands/mcp.json) is not enough: OpenHands only loads a global MCP
2292+ * server it finds registered under settings.json -> mcp_config, in its own
2293+ * shape ({transport, command, enabled} — no args array). Agent profiles then
2294+ * opt in individually via mcp_server_refs (below). */
2295+
2296+ static size_t cbm_openhands_ownership_fields(cbm_json_like_object_field_t fields[3]) {
2297+ fields[0] = (cbm_json_like_object_field_t){
2298+ .key = "transport",
2299+ .shape = CBM_JSON_LIKE_VALUE_STRING,
2300+ .expected_string = "stdio",
2301+ .flags = CBM_JSON_LIKE_FIELD_REQUIRED,
2302+ };
2303+ fields[1] = (cbm_json_like_object_field_t){
2304+ .key = "command",
2305+ .shape = CBM_JSON_LIKE_VALUE_STRING,
2306+ .expected_string = NULL,
2307+ .flags = CBM_JSON_LIKE_FIELD_REQUIRED | CBM_JSON_LIKE_FIELD_CAPTURE_STRING,
2308+ };
2309+ fields[2] = (cbm_json_like_object_field_t){
2310+ .key = "enabled",
2311+ .shape = CBM_JSON_LIKE_VALUE_LITERAL,
2312+ .expected_string = "true",
2313+ .flags = CBM_JSON_LIKE_FIELD_REQUIRED,
2314+ };
2315+ return 3U;
2316+ }
2317+
2318+ static char *cbm_build_openhands_mcp_entry(const char *binary_path) {
2319+ yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
2320+ if (!doc) {
2321+ return NULL;
2322+ }
2323+ yyjson_mut_val *root = yyjson_mut_obj(doc);
2324+ bool ok = root && yyjson_mut_obj_add_strcpy(doc, root, "transport", "stdio") &&
2325+ yyjson_mut_obj_add_strcpy(doc, root, "command", binary_path) &&
2326+ yyjson_mut_obj_add_bool(doc, root, "enabled", true);
2327+ char *json = NULL;
2328+ if (ok) {
2329+ yyjson_mut_doc_set_root(doc, root);
2330+ json = yyjson_mut_write(doc, YYJSON_WRITE_NOFLAG, NULL);
2331+ }
2332+ yyjson_mut_doc_free(doc);
2333+ return json;
2334+ }
2335+
2336+ /* Insert or leave alone: an already-owned entry (exact match, or annotated
2337+ * with extra keys the client added) needs no write. A same-named entry that
2338+ * does not match our shape is left untouched and reported as an error rather
2339+ * than overwritten, matching cbm_upsert_json_named_mcp's fail-closed rule for
2340+ * every other editor client. */
2341+ static int cbm_upsert_openhands_settings_mcp(const char *binary_path, const char *settings_path) {
2342+ if (!binary_path || !settings_path) {
2343+ return CLI_ERR;
2344+ }
2345+ static const char *const path[] = {"mcp_config"};
2346+ char *document = NULL;
2347+ size_t document_length = 0U;
2348+ int read_result = cbm_json_like_read_document(settings_path, &document, &document_length);
2349+ if (read_result < 0) {
2350+ return CLI_ERR;
2351+ }
2352+ if (read_result == 0) {
2353+ cbm_json_like_object_field_t fields[3];
2354+ size_t field_count = cbm_openhands_ownership_fields(fields);
2355+ char *command = NULL;
2356+ int ownership = cbm_json_like_match_object_entry(document, document_length, path, 1U,
2357+ CBM_DEFAULT_MCP_SERVER_NAME, fields,
2358+ field_count, &command);
2359+ free(command);
2360+ if (ownership == CBM_JSON_LIKE_OBJECT_MATCH ||
2361+ ownership == CBM_JSON_LIKE_OBJECT_MATCH_WITH_EXTRAS) {
2362+ free(document);
2363+ return CLI_OK;
2364+ }
2365+ if (ownership != CBM_JSON_LIKE_OBJECT_MISSING) {
2366+ free(document);
2367+ return CLI_ERR;
2368+ }
2369+ }
2370+ char *entry = cbm_build_openhands_mcp_entry(binary_path);
2371+ if (!entry) {
2372+ free(document);
2373+ return CLI_ERR;
2374+ }
2375+ int edit_result = cbm_json_like_upsert_entry_if_unchanged(
2376+ settings_path, path, 1U, CBM_DEFAULT_MCP_SERVER_NAME, entry,
2377+ read_result == 1 ? NULL : document, document_length);
2378+ free(entry);
2379+ free(document);
2380+ return edit_result == 0 ? CLI_OK : CLI_ERR;
2381+ }
2382+
2383+ /* Remove only an entry that is still recognisably ours (an annotated entry is
2384+ * left in place and reported, same rule as insertion above); a missing file,
2385+ * path, or entry is a successful no-op. */
2386+ static int cbm_remove_openhands_settings_mcp(const char *settings_path) {
2387+ if (!settings_path) {
2388+ return CLI_ERR;
2389+ }
2390+ static const char *const path[] = {"mcp_config"};
2391+ char *document = NULL;
2392+ size_t document_length = 0U;
2393+ int read_result = cbm_json_like_read_document(settings_path, &document, &document_length);
2394+ if (read_result == 1) {
2395+ free(document);
2396+ return CLI_OK;
2397+ }
2398+ if (read_result < 0) {
2399+ free(document);
2400+ return CLI_ERR;
2401+ }
2402+ cbm_json_like_object_field_t fields[3];
2403+ size_t field_count = cbm_openhands_ownership_fields(fields);
2404+ char *command = NULL;
2405+ int ownership = cbm_json_like_match_object_entry(document, document_length, path, 1U,
2406+ CBM_DEFAULT_MCP_SERVER_NAME, fields,
2407+ field_count, &command);
2408+ free(command);
2409+ if (ownership == CBM_JSON_LIKE_OBJECT_MISSING || ownership == CBM_JSON_LIKE_OBJECT_MISMATCH) {
2410+ free(document);
2411+ return CLI_OK;
2412+ }
2413+ if (ownership != CBM_JSON_LIKE_OBJECT_MATCH) {
2414+ free(document);
2415+ return CLI_ERR;
2416+ }
2417+ int edit_result = cbm_json_like_remove_entry_if_unchanged(
2418+ settings_path, path, 1U, CBM_DEFAULT_MCP_SERVER_NAME, document, document_length);
2419+ free(document);
2420+ return edit_result == 0 ? CLI_OK : CLI_ERR;
2421+ }
2422+
22892423/* ── VS Code MCP (servers key with type:stdio) ────────────────── */
22902424
22912425int cbm_install_vscode_mcp(const char *binary_path, const char *config_path) {
@@ -9355,6 +9489,47 @@ static void uninstall_vscode_profile_configs(const char *code_user, const char *
93559489 cbm_closedir(directory);
93569490}
93579491
9492+ static bool cbm_filename_has_suffix(const char *name, const char *suffix) {
9493+ size_t name_len = strlen(name);
9494+ size_t suffix_len = strlen(suffix);
9495+ return name_len >= suffix_len && strcmp(name + (name_len - suffix_len), suffix) == 0;
9496+ }
9497+
9498+ /* Register or unregister our server against every existing OpenHands agent
9499+ * profile's mcp_server_refs array (#1826). A missing agent-profiles/
9500+ * directory is a silent no-op in both directions — install must never invent
9501+ * it, and uninstall has nothing to undo there. Only *.json entries are
9502+ * touched; a profile directory may hold arbitrary notes alongside profiles. */
9503+ static void openhands_update_profile_refs(const char *profiles_dir, bool installing, bool dry_run) {
9504+ cbm_dir_t *d = cbm_opendir(profiles_dir);
9505+ if (!d) {
9506+ return;
9507+ }
9508+ cbm_dirent_t *ent;
9509+ while ((ent = cbm_readdir(d)) != NULL) {
9510+ if (strcmp(ent->name, ".") == 0 || strcmp(ent->name, "..") == 0 ||
9511+ !cbm_filename_has_suffix(ent->name, ".json")) {
9512+ continue;
9513+ }
9514+ char profile_path[CLI_BUF_1K];
9515+ snprintf(profile_path, sizeof(profile_path), "%s/%s", profiles_dir, ent->name);
9516+ struct stat state;
9517+ if (stat(profile_path, &state) != 0 || !S_ISREG(state.st_mode) || dry_run) {
9518+ continue;
9519+ }
9520+ int result = installing ? cbm_json_like_add_unique_string(profile_path, "mcp_server_refs",
9521+ CBM_DEFAULT_MCP_SERVER_NAME)
9522+ : cbm_json_like_remove_string(profile_path, "mcp_server_refs",
9523+ CBM_DEFAULT_MCP_SERVER_NAME);
9524+ if (result != CLI_OK) {
9525+ record_agent_config_error(
9526+ !installing, "OpenHands",
9527+ installing ? "profile_refs_install" : "profile_refs_uninstall", profile_path);
9528+ }
9529+ }
9530+ cbm_closedir(d);
9531+ }
9532+
93589533/* Install MCP configs for editor-based agents (Zed, KiloCode, VS Code, OpenClaw). */
93599534static void install_editor_agent_configs(const cbm_detected_agents_t *agents, const char *home,
93609535 const char *binary_path, bool force, bool dry_run) {
@@ -9617,11 +9792,32 @@ static void install_additional_agent_configs(const cbm_detected_agents_t *agents
96179792 if (agents->openhands) {
96189793 char cp[CLI_BUF_1K];
96199794 char skills_dir[CLI_BUF_1K];
9795+ char settings_path[CLI_BUF_1K];
9796+ char profiles_dir[CLI_BUF_1K];
96209797 snprintf(cp, sizeof(cp), "%s/.openhands/mcp.json", home);
96219798 snprintf(skills_dir, sizeof(skills_dir), "%s/.agents/skills", home);
9799+ snprintf(settings_path, sizeof(settings_path), "%s/.openhands/settings.json", home);
9800+ snprintf(profiles_dir, sizeof(profiles_dir), "%s/.openhands/agent-profiles", home);
96229801 install_generic_agent_config("OpenHands", binary_path, cp, NULL, dry_run,
96239802 cbm_install_editor_mcp);
96249803 install_agent_skill("OpenHands", skills_dir, force, dry_run);
9804+ /* #1826: the mcpServers-shaped mcp.json above is not enough — OpenHands
9805+ * only loads a server registered under settings.json -> mcp_config, and
9806+ * only for agent profiles that reference it. agent-profiles/ is never
9807+ * invented; a missing directory means nothing to register into yet. */
9808+ if (g_install_plan) {
9809+ plan_record("OpenHands", "mcp_config", settings_path);
9810+ } else {
9811+ if (!dry_run) {
9812+ if (!prepare_config_parent(settings_path) ||
9813+ cbm_upsert_openhands_settings_mcp(binary_path, settings_path) != CLI_OK) {
9814+ record_agent_config_error(false, "OpenHands", "settings_mcp_install",
9815+ settings_path);
9816+ }
9817+ }
9818+ printf(" settings mcp_config: %s\n", settings_path);
9819+ openhands_update_profile_refs(profiles_dir, true, dry_run);
9820+ }
96259821 }
96269822 if (agents->augment) {
96279823 char cp[CLI_BUF_1K];
@@ -11877,11 +12073,21 @@ static void uninstall_additional_agents(const cbm_detected_agents_t *agents, con
1187712073 if (agents->openhands) {
1187812074 char cp[CLI_BUF_1K];
1187912075 char skills_dir[CLI_BUF_1K];
12076+ char settings_path[CLI_BUF_1K];
12077+ char profiles_dir[CLI_BUF_1K];
1188012078 snprintf(cp, sizeof(cp), "%s/.openhands/mcp.json", home);
1188112079 snprintf(skills_dir, sizeof(skills_dir), "%s/.agents/skills", home);
12080+ snprintf(settings_path, sizeof(settings_path), "%s/.openhands/settings.json", home);
12081+ snprintf(profiles_dir, sizeof(profiles_dir), "%s/.openhands/agent-profiles", home);
1188212082 uninstall_agent_mcp_instr((mcp_uninstall_args_t){"OpenHands", cp, NULL}, dry_run,
1188312083 cbm_remove_editor_mcp_owned);
1188412084 printf(" removed %d skill(s)\n", cbm_remove_skills(skills_dir, dry_run));
12085+ /* #1826 counterpart: undo the settings.json registration and every
12086+ * agent profile's mcp_server_refs entry the install above added. */
12087+ if (!dry_run && cbm_remove_openhands_settings_mcp(settings_path) != CLI_OK) {
12088+ record_agent_config_error(true, "OpenHands", "settings_mcp_uninstall", settings_path);
12089+ }
12090+ openhands_update_profile_refs(profiles_dir, false, dry_run);
1188512091 }
1188612092 if (agents->augment) {
1188712093 char cp[CLI_BUF_1K];
0 commit comments