fix(mcp): Improve validation errors and field aliases to reduce failed LLM tool calls (#38625)

This commit is contained in:
Kamil Gabryjelski
2026-03-13 11:16:50 +01:00
committed by GitHub
parent 56d6bb1913
commit d91b96814e
6 changed files with 56 additions and 15 deletions

View File

@@ -481,10 +481,12 @@ class TestParseRequestDecorator:
result = sync_tool('{"name": "test", "count": 5}', extra="data")
assert result == "test:5:data"
def test_decorator_raises_validation_error_async(self):
"""Should raise ValidationError for invalid data in async function."""
def test_decorator_raises_tool_error_for_invalid_data_async(self):
"""Should raise ToolError with field details for invalid data."""
from unittest.mock import MagicMock, patch
from fastmcp.exceptions import ToolError
@parse_request(self.RequestModel)
async def async_tool(request, ctx=None):
return f"{request.name}:{request.count}"
@@ -493,20 +495,22 @@ class TestParseRequestDecorator:
mock_ctx = MagicMock()
with patch("fastmcp.server.dependencies.get_context", return_value=mock_ctx):
with pytest.raises(ValidationError):
with pytest.raises(ToolError, match="Required fields for RequestModel"):
asyncio.run(async_tool('{"name": "test"}')) # Missing count
def test_decorator_raises_validation_error_sync(self):
"""Should raise ValidationError for invalid data in sync function."""
def test_decorator_raises_tool_error_for_invalid_data_sync(self):
"""Should raise ToolError with field details for invalid data."""
from unittest.mock import MagicMock, patch
from fastmcp.exceptions import ToolError
@parse_request(self.RequestModel)
def sync_tool(request, ctx=None):
return f"{request.name}:{request.count}"
mock_ctx = MagicMock()
with patch("fastmcp.server.dependencies.get_context", return_value=mock_ctx):
with pytest.raises(ValidationError):
with pytest.raises(ToolError, match="Required fields for RequestModel"):
sync_tool('{"name": "test"}') # Missing count
def test_decorator_with_complex_model_async(self):