transport_stdio.cpp¶
src/mcp/transport_stdio.cpp
@file transport_stdio.cpp @brief StdioTransport implementation.
Owners: @tvanfossen
build_env
std::vector< std::string > entropic::StdioTransport::build_env() const
Build merged environment for child process.
src/mcp/transport_stdio.cpp:510
Source · 24 lines
std::vector<std::string> StdioTransport::build_env() const {
std::map<std::string, std::string> merged;
// Copy parent environment
for (char** ep = environ; ep && *ep; ++ep) {
std::string entry(*ep);
auto eq = entry.find('=');
if (eq != std::string::npos) {
merged[entry.substr(0, eq)] = entry.substr(eq + 1);
}
}
// Apply overrides
for (const auto& [key, val] : env_) {
merged[key] = val;
}
std::vector<std::string> result;
result.reserve(merged.size());
for (const auto& [key, val] : merged) {
result.push_back(key + "=" + val);
}
return result;
}Activity · 13 steps
Requirements
clear_interrupt
void entropic::StdioTransport::clear_interrupt() override
Release the cancel flag so this transport works again.
src/mcp/transport_stdio.cpp:370
Source · 3 lines
void StdioTransport::clear_interrupt() {
cancel_flag_.store(false, std::memory_order_release);
}Activity · 1 steps
Requirements
drain_orphaned_responses
int entropic::StdioTransport::drain_orphaned_responses()
Discard replies left over from an abandoned request (gh#158).
src/mcp/transport_stdio.cpp:303
Source · 15 lines
int StdioTransport::drain_orphaned_responses() {
constexpr int kMaxLines = 64;
constexpr size_t kMaxBytes = 1u << 20; // 1 MiB
int lines = 0;
size_t bytes = 0;
while (lines < kMaxLines && bytes < kMaxBytes) {
struct pollfd pfd{stdout_fd_, POLLIN, 0};
if (::poll(&pfd, 1, 0) <= 0) { break; }
char ch = 0;
if (::read(stdout_fd_, &ch, 1) <= 0) { break; }
++bytes;
if (ch == '\n') { ++lines; }
}
return lines;
}Activity · 15 steps
Requirements
interrupt
void entropic::StdioTransport::interrupt() override
Abort any blocking read and mark transport uncancellable.
src/mcp/transport_stdio.cpp:357
Source · 3 lines
void StdioTransport::interrupt() {
cancel_flag_.store(true, std::memory_order_release);
}Activity · 1 steps
Requirements
is_interrupted
bool entropic::StdioTransport::is_interrupted() const override
Whether an interrupt is currently short-circuiting calls.
src/mcp/transport_stdio.cpp:380
Source · 3 lines
bool StdioTransport::is_interrupted() const {
return cancel_flag_.load(std::memory_order_acquire);
}Activity · 1 steps
Requirements
sanitize_display_name
static std::string entropic::sanitize_display_name(const std::string &raw)
Sanitize a display_name for use as a log-line bracket label.
src/mcp/transport_stdio.cpp:64
Source · 11 lines
static std::string sanitize_display_name(const std::string& raw) {
std::string out;
out.reserve(raw.size());
for (char c : raw) {
if (out.empty() && c == '/') { continue; }
if (c == '[' || c == ']') { continue; }
if (static_cast<unsigned char>(c) < 0x20 || c == 0x7f) { continue; }
out.push_back(c);
}
return out.empty() ? std::string{"server"} : out;
}Activity · 11 steps
Requirements
send_request
std::string entropic::StdioTransport::send_request(const std::string &request_json, uint32_t timeout_ms=0) override
Send JSON-RPC request via stdin, read response from stdout.
src/mcp/transport_stdio.cpp:258
Source · 37 lines
std::string StdioTransport::send_request(
const std::string& request_json,
uint32_t timeout_ms) {
if (!connected_ || request_cancelled()) {
return "";
}
uint32_t actual_timeout = timeout_ms > 0
? timeout_ms : default_timeout_ms_;
std::lock_guard<std::mutex> lock(io_mutex_);
if (desynced_) {
int dropped = drain_orphaned_responses();
desynced_ = false;
logger->warn("Discarded {} orphaned response line(s) from '{}' "
"before sending the next request", dropped,
display_name_);
}
std::string msg = request_json + "\n";
ssize_t written = ::write(stdin_fd_, msg.data(), msg.size());
if (written < 0 || static_cast<size_t>(written) != msg.size()) {
logger->error("Write to child stdin failed: {}",
strerror(errno));
connected_ = false;
return "";
}
// gh#158: the request is on the wire. Anything other than a complete
// line back means we walked away from a reply the server will still
// send, so mark the pipe desynced and let the next caller drain it.
auto response = read_line(stdout_fd_, actual_timeout);
desynced_ = response.empty();
return response;
}