diff --git a/tests/ModelContextProtocol.Tests/MicrosoftLearnMcpServerTests.cs b/tests/ModelContextProtocol.Tests/MicrosoftLearnMcpServerTests.cs new file mode 100644 index 00000000..84562170 --- /dev/null +++ b/tests/ModelContextProtocol.Tests/MicrosoftLearnMcpServerTests.cs @@ -0,0 +1,100 @@ +using ModelContextProtocol.Client; +using ModelContextProtocol.Protocol; +using ModelContextProtocol.Tests.Utils; + +namespace ModelContextProtocol.Tests; + +/// +/// Integration tests for connecting to the Microsoft Learn MCP server. +/// These tests connect to a live external service and are marked as Manual execution. +/// +public class MicrosoftLearnMcpServerTests(ITestOutputHelper testOutputHelper) : LoggedTest(testOutputHelper) +{ + private const string MicrosoftLearnMcpEndpoint = "https://learn.microsoft.com/api/mcp"; + + private Task CreateClientAsync(CancellationToken cancellationToken = default) + { + var transportOptions = new HttpClientTransportOptions + { + Endpoint = new Uri(MicrosoftLearnMcpEndpoint), + Name = "Microsoft Learn MCP Server", + TransportMode = HttpTransportMode.StreamableHttp, + }; + + var clientOptions = new McpClientOptions + { + ClientInfo = new() { Name = "CSharpSdkIntegrationTest", Version = "1.0.0" } + }; + + return McpClient.CreateAsync( + new HttpClientTransport(transportOptions), + clientOptions, + loggerFactory: LoggerFactory, + cancellationToken: cancellationToken); + } + + [Fact] + [Trait("Execution", "Manual")] + public async Task ConnectAndInitialize_MicrosoftLearnServer_WithStreamableHttp() + { + // Act + await using var client = await CreateClientAsync(TestContext.Current.CancellationToken); + + // Assert + Assert.NotNull(client); + Assert.NotNull(client.ServerCapabilities); + Assert.NotNull(client.ServerInfo); + Assert.NotNull(client.NegotiatedProtocolVersion); + } + + [Fact] + [Trait("Execution", "Manual")] + public async Task ListTools_MicrosoftLearnServer() + { + // Act + await using var client = await CreateClientAsync(TestContext.Current.CancellationToken); + var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken); + + // Assert + Assert.NotNull(tools); + Assert.NotEmpty(tools); + } + + [Fact] + [Trait("Execution", "Manual")] + public async Task ListResources_MicrosoftLearnServer() + { + // Act + await using var client = await CreateClientAsync(TestContext.Current.CancellationToken); + var resources = await client.ListResourcesAsync(TestContext.Current.CancellationToken); + + // Assert + Assert.NotNull(resources); + // Microsoft Learn server may or may not have resources, so we don't assert NotEmpty + } + + [Fact] + [Trait("Execution", "Manual")] + public async Task ListPrompts_MicrosoftLearnServer() + { + // Act + await using var client = await CreateClientAsync(TestContext.Current.CancellationToken); + var prompts = await client.ListPromptsAsync(TestContext.Current.CancellationToken); + + // Assert + Assert.NotNull(prompts); + // Microsoft Learn server may or may not have prompts, so we don't assert NotEmpty + } + + [Fact] + [Trait("Execution", "Manual")] + public async Task PingServer_MicrosoftLearnServer() + { + // Act + await using var client = await CreateClientAsync(TestContext.Current.CancellationToken); + await client.PingAsync(TestContext.Current.CancellationToken); + + // Assert - if we get here without exception, ping was successful + Assert.True(true); + } +} diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 00000000..de511950 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,40 @@ +# Running Tests + +## Manual Tests + +Some tests in this repository are marked with the `[Trait("Execution", "Manual")]` attribute. These tests require external dependencies or network connectivity and are not run by default in CI/CD pipelines. + +### Microsoft Learn MCP Server Tests + +The `MicrosoftLearnMcpServerTests` class contains integration tests that connect to the Microsoft Learn MCP server at `https://learn.microsoft.com/api/mcp` using Streamable HTTP transport. + +These tests: +- Require network connectivity to Microsoft Learn services +- Test real-world integration with a production MCP server +- Validate the Streamable HTTP transport implementation + +#### Running the Microsoft Learn tests + +To run these tests, use the following command: + +```bash +dotnet test --filter "(FullyQualifiedName~MicrosoftLearnMcpServerTests)" +``` + +Or to run all manual tests: + +```bash +dotnet test --filter "(Execution=Manual)" +``` + +To exclude manual tests from a test run: + +```bash +dotnet test --filter "(Execution!=Manual)" +``` + +### Requirements + +- .NET 10.0 SDK (as specified in `global.json`) +- Internet connectivity +- Access to https://learn.microsoft.com/api/mcp (ensure no firewall restrictions)