Skip to content

Commit 0e1b453

Browse files
committed
fix(cli): derive --help tool list from the MCP registry
The top-level --help listed 14 hand-maintained tool names and omitted check_index_coverage; MCP tools/list correctly advertised all 15. The help string was a separate copy of the registry, so it drifted silently when the tool was added. Render the Tools block from the TOOLS registry itself (new cbm_mcp_tools_help_list, plus cbm_mcp_tool_count/cbm_mcp_tool_name accessors) so the two lists share one source of truth and cannot diverge again. A unit test pins the render against the registry: every advertised tool appears, cardinality matches, and lines stay 80-column safe. Fixes DeusData#1361 Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent a65faeb commit 0e1b453

4 files changed

Lines changed: 93 additions & 4 deletions

File tree

‎src/main.c‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -888,10 +888,13 @@ static void print_help(void) {
888888
printf(" Manual/UI MCP boundaries: Qodo, Warp, JetBrains AI/ACP, Replit,\n");
889889
printf(" Plandex, SWE-agent, BLACKBOX, GitHub cloud agents, Jules,\n");
890890
printf(" CodeRabbit.\n");
891-
printf("\nTools: index_repository, search_graph, query_graph, trace_path,\n");
892-
printf(" get_code_snippet, get_graph_schema, get_architecture, search_code,\n");
893-
printf(" list_projects, delete_project, index_status, detect_changes,\n");
894-
printf(" manage_adr, ingest_traces\n");
891+
/* Rendered from the MCP tool registry: a hand-maintained copy here
892+
* omitted check_index_coverage (#1361) and could silently drift again. */
893+
char *tools_help = cbm_mcp_tools_help_list();
894+
if (tools_help) {
895+
printf("\n%s", tools_help);
896+
free(tools_help);
897+
}
895898
}
896899

897900
/* ── Main ───────────────────────────────────────────────────────── */

‎src/mcp/mcp.c‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ enum {
3434
MCP_CONTENT_PREFIX = 15, /* strlen("Content-Length:") */
3535
MCP_RETURN_2 = 2,
3636
MCP_TOOLS_PAGE_SIZE = 8,
37+
MCP_HELP_TOOLS_WRAP_COL = 74, /* --help tool list stays readable on 80-col terminals */
3738
MCP_MAX_CROSS_REPO_TARGETS = 4096,
3839
};
3940
#define MCP_MS_TO_US 1000LL
@@ -890,6 +891,47 @@ const char *cbm_mcp_tool_input_schema(const char *tool_name) {
890891
return NULL;
891892
}
892893

