fix: check the cause of the tool error#2674
Conversation
WalkthroughThe error handling middleware was modified to inspect the underlying exception cause when determining the error type for MCP transformation. The change examines Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/fastmcp/server/middleware/error_handling.py (1)
90-90: LGTM! The fix correctly addresses the error transformation issue.The change properly inspects
error.__cause__to determine the error type for classification, which resolves the issue where wrapped exceptions (e.g.,ValueErrorwrapped inToolError) were being misclassified. This enables the middleware to map the underlying exception to the appropriate MCP error code.Optional consideration for future enhancement:
The error messages still use
str(error)(lines 94, 98, 102, 107, 111) rather thanstr(error.__cause__). This means when classifying based on the cause, the displayed message comes from the wrapper exception. This may be intentional to preserve tool context, but it's worth verifying this is the desired behavior.Additionally, the implementation only inspects one level of
__cause__. If there are deeper exception chains, the root cause won't be reached. For the current use case this appears sufficient.
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
tests/server/middleware/test_error_handling.pyis excluded by none and included by none
📒 Files selected for processing (1)
src/fastmcp/server/middleware/error_handling.py
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py
📄 CodeRabbit inference engine (AGENTS.md)
**/*.py: Use Python ≥ 3.10 with full type annotations
Never use bareexcept- be specific with exception types
Files:
src/fastmcp/server/middleware/error_handling.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: Run tests: Python 3.10 on windows-latest
- GitHub Check: Run tests: Python 3.13 on ubuntu-latest
- GitHub Check: Run tests with lowest-direct dependencies
- GitHub Check: Run tests: Python 3.10 on ubuntu-latest
Description
This PR updates
ErrorHandlingMiddleware._transform_errorto check theException.__cause__when ToolErrors are raised. The server re-raisesExceptionsasToolErrorswhen executing the tools; the middleware was not transforming the errors correctly since its expecting the__cause__of the tool errors.Contributors Checklist
Review Checklist