mcp_bridge.cpp¶
src/cli/mcp_bridge.cpp
@file mcp_bridge.cpp @brief `entropic mcp-bridge` — stdio↔unix-socket relay for MCP.
Owners: @tvanfossen
connect_unix_socket
int entropic::cli::anonymous_namespace{mcp_bridge.cpp}::connect_unix_socket(const std::string &path)Connect to a unix domain socket.
src/cli/mcp_bridge.cpp:123
Source · 21 lines
int connect_unix_socket(const std::string& path)
{
int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
bool ok = fd >= 0 && path.size() < sizeof(sockaddr_un::sun_path);
int saved = ok ? 0 : (fd < 0 ? errno : ENAMETOOLONG);
if (ok) {
struct sockaddr_un addr {};
addr.sun_family = AF_UNIX;
std::strncpy(addr.sun_path, path.c_str(),
sizeof(addr.sun_path) - 1);
ok = ::connect(fd, reinterpret_cast<sockaddr*>(&addr),
sizeof(addr)) == 0;
if (!ok) { saved = errno; }
}
if (!ok) {
if (fd >= 0) { ::close(fd); }
errno = saved;
fd = -1;
}
return fd;
}Activity · 16 steps
Requirements
emit_no_engine_error
int entropic::cli::anonymous_namespace{mcp_bridge.cpp}::emit_no_engine_error(const std::filesystem::path &requested, const std::filesystem::path &canonical, const std::filesystem::path &socket, const char *why)Emit the no-engine diagnostic and return exit code 1.
src/cli/mcp_bridge.cpp:161
Source · 21 lines
int emit_no_engine_error(
const std::filesystem::path& requested,
const std::filesystem::path& canonical,
const std::filesystem::path& socket,
const char* why)
{
std::fprintf(stderr,
"entropic mcp-bridge: no running engine.\n"
" project_dir (requested): %s\n"
" project_dir (canonical): %s\n"
" expected socket: %s\n"
" connect failed: %s\n"
"\n"
"An engine must be running for this project. Start one via the\n"
"engine host that owns the model (TUI, consumer app, or headless\n"
"server). mcp-bridge is a relay only — it does not load the\n"
"model itself.\n",
requested.c_str(), canonical.c_str(),
socket.c_str(), why ? why : "unknown");
return 1;
}Activity · 2 steps
Requirements
relay_loop
void entropic::cli::anonymous_namespace{mcp_bridge.cpp}::relay_loop(int sock_fd)Bidirectional byte relay between stdio and a connected socket.
src/cli/mcp_bridge.cpp:241
Source · 18 lines
void relay_loop(int sock_fd)
{
struct pollfd fds[2] = {};
fds[0].fd = STDIN_FILENO;
fds[1].fd = sock_fd;
bool stdin_open = true;
bool sock_open = true;
while (stdin_open || sock_open) {
fds[0].events = stdin_open ? POLLIN : 0;
fds[1].events = sock_open ? POLLIN : 0;
int rc = ::poll(fds, 2, -1);
if (rc < 0 && errno != EINTR) { break; }
if (rc > 0) {
service_revents(fds, sock_fd, stdin_open, sock_open);
}
}
}Activity · 13 steps
Requirements
run_mcp_bridge
int entropic::cli::run_mcp_bridge(int argc, char *argv[])
Entry point for the mcp-bridge subcommand.
src/cli/mcp_bridge.cpp:275
Called by none
Calls compute_socket_path, connect_unix_socket, emit_no_engine_error, parse_flag, relay_loop
Source · 32 lines
int run_mcp_bridge(int argc, char* argv[])
{
// --socket PATH overrides discovery. Primary uses: deterministic
// tests, and users whose engine is configured with a non-default
// socket path. Absent the override, the bridge derives the path
// from the canonicalized project_dir hash — same scheme as the
// engine's ExternalBridge.
std::string explicit_socket = parse_flag(argc, argv, "--socket");
std::string requested = parse_flag(argc, argv, "--project-dir");
std::filesystem::path requested_path = requested.empty()
? std::filesystem::current_path()
: std::filesystem::path(requested);
std::error_code ec;
auto canonical = std::filesystem::weakly_canonical(requested_path, ec);
if (ec) { canonical = requested_path; }
std::filesystem::path socket_path = explicit_socket.empty()
? entropic::compute_socket_path(canonical)
: std::filesystem::path(explicit_socket);
int fd = connect_unix_socket(socket_path.string());
if (fd < 0) {
return emit_no_engine_error(
requested_path, canonical, socket_path,
std::strerror(errno));
}
relay_loop(fd);
::close(fd);
return 0;
}Activity · 14 steps
Requirements
service_revents
void entropic::cli::anonymous_namespace{mcp_bridge.cpp}::service_revents(struct pollfd *fds, int sock_fd, bool &stdin_open, bool &sock_open)Service a single ready pollfd revent set.
src/cli/mcp_bridge.cpp:210
Source · 15 lines
void service_revents(struct pollfd* fds, int sock_fd,
bool& stdin_open, bool& sock_open)
{
if (stdin_open && (fds[0].revents & (POLLIN | POLLHUP))) {
if (!pump_once(STDIN_FILENO, sock_fd)) {
stdin_open = false;
::shutdown(sock_fd, SHUT_WR);
}
}
if (sock_open && (fds[1].revents & (POLLIN | POLLHUP))) {
if (!pump_once(sock_fd, STDOUT_FILENO)) {
sock_open = false;
}
}
}