import{readFileSync as S,writeFileSync as y,existsSync as u,mkdirSync as k}from"node:fs";import{join as d}from"node:path";import{homedir as v}from"node:os";var C=new Map;function a(e){return C.get(e)||null}var f=d(v(),".cursor","mcp.json");function h(e){let r=new Set;if(!e||typeof e!="object")return r;for(let o of Object.keys(e)){let t=e[o];if(Array.isArray(t.tools))for(let s of t.tools)r.add(s)}return r}function x(e){let r=h(e);if(r.size===0)return console.log("[MCP Config] No tools requested \u2014 skipping MCP config write"),!1;let o={mcpServers:{}};try{u(f)&&(o=JSON.parse(S(f,"utf-8")),o.mcpServers||(o.mcpServers={}))}catch(i){console.warn(`[MCP Config] Could not read existing config, starting fresh: ${i.message}`),o={mcpServers:{}}}let t=0;for(let i of r){let n=a(i);if(!n){console.log(`[MCP Config] Unknown skill "${i}" \u2014 skipping`);continue}if(o.mcpServers[n.serverName]){console.log(`[MCP Config] Server "${n.serverName}" already configured \u2014 skipping`);continue}let g={},p=!1;for(let c of n.envKeys||[]){let m=process.env[c];if(!m){console.warn(`[MCP Config] Missing env var ${c} for skill "${i}" \u2014 skipping server`),p=!0;break}g[c]=m}if(p)continue;let l=(n.args||[]).map(c=>c);if(n.command==="node"&&l.length>0&&!u(l[0])){console.warn(`[MCP Config] Binary not found at ${l[0]} for "${i}" \u2014 skipping server`);continue}o.mcpServers[n.serverName]={command:n.command,args:l,env:g,description:n.description},t++,console.log(`[MCP Config] Added "${n.serverName}" server`)}if(t===0)return console.log("[MCP Config] No new MCP servers to add"),!1;let s=d(v(),".cursor");return u(s)||k(s,{recursive:!0}),y(f,JSON.stringify(o,null,2),"utf-8"),console.log(`[MCP Config] Wrote ${f} with ${Object.keys(o.mcpServers).length} server(s)`),!0}function N(e){if(!Array.isArray(e)||e.length===0)return"";let r=[];for(let o of e){let t=a(o);if(!t)continue;let s=(t.tools||[]).map(i=>`- ${i.name}: ${i.description}`).join(`
`);r.push(`### ${t.description}
${s}`)}return r.length===0?"":`

AVAILABLE MCP TOOLS:
You have access to the following MCP tools. Use them when your task requires interacting with these services.

${r.join(`

`)}`}export{h as collectRequiredTools,N as getToolDescriptions,x as writeMcpConfig};
