@@ -115,19 +115,23 @@ export class Server extends Protocol<ServerContext> {
115115 this . setNotificationHandler ( 'notifications/initialized' , ( ) => this . oninitialized ?.( ) ) ;
116116
117117 if ( this . _capabilities . logging ) {
118- this . setRequestHandler ( 'logging/setLevel' , async ( request , ctx ) => {
119- const transportSessionId : string | undefined =
120- ctx . sessionId || ( ctx . http ?. req ?. headers . get ( 'mcp-session-id' ) as string ) || undefined ;
121- const { level } = request . params ;
122- const parseResult = parseSchema ( LoggingLevelSchema , level ) ;
123- if ( parseResult . success ) {
124- this . _loggingLevels . set ( transportSessionId , parseResult . data ) ;
125- }
126- return { } ;
127- } ) ;
118+ this . _registerLoggingHandler ( ) ;
128119 }
129120 }
130121
122+ private _registerLoggingHandler ( ) : void {
123+ this . setRequestHandler ( 'logging/setLevel' , async ( request , ctx ) => {
124+ const transportSessionId : string | undefined =
125+ ctx . sessionId || ( ctx . http ?. req ?. headers . get ( 'mcp-session-id' ) as string ) || undefined ;
126+ const { level } = request . params ;
127+ const parseResult = parseSchema ( LoggingLevelSchema , level ) ;
128+ if ( parseResult . success ) {
129+ this . _loggingLevels . set ( transportSessionId , parseResult . data ) ;
130+ }
131+ return { } ;
132+ } ) ;
133+ }
134+
131135 protected override buildContext ( ctx : BaseContext , transportInfo ?: MessageExtraInfo ) : ServerContext {
132136 // Only create http when there's actual HTTP transport info or auth info
133137 const hasHttpInfo =
@@ -188,7 +192,11 @@ export class Server extends Protocol<ServerContext> {
188192 if ( this . transport ) {
189193 throw new SdkError ( SdkErrorCode . AlreadyConnected , 'Cannot register capabilities after connecting to transport' ) ;
190194 }
195+ const hadLogging = ! ! this . _capabilities . logging ;
191196 this . _capabilities = mergeCapabilities ( this . _capabilities , capabilities ) ;
197+ if ( ! hadLogging && this . _capabilities . logging ) {
198+ this . _registerLoggingHandler ( ) ;
199+ }
192200 }
193201
194202 /**
0 commit comments