894+
int cbm_mcp_tool_count(void) {
895+
return TOOL_COUNT;
896+
}
897+
898+
const char *cbm_mcp_tool_name(int index) {
899+
if (index < 0 || index >= TOOL_COUNT) {
900+
return NULL;
901+
}
902+
return TOOLS[index].name;
903+
}
904+
905+
/* Render the top-level --help "Tools:" block from the registry tools/list
906+
* serves. The list used to be hand-maintained in the help text and drifted
907+
* when check_index_coverage was added (#1361); deriving it here makes that
908+
* divergence impossible. Heap-allocated; caller frees. */
909+
char *cbm_mcp_tools_help_list(void) {
910+
size_t cap = SLEN("Tools:") + 2; /* trailing newline + NUL */
911+
for (int i = 0; i < TOOL_COUNT; i++) {
912+
cap += strlen(TOOLS[i].name) + SLEN(" ,\n "); /* per-tool worst case incl. a wrap */
913+
}
914+
char *out = malloc(cap);
915+
if (!out) {
916+
return NULL;
917+
}
918+
size_t len = (size_t)snprintf(out, cap, "Tools:");
919+
size_t col = len;
920+
for (int i = 0; i < TOOL_COUNT; i++) {
921+
const char *sep = (i + 1 < TOOL_COUNT) ? "," : "";
922+
size_t item = SLEN(" ") + strlen(TOOLS[i].name) + strlen(sep);
923+
if (i > 0 && col + item > MCP_HELP_TOOLS_WRAP_COL) {
924+
len += (size_t)snprintf(out + len, cap - len, "\n ");
925+
col = 1;
926+
}
927+
size_t wrote = (size_t)snprintf(out + len, cap - len, " %s%s", TOOLS[i].name, sep);
928+
len += wrote;
929+
col += wrote;
930+
}
931+
snprintf(out + len, cap - len, "\n");
932+
return out;
933+
}
934+
893935
static int mcp_tools_cursor_offset(const char *params_json, bool *has_cursor_out) {
894936
if (has_cursor_out) {
895937
*has_cursor_out = false;

‎src/mcp/mcp.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ char *cbm_mcp_tools_list(void);
6767
* NULL if the tool is unknown. Backs the CLI flag parser + per-tool --help. */
6868
const char *cbm_mcp_tool_input_schema(const char *tool_name);
6969

70+
/* Registry accessors: the number of tools tools/list advertises, and the name
71+
* of tool `index` (static, do not free; NULL when out of range). */
72+
int cbm_mcp_tool_count(void);
73+
const char *cbm_mcp_tool_name(int index);
74+
75+
/* Render the top-level --help "Tools:" block from the registry so the help
76+
* text cannot drift from tools/list (#1361). Heap-allocated; caller frees. */
77+
char *cbm_mcp_tools_help_list(void);
78+
7079
/* Format the initialize response. params_json is the raw initialize params
7180
* (used for protocol version negotiation). Returns heap-allocated JSON. */
7281
char *cbm_mcp_initialize_response(const char *params_json);

‎tests/test_mcp.c‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,40 @@ TEST(mcp_tools_list) {
762762
PASS();
763763
}
764764

765+
/* #1361: --help omitted check_index_coverage because its tool list was a
766+
* hand-maintained copy. The list is now rendered from the registry; this pins
767+
* the render so a formatter bug cannot reintroduce a silent omission. */
768+
TEST(mcp_tools_help_list_matches_registry) {
769+
char *help = cbm_mcp_tools_help_list();
770+
ASSERT_NOT_NULL(help);
771+
int count = cbm_mcp_tool_count();
772+
ASSERT_GT(count, 0);
773+
for (int i = 0; i < count; i++) {
774+
const char *name = cbm_mcp_tool_name(i);
775+
ASSERT_NOT_NULL(name);
776+
ASSERT_NOT_NULL(strstr(help, name));
777+
}
778+
/* Exactly one comma between consecutive tools: the rendered cardinality
779+
* equals the registry's, so truncation or duplication fails here. */
780+
int commas = 0;
781+
for (const char *p = help; *p; p++) {
782+
if (*p == ',') {
783+
commas++;
784+
}
785+
}
786+
ASSERT_EQ(commas, count - 1);
787+
/* Wrapped for an 80-column terminal. */
788+
const char *line = help;
789+
while (line && *line) {
790+
const char *nl = strchr(line, '\n');
791+
size_t line_len = nl ? (size_t)(nl - line) : strlen(line);
792+
ASSERT_LT((int)line_len, 80);
793+
line = nl ? nl + 1 : NULL;
794+
}
795+
free(help);
796+
PASS();
797+
}
798+
765799
TEST(mcp_tools_list_latest_metadata) {
766800
char *json = cbm_mcp_tools_list();
767801
ASSERT_NOT_NULL(json);
@@ -9517,6 +9551,7 @@ SUITE(mcp) {
95179551
/* MCP protocol helpers */
95189552
RUN_TEST(mcp_initialize_response);
95199553
RUN_TEST(mcp_tools_list);
9554+
RUN_TEST(mcp_tools_help_list_matches_registry);
95209555
RUN_TEST(mcp_tools_list_latest_metadata);
95219556
RUN_TEST(mcp_tools_have_behavior_annotations);
95229557
RUN_TEST(mcp_index_repository_declares_name_override_issue571);

0 commit comments

Comments
 (0)