@@ -105,6 +105,7 @@ export class McpGatewaySession extends Disposable {
105105 return ;
106106 }
107107
108+ this . _logService . info ( `[McpGateway][session ${ this . id } ] Tools changed, notifying client` ) ;
108109 this . _rpc . sendNotification ( { method : 'notifications/tools/list_changed' } ) ;
109110 } ) ) ;
110111
@@ -113,6 +114,7 @@ export class McpGatewaySession extends Disposable {
113114 return ;
114115 }
115116
117+ this . _logService . info ( `[McpGateway][session ${ this . id } ] Resources changed, notifying client` ) ;
116118 this . _rpc . sendNotification ( { method : 'notifications/resources/list_changed' } ) ;
117119 } ) ) ;
118120 }
@@ -126,9 +128,11 @@ export class McpGatewaySession extends Disposable {
126128
127129 res . write ( ': connected\n\n' ) ;
128130 this . _sseClients . add ( res ) ;
131+ this . _logService . info ( `[McpGateway][session ${ this . id } ] SSE client attached (total: ${ this . _sseClients . size } )` ) ;
129132
130133 res . on ( 'close' , ( ) => {
131134 this . _sseClients . delete ( res ) ;
135+ this . _logService . info ( `[McpGateway][session ${ this . id } ] SSE client detached (total: ${ this . _sseClients . size } )` ) ;
132136 } ) ;
133137 }
134138
@@ -145,6 +149,7 @@ export class McpGatewaySession extends Disposable {
145149 }
146150
147151 public override dispose ( ) : void {
152+ this . _logService . info ( `[McpGateway][session ${ this . id } ] Disposing session (SSE clients: ${ this . _sseClients . size } )` ) ;
148153 for ( const client of this . _sseClients ) {
149154 if ( ! client . destroyed ) {
150155 client . end ( ) ;
@@ -160,10 +165,12 @@ export class McpGatewaySession extends Disposable {
160165 if ( this . _isCollectingPostResponses ) {
161166 this . _pendingResponses . push ( message ) ;
162167 }
168+ this . _logService . debug ( `[McpGateway][session ${ this . id } ] --> response: ${ JSON . stringify ( message ) } ` ) ;
163169 return ;
164170 }
165171
166172 if ( isJsonRpcNotification ( message ) ) {
173+ this . _logService . debug ( `[McpGateway][session ${ this . id } ] --> notification: ${ ( message as IJsonRpcNotification ) . method } ` ) ;
167174 this . _broadcastSse ( message ) ;
168175 return ;
169176 }
@@ -173,11 +180,13 @@ export class McpGatewaySession extends Disposable {
173180
174181 private _broadcastSse ( message : JsonRpcMessage ) : void {
175182 if ( this . _sseClients . size === 0 ) {
183+ this . _logService . debug ( `[McpGateway][session ${ this . id } ] No SSE clients to broadcast to, dropping message` ) ;
176184 return ;
177185 }
178186
179187 const payload = JSON . stringify ( message ) ;
180188 const eventId = String ( ++ this . _lastEventId ) ;
189+ this . _logService . debug ( `[McpGateway][session ${ this . id } ] Broadcasting SSE event id=${ eventId } to ${ this . _sseClients . size } ` ) ;
181190 const lines = payload . split ( / \r ? \n / g) ;
182191 const data = [
183192 `id: ${ eventId } ` ,
@@ -198,11 +207,14 @@ export class McpGatewaySession extends Disposable {
198207 }
199208
200209 private async _handleRequest ( request : IJsonRpcRequest ) : Promise < unknown > {
210+ this . _logService . debug ( `[McpGateway][session ${ this . id } ] <-- request: ${ request . method } (id=${ String ( request . id ) } )` ) ;
211+
201212 if ( request . method === 'initialize' ) {
202213 return this . _handleInitialize ( request ) ;
203214 }
204215
205216 if ( ! this . _isInitialized ) {
217+ this . _logService . warn ( `[McpGateway][session ${ this . id } ] Rejected request '${ request . method } ': session not initialized` ) ;
206218 throw new JsonRpcError ( MCP_INVALID_REQUEST , 'Session is not initialized' ) ;
207219 }
208220
@@ -220,13 +232,17 @@ export class McpGatewaySession extends Disposable {
220232 case 'resources/templates/list' :
221233 return this . _handleListResourceTemplates ( ) ;
222234 default :
235+ this . _logService . warn ( `[McpGateway][session ${ this . id } ] Unknown method: ${ request . method } ` ) ;
223236 throw new JsonRpcError ( MCP_METHOD_NOT_FOUND , `Method not found: ${ request . method } ` ) ;
224237 }
225238 }
226239
227240 private _handleNotification ( notification : IJsonRpcNotification ) : void {
241+ this . _logService . debug ( `[McpGateway][session ${ this . id } ] <-- notification: ${ notification . method } ` ) ;
242+
228243 if ( notification . method === 'notifications/initialized' ) {
229244 this . _isInitialized = true ;
245+ this . _logService . info ( `[McpGateway][session ${ this . id } ] Session initialized` ) ;
230246 this . _rpc . sendNotification ( { method : 'notifications/tools/list_changed' } ) ;
231247 this . _rpc . sendNotification ( { method : 'notifications/resources/list_changed' } ) ;
232248 }
@@ -276,21 +292,27 @@ export class McpGatewaySession extends Disposable {
276292 ? params . arguments as Record < string , unknown >
277293 : { } ;
278294
295+ this . _logService . debug ( `[McpGateway][session ${ this . id } ] Calling tool '${ params . name } ' with args: ${ JSON . stringify ( argumentsValue ) } ` ) ;
296+
279297 try {
280298 const { result, serverIndex } = await this . _toolInvoker . callTool ( params . name , argumentsValue ) ;
299+ this . _logService . debug ( `[McpGateway][session ${ this . id } ] Tool '${ params . name } ' completed (isError=${ result . isError ?? false } , content blocks=${ result . content . length } )` ) ;
281300 return {
282301 ...result ,
283302 content : encodeResourceUrisInContent ( result . content , serverIndex ) ,
284303 } ;
285304 } catch ( error ) {
286- this . _logService . error ( '[McpGatewayService] Tool call invocation failed' , error ) ;
305+ this . _logService . error ( `[McpGateway][session ${ this . id } ] Tool ' ${ params . name } ' invocation failed` , error ) ;
287306 throw new JsonRpcError ( MCP_INVALID_PARAMS , String ( error ) ) ;
288307 }
289308 }
290309
291310 private _handleListTools ( ) : unknown {
292311 return this . _toolInvoker . listTools ( )
293- . then ( tools => ( { tools } ) ) ;
312+ . then ( tools => {
313+ this . _logService . debug ( `[McpGateway][session ${ this . id } ] Listed ${ tools . length } tool(s): [${ tools . map ( t => t . name ) . join ( ', ' ) } ]` ) ;
314+ return { tools } ;
315+ } ) ;
294316 }
295317
296318 private async _handleListResources ( ) : Promise < MCP . ListResourcesResult > {
@@ -304,6 +326,7 @@ export class McpGatewaySession extends Disposable {
304326 } ) ;
305327 }
306328 }
329+ this . _logService . debug ( `[McpGateway][session ${ this . id } ] Listed ${ allResources . length } resource(s) from ${ serverResults . length } server(s)` ) ;
307330 return { resources : allResources } ;
308331 }
309332
@@ -314,16 +337,18 @@ export class McpGatewaySession extends Disposable {
314337 }
315338
316339 const { serverIndex, originalUri } = decodeGatewayResourceUri ( params . uri ) ;
340+ this . _logService . debug ( `[McpGateway][session ${ this . id } ] Reading resource '${ originalUri } ' from server ${ serverIndex } ` ) ;
317341 try {
318342 const result = await this . _toolInvoker . readResource ( serverIndex , originalUri ) ;
343+ this . _logService . debug ( `[McpGateway][session ${ this . id } ] Resource read returned ${ result . contents . length } content(s)` ) ;
319344 return {
320345 contents : result . contents . map ( content => ( {
321346 ...content ,
322347 uri : encodeGatewayResourceUri ( content . uri , serverIndex ) ,
323348 } ) ) ,
324349 } ;
325350 } catch ( error ) {
326- this . _logService . error ( '[McpGatewayService] Resource read failed' , error ) ;
351+ this . _logService . error ( `[McpGateway][session ${ this . id } ] Resource read failed for ' ${ originalUri } '` , error ) ;
327352 throw new JsonRpcError ( MCP_INVALID_PARAMS , String ( error ) ) ;
328353 }
329354 }
@@ -339,6 +364,7 @@ export class McpGatewaySession extends Disposable {
339364 } ) ;
340365 }
341366 }
367+ this . _logService . debug ( `[McpGateway][session ${ this . id } ] Listed ${ allTemplates . length } resource template(s) from ${ serverResults . length } server(s)` ) ;
342368 return { resourceTemplates : allTemplates } ;
343369 }
344370}
0 commit comments