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
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
}
}
},
"postCreateCommand": "dotnet --list-sdks && echo 'Available .NET SDKs installed successfully!'"
"postCreateCommand": "dotnet dev-certs https --trust && dotnet --list-sdks && echo 'Available .NET SDKs installed successfully!'"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using ModelContextProtocol.Tests.Utils;

namespace ModelContextProtocol.AspNetCore.Tests.Utils;
Expand All @@ -11,11 +11,11 @@ public class KestrelInMemoryTest : LoggedTest
public KestrelInMemoryTest(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
// Use SlimBuilder instead of EmptyBuilder to avoid having to call UseRouting() and UseEndpoints(_ => { })
// or a helper that does the same every test. But clear out the existing socket transport to avoid potential port conflicts.
Builder = WebApplication.CreateSlimBuilder();
Builder.Services.RemoveAll<IConnectionListenerFactory>();
Builder = WebApplication.CreateEmptyBuilder(new());
Builder.Services.AddSingleton<IConnectionListenerFactory>(KestrelInMemoryTransport);
Builder.WebHost.UseKestrelCore();
Builder.Services.AddRoutingCore();
Builder.Services.AddLogging();
Builder.Services.AddSingleton(XunitLoggerProvider);

SocketsHttpHandler.ConnectCallback = (context, token) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,30 @@ namespace ModelContextProtocol.AspNetCore.Tests.Utils;

public sealed class KestrelInMemoryTransport : IConnectionListenerFactory
{
// socket accept queues keyed by listen port.
// Socket accept queues keyed by listen port.
private readonly ConcurrentDictionary<int, Channel<ConnectionContext>> _acceptQueues = [];

public KestrelInMemoryConnection CreateConnection(EndPoint endpoint)
{
if (!_acceptQueues.TryGetValue(GetEndpointPort(endpoint), out var acceptQueue))
{
throw new IOException($"No listener is bound to endpoint '{endpoint}'.");
}

var connection = new KestrelInMemoryConnection();
if (!GetAcceptQueue(endpoint).Writer.TryWrite(connection))
if (!acceptQueue.Writer.TryWrite(connection))
{
throw new IOException("The KestrelInMemoryTransport has been shut down.");
};

return connection;
}

public ValueTask<IConnectionListener> BindAsync(EndPoint endpoint, CancellationToken cancellationToken = default) =>
new(new KestrelInMemoryListener(endpoint, GetAcceptQueue(endpoint)));

private Channel<ConnectionContext> GetAcceptQueue(EndPoint endpoint) =>
_acceptQueues.GetOrAdd(GetEndpointPort(endpoint), _ => Channel.CreateUnbounded<ConnectionContext>());
public ValueTask<IConnectionListener> BindAsync(EndPoint endpoint, CancellationToken cancellationToken = default)
{
var acceptQueue = _acceptQueues.GetOrAdd(GetEndpointPort(endpoint), _ => Channel.CreateUnbounded<ConnectionContext>());
return new(new KestrelInMemoryListener(endpoint, acceptQueue));
}

private static int GetEndpointPort(EndPoint endpoint) =>
endpoint switch
Expand Down
3 changes: 0 additions & 3 deletions tests/ModelContextProtocol.TestOAuthServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,6 @@ public async Task RunServerAsync(string[]? args = null, CancellationToken cancel

var app = builder.Build();

app.UseRouting();
app.UseEndpoints(_ => { });

// Set up the demo client
var clientId = "demo-client";
var clientSecret = "demo-secret";
Expand Down
2 changes: 0 additions & 2 deletions tests/ModelContextProtocol.TestSseServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,6 @@ public static async Task MainAsync(string[] args, ILoggerProvider? loggerProvide
.WithHttpTransport();

var app = builder.Build();
app.UseRouting();
app.UseEndpoints(_ => { });

// Handle the /stateless endpoint if no other endpoints have been matched by the call to UseRouting above.
HandleStatelessMcp(app);
Expand Down
Loading