tool_result_classify.cpp

src/mcp/tool_result_classify.cpp

@file tool_result_classify.cpp @brief Implementation of the tool-result content classifiers (#44).

Owners: @tvanfossen

is_effectively_empty

bool entropic::mcp::is_effectively_empty(std::string_view s)

True if s is empty or contains only ASCII whitespace.

src/mcp/tool_result_classify.cpp:61

Show on the map →

Called by SCENARIO, classify_tool_result

Calls none

Source · 6 lines
bool is_effectively_empty(std::string_view s) {
    for (unsigned char c : s) {
        if (std::isspace(c) == 0) { return false; }
    }
    return true;
}
Activity · 4 steps
lines 62–62◇ for (unsigned char c : s)lines 63–63◇ if (std::isspace(c) == 0)lines 63–63return false;lines 65–65return true;

Requirements

REQ-MCP-019

looks_like_tool_error

bool entropic::mcp::looks_like_tool_error(std::string_view content)

Heuristic: the rendered tool-result text reads as an error.

src/mcp/tool_result_classify.cpp:127

Source · 6 lines
bool looks_like_tool_error(std::string_view content) {
    auto trimmed = ltrim(content);
    return starts_with_ci(trimmed, "error")
        || starts_with_ci(trimmed, "[error]")
        || starts_with_json_error(trimmed);
}
Activity · 2 steps
lines 128–128auto trimmed = ltrim(content);lines 129–131return starts_with_ci(trimmed, "error") || starts_with_ci(trimmed, "[error]") || starts_with_json_error(trimmed);

Requirements

REQ-MCP-019

starts_with_json_error

bool entropic::mcp::starts_with_json_error(std::string_view trimmed)

True if input begins with a JSON top-level "error" key.

src/mcp/tool_result_classify.cpp:80

Source · 5 lines
inline bool starts_with_json_error(std::string_view trimmed) {
    if (trimmed.empty() || trimmed.front() != '{') { return false; }
    auto pos = trimmed.find("\"error\"");
    return pos != std::string_view::npos && pos < 32;
}
Activity · 4 steps
lines 81–81◇ if (trimmed.empty() || trimmed.front() != '{')lines 81–81return false;lines 82–82auto pos = trimmed.find("\"error\"");lines 83–83return pos != std::string_view::npos && pos < 32;

Requirements

REQ-MCP-019

truncate_to_cap

void entropic::mcp::truncate_to_cap(std::string &content, int cap)

Truncate tool-result content in place when it exceeds cap.

src/mcp/tool_result_classify.cpp:102

Show on the map →

Called by SCENARIO, apply_result_size_cap

Calls none

Source · 12 lines
void truncate_to_cap(std::string& content, int cap) {
    if (cap <= 0) { return; }
    auto cap_sz = static_cast<size_t>(cap);
    if (content.size() <= cap_sz) { return; }
    size_t lost = content.size() - cap_sz;
    std::string tail = "\n[... truncated, "
        + std::to_string(lost) + " more bytes]";
    size_t keep = (cap_sz > tail.size())
        ? cap_sz - tail.size() : 0;
    content.resize(keep);
    content += tail;
}
Activity · 10 steps
lines 103–103◇ if (cap <= 0)lines 103–103return;lines 104–104auto cap_sz = static_cast<size_t>(cap);lines 105–105◇ if (content.size() <= cap_sz)lines 105–105return;lines 106–106size_t lost = content.size() - cap_sz;lines 107–108std::string tail = "\n[... truncated, " + std::to_string(lost) + " more bytes]";lines 109–110size_t keep = (cap_sz > tail.size()) ? cap_sz - tail.size() : 0;lines 111–111content.resize(keep);lines 112–112content += tail;

Requirements

REQ-MCP-019