Emit root-relative /message endpoint when using SSE Server (#323)

This commit is contained in:
dogdie233 2025-04-21 05:31:48 +08:00 • committed by GitHub
commit 0c9e91f98c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -32,23 +32,34 @@ public class MapMcpTests(ITestOutputHelper testOutputHelper) : KestrelInMemoryTe
Assert.StartsWith("You must call WithHttpTransport()", exception.Message);
}
[Fact]
public async Task Allows_Customizing_Route()
[Theory]
[InlineData("/mcp")]
[InlineData("/mcp/secondary")]
public async Task Allows_Customizing_Route(string pattern)
{
Builder.Services.AddMcpServer().WithHttpTransport();
await using var app = Builder.Build();
app.MapMcp("/mcp");
app.MapMcp(pattern);
await app.StartAsync(TestContext.Current.CancellationToken);
using var response = await HttpClient.GetAsync("http://localhost/mcp/sse", HttpCompletionOption.ResponseHeadersRead, TestContext.Current.CancellationToken);
using var response = await HttpClient.GetAsync($"http://localhost{pattern}/sse", HttpCompletionOption.ResponseHeadersRead, TestContext.Current.CancellationToken);
response.EnsureSuccessStatusCode();
using var sseStream = await response.Content.ReadAsStreamAsync(TestContext.Current.CancellationToken);
using var sseStreamReader = new StreamReader(sseStream, System.Text.Encoding.UTF8);
var eventLine = await sseStreamReader.ReadLineAsync(TestContext.Current.CancellationToken);
var dataLine = await sseStreamReader.ReadLineAsync(TestContext.Current.CancellationToken);
Assert.NotNull(eventLine);
Assert.Equal("event: endpoint", eventLine);
Assert.NotNull(dataLine);
Assert.Equal($"data: {pattern}/message", dataLine[..dataLine.IndexOf('?')]);
}
[Theory]
[InlineData("/a", "/a/sse")]
[InlineData("/a/", "/a/sse")]
[InlineData("/a/b", "/a/b/sse")]
public async Task CanConnect_WithMcpClient_AfterCustomizingRoute(string routePattern, string requestPath)
{
Builder.Services.AddMcpServer(options =>