plugin_server.cpp

src/mcp/plugin_server.cpp

@file plugin_server.cpp @brief dlopen-loaded MCP server plugin implementation (gh#133).

Owners: @tvanfossen

configure

entropic_error_t entropic::PluginServer::configure(const std::string &config_json)

Pass configuration to the plugin instance.

src/mcp/plugin_server.cpp:345

Show on the map →

Called by none

Calls none

Source · 4 lines
entropic_error_t PluginServer::configure(const std::string& config_json) {
    std::lock_guard<std::mutex> lock(call_mutex_);
    return configure_fn_(instance_, config_json.c_str());
}
Activity · 2 steps
lines 346–346std::lock_guard<std::mutex> lock(call_mutex_);lines 347–347return configure_fn_(instance_, config_json.c_str());

Requirements

REQ-MCP-006

create_instance

entropic_error_t entropic::PluginServer::create_instance()

Create the plugin's server instance and read its name.

src/mcp/plugin_server.cpp:125

Source · 18 lines
entropic_error_t PluginServer::create_instance() {
    instance_ = create_fn_();
    if (instance_ == nullptr) {
        logger->error("Plugin load failed: entropic_create_server() returned "
                      "NULL for {}", path_.string());
        return ENTROPIC_ERROR_PLUGIN_LOAD_FAILED;
    }

    const char* raw_name = name_fn_(instance_);
    name_ = (raw_name != nullptr) ? raw_name : "";
    if (name_.empty()) {
        logger->error("Plugin load failed: entropic_mcp_server_name() gave an "
                      "empty name for {} — the name is the tool-routing "
                      "prefix and cannot be blank", path_.string());
        return ENTROPIC_ERROR_PLUGIN_LOAD_FAILED;
    }
    return ENTROPIC_OK;
}
Activity · 10 steps
lines 126–126instance_ = create_fn_();lines 127–127◇ if (instance_ == nullptr)lines 128–129logger->error("Plugin load failed: entropic_create_server() returned " "NULL for {}", path_.string());lines 130–130return ENTROPIC_ERROR_PLUGIN_LOAD_FAILED;lines 133–133const char* raw_name = name_fn_(instance_);lines 134–134name_ = (raw_name != nullptr) ? raw_name : "";lines 135–135◇ if (name_.empty())lines 136–138logger->error("Plugin load failed: entropic_mcp_server_name() gave an " "empty name for {} — the name is the tool-routing " "prefix and cannot be blank", path_.string());lines 139–139return ENTROPIC_ERROR_PLUGIN_LOAD_FAILED;lines 141–141return ENTROPIC_OK;

Requirements

REQ-MCP-004 REQ-MCP-005

execute

std::string entropic::PluginServer::execute(const std::string &tool_name, const std::string &args_json)

Execute one of the plugin's tools.

src/mcp/plugin_server.cpp:328

Show on the map →

Called by none

Calls take_string

Source · 6 lines
std::string PluginServer::execute(const std::string& tool_name,
                                  const std::string& args_json) {
    std::lock_guard<std::mutex> lock(call_mutex_);
    return take_string(
        execute_fn_(instance_, tool_name.c_str(), args_json.c_str()));
}
Activity · 2 steps
lines 330–330std::lock_guard<std::mutex> lock(call_mutex_);lines 331–332return take_string( execute_fn_(instance_, tool_name.c_str(), args_json.c_str()));

Requirements

REQ-MCP-002 REQ-MCP-006 REQ-MCP-007

list_tools

std::string entropic::PluginServer::list_tools() const

List the plugin's tools.

src/mcp/plugin_server.cpp:303

Show on the map →

Called by none

Calls take_string

Source · 5 lines
std::string PluginServer::list_tools() const {
    std::lock_guard<std::mutex> lock(call_mutex_);
    auto tools = take_string(list_tools_fn_(instance_));
    return tools.empty() ? "[]" : tools;
}
Activity · 3 steps
lines 304–304std::lock_guard<std::mutex> lock(call_mutex_);lines 305–305auto tools = take_string(list_tools_fn_(instance_));lines 306–306return tools.empty() ? "[]" : tools;

Requirements

REQ-MCP-006 REQ-MCP-007 REQ-MCP-008

load

entropic_error_t entropic::PluginServer::load(const std::filesystem::path &path, std::unique_ptr< PluginServer > &out)

Load a plugin .so and construct its server instance.

src/mcp/plugin_server.cpp:70

Source · 25 lines
entropic_error_t PluginServer::load(const std::filesystem::path& path,
                                    std::unique_ptr<PluginServer>& out) {
    // RTLD_LOCAL keeps plugin symbols out of the global namespace so two
    // plugins exporting the same entry-point names cannot collide.
    dlerror();  // clear any stale error before the call
    void* handle = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
    if (handle == nullptr) {
        const char* err = dlerror();
        logger->error("Plugin load failed: dlopen('{}'): {}", path.string(),
                      (err != nullptr) ? err : "unknown error");
        return ENTROPIC_ERROR_PLUGIN_LOAD_FAILED;
    }

    // Adopt the handle immediately so every failure path below closes it.
    std::unique_ptr<PluginServer> plugin(new PluginServer(handle, path));
    auto rc = plugin->resolve_and_init();
    if (rc != ENTROPIC_OK) {
        return rc;
    }

    logger->info("Loaded MCP plugin '{}' from {}", plugin->name_,
                 path.string());
    out = std::move(plugin);
    return ENTROPIC_OK;
}
Activity · 13 steps
lines 74–74dlerror();lines 75–75void* handle = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);lines 76–76◇ if (handle == nullptr)lines 77–77const char* err = dlerror();lines 78–79logger->error("Plugin load failed: dlopen('{}'): {}", path.string(), (err != nullptr) ? err : "unknown error");lines 80–80return ENTROPIC_ERROR_PLUGIN_LOAD_FAILED;lines 84–84std::unique_ptr<PluginServer> plugin(new PluginServer(handle, path));lines 85–85auto rc = plugin->resolve_and_init();lines 86–86◇ if (rc != ENTROPIC_OK)lines 87–87return rc;lines 90–91logger->info("Loaded MCP plugin '{}' from {}", plugin->name_, path.string());lines 92–92out = std::move(plugin);lines 93–93return ENTROPIC_OK;

