import{telemetry as u}from"../../../telemetry/index.js";import{mcpToolHandlers as p}from"../../../../client/mcp-tool-handlers-entry.js";import{getMcpMode as m}from"../mode.js";import{reportMcpToolCalled as w,reportMcpToolError as f}from"./mcp-tool-telemetry.js";async function b(n){u.initialize();const{toolName:t,args:c,context:o,extra:l}=n,r=m();try{const e=p[t];if(!e)throw new Error(`Unknown MCP tool: ${t}`);const{default:d}=await e(),i=d[t];if(!i)throw new Error(`MCP tool module does not export a handler for "${t}". Expected \`export default { '${t}': handler }\`.`);const a=await(typeof i=="function"?i:i.execute)(c,o,l);if(!x(a))throw new Error(`MCP tool "${t}" returned an invalid result.`);if(a.isError)f(t,a.content.map(({text:s})=>s).join(" "),r);else{const s=a.content.map(({text:h})=>h).join("").length;w(t,r,s)}return a}catch(e){throw f(t,e instanceof Error?e.message:String(e),r,e instanceof Error&&e.stack||""),e}}function x(n){return!n||typeof n!="object"?!1:Array.isArray(n.content)}async function T(n){u.initialize();const{toolName:t,context:c}=n;try{const o=p[t];if(!o)return!1;const{default:l}=await o(),r=l[t];return r?typeof r=="object"&&typeof r.isAvailable=="function"?await r.isAvailable(c):!0:!1}catch(o){throw f(t,"Failed to check if MCP tool is available: "+(o instanceof Error?o.message:String(o)),m(),o instanceof Error&&o.stack||""),o}}export{b as executeMcpTool,T as isMcpToolAvailable};
