forked from modelcontextprotocol/csharp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapMcpStreamableHttpTests.cs
More file actions
146 lines (117 loc) · 4.33 KB
/
MapMcpStreamableHttpTests.cs
File metadata and controls
146 lines (117 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using ModelContextProtocol.Client;
namespace ModelContextProtocol.AspNetCore.Tests;
public class MapMcpStreamableHttpTests(ITestOutputHelper outputHelper) : MapMcpTests(outputHelper)
{
protected override bool UseStreamableHttp => true;
protected override bool Stateless => false;
[Theory]
[InlineData("/a", "/a")]
[InlineData("/a", "/a/")]
[InlineData("/a/", "/a/")]
[InlineData("/a/", "/a")]
[InlineData("/a/b", "/a/b")]
public async Task CanConnect_WithMcpClient_AfterCustomizingRoute(string routePattern, string requestPath)
{
Builder.Services.AddMcpServer(options =>
{
options.ServerInfo = new()
{
Name = "TestCustomRouteServer",
Version = "1.0.0",
};
}).WithHttpTransport(ConfigureStateless);
await using var app = Builder.Build();
app.MapMcp(routePattern);
await app.StartAsync(TestContext.Current.CancellationToken);
await using var mcpClient = await ConnectAsync(requestPath);
Assert.Equal("TestCustomRouteServer", mcpClient.ServerInfo.Name);
}
[Fact]
public async Task StreamableHttpMode_Works_WithRootEndpoint()
{
Builder.Services.AddMcpServer(options =>
{
options.ServerInfo = new()
{
Name = "StreamableHttpTestServer",
Version = "1.0.0",
};
}).WithHttpTransport(ConfigureStateless);
await using var app = Builder.Build();
app.MapMcp();
await app.StartAsync(TestContext.Current.CancellationToken);
await using var mcpClient = await ConnectAsync("/", new()
{
Endpoint = new Uri("http://localhost/"),
TransportMode = HttpTransportMode.AutoDetect
});
Assert.Equal("StreamableHttpTestServer", mcpClient.ServerInfo.Name);
}
[Fact]
public async Task AutoDetectMode_Works_WithRootEndpoint()
{
Builder.Services.AddMcpServer(options =>
{
options.ServerInfo = new()
{
Name = "AutoDetectTestServer",
Version = "1.0.0",
};
}).WithHttpTransport(ConfigureStateless);
await using var app = Builder.Build();
app.MapMcp();
await app.StartAsync(TestContext.Current.CancellationToken);
await using var mcpClient = await ConnectAsync("/", new()
{
Endpoint = new Uri("http://localhost/"),
TransportMode = HttpTransportMode.AutoDetect
});
Assert.Equal("AutoDetectTestServer", mcpClient.ServerInfo.Name);
}
[Fact]
public async Task AutoDetectMode_Works_WithSseEndpoint()
{
Assert.SkipWhen(Stateless, "SSE endpoint is disabled in stateless mode.");
Builder.Services.AddMcpServer(options =>
{
options.ServerInfo = new()
{
Name = "AutoDetectSseTestServer",
Version = "1.0.0",
};
}).WithHttpTransport(ConfigureStateless);
await using var app = Builder.Build();
app.MapMcp();
await app.StartAsync(TestContext.Current.CancellationToken);
await using var mcpClient = await ConnectAsync("/sse", new()
{
Endpoint = new Uri("http://localhost/sse"),
TransportMode = HttpTransportMode.AutoDetect
});
Assert.Equal("AutoDetectSseTestServer", mcpClient.ServerInfo.Name);
}
[Fact]
public async Task SseMode_Works_WithSseEndpoint()
{
Assert.SkipWhen(Stateless, "SSE endpoint is disabled in stateless mode.");
Builder.Services.AddMcpServer(options =>
{
options.ServerInfo = new()
{
Name = "SseTestServer",
Version = "1.0.0",
};
}).WithHttpTransport(ConfigureStateless);
await using var app = Builder.Build();
app.MapMcp();
await app.StartAsync(TestContext.Current.CancellationToken);
await using var mcpClient = await ConnectAsync(transportOptions: new()
{
Endpoint = new Uri("http://localhost/sse"),
TransportMode = HttpTransportMode.Sse
});
Assert.Equal("SseTestServer", mcpClient.ServerInfo.Name);
}
}