Skip to content

Commit 3bc0c7d

Browse files
committed
Fix merge conflicts and address PR feedback
- Catch and log errors - Send _negotiatedProtocolVersion
1 parent df5a28e commit 3bc0c7d

3 files changed

Lines changed: 23 additions & 11 deletions

File tree

src/ModelContextProtocol.Core/Client/SseClientSessionTransport.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public override async Task SendMessageAsync(
9191
{
9292
Content = content,
9393
};
94-
StreamableHttpClientSessionTransport.CopyAdditionalHeaders(httpRequestMessage.Headers, _options.AdditionalHeaders);
94+
StreamableHttpClientSessionTransport.CopyAdditionalHeaders(httpRequestMessage.Headers, _options.AdditionalHeaders, sessionId: null, protocolVersion: null);
9595
var response = await _httpClient.SendAsync(httpRequestMessage, cancellationToken).ConfigureAwait(false);
9696

9797
if (!response.IsSuccessStatusCode)
@@ -152,7 +152,7 @@ private async Task ReceiveMessagesAsync(CancellationToken cancellationToken)
152152
{
153153
using var request = new HttpRequestMessage(HttpMethod.Get, _sseEndpoint);
154154
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/event-stream"));
155-
StreamableHttpClientSessionTransport.CopyAdditionalHeaders(request.Headers, _options.AdditionalHeaders);
155+
StreamableHttpClientSessionTransport.CopyAdditionalHeaders(request.Headers, _options.AdditionalHeaders, sessionId: null, protocolVersion: null);
156156

157157
using var response = await _httpClient.SendAsync(
158158
request,

src/ModelContextProtocol.Core/Client/StreamableHttpClientSessionTransport.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,9 @@ public override async ValueTask DisposeAsync()
145145
try
146146
{
147147
// Send DELETE request to terminate the session only send if we have a session ID per MCP spec
148-
if (!string.IsNullOrEmpty(_mcpSessionId))
148+
if (!string.IsNullOrEmpty(SessionId))
149149
{
150-
using var deleteRequest = new HttpRequestMessage(HttpMethod.Delete, _options.Endpoint);
151-
CopyAdditionalHeaders(deleteRequest.Headers, _options.AdditionalHeaders, _mcpSessionId);
152-
153-
// Do not validate we get a successful status code, because server support for the DELETE request is optional
154-
using var deleteResponse = await _httpClient.SendAsync(deleteRequest, CancellationToken.None).ConfigureAwait(false);
150+
await SendDeleteRequest();
155151
}
156152

157153
if (_getReceiveTask != null)
@@ -246,6 +242,22 @@ message is JsonRpcMessageWithId rpcResponseOrError &&
246242
return null;
247243
}
248244

245+
private async Task SendDeleteRequest()
246+
{
247+
using var deleteRequest = new HttpRequestMessage(HttpMethod.Delete, _options.Endpoint);
248+
CopyAdditionalHeaders(deleteRequest.Headers, _options.AdditionalHeaders, SessionId, _negotiatedProtocolVersion);
249+
250+
try
251+
{
252+
// Do not validate we get a successful status code, because server support for the DELETE request is optional
253+
(await _httpClient.SendAsync(deleteRequest, CancellationToken.None).ConfigureAwait(false)).Dispose();
254+
}
255+
catch (Exception ex)
256+
{
257+
LogTransportShutdownFailed(Name, ex);
258+
}
259+
}
260+
249261
private void LogJsonException(JsonException ex, string data)
250262
{
251263
if (_logger.IsEnabled(LogLevel.Trace))
@@ -261,8 +273,8 @@ private void LogJsonException(JsonException ex, string data)
261273
internal static void CopyAdditionalHeaders(
262274
HttpRequestHeaders headers,
263275
IDictionary<string, string>? additionalHeaders,
264-
string? sessionId = null,
265-
string? protocolVersion = null)
276+
string? sessionId,
277+
string? protocolVersion)
266278
{
267279
if (sessionId is not null)
268280
{

tests/ModelContextProtocol.AspNetCore.Tests/Utils/KestrelInMemoryConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public override Task FlushAsync(CancellationToken cancellationToken)
8181
protected override void Dispose(bool disposing)
8282
{
8383
// Signal to the server the the client has closed the connection, and dispose the client-half of the Pipes.
84-
connectionClosedCts.Cancel();
84+
ThreadPool.UnsafeQueueUserWorkItem(static cts => ((CancellationTokenSource)cts!).Cancel(), connectionClosedCts);
8585
duplexPipe.Input.Complete();
8686
duplexPipe.Output.Complete();
8787
}

0 commit comments

Comments
 (0)