Skip to content

Commit cae5dff

Browse files
authored
Improve SSE response handling and URI construction in SseClientSessio… (#251)
1 parent 43266ad commit cae5dff

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

src/ModelContextProtocol/Protocol/Transport/SseClientSessionTransport.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public override async Task SendMessageAsync(
115115
if (message is JsonRpcRequest request && request.Method == RequestMethods.Initialize)
116116
{
117117
// If the response is not a JSON-RPC response, it is an SSE message
118-
if (responseContent.Equals("accepted", StringComparison.OrdinalIgnoreCase))
118+
if (string.IsNullOrEmpty(responseContent) || responseContent.Equals("accepted", StringComparison.OrdinalIgnoreCase))
119119
{
120120
_logger.SSETransportPostAccepted(_endpointName, messageId);
121121
// The response will arrive as an SSE message
@@ -133,7 +133,7 @@ public override async Task SendMessageAsync(
133133
}
134134

135135
// Otherwise, check if the response was accepted (the response will come as an SSE message)
136-
if (responseContent.Equals("accepted", StringComparison.OrdinalIgnoreCase))
136+
if (string.IsNullOrEmpty(responseContent) || responseContent.Equals("accepted", StringComparison.OrdinalIgnoreCase))
137137
{
138138
_logger.SSETransportPostAccepted(_endpointName, messageId);
139139
}
@@ -294,13 +294,11 @@ private void HandleEndpointEvent(string data)
294294
else
295295
{
296296
// If the endpoint is a relative URI, we need to combine it with the relative path of the SSE endpoint
297-
var hostUrl = _sseEndpoint.AbsoluteUri;
298-
if (hostUrl.EndsWith("/sse", StringComparison.Ordinal))
299-
hostUrl = hostUrl[..^4];
297+
var baseUriBuilder = new UriBuilder(_sseEndpoint);
300298

301-
var endpointUri = $"{hostUrl.TrimEnd('/')}/{data.TrimStart('/')}";
302299

303-
_messageEndpoint = new Uri(endpointUri);
300+
// Instead of manually concatenating strings, use the Uri class's composition capabilities
301+
_messageEndpoint = new Uri(baseUriBuilder.Uri, data);
304302
}
305303

306304
// Set connected state

0 commit comments

Comments
 (0)