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

Function cbm_jsonrpc_parse

src/mcp/mcp.c:241–287  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

239 * ══════════════════════════════════════════════════════════════════ */
240
241int cbm_jsonrpc_parse(const char *line, cbm_jsonrpc_request_t *out) {
242 memset(out, 0, sizeof(*out));
243 out->id = CBM_NOT_FOUND;
244
245 yyjson_doc *doc = yyjson_read(line, strlen(line), 0);
246 if (!doc) {
247 return CBM_NOT_FOUND;
248 }
249
250 yyjson_val *root = yyjson_doc_get_root(doc);
251 if (!yyjson_is_obj(root)) {
252 yyjson_doc_free(doc);
253 return CBM_NOT_FOUND;
254 }
255
256 yyjson_val *v_jsonrpc = yyjson_obj_get(root, "jsonrpc");
257 yyjson_val *v_method = yyjson_obj_get(root, "method");
258 yyjson_val *v_id = yyjson_obj_get(root, "id");
259 yyjson_val *v_params = yyjson_obj_get(root, "params");
260
261 if (!v_method || !yyjson_is_str(v_method)) {
262 yyjson_doc_free(doc);
263 return CBM_NOT_FOUND;
264 }
265
266 out->jsonrpc =
267 heap_strdup(v_jsonrpc && yyjson_is_str(v_jsonrpc) ? yyjson_get_str(v_jsonrpc) : "2.0");
268 out->method = heap_strdup(yyjson_get_str(v_method));
269
270 if (v_id) {
271 out->has_id = true;
272 if (yyjson_is_int(v_id)) {
273 out->id = yyjson_get_int(v_id);
274 } else if (yyjson_is_str(v_id)) {
275 /* JSON-RPC 2.0 §4 permits string ids (Claude Desktop uses them).
276 * Preserve verbatim instead of coercing via strtol (issue #253). */
277 out->id_str = heap_strdup(yyjson_get_str(v_id));
278 }
279 }
280
281 if (v_params) {
282 out->params_raw = yyjson_val_write(v_params, 0, NULL);
283 }
284
285 yyjson_doc_free(doc);
286 return 0;
287}
288
289void cbm_jsonrpc_request_free(cbm_jsonrpc_request_t *r) {
290 if (!r) {

Callers 6

cbm_mcp_server_handleFunction · 0.85
application_mcp_requestFunction · 0.85
frontend_enqueueFunction · 0.85
test_mcp.cFile · 0.85

Calls 1

heap_strdupFunction · 0.70

Tested by

no test coverage detected