Add more tests (#51)

* Add more tests

* Fix some code smells

* Remove more code smells

* Combine "ServerIntegrationTests" and "ClientIntegrationTests" because both are client integration tests just using different servers

* Clean up resources after test

* Improve kill logic for spawned child processes

* Improved testability for Stdio

* Fix SseIntegration tests
This commit is contained in:
Toni Wenzel 2025-03-12 18:55:52 +01:00 • committed by GitHub
commit b5076263d1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -51,6 +51,7 @@ internal static class Program
ConfigureTools(server);
ConfigureResources(server);
ConfigurePrompts(server);
ConfigureCompletion(server);
Log.Logger.Information("Server initialized.");
@ -325,6 +326,43 @@ internal static class Program
};
}
private static void ConfigureCompletion(IMcpServer server)
{
List<string> sampleResourceIds = ["1", "2", "3", "4", "5"];
Dictionary<string, List<string>> exampleCompletions = new()
{
{"style", ["casual", "formal", "technical", "friendly"]},
{"temperature", ["0", "0.5", "0.7", "1.0"]},
};
server.GetCompletionHandler = (request, cancellationToken) =>
{
if (request.Params?.Ref?.Type == "ref/resource")
{
var resourceId = request.Params?.Ref?.Uri?.Split('/').LastOrDefault();
if (string.IsNullOrEmpty(resourceId))
return Task.FromResult(new CompleteResult() { Completion = new() { Values = [] } });
// Filter resource IDs that start with the input value
var values = sampleResourceIds.Where(id => id.StartsWith(request.Params.Argument.Value)).ToArray();
return Task.FromResult(new CompleteResult() { Completion = new() { Values = values, HasMore = false, Total = values.Length } });
}
if (request.Params?.Ref?.Type == "ref/prompt")
{
// Handle completion for prompt arguments
if (!exampleCompletions.TryGetValue(request.Params.Argument.Name, out var completions))
return Task.FromResult(new CompleteResult() { Completion = new() { Values = [] } });
var values = completions.Where(value => value.StartsWith(request.Params.Argument.Value)).ToArray();
return Task.FromResult(new CompleteResult() { Completion = new() { Values = values, HasMore = false, Total = values.Length } });
}
throw new McpServerException($"Unknown reference type: {request.Params?.Ref.Type}");
};
}
static CreateMessageRequestParams CreateRequestSamplingParams(string context, string uri, int maxTokens = 100)
{
return new CreateMessageRequestParams()