π The fast, Pythonic way to build MCP servers and clients.
Add mounted_components_raise_on_load_error setting for debugging (#1534)
Jeremiah Lowin26969e80c330
diff --git a/src/fastmcp/prompts/prompt_manager.py b/src/fastmcp/prompts/prompt_manager.pyindex a7fb12159..ab1a944e2 100644--- a/src/fastmcp/prompts/prompt_manager.py+++ b/src/fastmcp/prompts/prompt_manager.py@@ -80,6 +80,8 @@ class PromptManager:logger.warning(f"Failed to get prompts from server: {mounted.server.name!r}, mounted at: {mounted.prefix!r}: {e}")+ if settings.mounted_components_raise_on_load_error:+ raisecontinue# Finally, add local prompts, which always take precedencediff --git a/src/fastmcp/resources/resource_manager.py b/src/fastmcp/resources/resource_manager.pyindex 7e031a414..c646c71aa 100644--- a/src/fastmcp/resources/resource_manager.py+++ b/src/fastmcp/resources/resource_manager.py@@ -114,6 +114,8 @@ class ResourceManager:logger.warning(f"Failed to get resources from server: {mounted.server.name!r}, mounted at: {mounted.prefix!r}: {e}")+ if settings.mounted_components_raise_on_load_error:+ raisecontinue# Finally, add local resources, which always take precedence@@ -165,6 +167,8 @@ class ResourceManager:logger.warning(f"Failed to get templates from server: {mounted.server.name!r}, mounted at: {mounted.prefix!r}: {e}")+ if settings.mounted_components_raise_on_load_error:+ raisecontinue# Finally, add local templates, which always take precedencediff --git a/src/fastmcp/settings.py b/src/fastmcp/settings.pyindex ae6adff94..1ecda5a08 100644--- a/src/fastmcp/settings.py+++ b/src/fastmcp/settings.py@@ -320,6 +320,20 @@ class Settings(BaseSettings):),] = True+ mounted_components_raise_on_load_error: Annotated[+ bool,+ Field(+ default=False,+ description=inspect.cleandoc(+ """+ If True, errors encountered when loading mounted components (tools, resources, prompts)+ will be raised instead of logged as warnings. This is useful for debugging+ but will interrupt normal operation.+ """+ ),+ ),+ ] = False+def __getattr__(name: str):"""diff --git a/src/fastmcp/tools/tool_manager.py b/src/fastmcp/tools/tool_manager.pyindex b460c2b22..a40a9877c 100644--- a/src/fastmcp/tools/tool_manager.py+++ b/src/fastmcp/tools/tool_manager.py@@ -86,6 +86,8 @@ class ToolManager:logger.warning(f"Failed to get tools from server: {mounted.server.name!r}, mounted at: {mounted.prefix!r}: {e}")+ if settings.mounted_components_raise_on_load_error:+ raisecontinue# Finally, add local tools, which always take precedencediff --git a/tests/tools/test_tool_manager.py b/tests/tools/test_tool_manager.pyindex 13989276f..0707cf7f3 100644--- a/tests/tools/test_tool_manager.py+++ b/tests/tools/test_tool_manager.py@@ -13,7 +13,7 @@ from fastmcp.exceptions import NotFoundError, ToolErrorfrom fastmcp.tools import FunctionTool, ToolManagerfrom fastmcp.tools.tool import Toolfrom fastmcp.tools.tool_transform import ArgTransformConfig, ToolTransformConfig-from fastmcp.utilities.tests import caplog_for_fastmcp+from fastmcp.utilities.tests import caplog_for_fastmcp, temporary_settingsfrom fastmcp.utilities.types import Image@@ -996,3 +996,42 @@ class TestToolErrorHandling:# Exception message should contain the tool name but not the internal detailsassert "Error calling tool 'async_buggy_tool'" in str(excinfo.value)assert "Internal async error details" not in str(excinfo.value)+++class TestMountedComponentsRaiseOnLoadError:+ """Test the mounted_components_raise_on_load_error setting."""++ async def test_mounted_components_raise_on_load_error_default_false(self):+ """Test that by default, mounted component load errors are warned and not raised."""+ import fastmcp++ # Ensure default setting is False+ assert fastmcp.settings.mounted_components_raise_on_load_error is False++ parent_mcp = FastMCP("ParentServer")+ child_mcp = FastMCP("FailingChildServer")++ # Create a failing mounted server by corrupting it+ parent_mcp.mount(child_mcp, prefix="child")+ # Corrupt the child server to make it fail during tool loading+ child_mcp._tool_manager._mounted_servers.append("invalid") # type: ignore++ # Should not raise, just warn+ tools = await parent_mcp._tool_manager.list_tools()+ assert isinstance(tools, list) # Should return empty list, not raise++ async def test_mounted_components_raise_on_load_error_true(self):+ """Test that when enabled, mounted component load errors are raised."""+ parent_mcp = FastMCP("ParentServer")+ child_mcp = FastMCP("FailingChildServer")++ # Create a failing mounted server+ parent_mcp.mount(child_mcp, prefix="child")+ # Corrupt the child server to make it fail during tool loading+ child_mcp._tool_manager._mounted_servers.append("invalid") # type: ignore++ # Use temporary settings context manager+ with temporary_settings(mounted_components_raise_on_load_error=True):+ # Should raise the exception+ with pytest.raises(AttributeError, match=""):+ await parent_mcp._tool_manager.list_tools()