var Z=Object.defineProperty;var $=(f)=>f;function x(f,E){this[f]=$.bind(null,E)}var I=(f,E)=>{for(var R in E)Z(f,R,{get:E[R],enumerable:!0,configurable:!0,set:x.bind(E,R)})};var l=(f,E)=>()=>(f&&(E=f(f=0)),E);var G=["application/json","text/event-stream"];function H(f){return!f||f.includes("*/*")||f.includes("application/*")||G.some((E)=>f.includes(E))}function k(f){if(f.method!=="POST")return f;let E=f.headers.get("accept");if(!H(E))return f;let R=G.filter((_)=>!E?.includes(_));if(R.length===0)return f;let z=new Headers(f.headers);return z.set("accept",[E,...R].filter(Boolean).join(", ")),new Request(f,{headers:z})}import{createMcpServer as W}from"@contractspec/lib.contracts-runtime-server-mcp/provider-mcp";import{PresentationRegistry as c}from"@contractspec/lib.contracts-spec/presentations";import{McpServer as M}from"@modelcontextprotocol/sdk/server/mcp.js";import{WebStandardStreamableHTTPServerTransport as b}from"@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js";import{randomUUID as A}from"crypto";import{Elysia as N}from"elysia";var n={actor:"anonymous",decide:async()=>({effect:"allow"})};function F(f,E,R,z){return new Response(JSON.stringify({jsonrpc:"2.0",error:{code:E,message:R,...z?{data:z}:{}},id:null}),{status:f,headers:{"content-type":"application/json"}})}function J({logger:f,serverName:E,ops:R,resources:z,prompts:_,presentations:O,stateful:Q}){let y=new M({name:E,version:"1.0.0"},{capabilities:{tools:{},resources:{},prompts:{},logging:{}}});f.info("Setting up MCP server..."),W(y,R,z,_,{logger:f,toolCtx:()=>n,promptCtx:()=>({locale:"en"}),resourceCtx:()=>({locale:"en"}),presentations:new c(O)});let T=new b({sessionIdGenerator:Q?()=>A():void 0,enableJsonResponse:!0});return y.connect(T).then(()=>({server:y,transport:T}))}async function U(f){await Promise.allSettled([f.transport.close(),f.server.close()])}function m(f){return f instanceof Error?f.stack??f.message:String(f)}function o({logger:f,path:E,serverName:R,ops:z,resources:_,prompts:O,presentations:Q,validateAuth:y,requiredAuthMethods:T}){f.info("Setting up MCP handler...",{requiredAuthMethods:T??[]});let K=process.env.CONTRACTSPEC_MCP_STATEFUL==="1",D=new Map;async function L(j){let w=await J({logger:f,path:E,serverName:R,ops:z,resources:_,prompts:O,presentations:Q,stateful:!1});try{return await w.transport.handleRequest(k(j))}finally{await U(w)}}async function V(j){let w=D.get(j);if(!w)return;D.delete(j),await U(w)}async function X(j){let w=j.headers.get("mcp-session-id"),P,Y=!1;if(w){let B=D.get(w);if(!B)return F(404,-32001,"Session not found");P=B}else P=await J({logger:f,path:E,serverName:R,ops:z,resources:_,prompts:O,presentations:Q,stateful:!0}),Y=!0;try{let B=await P.transport.handleRequest(k(j)),C=P.transport.sessionId;if(C&&!D.has(C))D.set(C,P);if(j.method==="DELETE"&&C)await V(C);else if(!C&&Y)await U(P);return B}catch(B){if(Y)await U(P);throw B}}return new N({name:`mcp-${R}`}).all(E,async({request:j})=>{try{if(y){let w=await y(j);if(!w.valid)return F(401,-32002,"Authentication failed",w.reason)}if(K)return await X(j);return await L(j)}catch(w){return f.error("Error handling MCP request",{path:E,method:j.method,error:m(w)}),F(500,-32000,"Internal error")}})}export{o as createMcpElysiaHandler};
