|
13 | 13 | from fastmcp.tools import FunctionTool, ToolManager |
14 | 14 | from fastmcp.tools.tool import Tool |
15 | 15 | from fastmcp.tools.tool_transform import ArgTransformConfig, ToolTransformConfig |
16 | | -from fastmcp.utilities.tests import caplog_for_fastmcp |
| 16 | +from fastmcp.utilities.tests import caplog_for_fastmcp, temporary_settings |
17 | 17 | from fastmcp.utilities.types import Image |
18 | 18 |
|
19 | 19 |
|
@@ -996,3 +996,42 @@ async def async_buggy_tool(x: int) -> int: |
996 | 996 | # Exception message should contain the tool name but not the internal details |
997 | 997 | assert "Error calling tool 'async_buggy_tool'" in str(excinfo.value) |
998 | 998 | assert "Internal async error details" not in str(excinfo.value) |
| 999 | + |
| 1000 | + |
| 1001 | +class TestMountedComponentsRaiseOnLoadError: |
| 1002 | + """Test the mounted_components_raise_on_load_error setting.""" |
| 1003 | + |
| 1004 | + async def test_mounted_components_raise_on_load_error_default_false(self): |
| 1005 | + """Test that by default, mounted component load errors are warned and not raised.""" |
| 1006 | + import fastmcp |
| 1007 | + |
| 1008 | + # Ensure default setting is False |
| 1009 | + assert fastmcp.settings.mounted_components_raise_on_load_error is False |
| 1010 | + |
| 1011 | + parent_mcp = FastMCP("ParentServer") |
| 1012 | + child_mcp = FastMCP("FailingChildServer") |
| 1013 | + |
| 1014 | + # Create a failing mounted server by corrupting it |
| 1015 | + parent_mcp.mount(child_mcp, prefix="child") |
| 1016 | + # Corrupt the child server to make it fail during tool loading |
| 1017 | + child_mcp._tool_manager._mounted_servers.append("invalid") # type: ignore |
| 1018 | + |
| 1019 | + # Should not raise, just warn |
| 1020 | + tools = await parent_mcp._tool_manager.list_tools() |
| 1021 | + assert isinstance(tools, list) # Should return empty list, not raise |
| 1022 | + |
| 1023 | + async def test_mounted_components_raise_on_load_error_true(self): |
| 1024 | + """Test that when enabled, mounted component load errors are raised.""" |
| 1025 | + parent_mcp = FastMCP("ParentServer") |
| 1026 | + child_mcp = FastMCP("FailingChildServer") |
| 1027 | + |
| 1028 | + # Create a failing mounted server |
| 1029 | + parent_mcp.mount(child_mcp, prefix="child") |
| 1030 | + # Corrupt the child server to make it fail during tool loading |
| 1031 | + child_mcp._tool_manager._mounted_servers.append("invalid") # type: ignore |
| 1032 | + |
| 1033 | + # Use temporary settings context manager |
| 1034 | + with temporary_settings(mounted_components_raise_on_load_error=True): |
| 1035 | + # Should raise the exception |
| 1036 | + with pytest.raises(AttributeError, match=""): |
| 1037 | + await parent_mcp._tool_manager.list_tools() |
0 commit comments