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

Show on the map →

Called by open_child_process

Calls none

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
lines 511–511std::map<std::string, std::string> merged;lines 514–514◇ for (char** ep = environ; ep && *ep; ++ep)lines 515–515std::string entry(*ep);lines 516–516auto eq = entry.find('=');lines 517–517◇ if (eq != std::string::npos)lines 518–518merged[entry.substr(0, eq)] = entry.substr(eq + 1);lines 523–523◇ for (const auto& [key, val] : env_)lines 524–524merged[key] = val;lines 527–527std::vector<std::string> result;lines 528–528result.reserve(merged.size());lines 529–529◇ for (const auto& [key, val] : merged)lines 530–530result.push_back(key + "=" + val);lines 532–532return result;

Requirements

REQ-MCP-025

clear_interrupt

void entropic::StdioTransport::clear_interrupt() override

Release the cancel flag so this transport works again.

src/mcp/transport_stdio.cpp:370

Show on the map →

Called by SCENARIO

Calls none

Source · 3 lines
void StdioTransport::clear_interrupt() {
    cancel_flag_.store(false, std::memory_order_release);
}
Activity · 1 steps
lines 371–371cancel_flag_.store(false, std::memory_order_release);

Requirements

REQ-MCP-025

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
lines 304–304constexpr int kMaxLines = 64;lines 305–305constexpr size_t kMaxBytes = 1u << 20;lines 306–306int lines = 0;lines 307–307size_t bytes = 0;lines 308–308◇ while (lines < kMaxLines && bytes < kMaxBytes)lines 309–309struct pollfd pfd{stdout_fd_, POLLIN, 0};lines 310–310◇ if (::poll(&pfd, 1, 0) <= 0)lines 310–310break;lines 311–311char ch = 0;lines 312–312◇ if (::read(stdout_fd_, &ch, 1) <= 0)lines 312–312break;lines 313–313++bytes;lines 314–314◇ if (ch == '\n')lines 314–314++lines;lines 316–316return lines;

Requirements

REQ-MCP-026

interrupt

void entropic::StdioTransport::interrupt() override

Abort any blocking read and mark transport uncancellable.

src/mcp/transport_stdio.cpp:357

Show on the map →

Called by SCENARIO

Calls none

Source · 3 lines
void StdioTransport::interrupt() {
    cancel_flag_.store(true, std::memory_order_release);
}
Activity · 1 steps
lines 358–358cancel_flag_.store(true, std::memory_order_release);

Requirements

REQ-MCP-025

is_interrupted

bool entropic::StdioTransport::is_interrupted() const override

Whether an interrupt is currently short-circuiting calls.

src/mcp/transport_stdio.cpp:380

Show on the map →

Called by SCENARIO

Calls none

Source · 3 lines
bool StdioTransport::is_interrupted() const {
    return cancel_flag_.load(std::memory_order_acquire);
}
Activity · 1 steps
lines 381–381return cancel_flag_.load(std::memory_order_acquire);

Requirements

REQ-MCP-025

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

Show on the map →

Called by StdioTransport

Calls none

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
lines 65–65std::string out;lines 66–66out.reserve(raw.size());lines 67–67◇ for (char c : raw)lines 68–68◇ if (out.empty() && c == '/')lines 68–68continue;lines 69–69◇ if (c == '[' || c == ']')lines 69–69continue;lines 70–70◇ if (static_cast<unsigned char>(c) < 0x20 || c == 0x7f)lines 70–70continue;lines 71–71out.push_back(c);lines 73–73return out.empty() ? std::string{"server"} : out;

Requirements

REQ-MCP-025

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

Show on the map →

Called by none

Calls drain_orphaned_responses, request_cancelled

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;
}
Activity · 17 steps
lines 262–262◇ if (!connected_ || request_cancelled())lines 263–263return "";lines 266–267uint32_t actual_timeout = timeout_ms > 0 ? timeout_ms : default_timeout_ms_;lines 269–269std::lock_guard<std::mutex> lock(io_mutex_);lines 271–271◇ if (desynced_)lines 272–272int dropped = drain_orphaned_responses();lines 273–273desynced_ = false;lines 274–276logger->warn("Discarded {} orphaned response line(s) from '{}' " "before sending the next request", dropped, display_name_);lines 279–279std::string msg = request_json + "\n";lines 280–280ssize_t written = ::write(stdin_fd_, msg.data(), msg.size());lines 281–281◇ if (written < 0 || static_cast<size_t>(written) != msg.size())lines 282–283logger->error("Write to child stdin failed: {}", strerror(errno));lines 284–284connected_ = false;lines 285–285return "";lines 291–291auto response = read_line(stdout_fd_, actual_timeout);lines 292–292desynced_ = response.empty();lines 293–293return response;

Requirements

REQ-MCP-025 REQ-MCP-026