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
lines 125–125int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);lines 126–126bool ok = fd >= 0 && path.size() < sizeof(sockaddr_un::sun_path);lines 127–127int saved = ok ? 0 : (fd < 0 ? errno : ENAMETOOLONG);lines 128–128◇ if (ok)lines 129–129struct sockaddr_un addr {};lines 130–130addr.sun_family = AF_UNIX;lines 131–132std::strncpy(addr.sun_path, path.c_str(), sizeof(addr.sun_path) - 1);lines 133–134ok = ::connect(fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) == 0;lines 135–135◇ if (!ok)lines 135–135saved = errno;lines 137–137◇ if (!ok)lines 138–138◇ if (fd >= 0)lines 138–138::close(fd);lines 139–139errno = saved;lines 140–140fd = -1;lines 142–142return fd;

Requirements

REQ-PROD-002

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
lines 167–179std::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");lines 180–180return 1;

Requirements

REQ-PROD-002

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
lines 243–243struct pollfd fds[2] = {};lines 244–244fds[0].fd = STDIN_FILENO;lines 245–245fds[1].fd = sock_fd;lines 247–247bool stdin_open = true;lines 248–248bool sock_open = true;lines 249–249◇ while (stdin_open || sock_open)lines 250–250fds[0].events = stdin_open ? POLLIN : 0;lines 251–251fds[1].events = sock_open ? POLLIN : 0;lines 252–252int rc = ::poll(fds, 2, -1);lines 253–253◇ if (rc < 0 && errno != EINTR)lines 253–253break;lines 254–254◇ if (rc > 0)lines 255–255service_revents(fds, sock_fd, stdin_open, sock_open);

Requirements

REQ-PROD-002

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

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
lines 282–282std::string explicit_socket = parse_flag(argc, argv, "--socket");lines 283–283std::string requested = parse_flag(argc, argv, "--project-dir");lines 284–286std::filesystem::path requested_path = requested.empty() ? std::filesystem::current_path() : std::filesystem::path(requested);lines 288–288std::error_code ec;lines 289–289auto canonical = std::filesystem::weakly_canonical(requested_path, ec);lines 290–290◇ if (ec)lines 290–290canonical = requested_path;lines 292–294std::filesystem::path socket_path = explicit_socket.empty() ? entropic::compute_socket_path(canonical) : std::filesystem::path(explicit_socket);lines 296–296int fd = connect_unix_socket(socket_path.string());lines 297–297◇ if (fd < 0)lines 298–300return emit_no_engine_error( requested_path, canonical, socket_path, std::strerror(errno));lines 303–303relay_loop(fd);lines 304–304::close(fd);lines 305–305return 0;

Requirements

REQ-PROD-002

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

Show on the map →

Called by relay_loop

Calls pump_once

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;
        }
    }
}
Activity · 7 steps
lines 213–213◇ if (stdin_open && (fds[0].revents & (POLLIN | POLLHUP)))lines 214–214◇ if (!pump_once(STDIN_FILENO, sock_fd))lines 215–215stdin_open = false;lines 216–216::shutdown(sock_fd, SHUT_WR);lines 219–219◇ if (sock_open && (fds[1].revents & (POLLIN | POLLHUP)))lines 220–220◇ if (!pump_once(sock_fd, STDOUT_FILENO))lines 221–221sock_open = false;

Requirements

REQ-PROD-002