import { createRequire } from "node:module";
var __defProp = Object.defineProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, {
      get: all[name],
      enumerable: true,
      configurable: true,
      set: (newValue) => all[name] = () => newValue
    });
};
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
var __require = /* @__PURE__ */ createRequire(import.meta.url);

// src/tools/mcp-client-helpers.ts
import {
  Experimental_StdioMCPTransport as StdioClientTransport
} from "@ai-sdk/mcp/mcp-stdio";
function buildMcpTransport(config) {
  const transport = resolveTransportType(config);
  if (transport === "stdio") {
    const stdioConfig = resolveStdioConfig(config);
    return new StdioClientTransport(stdioConfig);
  }
  const remoteConfig = config;
  const headers = resolveRemoteHeaders(remoteConfig);
  const remoteTransport = {
    type: transport,
    url: requireNonEmptyString(remoteConfig.url, "url", config.name)
  };
  if (headers) {
    remoteTransport.headers = headers;
  }
  if (remoteConfig.authProvider) {
    remoteTransport.authProvider = remoteConfig.authProvider;
  }
  return remoteTransport;
}
function prefixToolNames(config, tools) {
  const prefix = config.toolPrefix?.trim();
  if (!prefix) {
    return tools;
  }
  const prefixedTools = {};
  for (const [toolName, tool] of Object.entries(tools)) {
    prefixedTools[`${prefix}_${toolName}`] = tool;
  }
  return prefixedTools;
}
function getErrorMessage(error) {
  if (error instanceof Error) {
    return error.message;
  }
  return String(error);
}
function resolveTransportType(config) {
  return config.transport ?? "stdio";
}
function resolveStdioConfig(config) {
  const stdioConfig = config;
  return {
    command: requireNonEmptyString(stdioConfig.command, "command", config.name),
    args: stdioConfig.args,
    env: stdioConfig.env,
    cwd: stdioConfig.cwd
  };
}
function resolveRemoteHeaders(config) {
  const headers = {
    ...config.headers ?? {}
  };
  const accessToken = config.accessToken ?? resolveEnvToken(config.accessTokenEnvVar);
  if (accessToken && headers.Authorization === undefined) {
    headers.Authorization = `Bearer ${accessToken}`;
  }
  return Object.keys(headers).length > 0 ? headers : undefined;
}
function resolveEnvToken(envVarName) {
  if (!envVarName) {
    return;
  }
  const value = process.env[envVarName];
  if (!value) {
    return;
  }
  const trimmed = value.trim();
  return trimmed.length > 0 ? trimmed : undefined;
}
function requireNonEmptyString(value, field, serverName) {
  if (!value) {
    throw new Error(`MCP server "${serverName}" is missing required "${field}".`);
  }
  const trimmed = value.trim();
  if (trimmed.length === 0) {
    throw new Error(`MCP server "${serverName}" has an empty "${field}".`);
  }
  return trimmed;
}
var init_mcp_client_helpers = () => {};
init_mcp_client_helpers();

export {
  prefixToolNames,
  getErrorMessage,
  buildMcpTransport
};
