|
32 | 32 | MCP_URI_PREFIX = 7, /* strlen("file://") */ |
33 | 33 | MCP_CONTENT_PREFIX = 15, /* strlen("Content-Length:") */ |
34 | 34 | MCP_RETURN_2 = 2, |
| 35 | + MCP_TOOLS_PAGE_SIZE = 8, |
35 | 36 | }; |
36 | 37 | #define MCP_MS_TO_US 1000LL |
37 | 38 | #define MCP_S_TO_US 1000000LL |
@@ -493,38 +494,111 @@ static const tool_def_t TOOLS[] = { |
493 | 494 |
|
494 | 495 | static const int TOOL_COUNT = sizeof(TOOLS) / sizeof(TOOLS[0]); |
495 | 496 |
|
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) { |
497 | 524 | yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); |
498 | 525 | yyjson_mut_val *root = yyjson_mut_obj(doc); |
499 | 526 | yyjson_mut_doc_set_root(doc, root); |
500 | 527 |
|
501 | 528 | yyjson_mut_val *tools = yyjson_mut_arr(doc); |
502 | 529 |
|
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 | + } |
508 | 539 |
|
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 | + } |
517 | 544 |
|
518 | | - yyjson_mut_arr_add_val(tools, tool); |
| 545 | + for (int i = offset; i < end; i++) { |
| 546 | + mcp_add_tool_def(doc, tools, i); |
519 | 547 | } |
520 | 548 |
|
521 | 549 | 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 | + } |
522 | 555 |
|
523 | 556 | char *out = yy_doc_to_str(doc); |
524 | 557 | yyjson_mut_doc_free(doc); |
525 | 558 | return out; |
526 | 559 | } |
527 | 560 |
|
| 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 | + |
528 | 602 | /* Supported protocol versions, newest first. The server picks the newest |
529 | 603 | * version that it shares with the client (per MCP spec version negotiation). */ |
530 | 604 | static const char *SUPPORTED_PROTOCOL_VERSIONS[] = { |
@@ -4879,7 +4953,7 @@ char *cbm_mcp_server_handle(cbm_mcp_server_t *srv, const char *line) { |
4879 | 4953 | } else if (strcmp(req.method, "ping") == 0) { |
4880 | 4954 | result_json = heap_strdup("{}"); |
4881 | 4955 | } 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); |
4883 | 4957 | } else if (strcmp(req.method, "tools/call") == 0) { |
4884 | 4958 | char *tool_name = req.params_raw ? cbm_mcp_get_tool_name(req.params_raw) : NULL; |
4885 | 4959 | char *tool_args = |
|
0 commit comments