1
1
using Microsoft . Extensions . Logging ;
2
- using ModelContextProtocol . Logging ;
3
2
using ModelContextProtocol . Protocol . Messages ;
4
3
using ModelContextProtocol . Protocol . Transport ;
5
4
using ModelContextProtocol . Protocol . Types ;
10
9
namespace ModelContextProtocol . Client ;
11
10
12
11
/// <inheritdoc/>
13
- internal sealed class McpClient : McpEndpoint , IMcpClient
12
+ internal sealed partial class McpClient : McpEndpoint , IMcpClient
14
13
{
15
14
private static Implementation DefaultImplementation { get ; } = new ( )
16
15
{
@@ -133,9 +132,12 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)
133
132
cancellationToken : initializationCts . Token ) . ConfigureAwait ( false ) ;
134
133
135
134
// 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
+ }
139
141
140
142
_serverCapabilities = initializeResponse . Capabilities ;
141
143
_serverInfo = initializeResponse . ServerInfo ;
@@ -144,7 +146,7 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)
144
146
// Validate protocol version
145
147
if ( initializeResponse . ProtocolVersion != _options . ProtocolVersion )
146
148
{
147
- _logger . ServerProtocolVersionMismatch ( EndpointName , _options . ProtocolVersion , initializeResponse . ProtocolVersion ) ;
149
+ LogServerProtocolVersionMismatch ( EndpointName , _options . ProtocolVersion , initializeResponse . ProtocolVersion ) ;
148
150
throw new McpException ( $ "Server protocol version mismatch. Expected { _options . ProtocolVersion } , got { initializeResponse . ProtocolVersion } ") ;
149
151
}
150
152
@@ -155,13 +157,13 @@ await SendMessageAsync(
155
157
}
156
158
catch ( OperationCanceledException oce ) when ( initializationCts . IsCancellationRequested )
157
159
{
158
- _logger . ClientInitializationTimeout ( EndpointName ) ;
160
+ LogClientInitializationTimeout ( EndpointName ) ;
159
161
throw new McpException ( "Initialization timed out" , oce ) ;
160
162
}
161
163
}
162
164
catch ( Exception e )
163
165
{
164
- _logger . ClientInitializationError ( EndpointName , e ) ;
166
+ LogClientInitializationError ( EndpointName , e ) ;
165
167
await DisposeAsync ( ) . ConfigureAwait ( false ) ;
166
168
throw ;
167
169
}
@@ -188,4 +190,16 @@ public override async ValueTask DisposeUnsynchronizedAsync()
188
190
}
189
191
}
190
192
}
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 ) ;
191
205
}
0 commit comments