|
| 1 | +"""Integration tests for dynamic tool behavior through the proxy.""" |
| 2 | + |
| 3 | +import fastmcp |
| 4 | +import logging |
| 5 | +import pytest |
| 6 | + |
| 7 | + |
| 8 | +logger = logging.getLogger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.asyncio(loop_scope='module') |
| 12 | +async def test_proxy_reflects_tool_addition(mcp_client: fastmcp.Client, is_using_agentcore: bool): |
| 13 | + """Test that when backend dynamically adds a tool, proxy reflects it on next list_tools call.""" |
| 14 | + if is_using_agentcore: |
| 15 | + pytest.skip() |
| 16 | + |
| 17 | + # Arrange - Get initial tool list |
| 18 | + initial_tools = await mcp_client.list_tools() |
| 19 | + initial_tool_names = [tool.name for tool in initial_tools] |
| 20 | + |
| 21 | + logger.info(f'Initial tools: {initial_tool_names}') |
| 22 | + |
| 23 | + # Verify 'multiply' tool doesn't exist yet |
| 24 | + assert 'multiply' not in initial_tool_names, 'multiply tool should not exist initially' |
| 25 | + |
| 26 | + # Act - Trigger backend to dynamically add a new tool |
| 27 | + logger.info('Calling add_tool_multiply to add a new tool to the backend') |
| 28 | + add_result = await mcp_client.call_tool('add_tool_multiply', {}) |
| 29 | + logger.info(f'Backend response: {add_result}') |
| 30 | + |
| 31 | + # Get updated tool list |
| 32 | + updated_tools = await mcp_client.list_tools() |
| 33 | + updated_tool_names = [tool.name for tool in updated_tools] |
| 34 | + |
| 35 | + logger.info(f'Updated tools: {updated_tool_names}') |
| 36 | + |
| 37 | + # Assert |
| 38 | + # The proxy should reflect the newly added tool |
| 39 | + assert 'multiply' in updated_tool_names, 'multiply tool should now exist after adding' |
| 40 | + assert len(updated_tools) == len(initial_tools) + 1, 'Should have one more tool' |
| 41 | + |
| 42 | + # Verify the new tool actually works |
| 43 | + logger.info('Testing the newly added multiply tool') |
| 44 | + multiply_result = await mcp_client.call_tool('multiply', {'x': 6, 'y': 7}) |
| 45 | + |
| 46 | + # Extract the result (handling different response formats) |
| 47 | + from tests.integ.test_proxy_simple_mcp_server import get_text_content |
| 48 | + |
| 49 | + result_text = get_text_content(multiply_result) |
| 50 | + assert result_text == '42', f'multiply(6, 7) should return 42, got {result_text}' |
0 commit comments