11using Microsoft . Extensions . Logging ;
2- using ModelContextProtocol . Logging ;
32using ModelContextProtocol . Protocol . Messages ;
43using ModelContextProtocol . Protocol . Transport ;
54using ModelContextProtocol . Protocol . Types ;
109namespace ModelContextProtocol . Client ;
1110
1211/// <inheritdoc/>
13- internal sealed class McpClient : McpEndpoint , IMcpClient
12+ internal sealed partial class McpClient : McpEndpoint , IMcpClient
1413{
1514 private static Implementation DefaultImplementation { get ; } = new ( )
1615 {
@@ -133,9 +132,12 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)
133132 cancellationToken : initializationCts . Token ) . ConfigureAwait ( false ) ;
134133
135134 // Store server information
136- _logger . ServerCapabilitiesReceived ( EndpointName ,
137- capabilities : JsonSerializer . Serialize ( initializeResponse . Capabilities , McpJsonUtilities . JsonContext . Default . ServerCapabilities ) ,
138- serverInfo : JsonSerializer . Serialize ( initializeResponse . ServerInfo , McpJsonUtilities . JsonContext . Default . Implementation ) ) ;
135+ if ( _logger . IsEnabled ( LogLevel . Information ) )
136+ {
137+ LogServerCapabilitiesReceived ( EndpointName ,
138+ capabilities : JsonSerializer . Serialize ( initializeResponse . Capabilities , McpJsonUtilities . JsonContext . Default . ServerCapabilities ) ,
139+ serverInfo : JsonSerializer . Serialize ( initializeResponse . ServerInfo , McpJsonUtilities . JsonContext . Default . Implementation ) ) ;
140+ }
139141
140142 _serverCapabilities = initializeResponse . Capabilities ;
141143 _serverInfo = initializeResponse . ServerInfo ;
@@ -144,7 +146,7 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)
144146 // Validate protocol version
145147 if ( initializeResponse . ProtocolVersion != _options . ProtocolVersion )
146148 {
147- _logger . ServerProtocolVersionMismatch ( EndpointName , _options . ProtocolVersion , initializeResponse . ProtocolVersion ) ;
149+ LogServerProtocolVersionMismatch ( EndpointName , _options . ProtocolVersion , initializeResponse . ProtocolVersion ) ;
148150 throw new McpException ( $ "Server protocol version mismatch. Expected { _options . ProtocolVersion } , got { initializeResponse . ProtocolVersion } ") ;
149151 }
150152
@@ -155,13 +157,13 @@ await SendMessageAsync(
155157 }
156158 catch ( OperationCanceledException oce ) when ( initializationCts . IsCancellationRequested )
157159 {
158- _logger . ClientInitializationTimeout ( EndpointName ) ;
160+ LogClientInitializationTimeout ( EndpointName ) ;
159161 throw new McpException ( "Initialization timed out" , oce ) ;
160162 }
161163 }
162164 catch ( Exception e )
163165 {
164- _logger . ClientInitializationError ( EndpointName , e ) ;
166+ LogClientInitializationError ( EndpointName , e ) ;
165167 await DisposeAsync ( ) . ConfigureAwait ( false ) ;
166168 throw ;
167169 }
@@ -188,4 +190,16 @@ public override async ValueTask DisposeUnsynchronizedAsync()
188190 }
189191 }
190192 }
193+
194+ [ LoggerMessage ( Level = LogLevel . Information , Message = "{EndpointName} client received server '{ServerInfo}' capabilities: '{Capabilities}'." ) ]
195+ private partial void LogServerCapabilitiesReceived ( string endpointName , string capabilities , string serverInfo ) ;
196+
197+ [ LoggerMessage ( Level = LogLevel . Error , Message = "{EndpointName} client initialization error." ) ]
198+ private partial void LogClientInitializationError ( string endpointName , Exception exception ) ;
199+
200+ [ LoggerMessage ( Level = LogLevel . Error , Message = "{EndpointName} client initialization timed out." ) ]
201+ private partial void LogClientInitializationTimeout ( string endpointName ) ;
202+
203+ [ LoggerMessage ( Level = LogLevel . Error , Message = "{EndpointName} client protocol version mismatch with server. Expected '{Expected}', received '{Received}'." ) ]
204+ private partial void LogServerProtocolVersionMismatch ( string endpointName , string expected , string received ) ;
191205}
0 commit comments