-
Notifications
You must be signed in to change notification settings - Fork 471
Use Kestrel for all in-memory HTTP tests #225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
halter73
merged 12 commits into
modelcontextprotocol:main
from
halter73:kestrel-in-memory
Apr 7, 2025
Merged
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e0318dd
Add route pattern parameter to MapMcp
halter73 31593c1
Add configureOptionsAsync
halter73 f098676
Use Kestrel for all in-memory HTTP tests
halter73 62587fd
Use ApplicationStopping token for SSE responses
halter73 339db54
Avoid listening with both socket and in-memory transport
halter73 7afc4bd
Merge remote-tracking branch 'origin/main' into kestrel-in-memory
halter73 575c2bc
Use RegisterNotificationHandler in MapMcp tests
halter73 b19ab58
Work around SocketsHttpHandler bug where it doesn't call DispseAsync …
halter73 e5f152d
Remove bogus assert
halter73 a9a4b8e
Move more lines into try
halter73 467a086
Fix race in ConnectAndReceiveNotification_InMemoryServer test
halter73 4072013
Add the rest of the changes that were supposed to be in the last commit
halter73 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
tests/ModelContextProtocol.Tests/DockerEverythingServerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
using ModelContextProtocol.Client; | ||
using ModelContextProtocol.Protocol.Transport; | ||
using ModelContextProtocol.Protocol.Types; | ||
using ModelContextProtocol.Tests.Utils; | ||
|
||
namespace ModelContextProtocol.Tests; | ||
|
||
public class DockerEverythingServerTests(ITestOutputHelper testOutputHelper) : LoggedTest(testOutputHelper) | ||
{ | ||
/// <summary>Port number to be grabbed by the next test.</summary> | ||
private static int s_nextPort = 3000; | ||
|
||
// If the tests run concurrently against different versions of the runtime, tests can conflict with | ||
// each other in the ports set up for interacting with containers. Ensure that such suites running | ||
// against different TFMs use different port numbers. | ||
private static readonly int s_portOffset = 1000 * (Environment.Version.Major switch | ||
{ | ||
int v when v >= 8 => Environment.Version.Major - 7, | ||
_ => 0, | ||
}); | ||
|
||
private static int CreatePortNumber() => Interlocked.Increment(ref s_nextPort) + s_portOffset; | ||
|
||
public static bool IsDockerAvailable => EverythingSseServerFixture.IsDockerAvailable; | ||
|
||
[Fact(Skip = "docker is not available", SkipUnless = nameof(IsDockerAvailable))] | ||
[Trait("Execution", "Manual")] | ||
public async Task ConnectAndReceiveMessage_EverythingServerWithSse() | ||
{ | ||
int port = CreatePortNumber(); | ||
|
||
await using var fixture = new EverythingSseServerFixture(port); | ||
await fixture.StartAsync(); | ||
|
||
var defaultOptions = new McpClientOptions | ||
{ | ||
ClientInfo = new() { Name = "IntegrationTestClient", Version = "1.0.0" } | ||
}; | ||
|
||
var defaultConfig = new McpServerConfig | ||
{ | ||
Id = "everything", | ||
Name = "Everything", | ||
TransportType = TransportTypes.Sse, | ||
TransportOptions = [], | ||
Location = $"http://localhost:{port}/sse" | ||
}; | ||
|
||
// Create client and run tests | ||
await using var client = await McpClientFactory.CreateAsync( | ||
defaultConfig, | ||
defaultOptions, | ||
loggerFactory: LoggerFactory, | ||
cancellationToken: TestContext.Current.CancellationToken); | ||
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken); | ||
|
||
// assert | ||
Assert.NotEmpty(tools); | ||
} | ||
|
||
[Fact(Skip = "docker is not available", SkipUnless = nameof(IsDockerAvailable))] | ||
[Trait("Execution", "Manual")] | ||
public async Task Sampling_Sse_EverythingServer() | ||
{ | ||
int port = CreatePortNumber(); | ||
|
||
await using var fixture = new EverythingSseServerFixture(port); | ||
await fixture.StartAsync(); | ||
|
||
var defaultConfig = new McpServerConfig | ||
{ | ||
Id = "everything", | ||
Name = "Everything", | ||
TransportType = TransportTypes.Sse, | ||
TransportOptions = [], | ||
Location = $"http://localhost:{port}/sse" | ||
}; | ||
|
||
int samplingHandlerCalls = 0; | ||
var defaultOptions = new McpClientOptions | ||
{ | ||
Capabilities = new() | ||
{ | ||
Sampling = new() | ||
{ | ||
SamplingHandler = (_, _, _) => | ||
{ | ||
samplingHandlerCalls++; | ||
return Task.FromResult(new CreateMessageResult | ||
{ | ||
Model = "test-model", | ||
Role = "assistant", | ||
Content = new Content | ||
{ | ||
Type = "text", | ||
Text = "Test response" | ||
} | ||
}); | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
await using var client = await McpClientFactory.CreateAsync( | ||
defaultConfig, | ||
defaultOptions, | ||
loggerFactory: LoggerFactory, | ||
cancellationToken: TestContext.Current.CancellationToken); | ||
|
||
// Call the server's sampleLLM tool which should trigger our sampling handler | ||
var result = await client.CallToolAsync("sampleLLM", new Dictionary<string, object?> | ||
{ | ||
["prompt"] = "Test prompt", | ||
["maxTokens"] = 100 | ||
}, cancellationToken: TestContext.Current.CancellationToken); | ||
|
||
// assert | ||
Assert.NotNull(result); | ||
var textContent = Assert.Single(result.Content); | ||
Assert.Equal("text", textContent.Type); | ||
Assert.False(string.IsNullOrEmpty(textContent.Text)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using ModelContextProtocol.Tests.Utils; | ||
|
||
namespace ModelContextProtocol.Tests.Server; | ||
|
||
public class MapMcpTests(ITestOutputHelper testOutputHelper) : KestrelInMemoryTest(testOutputHelper) | ||
{ | ||
[Fact] | ||
public async Task Allows_Customizing_Route() | ||
{ | ||
await using var app = Builder.Build(); | ||
app.MapMcp("/mcp"); | ||
await app.StartAsync(TestContext.Current.CancellationToken); | ||
|
||
using var httpClient = CreateHttpClient(); | ||
using var response = await httpClient.GetAsync("http://localhost/mcp/sse", HttpCompletionOption.ResponseHeadersRead, TestContext.Current.CancellationToken); | ||
response.EnsureSuccessStatusCode(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.