csharp-sdk/tests/ModelContextProtocol.AspNetCore.Tests/MapMcpStreamableHttpTests.cs
Stephen Halter ce9f73f43f
Add stateless Streamable HTTP support (#392)
- It allows a single MCP session spanning multiple requests
  to be handled by different servers without sharing state
- It does require the servers share data protection keys, but this
  is standard for ASP.NET Core cookies and antiforgery as well
- It immediately throws for unsupported operations like sampling
2025-05-14 04:51:03 -07:00

37 lines
1.2 KiB
C#

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace ModelContextProtocol.AspNetCore.Tests;
public class MapMcpStreamableHttpTests(ITestOutputHelper outputHelper) : MapMcpTests(outputHelper)
{
protected override bool UseStreamableHttp => true;
protected override bool Stateless => false;
[Theory]
[InlineData("/a", "/a")]
[InlineData("/a", "/a/")]
[InlineData("/a/", "/a/")]
[InlineData("/a/", "/a")]
[InlineData("/a/b", "/a/b")]
public async Task CanConnect_WithMcpClient_AfterCustomizingRoute(string routePattern, string requestPath)
{
Builder.Services.AddMcpServer(options =>
{
options.ServerInfo = new()
{
Name = "TestCustomRouteServer",
Version = "1.0.0",
};
}).WithHttpTransport(ConfigureStateless);
await using var app = Builder.Build();
app.MapMcp(routePattern);
await app.StartAsync(TestContext.Current.CancellationToken);
var mcpClient = await ConnectAsync(requestPath);
Assert.Equal("TestCustomRouteServer", mcpClient.ServerInfo.Name);
}
}