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