import { dynamicTool } from "ai";
import { toSafeZodSchema } from "./schema-utils.js";
function convertMcpToolsToAiSdk(mcpClient, mcpTools) {
  const aiTools = {};
  for (const mcpTool of mcpTools) {
    try {
      const safeSchema = toSafeZodSchema(mcpTool.inputSchema);
      aiTools[mcpTool.name] = dynamicTool({
        description: mcpTool.description,
        inputSchema: safeSchema,
        execute: async (args) => {
          const result = await mcpClient.callTool(mcpTool.name, args);
          if (!result.success) {
            throw new Error(result.error || "Tool execution failed");
          }
          return formatToolResult(result.result);
        }
      });
    } catch (error) {
      console.error(`[MCP Adapter] Error converting tool "${mcpTool.name}":`, error);
    }
  }
  return aiTools;
}
function formatToolResult(result) {
  if (result === null || result === void 0) {
    return "No result returned";
  }
  if (typeof result === "string") {
    return result;
  }
  if (typeof result === "number" || typeof result === "boolean") {
    return String(result);
  }
  try {
    return JSON.stringify(result, null, 2);
  } catch {
    return String(result);
  }
}
export {
  convertMcpToolsToAiSdk
};
//# sourceMappingURL=mcp-tool-adapter.js.map
