transport_sse.cpp¶
src/mcp/transport_sse.cpp
@file transport_sse.cpp @brief SSETransport implementation.
Owners: @tvanfossen
handle_sse_data
void entropic::SSETransport::handle_sse_data(const std::string &data)
Process a single SSE data line (JSON-RPC response).
src/mcp/transport_sse.cpp:323
Source · 20 lines
void SSETransport::handle_sse_data(const std::string& data) {
if (current_event_type_ == "endpoint") {
handle_endpoint_event(data);
current_event_type_.clear();
return;
}
current_event_type_.clear();
try {
auto j = nlohmann::json::parse(data);
int id = j.value("id", 0);
if (id > 0) {
std::lock_guard<std::mutex> lock(pending_mutex_);
pending_responses_[id] = data;
pending_cv_.notify_all();
}
} catch (const nlohmann::json::exception& e) {
logger->warn("Failed to parse SSE data: {}", e.what());
}
}Activity · 6 steps
Flows
process_sse_line
void entropic::SSETransport::process_sse_line(const std::string &line)
Process a single SSE line (event: or data: prefix).
src/mcp/transport_sse.cpp:303
Source · 13 lines
void SSETransport::process_sse_line(const std::string& line) {
// Strip trailing \r
std::string clean = line;
if (!clean.empty() && clean.back() == '\r') {
clean.pop_back();
}
if (clean.substr(0, 6) == "data: ") {
handle_sse_data(clean.substr(6));
} else if (clean.substr(0, 7) == "event: ") {
current_event_type_ = clean.substr(7);
}
}Activity · 8 steps
Flows
send_request
std::string entropic::SSETransport::send_request(const std::string &request_json, uint32_t timeout_ms=0) override
POST a JSON-RPC request and wait for matching response.
src/mcp/transport_sse.cpp:236
Source · 18 lines
std::string SSETransport::send_request(
const std::string& request_json,
uint32_t timeout_ms) {
logger->info("SSE request: {} bytes", request_json.size());
int request_id = 0;
bool ready = connected_ && !message_endpoint_.empty() &&
parse_request_id(request_json, request_id) &&
post_request(request_json);
if (!ready) {
return "";
}
uint32_t actual_timeout = timeout_ms > 0
? timeout_ms : default_timeout_ms_;
return await_response(request_id, actual_timeout);
}Activity · 7 steps
Requirements
sse_reader_loop
void entropic::SSETransport::sse_reader_loop()
SSE stream reader loop (runs on background thread).
src/mcp/transport_sse.cpp:270
Source · 26 lines
void SSETransport::sse_reader_loop() {
auto content_receiver = [this](
const char* data, size_t len) -> bool {
std::string chunk(data, len);
// Parse SSE events from chunk
std::istringstream stream(chunk);
std::string line;
while (std::getline(stream, line)) {
if (line.empty() || line == "\r") {
continue;
}
process_sse_line(line);
}
return running_.load();
};
auto res = client_->Get(
sse_path_,
httplib::Headers{{"Accept", "text/event-stream"}},
content_receiver);
connected_ = false;
if (running_) {
logger->warn("SSE stream disconnected from {}", url_);
}
}