11import unittest
2- from unittest .mock import Mock , patch
3- from mcpx_pydantic_ai import Agent , _convert_type
2+ from unittest .mock import Mock
43from typing import Dict , Any
54import os
65
6+
7+ from mcpx_pydantic_ai import Agent
8+
79os .environ ["ANTHROPIC_API_KEY" ] = "something"
810
911
@@ -35,33 +37,28 @@ def __init__(self):
3537 }
3638 self .called_tool = None
3739 self .called_params = None
40+ self .config = Mock (profile = "default" )
3841
3942 def call_tool (self , tool : str , params : Dict [str , Any ]) -> MockResponse :
4043 self .called_tool = tool
4144 self .called_params = params
4245 return MockResponse ("mock response" )
4346
44- def set_profile (self , profile : str ):
45- self .profile = profile
46-
4747 def _make_pydantic_function (self , tool ):
4848 def test (input : dict ):
4949 return self .call_tool (tool .name , input ).content [0 ].text
50- return test
5150
51+ return test
5252
53- class TestTypeConversion (unittest .TestCase ):
54- def test_convert_basic_types (self ):
55- self .assertEqual (_convert_type ("string" ), str )
56- self .assertEqual (_convert_type ("boolean" ), bool )
57- self .assertEqual (_convert_type ("number" ), float )
58- self .assertEqual (_convert_type ("integer" ), int )
59- self .assertEqual (_convert_type ("object" ), dict )
60- self .assertEqual (_convert_type ("array" ), list )
53+ def mcp_sse (self , profile = None , expires_in = None ):
54+ mock_mcp = Mock ()
55+ mock_mcp .is_sse = True
56+ mock_mcp .is_stdio = False
57+ mock_mcp .config = Mock (url = "http://mock-url.com" )
58+ return mock_mcp
6159
62- def test_convert_invalid_type (self ):
63- with self .assertRaises (TypeError ):
64- _convert_type ("invalid_type" )
60+ def _fix_profile (self , profile ):
61+ return profile
6562
6663
6764class TestAgent (unittest .TestCase ):
@@ -76,102 +73,6 @@ def setUp(self):
7673 def test_init_with_custom_client (self ):
7774 """Test agent initialization with custom client"""
7875 self .assertEqual (self .agent .client , self .mock_client )
79- self .assertEqual (
80- len (self .agent ._function_tools ), 1
81- ) # Should have our mock tool
82-
83- def test_init_with_ignore_tools (self ):
84- """Test agent initialization with ignored tools"""
85- agent = Agent (
86- model = "claude-3-5-sonnet-latest" ,
87- client = self .mock_client ,
88- ignore_tools = ["test_tool" ],
89- system_prompt = "test prompt" ,
90- )
91- self .assertEqual (
92- len (agent ._function_tools ), 0
93- ) # Should have no tools due to ignore
94-
95- def test_set_profile (self ):
96- """Test setting profile updates client profile"""
97- self .agent .set_profile ("test_profile" )
98- self .assertEqual (self .mock_client .profile , "test_profile" )
99-
100- def test_register_custom_tool (self ):
101- """Test registering a custom tool with custom function"""
102- custom_mock = Mock (return_value = "custom response" )
103-
104- self .agent .register_tool (
105- MockTool (
106- "custom_tool" ,
107- "A custom tool" ,
108- {"properties" : {"param" : {"type" : "string" }}},
109- ),
110- custom_mock ,
111- )
112-
113- # Verify tool was registered
114- self .assertIn ("custom_tool" , self .agent ._function_tools )
115-
116- # Test tool execution
117- tool_func = self .agent ._function_tools ["custom_tool" ].function
118- result = tool_func ({"param" : "test" })
119-
120- custom_mock .assert_called_once_with ({"param" : "test" })
121- self .assertEqual (result , "custom response" )
122-
123- def test_tool_execution (self ):
124- """Test executing a registered tool"""
125- # Our mock tool should be registered automatically
126- tool_func = self .agent ._function_tools ["test_tool" ].function
127-
128- result = tool_func ({"param1" : "test" , "param2" : 123 })
129-
130- self .assertEqual (self .mock_client .called_tool , "test_tool" )
131- self .assertEqual (
132- self .mock_client .called_params , {"param1" : "test" , "param2" : 123 }
133- )
134- self .assertEqual (result , "mock response" )
135-
136- def test_reset_tools (self ):
137- """Test resetting tools"""
138- # Add a custom tool
139- self .agent .register_tool (
140- MockTool (
141- "custom_tool" ,
142- "A custom tool" ,
143- {"properties" : {"param" : {"type" : "string" }}},
144- ),
145- Mock (),
146- )
147-
148- # Reset tools
149- self .agent .reset_tools ()
150-
151- # Only custom tool should remain
152- self .assertEqual (len (self .agent ._function_tools ), 1 )
153- self .assertIn ("custom_tool" , self .agent ._function_tools )
154- self .assertNotIn ("test_tool" , self .agent ._function_tools )
155-
156- @patch ("mcpx_pydantic_ai.pydantic_ai.Agent.run_sync" )
157- def test_run_sync_updates_tools (self , mock_run_sync ):
158- """Test that run_sync updates tools by default"""
159- mock_run_sync .return_value = "test response"
160-
161- result = self .agent .run_sync ("test prompt" )
162-
163- self .assertEqual (result , "test response" )
164- mock_run_sync .assert_called_once ()
165-
166- @patch ("mcpx_pydantic_ai.pydantic_ai.Agent.run_sync" )
167- def test_run_sync_without_tool_update (self , mock_run_sync ):
168- """Test that run_sync can skip tool updates"""
169- mock_run_sync .return_value = "test response"
170-
171- result = self .agent .run_sync ("test prompt" , update_tools = False )
172-
173- self .assertEqual (result , "test response" )
174- mock_run_sync .assert_called_once ()
17576
17677
17778if __name__ == "__main__" :
0 commit comments