server_base.cpp

src/mcp/server_base.cpp

@file server_base.cpp @brief MCPServerBase implementation.

Owners: @tvanfossen

configure

bool entropic::MCPServerBase::configure(const std::string &config_json)

Configure the server after creation.

src/mcp/server_base.cpp:261

Show on the map →

Called by entropic_mcp_server_configure, entropic_mcp_server_configure

Calls none

Source · 3 lines
bool MCPServerBase::configure(const std::string& /*config_json*/) {
    return true;
}
Activity · 1 steps
lines 262–262return true;

Requirements

REQ-MCP-001

directive_type_name

static const char * entropic::directive_type_name(entropic_directive_type_t type)

Map directive type enum to wire-format string.

src/mcp/server_base.cpp:324

Source · 10 lines
static const char* directive_type_name(
    entropic_directive_type_t type) {
    auto idx = static_cast<int>(type);
    constexpr int count = sizeof(DIRECTIVE_NAMES)
                        / sizeof(DIRECTIVE_NAMES[0]);
    if (idx < 0 || idx >= count) {
        return "unknown";
    }
    return DIRECTIVE_NAMES[idx];
}
Activity · 5 steps
lines 326–326auto idx = static_cast<int>(type);lines 327–328constexpr int count = sizeof(DIRECTIVE_NAMES) / sizeof(DIRECTIVE_NAMES[0]);lines 329–329◇ if (idx < 0 || idx >= count)lines 330–330return "unknown";lines 332–332return DIRECTIVE_NAMES[idx];

Requirements

REQ-MCP-002

execute

std::string entropic::MCPServerBase::execute(const std::string &tool_name, const std::string &raw_args_json)

Execute a tool and wrap result in ServerResponse JSON.

src/mcp/server_base.cpp:165

Show on the map →

Called by SCENARIO, TEST_CASE, TEST_CASE, TEST_CASE, entropic_mcp_server_execute, entropic_mcp_server_execute, run, run, timed_run

Calls dispatch, get_tool, inject_anchor_if_needed, normalize_args, serialize_response

