-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathDiagnosticTests.cs
72 lines (61 loc) · 2.66 KB
/
DiagnosticTests.cs
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
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol.Transport;
using ModelContextProtocol.Server;
using OpenTelemetry.Trace;
using System.Diagnostics;
using System.IO.Pipelines;
namespace ModelContextProtocol.Tests;
[Collection(nameof(DisableParallelization))]
public class DiagnosticTests
{
[Fact]
public async Task Session_TracksActivities()
{
var activities = new List<Activity>();
using (var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder()
.AddSource("Experimental.ModelContextProtocol")
.AddInMemoryExporter(activities)
.Build())
{
await RunConnected(async (client, server) =>
{
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.NotNull(tools);
Assert.NotEmpty(tools);
var tool = tools.First(t => t.Name == "DoubleValue");
await tool.InvokeAsync(new Dictionary<string, object?>() { ["amount"] = 42 }, TestContext.Current.CancellationToken);
});
}
Assert.NotEmpty(activities);
Activity toolCallActivity = activities.First(a =>
a.Tags.Any(t => t.Key == "rpc.method" && t.Value == "tools/call"));
Assert.Equal("DoubleValue", toolCallActivity.Tags.First(t => t.Key == "mcp.request.params.name").Value);
}
private static async Task RunConnected(Func<IMcpClient, IMcpServer, Task> action)
{
Pipe clientToServerPipe = new(), serverToClientPipe = new();
StreamServerTransport serverTransport = new(clientToServerPipe.Reader.AsStream(), serverToClientPipe.Writer.AsStream());
StreamClientTransport clientTransport = new(clientToServerPipe.Writer.AsStream(), serverToClientPipe.Reader.AsStream());
Task serverTask;
await using (IMcpServer server = McpServerFactory.Create(serverTransport, new()
{
Capabilities = new()
{
Tools = new()
{
ToolCollection = [McpServerTool.Create((int amount) => amount * 2, new() { Name = "DoubleValue", Description = "Doubles the value." })],
}
}
}))
{
serverTask = server.RunAsync(TestContext.Current.CancellationToken);
await using (IMcpClient client = await McpClientFactory.CreateAsync(
clientTransport,
cancellationToken: TestContext.Current.CancellationToken))
{
await action(client, server);
}
}
await serverTask;
}
}