fix: ignore empty Streamable HTTP JSON responses (#1626)
This commit is contained in:
parent
1f9b8a3955
commit
ffc557f920
2 changed files with 39 additions and 2 deletions
|
|
@ -186,7 +186,10 @@ internal sealed partial class StreamableHttpClientSessionTransport : TransportBa
|
||||||
if (response.Content.Headers.ContentType?.MediaType == "application/json")
|
if (response.Content.Headers.ContentType?.MediaType == "application/json")
|
||||||
{
|
{
|
||||||
var responseContent = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
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")
|
else if (response.Content.Headers.ContentType?.MediaType == "text/event-stream")
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -147,6 +147,40 @@ public class HttpClientTransportTests : LoggedTest
|
||||||
Assert.True(true);
|
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]
|
[Fact]
|
||||||
public async Task SendMessageAsync_Throws_HttpRequestException_With_ResponseBody_On_ErrorStatusCode()
|
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 - Total GET requests = 1 initial connection + MaxReconnectionAttempts reconnections.
|
||||||
Assert.Equal(1 + MaxReconnectionAttempts, getRequestCount);
|
Assert.Equal(1 + MaxReconnectionAttempts, getRequestCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue