import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url);

// src/adapters/claude/session/mcp-config.ts
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
function loadUserClaudeJsonMcpServerEntries(cwd, logger, homeDir = os.homedir()) {
  const claudeJsonPath = path.join(homeDir, ".claude.json");
  let raw;
  try {
    raw = fs.readFileSync(claudeJsonPath, "utf8");
  } catch {
    return [];
  }
  let cfg;
  try {
    cfg = JSON.parse(raw);
  } catch (err) {
    logger?.warn("Failed to parse ~/.claude.json", {
      error: err instanceof Error ? err.message : String(err)
    });
    return [];
  }
  const sections = [
    { scope: "user", servers: cfg.mcpServers },
    {
      scope: "project",
      servers: cwd ? cfg.projects?.[cwd]?.mcpServers : void 0
    }
  ];
  const byName = /* @__PURE__ */ new Map();
  for (const { scope, servers } of sections) {
    if (!servers || typeof servers !== "object") continue;
    for (const [name, config] of Object.entries(
      servers
    )) {
      byName.set(name, { name, scope, config });
    }
  }
  return [...byName.values()];
}
function loadUserClaudeJsonMcpServers(cwd, logger, homeDir = os.homedir()) {
  const servers = {};
  for (const entry of loadUserClaudeJsonMcpServerEntries(
    cwd,
    logger,
    homeDir
  )) {
    servers[entry.name] = entry.config;
  }
  return servers;
}
function sanitizeHeaders(headers) {
  if (!headers || typeof headers !== "object") return void 0;
  const entries = Object.entries(headers).filter(
    (entry) => typeof entry[1] === "string"
  );
  return entries.length > 0 ? Object.fromEntries(entries) : void 0;
}
function parseClaudeJsonTransport(config) {
  const raw = config;
  const type = typeof raw.type === "string" ? raw.type : void 0;
  const url = typeof raw.url === "string" ? raw.url : void 0;
  const command = typeof raw.command === "string" ? raw.command : void 0;
  if ((type === "http" || type === "sse") && url) {
    return { kind: type, url, headers: sanitizeHeaders(raw.headers) };
  }
  if ((type === void 0 || type === "stdio") && command) {
    const args = Array.isArray(raw.args) ? raw.args.filter((arg) => typeof arg === "string") : void 0;
    return { kind: "stdio", command, args, env: sanitizeHeaders(raw.env) };
  }
  if (type === void 0 && url) {
    return { kind: "http", url, headers: sanitizeHeaders(raw.headers) };
  }
  return { kind: "unknown" };
}
function toTransport(config) {
  const transport = parseClaudeJsonTransport(config);
  switch (transport.kind) {
    case "http":
    case "sse":
      return {
        type: transport.kind,
        url: transport.url,
        headers: transport.headers
      };
    case "stdio":
      return {
        type: "stdio",
        command: transport.command,
        args: transport.args
      };
    default:
      return { type: "unknown" };
  }
}
function loadUserClaudeJsonMcpServerDescriptors(cwd, logger, homeDir = os.homedir()) {
  return loadUserClaudeJsonMcpServerEntries(cwd, logger, homeDir).map(
    (entry) => ({
      name: entry.name,
      scope: entry.scope,
      transport: toTransport(entry.config)
    })
  );
}
function parseMcpServers(params, logger) {
  const mcpServers = {};
  if (!Array.isArray(params.mcpServers)) {
    return mcpServers;
  }
  for (const server of params.mcpServers) {
    if ("type" in server) {
      if (server.type === "http" || server.type === "sse") {
        mcpServers[server.name] = {
          type: server.type,
          url: server.url,
          headers: server.headers ? Object.fromEntries(
            server.headers.map((e) => [
              e.name,
              e.value
            ])
          ) : void 0
        };
      } else {
        logger?.warn("parseMcpServers: dropping unsupported MCP server type", {
          name: server.name,
          type: server.type
        });
      }
    } else {
      mcpServers[server.name] = {
        type: "stdio",
        command: server.command,
        args: server.args,
        env: server.env ? Object.fromEntries(server.env.map((e) => [e.name, e.value])) : void 0
      };
    }
  }
  return mcpServers;
}
export {
  loadUserClaudeJsonMcpServerDescriptors,
  loadUserClaudeJsonMcpServerEntries,
  loadUserClaudeJsonMcpServers,
  parseClaudeJsonTransport,
  parseMcpServers,
  sanitizeHeaders
};
//# sourceMappingURL=mcp-config.js.map