Spaces:
Sleeping
Sleeping
Jeremiah Lowin Jeremiah Lowin marvin-context-protocol[bot] commited on
Add mounted_components_raise_on_load_error setting for debugging (#1534)
Browse filesCo-authored-by: Jeremiah Lowin <jlowin@users.noreply.github.com>
Co-authored-by: marvin-context-protocol[bot] <225465937+marvin-context-protocol[bot]@users.noreply.github.com>
Files changed (5)
79 unmodified lines80818283848579 unmodified lineslogger.warning(f"Failed to get prompts from server: {mounted.server.name!r}, mounted at: {mounted.prefix!r}: {e}")continue# Finally, add local prompts, which always take precedence79 unmodified lines808182838485868779 unmodified lineslogger.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 precedence
113 unmodified lines11411511611711811945 unmodified lines165166167168169170113 unmodified lineslogger.warning(f"Failed to get resources from server: {mounted.server.name!r}, mounted at: {mounted.prefix!r}: {e}")continue# Finally, add local resources, which always take precedence45 unmodified lineslogger.warning(f"Failed to get templates from server: {mounted.server.name!r}, mounted at: {mounted.prefix!r}: {e}")continue# Finally, add local templates, which always take precedence113 unmodified lines11411511611711811912012145 unmodified lines167168169170171172173174113 unmodified lineslogger.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 precedence45 unmodified lineslogger.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 precedence
319 unmodified lines320321322323324325319 unmodified lines),] = Truedef __getattr__(name: str):"""319 unmodified lines320321322323324325326327328329330331332333334335336337338339319 unmodified lines),] = Truemounted_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 debuggingbut will interrupt normal operation."""),),] = Falsedef __getattr__(name: str):"""
85 unmodified lines86878889909185 unmodified lineslogger.warning(f"Failed to get tools from server: {mounted.server.name!r}, mounted at: {mounted.prefix!r}: {e}")continue# Finally, add local tools, which always take precedence85 unmodified lines868788899091929385 unmodified lineslogger.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 precedence
12 unmodified lines13141516171819976 unmodified lines99699799812 unmodified linesfrom fastmcp.tools import FunctionTool, ToolManagerfrom fastmcp.tools.tool import Toolfrom fastmcp.tools.tool_transform import ArgTransformConfig, ToolTransformConfigfrom fastmcp.utilities.tests import caplog_for_fastmcpfrom fastmcp.utilities.types import Image976 unmodified lines# 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)12 unmodified lines13141516171819976 unmodified lines9969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103712 unmodified linesfrom fastmcp.tools import FunctionTool, ToolManagerfrom fastmcp.tools.tool import Toolfrom fastmcp.tools.tool_transform import ArgTransformConfig, ToolTransformConfigfrom fastmcp.utilities.tests import caplog_for_fastmcp, temporary_settingsfrom fastmcp.utilities.types import Image976 unmodified lines# 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 Falseassert fastmcp.settings.mounted_components_raise_on_load_error is Falseparent_mcp = FastMCP("ParentServer")child_mcp = FastMCP("FailingChildServer")# Create a failing mounted server by corrupting itparent_mcp.mount(child_mcp, prefix="child")# Corrupt the child server to make it fail during tool loadingchild_mcp._tool_manager._mounted_servers.append("invalid") # type: ignore# Should not raise, just warntools = await parent_mcp._tool_manager.list_tools()assert isinstance(tools, list) # Should return empty list, not raiseasync 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 serverparent_mcp.mount(child_mcp, prefix="child")# Corrupt the child server to make it fail during tool loadingchild_mcp._tool_manager._mounted_servers.append("invalid") # type: ignore# Use temporary settings context managerwith temporary_settings(mounted_components_raise_on_load_error=True):# Should raise the exceptionwith pytest.raises(AttributeError, match=""):await parent_mcp._tool_manager.list_tools()
src/fastmcp/prompts/prompt_manager.py+2
src/fastmcp/resources/resource_manager.py+4
src/fastmcp/settings.py+14
src/fastmcp/tools/tool_manager.py+2
tests/tools/test_tool_manager.py+40-1