Skip to content

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.

Bump MCP protocol version with proper negotiation

Bing Ho83619f8d415e
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index 8a9a0329f..39350ca39 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -342,12 +342,44 @@ char *cbm_mcp_tools_list(void) {
return out;
}
-char *cbm_mcp_initialize_response(void) {
+/* Supported protocol versions, newest first. The server picks the newest
+ * version that it shares with the client (per MCP spec version negotiation). */
+static const char *SUPPORTED_PROTOCOL_VERSIONS[] = {
+ "2025-11-25",
+ "2025-06-18",
+ "2025-03-26",
+ "2024-11-05",
+};
+static const int SUPPORTED_VERSION_COUNT =
+ (int)(sizeof(SUPPORTED_PROTOCOL_VERSIONS) / sizeof(SUPPORTED_PROTOCOL_VERSIONS[0]));
+
+char *cbm_mcp_initialize_response(const char *params_json) {
+ /* Determine protocol version: if client requests a version we support,
+ * echo it back; otherwise respond with our latest. */
+ const char *version = SUPPORTED_PROTOCOL_VERSIONS[0]; /* default: latest */
+ if (params_json) {
+ yyjson_doc *pdoc = yyjson_read(params_json, strlen(params_json), 0);
+ if (pdoc) {
+ yyjson_val *pv =
+ yyjson_obj_get(yyjson_doc_get_root(pdoc), "protocolVersion");
+ if (pv && yyjson_is_str(pv)) {
+ const char *requested = yyjson_get_str(pv);
+ for (int i = 0; i < SUPPORTED_VERSION_COUNT; i++) {
+ if (strcmp(requested, SUPPORTED_PROTOCOL_VERSIONS[i]) == 0) {
+ version = SUPPORTED_PROTOCOL_VERSIONS[i];
+ break;
+ }
+ }
+ }
+ yyjson_doc_free(pdoc);
+ }
+ }
+
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
yyjson_mut_val *root = yyjson_mut_obj(doc);
yyjson_mut_doc_set_root(doc, root);
- yyjson_mut_obj_add_str(doc, root, "protocolVersion", "2024-11-05");
+ yyjson_mut_obj_add_str(doc, root, "protocolVersion", version);
yyjson_mut_val *impl = yyjson_mut_obj(doc);
yyjson_mut_obj_add_str(doc, impl, "name", "codebase-memory-mcp");
@@ -2253,7 +2285,7 @@ char *cbm_mcp_server_handle(cbm_mcp_server_t *srv, const char *line) {
char *result_json = NULL;
if (strcmp(req.method, "initialize") == 0) {
- result_json = cbm_mcp_initialize_response();
+ result_json = cbm_mcp_initialize_response(req.params_raw);
start_update_check(srv);
detect_session(srv);
maybe_auto_index(srv);
diff --git a/src/mcp/mcp.h b/src/mcp/mcp.h
index ebfefa87f..7e65912e9 100644
--- a/src/mcp/mcp.h
+++ b/src/mcp/mcp.h
@@ -55,8 +55,9 @@ char *cbm_mcp_text_result(const char *text, bool is_error);
/* Format the tools/list response. Returns heap-allocated JSON. */
char *cbm_mcp_tools_list(void);
-/* Format the initialize response. Returns heap-allocated JSON. */
-char *cbm_mcp_initialize_response(void);
+/* Format the initialize response. params_json is the raw initialize params
+ * (used for protocol version negotiation). Returns heap-allocated JSON. */
+char *cbm_mcp_initialize_response(const char *params_json);
/* ── Tool argument helpers ────────────────────────────────────── */
diff --git a/tests/test_mcp.c b/tests/test_mcp.c
index 9fd069015..3bd6c7339 100644
--- a/tests/test_mcp.c
+++ b/tests/test_mcp.c
@@ -98,11 +98,30 @@ TEST(jsonrpc_format_error) {
* ══════════════════════════════════════════════════════════════════ */
TEST(mcp_initialize_response) {
- char *json = cbm_mcp_initialize_response();
+ /* Default (no params): returns latest supported version */
+ char *json = cbm_mcp_initialize_response(NULL);
ASSERT_NOT_NULL(json);
ASSERT_NOT_NULL(strstr(json, "codebase-memory-mcp"));
ASSERT_NOT_NULL(strstr(json, "capabilities"));
ASSERT_NOT_NULL(strstr(json, "tools"));
+ ASSERT_NOT_NULL(strstr(json, "2025-11-25"));
+ free(json);
+
+ /* Client requests a supported version: server echoes it */
+ json = cbm_mcp_initialize_response("{\"protocolVersion\":\"2024-11-05\"}");
+ ASSERT_NOT_NULL(json);
+ ASSERT_NOT_NULL(strstr(json, "2024-11-05"));
+ free(json);
+
+ json = cbm_mcp_initialize_response("{\"protocolVersion\":\"2025-06-18\"}");
+ ASSERT_NOT_NULL(json);
+ ASSERT_NOT_NULL(strstr(json, "2025-06-18"));
+ free(json);
+
+ /* Client requests unknown version: server returns its latest */
+ json = cbm_mcp_initialize_response("{\"protocolVersion\":\"9999-01-01\"}");
+ ASSERT_NOT_NULL(json);
+ ASSERT_NOT_NULL(strstr(json, "2025-11-25"));
free(json);
PASS();
}