|
25 | 25 | from google.adk.auth.auth_credential import ServiceAccount |
26 | 26 | from google.adk.features import FeatureName |
27 | 27 | from google.adk.features._feature_registry import temporary_feature_override |
| 28 | +from google.adk.tools.mcp_tool import mcp_tool |
28 | 29 | from google.adk.tools.mcp_tool.mcp_session_manager import MCPSessionManager |
29 | 30 | from google.adk.tools.mcp_tool.mcp_tool import MCPTool |
30 | 31 | from google.adk.tools.tool_context import ToolContext |
@@ -225,7 +226,7 @@ async def test_run_async_impl_no_auth(self): |
225 | 226 | ) |
226 | 227 | # Fix: call_tool uses 'arguments' parameter, not positional args |
227 | 228 | self.mock_session.call_tool.assert_called_once_with( |
228 | | - "test_tool", arguments=args, progress_callback=None |
| 229 | + "test_tool", arguments=args, progress_callback=None, meta=None |
229 | 230 | ) |
230 | 231 |
|
231 | 232 | @pytest.mark.asyncio |
@@ -262,6 +263,55 @@ async def test_run_async_impl_with_oauth2(self): |
262 | 263 | headers = call_args[1]["headers"] |
263 | 264 | assert headers == {"Authorization": "Bearer test_access_token"} |
264 | 265 |
|
| 266 | + @patch.object(mcp_tool, "propagate", autospec=True) |
| 267 | + @pytest.mark.asyncio |
| 268 | + async def test_run_async_impl_with_trace_context(self, mock_propagate): |
| 269 | + """Test running tool with trace context injection.""" |
| 270 | + mock_propagator = Mock() |
| 271 | + |
| 272 | + def inject_context(carrier, context=None) -> None: |
| 273 | + carrier["traceparent"] = ( |
| 274 | + "00-1234567890abcdef1234567890abcdef-1234567890abcdef-01" |
| 275 | + ) |
| 276 | + carrier["tracestate"] = "foo=bar" |
| 277 | + carrier["baggage"] = "baz=qux" |
| 278 | + |
| 279 | + mock_propagator.inject.side_effect = inject_context |
| 280 | + mock_propagate.get_global_textmap.return_value = mock_propagator |
| 281 | + |
| 282 | + tool = MCPTool( |
| 283 | + mcp_tool=self.mock_mcp_tool, |
| 284 | + mcp_session_manager=self.mock_session_manager, |
| 285 | + ) |
| 286 | + |
| 287 | + mcp_response = CallToolResult( |
| 288 | + content=[TextContent(type="text", text="success")] |
| 289 | + ) |
| 290 | + self.mock_session.call_tool = AsyncMock(return_value=mcp_response) |
| 291 | + |
| 292 | + tool_context = Mock(spec=ToolContext) |
| 293 | + args = {"param1": "test_value"} |
| 294 | + |
| 295 | + await tool._run_async_impl( |
| 296 | + args=args, tool_context=tool_context, credential=None |
| 297 | + ) |
| 298 | + |
| 299 | + self.mock_session_manager.create_session.assert_called_once_with( |
| 300 | + headers=None |
| 301 | + ) |
| 302 | + self.mock_session.call_tool.assert_called_once_with( |
| 303 | + "test_tool", |
| 304 | + arguments=args, |
| 305 | + progress_callback=None, |
| 306 | + meta={ |
| 307 | + "traceparent": ( |
| 308 | + "00-1234567890abcdef1234567890abcdef-1234567890abcdef-01" |
| 309 | + ), |
| 310 | + "tracestate": "foo=bar", |
| 311 | + "baggage": "baz=qux", |
| 312 | + }, |
| 313 | + ) |
| 314 | + |
265 | 315 | @pytest.mark.asyncio |
266 | 316 | async def test_get_headers_oauth2(self): |
267 | 317 | """Test header generation for OAuth2 credentials.""" |
@@ -778,7 +828,7 @@ async def test_run_async_impl_with_header_provider_no_auth(self): |
778 | 828 | headers=expected_headers |
779 | 829 | ) |
780 | 830 | self.mock_session.call_tool.assert_called_once_with( |
781 | | - "test_tool", arguments=args, progress_callback=None |
| 831 | + "test_tool", arguments=args, progress_callback=None, meta=None |
782 | 832 | ) |
783 | 833 |
|
784 | 834 | @pytest.mark.asyncio |
@@ -821,7 +871,7 @@ async def test_run_async_impl_with_header_provider_and_oauth2(self): |
821 | 871 | "X-Tenant-ID": "test-tenant", |
822 | 872 | } |
823 | 873 | self.mock_session.call_tool.assert_called_once_with( |
824 | | - "test_tool", arguments=args, progress_callback=None |
| 874 | + "test_tool", arguments=args, progress_callback=None, meta=None |
825 | 875 | ) |
826 | 876 |
|
827 | 877 | def test_init_with_progress_callback(self): |
@@ -875,7 +925,10 @@ async def my_progress_callback( |
875 | 925 | ) |
876 | 926 | # Verify progress_callback was passed to call_tool |
877 | 927 | self.mock_session.call_tool.assert_called_once_with( |
878 | | - "test_tool", arguments=args, progress_callback=my_progress_callback |
| 928 | + "test_tool", |
| 929 | + arguments=args, |
| 930 | + progress_callback=my_progress_callback, |
| 931 | + meta=None, |
879 | 932 | ) |
880 | 933 |
|
881 | 934 | @pytest.mark.asyncio |
|
0 commit comments