Source · 28 lines
std::string MCPServerBase::execute(
    const std::string& tool_name,
    const std::string& raw_args_json) {
    logger->info("[EXECUTE] {}.{}", name_, tool_name);

    const std::string args_json = normalize_args(tool_name, raw_args_json);

    ServerResponse response;
    try {
        response = registry_.dispatch(tool_name, args_json);

        auto* tool = registry_.get_tool(tool_name);
        if (tool != nullptr) {
            inject_anchor_if_needed(*tool, args_json, response);
        }
    } catch (const std::exception& e) {
        // Full args are logged, never truncated — for this failure class
        // the arguments are the whole diagnosis.
        logger->error("Tool '{}.{}' threw: {} — args: {}",
                      name_, tool_name, e.what(), args_json);
        response.directives.clear();
        response.result =
            "Error: tool '" + name_ + "." + tool_name + "' failed: "
            + e.what();
    }

    return serialize_response(response);
}
Activity · 5 steps
lines 168–168logger->info("[EXECUTE] {}.{}", name_, tool_name);lines 170–170const std::string args_json = normalize_args(tool_name, raw_args_json);lines 172–172ServerResponse response;lines 173–189try { response = registry_.dispatch(tool_name, args_json); auto* tool = registry_.get_tool(tool_name); if (tool != nullptr) { inject_anchor_if_needed(*tool, args_json, response); } } catch (const std::exception& e) { // Full args are logged, never truncated — for this failure class // the arguments are the whole diagnosis. logger->error("Tool '{}.{}' threw: {} — args: {}", name_, tool_name, e.what(), args_json); response.directives.clear(); response.result = "Error: tool '" + name_ + "." + tool_name + "' failed: " + e.what(); }lines 191–191return serialize_response(response);

Requirements

REQ-MCP-001 REQ-MCP-002

get_permission_pattern

std::string entropic::MCPServerBase::get_permission_pattern(const std::string &tool_name, const std::string &args_json) const

Generate permission pattern for 'Always Allow/Deny'.

src/mcp/server_base.cpp:209

Show on the map →

Called by TEST_CASE

Calls none

Source · 5 lines
std::string MCPServerBase::get_permission_pattern(
    const std::string& tool_name,
    const std::string& /*args_json*/) const {
    return tool_name;
}
Activity · 1 steps
lines 212–212return tool_name;

Requirements

REQ-MCP-001 REQ-MCP-009

inject_anchor_if_needed

void entropic::MCPServerBase::inject_anchor_if_needed(const ToolBase &tool, const std::string &args_json, ServerResponse &response)

Inject ContextAnchor directive if tool declares anchor_key.

src/mcp/server_base.cpp:377

Show on the map →

Called by execute

Calls none

Source · 13 lines
void MCPServerBase::inject_anchor_if_needed(
    const ToolBase& tool,
    const std::string& args_json,
    ServerResponse& response) {
    auto key = tool.anchor_key(args_json);
    if (key.empty()) {
        return;
    }
    Directive anchor;
    anchor.type = ENTROPIC_DIRECTIVE_CONTEXT_ANCHOR;
    response.directives.push_back(std::move(anchor));
    logger->info("Auto-injected ContextAnchor: {}", key);
}
Activity · 7 steps
lines 381–381auto key = tool.anchor_key(args_json);lines 382–382◇ if (key.empty())lines 383–383return;lines 385–385Directive anchor;lines 386–386anchor.type = ENTROPIC_DIRECTIVE_CONTEXT_ANCHOR;lines 387–387response.directives.push_back(std::move(anchor));lines 388–388logger->info("Auto-injected ContextAnchor: {}", key);

Requirements

REQ-MCP-001

list_tools

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

List all registered tools as a JSON array string.

src/mcp/server_base.cpp:68

Show on the map →

Called by SCENARIO, TEST_CASE

Calls get_tools_json

Source · 3 lines
std::string MCPServerBase::list_tools() const {
    return registry_.get_tools_json();
}
Activity · 1 steps
lines 69–69return registry_.get_tools_json();

Requirements

REQ-MCP-001 REQ-MCP-007

normalize_args

std::string entropic::MCPServerBase::normalize_args(const std::string &tool_name, const std::string &raw) const

Read a "no arguments" payload as {} before dispatch (gh#143).

src/mcp/server_base.cpp:109

Show on the map →

Called by execute

Calls none

Source · 19 lines
std::string MCPServerBase::normalize_args(
    const std::string& tool_name,
    const std::string& raw) const {
    const auto first = raw.find_first_not_of(" \t\r\n");
    if (first == std::string::npos) {
        return "{}";
    }
    // Non-throwing parse: a discarded value is unparseable and is left
    // alone on purpose, as is any non-null non-object.
    const auto parsed = nlohmann::json::parse(raw, nullptr, false);
    if (!parsed.is_null()) {
        return raw;
    }
    logger->info(
        "[{}.{}] arguments arrived as JSON null — reading as \"no "
        "arguments\" and dispatching with {{}}",
        name_, tool_name);
    return "{}";
}
Activity · 8 steps
lines 112–112const auto first = raw.find_first_not_of(" \t\r\n");lines 113–113◇ if (first == std::string::npos)lines 114–114return "{}";lines 118–118const auto parsed = nlohmann::json::parse(raw, nullptr, false);lines 119–119◇ if (!parsed.is_null())lines 120–120return raw;lines 122–125logger->info( "[{}.{}] arguments arrived as JSON null — reading as \"no " "arguments\" and dispatching with {{}}", name_, tool_name);lines 126–126return "{}";

Requirements

REQ-MCP-013

register_tool

void entropic::MCPServerBase::register_tool(ToolBase *tool)

Register a tool with this server.

src/mcp/server_base.cpp:51

Show on the map →

Called by AuthTestServer, BashServer, DiagnosticsServer, OptionalArgsServer, PingServer, WebServer

Tested by

1 testsTestServer

Calls register_tool

Source · 3 lines
void MCPServerBase::register_tool(ToolBase* tool) {
    registry_.register_tool(tool);
}
Activity · 1 steps
lines 52–52registry_.register_tool(tool);

Requirements

REQ-MCP-001 REQ-MCP-003

requires_approval

bool entropic::MCPServerBase::requires_approval(const std::string &tool_name) const

Whether a call to this tool needs the host's approval.

src/mcp/server_base.cpp:245

Show on the map →

Called by none

Calls none

Source · 4 lines
bool MCPServerBase::requires_approval(
    const std::string& /*tool_name*/) const {
    return true;
}
Activity · 1 steps
lines 247–247return true;

Requirements

REQ-MCP-012

serialize_response

std::string entropic::MCPServerBase::serialize_response(const ServerResponse &response)

Serialize a ServerResponse to JSON envelope.

src/mcp/server_base.cpp:348

Source · 15 lines
std::string MCPServerBase::serialize_response(
    const ServerResponse& response) {
    nlohmann::json j;
    j["result"] = response.result;

    auto directives = nlohmann::json::array();
    for (const auto& d : response.directives) {
        nlohmann::json dj;
        dj["type"] = directive_type_name(d.type);
        directives.push_back(std::move(dj));
    }
    j["directives"] = std::move(directives);

    return j.dump();
}
Activity · 9 steps
lines 350–350nlohmann::json j;lines 351–351j["result"] = response.result;lines 353–353auto directives = nlohmann::json::array();lines 354–354◇ for (const auto& d : response.directives)lines 355–355nlohmann::json dj;lines 356–356dj["type"] = directive_type_name(d.type);lines 357–357directives.push_back(std::move(dj));lines 359–359j["directives"] = std::move(directives);lines 361–361return j.dump();

Requirements

REQ-MCP-002

set_working_dir

bool entropic::MCPServerBase::set_working_dir(const std::string &path)

Set the working directory.

src/mcp/server_base.cpp:276

Show on the map →

Called by entropic_mcp_server_set_working_dir, entropic_mcp_server_set_working_dir

Calls none

Source · 3 lines
bool MCPServerBase::set_working_dir(const std::string& /*path*/) {
    return true;
}
Activity · 1 steps
lines 277–277return true;

Requirements

REQ-MCP-001

skip_duplicate_check

bool entropic::MCPServerBase::skip_duplicate_check(const std::string &tool_name) const

Check if a tool should skip duplicate detection.

src/mcp/server_base.cpp:230

Show on the map →

Called by TEST_CASE

Calls none

Source · 4 lines
bool MCPServerBase::skip_duplicate_check(
    const std::string& /*tool_name*/) const {
    return false;
}
Activity · 1 steps
lines 232–232return false;

Requirements

REQ-MCP-001 REQ-MCP-015