Add httpx.HTTPStatusError error handling to tool run#1119
Add httpx.HTTPStatusError error handling to tool run#1119tmatthews90 wants to merge 3 commits intomodelcontextprotocol:mainfrom
Conversation
There was a problem hiding this comment.
Hi @tmatthews90 thank you for contributing to MCP! And apologies for the time it took to get back to you on this.
Having looked at the change and your motivating example, I think this is actually best handled by the application and not by the framework. FastMCP is transport and tool implementation agnostic , others might use requests or aiohttp for example. That's going to depend on the application and should therefore also be handled there in my opinion.
If you wanted to make your application code cleaner, you could for example create a decorator for your own tools that does this repeated error handling for you?
| except httpx.HTTPStatusError as e: | ||
| try: | ||
| error_detail = e.response.json() | ||
| except: | ||
| error_detail = e.response.text | ||
| raise ToolError(f"Error executing tool {self.name}: [{e.response.status_code}] {error_detail}") |
There was a problem hiding this comment.
I don't think we should be handling httpx specific errors in a framework that's agnostic of the transport - FastMCP might be using stdio after all, so this seems like the wrong place to handle this kind of error.
|
Closing this for the reasons discussed above - thanks again for the contribution! |
This change adds httpx.HTTPStatusError handling to the tool run class to provide detailed error information when returning from the MCP server.
Motivation and Context
The default error message from httpx does not have detailed information on the exception thrown. Our tools use httpx to make API calls and it would be much easier for us to
raise_for_statusand not have to catch the exception to format a better message to be rasied.Current tool implementation example
This is currently what we have to do to get a detailed exception message to the mcp tool run handler.
Ideal tool implementation
Current response formatIdeal responseHow Has This Been Tested?
Breaking Changes
Types of changes
Checklist
Additional context