|
| 1 | +using Microsoft.Extensions.AI; |
| 2 | +using Microsoft.Extensions.DependencyInjection; |
| 3 | +using ModelContextProtocol.Client; |
| 4 | +using ModelContextProtocol.Protocol; |
| 5 | +using ModelContextProtocol.Server; |
| 6 | +using System.Text.Json; |
| 7 | +using System.Text.Json.Serialization.Metadata; |
| 8 | + |
| 9 | +namespace ModelContextProtocol.Tests.Server; |
| 10 | + |
| 11 | +public class CallToolResultOfTTests : ClientServerTestBase |
| 12 | +{ |
| 13 | + private McpServerPrimitiveCollection<McpServerTool> _toolCollection = []; |
| 14 | + |
| 15 | + public CallToolResultOfTTests(ITestOutputHelper testOutputHelper) |
| 16 | + : base(testOutputHelper, startServer: false) |
| 17 | + { |
| 18 | + } |
| 19 | + |
| 20 | + protected override void ConfigureServices(ServiceCollection services, IMcpServerBuilder mcpServerBuilder) |
| 21 | + { |
| 22 | + mcpServerBuilder.Services.Configure<McpServerOptions>(options => |
| 23 | + { |
| 24 | + options.ToolCollection = _toolCollection; |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public async Task CallToolAsyncOfT_DeserializesStructuredContent() |
| 30 | + { |
| 31 | + JsonSerializerOptions serOpts = new() { TypeInfoResolver = new DefaultJsonTypeInfoResolver() }; |
| 32 | + _toolCollection.Add(McpServerTool.Create( |
| 33 | + () => new CallToolResult<PersonData> { Content = new PersonData { Name = "Alice", Age = 30 } }, |
| 34 | + new() { Name = "get_person", SerializerOptions = serOpts })); |
| 35 | + |
| 36 | + StartServer(); |
| 37 | + var client = await CreateMcpClientForServer(); |
| 38 | + |
| 39 | + var result = await client.CallToolAsync<PersonData>( |
| 40 | + "get_person", |
| 41 | + options: new() { JsonSerializerOptions = serOpts }, |
| 42 | + cancellationToken: TestContext.Current.CancellationToken); |
| 43 | + |
| 44 | + Assert.NotNull(result); |
| 45 | + Assert.Equal("Alice", result.Name); |
| 46 | + Assert.Equal(30, result.Age); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public async Task CallToolAsyncOfT_ThrowsOnIsError() |
| 51 | + { |
| 52 | + JsonSerializerOptions serOpts = new() { TypeInfoResolver = new DefaultJsonTypeInfoResolver() }; |
| 53 | + _toolCollection.Add(McpServerTool.Create( |
| 54 | + () => new CallToolResult<string> { Content = "something went wrong", IsError = true }, |
| 55 | + new() { Name = "error_tool", SerializerOptions = serOpts })); |
| 56 | + |
| 57 | + StartServer(); |
| 58 | + var client = await CreateMcpClientForServer(); |
| 59 | + |
| 60 | + var ex = await Assert.ThrowsAsync<McpException>( |
| 61 | + () => client.CallToolAsync<string>( |
| 62 | + "error_tool", |
| 63 | + options: new() { JsonSerializerOptions = serOpts }, |
| 64 | + cancellationToken: TestContext.Current.CancellationToken).AsTask()); |
| 65 | + |
| 66 | + Assert.Contains("something went wrong", ex.Message); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public async Task CallToolAsyncOfT_FallsBackToTextContent() |
| 71 | + { |
| 72 | + // Use a regular tool that returns structured text, not CallToolResult<T> |
| 73 | + JsonSerializerOptions serOpts = new() { TypeInfoResolver = new DefaultJsonTypeInfoResolver() }; |
| 74 | + _toolCollection.Add(McpServerTool.Create( |
| 75 | + () => new PersonData { Name = "Bob", Age = 25 }, |
| 76 | + new() { Name = "text_tool", UseStructuredContent = true, SerializerOptions = serOpts })); |
| 77 | + |
| 78 | + StartServer(); |
| 79 | + var client = await CreateMcpClientForServer(); |
| 80 | + |
| 81 | + var result = await client.CallToolAsync<PersonData>( |
| 82 | + "text_tool", |
| 83 | + options: new() { JsonSerializerOptions = serOpts }, |
| 84 | + cancellationToken: TestContext.Current.CancellationToken); |
| 85 | + |
| 86 | + Assert.NotNull(result); |
| 87 | + Assert.Equal("Bob", result.Name); |
| 88 | + Assert.Equal(25, result.Age); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public async Task CallToolResultOfT_AdvertisesOutputSchema() |
| 93 | + { |
| 94 | + JsonSerializerOptions serOpts = new() { TypeInfoResolver = new DefaultJsonTypeInfoResolver() }; |
| 95 | + _toolCollection.Add(McpServerTool.Create( |
| 96 | + () => new CallToolResult<PersonData> { Content = new PersonData { Name = "Alice", Age = 30 } }, |
| 97 | + new() { Name = "schema_tool", SerializerOptions = serOpts })); |
| 98 | + |
| 99 | + StartServer(); |
| 100 | + var client = await CreateMcpClientForServer(); |
| 101 | + |
| 102 | + var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken); |
| 103 | + var tool = Assert.Single(tools); |
| 104 | + |
| 105 | + Assert.NotNull(tool.ProtocolTool.OutputSchema); |
| 106 | + Assert.Equal("object", tool.ProtocolTool.OutputSchema.Value.GetProperty("type").GetString()); |
| 107 | + Assert.True(tool.ProtocolTool.OutputSchema.Value.TryGetProperty("properties", out var props)); |
| 108 | + Assert.True(props.TryGetProperty("Name", out _)); |
| 109 | + Assert.True(props.TryGetProperty("Age", out _)); |
| 110 | + } |
| 111 | + |
| 112 | + [Fact] |
| 113 | + public async Task CallToolAsyncOfT_WithAsyncTool_Works() |
| 114 | + { |
| 115 | + JsonSerializerOptions serOpts = new() { TypeInfoResolver = new DefaultJsonTypeInfoResolver() }; |
| 116 | + _toolCollection.Add(McpServerTool.Create( |
| 117 | + async () => |
| 118 | + { |
| 119 | + await Task.CompletedTask; |
| 120 | + return new CallToolResult<PersonData> { Content = new PersonData { Name = "Charlie", Age = 35 } }; |
| 121 | + }, |
| 122 | + new() { Name = "async_tool", SerializerOptions = serOpts })); |
| 123 | + |
| 124 | + StartServer(); |
| 125 | + var client = await CreateMcpClientForServer(); |
| 126 | + |
| 127 | + var result = await client.CallToolAsync<PersonData>( |
| 128 | + "async_tool", |
| 129 | + options: new() { JsonSerializerOptions = serOpts }, |
| 130 | + cancellationToken: TestContext.Current.CancellationToken); |
| 131 | + |
| 132 | + Assert.NotNull(result); |
| 133 | + Assert.Equal("Charlie", result.Name); |
| 134 | + Assert.Equal(35, result.Age); |
| 135 | + } |
| 136 | + |
| 137 | + [Fact] |
| 138 | + public async Task CallToolAsyncOfT_WithArguments_Works() |
| 139 | + { |
| 140 | + JsonSerializerOptions serOpts = new() { TypeInfoResolver = new DefaultJsonTypeInfoResolver() }; |
| 141 | + _toolCollection.Add(McpServerTool.Create( |
| 142 | + (string name, int age) => new CallToolResult<PersonData> |
| 143 | + { |
| 144 | + Content = new PersonData { Name = name, Age = age } |
| 145 | + }, |
| 146 | + new() { Name = "create_person", SerializerOptions = serOpts })); |
| 147 | + |
| 148 | + StartServer(); |
| 149 | + var client = await CreateMcpClientForServer(); |
| 150 | + |
| 151 | + var result = await client.CallToolAsync<PersonData>( |
| 152 | + "create_person", |
| 153 | + new Dictionary<string, object?> { ["name"] = "Diana", ["age"] = 28 }, |
| 154 | + options: new() { JsonSerializerOptions = serOpts }, |
| 155 | + cancellationToken: TestContext.Current.CancellationToken); |
| 156 | + |
| 157 | + Assert.NotNull(result); |
| 158 | + Assert.Equal("Diana", result.Name); |
| 159 | + Assert.Equal(28, result.Age); |
| 160 | + } |
| 161 | + |
| 162 | + private class PersonData |
| 163 | + { |
| 164 | + public string? Name { get; set; } |
| 165 | + public int Age { get; set; } |
| 166 | + } |
| 167 | +} |
0 commit comments