Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(mcp): preserve JSON-RPC string ids instead of coercing to int (#253)
Martin Vogel4abcaa43e7af
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.cindex c7d20dbb9..8f75a2602 100644--- a/src/mcp/mcp.c+++ b/src/mcp/mcp.c@@ -151,7 +151,9 @@ int cbm_jsonrpc_parse(const char *line, cbm_jsonrpc_request_t *out) {if (yyjson_is_int(v_id)) {out->id = yyjson_get_int(v_id);} else if (yyjson_is_str(v_id)) {- out->id = strtol(yyjson_get_str(v_id), NULL, CBM_DECIMAL_BASE);+ /* JSON-RPC 2.0 §4 permits string ids (Claude Desktop uses them).+ * Preserve verbatim instead of coercing via strtol (issue #253). */+ out->id_str = heap_strdup(yyjson_get_str(v_id));}}@@ -169,6 +171,7 @@ void cbm_jsonrpc_request_free(cbm_jsonrpc_request_t *r) {}safe_str_free(&r->jsonrpc);safe_str_free(&r->method);+ safe_str_free(&r->id_str);safe_str_free(&r->params_raw);memset(r, 0, sizeof(*r));}@@ -183,7 +186,11 @@ char *cbm_jsonrpc_format_response(const cbm_jsonrpc_response_t *resp) {yyjson_mut_doc_set_root(doc, root);yyjson_mut_obj_add_str(doc, root, "jsonrpc", "2.0");- yyjson_mut_obj_add_int(doc, root, "id", resp->id);+ if (resp->id_str) {+ yyjson_mut_obj_add_str(doc, root, "id", resp->id_str);+ } else {+ yyjson_mut_obj_add_int(doc, root, "id", resp->id);+ }if (resp->error_json) {/* Parse the error JSON and embed */@@ -4333,13 +4340,23 @@ char *cbm_mcp_server_handle(cbm_mcp_server_t *srv, const char *line) {free(tool_name);free(tool_args);} else {- char *err = cbm_jsonrpc_format_error(req.id, JSONRPC_METHOD_NOT_FOUND, "Method not found");+ /* Echo the original id (string or numeric, issue #253) on the error. */+ char err_obj[160];+ snprintf(err_obj, sizeof(err_obj), "{\"code\":%d,\"message\":\"Method not found\"}",+ JSONRPC_METHOD_NOT_FOUND);+ cbm_jsonrpc_response_t err_resp = {+ .id = req.id,+ .id_str = req.id_str,+ .error_json = err_obj,+ };+ char *err = cbm_jsonrpc_format_response(&err_resp);cbm_jsonrpc_request_free(&req);return err;}cbm_jsonrpc_response_t resp = {.id = req.id,+ .id_str = req.id_str,.result_json = result_json,};char *out = cbm_jsonrpc_format_response(&resp);diff --git a/src/mcp/mcp.h b/src/mcp/mcp.hindex 0ebcc6f83..121038cbd 100644--- a/src/mcp/mcp.h+++ b/src/mcp/mcp.h@@ -22,13 +22,15 @@ struct cbm_config; /* from cli/cli.h */typedef struct {const char *jsonrpc; /* "2.0" */const char *method; /* e.g. "initialize", "tools/call" */- int64_t id; /* request ID (-1 if notification) */+ int64_t id; /* request ID (numeric form; -1 if notification) */+ const char *id_str; /* non-NULL when id is a JSON string (issue #253) */bool has_id; /* false for notifications */const char *params_raw; /* raw JSON string of params */} cbm_jsonrpc_request_t;typedef struct {int64_t id;+ const char *id_str; /* non-NULL to echo a string id verbatim (issue #253) */const char *result_json; /* JSON string for result (success) */const char *error_json; /* JSON string for error (failure), NULL on success */int error_code; /* JSON-RPC error code */diff --git a/tests/test_mcp.c b/tests/test_mcp.cindex eeb732289..047962421 100644--- a/tests/test_mcp.c+++ b/tests/test_mcp.c@@ -64,6 +64,43 @@ TEST(jsonrpc_parse_tools_call) {PASS();}+/* issue #253: JSON-RPC 2.0 §4 permits string ids (Claude Desktop sends them+ * for "initialize"). Previously strtol-coerced to 0; must be preserved. */+TEST(jsonrpc_parse_string_id_issue253) {+ const char *line = "{\"jsonrpc\":\"2.0\",\"id\":\"init-abc\",\"method\":\"initialize\"}";+ cbm_jsonrpc_request_t req = {0};+ int rc = cbm_jsonrpc_parse(line, &req);+ ASSERT_EQ(rc, 0);+ ASSERT_TRUE(req.has_id);+ ASSERT_NOT_NULL(req.id_str);+ ASSERT_STR_EQ(req.id_str, "init-abc");+ cbm_jsonrpc_request_free(&req);++ /* A purely non-numeric string would have become 0 under strtol. */+ const char *line2 = "{\"jsonrpc\":\"2.0\",\"id\":\"xyz\",\"method\":\"ping\"}";+ cbm_jsonrpc_request_t req2 = {0};+ ASSERT_EQ(cbm_jsonrpc_parse(line2, &req2), 0);+ ASSERT_NOT_NULL(req2.id_str);+ ASSERT_STR_EQ(req2.id_str, "xyz");+ cbm_jsonrpc_request_free(&req2);+ PASS();+}++/* issue #253: the response must echo the string id verbatim, not as a number. */+TEST(jsonrpc_format_response_string_id_issue253) {+ cbm_jsonrpc_response_t resp = {+ .id_str = "init-abc",+ .result_json = "{\"ok\":true}",+ };+ char *json = cbm_jsonrpc_format_response(&resp);+ ASSERT_NOT_NULL(json);+ ASSERT_NOT_NULL(strstr(json, "\"id\":\"init-abc\""));+ /* Must NOT have coerced to a numeric id. */+ ASSERT_NULL(strstr(json, "\"id\":0"));+ free(json);+ PASS();+}+/* ══════════════════════════════════════════════════════════════════* JSON-RPC FORMATTING* ══════════════════════════════════════════════════════════════════ */@@ -1433,13 +1470,15 @@ TEST(jsonrpc_parse_missing_method) {}TEST(jsonrpc_parse_string_id) {- /* JSON-RPC spec allows string IDs; parser converts via strtol */+ /* JSON-RPC §4: string and numeric ids are distinct. A string id is+ * preserved verbatim (issue #253), never coerced to a number. */const char *line = "{\"jsonrpc\":\"2.0\",\"id\":\"99\",\"method\":\"tools/list\"}";cbm_jsonrpc_request_t req = {0};int rc = cbm_jsonrpc_parse(line, &req);ASSERT_EQ(rc, 0);ASSERT_TRUE(req.has_id);- ASSERT_EQ(req.id, 99);+ ASSERT_NOT_NULL(req.id_str);+ ASSERT_STR_EQ(req.id_str, "99");ASSERT_STR_EQ(req.method, "tools/list");cbm_jsonrpc_request_free(&req);PASS();@@ -1753,10 +1792,10 @@ TEST(mcp_server_run_rapid_messages) {* the buffer. Fill a temp cache dir with enough long-named .db files to* exceed 4 KB, then hit the bad-project path. Under ASan a regression aborts* here; the fixed bounds-check keeps it clean and returns a normal error. */-#define ISSUE235_DBNAME(buf, dir, i) \- snprintf((buf), sizeof(buf), \- "%s/proj_%02d_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \- "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.db", \+#define ISSUE235_DBNAME(buf, dir, i) \+ snprintf((buf), sizeof(buf), \+ "%s/proj_%02d_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.db", \(dir), (i))TEST(tool_bad_project_name_no_overflow_issue235) {char cache[256];@@ -1818,6 +1857,8 @@ SUITE(mcp) {RUN_TEST(jsonrpc_parse_notification);RUN_TEST(jsonrpc_parse_invalid);RUN_TEST(jsonrpc_parse_tools_call);+ RUN_TEST(jsonrpc_parse_string_id_issue253);+ RUN_TEST(jsonrpc_format_response_string_id_issue253);/* JSON-RPC parsing — edge cases */RUN_TEST(jsonrpc_parse_empty_string);