Skip to content

Commit 0d3e24c

Browse files
committed
feat(mcp): add tool schemas and pagination
Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 800476f commit 0d3e24c

2 files changed

Lines changed: 118 additions & 16 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 90 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ enum {
3232
MCP_URI_PREFIX = 7, /* strlen("file://") */
3333
MCP_CONTENT_PREFIX = 15, /* strlen("Content-Length:") */
3434
MCP_RETURN_2 = 2,
35+
MCP_TOOLS_PAGE_SIZE = 8,
3536
};
3637
#define MCP_MS_TO_US 1000LL
3738
#define MCP_S_TO_US 1000000LL
@@ -493,38 +494,111 @@ static const tool_def_t TOOLS[] = {
493494

494495
static const int TOOL_COUNT = sizeof(TOOLS) / sizeof(TOOLS[0]);
495496

496-
char *cbm_mcp_tools_list(void) {
497+
static const char MCP_TOOL_OUTPUT_SCHEMA[] = "{\"type\":\"object\",\"additionalProperties\":true}";
498+
499+
static void mcp_add_json_schema(yyjson_mut_doc *doc, yyjson_mut_val *obj, const char *key,
500+
const char *schema_json) {
501+
yyjson_doc *schema_doc = yyjson_read(schema_json, strlen(schema_json), 0);
502+
if (schema_doc) {
503+
yyjson_mut_val *schema = yyjson_val_mut_copy(doc, yyjson_doc_get_root(schema_doc));
504+
if (schema) {
505+
yyjson_mut_obj_add_val(doc, obj, key, schema);
506+
}
507+
yyjson_doc_free(schema_doc);
508+
}
509+
}
510+
511+
static void mcp_add_tool_def(yyjson_mut_doc *doc, yyjson_mut_val *tools, int i) {
512+
yyjson_mut_val *tool = yyjson_mut_obj(doc);
513+
yyjson_mut_obj_add_str(doc, tool, "name", TOOLS[i].name);
514+
yyjson_mut_obj_add_str(doc, tool, "title", TOOLS[i].title);
515+
yyjson_mut_obj_add_str(doc, tool, "description", TOOLS[i].description);
516+
517+
mcp_add_json_schema(doc, tool, "inputSchema", TOOLS[i].input_schema);
518+
mcp_add_json_schema(doc, tool, "outputSchema", MCP_TOOL_OUTPUT_SCHEMA);
519+
520+
yyjson_mut_arr_add_val(tools, tool);
521+
}
522+
523+
static char *cbm_mcp_tools_list_range(int offset, int limit, bool include_next_cursor) {
497524
yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
498525
yyjson_mut_val *root = yyjson_mut_obj(doc);
499526
yyjson_mut_doc_set_root(doc, root);
500527

501528
yyjson_mut_val *tools = yyjson_mut_arr(doc);
502529

503-
for (int i = 0; i < TOOL_COUNT; i++) {
504-
yyjson_mut_val *tool = yyjson_mut_obj(doc);
505-
yyjson_mut_obj_add_str(doc, tool, "name", TOOLS[i].name);
506-
yyjson_mut_obj_add_str(doc, tool, "title", TOOLS[i].title);
507-
yyjson_mut_obj_add_str(doc, tool, "description", TOOLS[i].description);
530+
if (offset < 0) {
531+
offset = 0;
532+
}
533+
if (offset > TOOL_COUNT) {
534+
offset = TOOL_COUNT;
535+
}
536+
if (limit < 0 || limit > TOOL_COUNT) {
537+
limit = TOOL_COUNT;
538+
}
508539

509-
/* Parse input schema JSON and embed */
510-
yyjson_doc *schema_doc =
511-
yyjson_read(TOOLS[i].input_schema, strlen(TOOLS[i].input_schema), 0);
512-
if (schema_doc) {
513-
yyjson_mut_val *schema = yyjson_val_mut_copy(doc, yyjson_doc_get_root(schema_doc));
514-
yyjson_mut_obj_add_val(doc, tool, "inputSchema", schema);
515-
yyjson_doc_free(schema_doc);
516-
}
540+
int end = offset + limit;
541+
if (end > TOOL_COUNT) {
542+
end = TOOL_COUNT;
543+
}
517544

518-
yyjson_mut_arr_add_val(tools, tool);
545+
for (int i = offset; i < end; i++) {
546+
mcp_add_tool_def(doc, tools, i);
519547
}
520548

521549
yyjson_mut_obj_add_val(doc, root, "tools", tools);
550+
if (include_next_cursor && end < TOOL_COUNT) {
551+
char cursor[32];
552+
snprintf(cursor, sizeof(cursor), "%d", end);
553+
yyjson_mut_obj_add_strcpy(doc, root, "nextCursor", cursor);
554+
}
522555

523556
char *out = yy_doc_to_str(doc);
524557
yyjson_mut_doc_free(doc);
525558
return out;
526559
}
527560

561+
char *cbm_mcp_tools_list(void) {
562+
return cbm_mcp_tools_list_range(0, TOOL_COUNT, false);
563+
}
564+
565+
static int mcp_tools_cursor_offset(const char *params_json) {
566+
if (!params_json) {
567+
return 0;
568+
}
569+
570+
yyjson_doc *doc = yyjson_read(params_json, strlen(params_json), 0);
571+
if (!doc) {
572+
return 0;
573+
}
574+
575+
int offset = 0;
576+
yyjson_val *root = yyjson_doc_get_root(doc);
577+
yyjson_val *cursor = root ? yyjson_obj_get(root, "cursor") : NULL;
578+
if (cursor) {
579+
offset = TOOL_COUNT;
580+
if (yyjson_is_str(cursor)) {
581+
const char *cursor_str = yyjson_get_str(cursor);
582+
if (cursor_str && *cursor_str != '\0') {
583+
char *endptr = NULL;
584+
errno = 0;
585+
long parsed = strtol(cursor_str, &endptr, 10);
586+
if (endptr && *endptr == '\0' && errno == 0 && parsed >= 0) {
587+
offset = parsed > TOOL_COUNT ? TOOL_COUNT : (int)parsed;
588+
}
589+
}
590+
}
591+
}
592+
593+
yyjson_doc_free(doc);
594+
return offset;
595+
}
596+
597+
static char *cbm_mcp_tools_list_page(const char *params_json) {
598+
return cbm_mcp_tools_list_range(mcp_tools_cursor_offset(params_json), MCP_TOOLS_PAGE_SIZE,
599+
true);
600+
}
601+
528602
/* Supported protocol versions, newest first. The server picks the newest
529603
* version that it shares with the client (per MCP spec version negotiation). */
530604
static const char *SUPPORTED_PROTOCOL_VERSIONS[] = {
@@ -4879,7 +4953,7 @@ char *cbm_mcp_server_handle(cbm_mcp_server_t *srv, const char *line) {
48794953
} else if (strcmp(req.method, "ping") == 0) {
48804954
result_json = heap_strdup("{}");
48814955
} else if (strcmp(req.method, "tools/list") == 0) {
4882-
result_json = cbm_mcp_tools_list();
4956+
result_json = cbm_mcp_tools_list_page(req.params_raw);
48834957
} else if (strcmp(req.method, "tools/call") == 0) {
48844958
char *tool_name = req.params_raw ? cbm_mcp_get_tool_name(req.params_raw) : NULL;
48854959
char *tool_args =

‎tests/test_mcp.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ TEST(mcp_tools_list_latest_metadata) {
194194
ASSERT_NOT_NULL(json);
195195
ASSERT_NOT_NULL(strstr(json, "\"title\":\"Search graph\""));
196196
ASSERT_NOT_NULL(strstr(json, "\"title\":\"Index repository\""));
197+
ASSERT_NOT_NULL(strstr(json, "\"outputSchema\":{\"type\":\"object\""));
198+
ASSERT_NOT_NULL(strstr(json, "\"additionalProperties\":true"));
197199
free(json);
198200
PASS();
199201
}
@@ -379,6 +381,31 @@ TEST(server_handle_tools_list) {
379381
PASS();
380382
}
381383

384+
TEST(server_handle_tools_list_paginates) {
385+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
386+
387+
char *resp =
388+
cbm_mcp_server_handle(srv, "{\"jsonrpc\":\"2.0\",\"id\":200,\"method\":\"tools/list\"}");
389+
ASSERT_NOT_NULL(resp);
390+
ASSERT_NOT_NULL(strstr(resp, "\"id\":200"));
391+
ASSERT_NOT_NULL(strstr(resp, "\"nextCursor\":\"8\""));
392+
ASSERT_NOT_NULL(strstr(resp, "index_repository"));
393+
ASSERT_NULL(strstr(resp, "manage_adr"));
394+
free(resp);
395+
396+
resp = cbm_mcp_server_handle(
397+
srv,
398+
"{\"jsonrpc\":\"2.0\",\"id\":201,\"method\":\"tools/list\",\"params\":{\"cursor\":\"8\"}}");
399+
ASSERT_NOT_NULL(resp);
400+
ASSERT_NOT_NULL(strstr(resp, "\"id\":201"));
401+
ASSERT_NULL(strstr(resp, "\"nextCursor\""));
402+
ASSERT_NOT_NULL(strstr(resp, "manage_adr"));
403+
free(resp);
404+
405+
cbm_mcp_server_free(srv);
406+
PASS();
407+
}
408+
382409
TEST(server_handle_unknown_method) {
383410
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
384411

@@ -2388,6 +2415,7 @@ SUITE(mcp) {
23882415
RUN_TEST(server_handle_initialize);
23892416
RUN_TEST(server_handle_initialized_notification);
23902417
RUN_TEST(server_handle_tools_list);
2418+
RUN_TEST(server_handle_tools_list_paginates);
23912419
RUN_TEST(server_handle_unknown_method);
23922420

23932421
/* Server handle — edge cases */

0 commit comments

Comments
 (0)