fix: ignore empty Streamable HTTP JSON responses (#1626)

This commit is contained in:
King Star 2026-07-15 02:23:33 +08:00 • committed by GitHub
commit ffc557f920
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 39 additions and 2 deletions

View file

@ -186,7 +186,10 @@ internal sealed partial class StreamableHttpClientSessionTransport : TransportBa
if (response.Content.Headers.ContentType?.MediaType == "application/json")
{
var responseContent = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
rpcResponseOrError = await ProcessMessageAsync(responseContent, rpcRequest, cancellationToken).ConfigureAwait(false);
if (responseContent.Length > 0)
{
rpcResponseOrError = await ProcessMessageAsync(responseContent, rpcRequest, cancellationToken).ConfigureAwait(false);
}
}
else if (response.Content.Headers.ContentType?.MediaType == "text/event-stream")
{

View file

@ -147,6 +147,40 @@ public class HttpClientTransportTests : LoggedTest
Assert.True(true);
}
[Fact]
public async Task StreamableHttp_NotificationWithEmptyAcceptedJsonResponse_DoesNotLogParseFailure()
{
var options = new HttpClientTransportOptions
{
Endpoint = new Uri("http://localhost:8080/mcp"),
TransportMode = HttpTransportMode.StreamableHttp,
};
using var mockHttpHandler = new MockHttpHandler();
using var httpClient = new HttpClient(mockHttpHandler);
await using var transport = new HttpClientTransport(options, httpClient, LoggerFactory);
mockHttpHandler.RequestHandler = request =>
{
Assert.Equal(HttpMethod.Post, request.Method);
Assert.Equal("http://localhost:8080/mcp", request.RequestUri?.AbsoluteUri);
return Task.FromResult(new HttpResponseMessage
{
StatusCode = HttpStatusCode.Accepted,
Content = new StringContent("", Encoding.UTF8, "application/json"),
});
};
await using var session = await transport.ConnectAsync(TestContext.Current.CancellationToken);
await session.SendMessageAsync(
new JsonRpcNotification { Method = "notifications/initialized" },
TestContext.Current.CancellationToken);
Assert.DoesNotContain(MockLoggerProvider.LogMessages, log =>
log.Message.Contains("transport message parsing failed", StringComparison.Ordinal));
}
[Fact]
public async Task SendMessageAsync_Throws_HttpRequestException_With_ResponseBody_On_ErrorStatusCode()
{
@ -377,4 +411,4 @@ public class HttpClientTransportTests : LoggedTest
// Assert - Total GET requests = 1 initial connection + MaxReconnectionAttempts reconnections.
Assert.Equal(1 + MaxReconnectionAttempts, getRequestCount);
}
}
}