Entropic 2.14.3
Local-first agentic inference engine
Loading...
Searching...
No Matches
external_bridge.cpp File Reference

Unix socket MCP bridge implementation. More...

#include <entropic/mcp/external_bridge.h>
#include <entropic/entropic.h>
#include <entropic/types/logging.h>
#include "engine_handle.h"
#include "final_text.h"
#include "tool_namespace.h"
#include <nlohmann/json.hpp>
#include <atomic>
#include <cerrno>
#include <algorithm>
#include <chrono>
#include <condition_variable>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <functional>
#include <mutex>
#include <sstream>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <poll.h>
Include dependency graph for external_bridge.cpp:

Go to the source code of this file.

Classes

struct  entropic::AsyncFinalState
 Handle entropic.context_clear. More...
 

Namespaces

namespace  entropic
 Activate model on GPU (WARM → ACTIVE).
 
namespace  entropic::tool_suffix
 The bare tool suffixes this bridge exposes, in advertised order.
 

Typedefs

using json = nlohmann::json
 
using entropic::ToolHandler = json(*)(entropic_handle_t, ExternalBridge *, const json &)
 A non-ask tool's handler (v2.14.3).
 

Functions

std::filesystem::path entropic::compute_socket_path (const std::filesystem::path &project_dir)
 Compute project-unique Unix socket path for self-detection.
 
static std::string entropic::rpc_ok (const json &id, const json &result)
 Build a JSON-RPC success response.
 
static std::string entropic::rpc_err (const json &id, int code, const std::string &msg)
 Build a JSON-RPC error response.
 
static json entropic::tool_text (const std::string &text)
 Wrap text in MCP tool result shape.
 
static std::string entropic::describe (const ExternalMCPConfig &cfg, const char *suffix, const char *fallback)
 Resolve a tool's description, honouring any consumer override.
 
static json entropic::no_args_tool (const ExternalMCPConfig &cfg, const char *suffix, const char *fallback)
 Definition for a tool that takes no arguments.
 
