import{withPathPrefix as h}from"@redocly/theme/core/utils";import{importApiRoutesHandler as g}from"../../../api-routes/import-api-routes-handlers.js";import{enhanceContext as A}from"../../../api-routes/helpers/enhance-context.js";import{telemetry as T}from"../../../telemetry/index.js";import{KvService as D}from"../../../persistence/kv/services/kv-service.js";import{envConfig as o}from"../../../config/env-config.js";import{acceptsHtml as R}from"../../../utils/ai-agent-detection.js";import{handleErrorPageRender as E}from"../../../web-server/utils.js";import{MCP_NOT_ALLOWED_TEMPLATE_ID as c}from"../constants.js";async function b(n,t,e){const i=t.req.raw;if(i.method==="GET"&&R(i.headers.get("accept"))&&e.templates.has(c))return E(t,e,{slug:n.slug},405,c);const r=t.get("auth"),s=await e.resolveRouteStaticData(n)||{},m=h(n.slug),{requestHandlers:p}=await g(e.serverOutDir),l=p[n.requestHandlerId],u=(await l()).default,d=async()=>await D.getInstance({baseDbDir:e.serverOutDir,sqldRemoteDatabaseUrl:o.SQLD_REMOTE_DATABASE_URL,sqldRemoteDatabaseAuthToken:o.SQLD_REMOTE_DATABASE_AUTH_TOKEN}),f=A({honoCtx:t,ctx:{user:{teams:r.teams,email:r.claims.email,claims:r.claims,idpAccessToken:r.idpAccessToken,idpId:r.claims.idpId,isAuthenticated:r.isAuthenticated},config:e.config,outdir:e.outdir,baseUrl:new URL(t.req.url).origin},telemetry:T,getKv:d}),a=await u(t.req.raw,f,{...s,props:{...s.props,routeSlug:m,outdir:e.outdir}});return a instanceof Response?a:typeof a=="string"?new Response(a,{headers:{"Content-Type":"text/plain"}}):new Response(JSON.stringify(a),{headers:{"Content-Type":"application/json"}})}export{b as handleMcpRequest};
