Skip to content

Commit 4071877

Browse files
authored
Merge pull request #980 from dpersek/fix/tools-list-pagination-971
Fix tools/list default pagination for non-paginating clients
2 parents a6207c6 + 8a4bef2 commit 4071877

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

src/mcp/mcp.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,10 @@ const char *cbm_mcp_tool_input_schema(const char *tool_name) {
621621
return NULL;
622622
}
623623

624-
static int mcp_tools_cursor_offset(const char *params_json) {
624+
static int mcp_tools_cursor_offset(const char *params_json, bool *has_cursor_out) {
625+
if (has_cursor_out) {
626+
*has_cursor_out = false;
627+
}
625628
if (!params_json) {
626629
return 0;
627630
}
@@ -635,6 +638,9 @@ static int mcp_tools_cursor_offset(const char *params_json) {
635638
yyjson_val *root = yyjson_doc_get_root(doc);
636639
yyjson_val *cursor = root ? yyjson_obj_get(root, "cursor") : NULL;
637640
if (cursor) {
641+
if (has_cursor_out) {
642+
*has_cursor_out = true;
643+
}
638644
offset = TOOL_COUNT;
639645
if (yyjson_is_str(cursor)) {
640646
const char *cursor_str = yyjson_get_str(cursor);
@@ -654,8 +660,12 @@ static int mcp_tools_cursor_offset(const char *params_json) {
654660
}
655661

656662
static char *cbm_mcp_tools_list_page(const char *params_json) {
657-
return cbm_mcp_tools_list_range(mcp_tools_cursor_offset(params_json), MCP_TOOLS_PAGE_SIZE,
658-
true);
663+
bool has_cursor = false;
664+
int offset = mcp_tools_cursor_offset(params_json, &has_cursor);
665+
if (!has_cursor) {
666+
return cbm_mcp_tools_list();
667+
}
668+
return cbm_mcp_tools_list_range(offset, MCP_TOOLS_PAGE_SIZE, true);
659669
}
660670

661671
/* Supported protocol versions, newest first. The server picks the newest

tests/test_mcp.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -586,16 +586,26 @@ TEST(server_handle_tools_list) {
586586
PASS();
587587
}
588588

589-
TEST(server_handle_tools_list_paginates) {
589+
TEST(server_handle_tools_list_defaults_to_all_tools_and_accepts_cursor) {
590590
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
591591

592592
char *resp =
593593
cbm_mcp_server_handle(srv, "{\"jsonrpc\":\"2.0\",\"id\":200,\"method\":\"tools/list\"}");
594594
ASSERT_NOT_NULL(resp);
595595
ASSERT_NOT_NULL(strstr(resp, "\"id\":200"));
596-
ASSERT_NOT_NULL(strstr(resp, "\"nextCursor\":\"8\""));
596+
ASSERT_NULL(strstr(resp, "\"nextCursor\""));
597597
ASSERT_NOT_NULL(strstr(resp, "index_repository"));
598-
ASSERT_NULL(strstr(resp, "manage_adr"));
598+
ASSERT_NOT_NULL(strstr(resp, "manage_adr"));
599+
ASSERT_NOT_NULL(strstr(resp, "ingest_traces"));
600+
free(resp);
601+
602+
resp = cbm_mcp_server_handle(
603+
srv, "{\"jsonrpc\":\"2.0\",\"id\":202,\"method\":\"tools/list\",\"params\":{}}");
604+
ASSERT_NOT_NULL(resp);
605+
ASSERT_NOT_NULL(strstr(resp, "\"id\":202"));
606+
ASSERT_NULL(strstr(resp, "\"nextCursor\""));
607+
ASSERT_NOT_NULL(strstr(resp, "manage_adr"));
608+
ASSERT_NOT_NULL(strstr(resp, "ingest_traces"));
599609
free(resp);
600610

601611
resp = cbm_mcp_server_handle(
@@ -5238,7 +5248,7 @@ SUITE(mcp) {
52385248
RUN_TEST(server_handle_initialize);
52395249
RUN_TEST(server_handle_initialized_notification);
52405250
RUN_TEST(server_handle_tools_list);
5241-
RUN_TEST(server_handle_tools_list_paginates);
5251+
RUN_TEST(server_handle_tools_list_defaults_to_all_tools_and_accepts_cursor);
52425252
RUN_TEST(server_handle_logs_request_without_params);
52435253
RUN_TEST(server_handle_unknown_method);
52445254

0 commit comments

Comments
 (0)