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. */
| 210 | * MCP results: {"content":[{"type":"text","text":"..."}],"isError":...} |
| 211 | * Returns 1 if the result was an error, 0 otherwise. */ |
| 212 | static int cli_print_mcp_result(const char *result) { |
| 213 | yyjson_doc *doc = yyjson_read(result, strlen(result), 0); |
| 214 | if (!doc) { |
| 215 | printf("%s\n", result); |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | yyjson_val *root = yyjson_doc_get_root(doc); |
| 220 | yyjson_val *err_val = yyjson_obj_get(root, "isError"); |
| 221 | bool is_error = err_val && yyjson_get_bool(err_val); |
| 222 | |
| 223 | const char *text = NULL; |
| 224 | yyjson_val *content = yyjson_obj_get(root, "content"); |
| 225 | if (yyjson_is_arr(content) && yyjson_arr_size(content) > 0) { |
| 226 | yyjson_val *tv = yyjson_obj_get(yyjson_arr_get_first(content), "text"); |
| 227 | text = tv ? yyjson_get_str(tv) : NULL; |
| 228 | } |
| 229 | |
| 230 | if (text) { |
| 231 | (void)fprintf(is_error ? stderr : stdout, "%s\n", text); |
| 232 | } else { |
| 233 | printf("%s\n", result); |
| 234 | } |
| 235 | |
| 236 | yyjson_doc_free(doc); |
| 237 | return is_error ? SKIP_ONE : 0; |
| 238 | } |
| 239 | |
| 240 | /* Strip a flag from argv, returning true if found. */ |
| 241 | static bool cli_strip_flag(int *argc, char **argv, const char *flag) { |