|
| 1 | +using Microsoft.Extensions.AI; |
| 2 | +using Microsoft.Extensions.DependencyInjection; |
| 3 | +using ModelContextProtocol.Client; |
| 4 | +using ModelContextProtocol.Protocol; |
| 5 | +using ModelContextProtocol.Server; |
| 6 | +using ModelContextProtocol.Tests.Utils; |
| 7 | + |
| 8 | +namespace ModelContextProtocol.Tests.Client; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Tests for MRTR compatibility across different experimental/non-experimental combinations. |
| 12 | +/// This test class configures the server WITHOUT ExperimentalProtocolVersion to test scenarios |
| 13 | +/// where the server is not opted-in to the experimental protocol. |
| 14 | +/// </summary> |
| 15 | +public class McpClientMrtrCompatTests : ClientServerTestBase |
| 16 | +{ |
| 17 | + public McpClientMrtrCompatTests(ITestOutputHelper testOutputHelper) |
| 18 | + : base(testOutputHelper, startServer: false) |
| 19 | + { |
| 20 | + } |
| 21 | + |
| 22 | + protected override void ConfigureServices(ServiceCollection services, IMcpServerBuilder mcpServerBuilder) |
| 23 | + { |
| 24 | + // Deliberately NOT setting ExperimentalProtocolVersion on the server. |
| 25 | + mcpServerBuilder.WithTools([ |
| 26 | + McpServerTool.Create( |
| 27 | + async (string prompt, McpServer server, CancellationToken ct) => |
| 28 | + { |
| 29 | + var result = await server.SampleAsync(new CreateMessageRequestParams |
| 30 | + { |
| 31 | + Messages = [new SamplingMessage { Role = Role.User, Content = [new TextContentBlock { Text = prompt }] }], |
| 32 | + MaxTokens = 100 |
| 33 | + }, ct); |
| 34 | + |
| 35 | + return result.Content.OfType<TextContentBlock>().FirstOrDefault()?.Text ?? "No response"; |
| 36 | + }, |
| 37 | + new McpServerToolCreateOptions |
| 38 | + { |
| 39 | + Name = "sampling-tool", |
| 40 | + Description = "A tool that requests sampling from the client" |
| 41 | + }), |
| 42 | + McpServerTool.Create( |
| 43 | + async (string message, McpServer server, CancellationToken ct) => |
| 44 | + { |
| 45 | + var result = await server.ElicitAsync(new ElicitRequestParams |
| 46 | + { |
| 47 | + Message = message, |
| 48 | + RequestedSchema = new() |
| 49 | + }, ct); |
| 50 | + |
| 51 | + return $"{result.Action}:{result.Content?.FirstOrDefault().Value}"; |
| 52 | + }, |
| 53 | + new McpServerToolCreateOptions |
| 54 | + { |
| 55 | + Name = "elicitation-tool", |
| 56 | + Description = "A tool that requests elicitation from the client" |
| 57 | + }), |
| 58 | + ]); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public async Task CallToolAsync_NeitherExperimental_UsesLegacyRequests() |
| 63 | + { |
| 64 | + // Neither client nor server sets ExperimentalProtocolVersion. |
| 65 | + // Server sends standard JSON-RPC sampling/elicitation requests. |
| 66 | + StartServer(); |
| 67 | + var clientOptions = new McpClientOptions(); |
| 68 | + clientOptions.Handlers.SamplingHandler = (request, progress, ct) => |
| 69 | + { |
| 70 | + var text = request?.Messages[request.Messages.Count - 1].Content.OfType<TextContentBlock>().FirstOrDefault()?.Text; |
| 71 | + return new ValueTask<CreateMessageResult>(new CreateMessageResult |
| 72 | + { |
| 73 | + Content = [new TextContentBlock { Text = $"Legacy: {text}" }], |
| 74 | + Model = "test-model" |
| 75 | + }); |
| 76 | + }; |
| 77 | + |
| 78 | + await using var client = await CreateMcpClientForServer(clientOptions); |
| 79 | + |
| 80 | + // Verify the negotiated version is a standard stable version |
| 81 | + Assert.NotEqual("2026-06-XX", client.NegotiatedProtocolVersion); |
| 82 | + |
| 83 | + var result = await client.CallToolAsync("sampling-tool", |
| 84 | + new Dictionary<string, object?> { ["prompt"] = "Hello" }, |
| 85 | + cancellationToken: TestContext.Current.CancellationToken); |
| 86 | + |
| 87 | + var content = Assert.Single(result.Content); |
| 88 | + Assert.Equal("Legacy: Hello", Assert.IsType<TextContentBlock>(content).Text); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public async Task CallToolAsync_ClientExperimentalServerNot_FallsBackToLegacy() |
| 93 | + { |
| 94 | + // Client requests experimental version, server doesn't recognize it, |
| 95 | + // negotiates to stable. Everything works via legacy path. |
| 96 | + StartServer(); |
| 97 | + var clientOptions = new McpClientOptions { ExperimentalProtocolVersion = "2026-06-XX" }; |
| 98 | + clientOptions.Handlers.SamplingHandler = (request, progress, ct) => |
| 99 | + { |
| 100 | + var text = request?.Messages[request.Messages.Count - 1].Content.OfType<TextContentBlock>().FirstOrDefault()?.Text; |
| 101 | + return new ValueTask<CreateMessageResult>(new CreateMessageResult |
| 102 | + { |
| 103 | + Content = [new TextContentBlock { Text = $"Legacy: {text}" }], |
| 104 | + Model = "test-model" |
| 105 | + }); |
| 106 | + }; |
| 107 | + |
| 108 | + await using var client = await CreateMcpClientForServer(clientOptions); |
| 109 | + |
| 110 | + // Verify the server did NOT negotiate to the experimental version |
| 111 | + Assert.NotEqual("2026-06-XX", client.NegotiatedProtocolVersion); |
| 112 | + |
| 113 | + var result = await client.CallToolAsync("sampling-tool", |
| 114 | + new Dictionary<string, object?> { ["prompt"] = "From exp client" }, |
| 115 | + cancellationToken: TestContext.Current.CancellationToken); |
| 116 | + |
| 117 | + var content = Assert.Single(result.Content); |
| 118 | + Assert.Equal("Legacy: From exp client", Assert.IsType<TextContentBlock>(content).Text); |
| 119 | + } |
| 120 | + |
| 121 | + [Fact] |
| 122 | + public async Task CallToolAsync_NeitherExperimental_ElicitationUsesLegacyRequests() |
| 123 | + { |
| 124 | + StartServer(); |
| 125 | + var clientOptions = new McpClientOptions(); |
| 126 | + clientOptions.Handlers.ElicitationHandler = (request, ct) => |
| 127 | + { |
| 128 | + return new ValueTask<ElicitResult>(new ElicitResult |
| 129 | + { |
| 130 | + Action = "confirm", |
| 131 | + Content = new Dictionary<string, System.Text.Json.JsonElement> |
| 132 | + { |
| 133 | + ["response"] = System.Text.Json.JsonDocument.Parse("\"yes\"").RootElement.Clone() |
| 134 | + } |
| 135 | + }); |
| 136 | + }; |
| 137 | + |
| 138 | + await using var client = await CreateMcpClientForServer(clientOptions); |
| 139 | + |
| 140 | + var result = await client.CallToolAsync("elicitation-tool", |
| 141 | + new Dictionary<string, object?> { ["message"] = "Agree?" }, |
| 142 | + cancellationToken: TestContext.Current.CancellationToken); |
| 143 | + |
| 144 | + var content = Assert.Single(result.Content); |
| 145 | + Assert.Equal("confirm:yes", Assert.IsType<TextContentBlock>(content).Text); |
| 146 | + } |
| 147 | +} |
0 commit comments