* Reduce dependencies to 8.x versions of packages When building for netstandard2.0 or net8.0, the ModelContextProtocol library now only depends on 8.x versions of nuget packages, rather than using the newer 9.x versions. The exception to this are packages that don't have 8.x versions. This also introduces a net9.0 tfm. When building for net9.0, it'll use 9.x versions of packages. The PR temporarily removes the Microsoft.Extensions.AI.nupkg reference (the Microsoft.Extensions.AI.Abstractions reference is still very much there). The latest Microsoft.Extensions.AI package on nuget has a System.Text.Json 9.x reference, but the next build to be published drops that down to 8.x as well. Once that build is published, the dependency will be added back, and temporary polyfills (in particular TemporaryAIFunctionFactory) will be deleted. * Fix StdioClientSessionTransport shutdown StreamClientSessionTransport's ReadMessageAsync method does await CleanupAsync in its finally. CleanupAsync awaits _readTask. _readTask _is_ ReadMessageAsync. So it's waiting for itself to complete, which will then always timeout. We may want to refactor how this works more in the future, but for now, make sure it doesn't wait for itself. * Fix dangling requests when server unexpectedly shuts down If the client makes a request to the server and while waiting for the response the server shuts down, the client hangs. Make sure that when the client's message loops ends due to the server's output it's reading from ends, any pending requests are notified. * Add net9.0 testing * Avoid docker port conflicts from test suites running concurrently
63 lines
1.9 KiB
C#
63 lines
1.9 KiB
C#
using ModelContextProtocol.Client;
|
|
using ModelContextProtocol.Protocol.Transport;
|
|
using ModelContextProtocol.Test.Utils;
|
|
using ModelContextProtocol.Tests.Utils;
|
|
using ModelContextProtocol.TestSseServer;
|
|
|
|
namespace ModelContextProtocol.Tests;
|
|
|
|
public class SseServerIntegrationTestFixture : IAsyncDisposable
|
|
{
|
|
private readonly Task _serverTask;
|
|
private readonly CancellationTokenSource _stopCts = new();
|
|
|
|
private readonly DelegatingTestOutputHelper _delegatingTestOutputHelper = new();
|
|
|
|
public McpServerConfig DefaultConfig { get; }
|
|
|
|
public SseServerIntegrationTestFixture()
|
|
{
|
|
// Ensure that test suites running against different TFMs and possibly concurrently use different port numbers.
|
|
int port = 3001 + Environment.Version.Major;
|
|
|
|
DefaultConfig = new McpServerConfig
|
|
{
|
|
Id = "test_server",
|
|
Name = "TestServer",
|
|
TransportType = TransportTypes.Sse,
|
|
TransportOptions = [],
|
|
Location = $"http://localhost:{port}/sse"
|
|
};
|
|
|
|
_serverTask = Program.MainAsync([port.ToString()], new XunitLoggerProvider(_delegatingTestOutputHelper), _stopCts.Token);
|
|
}
|
|
|
|
public static McpClientOptions CreateDefaultClientOptions() => new()
|
|
{
|
|
ClientInfo = new() { Name = "IntegrationTestClient", Version = "1.0.0" },
|
|
};
|
|
|
|
public void Initialize(ITestOutputHelper output)
|
|
{
|
|
_delegatingTestOutputHelper.CurrentTestOutputHelper = output;
|
|
}
|
|
|
|
public void TestCompleted()
|
|
{
|
|
_delegatingTestOutputHelper.CurrentTestOutputHelper = null;
|
|
}
|
|
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
_delegatingTestOutputHelper.CurrentTestOutputHelper = null;
|
|
_stopCts.Cancel();
|
|
try
|
|
{
|
|
await _serverTask.ConfigureAwait(false);
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
}
|
|
_stopCts.Dispose();
|
|
}
|
|
}
|