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
Source · 3 lines
bool MCPServerBase::configure(const std::string& /*config_json*/) {
return true;
}Activity · 1 steps
Requirements
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
Requirements
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
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
Requirements
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
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
Requirements
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
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
Requirements
list_tools
std::string entropic::MCPServerBase::list_tools() const
List all registered tools as a JSON array string.
src/mcp/server_base.cpp:68
Source · 3 lines
std::string MCPServerBase::list_tools() const {
return registry_.get_tools_json();
}Activity · 1 steps
Requirements
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
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
Requirements
register_tool
void entropic::MCPServerBase::register_tool(ToolBase *tool)
Register a tool with this server.
src/mcp/server_base.cpp:51
Called by AuthTestServer, BashServer, DiagnosticsServer, OptionalArgsServer, PingServer, WebServer
Tested by 1 tests
TestServer
Calls register_tool
Source · 3 lines
void MCPServerBase::register_tool(ToolBase* tool) {
registry_.register_tool(tool);
}Activity · 1 steps
Requirements
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
Source · 4 lines
bool MCPServerBase::requires_approval(
const std::string& /*tool_name*/) const {
return true;
}Activity · 1 steps
Requirements
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
Requirements
set_working_dir
bool entropic::MCPServerBase::set_working_dir(const std::string &path)
Set the working directory.
src/mcp/server_base.cpp:276
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
Requirements
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
Source · 4 lines
bool MCPServerBase::skip_duplicate_check(
const std::string& /*tool_name*/) const {
return false;
}