1 line
30 KiB
JavaScript
1 line
30 KiB
JavaScript
|
|
(()=>{"use strict";var e={142:e=>{e.exports=require("next/dist/compiled/content-type")},238:e=>{e.exports=require("next/dist/compiled/raw-body")},788:e=>{e.exports=require("next/dist/compiled/zod")},5:e=>{e.exports=require("node:crypto")},685:function(e,t,s){var o=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:true});t.StreamableHTTPServerTransport=void 0;const a=s(383);const i=o(s(238));const n=o(s(142));const r=s(5);const c="4mb";class StreamableHTTPServerTransport{constructor(e){var t,s;this._started=false;this._streamMapping=new Map;this._requestToStreamMapping=new Map;this._requestResponseMap=new Map;this._initialized=false;this._enableJsonResponse=false;this._standaloneSseStreamId="_GET_stream";this.sessionIdGenerator=e.sessionIdGenerator;this._enableJsonResponse=(t=e.enableJsonResponse)!==null&&t!==void 0?t:false;this._eventStore=e.eventStore;this._onsessioninitialized=e.onsessioninitialized;this._onsessionclosed=e.onsessionclosed;this._allowedHosts=e.allowedHosts;this._allowedOrigins=e.allowedOrigins;this._enableDnsRebindingProtection=(s=e.enableDnsRebindingProtection)!==null&&s!==void 0?s:false}async start(){if(this._started){throw new Error("Transport already started")}this._started=true}validateRequestHeaders(e){if(!this._enableDnsRebindingProtection){return undefined}if(this._allowedHosts&&this._allowedHosts.length>0){const t=e.headers.host;if(!t||!this._allowedHosts.includes(t)){return`Invalid Host header: ${t}`}}if(this._allowedOrigins&&this._allowedOrigins.length>0){const t=e.headers.origin;if(!t||!this._allowedOrigins.includes(t)){return`Invalid Origin header: ${t}`}}return undefined}async handleRequest(e,t,s){var o;const a=this.validateRequestHeaders(e);if(a){t.writeHead(403).end(JSON.stringify({jsonrpc:"2.0",error:{code:-32e3,message:a},id:null}));(o=this.onerror)===null||o===void 0?void 0:o.call(this,new Error(a));return}if(e.method==="POST"){await this.handlePostRequest(e,t,s)}else if(e.method==="GET"){await this.handleGetRequest(e,t)}else if(e.method==="DELETE"){await this.handleDeleteRequest(e,t)}else{await this.handleUnsupportedRequest(t)}}async handleGetRequest(e,t){const s=e.headers.accept;if(!(s===null||s===void 0?void 0:s.includes("text/event-stream"))){t.writeHead(406).end(JSON.stringify({jsonrpc:"2.0",error:{code:-32e3,message:"Not Acceptable: Client must accept text/event-stream"},id:null}));return}if(!this.validateSession(e,t)){return}if(!this.validateProtocolVersion(e,t)){return}if(this._eventStore){const s=e.headers["last-event-id"];if(s){await this.replayEvents(s,t);return}}const o={"Content-Type":"text/event-stream","Cache-Control":"no-cache, no-transform",Connection:"keep-alive"};if(this.sessionId!==undefined){o["mcp-session-id"]=this.sessionId}if(this._streamMapping.get(this._standaloneSseStreamId)!==undefined){t.writeHead(409).end(JSON.stringify({jsonrpc:"2.0",error:{code:-32e3,message:"Conflict: Only one SSE stream is allowed per session"},id:null}));return}t.writeHead(200,o).flushHeaders();this._streamMapping.set(this._standaloneSseStreamId,t);t.on("close",(()=>{this._streamMapping.delete(this._standaloneSseStreamId)}));t.on("error",(e=>{var t;(t=this.onerror)===null||t===void 0?void 0:t.call(this,e)}))}async replayEvents(e,t){var s,o;if(!this._eventStore){return}try{const o={"Content-Type":"text/event-stream","Cache-Control":"no-cache, no-transform",Connection:"keep-alive"};if(this.sessionId!==undefined){o["mcp-session-id"]=this.sessionId}t.writeHead(200,o).flushHeaders();const a=await((s=this._eventStore)===null||s===void 0?void 0:s.replayEventsAfter(e,{send:async(e,s)=>{var o;if(!this.writeSSEEvent(t,s,e)){(o=this.onerror)===null||o===void 0?void 0:o.call(this,new Error("Failed replay events"));t.end()}}}));this._streamMapping.set(a,t);t.on("error",(e=>{var t;(t=this.onerror)===null||t===void 0?void 0:t.call(this,e)}))}catch(e){(o=this.onerror)===null||o===void 0?void 0:o.call(this,e)}}writeSSEEvent(e,t,s){let o=`event: message\n`;if(s){o+=`id: ${s}\n`}o+=`data: ${JSON.stringify(t)}\n\n`;return e.write(o
|