Entropic 2.14.3
Local-first agentic inference engine
Loading...
Searching...
No Matches
transport_stdio.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
13#pragma once
14
16
17#include <atomic>
18#include <chrono>
19#include <map>
20#include <mutex>
21#include <string>
22#include <thread>
23#include <vector>
24
25#include <sys/types.h>
26
27namespace entropic {
28
37class StdioTransport : public Transport {
38public:
49 std::string command,
50 std::vector<std::string> args,
51 std::map<std::string, std::string> env = {},
53
78 std::string display_name,
79 std::string command,
80 std::vector<std::string> args,
81 std::map<std::string, std::string> env,
83
84 ~StdioTransport() override;
85
91 bool open() override;
92
103 void close() override;
104
117 std::string send_request(
118 const std::string& request_json,
119 uint32_t timeout_ms = 0) override;
120
126 bool is_connected() const override;
127
137 void interrupt() override;
138
151 void clear_interrupt() override;
152
164 bool request_cancelled() const;
165
172 bool is_interrupted() const override;
173
187 const std::string& display_name() const { return display_name_; }
188
201 void set_working_dir(std::string dir) { working_dir_ = std::move(dir); }
202
203private:
204 std::atomic<bool> cancel_flag_{false};
205
206 std::string display_name_;
207 std::string command_;
208 std::vector<std::string> args_;
209 std::map<std::string, std::string> env_;
210 std::string working_dir_;
211 uint32_t default_timeout_ms_;
212
213 pid_t child_pid_{-1};
214 int stdin_fd_{-1};
215 int stdout_fd_{-1};
216 int stderr_fd_{-1};
217 std::atomic<bool> connected_{false};
218 std::thread stderr_thread_;
219 std::mutex io_mutex_;
235 bool desynced_ = false;
236
243 std::vector<std::string> build_env() const;
244
255 bool spawn_child(int stdin_r, int stdout_w, int stderr_w,
256 const std::vector<std::string>& env_strs);
257
266 static bool create_pipe(int& read_fd, int& write_fd);
267
276 std::string read_line(int fd, uint32_t timeout_ms);
277
291 int drain_orphaned_responses();
292
303 int poll_until_ready(
304 int fd,
305 std::chrono::steady_clock::time_point deadline);
306
314 bool create_all_pipes(int (&fds)[6]);
315
322 bool open_child_process();
323
329 void stderr_reader_loop();
330
336 void terminate_child();
337
344 static void close_fd(int& fd);
345};
346
347} // namespace entropic
Stdio transport: fork/exec child, pipe stdin/stdout.
void set_working_dir(std::string dir)
Set the child's working directory, before connect (gh#166).
bool is_interrupted() const override
Whether an interrupt is currently short-circuiting calls.
bool open() override
Spawn child process and open pipes.
bool is_connected() const override
Check if child process is alive.
void interrupt() override
Abort any blocking read and mark transport uncancellable.
std::string send_request(const std::string &request_json, uint32_t timeout_ms=0) override
Send JSON-RPC request via stdin, read response from stdout.
void close() override
Send SIGTERM, reap the child, join the stderr pump, close pipes.
const std::string & display_name() const
Get the sanitized display name used for stderr labeling.
bool request_cancelled() const
Whether THIS request must abort (gh#158, v2.13.0).
~StdioTransport() override
Destructor — ensures child is cleaned up.
void clear_interrupt() override
Release the cancel flag so this transport works again.
Abstract transport for external MCP communication.
Definition transport.h:29
int read_fd
Our end.
Activate model on GPU (WARM → ACTIVE).
@ optional
optional: every escape asks the host's path approver
Abstract transport for external MCP server communication.