Skip to content

Run integration test SSE server in process #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 22, 2025
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
21 changes: 16 additions & 5 deletions tests/ModelContextProtocol.TestSseServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using System.Text;
using System.Text.Json;

internal class Program
namespace ModelContextProtocol.TestSseServer;

public class Program
{
private static ILoggerFactory CreateLoggerFactory()
{
Expand All @@ -25,7 +27,9 @@ private static ILoggerFactory CreateLoggerFactory()
});
}

private static async Task Main(string[] args)
public static Task Main(string[] args) => MainAsync(args);

public static async Task MainAsync(string[] args, CancellationToken cancellationToken = default)
{
Console.WriteLine("Starting server...");

Expand Down Expand Up @@ -386,12 +390,19 @@ static CreateMessageRequestParams CreateRequestSamplingParams(string context, st

Console.WriteLine("Server initialized.");

await server.StartAsync();
await server.StartAsync(cancellationToken);

Console.WriteLine("Server started.");

// Run until process is stopped by the client (parent process)
await Task.Delay(Timeout.Infinite);
try
{
// Run until process is stopped by the client (parent process) or test
await Task.Delay(Timeout.Infinite, cancellationToken);
}
finally
{
await server.DisposeAsync();
}
}

const string MCP_TINY_IMAGE =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@
<Content Condition="$([MSBuild]::IsOSPlatform('Linux'))" Include="..\ModelContextProtocol.TestServer\bin\$(Configuration)\net8.0\TestServer.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Condition="$([MSBuild]::IsOSPlatform('Windows'))" Include="..\ModelContextProtocol.TestSseServer\bin\$(Configuration)\net8.0\TestSseServer.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Condition="$([MSBuild]::IsOSPlatform('Linux'))" Include="..\ModelContextProtocol.TestSseServer\bin\$(Configuration)\net8.0\TestSseServer.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
45 changes: 12 additions & 33 deletions tests/ModelContextProtocol.Tests/SseServerIntegrationTestFixture.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using ModelContextProtocol.Client;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Client;
using ModelContextProtocol.Configuration;
using ModelContextProtocol.Protocol.Transport;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace ModelContextProtocol.Tests;

public class SseServerIntegrationTestFixture : IDisposable
public class SseServerIntegrationTestFixture : IAsyncDisposable
{
private Process _process;
private readonly CancellationTokenSource _stopCts = new();
private readonly Task _serverTask;

public ILoggerFactory LoggerFactory { get; }
public McpClientOptions DefaultOptions { get; }
Expand All @@ -35,40 +34,20 @@ public SseServerIntegrationTestFixture()
Location = "http://localhost:3001/sse"
};

Start();
_serverTask = TestSseServer.Program.MainAsync([], _stopCts.Token);
}

[MemberNotNull(nameof(_process))]
public void Start()
{
// Start the server (which is at TestSseServer.exe on windows and "dotnet TestSseServer.dll" on linux)
var processStartInfo = new ProcessStartInfo
{
FileName = OperatingSystem.IsWindows() ? "TestSseServer.exe" : "dotnet",
Arguments = "TestSseServer.dll",
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
};

_process = Process.Start(processStartInfo)
?? throw new InvalidOperationException($"Could not start process for {processStartInfo.FileName} with '{processStartInfo.Arguments}'.");

// Wait 1 second
Thread.Sleep(1000);
}

public void Dispose()
public async ValueTask DisposeAsync()
{
LoggerFactory.Dispose();
_stopCts.Cancel();
try
{
LoggerFactory?.Dispose();
await _serverTask.ConfigureAwait(false);
}
finally
catch (OperationCanceledException)
{
// Kill the server process
_process.Kill();
}
_stopCts.Dispose();
}
}