From cae5dff318c5556b9706dc51b63b6b7bdb3806bf Mon Sep 17 00:00:00 2001 From: token <61819790+239573049@users.noreply.github.com> Date: Wed, 9 Apr 2025 23:33:49 +0800 Subject: [PATCH] =?UTF-8?q?Improve=20SSE=20response=20handling=20and=20URI?= =?UTF-8?q?=20construction=20in=20SseClientSessio=E2=80=A6=20(#251)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Protocol/Transport/SseClientSessionTransport.cs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/ModelContextProtocol/Protocol/Transport/SseClientSessionTransport.cs b/src/ModelContextProtocol/Protocol/Transport/SseClientSessionTransport.cs index b45f9f2..6df89d1 100644 --- a/src/ModelContextProtocol/Protocol/Transport/SseClientSessionTransport.cs +++ b/src/ModelContextProtocol/Protocol/Transport/SseClientSessionTransport.cs @@ -115,7 +115,7 @@ internal sealed class SseClientSessionTransport : TransportBase if (message is JsonRpcRequest request && request.Method == RequestMethods.Initialize) { // If the response is not a JSON-RPC response, it is an SSE message - if (responseContent.Equals("accepted", StringComparison.OrdinalIgnoreCase)) + if (string.IsNullOrEmpty(responseContent) || responseContent.Equals("accepted", StringComparison.OrdinalIgnoreCase)) { _logger.SSETransportPostAccepted(_endpointName, messageId); // The response will arrive as an SSE message @@ -133,7 +133,7 @@ internal sealed class SseClientSessionTransport : TransportBase } // Otherwise, check if the response was accepted (the response will come as an SSE message) - if (responseContent.Equals("accepted", StringComparison.OrdinalIgnoreCase)) + if (string.IsNullOrEmpty(responseContent) || responseContent.Equals("accepted", StringComparison.OrdinalIgnoreCase)) { _logger.SSETransportPostAccepted(_endpointName, messageId); } @@ -294,13 +294,11 @@ internal sealed class SseClientSessionTransport : TransportBase else { // If the endpoint is a relative URI, we need to combine it with the relative path of the SSE endpoint - var hostUrl = _sseEndpoint.AbsoluteUri; - if (hostUrl.EndsWith("/sse", StringComparison.Ordinal)) - hostUrl = hostUrl[..^4]; + var baseUriBuilder = new UriBuilder(_sseEndpoint); - var endpointUri = $"{hostUrl.TrimEnd('/')}/{data.TrimStart('/')}"; - _messageEndpoint = new Uri(endpointUri); + // Instead of manually concatenating strings, use the Uri class's composition capabilities + _messageEndpoint = new Uri(baseUriBuilder.Uri, data); } // Set connected state