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

Function build_snippet_response

src/mcp/mcp.c:8470–8590  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8468}
8469
8470static char *build_snippet_response(cbm_mcp_server_t *srv, cbm_node_t *node,
8471 const char *match_method, bool include_neighbors,
8472 cbm_node_t *alternatives, int alt_count) {
8473 char *root_path = get_project_root(srv, node->project);
8474
8475 int start = node->start_line > 0 ? node->start_line : SKIP_ONE;
8476 int end = node->end_line > start ? node->end_line : start + SNIPPET_DEFAULT_LINES;
8477 /* Context-bomb guard: a structural node (Module/File) spans its whole file,
8478 * so an unclipped read returned the ENTIRE source — a field-eval agent that
8479 * fell back to a Module snippet pulled 400KB in one call. Cap the line span
8480 * (far above any real function) and flag it; the exact range is still in
8481 * start_line/end_line for a targeted re-read. */
8482 bool snippet_clipped = false;
8483 if (end - start + 1 > MCP_SNIPPET_MAX_LINES) {
8484 end = start + MCP_SNIPPET_MAX_LINES - 1;
8485 snippet_clipped = true;
8486 }
8487 char *abs_path = NULL;
8488 char *source = resolve_snippet_source(root_path, node->file_path, start, end, &abs_path);
8489
8490 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL);
8491 yyjson_mut_val *root_obj = yyjson_mut_obj(doc);
8492 yyjson_mut_doc_set_root(doc, root_obj);
8493
8494 yyjson_mut_obj_add_str(doc, root_obj, "name", node->name ? node->name : "");
8495 yyjson_mut_obj_add_str(doc, root_obj, "qualified_name",
8496 node->qualified_name ? node->qualified_name : "");
8497 yyjson_mut_obj_add_str(doc, root_obj, "label", node->label ? node->label : "");
8498
8499 const char *display_path = "";
8500 if (abs_path) {
8501 display_path = abs_path;
8502 } else if (node->file_path) {
8503 display_path = node->file_path;
8504 }
8505 yyjson_mut_obj_add_str(doc, root_obj, "file_path", display_path);
8506 yyjson_mut_obj_add_int(doc, root_obj, "start_line", start);
8507 yyjson_mut_obj_add_int(doc, root_obj, "end_line", end);
8508 if (snippet_clipped) {
8509 yyjson_mut_obj_add_bool(doc, root_obj, "source_clipped", true);
8510 yyjson_mut_obj_add_int(doc, root_obj, "clipped_at_lines", MCP_SNIPPET_MAX_LINES);
8511 }
8512
8513 if (source) {
8514 char *safe_source = sanitize_utf8_lossy(source);
8515 if (safe_source) {
8516 yyjson_mut_obj_add_strcpy(doc, root_obj, "source", safe_source);
8517 free(safe_source);
8518 } else {
8519 yyjson_mut_obj_add_str(doc, root_obj, "source", "(source not available)");
8520 }
8521 } else {
8522 yyjson_mut_obj_add_str(doc, root_obj, "source", "(source not available)");
8523 }
8524
8525 /* match_method — omitted for exact matches */
8526 if (match_method) {
8527 yyjson_mut_obj_add_str(doc, root_obj, "match_method", match_method);

Callers 1

handle_get_code_snippetFunction · 0.85

Calls 9

get_project_rootFunction · 0.85
resolve_snippet_sourceFunction · 0.85
sanitize_utf8_lossyFunction · 0.85
cbm_store_node_degreeFunction · 0.85
add_string_arrayFunction · 0.85
yy_doc_to_strFunction · 0.85
cbm_mcp_text_resultFunction · 0.85

Tested by

no test coverage detected