Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions tests/ModelContextProtocol.Tests/MicrosoftLearnMcpServerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using ModelContextProtocol.Client;
using ModelContextProtocol.Protocol;
using ModelContextProtocol.Tests.Utils;

namespace ModelContextProtocol.Tests;

/// <summary>
/// Integration tests for connecting to the Microsoft Learn MCP server.
/// These tests connect to a live external service and are marked as Manual execution.
/// </summary>
public class MicrosoftLearnMcpServerTests(ITestOutputHelper testOutputHelper) : LoggedTest(testOutputHelper)
{
private const string MicrosoftLearnMcpEndpoint = "https://learn.microsoft.com/api/mcp";

private Task<McpClient> 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);
}
}
40 changes: 40 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -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)