1+ """Integration tests for runtime tools functionality."""
2+
3+ import pytest
4+
5+ from mcp .server .fastmcp import FastMCP
6+ from mcp .shared .memory import create_connected_server_and_client_session
7+ from mcp .server .fastmcp .tools .base import Tool
8+ from mcp .types import TextContent
9+
10+ @pytest .mark .anyio
11+ async def test_runtime_tools ():
12+ """Test that runtime tools work correctly."""
13+ async def runtime_mcp_tools_generator () -> list [Tool ]:
14+ """Generate runtime tools."""
15+ def runtime_tool_1 (message : str ):
16+ return message
17+
18+ def runtime_tool_2 (message : str ):
19+ return message
20+
21+ return [
22+ Tool .from_function (runtime_tool_1 ),
23+ Tool .from_function (runtime_tool_2 )
24+ ]
25+
26+ # Create server with various tool configurations, both static and runtime
27+ mcp = FastMCP (name = "RuntimeToolsTestServer" , runtime_mcp_tools_generator = runtime_mcp_tools_generator )
28+
29+ # Static tool
30+ @mcp .tool (description = "Static tool" )
31+ def static_tool (message : str ) -> str :
32+ return message
33+
34+ # Start server and connect client
35+ async with create_connected_server_and_client_session (mcp ._mcp_server ) as client :
36+ await client .initialize ()
37+
38+ # List tools
39+ tools_result = await client .list_tools ()
40+ tool_names = {tool .name : tool for tool in tools_result .tools }
41+
42+ # Verify both tools
43+ assert "static_tool" in tool_names
44+ assert "runtime_tool_1" in tool_names
45+ assert "runtime_tool_2" in tool_names
46+
47+ # Check static tool
48+ result = await client .call_tool ("static_tool" , {"message" : "This is a test" })
49+ assert len (result .content ) == 1
50+ content = result .content [0 ]
51+ assert isinstance (content , TextContent )
52+ assert content .text == "This is a test"
53+
54+ # Check runtime tool 1
55+ result = await client .call_tool ("runtime_tool_1" , {"message" : "This is a test" })
56+ assert len (result .content ) == 1
57+ content = result .content [0 ]
58+ assert isinstance (content , TextContent )
59+ assert content .text == "This is a test"
60+
61+ # Check runtime tool 2
62+ result = await client .call_tool ("runtime_tool_2" , {"message" : "This is a test" })
63+ assert len (result .content ) == 1
64+ content = result .content [0 ]
65+ assert isinstance (content , TextContent )
66+ assert content .text == "This is a test"
67+
68+ # Check non existing tool
69+ result = await client .call_tool ("non_existing_tool" , {"message" : "This is a test" })
70+ assert len (result .content ) == 1
71+ content = result .content [0 ]
72+ assert isinstance (content , TextContent )
73+ assert content .text == "Unknown tool: non_existing_tool"
0 commit comments