Skip to content

Commit 387a523

Browse files
authored
test: add integration test for dynamic tooling (#44)
* test: add integration tests for dynamic tooling * refactor(mcp_server.py): use middleware context getters and setters instead of global variables * test(test_proxy_dynamic_tools.py): skip dynamic tool tests on agent core
1 parent ea78200 commit 387a523

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

tests/integ/mcp/simple_mcp_server/mcp_server.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ def greet(name: str):
2020
return f'Hello {name}'
2121

2222

23+
##### Dynamic Tool Testing
24+
25+
26+
@mcp.tool
27+
def add_tool_multiply(ctx: Context):
28+
"""MCP Tool used for testing dynamic tool behavior through the proxy."""
29+
if not ctx.get_state('multiply_registered'):
30+
31+
@mcp.tool
32+
def multiply(x: int, y: int):
33+
"""Multiply two numbers."""
34+
return x * y
35+
36+
ctx.set_state('multiply_registered', True)
37+
return 'Tool "multiply" added successfully'
38+
return 'Tool "multiply" already exists'
39+
40+
2341
##### Elicitation Testing
2442

2543

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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

Comments
 (0)