tool_call_history.cpp¶
src/mcp/tool_call_history.cpp
@file tool_call_history.cpp @brief ToolCallHistory ring buffer implementation.
Owners: @tvanfossen
ToolCallHistory
entropic::ToolCallHistory::ToolCallHistory(size_t capacity=100)
Construct with buffer capacity.
src/mcp/tool_call_history.cpp:30
Source · 5 lines
ToolCallHistory::ToolCallHistory(size_t capacity)
: capacity_(capacity) {
buffer_.resize(capacity);
logger->info("ToolCallHistory initialized (capacity={})", capacity);
}Activity · 2 steps
Requirements
all
std::vector< ToolCallRecord > entropic::ToolCallHistory::all() const
Get all stored entries (oldest first, insertion order).
src/mcp/tool_call_history.cpp:97
Source · 13 lines
std::vector<ToolCallRecord> ToolCallHistory::all() const {
std::shared_lock lock(mutex_);
std::vector<ToolCallRecord> result;
result.reserve(count_);
size_t start = (count_ < capacity_) ? 0
: head_;
for (size_t i = 0; i < count_; ++i) {
size_t idx = (start + i) % capacity_;
result.push_back(buffer_[idx]);
}
return result;
}Activity · 8 steps
Requirements
recent
std::vector< ToolCallRecord > entropic::ToolCallHistory::recent(size_t count) const
Get the N most recent entries (newest first).
src/mcp/tool_call_history.cpp:72
Source · 12 lines
std::vector<ToolCallRecord> ToolCallHistory::recent(size_t count) const {
std::shared_lock lock(mutex_);
size_t n = std::min(count, count_);
std::vector<ToolCallRecord> result;
result.reserve(n);
for (size_t i = 0; i < n; ++i) {
size_t idx = (head_ + capacity_ - 1 - i) % capacity_;
result.push_back(buffer_[idx]);
}
return result;
}Activity · 8 steps
Requirements
record
void entropic::ToolCallHistory::record(const ToolCallRecord &entry)
Record a completed tool call.
src/mcp/tool_call_history.cpp:49
Source · 10 lines
void ToolCallHistory::record(const ToolCallRecord& entry) {
std::unique_lock lock(mutex_);
buffer_[head_] = entry;
head_ = (head_ + 1) % capacity_;
if (count_ < capacity_) {
++count_;
}
logger->info("Recorded: tool='{}', {}/{} slots",
entry.tool_name, count_, capacity_);
}Activity · 6 steps
Requirements
record_to_json
static nlohmann::json entropic::record_to_json(const ToolCallRecord &rec)
Serialize a single ToolCallRecord to JSON.
src/mcp/tool_call_history.cpp:120
Source · 14 lines
static nlohmann::json record_to_json(const ToolCallRecord& rec) {
nlohmann::json j;
j["sequence"] = rec.sequence;
j["tool_name"] = rec.tool_name;
j["params_summary"] = rec.params_summary;
j["status"] = rec.status;
j["result_summary"] = rec.result_summary;
j["elapsed_ms"] = rec.elapsed_ms;
j["iteration"] = rec.iteration;
if (!rec.error_detail.empty()) {
j["error_detail"] = rec.error_detail;
}
return j;
}Activity · 11 steps
Requirements
size
size_t entropic::ToolCallHistory::size() const
Current number of stored entries.
src/mcp/tool_call_history.cpp:163
Source · 4 lines
size_t ToolCallHistory::size() const {
std::shared_lock lock(mutex_);
return count_;
}Activity · 2 steps
Requirements
summarize_params
std::string entropic::summarize_params(const std::string &args_json)
Extract top-level JSON keys as a comma-separated summary.
src/mcp/tool_call_history.cpp:179
Source · 18 lines
std::string summarize_params(const std::string& args_json) {
try {
auto j = nlohmann::json::parse(args_json);
if (!j.is_object()) {
return args_json;
}
std::string result;
for (auto it = j.begin(); it != j.end(); ++it) {
if (!result.empty()) {
result += ", ";
}
result += it.key();
}
return result;
} catch (...) {
return args_json;
}
}Activity · 1 steps
Requirements
to_json
std::string entropic::ToolCallHistory::to_json(size_t count) const
Serialize recent entries to JSON array string.
src/mcp/tool_call_history.cpp:144
Called by SCENARIO, SCENARIO, TEST_CASE, tool_history_json_thunk
Calls all, recent, record_to_json
Source · 11 lines
std::string ToolCallHistory::to_json(size_t count) const {
auto entries = (count == 0) ? all() : recent(count);
nlohmann::json arr = nlohmann::json::array();
for (const auto& rec : entries) {
arr.push_back(record_to_json(rec));
}
// gh#216: never throw on bad bytes. The history is handle-wide and
// re-serialized every run, so one record that cannot be dumped failed
// every later turn on the host. Invalid UTF-8 becomes U+FFFD.
return arr.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace);
}Activity · 5 steps
Requirements
truncate_result
std::string entropic::truncate_result(const std::string &text, size_t max_len)
Truncate a string to max_len characters with "..." suffix.
src/mcp/tool_call_history.cpp:215
Source · 10 lines
std::string truncate_result(const std::string& text, size_t max_len) {
if (text.size() <= max_len) {
return text;
}
size_t cut = max_len;
while (cut > 0 && (static_cast<unsigned char>(text[cut]) & 0xC0) == 0x80) {
--cut;
}
return text.substr(0, cut) + "...";
}