// @bun
var g=Object.defineProperty;var m=(t)=>t;function M(t,r){this[t]=m.bind(null,r)}var k=(t,r)=>{for(var n in r)g(t,n,{get:r[n],enumerable:!0,configurable:!0,set:M.bind(r,n)})};var C=(t,r)=>()=>(t&&(r=t(t=0)),r);var x=import.meta.require;import{Experimental_StdioMCPTransport as v}from"@ai-sdk/mcp/mcp-stdio";function u(t){let r=R(t);if(r==="stdio"){let s=y(t);return new v(s)}let n=t,e=w(n),d={type:r,url:f(n.url,"url",t.name)};if(e)d.headers=e;if(n.authProvider)d.authProvider=n.authProvider;return d}function a(t,r){let n=t.toolPrefix?.trim();if(!n)return r;let e={};for(let[d,s]of Object.entries(r))e[`${n}_${d}`]=s;return e}function h(t){if(t instanceof Error)return t.message;return String(t)}function R(t){return t.transport??"stdio"}function y(t){let r=t;return{command:f(r.command,"command",t.name),args:r.args,env:r.env,cwd:r.cwd}}function w(t){let r={...t.headers??{}},n=t.accessToken??P(t.accessTokenEnvVar);if(n&&r.Authorization===void 0)r.Authorization=`Bearer ${n}`;return Object.keys(r).length>0?r:void 0}function P(t){if(!t)return;let r=process.env[t];if(!r)return;let n=r.trim();return n.length>0?n:void 0}function f(t,r,n){if(!t)throw Error(`MCP server "${n}" is missing required "${r}".`);let e=t.trim();if(e.length===0)throw Error(`MCP server "${n}" has an empty "${r}".`);return e}var T=()=>{};import{experimental_createMCPClient as S}from"@ai-sdk/mcp";async function c(t){let r=null;try{let n=u(t);r=await S({transport:n,name:t.clientName,version:t.clientVersion});let e=await r.tools(),d=a(t,e),s=r;return{tools:d,cleanup:()=>s.close(),serverToolNames:{[t.name]:Object.keys(d)}}}catch(n){if(r)await r.close().catch(()=>{return});throw Error(`[MCP:${t.name}] Failed to connect tools: ${h(n)}`)}}async function B(t,r={}){let n=[];try{for(let o of t){let i=await c(o);n.push(i)}}catch(o){throw await Promise.allSettled(n.map((i)=>i.cleanup())),o}let e={},d={},s=r.onNameCollision??"overwrite";try{for(let o of n){for(let[i,p]of Object.entries(o.serverToolNames))d[i]=p;for(let[i,p]of Object.entries(o.tools)){if(e[i]!==void 0&&s==="error")throw Error(`Duplicate MCP tool name "${i}" detected. Use "toolPrefix" or set onNameCollision to "overwrite".`);e[i]=p}}}catch(o){throw await Promise.allSettled(n.map((i)=>i.cleanup())),o}return{tools:e,serverToolNames:d,cleanup:async()=>{let i=(await Promise.allSettled(n.map((p)=>p.cleanup()))).filter((p)=>p.status==="rejected");if(i.length>0)throw Error(`Failed to cleanup ${i.length} MCP client connection(s).`)}}}var E=C(()=>{T()});E();export{c as mcpServerToTools,B as createMcpToolsets};
