Skip to content

Commit 3a4160b

Browse files
DeusDataclaude
andcommitted
fix(cli): complete the OpenHands integration (settings.json mcp_config + agent-profile refs) (DeusData#1826)
install wrote only part of the OpenHands config. Register the server globally under ~/.openhands/settings.json -> mcp_config {transport:stdio, command, enabled} (owned-entry, unrelated content preserved), and add "codebase-memory-mcp" to each ~/.openhands/agent-profiles/<p>.json mcp_server_refs array (null -> [it], existing -> appended once). uninstall removes exactly what it added. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 208ee99 commit 3a4160b

5 files changed

Lines changed: 696 additions & 5 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

22872422
int 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). */
92329410
static 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];

‎src/cli/config_json_like.c‎

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,8 +1223,19 @@ static int jl_make_insertion(const char *text, size_t length, size_t object_star
12231223
return 0;
12241224
}
12251225

1226-
if (object->close_pos == gap_start ||
1227-
!jl_is_space((unsigned char)text[object->close_pos - 1U])) {
1226+
/* Zero-width insertion right at close_pos: whatever byte already sits at
1227+
* close_pos - 1 is untouched and stays in the output. When the original
1228+
* had a real gap there (close_pos != gap_start, e.g. "[ \"a\" ]"), that
1229+
* preserved byte already supplies a separator on the leading side, so
1230+
* mirroring it on the trailing side (space before the bracket) matches
1231+
* the array's own loose style. The fully tight case ("[\"a\"]", nothing
1232+
* at all between the last value and the bracket) has no such byte to
1233+
* lean on: manufacture ONE space so `,new` reads `, new` — the
1234+
* comma-spacing convention every other insertion path uses — and add
1235+
* nothing after, so `new]` stays `new]` rather than gaining a trailing
1236+
* space the original never had (#1826 byte-for-byte round-trip). */
1237+
bool tight = object->close_pos == gap_start;
1238+
if (tight || !jl_is_space((unsigned char)text[object->close_pos - 1U])) {
12281239
if (jl_buffer_char(insertion, ' ') != 0) {
12291240
return -1;
12301241
}
@@ -1235,7 +1246,7 @@ static int jl_make_insertion(const char *text, size_t length, size_t object_star
12351246
if (object->trailing_comma && jl_buffer_char(insertion, ',') != 0) {
12361247
return -1;
12371248
}
1238-
return jl_buffer_char(insertion, ' ');
1249+
return tight ? 0 : jl_buffer_char(insertion, ' ');
12391250
}
12401251

12411252
static int jl_apply_edits(const char *source, size_t source_length, jl_edit_t *edits,
@@ -2406,6 +2417,13 @@ int cbm_json_like_remove_entry_if_unchanged(const char *file_path, const char *c
24062417
expected_length);
24072418
}
24082419

2420+
/* True when [start, end) is the bare 4-byte literal token null (never a
2421+
* quoted "null" string, which is 6 bytes with the quotes) — the documented
2422+
* OpenHands "no list yet" shape for mcp_server_refs (#1826). */
2423+
static bool jl_is_null_literal(const char *text, size_t start, size_t end) {
2424+
return end - start == 4U && memcmp(text + start, "null", 4U) == 0;
2425+
}
2426+
24092427
int cbm_json_like_add_unique_string_at_path(const char *file_path, const char *const *object_path,
24102428
size_t path_len, const char *array_key,
24112429
const char *string_value) {
@@ -2497,6 +2515,24 @@ int cbm_json_like_add_unique_string_at_path(const char *file_path, const char *c
24972515
result = jl_insert_member(source, source_length, object_start, &object, object_path,
24982516
path_len, SIZE_MAX, array_key, array_json.data, array_json.length,
24992517
&updated, &updated_length);
2518+
} else if (jl_is_null_literal(source, object.match.value_start, object.match.value_end)) {
2519+
/* OpenHands profiles ship `"mcp_server_refs": null` as the documented
2520+
* "no list yet" shape (#1826). Splice the built one-element array over
2521+
* the bare null token in place, preserving every other byte (trailing
2522+
* comments included) the way the object-member and array-element
2523+
* edits below already do for their own value spans. */
2524+
size_t head = object.match.value_start;
2525+
size_t tail = object.match.value_end;
2526+
updated_length = head + array_json.length + (source_length - tail);
2527+
updated = (char *)malloc(updated_length + 1U);
2528+
if (!updated) {
2529+
result = -1;
2530+
} else {
2531+
memcpy(updated, source, head);
2532+
memcpy(updated + head, array_json.data, array_json.length);
2533+
memcpy(updated + head + array_json.length, source + tail, source_length - tail);
2534+
updated[updated_length] = '\0';
2535+
}
25002536
} else if (source[object.match.value_start] != '[') {
25012537
result = -1;
25022538
} else {
@@ -2759,6 +2795,24 @@ static int jl_decode_field_string(const char *text, size_t start, size_t end,
27592795
}
27602796
return jl_decode_string_value(text, start, end, value_out) == 0 ? 0 : 1;
27612797
}
2798+
if (shape == CBM_JSON_LIKE_VALUE_LITERAL) {
2799+
/* The value's token boundaries already exclude surrounding trivia
2800+
* (jl_parse_value advances pos past exactly the token). Copy the raw
2801+
* bytes verbatim so a quoted "true" never equals the bare literal
2802+
* true, and the caller's expected_string comparison decides match. */
2803+
if (start >= end) {
2804+
return 1;
2805+
}
2806+
size_t length = end - start;
2807+
char *copy = (char *)malloc(length + 1U);
2808+
if (!copy) {
2809+
return 1;
2810+
}
2811+
memcpy(copy, text + start, length);
2812+
copy[length] = '\0';
2813+
*value_out = copy;
2814+
return 0;
2815+
}
27622816
if (shape != CBM_JSON_LIKE_VALUE_SINGLE_STRING_ARRAY || start >= end || text[start] != '[') {
27632817
return 1;
27642818
}
@@ -2827,11 +2881,13 @@ int cbm_json_like_match_object_entry(const char *document, size_t document_lengt
28272881
size_t capture_count = 0U;
28282882
for (size_t i = 0U; i < field_count; ++i) {
28292883
if (!fields[i].key || fields[i].key[0] == '\0' ||
2830-
fields[i].shape > CBM_JSON_LIKE_VALUE_SINGLE_STRING_ARRAY ||
2884+
fields[i].shape > CBM_JSON_LIKE_VALUE_LITERAL ||
28312885
(fields[i].flags &
28322886
~(CBM_JSON_LIKE_FIELD_REQUIRED | CBM_JSON_LIKE_FIELD_CAPTURE_STRING)) != 0U ||
28332887
((fields[i].flags & CBM_JSON_LIKE_FIELD_CAPTURE_STRING) != 0U &&
2834-
fields[i].shape == CBM_JSON_LIKE_VALUE_EMPTY_ARRAY)) {
2888+
(fields[i].shape == CBM_JSON_LIKE_VALUE_EMPTY_ARRAY ||
2889+
fields[i].shape == CBM_JSON_LIKE_VALUE_LITERAL)) ||
2890+
(fields[i].shape == CBM_JSON_LIKE_VALUE_LITERAL && !fields[i].expected_string)) {
28352891
return -1;
28362892
}
28372893
capture_count += (fields[i].flags & CBM_JSON_LIKE_FIELD_CAPTURE_STRING) != 0U ? 1U : 0U;

‎src/cli/config_json_like.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ typedef enum {
4747
CBM_JSON_LIKE_VALUE_STRING,
4848
CBM_JSON_LIKE_VALUE_EMPTY_ARRAY,
4949
CBM_JSON_LIKE_VALUE_SINGLE_STRING_ARRAY,
50+
/* An exact bare token (true/false/null/a number) matched byte-for-byte
51+
* against expected_string. Requires expected_string and may not be
52+
* combined with CBM_JSON_LIKE_FIELD_CAPTURE_STRING — a fixed token has
53+
* nothing meaningful to capture for the caller. */
54+
CBM_JSON_LIKE_VALUE_LITERAL,
5055
} cbm_json_like_value_shape_t;
5156

5257
enum {

0 commit comments

Comments
 (0)