Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136"""Tests for distinguishing argument-validation errors from tool-body errors.
See https://github.com/PrefectHQ/fastmcp/issues/4128: a bad call (invalidarguments) should surface as fastmcp's ``ValidationError`` so downstream errortaxonomy (middleware, Sentry filters) can treat it as a client error, while a``pydantic.ValidationError`` raised by the tool's own body is a server-side bugand must not be reclassified as a bad call."""
from typing import Annotated, Any
import pytestfrom pydantic import BaseModel, Fieldfrom pydantic import ValidationError as PydanticValidationError
from fastmcp.exceptions import ValidationErrorfrom fastmcp.tools.base import Toolfrom fastmcp.tools.function_tool import _ToolBodyError
class _Inner(BaseModel): x: int
class TestArgumentValidationErrors: """Invalid arguments are converted to fastmcp's ValidationError."""
async def test_async_constraint_violation(self): async def tool_fn(n: Annotated[int, Field(le=10)]) -> int: return n
tool = Tool.from_function(tool_fn) with pytest.raises(ValidationError): await tool.run({"n": 20})
async def test_sync_constraint_violation(self): def tool_fn(n: Annotated[int, Field(le=10)]) -> int: return n
tool = Tool.from_function(tool_fn) with pytest.raises(ValidationError): await tool.run({"n": 20})
async def test_wrong_type_is_argument_error(self): async def tool_fn(n: int) -> int: return n
tool = Tool.from_function(tool_fn) with pytest.raises(ValidationError): await tool.run({"n": "not-an-int"})
async def test_missing_required_argument(self): async def tool_fn(n: int) -> int: return n
tool = Tool.from_function(tool_fn) with pytest.raises(ValidationError): await tool.run({})
class TestToolBodyErrors: """A pydantic error raised by the body is NOT reclassified as a bad call."""
async def test_async_body_pydantic_error_propagates(self): async def tool_fn(data: str) -> int: bad_value: Any = "not-an-int" _Inner(x=bad_value) # raises a pydantic ValidationError from the body return 1
tool = Tool.from_function(tool_fn) with pytest.raises(_ToolBodyError) as exc_info: await tool.run({"data": "valid"}) # It must not be fastmcp's ValidationError, and the original is chained. assert not isinstance(exc_info.value, ValidationError) assert isinstance(exc_info.value.__cause__, PydanticValidationError)
async def test_sync_body_pydantic_error_propagates(self): def tool_fn(data: str) -> int: bad_value: Any = "not-an-int" _Inner(x=bad_value) # raises a pydantic ValidationError from the body return 1
tool = Tool.from_function(tool_fn) with pytest.raises(_ToolBodyError) as exc_info: await tool.run({"data": "valid"}) assert isinstance(exc_info.value.__cause__, PydanticValidationError)
class TestTaskArgumentValidation: """The task-execution path (coerce_task_arguments) converts arg errors too.
The coercion logic moved to ``fastmcp_tasks.components`` during the SEP-1686 -> SEP-2663 migration, keyed by component type instead of being a method on the component. """
def test_coerce_task_arguments_wrong_type(self): from fastmcp_tasks.components import coerce_task_arguments
def tool_fn(n: int) -> int: return n
tool = Tool.from_function(tool_fn) with pytest.raises(ValidationError): coerce_task_arguments(tool, {"n": "not-an-int"})
def test_coerce_task_arguments_constraint_violation(self): from fastmcp_tasks.components import coerce_task_arguments
def tool_fn(n: Annotated[int, Field(le=10)]) -> int: return n
tool = Tool.from_function(tool_fn) with pytest.raises(ValidationError): coerce_task_arguments(tool, {"n": 20})
class TestValidCallsStillWork: """Regression: the happy path is unaffected."""
async def test_async_valid_call(self): async def tool_fn(n: Annotated[int, Field(le=10)]) -> int: return n * 2
tool = Tool.from_function(tool_fn) result = await tool.run({"n": 5}) assert result.structured_content == {"result": 10}
async def test_sync_valid_call(self): def tool_fn(n: int) -> int: return n + 1
tool = Tool.from_function(tool_fn) result = await tool.run({"n": 5}) assert result.structured_content == {"result": 6}