-
Notifications
You must be signed in to change notification settings - Fork 689
Expand file tree
/
Copy pathMcpClientToolTests.cs
More file actions
857 lines (687 loc) · 35.9 KB
/
McpClientToolTests.cs
File metadata and controls
857 lines (687 loc) · 35.9 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Server;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace ModelContextProtocol.Tests.Client;
public class McpClientToolTests : ClientServerTestBase
{
public McpClientToolTests(ITestOutputHelper outputHelper)
: base(outputHelper)
{
}
protected override void ConfigureServices(ServiceCollection services, IMcpServerBuilder mcpServerBuilder)
{
mcpServerBuilder.WithTools<TestTools>();
}
private class TestTools
{
// Tool that returns only text content
[McpServerTool]
public static TextContentBlock TextOnlyTool() =>
new()
{ Text = "Simple text result" };
// Tool that returns only text content (string)
[McpServerTool]
public static string StringTool() => "Simple string result";
// Tool that returns image content as single ContentBlock
[McpServerTool]
public static ImageContentBlock ImageTool() =>
new()
{ Data = System.Text.Encoding.UTF8.GetBytes(Convert.ToBase64String(Encoding.UTF8.GetBytes("fake-image-data"))), MimeType = "image/png" };
// Tool that returns audio content as single ContentBlock
[McpServerTool]
public static AudioContentBlock AudioTool() =>
new()
{ Data = System.Text.Encoding.UTF8.GetBytes(Convert.ToBase64String(Encoding.UTF8.GetBytes("fake-audio-data"))), MimeType = "audio/mp3" };
// Tool that returns embedded resource
[McpServerTool]
public static EmbeddedResourceBlock EmbeddedResourceTool() =>
new()
{ Resource = new TextResourceContents { Uri = "resource-uri", Text = "Resource text content", MimeType = "text/plain" } };
// Tool that returns mixed content (text + image) using IEnumerable<AIContent>
[McpServerTool]
public static IEnumerable<AIContent> MixedContentTool()
{
yield return new TextContent("Description of the image");
yield return new DataContent(Encoding.UTF8.GetBytes("fake-image-data"), "image/png");
}
// Tool that returns multiple images using IEnumerable<AIContent>
[McpServerTool]
public static IEnumerable<AIContent> MultipleImagesTool()
{
yield return new DataContent(Encoding.UTF8.GetBytes("image1"), "image/png");
yield return new DataContent(Encoding.UTF8.GetBytes("image2"), "image/jpeg");
}
// Tool that returns audio + text using IEnumerable<AIContent>
[McpServerTool]
public static IEnumerable<AIContent> AudioWithTextTool()
{
yield return new TextContent("Audio transcription");
yield return new DataContent(Encoding.UTF8.GetBytes("fake-audio"), "audio/wav");
}
// Tool that returns embedded resource + text using IEnumerable<ContentBlock>
[McpServerTool]
public static IEnumerable<ContentBlock> ResourceWithTextTool()
{
yield return new TextContentBlock { Text = "Resource description" };
yield return new EmbeddedResourceBlock { Resource = new TextResourceContents { Uri = "file://test.txt", Text = "File content", MimeType = "text/plain" } };
}
// Tool that returns all content types using IEnumerable<AIContent>
[McpServerTool]
public static IEnumerable<AIContent> AllContentTypesTool()
{
yield return new TextContent("Mixed content");
yield return new DataContent(Encoding.UTF8.GetBytes("image"), "image/png");
yield return new DataContent(Encoding.UTF8.GetBytes("audio"), "audio/mp3");
yield return new DataContent(Encoding.UTF8.GetBytes("blob"), "application/octet-stream");
}
// Tool that returns content that can't be converted to AIContent (ResourceLinkBlock)
[McpServerTool]
public static ResourceLinkBlock ResourceLinkTool() =>
new()
{ Uri = "file://test.txt", Name = "test.txt" };
// Tool that returns mixed content where some can't be converted (ResourceLinkBlock + Image)
[McpServerTool]
public static IEnumerable<ContentBlock> MixedWithNonConvertibleTool()
{
yield return new ImageContentBlock { Data = System.Text.Encoding.UTF8.GetBytes(Convert.ToBase64String(Encoding.UTF8.GetBytes("image-data"))), MimeType = "image/png" };
yield return new ResourceLinkBlock { Uri = "file://linked.txt", Name = "linked.txt" };
}
// Tool that returns CallToolResult with IsError = true
[McpServerTool]
public static CallToolResult ErrorTool() =>
new()
{
IsError = true,
Content = [new TextContentBlock { Text = "Error message" }]
};
// Tool that returns CallToolResult with StructuredContent
[McpServerTool]
public static CallToolResult StructuredContentTool() =>
new()
{
Content = [new TextContentBlock { Text = "Regular content" }],
StructuredContent = JsonElement.Parse("{\"key\":\"value\"}")
};
// Tool that returns CallToolResult with Meta
[McpServerTool]
public static CallToolResult MetaTool() =>
new()
{
Content = [new TextContentBlock { Text = "Content with meta" }],
Meta = new JsonObject { ["customKey"] = "customValue" }
};
// Tool that returns CallToolResult with multiple properties (IsError + Meta)
[McpServerTool]
public static CallToolResult ErrorWithMetaTool() =>
new()
{
IsError = true,
Content = [new TextContentBlock { Text = "Error with metadata" }],
Meta = new JsonObject { ["errorCode"] = 500 }
};
// Tool that returns binary resource (non-text)
[McpServerTool]
public static EmbeddedResourceBlock BinaryResourceTool() =>
new()
{
Resource = new BlobResourceContents
{
Uri = "data://blob",
Blob = System.Text.Encoding.UTF8.GetBytes(Convert.ToBase64String(Encoding.UTF8.GetBytes("binary-data"))),
MimeType = "application/octet-stream"
}
};
// Tool that echoes back the metadata it receives
[McpServerTool]
public static TextContentBlock MetadataEchoTool(RequestContext<CallToolRequestParams> context)
{
var meta = context.Params?.Meta;
var metaJson = meta?.ToJsonString() ?? "{}";
return new TextContentBlock { Text = metaJson };
}
// Tool that accepts arbitrary JsonElement parameter to test anonymous type serialization
[McpServerTool]
public static TextContentBlock ArgumentEchoTool(string text, JsonElement coordinates)
{
var result = new { text, coordinates };
return new TextContentBlock { Text = JsonSerializer.Serialize(result) };
}
}
[Fact]
public async Task TextOnlyTool_ReturnsSingleTextContent()
{
// Arrange
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "text_only_tool");
// Act
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
// Assert - single text content should return TextContent
var textContent = Assert.IsType<TextContent>(result);
Assert.Equal("Simple text result", textContent.Text);
}
[Fact]
public async Task StringTool_ReturnsSingleTextContent()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "string_tool");
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
var textContent = Assert.IsType<TextContent>(result);
Assert.Equal("Simple string result", textContent.Text);
}
[Fact]
public async Task ImageTool_ReturnsSingleDataContent()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "image_tool");
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
var dataContent = Assert.IsType<DataContent>(result);
Assert.Equal("image/png", dataContent.MediaType);
Assert.Equal("fake-image-data", Encoding.UTF8.GetString(dataContent.Data.ToArray()));
}
[Fact]
public async Task AudioTool_ReturnsSingleDataContent()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "audio_tool");
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
var dataContent = Assert.IsType<DataContent>(result);
Assert.Equal("audio/mp3", dataContent.MediaType);
Assert.Equal("fake-audio-data", Encoding.UTF8.GetString(dataContent.Data.ToArray()));
}
[Fact]
public async Task EmbeddedResourceTool_ReturnsSingleTextContent()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "embedded_resource_tool");
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
var textContent = Assert.IsType<TextContent>(result);
Assert.Equal("Resource text content", textContent.Text);
}
[Fact]
public async Task MixedContentTool_ReturnsAIContentArray()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "mixed_content_tool");
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
var aiContents = Assert.IsType<AIContent[]>(result);
Assert.Equal(2, aiContents.Length);
var textContent = Assert.IsType<TextContent>(aiContents[0]);
Assert.Equal("Description of the image", textContent.Text);
var dataContent = Assert.IsType<DataContent>(aiContents[1]);
Assert.Equal("image/png", dataContent.MediaType);
}
[Fact]
public async Task MultipleImagesTool_ReturnsAIContentArray()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "multiple_images_tool");
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
var aiContents = Assert.IsType<AIContent[]>(result);
Assert.Equal(2, aiContents.Length);
var dataContent0 = Assert.IsType<DataContent>(aiContents[0]);
Assert.Equal("image/png", dataContent0.MediaType);
Assert.Equal("image1", Encoding.UTF8.GetString(dataContent0.Data.ToArray()));
var dataContent1 = Assert.IsType<DataContent>(aiContents[1]);
Assert.Equal("image/jpeg", dataContent1.MediaType);
Assert.Equal("image2", Encoding.UTF8.GetString(dataContent1.Data.ToArray()));
}
[Fact]
public async Task AudioWithTextTool_ReturnsAIContentArray()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "audio_with_text_tool");
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
var aiContents = Assert.IsType<AIContent[]>(result);
Assert.Equal(2, aiContents.Length);
var textContent = Assert.IsType<TextContent>(aiContents[0]);
Assert.Equal("Audio transcription", textContent.Text);
var dataContent = Assert.IsType<DataContent>(aiContents[1]);
Assert.Equal("audio/wav", dataContent.MediaType);
}
[Fact]
public async Task ResourceWithTextTool_ReturnsAIContentArray()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "resource_with_text_tool");
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
var aiContents = Assert.IsType<AIContent[]>(result);
Assert.Equal(2, aiContents.Length);
var textContent0 = Assert.IsType<TextContent>(aiContents[0]);
Assert.Equal("Resource description", textContent0.Text);
var textContent1 = Assert.IsType<TextContent>(aiContents[1]);
Assert.Equal("File content", textContent1.Text);
}
[Fact]
public async Task AllContentTypesTool_ReturnsAIContentArray()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "all_content_types_tool");
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
var aiContents = Assert.IsType<AIContent[]>(result);
Assert.Equal(4, aiContents.Length);
var textContent = Assert.IsType<TextContent>(aiContents[0]);
Assert.Equal("Mixed content", textContent.Text);
var dataContent1 = Assert.IsType<DataContent>(aiContents[1]);
Assert.Equal("image/png", dataContent1.MediaType);
var dataContent2 = Assert.IsType<DataContent>(aiContents[2]);
Assert.Equal("audio/mp3", dataContent2.MediaType);
var dataContent3 = Assert.IsType<DataContent>(aiContents[3]);
Assert.Equal("application/octet-stream", dataContent3.MediaType);
}
[Fact]
public async Task SingleAIContent_PreservesRawRepresentation()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "image_tool");
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
var dataContent = Assert.IsType<DataContent>(result);
Assert.NotNull(dataContent.RawRepresentation);
var imageBlock = Assert.IsType<ImageContentBlock>(dataContent.RawRepresentation);
Assert.Equal("image/png", imageBlock.MimeType);
}
[Fact]
public async Task ResourceLinkTool_ReturnsJsonElement()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "resource_link_tool");
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.IsType<JsonElement>(result);
JsonElement jsonElement = (JsonElement)result!;
Assert.True(jsonElement.TryGetProperty("content", out var contentValue));
Assert.Equal(JsonValueKind.Array, contentValue.ValueKind);
Assert.Equal(1, contentValue.GetArrayLength());
}
[Fact]
public async Task MixedWithNonConvertibleTool_ReturnsJsonElement()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "mixed_with_non_convertible_tool");
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
var jsonElement = Assert.IsType<JsonElement>(result);
Assert.True(jsonElement.TryGetProperty("content", out var contentArray));
Assert.Equal(JsonValueKind.Array, contentArray.ValueKind);
Assert.Equal(2, contentArray.GetArrayLength());
var firstContent = contentArray[0];
Assert.True(firstContent.TryGetProperty("type", out var type1));
Assert.Equal("image", type1.GetString());
var secondContent = contentArray[1];
Assert.True(secondContent.TryGetProperty("type", out var type2));
Assert.Equal("resource_link", type2.GetString());
}
[Fact]
public async Task ErrorTool_ReturnsJsonElement()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "error_tool");
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.IsType<JsonElement>(result);
JsonElement jsonElement = (JsonElement)result!;
Assert.True(jsonElement.TryGetProperty("isError", out var isError));
Assert.True(isError.GetBoolean());
Assert.True(jsonElement.TryGetProperty("content", out var content));
Assert.Equal(JsonValueKind.Array, content.ValueKind);
}
[Fact]
public async Task StructuredContentTool_ReturnsJsonElement()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "structured_content_tool");
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
var jsonElement = Assert.IsType<JsonElement>(result);
Assert.True(jsonElement.TryGetProperty("structuredContent", out var structuredContent));
Assert.True(structuredContent.TryGetProperty("key", out var key));
Assert.Equal("value", key.GetString());
Assert.True(jsonElement.TryGetProperty("content", out var content));
Assert.Equal(JsonValueKind.Array, content.ValueKind);
}
[Fact]
public async Task MetaTool_ReturnsJsonElement()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "meta_tool");
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
var jsonElement = Assert.IsType<JsonElement>(result);
Assert.True(jsonElement.TryGetProperty("_meta", out var meta));
Assert.True(meta.TryGetProperty("customKey", out var customKey));
Assert.Equal("customValue", customKey.GetString());
Assert.True(jsonElement.TryGetProperty("content", out var content));
Assert.Equal(JsonValueKind.Array, content.ValueKind);
}
[Fact]
public async Task ErrorWithMetaTool_ReturnsJsonElement()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "error_with_meta_tool");
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.IsType<JsonElement>(result);
JsonElement jsonElement = (JsonElement)result!;
Assert.True(jsonElement.TryGetProperty("isError", out var isError));
Assert.True(isError.GetBoolean());
Assert.True(jsonElement.TryGetProperty("_meta", out var meta));
Assert.True(meta.TryGetProperty("errorCode", out var errorCode));
Assert.Equal(500, errorCode.GetInt32());
Assert.True(jsonElement.TryGetProperty("content", out var content));
Assert.Equal(JsonValueKind.Array, content.ValueKind);
}
[Fact]
public async Task BinaryResourceTool_ReturnsSingleDataContent()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "binary_resource_tool");
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
var dataContent = Assert.IsType<DataContent>(result);
Assert.Equal("application/octet-stream", dataContent.MediaType);
Assert.Equal("binary-data", Encoding.UTF8.GetString(dataContent.Data.ToArray()));
}
[Fact]
public async Task MultipleAIContent_PreservesRawRepresentation()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "mixed_content_tool");
var result = await tool.InvokeAsync(cancellationToken: TestContext.Current.CancellationToken);
var aiContents = Assert.IsType<AIContent[]>(result);
Assert.Equal(2, aiContents.Length);
var textContent = Assert.IsType<TextContent>(aiContents[0]);
Assert.NotNull(textContent.RawRepresentation);
Assert.IsType<TextContentBlock>(textContent.RawRepresentation);
var dataContent = Assert.IsType<DataContent>(aiContents[1]);
Assert.NotNull(dataContent.RawRepresentation);
Assert.IsType<ImageContentBlock>(dataContent.RawRepresentation);
}
[Fact]
public async Task WithMeta_MetaIsPassedToServer()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "metadata_echo_tool");
var result = await tool.WithMeta(new()
{
["traceId"] = "test-trace-123",
["customKey"] = "customValue"
}).CallAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.NotNull(result);
Assert.Single(result.Content);
var textBlock = Assert.IsType<TextContentBlock>(result.Content[0]);
// The tool echoes back the metadata it received
var receivedMetadata = JsonNode.Parse(textBlock.Text)?.AsObject();
Assert.NotNull(receivedMetadata);
Assert.Equal("test-trace-123", receivedMetadata["traceId"]?.GetValue<string>());
Assert.Equal("customValue", receivedMetadata["customKey"]?.GetValue<string>());
}
[Fact]
public async Task WithMeta_Null_PreviousMetaIsRemoved()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "metadata_echo_tool");
var result = await tool.WithMeta(new()
{
["traceId"] = "test-trace-123",
["customKey"] = "customValue"
}).WithMeta(null).CallAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.NotNull(result);
Assert.Single(result.Content);
var textBlock = Assert.IsType<TextContentBlock>(result.Content[0]);
var receivedMetadata = JsonNode.Parse(textBlock.Text)?.AsObject();
Assert.NotNull(receivedMetadata);
Assert.Null(receivedMetadata["traceId"]?.GetValue<string>());
Assert.Null(receivedMetadata["customKey"]?.GetValue<string>());
}
[Fact]
public async Task WithMeta_PreviousMetaIsOverwritten()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "metadata_echo_tool");
var result = await tool.WithMeta(new()
{
["traceId"] = "test-trace-123",
["customKey"] = "customValue"
}).WithMeta(new()
{
["traceId2"] = "abc",
["customKey2"] = "def"
}).CallAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.NotNull(result);
Assert.Single(result.Content);
var textBlock = Assert.IsType<TextContentBlock>(result.Content[0]);
var receivedMetadata = JsonNode.Parse(textBlock.Text)?.AsObject();
Assert.NotNull(receivedMetadata);
Assert.Null(receivedMetadata["traceId"]?.GetValue<string>());
Assert.Null(receivedMetadata["customKey"]?.GetValue<string>());
Assert.Equal("abc", receivedMetadata["traceId2"]?.GetValue<string>());
Assert.Equal("def", receivedMetadata["customKey2"]?.GetValue<string>());
}
[Fact]
public async Task WithMeta_CreatesNewInstance()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "text_only_tool");
var toolWithMeta = tool.WithMeta(new() { ["key"] = "value" });
Assert.NotSame(tool, toolWithMeta);
Assert.Equal(tool.Name, toolWithMeta.Name);
Assert.Equal(tool.Description, toolWithMeta.Description);
}
[Fact]
public async Task WithMeta_ChainsWithOtherWithMethods()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "metadata_echo_tool");
var modifiedTool = tool
.WithName("custom_name")
.WithDescription("Custom description")
.WithMeta(new() { ["chainedKey"] = "chainedValue" });
Assert.Equal("custom_name", modifiedTool.Name);
Assert.Equal("Custom description", modifiedTool.Description);
var result = await modifiedTool.CallAsync(cancellationToken: TestContext.Current.CancellationToken);
var textBlock = Assert.IsType<TextContentBlock>(result.Content[0]);
var receivedMetadata = JsonNode.Parse(textBlock.Text)?.AsObject();
Assert.NotNull(receivedMetadata);
Assert.Equal("chainedValue", receivedMetadata["chainedKey"]?.GetValue<string>());
}
[Fact]
public async Task WithMeta_MultipleToolInstancesWithDifferentMetadata()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "metadata_echo_tool");
var tool1 = tool.WithMeta(new() { ["clientId"] = "client-1" });
var tool2 = tool.WithMeta(new() { ["clientId"] = "client-2" });
var result1 = await tool1.CallAsync(cancellationToken: TestContext.Current.CancellationToken);
var result2 = await tool2.CallAsync(cancellationToken: TestContext.Current.CancellationToken);
// Assert - each call should have its own metadata
var textBlock1 = Assert.IsType<TextContentBlock>(result1.Content[0]);
var receivedMetadata1 = JsonNode.Parse(textBlock1.Text)?.AsObject();
Assert.Equal("client-1", receivedMetadata1?["clientId"]?.GetValue<string>());
var textBlock2 = Assert.IsType<TextContentBlock>(result2.Content[0]);
var receivedMetadata2 = JsonNode.Parse(textBlock2.Text)?.AsObject();
Assert.Equal("client-2", receivedMetadata2?["clientId"]?.GetValue<string>());
}
[Fact]
public async Task WithMeta_MergesWithRequestOptionsMeta_NonOverlappingKeys()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "metadata_echo_tool");
RequestOptions requestOptions = new()
{
Meta = new()
{
["requestKey"] = "requestValue"
}
};
var result = await tool.WithMeta(new()
{
["toolKey"] = "toolValue",
["sharedContext"] = "fromTool"
}).CallAsync(options: requestOptions, cancellationToken: TestContext.Current.CancellationToken);
var textBlock = Assert.IsType<TextContentBlock>(result.Content[0]);
var receivedMetadata = JsonNode.Parse(textBlock.Text)?.AsObject();
Assert.NotNull(receivedMetadata);
Assert.Equal("toolValue", receivedMetadata["toolKey"]?.GetValue<string>());
Assert.Equal("requestValue", receivedMetadata["requestKey"]?.GetValue<string>());
Assert.Equal("fromTool", receivedMetadata["sharedContext"]?.GetValue<string>());
}
[Fact]
public async Task WithMeta_MergesWithRequestOptionsMeta_OverlappingKeys_RequestOptionsWins()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "metadata_echo_tool");
RequestOptions requestOptions = new()
{
Meta = new JsonObject
{
["sharedKey"] = "fromRequestOptions",
["requestOnlyKey"] = "requestValue"
}
};
var result = await tool.WithMeta(new()
{
["sharedKey"] = "fromWithMeta",
["toolOnlyKey"] = "toolValue"
}).CallAsync(options: requestOptions, cancellationToken: TestContext.Current.CancellationToken);
var textBlock = Assert.IsType<TextContentBlock>(result.Content[0]);
var receivedMetadata = JsonNode.Parse(textBlock.Text)?.AsObject();
Assert.NotNull(receivedMetadata);
// RequestOptions should win for the shared key
Assert.Equal("fromRequestOptions", receivedMetadata["sharedKey"]?.GetValue<string>());
// Non-overlapping keys should both be present
Assert.Equal("toolValue", receivedMetadata["toolOnlyKey"]?.GetValue<string>());
Assert.Equal("requestValue", receivedMetadata["requestOnlyKey"]?.GetValue<string>());
}
[Fact]
public async Task WithMeta_WithEmptyRequestOptionsMeta_UsesToolMeta()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "metadata_echo_tool");
RequestOptions requestOptions = new()
{
Meta = new() // Empty meta
};
var result = await tool.WithMeta(new()
{
["toolKey"] = "toolValue"
}).CallAsync(options: requestOptions, cancellationToken: TestContext.Current.CancellationToken);
var textBlock = Assert.IsType<TextContentBlock>(result.Content[0]);
var receivedMetadata = JsonNode.Parse(textBlock.Text)?.AsObject();
Assert.NotNull(receivedMetadata);
Assert.Equal("toolValue", receivedMetadata["toolKey"]?.GetValue<string>());
}
[Fact]
public async Task WithMeta_DoesNotMutateOriginalMetadata()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "metadata_echo_tool");
JsonObject toolMeta = new()
{
["originalKey"] = "originalValue"
};
RequestOptions requestOptions = new()
{
Meta = new()
{
["newKey"] = "newValue"
}
};
await tool.WithMeta(toolMeta).CallAsync(options: requestOptions, cancellationToken: TestContext.Current.CancellationToken);
// original toolMeta should not be mutated
Assert.Single(toolMeta);
Assert.Equal("originalValue", toolMeta["originalKey"]?.GetValue<string>());
Assert.False(toolMeta.ContainsKey("newKey"));
}
[Fact]
public async Task WithMeta_MultipleCallsWithDifferentRequestOptions_DoNotInterfere()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "metadata_echo_tool");
var toolWithMeta = tool.WithMeta(new()
{
["baseKey"] = "baseValue"
});
var result1 = await toolWithMeta.CallAsync(
options: new RequestOptions { Meta = new JsonObject { ["callId"] = "call1" } },
cancellationToken: TestContext.Current.CancellationToken);
var result2 = await toolWithMeta.CallAsync(
options: new RequestOptions { Meta = new JsonObject { ["callId"] = "call2" } },
cancellationToken: TestContext.Current.CancellationToken);
var textBlock1 = Assert.IsType<TextContentBlock>(result1.Content[0]);
var receivedMetadata1 = JsonNode.Parse(textBlock1.Text)?.AsObject();
Assert.Equal("baseValue", receivedMetadata1?["baseKey"]?.GetValue<string>());
Assert.Equal("call1", receivedMetadata1?["callId"]?.GetValue<string>());
var textBlock2 = Assert.IsType<TextContentBlock>(result2.Content[0]);
var receivedMetadata2 = JsonNode.Parse(textBlock2.Text)?.AsObject();
Assert.Equal("baseValue", receivedMetadata2?["baseKey"]?.GetValue<string>());
Assert.Equal("call2", receivedMetadata2?["callId"]?.GetValue<string>());
}
[Fact]
public async Task CallAsync_WithOnlyRequestOptionsMeta_NoWithMeta_WorksCorrectly()
{
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
var tool = tools.Single(t => t.Name == "metadata_echo_tool");
RequestOptions requestOptions = new()
{
Meta = new()
{
["requestOnlyKey"] = "requestOnlyValue"
}
};
var result = await tool.CallAsync(options: requestOptions, cancellationToken: TestContext.Current.CancellationToken);
var textBlock = Assert.IsType<TextContentBlock>(result.Content[0]);
var receivedMetadata = JsonNode.Parse(textBlock.Text)?.AsObject();
Assert.NotNull(receivedMetadata);
Assert.Equal("requestOnlyValue", receivedMetadata["requestOnlyKey"]?.GetValue<string>());
}
[Fact]
public async Task CallToolAsync_WithAnonymousTypeArguments_Works()
{
if (!JsonSerializer.IsReflectionEnabledByDefault)
{
return;
}
await using McpClient client = await CreateMcpClientForServer();
// Call with dictionary containing anonymous type values
var arguments = new Dictionary<string, object?>
{
["text"] = "test",
["coordinates"] = new { X = 1.0, Y = 2.0 } // Anonymous type
};
// This should not throw NotSupportedException
var result = await client.CallToolAsync("argument_echo_tool", arguments, cancellationToken: TestContext.Current.CancellationToken);
Assert.NotNull(result);
Assert.NotEmpty(result.Content);
// Verify the anonymous type was serialized correctly
var textBlock = Assert.IsType<TextContentBlock>(result.Content[0]);
Assert.Contains("coordinates", textBlock.Text);
}
}