|
1 | 1 | using Microsoft.AspNetCore.Builder; |
| 2 | +using Microsoft.AspNetCore.Http; |
2 | 3 | using Microsoft.Extensions.DependencyInjection; |
3 | 4 | using Microsoft.Extensions.Primitives; |
4 | 5 | using ModelContextProtocol.Client; |
@@ -355,4 +356,60 @@ public async Task EnablePollingAsync_ThrowsInvalidOperationException_WhenNoEvent |
355 | 356 | Assert.NotNull(capturedException); |
356 | 357 | Assert.Contains("event stream store", capturedException.Message, StringComparison.OrdinalIgnoreCase); |
357 | 358 | } |
| 359 | + |
| 360 | + [Fact] |
| 361 | + public async Task AdditionalHeaders_AreSent_InPostAndDeleteRequests() |
| 362 | + { |
| 363 | + Assert.SkipWhen(Stateless, "DELETE requests are not sent in stateless mode due to lack of session ID."); |
| 364 | + |
| 365 | + bool wasPostRequest = false; |
| 366 | + bool wasDeleteRequest = false; |
| 367 | + |
| 368 | + Builder.Services.AddMcpServer().WithHttpTransport(ConfigureStateless).WithTools<EchoHttpContextUserTools>(); |
| 369 | + |
| 370 | + await using var app = Builder.Build(); |
| 371 | + |
| 372 | + app.Use(next => |
| 373 | + { |
| 374 | + return async context => |
| 375 | + { |
| 376 | + Assert.Equal("Bearer testToken", context.Request.Headers["Authorize"]); |
| 377 | + if (context.Request.Method == HttpMethods.Post) |
| 378 | + { |
| 379 | + wasPostRequest = true; |
| 380 | + } |
| 381 | + else if (context.Request.Method == HttpMethods.Delete) |
| 382 | + { |
| 383 | + wasDeleteRequest = true; |
| 384 | + } |
| 385 | + await next(context); |
| 386 | + }; |
| 387 | + }); |
| 388 | + |
| 389 | + app.MapMcp(); |
| 390 | + |
| 391 | + await app.StartAsync(TestContext.Current.CancellationToken); |
| 392 | + |
| 393 | + var transportOptions = new HttpClientTransportOptions |
| 394 | + { |
| 395 | + Endpoint = new("http://localhost:5000/"), |
| 396 | + Name = "In-memory Streamable HTTP Client", |
| 397 | + TransportMode = HttpTransportMode.StreamableHttp, |
| 398 | + AdditionalHeaders = new Dictionary<string, string> |
| 399 | + { |
| 400 | + ["Authorize"] = "Bearer testToken" |
| 401 | + }, |
| 402 | + }; |
| 403 | + |
| 404 | + await using var mcpClient = await ConnectAsync(transportOptions: transportOptions); |
| 405 | + |
| 406 | + // Do a tool call to ensure there's more than just the initialize request |
| 407 | + await mcpClient.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken); |
| 408 | + |
| 409 | + // Dispose the client to trigger the DELETE request |
| 410 | + await mcpClient.DisposeAsync(); |
| 411 | + |
| 412 | + Assert.True(wasPostRequest, "POST request was not made"); |
| 413 | + Assert.True(wasDeleteRequest, "DELETE request was not made"); |
| 414 | + } |
358 | 415 | } |
0 commit comments