-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMcpServerFixture.cs
More file actions
85 lines (71 loc) · 2.66 KB
/
Copy pathMcpServerFixture.cs
File metadata and controls
85 lines (71 loc) · 2.66 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
using ModelContextProtocol.Client;
using Xunit;
namespace GroupDocs.Viewer.Mcp.IntegrationTests.Fixtures;
/// Boots the published GroupDocs.Viewer.Mcp NuGet via `dnx` as a child process,
/// wires an MCP stdio client, and seeds a temporary storage folder with sample
/// documents. Shared across all tests in the same xUnit collection.
public sealed class McpServerFixture : IAsyncLifetime
{
public string StoragePath { get; } = Path.Combine(
Path.GetTempPath(),
$"gdview-mcp-it-{Guid.NewGuid():N}");
public string PackageVersionUnderTest => PackageVersion.Value;
public McpClient Client { get; private set; } = null!;
public async Task InitializeAsync()
{
Directory.CreateDirectory(StoragePath);
SampleDocuments.WriteAll(StoragePath);
SampleDocuments.CopyRealSamples(StoragePath, SampleDocuments.ResolveSourceSampleDocs());
var packageSpec = PackageVersion.IsLatest
? "GroupDocs.Viewer.Mcp"
: $"GroupDocs.Viewer.Mcp@{PackageVersion.Value}";
var transport = new StdioClientTransport(new StdioClientTransportOptions
{
Name = "groupdocs-viewer-mcp",
Command = CommandResolver.Resolve("dnx"),
Arguments = new[] { packageSpec, "--yes" },
WorkingDirectory = StoragePath,
EnvironmentVariables = BuildServerEnv(),
});
using var cts = new CancellationTokenSource(TimeSpan.FromMinutes(3));
Client = await McpClient.CreateAsync(transport, cancellationToken: cts.Token);
}
private Dictionary<string, string?> BuildServerEnv()
{
var env = new Dictionary<string, string?>
{
["GROUPDOCS_MCP_STORAGE_PATH"] = StoragePath,
["DOTNET_NOLOGO"] = "true",
};
var licensePath = Environment.GetEnvironmentVariable("GROUPDOCS_LICENSE_PATH");
if (!string.IsNullOrEmpty(licensePath))
env["GROUPDOCS_LICENSE_PATH"] = licensePath;
return env;
}
public async Task DisposeAsync()
{
try
{
if (Client is not null)
await Client.DisposeAsync();
}
catch
{
// Swallow disposal errors — we don't want them to mask test failures.
}
try
{
if (Directory.Exists(StoragePath))
Directory.Delete(StoragePath, recursive: true);
}
catch
{
// Best-effort cleanup on Windows where handles may linger briefly.
}
}
}
[CollectionDefinition(Name)]
public sealed class McpServerCollection : ICollectionFixture<McpServerFixture>
{
public const string Name = "mcp-server";
}