mcp_json_discovery.cpp

src/mcp/mcp_json_discovery.cpp

@file mcp_json_discovery.cpp @brief MCPJsonDiscovery implementation.

Owners: @tvanfossen

compute_socket_path

std::filesystem::path entropic::compute_socket_path(const std::filesystem::path &project_dir)

Compute project-unique Unix socket path for self-detection.

src/mcp/mcp_json_discovery.cpp:288

Show on the map →

Called by ExternalBridge, SCENARIO, TEST_CASE, discover, expected_bridge_socket, launches_own_bridge, run_mcp_bridge

Calls none

Source · 16 lines
std::filesystem::path compute_socket_path(
    const std::filesystem::path& project_dir) {

    auto resolved = std::filesystem::weakly_canonical(project_dir);
    auto input = resolved.string();

    // SHA-256 first 8 hex chars (simplified: use std::hash)
    auto h = std::hash<std::string>{}(input);
    std::ostringstream ss;
    ss << std::hex << std::setfill('0') << std::setw(16) << h;
    auto hash_str = ss.str().substr(0, 8);

    auto home = std::filesystem::path(
        getenv("HOME") ? getenv("HOME") : "/tmp");
    return home / ".entropic" / "socks" / (hash_str + ".sock");
}
Activity · 8 steps
lines 291–291auto resolved = std::filesystem::weakly_canonical(project_dir);lines 292–292auto input = resolved.string();lines 295–295auto h = std::hash<std::string>{}(input);lines 296–296std::ostringstream ss;lines 297–297ss << std::hex << std::setfill('0') << std::setw(16) << h;lines 298–298auto hash_str = ss.str().substr(0, 8);lines 300–301auto home = std::filesystem::path( getenv("HOME") ? getenv("HOME") : "/tmp");lines 302–302return home / ".entropic" / "socks" / (hash_str + ".sock");

Requirements

REQ-MCP-025

discover

std::vector< ExternalServerConfig > entropic::MCPJsonDiscovery::discover(const std::set< std::string > &existing_names) const

Discover servers from .mcp.json files.

src/mcp/mcp_json_discovery.cpp:46

Source · 21 lines
std::vector<ExternalServerConfig> MCPJsonDiscovery::discover(
    const std::set<std::string>& existing_names) const {

    auto own_socket = compute_socket_path(project_dir_);
    std::vector<ExternalServerConfig> result;
    std::set<std::string> seen;

    // Project-level first, then global fallback
    auto project_mcp = project_dir_ / ".mcp.json";
    auto home = std::filesystem::path(getenv("HOME") ? getenv("HOME") : "");
    auto global_mcp = home / ".entropic" / ".mcp.json";

    parse_mcp_json(project_mcp, own_socket, existing_names,
                   seen, result);
    parse_mcp_json(global_mcp, own_socket, existing_names,
                   seen, result);

    logger->info("Discovered {} external servers from .mcp.json",
                 result.size());
    return result;
}
Activity · 10 steps
lines 49–49auto own_socket = compute_socket_path(project_dir_);lines 50–50std::vector<ExternalServerConfig> result;lines 51–51std::set<std::string> seen;lines 54–54auto project_mcp = project_dir_ / ".mcp.json";lines 55–55auto home = std::filesystem::path(getenv("HOME") ? getenv("HOME") : "");lines 56–56auto global_mcp = home / ".entropic" / ".mcp.json";lines 58–59parse_mcp_json(project_mcp, own_socket, existing_names, seen, result);lines 60–61parse_mcp_json(global_mcp, own_socket, existing_names, seen, result);lines 63–64logger->info("Discovered {} external servers from .mcp.json", result.size());lines 65–65return result;

Requirements

REQ-MCP-025

has_blocked_prefix

static bool entropic::has_blocked_prefix(const std::string &key)

Check if key matches a blocked prefix pattern.

src/mcp/mcp_json_discovery.cpp:312

Source · 4 lines
static bool has_blocked_prefix(const std::string& key) {
    return (key.size() >= 9 && key.substr(0, 9) == "ENTROPIC_") ||
           (key.size() >= 5 && key.substr(0, 5) == "DYLD_");
}
Activity · 1 steps
lines 313–314return (key.size() >= 9 && key.substr(0, 9) == "ENTROPIC_") || (key.size() >= 5 && key.substr(0, 5) == "DYLD_");

Requirements

REQ-MCP-025

infer_transport

bool entropic::MCPJsonDiscovery::infer_transport(const nlohmann::json &cfg, const std::string &type, ExternalServerConfig &entry) const

Infer transport and populate config entry.

src/mcp/mcp_json_discovery.cpp:184

