Extract text content from MCP tool result envelope and print it. * MCP results: {"content":[{"type":"text","text":"..."}],"isError":...} * Returns 1 if the result was an error, 0 otherwise. */
| 709 | * MCP results: {"content":[{"type":"text","text":"..."}],"isError":...} |
| 710 | * Returns 1 if the result was an error, 0 otherwise. */ |
| 711 | static int cli_print_mcp_result(const char *result) { |
| 712 | yyjson_doc *doc = yyjson_read(result, strlen(result), 0); |
| 713 | if (!doc) { |
| 714 | printf("%s\n", result); |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | yyjson_val *root = yyjson_doc_get_root(doc); |
| 719 | yyjson_val *err_val = yyjson_obj_get(root, "isError"); |
| 720 | bool is_error = err_val && yyjson_get_bool(err_val); |
| 721 | |
| 722 | const char *text = NULL; |
| 723 | yyjson_val *content = yyjson_obj_get(root, "content"); |
| 724 | if (yyjson_is_arr(content) && yyjson_arr_size(content) > 0) { |
| 725 | yyjson_val *tv = yyjson_obj_get(yyjson_arr_get_first(content), "text"); |
| 726 | text = tv ? yyjson_get_str(tv) : NULL; |
| 727 | } |
| 728 | |
| 729 | if (text) { |
| 730 | (void)fprintf(is_error ? stderr : stdout, "%s\n", text); |
| 731 | } else { |
| 732 | printf("%s\n", result); |
| 733 | } |
| 734 | |
| 735 | yyjson_doc_free(doc); |
| 736 | return is_error ? SKIP_ONE : 0; |
| 737 | } |
| 738 | |
| 739 | /* Strip a flag from argv, returning true if found. */ |
| 740 | static bool cli_strip_flag(int *argc, char **argv, const char *flag) { |