Requirements

REQ-MCP-004 REQ-MCP-005

resolve_and_check_version

entropic_error_t entropic::PluginServer::resolve_and_check_version()

Resolve every entry point and verify the reported API version.

src/mcp/plugin_server.cpp:159

Source · 16 lines
entropic_error_t PluginServer::resolve_and_check_version() {
    if (!resolve_symbols()) {
        return ENTROPIC_ERROR_PLUGIN_LOAD_FAILED;
    }

    const int reported = version_fn_();
    if (reported != ENTROPIC_MCP_PLUGIN_API_VERSION) {
        logger->error("Plugin version mismatch for {}: plugin reports API "
                      "version {}, this engine implements {}. Rebuild the "
                      "plugin against matching entropic headers.",
                      path_.string(), reported,
                      ENTROPIC_MCP_PLUGIN_API_VERSION);
        return ENTROPIC_ERROR_PLUGIN_VERSION_MISMATCH;
    }
    return ENTROPIC_OK;
}
Activity · 7 steps
lines 160–160◇ if (!resolve_symbols())lines 161–161return ENTROPIC_ERROR_PLUGIN_LOAD_FAILED;lines 164–164const int reported = version_fn_();lines 165–165◇ if (reported != ENTROPIC_MCP_PLUGIN_API_VERSION)lines 166–170logger->error("Plugin version mismatch for {}: plugin reports API " "version {}, this engine implements {}. Rebuild the " "plugin against matching entropic headers.", path_.string(), reported, ENTROPIC_MCP_PLUGIN_API_VERSION);lines 171–171return ENTROPIC_ERROR_PLUGIN_VERSION_MISMATCH;lines 173–173return ENTROPIC_OK;

Requirements

REQ-MCP-004 REQ-MCP-005

resolve_and_init

entropic_error_t entropic::PluginServer::resolve_and_init()

Resolve entry points, check version, create the instance.

src/mcp/plugin_server.cpp:104

Source · 7 lines
entropic_error_t PluginServer::resolve_and_init() {
    auto rc = resolve_and_check_version();
    if (rc != ENTROPIC_OK) {
        return rc;
    }
    return create_instance();
}
Activity · 4 steps
lines 105–105auto rc = resolve_and_check_version();lines 106–106◇ if (rc != ENTROPIC_OK)lines 107–107return rc;lines 109–109return create_instance();

Requirements

REQ-MCP-004

resolve_factory_symbols

bool entropic::PluginServer::resolve_factory_symbols()

Resolve the process-level entry points.

src/mcp/plugin_server.cpp:227

Source · 12 lines
bool PluginServer::resolve_factory_symbols() {
    bool ok = true;
    version_fn_ = reinterpret_cast<version_fn_t>(
        resolve_one("entropic_plugin_api_version", ok));
    create_fn_ = reinterpret_cast<create_fn_t>(
        resolve_one("entropic_create_server", ok));
    name_fn_ = reinterpret_cast<name_fn_t>(
        resolve_one("entropic_mcp_server_name", ok));
    free_fn_ = reinterpret_cast<free_fn_t>(
        resolve_one("entropic_free", ok));
    return ok;
}
Activity · 6 steps
lines 228–228bool ok = true;lines 229–230version_fn_ = reinterpret_cast<version_fn_t>( resolve_one("entropic_plugin_api_version", ok));lines 231–232create_fn_ = reinterpret_cast<create_fn_t>( resolve_one("entropic_create_server", ok));lines 233–234name_fn_ = reinterpret_cast<name_fn_t>( resolve_one("entropic_mcp_server_name", ok));lines 235–236free_fn_ = reinterpret_cast<free_fn_t>( resolve_one("entropic_free", ok));lines 237–237return ok;

