Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion src/ModelContextProtocol.AspNetCore/SseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public async Task HandleSseRequestAsync(HttpContext context)

StreamableHttpHandler.InitializeSseResponse(context);

await using var transport = new SseResponseStreamTransport(context.Response.Body, $"message?sessionId={sessionId}");
var requestPath = (context.Request.PathBase + context.Request.Path).ToString();
var endpointPattern = requestPath[..(requestPath.LastIndexOf('/') + 1)];
await using var transport = new SseResponseStreamTransport(context.Response.Body, $"{endpointPattern}message?sessionId={sessionId}");
await using var httpMcpSession = new HttpMcpSession<SseResponseStreamTransport>(sessionId, transport, context.User, httpMcpServerOptions.Value.TimeProvider);
if (!_sessions.TryAdd(sessionId, httpMcpSession))
{
Expand Down
19 changes: 15 additions & 4 deletions tests/ModelContextProtocol.AspNetCore.Tests/MapMcpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,34 @@ public async Task MapMcp_ThrowsInvalidOperationException_IfWithHttpTransportIsNo
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 =>
Expand Down