88} from '@modelcontextprotocol/sdk/types.js' ;
99import { IncomingMessage , ServerResponse } from 'node:http' ;
1010
11+ import { logger } from './logger' ;
12+
1113type RequestId = number | string ;
1214
1315type ConnectionContext = {
@@ -50,6 +52,7 @@ export class JsonHTTPServerTransport implements Transport {
5052 } catch {
5153 // ignore
5254 }
55+ logger . info ( 'SSE connection terminated during transport close' ) ;
5356 this . sse = undefined ;
5457 }
5558 this . connections . forEach ( ( ctx ) => {
@@ -144,11 +147,13 @@ export class JsonHTTPServerTransport implements Transport {
144147 res,
145148 keepalive,
146149 } ;
150+ logger . info ( { headers : req . headers } , 'SSE connection established' ) ;
147151
148152 res . on ( 'close' , ( ) => {
149153 try {
150154 clearInterval ( keepalive ) ;
151155 } finally {
156+ logger . info ( 'SSE connection closed by client' ) ;
152157 this . sse = undefined ;
153158 }
154159 } ) ;
@@ -166,6 +171,13 @@ export class JsonHTTPServerTransport implements Transport {
166171 id : null ,
167172 } ) ,
168173 ) ;
174+ logger . warn (
175+ {
176+ method : req . method ,
177+ headers : req . headers ,
178+ } ,
179+ 'Rejected non-POST HTTP request' ,
180+ ) ;
169181 return ;
170182 }
171183
@@ -185,6 +197,7 @@ export class JsonHTTPServerTransport implements Transport {
185197 id : null ,
186198 } ) ,
187199 ) ;
200+ logger . warn ( { headers : req . headers } , 'Rejected POST due to unacceptable Accept header' ) ;
188201 return ;
189202 }
190203
@@ -201,6 +214,7 @@ export class JsonHTTPServerTransport implements Transport {
201214 id : null ,
202215 } ) ,
203216 ) ;
217+ logger . warn ( { headers : req . headers } , 'Rejected POST due to unsupported Content-Type' ) ;
204218 return ;
205219 }
206220
@@ -230,6 +244,7 @@ export class JsonHTTPServerTransport implements Transport {
230244 authInfo : req . auth ,
231245 } ) ;
232246 }
247+ logger . info ( 'Accepted POST without requests (notifications only)' ) ;
233248 return ;
234249 }
235250
@@ -246,6 +261,7 @@ export class JsonHTTPServerTransport implements Transport {
246261 authInfo : req . auth ,
247262 } ) ;
248263 }
264+ logger . info ( 'POST handled with SSE active: responded 202 and streaming via SSE' ) ;
249265 return ;
250266 }
251267
@@ -271,6 +287,7 @@ export class JsonHTTPServerTransport implements Transport {
271287 authInfo : req . auth ,
272288 } ) ;
273289 }
290+ logger . info ( { requestIds : orderedIds } , 'POST handled with JSON response mode' ) ;
274291 } catch ( error ) {
275292 this . onerror ?.( error as Error ) ;
276293 res . writeHead ( 400 ) ;
@@ -285,6 +302,7 @@ export class JsonHTTPServerTransport implements Transport {
285302 id : null ,
286303 } ) ,
287304 ) ;
305+ logger . error ( error as Error , 'HTTP request handling parse/validation error' ) ;
288306 }
289307 }
290308}
0 commit comments