|
| 1 | +using ModelContextProtocol.Client; |
| 2 | +using ModelContextProtocol.Protocol; |
| 3 | +using ModelContextProtocol.Tests.Utils; |
| 4 | + |
| 5 | +namespace ModelContextProtocol.Tests; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Integration tests for connecting to the Microsoft Learn MCP server. |
| 9 | +/// These tests connect to a live external service and are marked as Manual execution. |
| 10 | +/// </summary> |
| 11 | +public class MicrosoftLearnMcpServerTests(ITestOutputHelper testOutputHelper) : LoggedTest(testOutputHelper) |
| 12 | +{ |
| 13 | + private const string MicrosoftLearnMcpEndpoint = "https://learn.microsoft.com/api/mcp"; |
| 14 | + |
| 15 | + [Fact] |
| 16 | + [Trait("Execution", "Manual")] |
| 17 | + public async Task ConnectAndInitialize_MicrosoftLearnServer_WithStreamableHttp() |
| 18 | + { |
| 19 | + // Arrange |
| 20 | + var transportOptions = new HttpClientTransportOptions |
| 21 | + { |
| 22 | + Endpoint = new Uri(MicrosoftLearnMcpEndpoint), |
| 23 | + Name = "Microsoft Learn MCP Server", |
| 24 | + TransportMode = HttpTransportMode.StreamableHttp, |
| 25 | + }; |
| 26 | + |
| 27 | + var clientOptions = new McpClientOptions |
| 28 | + { |
| 29 | + ClientInfo = new() { Name = "CSharpSdkIntegrationTest", Version = "1.0.0" } |
| 30 | + }; |
| 31 | + |
| 32 | + // Act |
| 33 | + await using var client = await McpClient.CreateAsync( |
| 34 | + new HttpClientTransport(transportOptions), |
| 35 | + clientOptions, |
| 36 | + loggerFactory: LoggerFactory, |
| 37 | + cancellationToken: TestContext.Current.CancellationToken); |
| 38 | + |
| 39 | + // Assert |
| 40 | + Assert.NotNull(client); |
| 41 | + Assert.NotNull(client.ServerCapabilities); |
| 42 | + Assert.NotNull(client.ServerInfo); |
| 43 | + Assert.NotNull(client.NegotiatedProtocolVersion); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + [Trait("Execution", "Manual")] |
| 48 | + public async Task ListTools_MicrosoftLearnServer() |
| 49 | + { |
| 50 | + // Arrange |
| 51 | + var transportOptions = new HttpClientTransportOptions |
| 52 | + { |
| 53 | + Endpoint = new Uri(MicrosoftLearnMcpEndpoint), |
| 54 | + Name = "Microsoft Learn MCP Server", |
| 55 | + TransportMode = HttpTransportMode.StreamableHttp, |
| 56 | + }; |
| 57 | + |
| 58 | + var clientOptions = new McpClientOptions |
| 59 | + { |
| 60 | + ClientInfo = new() { Name = "CSharpSdkIntegrationTest", Version = "1.0.0" } |
| 61 | + }; |
| 62 | + |
| 63 | + // Act |
| 64 | + await using var client = await McpClient.CreateAsync( |
| 65 | + new HttpClientTransport(transportOptions), |
| 66 | + clientOptions, |
| 67 | + loggerFactory: LoggerFactory, |
| 68 | + cancellationToken: TestContext.Current.CancellationToken); |
| 69 | + |
| 70 | + var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken); |
| 71 | + |
| 72 | + // Assert |
| 73 | + Assert.NotNull(tools); |
| 74 | + Assert.NotEmpty(tools); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + [Trait("Execution", "Manual")] |
| 79 | + public async Task ListResources_MicrosoftLearnServer() |
| 80 | + { |
| 81 | + // Arrange |
| 82 | + var transportOptions = new HttpClientTransportOptions |
| 83 | + { |
| 84 | + Endpoint = new Uri(MicrosoftLearnMcpEndpoint), |
| 85 | + Name = "Microsoft Learn MCP Server", |
| 86 | + TransportMode = HttpTransportMode.StreamableHttp, |
| 87 | + }; |
| 88 | + |
| 89 | + var clientOptions = new McpClientOptions |
| 90 | + { |
| 91 | + ClientInfo = new() { Name = "CSharpSdkIntegrationTest", Version = "1.0.0" } |
| 92 | + }; |
| 93 | + |
| 94 | + // Act |
| 95 | + await using var client = await McpClient.CreateAsync( |
| 96 | + new HttpClientTransport(transportOptions), |
| 97 | + clientOptions, |
| 98 | + loggerFactory: LoggerFactory, |
| 99 | + cancellationToken: TestContext.Current.CancellationToken); |
| 100 | + |
| 101 | + var resources = await client.ListResourcesAsync(TestContext.Current.CancellationToken); |
| 102 | + |
| 103 | + // Assert |
| 104 | + Assert.NotNull(resources); |
| 105 | + // Microsoft Learn server may or may not have resources, so we don't assert NotEmpty |
| 106 | + } |
| 107 | + |
| 108 | + [Fact] |
| 109 | + [Trait("Execution", "Manual")] |
| 110 | + public async Task ListPrompts_MicrosoftLearnServer() |
| 111 | + { |
| 112 | + // Arrange |
| 113 | + var transportOptions = new HttpClientTransportOptions |
| 114 | + { |
| 115 | + Endpoint = new Uri(MicrosoftLearnMcpEndpoint), |
| 116 | + Name = "Microsoft Learn MCP Server", |
| 117 | + TransportMode = HttpTransportMode.StreamableHttp, |
| 118 | + }; |
| 119 | + |
| 120 | + var clientOptions = new McpClientOptions |
| 121 | + { |
| 122 | + ClientInfo = new() { Name = "CSharpSdkIntegrationTest", Version = "1.0.0" } |
| 123 | + }; |
| 124 | + |
| 125 | + // Act |
| 126 | + await using var client = await McpClient.CreateAsync( |
| 127 | + new HttpClientTransport(transportOptions), |
| 128 | + clientOptions, |
| 129 | + loggerFactory: LoggerFactory, |
| 130 | + cancellationToken: TestContext.Current.CancellationToken); |
| 131 | + |
| 132 | + var prompts = await client.ListPromptsAsync(TestContext.Current.CancellationToken); |
| 133 | + |
| 134 | + // Assert |
| 135 | + Assert.NotNull(prompts); |
| 136 | + // Microsoft Learn server may or may not have prompts, so we don't assert NotEmpty |
| 137 | + } |
| 138 | + |
| 139 | + [Fact] |
| 140 | + [Trait("Execution", "Manual")] |
| 141 | + public async Task PingServer_MicrosoftLearnServer() |
| 142 | + { |
| 143 | + // Arrange |
| 144 | + var transportOptions = new HttpClientTransportOptions |
| 145 | + { |
| 146 | + Endpoint = new Uri(MicrosoftLearnMcpEndpoint), |
| 147 | + Name = "Microsoft Learn MCP Server", |
| 148 | + TransportMode = HttpTransportMode.StreamableHttp, |
| 149 | + }; |
| 150 | + |
| 151 | + var clientOptions = new McpClientOptions |
| 152 | + { |
| 153 | + ClientInfo = new() { Name = "CSharpSdkIntegrationTest", Version = "1.0.0" } |
| 154 | + }; |
| 155 | + |
| 156 | + // Act |
| 157 | + await using var client = await McpClient.CreateAsync( |
| 158 | + new HttpClientTransport(transportOptions), |
| 159 | + clientOptions, |
| 160 | + loggerFactory: LoggerFactory, |
| 161 | + cancellationToken: TestContext.Current.CancellationToken); |
| 162 | + |
| 163 | + await client.PingAsync(TestContext.Current.CancellationToken); |
| 164 | + |
| 165 | + // Assert - if we get here without exception, ping was successful |
| 166 | + Assert.True(true); |
| 167 | + } |
| 168 | +} |
0 commit comments