Requirements

REQ-MCP-004

resolve_instance_symbols

bool entropic::PluginServer::resolve_instance_symbols()

Resolve the per-instance entry points.

src/mcp/plugin_server.cpp:246

Source · 14 lines
bool PluginServer::resolve_instance_symbols() {
    bool ok = true;
    list_tools_fn_ = reinterpret_cast<list_tools_fn_t>(
        resolve_one("entropic_mcp_server_list_tools", ok));
    execute_fn_ = reinterpret_cast<execute_fn_t>(
        resolve_one("entropic_mcp_server_execute", ok));
    configure_fn_ = reinterpret_cast<configure_fn_t>(
        resolve_one("entropic_mcp_server_configure", ok));
    set_dir_fn_ = reinterpret_cast<set_dir_fn_t>(
        resolve_one("entropic_mcp_server_set_working_dir", ok));
    destroy_fn_ = reinterpret_cast<destroy_fn_t>(
        resolve_one("entropic_mcp_server_destroy", ok));
    return ok;
}
Activity · 7 steps
lines 247–247bool ok = true;lines 248–249list_tools_fn_ = reinterpret_cast<list_tools_fn_t>( resolve_one("entropic_mcp_server_list_tools", ok));lines 250–251execute_fn_ = reinterpret_cast<execute_fn_t>( resolve_one("entropic_mcp_server_execute", ok));lines 252–253configure_fn_ = reinterpret_cast<configure_fn_t>( resolve_one("entropic_mcp_server_configure", ok));lines 254–255set_dir_fn_ = reinterpret_cast<set_dir_fn_t>( resolve_one("entropic_mcp_server_set_working_dir", ok));lines 256–257destroy_fn_ = reinterpret_cast<destroy_fn_t>( resolve_one("entropic_mcp_server_destroy", ok));lines 258–258return ok;

Requirements

REQ-MCP-004

resolve_symbols

bool entropic::PluginServer::resolve_symbols()

dlsym all nine entry points, logging each one that is missing.

src/mcp/plugin_server.cpp:192

Source · 7 lines
bool PluginServer::resolve_symbols() {
    // Both halves run unconditionally: a plugin missing several entry points
    // should be told about all of them in one build/run cycle.
    const bool factory_ok = resolve_factory_symbols();
    const bool instance_ok = resolve_instance_symbols();
    return factory_ok && instance_ok;
}
Activity · 3 steps
lines 195–195const bool factory_ok = resolve_factory_symbols();lines 196–196const bool instance_ok = resolve_instance_symbols();lines 197–197return factory_ok && instance_ok;

Requirements

REQ-MCP-004

set_working_dir

entropic_error_t entropic::PluginServer::set_working_dir(const std::string &dir)

Set the plugin's working directory.

src/mcp/plugin_server.cpp:360

Source · 4 lines
entropic_error_t PluginServer::set_working_dir(const std::string& dir) {
    std::lock_guard<std::mutex> lock(call_mutex_);
    return set_dir_fn_(instance_, dir.c_str());
}
Activity · 2 steps
lines 361–361std::lock_guard<std::mutex> lock(call_mutex_);lines 362–362return set_dir_fn_(instance_, dir.c_str());

Requirements

REQ-MCP-006

take_string

std::string entropic::PluginServer::take_string(char *raw) const

Copy a plugin-allocated string and release it via the plugin.

src/mcp/plugin_server.cpp:274

Source · 14 lines
std::string PluginServer::take_string(char* raw) const {
    if (raw == nullptr) {
        return {};
    }
    std::string copy;
    try {
        copy.assign(raw);
    } catch (...) {
        free_fn_(raw);
        throw;
    }
    free_fn_(raw);
    return copy;
}
Activity · 6 steps
lines 275–275◇ if (raw == nullptr)lines 276–276return {};lines 278–278std::string copy;lines 279–284try { copy.assign(raw); } catch (...) { free_fn_(raw); throw; }lines 285–285free_fn_(raw);lines 286–286return copy;

Requirements

REQ-MCP-004

~PluginServer

entropic::PluginServer::~PluginServer()

Destroy the plugin instance, then close the library.

src/mcp/plugin_server.cpp:40

Show on the map →

Called by none

Calls none

The index is older than this file: the lines it records no longer hold this function. Refresh the index to bundle its body.

Requirements

REQ-MCP-006