Add initial Activity / Metric support (#183)
This commit is contained in:
parent
674cb1515c
commit
4b38d3e71b
1 changed files with 443 additions and 61 deletions
79
tests/ModelContextProtocol.Tests/DiagnosticTests.cs
Normal file
79
tests/ModelContextProtocol.Tests/DiagnosticTests.cs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
using ModelContextProtocol.Client;
|
||||
using ModelContextProtocol.Protocol.Transport;
|
||||
using ModelContextProtocol.Protocol.Types;
|
||||
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("ModelContextProtocol")
|
||||
.AddInMemoryExporter(activities)
|
||||
.Build())
|
||||
{
|
||||
await RunConnected(async (client, server) =>
|
||||
{
|
||||
var tools = await client.ListToolsAsync(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()
|
||||
{
|
||||
ServerInfo = new Implementation { Name = "TestServer", Version = "1.0.0" },
|
||||
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(new()
|
||||
{
|
||||
Id = "TestServer",
|
||||
Name = "TestServer",
|
||||
TransportType = TransportTypes.StdIo,
|
||||
},
|
||||
createTransportFunc: (_, __) => clientTransport,
|
||||
cancellationToken: TestContext.Current.CancellationToken))
|
||||
{
|
||||
await action(client, server);
|
||||
}
|
||||
}
|
||||
|
||||
await serverTask;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue