Log tool call name on success, not just failure (#1256)

This commit is contained in:
Copilot 2026-02-13 09:24:02 -08:00 • committed by GitHub
commit 53e9ed8f65
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -127,7 +127,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.Equal(16, tools.Count);
Assert.Equal(19, tools.Count);
McpClientTool echoTool = tools.First(t => t.Name == "echo");
Assert.Equal("Echoes the input back to the client.", echoTool.Description);
@ -165,7 +165,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
cancellationToken: TestContext.Current.CancellationToken))
{
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.Equal(16, tools.Count);
Assert.Equal(19, tools.Count);
McpClientTool echoTool = tools.First(t => t.Name == "echo");
Assert.Equal("Echoes the input back to the client.", echoTool.Description);
@ -191,7 +191,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
await using McpClient client = await CreateMcpClientForServer();
var tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.Equal(16, tools.Count);
Assert.Equal(19, tools.Count);
Channel<JsonRpcNotification> listChanged = Channel.CreateUnbounded<JsonRpcNotification>();
var notificationRead = listChanged.Reader.ReadAsync(TestContext.Current.CancellationToken);
@ -212,7 +212,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
await notificationRead;
tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.Equal(17, tools.Count);
Assert.Equal(20, tools.Count);
Assert.Contains(tools, t => t.Name == "NewTool");
notificationRead = listChanged.Reader.ReadAsync(TestContext.Current.CancellationToken);
@ -222,7 +222,7 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
}
tools = await client.ListToolsAsync(cancellationToken: TestContext.Current.CancellationToken);
Assert.Equal(16, tools.Count);
Assert.Equal(19, tools.Count);
Assert.DoesNotContain(tools, t => t.Name == "NewTool");
}
@ -380,6 +380,78 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
Assert.Equal("Test error", errorLog.Exception.Message);
}
[Fact]
public async Task Logs_Tool_Name_On_Successful_Call()
{
await using McpClient client = await CreateMcpClientForServer();
var result = await client.CallToolAsync(
"echo",
new Dictionary<string, object?> { ["message"] = "test" },
cancellationToken: TestContext.Current.CancellationToken);
Assert.True(result.IsError is not true);
Assert.Equal("hello test", (result.Content[0] as TextContentBlock)?.Text);
var infoLog = Assert.Single(MockLoggerProvider.LogMessages, m => m.Message == "\"echo\" completed. IsError = False.");
Assert.Equal(LogLevel.Information, infoLog.LogLevel);
}
[Fact]
public async Task Logs_Tool_Name_With_IsError_When_Tool_Returns_Error()
{
await using McpClient client = await CreateMcpClientForServer();
var result = await client.CallToolAsync(
"return_is_error",
cancellationToken: TestContext.Current.CancellationToken);
Assert.True(result.IsError);
Assert.Contains("Tool returned an error", (result.Content[0] as TextContentBlock)?.Text);
var infoLog = Assert.Single(MockLoggerProvider.LogMessages, m => m.Message == "\"return_is_error\" completed. IsError = True.");
Assert.Equal(LogLevel.Information, infoLog.LogLevel);
}
[Fact]
public async Task Logs_Tool_Error_When_Tool_Throws_OperationCanceledException()
{
await using McpClient client = await CreateMcpClientForServer();
var result = await client.CallToolAsync(
"throw_operation_canceled_exception",
cancellationToken: TestContext.Current.CancellationToken);
Assert.True(result.IsError);
Assert.NotNull(result.Content);
Assert.NotEmpty(result.Content);
Assert.Contains("An error occurred", (result.Content[0] as TextContentBlock)?.Text);
Assert.Contains(MockLoggerProvider.LogMessages, m =>
m.LogLevel == LogLevel.Error &&
m.Message == "\"throw_operation_canceled_exception\" threw an unhandled exception." &&
m.Exception is OperationCanceledException);
}
[Fact]
public async Task Logs_Tool_Error_When_Tool_Throws_McpProtocolException()
{
await using McpClient client = await CreateMcpClientForServer();
await Assert.ThrowsAsync<McpProtocolException>(async () => await client.CallToolAsync(
"throw_mcp_protocol_exception",
cancellationToken: TestContext.Current.CancellationToken));
Assert.Contains(MockLoggerProvider.LogMessages, m =>
m.LogLevel == LogLevel.Error &&
m.Message == "\"throw_mcp_protocol_exception\" threw an unhandled exception." &&
m.Exception is McpProtocolException);
Assert.Contains(MockLoggerProvider.LogMessages, m =>
m.LogLevel == LogLevel.Warning &&
m.Message.Contains("request handler failed"));
}
[Fact]
public async Task Throws_Exception_On_Unknown_Tool()
{
@ -786,6 +858,28 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
throw new InvalidOperationException("Test error");
}
[McpServerTool]
public static string ThrowOperationCanceledException()
{
throw new OperationCanceledException("Tool was canceled");
}
[McpServerTool]
public static string ThrowMcpProtocolException()
{
throw new McpProtocolException("Tool protocol error", McpErrorCode.InvalidParams);
}
[McpServerTool]
public static CallToolResult ReturnIsError()
{
return new CallToolResult
{
IsError = true,
Content = [new TextContentBlock { Text = "Tool returned an error" }],
};
}
[McpServerTool]
public static int ReturnCancellationToken(CancellationToken cancellationToken)
{
@ -868,5 +962,6 @@ public partial class McpServerBuilderExtensionsToolsTests : ClientServerTestBase
[JsonSerializable(typeof(ComplexObject))]
[JsonSerializable(typeof(string[]))]
[JsonSerializable(typeof(JsonElement))]
[JsonSerializable(typeof(CallToolResult))]
partial class BuilderToolsJsonContext : JsonSerializerContext;
}