import{withPathPrefix as u}from"@redocly/theme/core/utils";import{ServerRoutes as m}from"../../../../../constants/common.js";import{telemetry as s}from"../../../../telemetry/index.js";import{processDocuments as y}from"./utils.js";const o="search",f={type:"object",required:["query"],additionalProperties:!1,properties:{query:{type:"string",description:"Search query. Should be a single word or that phrase that is presented in a documentation.",minLength:1}}};function k(n,a,c,i){n.tool(o,"Search across the documentation to fetch relevant content for a given query",f,async({query:p})=>{try{const e=JSON.stringify({query:p}),h=`${a}${u(m.SEARCH)}`.replace("http","https"),d=`authorization=${String(i?.authorization).replace(/^Bearer /,"")}`,t=await fetch(h,{method:"POST",credentials:"include",headers:{"Content-Type":"application/json",Cookie:d},body:e});if(!t.ok)throw new Error(`Search request failed with status ${t.status}`);const l=await t.json(),r=y(l.documents||{},c);return s.sendMcpToolCalledMessage({server_type:"docs",tool:o}),{content:[{type:"text",text:r.trim().length?r:"No results found."}]}}catch(e){throw s.sendMcpErrorMessage({server_type:"docs",tool:o,message:e?.message||"",stack:e?.stack||""}),e}})}export{k as registerSearchTool};
