Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821"""Tests for input validation behavior with strict_input_validation setting.
This module tests the difference between strict JSON schema validation (whenstrict_input_validation=True) and Pydantic-based coercion (whenstrict_input_validation=False, the default)."""
import datetimeimport enumimport jsonimport uuidfrom dataclasses import dataclassfrom typing import Annotated, Any
import httpx2import pytestfrom mcp.shared.exceptions import MCPErrorfrom mcp_types import TextContentfrom pydantic import ( BaseModel, ConfigDict, Field, SecretStr, StrictInt, field_validator,)from pydantic_core import PydanticCustomError
from fastmcp import Client, FastMCPfrom fastmcp.exceptions import ToolError, ValidationErrorfrom fastmcp.server.middleware import Middlewarefrom fastmcp.server.transforms import ToolTransformfrom fastmcp.tools.base import Tool, ToolResultfrom fastmcp.tools.tool_transform import ToolTransformConfig
class UserProfile(BaseModel): """A test model for validating Pydantic model arguments."""
name: str age: int email: str
class PrivatePayload(BaseModel): model_config = ConfigDict(extra="forbid")
token: str values: dict[str, int] = Field(default_factory=dict)
@field_validator("token") @classmethod def reject_private_token(cls, value: str) -> str: # Simulate a validator that includes the rejected input in its message. if value != "ok": raise ValueError(f"Rejected token: {value}") return value
class RawValidationTool(Tool): async def run(self, arguments: dict[str, Any]) -> ToolResult: PrivatePayload.model_validate(arguments) return ToolResult(content="ok")
class TestStringToIntegerCoercion: """Test string-to-integer coercion behavior."""
async def test_string_integer_with_strict_validation(self): """With strict validation, string integers should raise an error.""" mcp = FastMCP("TestServer", strict_input_validation=True)
@mcp.tool def add_numbers(a: int, b: int) -> int: """Add two numbers together.""" return a + b
async with Client(mcp) as client: # String integers should fail with strict validation with pytest.raises(Exception) as exc_info: await client.call_tool("add_numbers", {"a": "10", "b": "20"})
# Verify it's a validation error error_msg = str(exc_info.value).lower() assert ( "validation" in error_msg or "invalid" in error_msg or "type" in error_msg )
async def test_string_integer_without_strict_validation(self): """Without strict validation, string integers should be coerced.""" mcp = FastMCP("TestServer", strict_input_validation=False)
@mcp.tool def add_numbers(a: int, b: int) -> int: """Add two numbers together.""" return a + b
async with Client(mcp) as client: # String integers should be coerced to integers result = await client.call_tool("add_numbers", {"a": "10", "b": "20"}) assert isinstance(result.content[0], TextContent) assert result.content[0].text == "30"
async def test_default_is_not_strict(self): """By default, strict_input_validation should be False.""" mcp = FastMCP("TestServer")
@mcp.tool def multiply(x: int, y: int) -> int: """Multiply two numbers.""" return x * y
async with Client(mcp) as client: # Should work with string integers by default result = await client.call_tool("multiply", {"x": "5", "y": "3"}) assert isinstance(result.content[0], TextContent) assert result.content[0].text == "15"
async def test_string_float_coercion(self): """Test that string floats are also coerced.""" mcp = FastMCP("TestServer", strict_input_validation=False)
@mcp.tool def calculate_area(length: float, width: float) -> float: """Calculate rectangle area.""" return length * width
async with Client(mcp) as client: result = await client.call_tool( "calculate_area", {"length": "10.5", "width": "20.0"} ) assert isinstance(result.content[0], TextContent) assert result.content[0].text == "210.0"
async def test_invalid_coercion_still_fails(self): """Even without strict validation, truly invalid inputs should fail.""" mcp = FastMCP("TestServer", strict_input_validation=False)
@mcp.tool def square(n: int) -> int: """Square a number.""" return n * n
async with Client(mcp) as client: # Non-numeric strings should still fail with pytest.raises(Exception): await client.call_tool("square", {"n": "not-a-number"})
class TestPydanticModelArguments: """Test validation of Pydantic model arguments."""
async def test_pydantic_model_with_dict_no_strict(self): """Pydantic models should accept dict arguments without strict validation.""" mcp = FastMCP("TestServer", strict_input_validation=False)
@mcp.tool def create_user(profile: UserProfile) -> str: """Create a user from a profile.""" return f"Created user {profile.name}, age {profile.age}"
async with Client(mcp) as client: result = await client.call_tool( "create_user", {"profile": {"name": "Alice", "age": 30, "email": "alice@example.com"}}, ) assert isinstance(result.content[0], TextContent) assert "Alice" in result.content[0].text assert "30" in result.content[0].text
async def test_stringified_json_not_auto_parsed_for_pydantic_models(self): """Stringified JSON is rejected for Pydantic model parameters.
Some LLM clients send stringified JSON (a JSON string containing a JSON object) instead of a proper JSON object. FastMCP does not auto-parse these; callers get a validation error. """ mcp = FastMCP("TestServer", strict_input_validation=False)
@mcp.tool def create_user(profile: UserProfile) -> str: """Create a user from a profile.""" return f"Created user {profile.name}, age {profile.age}"
async with Client(mcp) as client: stringified = json.dumps( {"name": "Bob", "age": 25, "email": "bob@example.com"} )
with pytest.raises(ToolError, match="validation"): await client.call_tool("create_user", {"profile": stringified})
async def test_pydantic_model_with_coercion(self): """Pydantic models should benefit from coercion without strict validation.""" mcp = FastMCP("TestServer", strict_input_validation=False)
@mcp.tool def create_user(profile: UserProfile) -> str: """Create a user from a profile.""" return f"Created user {profile.name}, age {profile.age}"
async with Client(mcp) as client: # Age as string should be coerced result = await client.call_tool( "create_user", { "profile": { "name": "Charlie", "age": "35", # String instead of int "email": "charlie@example.com", } }, ) assert isinstance(result.content[0], TextContent) assert "Charlie" in result.content[0].text assert "35" in result.content[0].text
async def test_pydantic_model_strict_validation(self): """With strict validation, Pydantic models should enforce exact types.""" mcp = FastMCP("TestServer", strict_input_validation=True)
@mcp.tool def create_user(profile: UserProfile) -> str: """Create a user from a profile.""" return f"Created user {profile.name}, age {profile.age}"
async with Client(mcp) as client: # Age as string should fail with strict validation with pytest.raises(Exception): await client.call_tool( "create_user", { "profile": { "name": "Dave", "age": "40", # String instead of int "email": "dave@example.com", } }, )
class Color(enum.Enum): RED = "red"
@dataclassclass Point: x: int
class TestStrictModeAcceptsJsonValues: """Strict mode rejects lax coercion, not the JSON form of a declared type."""
@pytest.mark.parametrize( ("annotation", "value", "expected"), [ (Color, "red", Color.RED), (datetime.datetime, "2026-01-01T00:00:00", datetime.datetime(2026, 1, 1)), (datetime.date, "2026-01-01", datetime.date(2026, 1, 1)), ( uuid.UUID, "12345678-1234-5678-1234-567812345678", uuid.UUID("12345678-1234-5678-1234-567812345678"), ), (Point, {"x": 1}, Point(x=1)), (tuple[int, int], [1, 2], (1, 2)), (set[int], [1], {1}), (float, 1, 1.0), ], ) async def test_json_value_is_accepted(self, annotation, value, expected): mcp = FastMCP("TestServer", strict_input_validation=True) received = []
async def tool_fn(arg): received.append(arg) return "ok"
tool_fn.__annotations__["arg"] = annotation mcp.tool(tool_fn, name="check")
async with Client(mcp) as client: await client.call_tool("check", {"arg": value})
assert received == [expected]
async def test_lax_coercion_is_still_rejected(self): mcp = FastMCP("TestServer", strict_input_validation=True)
@mcp.tool def check(when: datetime.datetime, count: int) -> str: return "ok"
async with Client(mcp) as client: with pytest.raises(ToolError): await client.call_tool( "check", {"when": "2026-01-01T00:00:00", "count": "1"} ) with pytest.raises(ToolError): await client.call_tool("check", {"when": 1, "count": 1})
async def test_python_objects_are_accepted_when_called_directly(self): mcp = FastMCP("TestServer", strict_input_validation=True)
@mcp.tool def check(color: Color, when: datetime.datetime) -> str: return f"{color.value} {when.year}"
result = await mcp.call_tool( "check", {"color": Color.RED, "when": datetime.datetime(2026, 1, 1)} )
assert isinstance(result.content[0], TextContent) assert result.content[0].text == "red 2026"
async def test_python_objects_are_not_round_tripped_through_json(self): mcp = FastMCP("TestServer", strict_input_validation=True)
@mcp.tool def reveal(secret: SecretStr) -> str: return secret.get_secret_value()
result = await mcp.call_tool("reveal", {"secret": SecretStr("s3cret")})
assert isinstance(result.content[0], TextContent) assert result.content[0].text == "s3cret"
class TestFieldLevelStrictness: """Strictness declared on the parameter itself must survive lax server mode."""
async def test_strict_field_rejects_coercion(self): mcp = FastMCP("TestServer")
@mcp.tool def echo(value: Annotated[int, Field(strict=True)]) -> int: return value
async with Client(mcp) as client: with pytest.raises(ToolError): await client.call_tool("echo", {"value": "5"}) result = await client.call_tool("echo", {"value": 5}) assert isinstance(result.content[0], TextContent) assert result.content[0].text == "5"
async def test_strict_type_rejects_coercion(self): mcp = FastMCP("TestServer")
@mcp.tool def echo(value: StrictInt) -> int: return value
async with Client(mcp) as client: with pytest.raises(ToolError): await client.call_tool("echo", {"value": 5.0})
async def test_strict_model_config_rejects_coercion(self): class Payload(BaseModel): model_config = ConfigDict(strict=True) count: int
mcp = FastMCP("TestServer")
@mcp.tool def echo(payload: Payload) -> int: return payload.count
async with Client(mcp) as client: with pytest.raises(ToolError): await client.call_tool("echo", {"payload": {"count": "5"}})
async def test_lax_fields_still_coerce(self): mcp = FastMCP("TestServer")
@mcp.tool def echo(value: int) -> int: return value
async with Client(mcp) as client: result = await client.call_tool("echo", {"value": "5"}) assert isinstance(result.content[0], TextContent) assert result.content[0].text == "5"
class TestValidationErrorMessages: """Test the quality of validation error messages."""
async def test_error_message_quality_strict(self): """Capture error message with strict validation.""" mcp = FastMCP("TestServer", strict_input_validation=True)
@mcp.tool def process_data(count: int, name: str) -> str: """Process some data.""" return f"Processed {count} items for {name}"
async with Client(mcp) as client: with pytest.raises(Exception) as exc_info: await client.call_tool( "process_data", {"count": "not-a-number", "name": "test"} )
error_msg = str(exc_info.value) # Strict validation error message # Should mention validation or type error assert ( "validation" in error_msg.lower() or "invalid" in error_msg.lower() or "type" in error_msg.lower() )
async def test_error_message_quality_pydantic(self): """Capture error message with Pydantic validation.""" mcp = FastMCP("TestServer", strict_input_validation=False)
@mcp.tool def process_data(count: int, name: str) -> str: """Process some data.""" return f"Processed {count} items for {name}"
async with Client(mcp) as client: with pytest.raises(Exception) as exc_info: await client.call_tool( "process_data", {"count": "not-a-number", "name": "test"} )
error_msg = str(exc_info.value) # Pydantic validation error message # Should be more detailed and mention validation assert "validation" in error_msg.lower() or "invalid" in error_msg.lower()
async def test_missing_required_field_error(self): """Test error message for missing required fields.""" mcp = FastMCP("TestServer", strict_input_validation=False)
@mcp.tool def greet(name: str, age: int) -> str: """Greet a person.""" return f"Hello {name}, you are {age} years old"
async with Client(mcp) as client: with pytest.raises(Exception) as exc_info: # Missing 'age' parameter await client.call_tool("greet", {"name": "Alice"})
error_msg = str(exc_info.value) # Should mention the missing field assert "age" in error_msg.lower() or "required" in error_msg.lower()
class TestEdgeCases: """Test edge cases and boundary conditions."""
async def test_optional_parameters_with_coercion(self): """Optional parameters should work with coercion.""" mcp = FastMCP("TestServer", strict_input_validation=False)
@mcp.tool def format_message(text: str, repeat: int = 1) -> str: """Format a message with optional repetition.""" return text * repeat
async with Client(mcp) as client: # String for optional int parameter result = await client.call_tool( "format_message", {"text": "hi", "repeat": "3"} ) assert isinstance(result.content[0], TextContent) assert result.content[0].text == "hihihi"
async def test_none_values(self): """Test handling of None values.""" mcp = FastMCP("TestServer", strict_input_validation=False)
@mcp.tool def process_optional(value: int | None) -> str: """Process an optional value.""" return f"Value: {value}"
async with Client(mcp) as client: result = await client.call_tool("process_optional", {"value": None}) assert isinstance(result.content[0], TextContent) assert "None" in result.content[0].text
async def test_empty_string_to_int(self): """Empty strings should fail conversion to int.""" mcp = FastMCP("TestServer", strict_input_validation=False)
@mcp.tool def square(n: int) -> int: """Square a number.""" return n * n
async with Client(mcp) as client: with pytest.raises(Exception): await client.call_tool("square", {"n": ""})
async def test_boolean_coercion(self): """Test boolean value coercion.""" mcp = FastMCP("TestServer", strict_input_validation=False)
@mcp.tool def toggle(enabled: bool) -> str: """Toggle a feature.""" return f"Feature is {'enabled' if enabled else 'disabled'}"
async with Client(mcp) as client: # String "true" should be coerced to boolean result = await client.call_tool("toggle", {"enabled": "true"}) assert isinstance(result.content[0], TextContent) assert "enabled" in result.content[0].text.lower()
# String "false" should be coerced to boolean result = await client.call_tool("toggle", {"enabled": "false"}) assert isinstance(result.content[0], TextContent) assert "disabled" in result.content[0].text.lower()
async def test_list_of_integers_with_string_elements(self): """Test lists containing string representations of integers.""" mcp = FastMCP("TestServer", strict_input_validation=False)
@mcp.tool def sum_numbers(numbers: list[int]) -> int: """Sum a list of numbers.""" return sum(numbers)
async with Client(mcp) as client: # List with string integers 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: @pytest.mark.parametrize("mask_error_details", [False, True]) @pytest.mark.parametrize( "tool_path", ["direct", "transformed", "mounted", "middleware"] ) async def test_tool_body_validation_error_returns_error_result( self, mask_error_details: bool, tool_path: str, caplog ): class StoredRecord(BaseModel): content: str
mcp = FastMCP("TestServer", mask_error_details=mask_error_details)
@mcp.tool def load_record(record: dict[str, Any]) -> str: return StoredRecord.model_validate(record).content
tool_name = "load_record" server = mcp if tool_path == "transformed": mcp.add_transform( ToolTransform( {"load_record": ToolTransformConfig(name="load_transformed")} ) ) tool_name = "load_transformed" elif tool_path == "mounted": server = FastMCP("Parent", mask_error_details=mask_error_details) server.mount(mcp, namespace="child") tool_name = "child_load_record" elif tool_path == "middleware":
class Reroute(Middleware): async def on_call_tool(self, context, call_next): if context.message.name == "outer": return await context.fastmcp_context.fastmcp.call_tool( "load_record", {"record": {}} ) return await call_next(context)
@mcp.tool def outer() -> str: return "not reached"
mcp.add_middleware(Reroute()) tool_name = "outer"
with caplog.at_level("DEBUG"): async with Client(server) as client: result = await client.call_tool( tool_name, {"record": {}}, raise_on_error=False )
assert result.is_error assert ("content" in str(result.content)) is not mask_error_details assert not any( "Invalid arguments for tool" in record.getMessage() for record in caplog.records ) error_records = [ record for record in caplog.records if record.levelname == "ERROR" and record.getMessage().startswith("Error calling tool") and record.exc_info ] assert len(error_records) == 1
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 "'error_count': 1" in records[0].getMessage() assert "errors.pydantic.dev" not in records[0].getMessage()
@pytest.mark.parametrize( "arguments, error_type", [ ({"note": "PRIVATE_VALUE"}, "missing_argument"), ({"payload": ["PRIVATE_VALUE"]}, "model_type"), ( {"payload": {"token": "ok"}, "PRIVATE_KEY": 1}, "unexpected_keyword_argument", ), ({"payload": {"token": "ok", "PRIVATE_KEY": 1}}, "extra_forbidden"), ( {"payload": {"token": "ok", "values": {"PRIVATE_KEY": "bad"}}}, "int_parsing", ), ({"payload": {"token": "PRIVATE_TOKEN"}}, "value_error"), ], ids=[ "missing", "wrong-type", "extra", "nested-extra", "dict-key", "validator-message", ], ) async def test_validation_logs_omit_client_data( self, caplog, arguments, error_type ): mcp = FastMCP("TestServer")
@mcp.tool def submit(payload: PrivatePayload, note: str = "") -> str: return "ok"
with caplog.at_level("WARNING", logger="fastmcp.server.server"): async with Client(mcp) as client: result = await client.call_tool( "submit", arguments, raise_on_error=False )
assert result.is_error # The client still gets its detailed validation error. assert "PRIVATE" in str(result.content) records = [r for r in caplog.records if r.name == "fastmcp.server.server"] assert len(records) == 1 assert "PRIVATE" not in records[0].getMessage() assert error_type in records[0].getMessage() assert "'error_count': 1" in records[0].getMessage() assert records[0].exc_info is None
@pytest.mark.parametrize("raw_pydantic", [False, True]) async def test_other_validation_logs_omit_client_data(self, caplog, raw_pydantic): mcp = FastMCP("TestServer") if raw_pydantic: mcp.add_tool( RawValidationTool(name="submit", parameters={"type": "object"}) ) arguments = {"token": "PRIVATE_TOKEN"} else:
@mcp.tool def submit() -> str: raise ValidationError("PRIVATE_CUSTOM_ERROR")
arguments = {}
with caplog.at_level("WARNING", logger="fastmcp.server.server"): async with Client(mcp) as client: if raw_pydantic: with pytest.raises(MCPError, match="Invalid request parameters"): await client.call_tool( "submit", arguments, raise_on_error=False ) else: result = await client.call_tool( "submit", arguments, raise_on_error=False ) assert result.is_error assert "PRIVATE" in str(result.content)
records = [r for r in caplog.records if r.name == "fastmcp.server.server"] assert len(records) == 1 assert "PRIVATE" not in records[0].getMessage() if raw_pydantic: assert "value_error" in records[0].getMessage() else: assert records[0].getMessage() == "Invalid arguments for tool 'submit'" assert records[0].exc_info is None
async def test_custom_error_codes_are_not_logged(self, caplog): class Payload(BaseModel): values: list[str]
@field_validator("values") @classmethod def reject_values(cls, values: list[str]) -> list[str]: # Runtime validators can use input-derived codes despite the type hint. raise PydanticCustomError( values[0], # ty: ignore[invalid-argument-type] "Rejected {value}", {"value": values}, )
mcp = FastMCP("TestServer")
@mcp.tool def submit(payload: Payload, count: int) -> str: return "ok"
async with Client(mcp) as client: result = await client.call_tool( "submit", {"payload": {"values": ["PRIVATE_CODE"]}, "count": "PRIVATE_VALUE"}, raise_on_error=False, ) assert result.is_error assert "PRIVATE" in str(result.content) records = [r for r in caplog.records if r.name == "fastmcp.server.server"] assert len(records) == 1 assert "PRIVATE" not in records[0].getMessage() assert "'error_count': 2" in records[0].getMessage() assert "custom_error" in records[0].getMessage() assert "int_parsing" in records[0].getMessage() assert records[0].exc_info is None
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_upstream_http_status_error_logs_without_traceback(self, caplog): mcp = FastMCP("TestServer")
@mcp.tool def do_thing() -> str: request = httpx2.Request("GET", "https://api.example.test/items") response = httpx2.Response(404, request=request) raise httpx2.HTTPStatusError( "not found", request=request, response=response )
with caplog.at_level("DEBUG", logger="fastmcp.server.server"): async with Client(mcp) as client: with pytest.raises(ToolError, match="not found"): 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