-
Notifications
You must be signed in to change notification settings - Fork 657
Expand file tree
/
Copy pathClientServerTestBase.cs
More file actions
98 lines (81 loc) · 3.2 KB
/
ClientServerTestBase.cs
File metadata and controls
98 lines (81 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
using ModelContextProtocol.Tests.Utils;
using System.IO.Pipelines;
namespace ModelContextProtocol.Tests;
public abstract class ClientServerTestBase : LoggedTest, IAsyncDisposable
{
private readonly Pipe _clientToServerPipe = new();
private readonly Pipe _serverToClientPipe = new();
private readonly CancellationTokenSource _cts = CancellationTokenSource.CreateLinkedTokenSource(TestContext.Current.CancellationToken);
private Task _serverTask = Task.CompletedTask;
public ClientServerTestBase(ITestOutputHelper testOutputHelper, bool startServer = true)
: base(testOutputHelper)
{
ServiceCollection.AddLogging();
ServiceCollection.AddSingleton(XunitLoggerProvider);
ServiceCollection.AddSingleton<ILoggerProvider>(MockLoggerProvider);
McpServerBuilder = ServiceCollection
.AddMcpServer()
.WithStreamServerTransport(_clientToServerPipe.Reader.AsStream(), _serverToClientPipe.Writer.AsStream());
ConfigureServices(ServiceCollection, McpServerBuilder);
if (startServer)
{
StartServer();
}
}
protected ServiceCollection ServiceCollection { get; } = [];
protected IMcpServerBuilder McpServerBuilder { get; }
protected McpServer Server
{
get => field ?? throw new InvalidOperationException("You must call StartServer first.");
private set => field = value;
}
protected ServiceProvider ServiceProvider
{
get => field ?? throw new InvalidOperationException("You must call StartServer first.");
private set => field = value;
}
protected virtual void ConfigureServices(ServiceCollection services, IMcpServerBuilder mcpServerBuilder)
{
}
protected McpServer StartServer()
{
ServiceProvider = ServiceCollection.BuildServiceProvider(validateScopes: true);
Server = ServiceProvider.GetRequiredService<McpServer>();
_serverTask = Server.RunAsync(_cts.Token);
return Server;
}
public async ValueTask DisposeAsync()
{
await _cts.CancelAsync();
_clientToServerPipe.Writer.Complete();
_serverToClientPipe.Writer.Complete();
await _serverTask;
if (ServiceProvider is IAsyncDisposable asyncDisposable)
{
await asyncDisposable.DisposeAsync();
}
else if (ServiceProvider is IDisposable disposable)
{
disposable.Dispose();
}
_cts.Dispose();
Dispose();
}
protected async Task<McpClient> CreateMcpClientForServer(McpClientOptions? clientOptions = null)
{
return await McpClient.CreateAsync(
new StreamClientTransport(
serverInput: _clientToServerPipe.Writer.AsStream(),
_serverToClientPipe.Reader.AsStream(),
LoggerFactory),
clientOptions: clientOptions,
loggerFactory: LoggerFactory,
cancellationToken: TestContext.Current.CancellationToken);
}
}