* Simplify ModelContextProtocol.AspNetCore README * Add StreamableHttpHandler * Use "docker info" in CheckIsDockerAvailable * Call WithHttpTransport in samples, tests and README * Cleanup test namespaces * Add CanConnect_WithMcpClient_AfterCustomizingRoute test * Simplify relative URI handling * Handle request made directly to the MapMcp route pattern * Add Messages_FromNewUser_AreRejected test * Fix README * Shorten UserIdClaim ValueTuple names * Remove MaxReconnectAttempts and ReconnectDelay from SseClientTransportOptions - Add proper AdditionalHeaders support
50 lines
1.5 KiB
C#
50 lines
1.5 KiB
C#
using Microsoft.AspNetCore.Connections;
|
|
using System.Net;
|
|
using System.Threading.Channels;
|
|
|
|
namespace ModelContextProtocol.AspNetCore.Tests.Utils;
|
|
|
|
public sealed class KestrelInMemoryTransport : IConnectionListenerFactory, IConnectionListener
|
|
{
|
|
private readonly Channel<ConnectionContext> _acceptQueue = Channel.CreateUnbounded<ConnectionContext>();
|
|
private EndPoint? _endPoint;
|
|
|
|
public EndPoint EndPoint => _endPoint ?? throw new InvalidOperationException("EndPoint is not set. Call BindAsync first.");
|
|
|
|
public KestrelInMemoryConnection CreateConnection()
|
|
{
|
|
var connection = new KestrelInMemoryConnection();
|
|
_acceptQueue.Writer.TryWrite(connection);
|
|
return connection;
|
|
}
|
|
|
|
public async ValueTask<ConnectionContext?> AcceptAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
if (await _acceptQueue.Reader.WaitToReadAsync(cancellationToken))
|
|
{
|
|
while (_acceptQueue.Reader.TryRead(out var item))
|
|
{
|
|
return item;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public ValueTask<IConnectionListener> BindAsync(EndPoint endpoint, CancellationToken cancellationToken = default)
|
|
{
|
|
_endPoint = endpoint;
|
|
return new ValueTask<IConnectionListener>(this);
|
|
}
|
|
|
|
public ValueTask DisposeAsync()
|
|
{
|
|
return UnbindAsync(default);
|
|
}
|
|
|
|
public ValueTask UnbindAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
_acceptQueue.Writer.TryComplete();
|
|
return default;
|
|
}
|
|
}
|