import{o as e}from"./rolldown-runtime-BM3Ffeng.js";import{B_ as t,Gy as n,Hy as r,Zv as i,ap as a,cn as o,dg as s,dv as c,fv as l,jg as u,kp as d,m_ as f,mv as p,ph as m,pv as h,sh as g,sn as _,ty as v}from"./index-DDwwBb9e.js";var y=e(n()),b=r();function x(){let e=d(),{data:n,status:r}=g(),x={response_type:e.query.response_type??``,client_id:e.query.client_id??``,redirect_uri:e.query.redirect_uri??``,state:e.query.state??``,code_challenge:e.query.code_challenge??``,code_challenge_method:e.query.code_challenge_method??``,scope:e.query.scope??``},{organizations:S,project:C}=a(),[w,T]=(0,y.useState)(!1);if((0,y.useEffect)(()=>{if(r!==`loading`&&!n){let t=new URLSearchParams;for(let[e,n]of Object.entries(x))n&&t.set(e,n);let n=`/mcp/authorize?${t.toString()}`;e.replace(`/auth/signin?callbackUrl=${encodeURIComponent(n)}`)}},[n,r]),r===`loading`||!n)return null;let E=e=>{m.create({title:`Authorization failed`,description:e,type:`error`,meta:{closable:!0}}),T(!1)},D=async()=>{if(C){T(!0);try{let e=await fetch(`/api/mcp/authorize`,{method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({projectId:C.id,redirect_uri:x.redirect_uri,state:x.state,code_challenge:x.code_challenge,code_challenge_method:x.code_challenge_method,client_id:x.client_id})}),t=await e.json();if(!e.ok){E(t.error??`Unknown error`);return}t.redirect?window.location.href=t.redirect:E(`No redirect URL received from server`)}catch(e){E(e instanceof Error?e.message:`Network error`)}}},O=()=>{if(x.redirect_uri){let t=new URL(x.redirect_uri);if(t.protocol===`javascript:`||t.protocol===`data:`||t.protocol===`vbscript:`){e.push(`/`);return}t.searchParams.set(`error`,`access_denied`),x.state&&t.searchParams.set(`state`,x.state),window.location.href=t.toString()}else e.push(`/`)},k=x.scope||`mcp:tools`;return(0,b.jsx)(_,{children:(0,b.jsx)(t,{maxWidth:`600px`,paddingTop:`200px`,children:(0,b.jsxs)(h,{children:[(0,b.jsx)(l,{children:(0,b.jsxs)(f,{width:`full`,align:`center`,children:[(0,b.jsx)(v,{as:`h1`,size:`md`,children:`Authorize MCP Connection`}),(0,b.jsx)(u,{}),S&&C&&(0,b.jsx)(o,{organizations:S,project:C})]})}),(0,b.jsx)(c,{children:(0,b.jsxs)(s,{align:`start`,gap:6,children:[(0,b.jsx)(i,{children:`Allow this application to access your LangWatch project tools and data?`}),(0,b.jsxs)(i,{fontSize:`sm`,color:`fg.muted`,children:[`Scopes: `,k]}),(0,b.jsxs)(f,{width:`full`,gap:2,children:[(0,b.jsx)(p,{colorScheme:`blue`,onClick:D,disabled:!C||w,loading:w,children:`Allow`}),(0,b.jsx)(p,{variant:`outline`,onClick:O,children:`Deny`})]})]})})]})})})}export{x as default};
//# sourceMappingURL=authorize-g-VGWKq8.js.map