-
Notifications
You must be signed in to change notification settings - Fork 689
Expand file tree
/
Copy pathDiagnosticTests.cs
More file actions
201 lines (165 loc) · 8.89 KB
/
DiagnosticTests.cs
File metadata and controls
201 lines (165 loc) · 8.89 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
using OpenTelemetry.Trace;
using System.Diagnostics;
using System.IO.Pipelines;
using System.Text;
using System.Text.Json;
namespace ModelContextProtocol.Tests;
[Collection(nameof(DisableParallelization))]
public class DiagnosticTests
{
[Fact]
public async Task Session_TracksActivities()
{
// Ensure no ambient activity from previous tests
Activity.Current = null;
var activities = new List<Activity>();
var clientToServerLog = new List<string>();
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() { ["amount"] = 42 }, TestContext.Current.CancellationToken);
}, clientToServerLog);
// Ensure all activities are flushed before disposal
tracerProvider.ForceFlush();
}
Assert.NotEmpty(activities);
var clientToolCall = Assert.Single(activities, a =>
a.Tags.Any(t => t.Key == "mcp.tool.name" && t.Value == "DoubleValue") &&
a.Tags.Any(t => t.Key == "mcp.method.name" && t.Value == "tools/call") &&
a.DisplayName == "tools/call DoubleValue" &&
a.Kind == ActivityKind.Client &&
a.Status == ActivityStatusCode.Unset);
var serverToolCall = Assert.Single(activities, a =>
a.Tags.Any(t => t.Key == "mcp.tool.name" && t.Value == "DoubleValue") &&
a.Tags.Any(t => t.Key == "mcp.method.name" && t.Value == "tools/call") &&
a.DisplayName == "tools/call DoubleValue" &&
a.Kind == ActivityKind.Server &&
a.Status == ActivityStatusCode.Unset);
Assert.Equal(clientToolCall.SpanId, serverToolCall.ParentSpanId);
Assert.Equal(clientToolCall.TraceId, serverToolCall.TraceId);
var clientListToolsCall = Assert.Single(activities, a =>
a.Tags.Any(t => t.Key == "mcp.method.name" && t.Value == "tools/list") &&
a.DisplayName == "tools/list" &&
a.Kind == ActivityKind.Client &&
a.Status == ActivityStatusCode.Unset);
var serverListToolsCall = Assert.Single(activities, a =>
a.Tags.Any(t => t.Key == "mcp.method.name" && t.Value == "tools/list") &&
a.DisplayName == "tools/list" &&
a.Kind == ActivityKind.Server &&
a.Status == ActivityStatusCode.Unset);
Assert.Equal(clientListToolsCall.SpanId, serverListToolsCall.ParentSpanId);
Assert.Equal(clientListToolsCall.TraceId, serverListToolsCall.TraceId);
// Validate that the client trace context encoded to request.params._meta[traceparent]
using var listToolsJson = JsonDocument.Parse(clientToServerLog.First(s => s.Contains("\"method\":\"tools/list\"")));
var metaJson = listToolsJson.RootElement.GetProperty("params").GetProperty("_meta").GetRawText();
Assert.Equal($$"""{"traceparent":"00-{{clientListToolsCall.TraceId}}-{{clientListToolsCall.SpanId}}-01"}""", metaJson);
}
[Fact]
public async Task Session_FailedToolCall()
{
// Ensure no ambient activity from previous tests
Activity.Current = null;
var activities = new List<Activity>();
using (var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder()
.AddSource("Experimental.ModelContextProtocol")
.AddInMemoryExporter(activities)
.Build())
{
await RunConnected(async (client, server) =>
{
await client.CallToolAsync("Throw", cancellationToken: TestContext.Current.CancellationToken);
await Assert.ThrowsAsync<McpProtocolException>(async () => await client.CallToolAsync("does-not-exist", cancellationToken: TestContext.Current.CancellationToken));
}, []);
// Ensure all activities are flushed before disposal
tracerProvider.ForceFlush();
}
Assert.NotEmpty(activities);
var throwToolClient = Assert.Single(activities, a =>
a.Tags.Any(t => t.Key == "mcp.tool.name" && t.Value == "Throw") &&
a.Tags.Any(t => t.Key == "mcp.method.name" && t.Value == "tools/call") &&
a.DisplayName == "tools/call Throw" &&
a.Kind == ActivityKind.Client);
Assert.Equal(ActivityStatusCode.Error, throwToolClient.Status);
var throwToolServer = Assert.Single(activities, a =>
a.Tags.Any(t => t.Key == "mcp.tool.name" && t.Value == "Throw") &&
a.Tags.Any(t => t.Key == "mcp.method.name" && t.Value == "tools/call") &&
a.DisplayName == "tools/call Throw" &&
a.Kind == ActivityKind.Server);
Assert.Equal(ActivityStatusCode.Error, throwToolServer.Status);
var doesNotExistToolClient = Assert.Single(activities, a =>
a.Tags.Any(t => t.Key == "mcp.tool.name" && t.Value == "does-not-exist") &&
a.Tags.Any(t => t.Key == "mcp.method.name" && t.Value == "tools/call") &&
a.DisplayName == "tools/call does-not-exist" &&
a.Kind == ActivityKind.Client);
Assert.Equal(ActivityStatusCode.Error, doesNotExistToolClient.Status);
Assert.Equal("-32602", doesNotExistToolClient.Tags.Single(t => t.Key == "rpc.jsonrpc.error_code").Value);
var doesNotExistToolServer = Assert.Single(activities, a =>
a.Tags.Any(t => t.Key == "mcp.tool.name" && t.Value == "does-not-exist") &&
a.Tags.Any(t => t.Key == "mcp.method.name" && t.Value == "tools/call") &&
a.DisplayName == "tools/call does-not-exist" &&
a.Kind == ActivityKind.Server);
Assert.Equal(ActivityStatusCode.Error, doesNotExistToolServer.Status);
Assert.Equal("-32602", doesNotExistToolClient.Tags.Single(t => t.Key == "rpc.jsonrpc.error_code").Value);
}
private static async Task RunConnected(Func<McpClient, McpServer, Task> action, List<string> clientToServerLog)
{
Pipe clientToServerPipe = new(), serverToClientPipe = new();
StreamServerTransport serverTransport = new(clientToServerPipe.Reader.AsStream(), serverToClientPipe.Writer.AsStream());
StreamClientTransport clientTransport = new(new LoggingStream(
clientToServerPipe.Writer.AsStream(), clientToServerLog.Add), serverToClientPipe.Reader.AsStream());
Task serverTask;
await using (McpServer server = McpServer.Create(serverTransport, new()
{
ToolCollection = [
McpServerTool.Create((int amount) => amount * 2, new() { Name = "DoubleValue", Description = "Doubles the value." }),
McpServerTool.Create(() => { throw new Exception("boom"); }, new() { Name = "Throw", Description = "Throws error." }),
]
}))
{
serverTask = server.RunAsync(TestContext.Current.CancellationToken);
await using (McpClient client = await McpClient.CreateAsync(
clientTransport,
cancellationToken: TestContext.Current.CancellationToken))
{
await action(client, server);
}
}
await serverTask;
}
}
public class LoggingStream : Stream
{
private readonly Stream _innerStream;
private readonly Action<string> _logAction;
public LoggingStream(Stream innerStream, Action<string> logAction)
{
_innerStream = innerStream ?? throw new ArgumentNullException(nameof(innerStream));
_logAction = logAction ?? throw new ArgumentNullException(nameof(logAction));
}
public override void Write(byte[] buffer, int offset, int count)
{
var data = Encoding.UTF8.GetString(buffer, offset, count);
_logAction(data);
_innerStream.Write(buffer, offset, count);
}
public override bool CanRead => _innerStream.CanRead;
public override bool CanSeek => _innerStream.CanSeek;
public override bool CanWrite => _innerStream.CanWrite;
public override long Length => _innerStream.Length;
public override long Position { get => _innerStream.Position; set => _innerStream.Position = value; }
public override void Flush() => _innerStream.Flush();
public override int Read(byte[] buffer, int offset, int count) => _innerStream.Read(buffer, offset, count);
public override long Seek(long offset, SeekOrigin origin) => _innerStream.Seek(offset, origin);
public override void SetLength(long value) => _innerStream.SetLength(value);
}