Skip to content

Commit b30f236

Browse files
halter73Copilot
andcommitted
Fix tests after rebase on MRTR: use SendRequestAsync to reach transport-level guards
MRTR (#1458) added a session-level guard in McpSessionHandler.SendMessageAsync that throws InvalidOperationException for any JsonRpcRequest, directing callers to SendRequestAsync. The two tests that exercised the transport's request-handling code paths called server.SendMessageAsync(request, ...) and so were rejected at the session layer before reaching the transport. Switch them to SendRequestAsync, which bypasses that guard and routes through SendToRelatedTransportAsync to the transport's SendMessageAsync — the actual layer the new diagnostics live on. For the GET-open warning test, SendRequestAsync awaits a response that the test never produces, so wrap it in a CancellationTokenSource that we cancel after confirming the request landed on the SSE wire. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent fe01e72 commit b30f236

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

tests/ModelContextProtocol.AspNetCore.Tests/StreamableHttpServerConformanceTests.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,12 @@ public async Task SendNotificationAsync_DoesNotThrow_WhenNoGetRequestHasBeenMade
419419
}
420420

421421
[Fact]
422-
public async Task SendMessageAsync_Throws_OnRequest_WhenNoGetRequestHasBeenMade()
422+
public async Task SendRequestAsync_Throws_WhenNoGetRequestHasBeenMade()
423423
{
424424
// A server-to-client request sent before any GET SSE stream is opened can never
425-
// receive a response, so SendMessageAsync should fail fast with InvalidOperationException
426-
// instead of silently dropping the message and leaving the caller hanging.
425+
// receive a response, so the transport should fail fast with InvalidOperationException
426+
// instead of silently dropping the message and leaving the caller hanging on the TCS
427+
// registered by SendRequestAsync.
427428
McpServer? server = null;
428429

429430
Builder.Services.AddMcpServer()
@@ -450,7 +451,7 @@ public async Task SendMessageAsync_Throws_OnRequest_WhenNoGetRequestHasBeenMade(
450451
};
451452

452453
var ex = await Assert.ThrowsAsync<InvalidOperationException>(() =>
453-
server.SendMessageAsync(request, TestContext.Current.CancellationToken));
454+
server.SendRequestAsync(request, TestContext.Current.CancellationToken));
454455

455456
Assert.Contains("roots/list", ex.Message);
456457
Assert.Contains("no GET SSE stream", ex.Message);
@@ -502,7 +503,7 @@ public async Task SendMessageAsync_LogsWarning_OnUnexpectedResponse_WhenNoGetReq
502503
}
503504

504505
[Fact]
505-
public async Task SendMessageAsync_LogsWarning_OnRequest_WhenGetRequestIsOpen()
506+
public async Task SendRequestAsync_LogsWarning_WhenGetRequestIsOpen()
506507
{
507508
// Even when the GET SSE stream is open and the request is delivered, server-to-client
508509
// requests sent via the GET path are fragile (no per-request correlation, depend on a
@@ -531,13 +532,16 @@ public async Task SendMessageAsync_LogsWarning_OnRequest_WhenGetRequestIsOpen()
531532
Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode);
532533

533534
// Send a request via the GET stream and assert it lands on the wire (proving behavior is unchanged).
535+
// SendRequestAsync awaits a response that the test never produces, so use a CTS to cancel after
536+
// confirming wire delivery.
534537
var request = new JsonRpcRequest
535538
{
536539
Method = "roots/list",
537540
Id = new RequestId(99),
538541
};
539542

540-
var sendTask = server.SendMessageAsync(request, TestContext.Current.CancellationToken);
543+
using var requestCts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.Current.CancellationToken);
544+
var sendTask = server.SendRequestAsync(request, requestCts.Token);
541545

542546
await foreach (var sseEvent in ReadSseAsync(getResponse.Content))
543547
{
@@ -547,7 +551,9 @@ public async Task SendMessageAsync_LogsWarning_OnRequest_WhenGetRequestIsOpen()
547551
break;
548552
}
549553

550-
await sendTask;
554+
// Cancel the awaited response so SendRequestAsync completes — the wire delivery has already happened.
555+
requestCts.Cancel();
556+
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => sendTask);
551557

552558
Assert.Contains(MockLoggerProvider.LogMessages, log =>
553559
log.Category == typeof(StreamableHttpServerTransport).FullName &&

0 commit comments

Comments
 (0)