Skip to content

Commit 8e66f2e

Browse files
author
Kyon Caldera
committed
test: add integration tests for dynamic tooling
1 parent 0a6e54f commit 8e66f2e

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

tests/integ/mcp/simple_mcp_server/mcp_server.py

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

2222

23+
##### Dynamic Tool Testing
24+
25+
_multiply_registered = False
26+
27+
28+
@mcp.tool
29+
def add_tool_multiply():
30+
"""MCP Tool used for testing dynamic tool behavior through the proxy."""
31+
global _multiply_registered
32+
33+
if not _multiply_registered:
34+
35+
@mcp.tool
36+
def multiply(x: int, y: int):
37+
"""Multiply two numbers."""
38+
return x * y
39+
40+
_multiply_registered = True
41+
return 'Tool "multiply" added successfully'
42+
return 'Tool "multiply" already exists'
43+
44+
2345
##### Elicitation Testing
2446

2547

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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):
13+
"""Test that when backend dynamically adds a tool, proxy reflects it on next list_tools call."""
14+
# Arrange - Get initial tool list
15+
initial_tools = await mcp_client.list_tools()
16+
initial_tool_names = [tool.name for tool in initial_tools]
17+
18+
logger.info(f'Initial tools: {initial_tool_names}')
19+
20+
# Verify 'multiply' tool doesn't exist yet
21+
assert 'multiply' not in initial_tool_names, 'multiply tool should not exist initially'
22+
23+
# Act - Trigger backend to dynamically add a new tool
24+
logger.info('Calling add_tool_multiply to add a new tool to the backend')
25+
add_result = await mcp_client.call_tool('add_tool_multiply', {})
26+
logger.info(f'Backend response: {add_result}')
27+
28+
# Get updated tool list
29+
updated_tools = await mcp_client.list_tools()
30+
updated_tool_names = [tool.name for tool in updated_tools]
31+
32+
logger.info(f'Updated tools: {updated_tool_names}')
33+
34+
# Assert
35+
# The proxy should reflect the newly added tool
36+
assert 'multiply' in updated_tool_names, 'multiply tool should now exist after adding'
37+
assert len(updated_tools) == len(initial_tools) + 1, 'Should have one more tool'
38+
39+
# Verify the new tool actually works
40+
logger.info('Testing the newly added multiply tool')
41+
multiply_result = await mcp_client.call_tool('multiply', {'x': 6, 'y': 7})
42+
43+
# Extract the result (handling different response formats)
44+
from tests.integ.test_proxy_simple_mcp_server import get_text_content
45+
46+
result_text = get_text_content(multiply_result)
47+
assert result_text == '42', f'multiply(6, 7) should return 42, got {result_text}'

0 commit comments

Comments
 (0)