Source · 21 lines
bool MCPJsonDiscovery::infer_transport(
    const nlohmann::json& cfg,
    const std::string& type,
    ExternalServerConfig& entry) const {

    bool has_url = cfg.contains("url");
    bool has_command = cfg.contains("command");

    // Explicit type overrides inference
    if (type == "sse" || type == "http" ||
        (type.empty() && has_url)) {
        return parse_sse_entry(cfg, entry);
    }
    if (type == "stdio" || (type.empty() && has_command)) {
        return parse_stdio_entry(cfg, entry);
    }

    logger->warn(".mcp.json entry '{}': cannot determine transport "
                 "(no url or command)", entry.name);
    return false;
}
Activity · 8 steps
lines 189–189bool has_url = cfg.contains("url");lines 190–190bool has_command = cfg.contains("command");lines 193–194◇ if (type == "sse" || type == "http" || (type.empty() && has_url))lines 195–195return parse_sse_entry(cfg, entry);lines 197–197◇ if (type == "stdio" || (type.empty() && has_command))lines 198–198return parse_stdio_entry(cfg, entry);lines 201–202logger->warn(".mcp.json entry '{}': cannot determine transport " "(no url or command)", entry.name);lines 203–203return false;

Requirements

REQ-MCP-025

is_blocked_env_var

bool entropic::is_blocked_env_var(const std::string &key)

Environment variable blocklist for .mcp.json env field.

src/mcp/mcp_json_discovery.cpp:332

Show on the map →

Called by TEST_CASE, parse_external_server_spec, parse_stdio_entry

Calls has_blocked_prefix

Source · 12 lines
bool is_blocked_env_var(const std::string& key) {
    static const std::array<std::string, 7> blocked = {{
        "LD_PRELOAD", "LD_LIBRARY_PATH", "PATH",
        "HOME", "SHELL", "DYLD_INSERT_LIBRARIES",
        "DYLD_LIBRARY_PATH"
    }};

    bool exact_match = std::any_of(blocked.begin(), blocked.end(),
        [&key](const std::string& b) { return key == b; });

    return exact_match || has_blocked_prefix(key);
}
Activity · 3 steps
lines 333–337static const std::array<std::string, 7> blocked = {{ "LD_PRELOAD", "LD_LIBRARY_PATH", "PATH", "HOME", "SHELL", "DYLD_INSERT_LIBRARIES", "DYLD_LIBRARY_PATH" }};lines 339–340bool exact_match = std::any_of(blocked.begin(), blocked.end(), [&key](const std::string& b) { return key == b; });lines 342–342return exact_match || has_blocked_prefix(key);

Requirements

REQ-MCP-025

parse_mcp_json

void entropic::MCPJsonDiscovery::parse_mcp_json(const std::filesystem::path &path, const std::filesystem::path &own_socket, const std::set< std::string > &existing_names, std::set< std::string > &seen, std::vector< ExternalServerConfig > &out) const

Parse a single .mcp.json file.

src/mcp/mcp_json_discovery.cpp:78

Source · 30 lines
void MCPJsonDiscovery::parse_mcp_json(
    const std::filesystem::path& path,
    const std::filesystem::path& own_socket,
    const std::set<std::string>& existing_names,
    std::set<std::string>& seen,
    std::vector<ExternalServerConfig>& out) const {

    if (!std::filesystem::exists(path)) {
        return;
    }

    nlohmann::json data;
    try {
        std::ifstream f(path);
        data = nlohmann::json::parse(f);
    } catch (const std::exception& e) {
        logger->warn("Failed to read {}: {}", path.string(), e.what());
        return;
    }

    auto servers_it = data.find("mcpServers");
    if (servers_it == data.end() || !servers_it->is_object()) {
        return;
    }

    for (auto& [name, cfg] : servers_it->items()) {
        parse_server_entry(name, cfg, own_socket,
                           existing_names, seen, out);
    }
}
Activity · 9 steps
lines 85–85◇ if (!std::filesystem::exists(path))lines 86–86return;lines 89–89nlohmann::json data;lines 90–96try { std::ifstream f(path); data = nlohmann::json::parse(f); } catch (const std::exception& e) { logger->warn("Failed to read {}: {}", path.string(), e.what()); return; }lines 98–98auto servers_it = data.find("mcpServers");lines 99–99◇ if (servers_it == data.end() || !servers_it->is_object())lines 100–100return;lines 103–103◇ for (auto& [name, cfg] : servers_it->items())lines 104–105parse_server_entry(name, cfg, own_socket, existing_names, seen, out);

Requirements

REQ-MCP-025

parse_server_entry

void entropic::MCPJsonDiscovery::parse_server_entry(const std::string &name, const nlohmann::json &cfg, const std::filesystem::path &own_socket, const std::set< std::string > &existing_names, std::set< std::string > &seen, std::vector< ExternalServerConfig > &out) const

Parse one server entry from .mcp.json.

src/mcp/mcp_json_discovery.cpp:126

