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
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
Requirements
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
Requirements
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
Requirements
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
Requirements
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
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
Requirements
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
Requirements
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
Requirements
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
Requirements
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;
}