Skip to content

Commit 171a786

Browse files
Copilotstephentoub
andcommitted
Refactor Microsoft Learn MCP server tests to reduce duplication
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent 69723c3 commit 171a786

File tree

1 file changed

+15
-83
lines changed

1 file changed

+15
-83
lines changed

tests/ModelContextProtocol.Tests/MicrosoftLearnMcpServerTests.cs

Lines changed: 15 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ public class MicrosoftLearnMcpServerTests(ITestOutputHelper testOutputHelper) :
1212
{
1313
private const string MicrosoftLearnMcpEndpoint = "https://learn.microsoft.com/api/mcp";
1414

15-
[Fact]
16-
[Trait("Execution", "Manual")]
17-
public async Task ConnectAndInitialize_MicrosoftLearnServer_WithStreamableHttp()
15+
private Task<McpClient> CreateClientAsync(CancellationToken cancellationToken = default)
1816
{
19-
// Arrange
2017
var transportOptions = new HttpClientTransportOptions
2118
{
2219
Endpoint = new Uri(MicrosoftLearnMcpEndpoint),
@@ -29,12 +26,19 @@ public async Task ConnectAndInitialize_MicrosoftLearnServer_WithStreamableHttp()
2926
ClientInfo = new() { Name = "CSharpSdkIntegrationTest", Version = "1.0.0" }
3027
};
3128

32-
// Act
33-
await using var client = await McpClient.CreateAsync(
29+
return McpClient.CreateAsync(
3430
new HttpClientTransport(transportOptions),
3531
clientOptions,
3632
loggerFactory: LoggerFactory,
37-
cancellationToken: TestContext.Current.CancellationToken);
33+
cancellationToken: cancellationToken);
34+
}
35+
36+
[Fact]
37+
[Trait("Execution", "Manual")]
38+
public async Task ConnectAndInitialize_MicrosoftLearnServer_WithStreamableHttp()
39+
{
40+
// Act
41+
await using var client = await CreateClientAsync(TestContext.Current.CancellationToken);
3842

3943
// Assert
4044
Assert.NotNull(client);
@@ -47,26 +51,8 @@ public async Task ConnectAndInitialize_MicrosoftLearnServer_WithStreamableHttp()
4751
[Trait("Execution", "Manual")]
4852
public async Task ListTools_MicrosoftLearnServer()
4953
{
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-
6354
// Act
64-
await using var client = await McpClient.CreateAsync(
65-
new HttpClientTransport(transportOptions),
66-
clientOptions,
67-
loggerFactory: LoggerFactory,
68-
cancellationToken: TestContext.Current.CancellationToken);
69-
55+
await using var client = await CreateClientAsync(TestContext.Current.CancellationToken);
7056
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
7157

7258
// Assert
@@ -78,26 +64,8 @@ public async Task ListTools_MicrosoftLearnServer()
7864
[Trait("Execution", "Manual")]
7965
public async Task ListResources_MicrosoftLearnServer()
8066
{
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-
9467
// Act
95-
await using var client = await McpClient.CreateAsync(
96-
new HttpClientTransport(transportOptions),
97-
clientOptions,
98-
loggerFactory: LoggerFactory,
99-
cancellationToken: TestContext.Current.CancellationToken);
100-
68+
await using var client = await CreateClientAsync(TestContext.Current.CancellationToken);
10169
var resources = await client.ListResourcesAsync(TestContext.Current.CancellationToken);
10270

10371
// Assert
@@ -109,26 +77,8 @@ public async Task ListResources_MicrosoftLearnServer()
10977
[Trait("Execution", "Manual")]
11078
public async Task ListPrompts_MicrosoftLearnServer()
11179
{
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-
12580
// Act
126-
await using var client = await McpClient.CreateAsync(
127-
new HttpClientTransport(transportOptions),
128-
clientOptions,
129-
loggerFactory: LoggerFactory,
130-
cancellationToken: TestContext.Current.CancellationToken);
131-
81+
await using var client = await CreateClientAsync(TestContext.Current.CancellationToken);
13282
var prompts = await client.ListPromptsAsync(TestContext.Current.CancellationToken);
13383

13484
// Assert
@@ -140,26 +90,8 @@ public async Task ListPrompts_MicrosoftLearnServer()
14090
[Trait("Execution", "Manual")]
14191
public async Task PingServer_MicrosoftLearnServer()
14292
{
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-
15693
// Act
157-
await using var client = await McpClient.CreateAsync(
158-
new HttpClientTransport(transportOptions),
159-
clientOptions,
160-
loggerFactory: LoggerFactory,
161-
cancellationToken: TestContext.Current.CancellationToken);
162-
94+
await using var client = await CreateClientAsync(TestContext.Current.CancellationToken);
16395
await client.PingAsync(TestContext.Current.CancellationToken);
16496

16597
// Assert - if we get here without exception, ping was successful

0 commit comments

Comments
 (0)