From a46a476fe98c295fc6c5c5ab22ebcf9ba516352f Mon Sep 17 00:00:00 2001 From: Jeremiah Lowin <153965+jlowin@users.noreply.github.com> Date: Tue, 23 Dec 2025 18:25:25 -0500 Subject: [PATCH] Let FastMCPError propagate unchanged from managers --- src/fastmcp/prompts/prompt_manager.py | 7 +++---- src/fastmcp/resources/resource_manager.py | 23 +++++++++-------------- src/fastmcp/tools/tool_manager.py | 12 +++++------- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/fastmcp/prompts/prompt_manager.py b/src/fastmcp/prompts/prompt_manager.py index 1563a1831f..e66c1e8783 100644 --- a/src/fastmcp/prompts/prompt_manager.py +++ b/src/fastmcp/prompts/prompt_manager.py @@ -7,7 +7,7 @@ from mcp import GetPromptResult from fastmcp import settings -from fastmcp.exceptions import NotFoundError, PromptError +from fastmcp.exceptions import FastMCPError, NotFoundError, PromptError from fastmcp.prompts.prompt import FunctionPrompt, Prompt, PromptResult from fastmcp.settings import DuplicateBehavior from fastmcp.utilities.logging import get_logger @@ -107,9 +107,8 @@ async def render_prompt( try: messages = await prompt.render(arguments) return GetPromptResult(description=prompt.description, messages=messages) - except PromptError as e: - logger.exception(f"Error rendering prompt {name!r}") - raise e + except FastMCPError: + raise except Exception as e: logger.exception(f"Error rendering prompt {name!r}") if self.mask_error_details: diff --git a/src/fastmcp/resources/resource_manager.py b/src/fastmcp/resources/resource_manager.py index 7367fb3e9c..28416a692a 100644 --- a/src/fastmcp/resources/resource_manager.py +++ b/src/fastmcp/resources/resource_manager.py @@ -10,7 +10,7 @@ from pydantic import AnyUrl from fastmcp import settings -from fastmcp.exceptions import NotFoundError, ResourceError +from fastmcp.exceptions import FastMCPError, NotFoundError, ResourceError from fastmcp.resources.resource import Resource from fastmcp.resources.template import ( ResourceTemplate, @@ -268,10 +268,9 @@ async def get_resource(self, uri: AnyUrl | str) -> Resource: uri_str, params=params, ) - # Pass through ResourceErrors as-is - except ResourceError as e: - logger.error(f"Error creating resource from template: {e}") - raise e + # Pass through FastMCPErrors as-is + except FastMCPError: + raise # Handle other exceptions except Exception as e: logger.error(f"Error creating resource from template: {e}") @@ -299,10 +298,9 @@ async def read_resource(self, uri: AnyUrl | str) -> str | bytes: try: return await resource.read() - # raise ResourceErrors as-is - except ResourceError as e: - logger.exception(f"Error reading resource {uri_str!r}") - raise e + # raise FastMCPErrors as-is + except FastMCPError: + raise # Handle other exceptions except Exception as e: @@ -322,11 +320,8 @@ async def read_resource(self, uri: AnyUrl | str) -> str | bytes: try: resource = await template.create_resource(uri_str, params=params) return await resource.read() - except ResourceError as e: - logger.exception( - f"Error reading resource from template {uri_str!r}" - ) - raise e + except FastMCPError: + raise except Exception as e: logger.exception( f"Error reading resource from template {uri_str!r}" diff --git a/src/fastmcp/tools/tool_manager.py b/src/fastmcp/tools/tool_manager.py index 9c6ee88604..e1d1f5c311 100644 --- a/src/fastmcp/tools/tool_manager.py +++ b/src/fastmcp/tools/tool_manager.py @@ -8,7 +8,7 @@ from pydantic import ValidationError from fastmcp import settings -from fastmcp.exceptions import NotFoundError, ToolError +from fastmcp.exceptions import FastMCPError, NotFoundError, ToolError from fastmcp.settings import DuplicateBehavior from fastmcp.tools.tool import Tool, ToolResult from fastmcp.tools.tool_transform import ( @@ -158,12 +158,10 @@ async def call_tool(self, key: str, arguments: dict[str, Any]) -> ToolResult: tool = await self.get_tool(key) try: return await tool.run(arguments) - except ValidationError as e: - logger.exception(f"Error validating tool {key!r}: {e}") - raise e - except ToolError as e: - logger.exception(f"Error calling tool {key!r}") - raise e + except FastMCPError: + raise + except ValidationError: + raise except Exception as e: logger.exception(f"Error calling tool {key!r}") if self.mask_error_details: