|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Unit tests for McpInstructionProvider.""" |
| 16 | +import sys |
| 17 | +from unittest.mock import AsyncMock |
| 18 | +from unittest.mock import MagicMock |
| 19 | +from unittest.mock import patch |
| 20 | + |
| 21 | +from google.adk.agents.readonly_context import ReadonlyContext |
| 22 | +import pytest |
| 23 | + |
| 24 | +# Skip all tests in this module if Python version is less than 3.10 |
| 25 | +pytestmark = pytest.mark.skipif( |
| 26 | + sys.version_info < (3, 10), |
| 27 | + reason="MCP instruction provider requires Python 3.10+", |
| 28 | +) |
| 29 | + |
| 30 | +# Import dependencies with version checking |
| 31 | +try: |
| 32 | + from google.adk.agents.mcp_instruction_provider import McpInstructionProvider |
| 33 | +except ImportError as e: |
| 34 | + if sys.version_info < (3, 10): |
| 35 | + # Create dummy classes to prevent NameError during test collection |
| 36 | + # Tests will be skipped anyway due to pytestmark |
| 37 | + class DummyClass: |
| 38 | + pass |
| 39 | + |
| 40 | + McpInstructionProvider = DummyClass |
| 41 | + else: |
| 42 | + raise e |
| 43 | + |
| 44 | + |
| 45 | +class TestMcpInstructionProvider: |
| 46 | + """Unit tests for McpInstructionProvider.""" |
| 47 | + |
| 48 | + def setup_method(self): |
| 49 | + """Sets up the test environment.""" |
| 50 | + self.connection_params = {"host": "localhost", "port": 8000} |
| 51 | + self.prompt_name = "test_prompt" |
| 52 | + self.mock_mcp_session_manager_cls = patch( |
| 53 | + "google.adk.agents.mcp_instruction_provider.MCPSessionManager" |
| 54 | + ).start() |
| 55 | + self.mock_mcp_session_manager = ( |
| 56 | + self.mock_mcp_session_manager_cls.return_value |
| 57 | + ) |
| 58 | + self.mock_session = MagicMock() |
| 59 | + self.mock_session.list_prompts = AsyncMock() |
| 60 | + self.mock_session.get_prompt = AsyncMock() |
| 61 | + self.mock_mcp_session_manager.create_session = AsyncMock( |
| 62 | + return_value=self.mock_session |
| 63 | + ) |
| 64 | + self.provider = McpInstructionProvider( |
| 65 | + self.connection_params, self.prompt_name |
| 66 | + ) |
| 67 | + |
| 68 | + @pytest.mark.asyncio |
| 69 | + async def test_call_success_no_args(self): |
| 70 | + """Tests __call__ with a prompt that has no arguments.""" |
| 71 | + mock_prompt = MagicMock() |
| 72 | + mock_prompt.name = self.prompt_name |
| 73 | + mock_prompt.arguments = None |
| 74 | + self.mock_session.list_prompts.return_value = MagicMock( |
| 75 | + prompts=[mock_prompt] |
| 76 | + ) |
| 77 | + |
| 78 | + mock_msg1 = MagicMock() |
| 79 | + mock_msg1.content.type = "text" |
| 80 | + mock_msg1.content.text = "instruction part 1. " |
| 81 | + mock_msg2 = MagicMock() |
| 82 | + mock_msg2.content.type = "text" |
| 83 | + mock_msg2.content.text = "instruction part 2" |
| 84 | + self.mock_session.get_prompt.return_value = MagicMock( |
| 85 | + messages=[mock_msg1, mock_msg2] |
| 86 | + ) |
| 87 | + |
| 88 | + mock_invocation_context = MagicMock() |
| 89 | + mock_invocation_context.session.state = {} |
| 90 | + context = ReadonlyContext(mock_invocation_context) |
| 91 | + |
| 92 | + # Call |
| 93 | + instruction = await self.provider(context) |
| 94 | + |
| 95 | + # Assert |
| 96 | + assert instruction == "instruction part 1. instruction part 2" |
| 97 | + self.mock_session.get_prompt.assert_called_once_with( |
| 98 | + self.prompt_name, arguments={} |
| 99 | + ) |
| 100 | + |
| 101 | + @pytest.mark.asyncio |
| 102 | + async def test_call_success_with_args(self): |
| 103 | + """Tests __call__ with a prompt that has arguments.""" |
| 104 | + mock_arg1 = MagicMock() |
| 105 | + mock_arg1.name = "arg1" |
| 106 | + mock_prompt = MagicMock() |
| 107 | + mock_prompt.name = self.prompt_name |
| 108 | + mock_prompt.arguments = [mock_arg1] |
| 109 | + self.mock_session.list_prompts.return_value = MagicMock( |
| 110 | + prompts=[mock_prompt] |
| 111 | + ) |
| 112 | + |
| 113 | + mock_msg = MagicMock() |
| 114 | + mock_msg.content.type = "text" |
| 115 | + mock_msg.content.text = "instruction with arg1" |
| 116 | + self.mock_session.get_prompt.return_value = MagicMock(messages=[mock_msg]) |
| 117 | + |
| 118 | + mock_invocation_context = MagicMock() |
| 119 | + mock_invocation_context.session.state = {"arg1": "value1", "arg2": "value2"} |
| 120 | + context = ReadonlyContext(mock_invocation_context) |
| 121 | + |
| 122 | + instruction = await self.provider(context) |
| 123 | + |
| 124 | + assert instruction == "instruction with arg1" |
| 125 | + self.mock_session.get_prompt.assert_called_once_with( |
| 126 | + self.prompt_name, arguments={"arg1": "value1"} |
| 127 | + ) |
| 128 | + |
| 129 | + @pytest.mark.asyncio |
| 130 | + async def test_call_prompt_not_found_in_list_prompts(self): |
| 131 | + """Tests __call__ when list_prompts doesn't return the prompt.""" |
| 132 | + self.mock_session.list_prompts.return_value = MagicMock(prompts=[]) |
| 133 | + |
| 134 | + mock_msg = MagicMock() |
| 135 | + mock_msg.content.type = "text" |
| 136 | + mock_msg.content.text = "instruction" |
| 137 | + self.mock_session.get_prompt.return_value = MagicMock(messages=[mock_msg]) |
| 138 | + |
| 139 | + mock_invocation_context = MagicMock() |
| 140 | + mock_invocation_context.session.state = {"arg1": "value1"} |
| 141 | + context = ReadonlyContext(mock_invocation_context) |
| 142 | + |
| 143 | + instruction = await self.provider(context) |
| 144 | + |
| 145 | + assert instruction == "instruction" |
| 146 | + self.mock_session.get_prompt.assert_called_once_with( |
| 147 | + self.prompt_name, arguments={} |
| 148 | + ) |
| 149 | + |
| 150 | + @pytest.mark.asyncio |
| 151 | + async def test_call_get_prompt_returns_no_messages(self): |
| 152 | + """Tests __call__ when get_prompt returns no messages.""" |
| 153 | + # Setup mocks |
| 154 | + self.mock_session.list_prompts.return_value = MagicMock(prompts=[]) |
| 155 | + self.mock_session.get_prompt.return_value = MagicMock(messages=[]) |
| 156 | + |
| 157 | + mock_invocation_context = MagicMock() |
| 158 | + mock_invocation_context.session.state = {} |
| 159 | + context = ReadonlyContext(mock_invocation_context) |
| 160 | + |
| 161 | + # Call and assert |
| 162 | + with pytest.raises( |
| 163 | + ValueError, match="Failed to load MCP prompt 'test_prompt'." |
| 164 | + ): |
| 165 | + await self.provider(context) |
| 166 | + |
| 167 | + # Assert |
| 168 | + self.mock_session.get_prompt.assert_called_once_with( |
| 169 | + self.prompt_name, arguments={} |
| 170 | + ) |
| 171 | + |
| 172 | + @pytest.mark.asyncio |
| 173 | + async def test_call_ignore_non_text_messages(self): |
| 174 | + """Tests __call__ ignores non-text messages.""" |
| 175 | + # Setup mocks |
| 176 | + mock_prompt = MagicMock() |
| 177 | + mock_prompt.name = self.prompt_name |
| 178 | + mock_prompt.arguments = None |
| 179 | + self.mock_session.list_prompts.return_value = MagicMock( |
| 180 | + prompts=[mock_prompt] |
| 181 | + ) |
| 182 | + |
| 183 | + mock_msg1 = MagicMock() |
| 184 | + mock_msg1.content.type = "text" |
| 185 | + mock_msg1.content.text = "instruction part 1. " |
| 186 | + |
| 187 | + mock_msg2 = MagicMock() |
| 188 | + mock_msg2.content.type = "image" |
| 189 | + mock_msg2.content.text = "ignored" |
| 190 | + |
| 191 | + mock_msg3 = MagicMock() |
| 192 | + mock_msg3.content.type = "text" |
| 193 | + mock_msg3.content.text = "instruction part 2" |
| 194 | + |
| 195 | + self.mock_session.get_prompt.return_value = MagicMock( |
| 196 | + messages=[mock_msg1, mock_msg2, mock_msg3] |
| 197 | + ) |
| 198 | + |
| 199 | + mock_invocation_context = MagicMock() |
| 200 | + mock_invocation_context.session.state = {} |
| 201 | + context = ReadonlyContext(mock_invocation_context) |
| 202 | + |
| 203 | + # Call |
| 204 | + instruction = await self.provider(context) |
| 205 | + |
| 206 | + # Assert |
| 207 | + assert instruction == "instruction part 1. instruction part 2" |
| 208 | + self.mock_session.get_prompt.assert_called_once_with( |
| 209 | + self.prompt_name, arguments={} |
| 210 | + ) |
0 commit comments