From ca6d0f0918f51ca18a007e11fcc9af744eee06da Mon Sep 17 00:00:00 2001 From: Jon Gallant <2163001+jongio@users.noreply.github.com> Date: Tue, 23 Sep 2025 12:02:03 -0700 Subject: [PATCH 1/2] Add test for handling invalid tool requests in MockClientTests --- .../Client/MockClientTests.cs | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Client/MockClientTests.cs b/core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Client/MockClientTests.cs index 007d16138..0040d64e8 100644 --- a/core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Client/MockClientTests.cs +++ b/core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Client/MockClientTests.cs @@ -180,6 +180,45 @@ await Invoke_Request_To_Server( }); } + [Fact] + public async Task Invoke_Invalid_Tool_Returns_Error() + { + await Invoke_Request_To_Server( + method: "tools/call", + new ServerCapabilities + { + Tools = new() + { + CallToolHandler = (request, ct) => + { + // Simulate the behavior when an invalid tool is called + return ValueTask.FromResult(new CallToolResult + { + Content = [new TextContentBlock { Text = $"The tool {request.Params?.Name} was not found" }], + IsError = true + }); + }, + ListToolsHandler = (request, ct) => throw new NotImplementedException(), + } + }, + requestParams: JsonSerializer.SerializeToNode(new + { + name = "non_existent_tool", + arguments = new { } + }), + configureOptions: null, + assertResult: response => + { + var result = JsonSerializer.Deserialize(response); + Assert.NotNull(result); + Assert.True(result.IsError, "Expected error response for non-existent tool"); + Assert.NotEmpty(result.Content); + + var textContent = Assert.IsType(result.Content[0]); + Assert.Contains("The tool non_existent_tool was not found", textContent.Text); + }); + } + private async Task Invoke_Request_To_Server(string method, ServerCapabilities? serverCapabilities, Action? configureOptions, Action assertResult) { From 1a52cec5f064b7386e9e5661f76045e2bb3f8be1 Mon Sep 17 00:00:00 2001 From: Jon Gallant <2163001+jongio@users.noreply.github.com> Date: Tue, 23 Sep 2025 12:03:02 -0700 Subject: [PATCH 2/2] Fix assertion in Invoke_Invalid_Tool_Returns_Error test to ensure proper error handling for non-existent tools --- .../tests/Azure.Mcp.Core.UnitTests/Client/MockClientTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Client/MockClientTests.cs b/core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Client/MockClientTests.cs index 0040d64e8..555fac706 100644 --- a/core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Client/MockClientTests.cs +++ b/core/Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Client/MockClientTests.cs @@ -213,7 +213,7 @@ await Invoke_Request_To_Server( Assert.NotNull(result); Assert.True(result.IsError, "Expected error response for non-existent tool"); Assert.NotEmpty(result.Content); - + var textContent = Assert.IsType(result.Content[0]); Assert.Contains("The tool non_existent_tool was not found", textContent.Text); });