- Changes IJsonRpcMessage to an abstract base class so RelatedTransport will always be available - Streamable HTTP supports multiple concurrent HTTP request with their own indpendent SSE response streams - RelatedTransport indicates the source or destination of the JsonRpcMessage - Changes the default RequestId to a JSON number for better compatibility with MCP servers in the wild
46 lines
1.6 KiB
C#
46 lines
1.6 KiB
C#
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Connections;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
using ModelContextProtocol.Tests.Utils;
|
|
|
|
namespace ModelContextProtocol.AspNetCore.Tests.Utils;
|
|
|
|
public class KestrelInMemoryTest : LoggedTest
|
|
{
|
|
private readonly KestrelInMemoryTransport _inMemoryTransport = new();
|
|
|
|
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.Services.AddSingleton<IConnectionListenerFactory>(_inMemoryTransport);
|
|
Builder.Services.AddSingleton(LoggerProvider);
|
|
|
|
HttpClient = new HttpClient(new SocketsHttpHandler()
|
|
{
|
|
ConnectCallback = (context, token) =>
|
|
{
|
|
var connection = _inMemoryTransport.CreateConnection();
|
|
return new(connection.ClientStream);
|
|
},
|
|
})
|
|
{
|
|
BaseAddress = new Uri("http://localhost:5000/"),
|
|
Timeout = TimeSpan.FromSeconds(10),
|
|
};
|
|
}
|
|
|
|
public WebApplicationBuilder Builder { get; }
|
|
|
|
public HttpClient HttpClient { get; }
|
|
|
|
public override void Dispose()
|
|
{
|
|
HttpClient.Dispose();
|
|
base.Dispose();
|
|
}
|
|
}
|