@@ -16,6 +16,7 @@ namespace ModelContextProtocol.Protocol.Transport;
1616/// </summary>
1717internal sealed class SseClientSessionTransport : TransportBase
1818{
19+ private readonly string _endpointName ;
1920 private readonly HttpClient _httpClient ;
2021 private readonly SseClientTransportOptions _options ;
2122 private readonly Uri _sseEndpoint ;
@@ -25,16 +26,15 @@ internal sealed class SseClientSessionTransport : TransportBase
2526 private readonly ILogger _logger ;
2627 private readonly TaskCompletionSource < bool > _connectionEstablished ;
2728
28- private string EndpointName => $ "Client (SSE) for ({ _options . Id } : { _options . Name } )";
29-
3029 /// <summary>
3130 /// SSE transport for client endpoints. Unlike stdio it does not launch a process, but connects to an existing server.
3231 /// The HTTP server can be local or remote, and must support the SSE protocol.
3332 /// </summary>
3433 /// <param name="transportOptions">Configuration options for the transport.</param>
3534 /// <param name="httpClient">The HTTP client instance used for requests.</param>
3635 /// <param name="loggerFactory">Logger factory for creating loggers.</param>
37- public SseClientSessionTransport ( SseClientTransportOptions transportOptions , HttpClient httpClient , ILoggerFactory ? loggerFactory )
36+ /// <param name="endpointName">The endpoint name used for logging purposes.</param>
37+ public SseClientSessionTransport ( SseClientTransportOptions transportOptions , HttpClient httpClient , ILoggerFactory ? loggerFactory , string endpointName )
3838 : base ( loggerFactory )
3939 {
4040 Throw . IfNull ( transportOptions ) ;
@@ -46,6 +46,7 @@ public SseClientSessionTransport(SseClientTransportOptions transportOptions, Htt
4646 _connectionCts = new CancellationTokenSource ( ) ;
4747 _logger = ( ILogger ? ) loggerFactory ? . CreateLogger < SseClientTransport > ( ) ?? NullLogger . Instance ;
4848 _connectionEstablished = new TaskCompletionSource < bool > ( ) ;
49+ _endpointName = endpointName ;
4950 }
5051
5152 /// <inheritdoc/>
@@ -55,14 +56,14 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)
5556 {
5657 if ( IsConnected )
5758 {
58- _logger . TransportAlreadyConnected ( EndpointName ) ;
59+ _logger . TransportAlreadyConnected ( _endpointName ) ;
5960 throw new McpTransportException ( "Transport is already connected" ) ;
6061 }
6162
6263 // Start message receiving loop
6364 _receiveTask = ReceiveMessagesAsync ( _connectionCts . Token ) ;
6465
65- _logger . TransportReadingMessages ( EndpointName ) ;
66+ _logger . TransportReadingMessages ( _endpointName ) ;
6667
6768 await _connectionEstablished . Task . WaitAsync ( _options . ConnectionTimeout , cancellationToken ) . ConfigureAwait ( false ) ;
6869 }
@@ -73,7 +74,7 @@ public async Task ConnectAsync(CancellationToken cancellationToken = default)
7374 }
7475 catch ( Exception ex )
7576 {
76- _logger . TransportConnectFailed ( EndpointName , ex ) ;
77+ _logger . TransportConnectFailed ( _endpointName , ex ) ;
7778 await CloseAsync ( ) . ConfigureAwait ( false ) ;
7879 throw new McpTransportException ( "Failed to connect transport" , ex ) ;
7980 }
@@ -116,29 +117,29 @@ public override async Task SendMessageAsync(
116117 // If the response is not a JSON-RPC response, it is an SSE message
117118 if ( responseContent . Equals ( "accepted" , StringComparison . OrdinalIgnoreCase ) )
118119 {
119- _logger . SSETransportPostAccepted ( EndpointName , messageId ) ;
120+ _logger . SSETransportPostAccepted ( _endpointName , messageId ) ;
120121 // The response will arrive as an SSE message
121122 }
122123 else
123124 {
124125 JsonRpcResponse initializeResponse = JsonSerializer . Deserialize ( responseContent , McpJsonUtilities . JsonContext . Default . JsonRpcResponse ) ??
125126 throw new McpTransportException ( "Failed to initialize client" ) ;
126127
127- _logger . TransportReceivedMessageParsed ( EndpointName , messageId ) ;
128+ _logger . TransportReceivedMessageParsed ( _endpointName , messageId ) ;
128129 await WriteMessageAsync ( initializeResponse , cancellationToken ) . ConfigureAwait ( false ) ;
129- _logger . TransportMessageWritten ( EndpointName , messageId ) ;
130+ _logger . TransportMessageWritten ( _endpointName , messageId ) ;
130131 }
131132 return ;
132133 }
133134
134135 // Otherwise, check if the response was accepted (the response will come as an SSE message)
135136 if ( responseContent . Equals ( "accepted" , StringComparison . OrdinalIgnoreCase ) )
136137 {
137- _logger . SSETransportPostAccepted ( EndpointName , messageId ) ;
138+ _logger . SSETransportPostAccepted ( _endpointName , messageId ) ;
138139 }
139140 else
140141 {
141- _logger . SSETransportPostNotAccepted ( EndpointName , messageId , responseContent ) ;
142+ _logger . SSETransportPostNotAccepted ( _endpointName , messageId , responseContent ) ;
142143 throw new McpTransportException ( "Failed to send message" ) ;
143144 }
144145 }
@@ -216,17 +217,17 @@ private async Task ReceiveMessagesAsync(CancellationToken cancellationToken)
216217 }
217218 catch ( OperationCanceledException ) when ( cancellationToken . IsCancellationRequested )
218219 {
219- _logger . TransportReadMessagesCancelled ( EndpointName ) ;
220+ _logger . TransportReadMessagesCancelled ( _endpointName ) ;
220221 // Normal shutdown
221222 }
222223 catch ( IOException ) when ( cancellationToken . IsCancellationRequested )
223224 {
224- _logger . TransportReadMessagesCancelled ( EndpointName ) ;
225+ _logger . TransportReadMessagesCancelled ( _endpointName ) ;
225226 // Normal shutdown
226227 }
227228 catch ( Exception ex ) when ( ! cancellationToken . IsCancellationRequested )
228229 {
229- _logger . TransportConnectionError ( EndpointName , ex ) ;
230+ _logger . TransportConnectionError ( _endpointName , ex ) ;
230231
231232 reconnectAttempts ++ ;
232233 if ( reconnectAttempts >= _options . MaxReconnectAttempts )
@@ -245,7 +246,7 @@ private async Task ProcessSseMessage(string data, CancellationToken cancellation
245246 {
246247 if ( ! IsConnected )
247248 {
248- _logger . TransportMessageReceivedBeforeConnected ( EndpointName , data ) ;
249+ _logger . TransportMessageReceivedBeforeConnected ( _endpointName , data ) ;
249250 return ;
250251 }
251252
@@ -254,7 +255,7 @@ private async Task ProcessSseMessage(string data, CancellationToken cancellation
254255 var message = JsonSerializer . Deserialize ( data , McpJsonUtilities . JsonContext . Default . IJsonRpcMessage ) ;
255256 if ( message == null )
256257 {
257- _logger . TransportMessageParseUnexpectedType ( EndpointName , data ) ;
258+ _logger . TransportMessageParseUnexpectedType ( _endpointName , data ) ;
258259 return ;
259260 }
260261
@@ -264,13 +265,13 @@ private async Task ProcessSseMessage(string data, CancellationToken cancellation
264265 messageId = messageWithId . Id . ToString ( ) ;
265266 }
266267
267- _logger . TransportReceivedMessageParsed ( EndpointName , messageId ) ;
268+ _logger . TransportReceivedMessageParsed ( _endpointName , messageId ) ;
268269 await WriteMessageAsync ( message , cancellationToken ) . ConfigureAwait ( false ) ;
269- _logger . TransportMessageWritten ( EndpointName , messageId ) ;
270+ _logger . TransportMessageWritten ( _endpointName , messageId ) ;
270271 }
271272 catch ( JsonException ex )
272273 {
273- _logger . TransportMessageParseFailed ( EndpointName , data , ex ) ;
274+ _logger . TransportMessageParseFailed ( _endpointName , data , ex ) ;
274275 }
275276 }
276277
@@ -280,7 +281,7 @@ private void HandleEndpointEvent(string data)
280281 {
281282 if ( string . IsNullOrEmpty ( data ) )
282283 {
283- _logger . TransportEndpointEventInvalid ( EndpointName , data ) ;
284+ _logger . TransportEndpointEventInvalid ( _endpointName , data ) ;
284285 return ;
285286 }
286287
@@ -308,7 +309,7 @@ private void HandleEndpointEvent(string data)
308309 }
309310 catch ( JsonException ex )
310311 {
311- _logger . TransportEndpointEventParseFailed ( EndpointName , data , ex ) ;
312+ _logger . TransportEndpointEventParseFailed ( _endpointName , data , ex ) ;
312313 throw new McpTransportException ( "Failed to parse endpoint event" , ex ) ;
313314 }
314315 }
0 commit comments