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
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
Requirements
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
Called by SCENARIO, SCENARIO, classify_tool_result, record_tool_call, record_tool_history
Calls ltrim, starts_with_ci, starts_with_json_error
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
Requirements
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
Requirements
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
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;
}