Skip to content

Commit e697a87

Browse files
Fix CI failures in agents module (#294)
- Fix test_message_params.py: Update tests to handle agent.run() returning list format - Fix mcp_tool.py: Add missing MCPConnection import - Fix agent.py: Properly merge extra_headers to avoid conflicts with beta headers These fixes resolve pyright type errors and ruff linting issues that were causing CI failures on PR #293. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Claude <[email protected]>
1 parent a78013a commit e697a87

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

agents/agent.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,18 @@ async def _agent_loop(self, user_input: str) -> list[dict[str, Any]]:
9999
self.history.truncate()
100100
params = self._prepare_message_params()
101101

102+
# Merge headers properly - default beta header can be overridden by message_params
103+
default_headers = {"anthropic-beta": "code-execution-2025-05-22"}
104+
if "extra_headers" in params:
105+
# Pop extra_headers from params and merge with defaults
106+
custom_headers = params.pop("extra_headers")
107+
merged_headers = {**default_headers, **custom_headers}
108+
else:
109+
merged_headers = default_headers
110+
102111
response = self.client.messages.create(
103112
**params,
104-
extra_headers={"anthropic-beta": "code-execution-2025-05-22"}
113+
extra_headers=merged_headers
105114
)
106115
tool_calls = [
107116
block for block in response.content if block.type == "tool_use"

agents/test_message_params.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
import os
1010
import sys
11-
from typing import Any
12-
1311
# Add parent directory to path for imports
1412
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
1513

@@ -65,8 +63,10 @@ def test_basic_agent(self) -> None:
6563
)
6664

6765
response = agent.run("What is 2+2?")
68-
assert response.content[0].text.strip() in ["4", "2+2=4", "2 + 2 = 4"]
69-
self._print(f"Response: {response.content[0].text}")
66+
# response is a list of message content blocks
67+
assert any("4" in str(block.get("text", "")) for block in response if block.get("type") == "text")
68+
response_text = next((block["text"] for block in response if block.get("type") == "text"), "")
69+
self._print(f"Response: {response_text}")
7070

7171
def test_custom_headers(self) -> None:
7272
"""Test passing custom headers through message_params."""
@@ -87,8 +87,9 @@ def test_custom_headers(self) -> None:
8787
assert agent.message_params["extra_headers"]["X-Custom-Header"] == "test-value"
8888

8989
response = agent.run("What is 3+3?")
90-
assert "6" in response.content[0].text
91-
self._print(f"Response with custom headers: {response.content[0].text}")
90+
response_text = next((block["text"] for block in response if block.get("type") == "text"), "")
91+
assert "6" in response_text
92+
self._print(f"Response with custom headers: {response_text}")
9293

9394
def test_beta_headers(self) -> None:
9495
"""Test passing beta feature headers."""
@@ -105,8 +106,9 @@ def test_beta_headers(self) -> None:
105106

106107
# The API call should succeed even with beta headers
107108
response = agent.run("What is 5*5?")
108-
assert "25" in response.content[0].text
109-
self._print(f"Response with beta headers: {response.content[0].text}")
109+
response_text = next((block["text"] for block in response if block.get("type") == "text"), "")
110+
assert "25" in response_text
111+
self._print(f"Response with beta headers: {response_text}")
110112

111113
def test_metadata(self) -> None:
112114
"""Test passing valid metadata fields."""
@@ -122,8 +124,9 @@ def test_metadata(self) -> None:
122124
)
123125

124126
response = agent.run("What is 10/2?")
125-
assert "5" in response.content[0].text
126-
self._print(f"Response with metadata: {response.content[0].text}")
127+
response_text = next((block["text"] for block in response if block.get("type") == "text"), "")
128+
assert "5" in response_text
129+
self._print(f"Response with metadata: {response_text}")
127130

128131
def test_api_parameters(self) -> None:
129132
"""Test passing various API parameters."""
@@ -145,8 +148,9 @@ def test_api_parameters(self) -> None:
145148
assert params["temperature"] == 0.7
146149

147150
response = agent.run("Say 'test'")
148-
assert response.content[0].text
149-
self._print(f"Response with custom params: {response.content[0].text}")
151+
response_text = next((block["text"] for block in response if block.get("type") == "text"), "")
152+
assert response_text
153+
self._print(f"Response with custom params: {response_text}")
150154

151155
def test_parameter_override(self) -> None:
152156
"""Test that message_params override config defaults."""
@@ -219,8 +223,9 @@ def test_combined_parameters(self) -> None:
219223
assert params["top_k"] == 5
220224

221225
response = agent.run("What is 1+1?")
222-
assert "2" in response.content[0].text
223-
self._print(f"Response with combined params: {response.content[0].text}")
226+
response_text = next((block["text"] for block in response if block.get("type") == "text"), "")
227+
assert "2" in response_text
228+
self._print(f"Response with combined params: {response_text}")
224229

225230
def run_all_tests(self) -> None:
226231
"""Run all test cases."""

agents/tools/mcp_tool.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from typing import Any
44
from .base import Tool
5+
from ..utils.connections import MCPConnection
56

67

78
class MCPTool(Tool):

0 commit comments

Comments
 (0)