From 428a1d8506a6c3df0003c8457d6e83f43c1f81c1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 22 Jan 2026 17:56:58 +0000 Subject: [PATCH 1/6] Initial plan From c624b15696e5520e270f6c13aa3507dd9a8a02c7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 22 Jan 2026 18:01:38 +0000 Subject: [PATCH 2/6] Add unit tests to improve coverage for Microsoft.Agents.AI.Anthropic Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com> --- .../AnthropicBetaServiceExtensionsTests.cs | 124 ++++++++++++++++++ .../AnthropicClientExtensionsTests.cs | 124 ++++++++++++++++++ 2 files changed, 248 insertions(+) diff --git a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs index 91d2cb988a..532304e54a 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs @@ -188,6 +188,130 @@ public void CreateAIAgent_WithNullOptions_ThrowsArgumentNullException() Assert.Equal("options", exception.ParamName); } + /// + /// Verify that CreateAIAgent with tools correctly assigns tools to ChatOptions. + /// + [Fact] + public void CreateAIAgent_WithTools_AssignsToolsCorrectly() + { + // Arrange + var chatClient = new TestAnthropicChatClient(); + IList tools = [AIFunctionFactory.Create(() => "test result", "TestFunction", "A test function")]; + + // Act + var agent = chatClient.Beta.AsAIAgent( + model: "test-model", + name: "Test Agent", + tools: tools); + + // Assert + Assert.NotNull(agent); + Assert.Equal("Test Agent", agent.Name); + } + + /// + /// Verify that CreateAIAgent with explicit defaultMaxTokens uses the provided value. + /// + [Fact] + public void CreateAIAgent_WithExplicitMaxTokens_UsesProvidedValue() + { + // Arrange + var chatClient = new TestAnthropicChatClient(); + + // Act + var agent = chatClient.Beta.AsAIAgent( + model: "test-model", + name: "Test Agent", + defaultMaxTokens: 8192); + + // Assert + Assert.NotNull(agent); + Assert.Equal("Test Agent", agent.Name); + } + + /// + /// Verify that CreateAIAgent with tools and instructions correctly assigns both. + /// + [Fact] + public void CreateAIAgent_WithToolsAndInstructions_AssignsBothCorrectly() + { + // Arrange + var chatClient = new TestAnthropicChatClient(); + IList tools = [AIFunctionFactory.Create(() => "test result", "TestFunction", "A test function")]; + + // Act + var agent = chatClient.Beta.AsAIAgent( + model: "test-model", + name: "Test Agent", + instructions: "Test instructions", + tools: tools); + + // Assert + Assert.NotNull(agent); + Assert.Equal("Test Agent", agent.Name); + } + + /// + /// Verify that CreateAIAgent with empty tools list does not assign tools. + /// + [Fact] + public void CreateAIAgent_WithEmptyTools_DoesNotAssignTools() + { + // Arrange + var chatClient = new TestAnthropicChatClient(); + IList tools = []; + + // Act + var agent = chatClient.Beta.AsAIAgent( + model: "test-model", + name: "Test Agent", + tools: tools); + + // Assert + Assert.NotNull(agent); + Assert.Equal("Test Agent", agent.Name); + } + + /// + /// Verify that CreateAIAgent with null instructions does not set instructions. + /// + [Fact] + public void CreateAIAgent_WithNullInstructions_DoesNotSetInstructions() + { + // Arrange + var chatClient = new TestAnthropicChatClient(); + + // Act + var agent = chatClient.Beta.AsAIAgent( + model: "test-model", + name: "Test Agent", + instructions: null); + + // Assert + Assert.NotNull(agent); + Assert.Equal("Test Agent", agent.Name); + } + + /// + /// Verify that CreateAIAgent with whitespace instructions does not set instructions. + /// + [Fact] + public void CreateAIAgent_WithWhitespaceInstructions_DoesNotSetInstructions() + { + // Arrange + var chatClient = new TestAnthropicChatClient(); + + // Act + var agent = chatClient.Beta.AsAIAgent( + model: "test-model", + name: "Test Agent", + instructions: " "); + + // Assert + Assert.NotNull(agent); + Assert.Equal("Test Agent", agent.Name); + } + /// /// Test custom chat client that can be used to verify clientFactory functionality. /// diff --git a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs index 90f20d15c3..100bec6295 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs @@ -254,4 +254,128 @@ public void CreateAIAgent_WithNullOptions_ThrowsArgumentNullException() Assert.Equal("options", exception.ParamName); } + + /// + /// Verify that CreateAIAgent with tools correctly assigns tools to ChatOptions. + /// + [Fact] + public void CreateAIAgent_WithTools_AssignsToolsCorrectly() + { + // Arrange + var chatClient = new TestAnthropicChatClient(); + IList tools = [AIFunctionFactory.Create(() => "test result", "TestFunction", "A test function")]; + + // Act + var agent = chatClient.AsAIAgent( + model: "test-model", + name: "Test Agent", + tools: tools); + + // Assert + Assert.NotNull(agent); + Assert.Equal("Test Agent", agent.Name); + } + + /// + /// Verify that CreateAIAgent with explicit defaultMaxTokens uses the provided value. + /// + [Fact] + public void CreateAIAgent_WithExplicitMaxTokens_UsesProvidedValue() + { + // Arrange + var chatClient = new TestAnthropicChatClient(); + + // Act + var agent = chatClient.AsAIAgent( + model: "test-model", + name: "Test Agent", + defaultMaxTokens: 8192); + + // Assert + Assert.NotNull(agent); + Assert.Equal("Test Agent", agent.Name); + } + + /// + /// Verify that CreateAIAgent with tools and instructions correctly assigns both. + /// + [Fact] + public void CreateAIAgent_WithToolsAndInstructions_AssignsBothCorrectly() + { + // Arrange + var chatClient = new TestAnthropicChatClient(); + IList tools = [AIFunctionFactory.Create(() => "test result", "TestFunction", "A test function")]; + + // Act + var agent = chatClient.AsAIAgent( + model: "test-model", + name: "Test Agent", + instructions: "Test instructions", + tools: tools); + + // Assert + Assert.NotNull(agent); + Assert.Equal("Test Agent", agent.Name); + } + + /// + /// Verify that CreateAIAgent with empty tools list does not assign tools. + /// + [Fact] + public void CreateAIAgent_WithEmptyTools_DoesNotAssignTools() + { + // Arrange + var chatClient = new TestAnthropicChatClient(); + IList tools = []; + + // Act + var agent = chatClient.AsAIAgent( + model: "test-model", + name: "Test Agent", + tools: tools); + + // Assert + Assert.NotNull(agent); + Assert.Equal("Test Agent", agent.Name); + } + + /// + /// Verify that CreateAIAgent with null instructions does not set instructions. + /// + [Fact] + public void CreateAIAgent_WithNullInstructions_DoesNotSetInstructions() + { + // Arrange + var chatClient = new TestAnthropicChatClient(); + + // Act + var agent = chatClient.AsAIAgent( + model: "test-model", + name: "Test Agent", + instructions: null); + + // Assert + Assert.NotNull(agent); + Assert.Equal("Test Agent", agent.Name); + } + + /// + /// Verify that CreateAIAgent with whitespace instructions does not set instructions. + /// + [Fact] + public void CreateAIAgent_WithWhitespaceInstructions_DoesNotSetInstructions() + { + // Arrange + var chatClient = new TestAnthropicChatClient(); + + // Act + var agent = chatClient.AsAIAgent( + model: "test-model", + name: "Test Agent", + instructions: " "); + + // Assert + Assert.NotNull(agent); + Assert.Equal("Test Agent", agent.Name); + } } From fd0337728a0b13715310ccb8a2625768d788dfcb Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 23 Jan 2026 12:08:35 +0000 Subject: [PATCH 3/6] Update instruction verification tests to check agent.Instructions property Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com> --- .../Extensions/AnthropicBetaServiceExtensionsTests.cs | 2 ++ .../Extensions/AnthropicClientExtensionsTests.cs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs index 532304e54a..2d623744d0 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs @@ -290,6 +290,7 @@ public void CreateAIAgent_WithNullInstructions_DoesNotSetInstructions() // Assert Assert.NotNull(agent); Assert.Equal("Test Agent", agent.Name); + Assert.Null(agent.Instructions); } /// @@ -310,6 +311,7 @@ public void CreateAIAgent_WithWhitespaceInstructions_DoesNotSetInstructions() // Assert Assert.NotNull(agent); Assert.Equal("Test Agent", agent.Name); + Assert.Null(agent.Instructions); } /// diff --git a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs index 100bec6295..9723b61cf9 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs @@ -357,6 +357,7 @@ public void CreateAIAgent_WithNullInstructions_DoesNotSetInstructions() // Assert Assert.NotNull(agent); Assert.Equal("Test Agent", agent.Name); + Assert.Null(agent.Instructions); } /// @@ -377,5 +378,6 @@ public void CreateAIAgent_WithWhitespaceInstructions_DoesNotSetInstructions() // Assert Assert.NotNull(agent); Assert.Equal("Test Agent", agent.Name); + Assert.Null(agent.Instructions); } } From f00de62520bf742ec92a07d80981ae0a07b07883 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 23 Jan 2026 12:38:59 +0000 Subject: [PATCH 4/6] Update unit tests with proper assertions for agent scenarios Co-authored-by: rogerbarreto <19890735+rogerbarreto@users.noreply.github.com> --- .../Extensions/AnthropicBetaServiceExtensionsTests.cs | 7 +++++++ .../Extensions/AnthropicClientExtensionsTests.cs | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs index 2d623744d0..fc2639dd2e 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs @@ -207,6 +207,8 @@ public void CreateAIAgent_WithTools_AssignsToolsCorrectly() // Assert Assert.NotNull(agent); Assert.Equal("Test Agent", agent.Name); + // When tools are provided, ChatOptions is created but instructions remain null + Assert.Null(agent.Instructions); } /// @@ -227,6 +229,8 @@ public void CreateAIAgent_WithExplicitMaxTokens_UsesProvidedValue() // Assert Assert.NotNull(agent); Assert.Equal("Test Agent", agent.Name); + // MaxTokens is applied at the chat client level, agent is created successfully + Assert.NotNull(agent.ChatClient); } /// @@ -249,6 +253,7 @@ public void CreateAIAgent_WithToolsAndInstructions_AssignsBothCorrectly() // Assert Assert.NotNull(agent); Assert.Equal("Test Agent", agent.Name); + Assert.Equal("Test instructions", agent.Instructions); } /// @@ -270,6 +275,8 @@ public void CreateAIAgent_WithEmptyTools_DoesNotAssignTools() // Assert Assert.NotNull(agent); Assert.Equal("Test Agent", agent.Name); + // With empty tools and no instructions, agent instructions remain null + Assert.Null(agent.Instructions); } /// diff --git a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs index 9723b61cf9..9ac988f7ef 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs @@ -274,6 +274,8 @@ public void CreateAIAgent_WithTools_AssignsToolsCorrectly() // Assert Assert.NotNull(agent); Assert.Equal("Test Agent", agent.Name); + // When tools are provided, ChatOptions is created but instructions remain null + Assert.Null(agent.Instructions); } /// @@ -294,6 +296,8 @@ public void CreateAIAgent_WithExplicitMaxTokens_UsesProvidedValue() // Assert Assert.NotNull(agent); Assert.Equal("Test Agent", agent.Name); + // MaxTokens is applied at the chat client level, agent is created successfully + Assert.NotNull(agent.ChatClient); } /// @@ -316,6 +320,7 @@ public void CreateAIAgent_WithToolsAndInstructions_AssignsBothCorrectly() // Assert Assert.NotNull(agent); Assert.Equal("Test Agent", agent.Name); + Assert.Equal("Test instructions", agent.Instructions); } /// @@ -337,6 +342,8 @@ public void CreateAIAgent_WithEmptyTools_DoesNotAssignTools() // Assert Assert.NotNull(agent); Assert.Equal("Test Agent", agent.Name); + // With empty tools and no instructions, agent instructions remain null + Assert.Null(agent.Instructions); } /// From 03b3cc020890d083884a5084b371c4ba98db9e98 Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> Date: Fri, 23 Jan 2026 12:53:51 +0000 Subject: [PATCH 5/6] Add assertions for tools and maxTokens in Anthropic extension tests --- .../AnthropicBetaServiceExtensionsTests.cs | 22 +++++++++++++++++++ .../AnthropicClientExtensionsTests.cs | 22 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs index fc2639dd2e..e3d3968191 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs @@ -209,6 +209,12 @@ public void CreateAIAgent_WithTools_AssignsToolsCorrectly() Assert.Equal("Test Agent", agent.Name); // When tools are provided, ChatOptions is created but instructions remain null Assert.Null(agent.Instructions); + + // Verify that tools are registered in the FunctionInvokingChatClient + var functionInvokingClient = agent.GetService(); + Assert.NotNull(functionInvokingClient); + Assert.NotNull(functionInvokingClient.AdditionalTools); + Assert.Contains(functionInvokingClient.AdditionalTools, t => t is AIFunction func && func.Name == "TestFunction"); } /// @@ -231,6 +237,11 @@ public void CreateAIAgent_WithExplicitMaxTokens_UsesProvidedValue() Assert.Equal("Test Agent", agent.Name); // MaxTokens is applied at the chat client level, agent is created successfully Assert.NotNull(agent.ChatClient); + + // Verify that the AnthropicChatClient is available and configured + var anthropicChatClient = agent.GetService(); + Assert.NotNull(anthropicChatClient); + Assert.Equal(8192, anthropicChatClient.DefaultMaxOutputTokens); } /// @@ -254,6 +265,12 @@ public void CreateAIAgent_WithToolsAndInstructions_AssignsBothCorrectly() Assert.NotNull(agent); Assert.Equal("Test Agent", agent.Name); Assert.Equal("Test instructions", agent.Instructions); + + // Verify that tools are registered in the FunctionInvokingChatClient + var functionInvokingClient = agent.GetService(); + Assert.NotNull(functionInvokingClient); + Assert.NotNull(functionInvokingClient.AdditionalTools); + Assert.Contains(functionInvokingClient.AdditionalTools, t => t is AIFunction func && func.Name == "TestFunction"); } /// @@ -277,6 +294,11 @@ public void CreateAIAgent_WithEmptyTools_DoesNotAssignTools() Assert.Equal("Test Agent", agent.Name); // With empty tools and no instructions, agent instructions remain null Assert.Null(agent.Instructions); + + // Verify that FunctionInvokingChatClient has no additional tools assigned + var functionInvokingClient = agent.GetService(); + Assert.NotNull(functionInvokingClient); + Assert.True(functionInvokingClient.AdditionalTools is null or { Count: 0 }); } /// diff --git a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs index 9ac988f7ef..b6067e4714 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs @@ -276,6 +276,12 @@ public void CreateAIAgent_WithTools_AssignsToolsCorrectly() Assert.Equal("Test Agent", agent.Name); // When tools are provided, ChatOptions is created but instructions remain null Assert.Null(agent.Instructions); + + // Verify that tools are registered in the FunctionInvokingChatClient + var functionInvokingClient = agent.GetService(); + Assert.NotNull(functionInvokingClient); + Assert.NotNull(functionInvokingClient.AdditionalTools); + Assert.Contains(functionInvokingClient.AdditionalTools, t => t is AIFunction func && func.Name == "TestFunction"); } /// @@ -298,6 +304,11 @@ public void CreateAIAgent_WithExplicitMaxTokens_UsesProvidedValue() Assert.Equal("Test Agent", agent.Name); // MaxTokens is applied at the chat client level, agent is created successfully Assert.NotNull(agent.ChatClient); + + // Verify that the AnthropicChatClient is available and configured + var anthropicChatClient = agent.GetService(); + Assert.NotNull(anthropicChatClient); + Assert.Equal(8192, anthropicChatClient.DefaultMaxOutputTokens); } /// @@ -321,6 +332,12 @@ public void CreateAIAgent_WithToolsAndInstructions_AssignsBothCorrectly() Assert.NotNull(agent); Assert.Equal("Test Agent", agent.Name); Assert.Equal("Test instructions", agent.Instructions); + + // Verify that tools are registered in the FunctionInvokingChatClient + var functionInvokingClient = agent.GetService(); + Assert.NotNull(functionInvokingClient); + Assert.NotNull(functionInvokingClient.AdditionalTools); + Assert.Contains(functionInvokingClient.AdditionalTools, t => t is AIFunction func && func.Name == "TestFunction"); } /// @@ -344,6 +361,11 @@ public void CreateAIAgent_WithEmptyTools_DoesNotAssignTools() Assert.Equal("Test Agent", agent.Name); // With empty tools and no instructions, agent instructions remain null Assert.Null(agent.Instructions); + + // Verify that FunctionInvokingChatClient has no additional tools assigned + var functionInvokingClient = agent.GetService(); + Assert.NotNull(functionInvokingClient); + Assert.True(functionInvokingClient.AdditionalTools is null or { Count: 0 }); } /// From 4fcf5600b279c5235e112aca992a41f160d45fd9 Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> Date: Fri, 23 Jan 2026 14:36:13 +0000 Subject: [PATCH 6/6] Add proper assertions for tools and maxTokens in Anthropic tests --- .../AnthropicBetaServiceExtensionsTests.cs | 68 +++++++++++++++---- .../AnthropicClientExtensionsTests.cs | 68 +++++++++++++++---- 2 files changed, 112 insertions(+), 24 deletions(-) diff --git a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs index e3d3968191..8415b09b25 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicBetaServiceExtensionsTests.cs @@ -221,27 +221,71 @@ public void CreateAIAgent_WithTools_AssignsToolsCorrectly() /// Verify that CreateAIAgent with explicit defaultMaxTokens uses the provided value. /// [Fact] - public void CreateAIAgent_WithExplicitMaxTokens_UsesProvidedValue() + public async Task CreateAIAgent_WithExplicitMaxTokens_UsesProvidedValueAsync() { // Arrange - var chatClient = new TestAnthropicChatClient(); + int capturedMaxTokens = 0; + var handler = new CapturingHttpHandler(request => + { + // Parse the request body to capture max_tokens + var content = request.Content?.ReadAsStringAsync().GetAwaiter().GetResult(); + if (content is not null) + { + var json = System.Text.Json.JsonDocument.Parse(content); + if (json.RootElement.TryGetProperty("max_tokens", out var maxTokens)) + { + capturedMaxTokens = maxTokens.GetInt32(); + } + } + }); + + var client = new AnthropicClient + { + HttpClient = new HttpClient(handler) { BaseAddress = new Uri("http://localhost") }, + APIKey = "test-key" + }; // Act - var agent = chatClient.Beta.AsAIAgent( - model: "test-model", + var agent = client.Beta.AsAIAgent( + model: "claude-haiku-4-5", name: "Test Agent", defaultMaxTokens: 8192); + // Invoke the agent to trigger the request + var thread = await agent.GetNewThreadAsync(); + try + { + await agent.RunAsync("Test message", thread); + } + catch + { + // Expected to fail since we're using a test handler + } + // Assert - Assert.NotNull(agent); - Assert.Equal("Test Agent", agent.Name); - // MaxTokens is applied at the chat client level, agent is created successfully - Assert.NotNull(agent.ChatClient); + Assert.Equal(8192, capturedMaxTokens); + } + + /// + /// HTTP handler that captures requests for verification. + /// + private sealed class CapturingHttpHandler : HttpMessageHandler + { + private readonly Action _captureRequest; - // Verify that the AnthropicChatClient is available and configured - var anthropicChatClient = agent.GetService(); - Assert.NotNull(anthropicChatClient); - Assert.Equal(8192, anthropicChatClient.DefaultMaxOutputTokens); + public CapturingHttpHandler(Action captureRequest) + { + this._captureRequest = captureRequest; + } + + protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + this._captureRequest(request); + return Task.FromResult(new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest) + { + Content = new StringContent("{\"error\": \"test\"}") + }); + } } /// diff --git a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs index b6067e4714..cd7112fc5b 100644 --- a/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs +++ b/dotnet/tests/Microsoft.Agents.AI.Anthropic.UnitTests/Extensions/AnthropicClientExtensionsTests.cs @@ -288,27 +288,71 @@ public void CreateAIAgent_WithTools_AssignsToolsCorrectly() /// Verify that CreateAIAgent with explicit defaultMaxTokens uses the provided value. /// [Fact] - public void CreateAIAgent_WithExplicitMaxTokens_UsesProvidedValue() + public async Task CreateAIAgent_WithExplicitMaxTokens_UsesProvidedValueAsync() { // Arrange - var chatClient = new TestAnthropicChatClient(); + int capturedMaxTokens = 0; + var handler = new CapturingHttpHandler(request => + { + // Parse the request body to capture max_tokens + var content = request.Content?.ReadAsStringAsync().GetAwaiter().GetResult(); + if (content is not null) + { + var json = System.Text.Json.JsonDocument.Parse(content); + if (json.RootElement.TryGetProperty("max_tokens", out var maxTokens)) + { + capturedMaxTokens = maxTokens.GetInt32(); + } + } + }); + + var client = new AnthropicClient + { + HttpClient = new HttpClient(handler) { BaseAddress = new Uri("http://localhost") }, + APIKey = "test-key" + }; // Act - var agent = chatClient.AsAIAgent( - model: "test-model", + var agent = client.AsAIAgent( + model: "claude-haiku-4-5", name: "Test Agent", defaultMaxTokens: 8192); + // Invoke the agent to trigger the request + var thread = await agent.GetNewThreadAsync(); + try + { + await agent.RunAsync("Test message", thread); + } + catch + { + // Expected to fail since we're using a test handler + } + // Assert - Assert.NotNull(agent); - Assert.Equal("Test Agent", agent.Name); - // MaxTokens is applied at the chat client level, agent is created successfully - Assert.NotNull(agent.ChatClient); + Assert.Equal(8192, capturedMaxTokens); + } + + /// + /// HTTP handler that captures requests for verification. + /// + private sealed class CapturingHttpHandler : HttpMessageHandler + { + private readonly Action _captureRequest; - // Verify that the AnthropicChatClient is available and configured - var anthropicChatClient = agent.GetService(); - Assert.NotNull(anthropicChatClient); - Assert.Equal(8192, anthropicChatClient.DefaultMaxOutputTokens); + public CapturingHttpHandler(Action captureRequest) + { + this._captureRequest = captureRequest; + } + + protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + this._captureRequest(request); + return Task.FromResult(new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest) + { + Content = new StringContent("{\"error\": \"test\"}") + }); + } } ///