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.

fix(mcp): address QA round 2 findings

Shane McCarron5b534ce4652c
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c
index 0729d0fa5..6ccccbbba 100644
--- a/src/mcp/mcp.c
+++ b/src/mcp/mcp.c
@@ -2425,20 +2425,10 @@ int cbm_mcp_server_run(cbm_mcp_server_t *srv, FILE *in, FILE *out) {
* would block indefinitely, preventing the Phase 3 idle eviction
* timeout from ever firing. */
int saved_flags = fcntl(fd, F_GETFL);
- if (saved_flags >= 0) {
- (void)fcntl(fd, F_SETFL, saved_flags | O_NONBLOCK);
- }
- int c = fgetc(in);
- if (saved_flags >= 0) {
- (void)fcntl(fd, F_SETFL, saved_flags); /* restore blocking */
- }
- if (c == EOF) {
- if (feof(in)) {
- break; /* true EOF */
- }
- /* No buffered data (EAGAIN from non-blocking read) — clear the
- * ferror indicator set by EAGAIN, then do the blocking poll. */
- clearerr(in);
+ if (saved_flags < 0) {
+ /* fcntl failed (should not happen on a valid fd) — skip the
+ * FILE* peek and fall straight through to the blocking poll so
+ * idle eviction still fires on timeout. */
pr = poll(&pfd, 1, STORE_IDLE_TIMEOUT_S * 1000);
if (pr < 0) {
break;
@@ -2448,8 +2438,28 @@ int cbm_mcp_server_run(cbm_mcp_server_t *srv, FILE *in, FILE *out) {
continue;
}
} else {
- /* Buffered data found — push back and fall through to getline */
- (void)ungetc(c, in);
+ (void)fcntl(fd, F_SETFL, saved_flags | O_NONBLOCK);
+ int c = fgetc(in);
+ (void)fcntl(fd, F_SETFL, saved_flags); /* restore blocking */
+ if (c == EOF) {
+ if (feof(in)) {
+ break; /* true EOF */
+ }
+ /* No buffered data (EAGAIN from non-blocking read) — clear
+ * the ferror indicator set by EAGAIN, then blocking poll. */
+ clearerr(in);
+ pr = poll(&pfd, 1, STORE_IDLE_TIMEOUT_S * 1000);
+ if (pr < 0) {
+ break;
+ }
+ if (pr == 0) {
+ cbm_mcp_server_evict_idle(srv, STORE_IDLE_TIMEOUT_S);
+ continue;
+ }
+ } else {
+ /* Buffered data found — push back and fall through to getline */
+ (void)ungetc(c, in);
+ }
}
}
#endif
diff --git a/tests/test_mcp.c b/tests/test_mcp.c
index eb9f34601..bd07cba25 100644
--- a/tests/test_mcp.c
+++ b/tests/test_mcp.c
@@ -1272,11 +1272,16 @@ TEST(mcp_server_run_rapid_messages) {
ASSERT_EQ(rc, 0);
- /* Verify that tools/list response is present in output */
+ /* Verify both responses are present:
+ * id:1 — initialize response
+ * id:2 — tools/list response (notifications/initialized produces none)
+ * and that the tools list payload is included. */
rewind(out_fp);
char buf[4096] = {0};
size_t nread = fread(buf, 1, sizeof(buf) - 1, out_fp);
ASSERT_TRUE(nread > 0);
+ ASSERT_NOT_NULL(strstr(buf, "\"id\":1"));
+ ASSERT_NOT_NULL(strstr(buf, "\"id\":2"));
ASSERT_NOT_NULL(strstr(buf, "tools"));
cbm_mcp_server_free(srv);