static json entropic::session_scoped_tool (const ExternalMCPConfig &cfg, const char *suffix, const char *fallback)
 Definition for a tool scoped to an optional session (gh#144).
 
static json entropic::ask_tool (const ExternalMCPConfig &cfg)
 Definition for the ask tool (prompt + optional async).
 
static json entropic::ask_status_tool (const ExternalMCPConfig &cfg)
 Definition for the ask_status tool (task_id lookup).
 
static json entropic::tool_definitions (const ExternalMCPConfig &cfg)
 MCP tool definitions exposed by the bridge.
 
static void entropic::write_json_line (int fd, const json &msg)
 Write a JSON-RPC line to a socket fd.
 
static void entropic::send_progress (int fd, const std::string &token_text, const json &progress_token, uint64_t seq)
 Send an MCP progress notification with a text token.
 
static json entropic::final_answer_from_context (entropic_handle_t handle, const std::string &session_key)
 Read the live conversation and render the operator-visible answer.
 
static entropic_error_t entropic::run_plain_turn (entropic_handle_t handle, const std::string &session_key, const std::string &tier, const std::string &prompt, char **result_json)
 Run one non-streaming turn, under tier when one is given (gh#205).
 
static entropic_error_t entropic::run_stream_turn (entropic_handle_t handle, const std::string &session_key, const std::string &tier, const std::string &prompt, void(*on_token)(const char *, size_t, void *), void *ud)
 Run one streaming turn, under tier when one is given (gh#205).
 
static json entropic::handle_ask_plain (entropic_handle_t handle, const json &args, const std::string &session_key)
 Handle entropic.ask — non-streaming path (gh#115, v2.9.12).
 
static json entropic::handle_ask (entropic_handle_t handle, const json &args, const std::string &session_key, int client_fd, const json &progress_token)
 Handle entropic.ask — stream tokens then return final text.
 
static json entropic::handle_status (entropic_handle_t handle, const ExternalBridge *bridge)
 Handle the status tool.
 
static AsyncFinalState entropic::derive_async_final_state (entropic_handle_t handle, entropic_error_t err, char *result_json)
 Translate entropic_run's return code into a final task state.
 
static bool entropic::mark_tasks_cancelling (ExternalBridge *bridge, const std::string &session_key)
 Mark one session's queued/running tasks as cancelling.
 
static bool entropic::any_cancelling_left (ExternalBridge *bridge, const std::string &session_key)
 True while one session's task is still phase=cancelling.
 
static void entropic::cancel_inflight_async_tasks (entropic_handle_t handle, ExternalBridge *bridge, const std::string &session_key)
 Cancel any async tasks currently running on the bridge.
 
static json entropic::handle_clear (entropic_handle_t handle, ExternalBridge *bridge, const std::string &session_key)
 entropic.context_clear MCP tool handler.
 
static json entropic::handle_count (entropic_handle_t handle, const std::string &session_key)
 Handle entropic.context_count.
 
static std::string entropic::generate_task_id ()
 Generate a simple UUID-like task ID.
 
static std::string entropic::bound_workspace (entropic_handle_t handle, const std::string &session_key)
 The session's current workspace binding, or "" (gh#205).
 
static std::string entropic::resolve_workspace (entropic_handle_t handle, const std::string &workspace, std::string &err)
 Resolve workspace to a workspace name, creating one for a path.
 
static std::string entropic::bind_session_workspace (entropic_handle_t handle, const std::string &session_key, const std::string &workspace)
 Bind a session to the requested workspace, idempotently (gh#205).
 
static json entropic::ask_in_turn (entropic_handle_t handle, ExternalBridge *bridge, const json &args, const std::string &session_key, int client_fd, const json &progress_token)
 Run a sync ask once this session's turn comes up (gh#144, gh#205).
 
static json entropic::dispatch_ask (entropic_handle_t handle, ExternalBridge *bridge, const json &args, int client_fd, const json &progress_token)
 Route entropic.ask — sync (streaming) or async.
 
static std::string entropic::resident_tiers (entropic_handle_t handle)
 Names of the models resident right now, from the snapshot.
 
static json entropic::handle_release (entropic_handle_t handle)
 release: unload every resident model, keep everything else.
 
static const std::unordered_map< std::string, ToolHandler > & entropic::tool_handlers ()
 Non-ask tools by suffix (v2.14.3).
 
static json entropic::dispatch_tool (entropic_handle_t handle, ExternalBridge *bridge, const json &params, int client_fd, const std::string &call_id)
 Dispatch a tools/call to the appropriate handler.
 
static void entropic::prepare_socket_dir (const std::filesystem::path &parent)
 Prepare the socket containing directory with 0700 perms.
 
static bool entropic::socket_path_safe (const std::filesystem::path &path)
 Reject a pre-existing path that is a symlink or non-socket.
 
static bool entropic::bind_and_listen (int fd, const std::filesystem::path &path)
 Bind+listen on an AF_UNIX socket, applying 0600 perms.
 
static int entropic::create_listen_socket (const std::filesystem::path &path)
 Create, bind, and listen on a unix domain socket.
 
static bool entropic::peer_uid_matches (int client_fd)
 Validate that the connecting peer shares the engine's UID.
 
static std::string entropic::read_line (int fd)
 Read one newline-delimited line from a socket fd.
 
static json entropic::initialize_result (const ExternalMCPConfig &cfg)
 Build the MCP initialize response payload.
 
static void entropic::phase_observer_cb (int state, void *ud)
 State observer that projects VERIFYING onto task phase.
 
static json entropic::task_complete_notification (const std::string &task_id, const std::string &status)
 The completion notification for an async task (Issue #4, v2.1.2).
 
static bool entropic::send_nonblocking (int fd, const std::string &payload)
 One non-blocking write of a whole payload to one fd.
 
static const char * entropic::sentinel_suffix_for_status (const std::string &status)
 Map a terminal status string to a sentinel filename suffix.
 

Variables

static auto entropic::logger = entropic::log::get("mcp.external_bridge")
 
static std::mutex entropic::s_bound_sockets_mu
 
static std::unordered_set< std::string > entropic::s_bound_sockets
 
constexpr const char * entropic::tool_suffix::kAsk = "ask"
 
constexpr const char * entropic::tool_suffix::kAskStatus = "ask_status"
 
constexpr const char * entropic::tool_suffix::kStatus = "status"
 
constexpr const char * entropic::tool_suffix::kContextClear = "context_clear"
 
constexpr const char * entropic::tool_suffix::kContextCount = "context_count"
 
constexpr const char * entropic::tool_suffix::kRelease = "release"
 

Detailed Description

Unix socket MCP bridge implementation.

Listens on a unix domain socket, serves every connected client on its own thread, and dispatches JSON-RPC 2.0 (MCP) to the engine handle. Turns queue per session (v2.14.0); a session's async completions, clears and cancels touch only that session and that client.

Version
2.14.0

Definition in file external_bridge.cpp.

Typedef Documentation

◆ json

using json = nlohmann::json

Definition at line 44 of file external_bridge.cpp.