Source · 45 lines
void MCPJsonDiscovery::parse_server_entry(
    const std::string& name,
    const nlohmann::json& cfg,
    const std::filesystem::path& own_socket,
    const std::set<std::string>& existing_names,
    std::set<std::string>& seen,
    std::vector<ExternalServerConfig>& out) const {

    // Skip if already discovered from a higher-priority file
    if (seen.count(name) > 0) {
        return;
    }

    // Self-detection: skip own socket
    if (cfg.contains("path")) {
        auto sock = std::filesystem::path(
            cfg["path"].get<std::string>());
        if (std::filesystem::weakly_canonical(sock) ==
            std::filesystem::weakly_canonical(own_socket)) {
            logger->debug("Skipping '{}' (matches own socket)", name);
            seen.insert(name);
            return;
        }
    }

    // Shadow warning: skip if already registered
    if (existing_names.count(name) > 0) {
        logger->warn(".mcp.json entry '{}' shadowed by existing "
                     "config — skipping", name);
        seen.insert(name);
        return;
    }

    // Determine transport
    ExternalServerConfig entry;
    entry.name = name;
    std::string type = cfg.value("type", "");

    if (infer_transport(cfg, type, entry)) {
        seen.insert(name);
        out.push_back(std::move(entry));
        logger->info("Discovered external server '{}' "
                     "(transport={})", name, entry.transport);
    }
}
Activity · 19 steps
lines 135–135◇ if (seen.count(name) > 0)lines 136–136return;lines 140–140◇ if (cfg.contains("path"))lines 141–142auto sock = std::filesystem::path( cfg["path"].get<std::string>());lines 143–144◇ if (std::filesystem::weakly_canonical(sock) == std::filesystem::weakly_canonical(own_socket))lines 145–145logger->debug("Skipping '{}' (matches own socket)", name);lines 146–146seen.insert(name);lines 147–147return;lines 152–152◇ if (existing_names.count(name) > 0)lines 153–154logger->warn(".mcp.json entry '{}' shadowed by existing " "config — skipping", name);lines 155–155seen.insert(name);lines 156–156return;lines 160–160ExternalServerConfig entry;lines 161–161entry.name = name;lines 162–162std::string type = cfg.value("type", "");lines 164–164◇ if (infer_transport(cfg, type, entry))lines 165–165seen.insert(name);lines 166–166out.push_back(std::move(entry));lines 167–168logger->info("Discovered external server '{}' " "(transport={})", name, entry.transport);

Requirements

REQ-MCP-025

parse_sse_entry

bool entropic::MCPJsonDiscovery::parse_sse_entry(const nlohmann::json &cfg, ExternalServerConfig &entry) const

Parse SSE entry fields from JSON config.

src/mcp/mcp_json_discovery.cpp:215

Source · 13 lines
bool MCPJsonDiscovery::parse_sse_entry(
    const nlohmann::json& cfg,
    ExternalServerConfig& entry) const {

    entry.url = cfg.value("url", "");
    if (entry.url.empty()) {
        logger->warn(".mcp.json entry '{}' missing 'url' — skipping",
                     entry.name);
        return false;
    }
    entry.transport = "sse";
    return true;
}
Activity · 6 steps
lines 219–219entry.url = cfg.value("url", "");lines 220–220◇ if (entry.url.empty())lines 221–222logger->warn(".mcp.json entry '{}' missing 'url' — skipping", entry.name);lines 223–223return false;lines 225–225entry.transport = "sse";lines 226–226return true;

Requirements

REQ-MCP-025

parse_stdio_entry

bool entropic::MCPJsonDiscovery::parse_stdio_entry(const nlohmann::json &cfg, ExternalServerConfig &entry) const

Parse stdio entry with env blocklist enforcement.

src/mcp/mcp_json_discovery.cpp:243

Source · 32 lines
bool MCPJsonDiscovery::parse_stdio_entry(
    const nlohmann::json& cfg,
    ExternalServerConfig& entry) const {

    entry.command = cfg.value("command", "");
    if (entry.command.empty()) {
        logger->warn(".mcp.json entry '{}' missing 'command' — "
                     "skipping", entry.name);
        return false;
    }
    entry.transport = "stdio";

    // Args
    if (cfg.contains("args") && cfg["args"].is_array()) {
        for (const auto& arg : cfg["args"]) {
            entry.args.push_back(arg.get<std::string>());
        }
    }

    // Env with blocklist enforcement
    if (cfg.contains("env") && cfg["env"].is_object()) {
        for (auto& [key, val] : cfg["env"].items()) {
            if (is_blocked_env_var(key)) {
                logger->warn(".mcp.json '{}': blocked env var '{}' "
                             "— skipping", entry.name, key);
                continue;
            }
            entry.env[key] = val.get<std::string>();
        }
    }
    return true;
}
Activity · 15 steps
lines 247–247entry.command = cfg.value("command", "");lines 248–248◇ if (entry.command.empty())lines 249–250logger->warn(".mcp.json entry '{}' missing 'command' — " "skipping", entry.name);lines 251–251return false;lines 253–253entry.transport = "stdio";lines 256–256◇ if (cfg.contains("args") && cfg["args"].is_array())lines 257–257◇ for (const auto& arg : cfg["args"])lines 258–258entry.args.push_back(arg.get<std::string>());lines 263–263◇ if (cfg.contains("env") && cfg["env"].is_object())lines 264–264◇ for (auto& [key, val] : cfg["env"].items())lines 265–265◇ if (is_blocked_env_var(key))lines 266–267logger->warn(".mcp.json '{}': blocked env var '{}' " "— skipping", entry.name, key);lines 268–268continue;lines 270–270entry.env[key] = val.get<std::string>();lines 273–273return true;

Requirements

REQ-MCP-025