Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;
using ModelContextProtocol.Client;
Expand Down Expand Up @@ -355,4 +356,60 @@ public async Task EnablePollingAsync_ThrowsInvalidOperationException_WhenNoEvent
Assert.NotNull(capturedException);
Assert.Contains("event stream store", capturedException.Message, StringComparison.OrdinalIgnoreCase);
}

[Fact]
public async Task AdditionalHeaders_AreSent_InPostAndDeleteRequests()
{
Assert.SkipWhen(Stateless, "DELETE requests are not sent in stateless mode due to lack of session ID.");

bool wasPostRequest = false;
bool wasDeleteRequest = false;

Builder.Services.AddMcpServer().WithHttpTransport(ConfigureStateless).WithTools<EchoHttpContextUserTools>();

await using var app = Builder.Build();

app.Use(next =>
{
return async context =>
{
Assert.Equal("Bearer testToken", context.Request.Headers["Authorize"]);
if (context.Request.Method == HttpMethods.Post)
{
wasPostRequest = true;
}
else if (context.Request.Method == HttpMethods.Delete)
{
wasDeleteRequest = true;
}
await next(context);
};
});

app.MapMcp();

await app.StartAsync(TestContext.Current.CancellationToken);

var transportOptions = new HttpClientTransportOptions
{
Endpoint = new("http://localhost:5000/"),
Name = "In-memory Streamable HTTP Client",
TransportMode = HttpTransportMode.StreamableHttp,
AdditionalHeaders = new Dictionary<string, string>
{
["Authorize"] = "Bearer testToken"
},
};

await using var mcpClient = await ConnectAsync(transportOptions: transportOptions);

// Do a tool call to ensure there's more than just the initialize request
await mcpClient.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);

// Dispose the client to trigger the DELETE request
await mcpClient.DisposeAsync();

Assert.True(wasPostRequest, "POST request was not made");
Assert.True(wasDeleteRequest, "DELETE request was not made");
}
}