MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cbm_jsonrpc_format_response

Function cbm_jsonrpc_format_response

src/mcp/mcp.c:217–253  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

215 * ══════════════════════════════════════════════════════════════════ */
216
217char *cbm_jsonrpc_format_response(const cbm_jsonrpc_response_t *resp) {
218 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
219 yyjson_mut_val *root = yyjson_mut_obj(doc);
220 yyjson_mut_doc_set_root(doc, root);
221
222 yyjson_mut_obj_add_str(doc, root, "jsonrpc", "2.0");
223 if (resp->id_str) {
224 yyjson_mut_obj_add_str(doc, root, "id", resp->id_str);
225 } else {
226 yyjson_mut_obj_add_int(doc, root, "id", resp->id);
227 }
228
229 if (resp->error_json) {
230 /* Parse the error JSON and embed */
231 yyjson_doc *err_doc = yyjson_read(resp->error_json, strlen(resp->error_json), 0);
232 if (err_doc) {
233 yyjson_mut_val *err_val = yyjson_val_mut_copy(doc, yyjson_doc_get_root(err_doc));
234 yyjson_mut_obj_add_val(doc, root, "error", err_val);
235 yyjson_doc_free(err_doc);
236 }
237 } else if (resp->result_json) {
238 /* Parse the result JSON and embed */
239 yyjson_doc *res_doc = yyjson_read(resp->result_json, strlen(resp->result_json), 0);
240 if (res_doc) {
241 yyjson_mut_val *res_val = yyjson_val_mut_copy(doc, yyjson_doc_get_root(res_doc));
242 yyjson_mut_obj_add_val(doc, root, "result", res_val);
243 yyjson_doc_free(res_doc);
244 }
245 } else {
246 /* JSON-RPC 2.0 spec: response MUST contain "result" or "error" */
247 yyjson_mut_obj_add_null(doc, root, "result");
248 }
249
250 char *out = yy_doc_to_str(doc);
251 yyjson_mut_doc_free(doc);
252 return out;
253}
254
255char *cbm_jsonrpc_format_error(int64_t id, int code, const char *message) {
256 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);

Callers 4

cbm_mcp_server_handleFunction · 0.85
test_mcp.cFile · 0.85

Calls 1

yy_doc_to_strFunction · 0.85

Tested by

no test coverage detected