Clean up (use of) McpException and McpTransportException (#321)

* Clean up (use of) McpException

* Replace McpTransportException with InvalidOperationException

* Add cancellation test
This commit is contained in:
Stephen Toub 2025-04-18 12:25:29 -04:00 • committed by GitHub
commit 7d77be68cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -5,6 +5,7 @@ using ModelContextProtocol.Protocol.Transport;
using ModelContextProtocol.Protocol.Types;
using ModelContextProtocol.Utils.Json;
using Moq;
using System.IO.Pipelines;
using System.Text.Json;
using System.Threading.Channels;
@ -29,6 +30,31 @@ public class McpClientFactoryTests
Assert.NotNull(client);
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task Cancellation_ThrowsCancellationException(bool preCanceled)
{
var cts = new CancellationTokenSource();
if (preCanceled)
{
cts.Cancel();
}
Task t = McpClientFactory.CreateAsync(
new StreamClientTransport(new Pipe().Writer.AsStream(), new Pipe().Reader.AsStream()),
cancellationToken: cts.Token);
Assert.False(t.IsCompleted);
if (!preCanceled)
{
cts.Cancel();
}
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => t);
}
[Theory]
[InlineData(typeof(NopTransport))]
[InlineData(typeof(FailureTransport))]