π The fast, Pythonic way to build MCP servers and clients.
fix: drop exc_info for expected tool failures, remove unreachable ValidationError (#4029)
sergeykad28722f846a03
diff --git a/src/fastmcp/server/server.py b/src/fastmcp/server/server.pyindex 6c7277605..190e76d67 100644--- a/src/fastmcp/server/server.py+++ b/src/fastmcp/server/server.py@@ -55,7 +55,6 @@ from fastmcp.exceptions import (PromptError,ResourceError,ToolError,- ValidationError,)from fastmcp.mcp_config import MCPConfigfrom fastmcp.prompts import Prompt@@ -1275,11 +1274,16 @@ class FastMCP(return await tool._run(arguments or {}, task_meta=task_meta)except FastMCPError as e:logger.log(- e.log_level, f"Error calling tool {name!r}", exc_info=True+ e.log_level, f"Error calling tool {name!r}", exc_info=False)raise- except (ValidationError, PydanticValidationError):- logger.exception(f"Error validating tool {name!r}")+ except PydanticValidationError as e:+ # fastmcp's own ValidationError is a FastMCPError, already handled above.+ logger.warning(+ "Invalid arguments for tool %r: %s",+ name,+ e.errors(include_url=False),+ )raiseexcept Exception as e:logger.exception(f"Error calling tool {name!r}")diff --git a/tests/server/test_input_validation.py b/tests/server/test_input_validation.pyindex 60456d0c3..264e2bd70 100644--- a/tests/server/test_input_validation.py+++ b/tests/server/test_input_validation.py@@ -349,3 +349,71 @@ class TestEdgeCases:result = await client.call_tool("sum_numbers", {"numbers": ["1", "2", "3"]})assert isinstance(result.content[0], TextContent)assert result.content[0].text == "6"+++class TestExpectedToolFailureLogging:+ async def test_validation_error_logs_warning_without_traceback(self, caplog):+ mcp = FastMCP("TestServer", strict_input_validation=False)++ @mcp.tool+ def create_user(profile: UserProfile) -> str:+ return profile.name++ with caplog.at_level("DEBUG", logger="fastmcp.server.server"):+ async with Client(mcp) as client:+ with pytest.raises(ToolError):+ await client.call_tool(+ "create_user",+ {"profile": {"name": "x", "age": "nope", "email": "e"}},+ )++ records = [+ r for r in caplog.records if "Invalid arguments for tool" in r.getMessage()+ ]+ assert records, "expected a single 'Invalid arguments' warning"+ assert records[0].levelname == "WARNING"+ assert records[0].exc_info is None+ assert "int_parsing" in records[0].getMessage()+ assert "errors.pydantic.dev" not in records[0].getMessage()++ async def test_tool_raised_tool_error_logs_without_traceback(self, caplog):+ mcp = FastMCP("TestServer")++ @mcp.tool+ def do_thing() -> str:+ raise ToolError("structured failure payload")++ with caplog.at_level("DEBUG", logger="fastmcp.server.server"):+ async with Client(mcp) as client:+ with pytest.raises(ToolError):+ await client.call_tool("do_thing", {})++ records = [+ r+ for r in caplog.records+ if r.getMessage() == "Error calling tool 'do_thing'"+ ]+ assert records, "expected an 'Error calling tool' log without traceback"+ assert records[0].levelname == "ERROR"+ assert not records[0].exc_info++ async def test_unexpected_exception_still_logs_with_traceback(self, caplog):+ mcp = FastMCP("TestServer")++ @mcp.tool+ def do_thing() -> str:+ raise RuntimeError("actual bug")++ with caplog.at_level("DEBUG", logger="fastmcp.server.server"):+ async with Client(mcp) as client:+ with pytest.raises(ToolError):+ await client.call_tool("do_thing", {})++ records = [+ r+ for r in caplog.records+ if r.getMessage() == "Error calling tool 'do_thing'"+ ]+ assert records, "expected an 'Error calling tool' exception log"+ assert records[0].levelname == "ERROR"+ assert records[0].exc_info is not None