tool_base.cpp

src/mcp/tool_base.cpp

@file tool_base.cpp @brief ToolBase implementation, load_tool_definition, and the response-format renderer (gh#203).

Owners: @tvanfossen

anchor_key

std::string entropic::ToolBase::anchor_key(const std::string &args_json) const

Generate anchor key for this tool result.

src/mcp/tool_base.cpp:65

Show on the map →

Called by none

Calls none

Source · 4 lines
std::string ToolBase::anchor_key(
    const std::string& /*args_json*/) const {
    return "";
}
Activity · 1 steps
lines 67–67return "";

Requirements

REQ-MCP-001

load_tool_definition

ToolDefinition entropic::load_tool_definition(const std::string &tool_name, const std::string &server_prefix, const std::string &data_dir)

Load a tool definition from a JSON file.

src/mcp/tool_base.cpp:297

Show on the map →

Called by BashServer, DiagnosticsServer, EditFileTool, GlobTool, GrepTool, ListDirectoryTool, ReadFileTool, SCENARIO, SCENARIO, TEST_CASE, WebServer, WriteFileTool

5 morecreate_git_tools, output_schema, register_core_tools, register_delegation_tools, register_introspection_tools

Calls render_response_format

Source · 33 lines
ToolDefinition load_tool_definition(
    const std::string& tool_name,
    const std::string& server_prefix,
    const std::string& data_dir) {

    std::string path = data_dir;
    if (!server_prefix.empty()) {
        path += "/" + server_prefix;
    }
    path += "/" + tool_name + ".json";

    std::ifstream file(path);
    if (!file.is_open()) {
        throw std::runtime_error(
            "Tool definition not found: " + path);
    }

    // ordered: the rendered Response format keeps the authored key order.
    auto json = nlohmann::ordered_json::parse(file);

    ToolDefinition def;
    def.name = json.at("name").get<std::string>();
    def.description = json.value("description", "");
    def.input_schema = json.at("inputSchema").dump();
    if (json.contains("outputSchema")) {
        def.output_schema = json["outputSchema"].dump();
        def.description += render_response_format(def.output_schema);
    }

    logger->info("Loaded tool definition: {} from {}",
                 def.name, path);
    return def;
}
Activity · 17 steps
lines 302–302std::string path = data_dir;lines 303–303◇ if (!server_prefix.empty())lines 304–304path += "/" + server_prefix;lines 306–306path += "/" + tool_name + ".json";lines 308–308std::ifstream file(path);lines 309–309◇ if (!file.is_open())lines 310–311throw std::runtime_error( "Tool definition not found: " + path);lines 315–315auto json = nlohmann::ordered_json::parse(file);lines 317–317ToolDefinition def;lines 318–318def.name = json.at("name").get<std::string>();lines 319–319def.description = json.value("description", "");lines 320–320def.input_schema = json.at("inputSchema").dump();lines 321–321◇ if (json.contains("outputSchema"))lines 322–322def.output_schema = json["outputSchema"].dump();lines 323–323def.description += render_response_format(def.output_schema);lines 326–327logger->info("Loaded tool definition: {} from {}", def.name, path);lines 328–328return def;

Requirements

REQ-MCP-013

render_response_format

std::string entropic::render_response_format(const std::string &output_schema_json)

Render an outputSchema as the compact "Response format" text a model reads (gh#203).

src/mcp/tool_base.cpp:244

Show on the map →

Called by SCENARIO, SCENARIO, load_tool_definition, phase_change_definition

Calls collect_notes, render_variants

Source · 15 lines
std::string render_response_format(const std::string& output_schema_json) {
    auto schema = ojson::parse(output_schema_json, nullptr, false);
    std::string out;
    if (schema.is_object()) {
        out = "\n\n**Response format** (JSON):\n" + render_variants(schema);
        std::vector<std::string> notes;
        collect_notes(schema, notes);
        for (const auto& note : notes) { out += note + "\n"; }
        // Engine-wide, not per tool: a call refused before or thrown out of
        // the handler (bad arguments, a refused path, an I/O fault) is
        // reported by the server base as plain text (server_base.cpp).
        out += "Other failures: plain text `Error: ...`.\n";
    }
    return out;
}
Activity · 10 steps
lines 245–245auto schema = ojson::parse(output_schema_json, nullptr, false);lines 246–246std::string out;lines 247–247◇ if (schema.is_object())lines 248–248out = "\n\n**Response format** (JSON):\n" + render_variants(schema);lines 249–249std::vector<std::string> notes;lines 250–250collect_notes(schema, notes);lines 251–251◇ for (const auto& note : notes)lines 251–251out += note + "\n";lines 255–255out += "Other failures: plain text `Error: ...`.\n";lines 257–257return out;

Requirements

REQ-MCP-013

required_access_level

MCPAccessLevel entropic::ToolBase::required_access_level() const

Minimum access level required to execute this tool.

src/mcp/tool_base.cpp:80

Show on the map →

Called by TEST_CASE

Calls none

Source · 3 lines
MCPAccessLevel ToolBase::required_access_level() const {
    return MCPAccessLevel::WRITE;
}
Activity · 1 steps
lines 81–81return MCPAccessLevel::WRITE;

Requirements

REQ-MCP-011

tool_error_json

std::string entropic::tool_error_json(const std::string &code, const std::string &message)

A declared tool error: {"error": code, "message": message} .

src/mcp/tool_base.cpp:269

Show on the map →

Called by apply_todo_action, dispatch_inspect, execute, execute, execute, filter_json_by_key, option_refusal, pipeline_rejection, todo_index_error

Calls none

Source · 6 lines
std::string tool_error_json(const std::string& code, const std::string& message) {
    nlohmann::json err;
    err["error"] = code;
    err["message"] = message;
    return err.dump();
}
Activity · 4 steps
lines 270–270nlohmann::json err;lines 271–271err["error"] = code;lines 272–272err["message"] = message;lines 273–273return err.dump();

Requirements

REQ-MCP-015