@@ -220,72 +220,58 @@ export class MCPControllerRegister implements ControllerRegister {
220220 }
221221 }
222222 const sessionId = ctx . req . headers [ 'mcp-session-id' ] as string | undefined ;
223- if ( ! sessionId ) {
224- const ct = contentType . parse ( ctx . req . headers [ 'content-type' ] ?? '' ) ;
223+ const ct = contentType . parse ( ctx . req . headers [ 'content-type' ] ?? '' ) ;
225224
226- let body ;
225+ let body ;
227226
228- try {
229- const rawBody = await getRawBody ( ctx . req , {
230- limit : '4mb' ,
231- encoding : ct . parameters . charset ?? 'utf-8' ,
232- } ) ;
233-
234- body = JSON . parse ( rawBody ) ;
235- } catch ( e ) {
236- ctx . status = 400 ;
237- ctx . body = {
238- jsonrpc : '2.0' ,
239- error : {
240- code : - 32000 ,
241- message : `Bad Request: body should is json, ${ e . toString ( ) } ` ,
242- } ,
243- id : null ,
244- } ;
245- return ;
246- }
227+ try {
228+ const rawBody = await getRawBody ( ctx . req , {
229+ limit : '4mb' ,
230+ encoding : ct . parameters . charset ?? 'utf-8' ,
231+ } ) ;
247232
248- if ( isInitializeRequest ( body ) ) {
249- ctx . respond = false ;
250- const eventStore = this . mcpConfig . eventStore ;
251- const self = this ;
252- const transport = new StreamableHTTPServerTransport ( {
253- sessionIdGenerator : ( ) => this . mcpConfig . sessionIdGenerator ( ) ,
254- eventStore,
255- onsessioninitialized : async ( ) => {
256- if ( MCPControllerRegister . hooks . length > 0 ) {
257- for ( const hook of MCPControllerRegister . hooks ) {
258- await hook . onStreamSessionInitialized ?.( self . app . currentContext , transport , self ) ;
259- }
233+ body = JSON . parse ( rawBody ) ;
234+ } catch ( e ) {
235+ ctx . status = 400 ;
236+ ctx . body = {
237+ jsonrpc : '2.0' ,
238+ error : {
239+ code : - 32000 ,
240+ message : `Bad Request: body should is json, ${ e . toString ( ) } ` ,
241+ } ,
242+ id : null ,
243+ } ;
244+ return ;
245+ }
246+ if ( isInitializeRequest ( body ) ) {
247+ ctx . respond = false ;
248+ const eventStore = this . mcpConfig . eventStore ;
249+ const self = this ;
250+ const transport = new StreamableHTTPServerTransport ( {
251+ sessionIdGenerator : ( ) => this . mcpConfig . sessionIdGenerator ( ctx ) ,
252+ eventStore,
253+ onsessioninitialized : async ( ) => {
254+ if ( MCPControllerRegister . hooks . length > 0 ) {
255+ for ( const hook of MCPControllerRegister . hooks ) {
256+ await hook . onStreamSessionInitialized ?.( self . app . currentContext , transport , self ) ;
260257 }
261- } ,
262- } ) ;
258+ }
259+ } ,
260+ } ) ;
263261
264- ctx . set ( {
265- 'content-type' : 'text/event-stream' ,
266- 'transfer-encoding' : 'chunked' ,
267- } ) ;
262+ ctx . set ( {
263+ 'content-type' : 'text/event-stream' ,
264+ 'transfer-encoding' : 'chunked' ,
265+ } ) ;
268266
269- await self . mcpServer . connect ( transport ) ;
267+ await self . mcpServer . connect ( transport ) ;
270268
271- await ctx . app . ctxStorage . run ( ctx , async ( ) => {
272- await mw ( ctx , async ( ) => {
273- await transport . handleRequest ( ctx . req , ctx . res , body ) ;
274- await awaitEvent ( ctx . res , 'close' ) ;
275- } ) ;
269+ await ctx . app . ctxStorage . run ( ctx , async ( ) => {
270+ await mw ( ctx , async ( ) => {
271+ await transport . handleRequest ( ctx . req , ctx . res , body ) ;
272+ await awaitEvent ( ctx . res , 'close' ) ;
276273 } ) ;
277- } else {
278- ctx . status = 400 ;
279- ctx . body = {
280- jsonrpc : '2.0' ,
281- error : {
282- code : - 32000 ,
283- message : 'Bad Request: No valid session ID provided' ,
284- } ,
285- id : null ,
286- } ;
287- return ;
288- }
274+ } ) ;
289275 } else if ( sessionId ) {
290276 const transport = self . streamTransports [ sessionId ] ;
291277 if ( transport ) {
@@ -301,7 +287,7 @@ export class MCPControllerRegister implements ControllerRegister {
301287 } ) ;
302288 await ctx . app . ctxStorage . run ( ctx , async ( ) => {
303289 await mw ( ctx , async ( ) => {
304- await transport . handleRequest ( ctx . req , ctx . res ) ;
290+ await transport . handleRequest ( ctx . req , ctx . res , body ) ;
305291 await awaitEvent ( ctx . res , 'close' ) ;
306292 } ) ;
307293 } ) ;
@@ -315,6 +301,17 @@ export class MCPControllerRegister implements ControllerRegister {
315301 }
316302 }
317303 }
304+ } else {
305+ ctx . status = 400 ;
306+ ctx . body = {
307+ jsonrpc : '2.0' ,
308+ error : {
309+ code : - 32000 ,
310+ message : 'Bad Request: No valid session ID provided' ,
311+ } ,
312+ id : null ,
313+ } ;
314+ return ;
318315 }
319316 return ;
320317 } ;
0 commit comments