fix(time): preserve McpError INVALID_PARAMS code in call_tool error handler#4279
Open
basilalshukaili wants to merge 1 commit into
Conversation
…e is preserved The call_tool handler had a broad `except Exception as e` block that caught `McpError` (raised by `get_zoneinfo` for unknown timezone names) and re-raised it as a plain `ValueError`. This silently discarded the structured INVALID_PARAMS error code that the MCP framework uses to distinguish client errors from server errors. Fix: add an explicit `except McpError: raise` guard before the broad handler so McpError propagates to the MCP transport layer with its code intact. Also fix a minor description typo: 'in a specific timezones' -> 'in a specific timezone'. Tests: add three focused assertions verifying that McpError carries INVALID_PARAMS when an invalid timezone is supplied to both tools.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
call_toolinsrc/time/src/mcp_server_time/server.pywraps its entire body intry … except Exception as e: raise ValueError(…).McpError(raised byget_zoneinfofor unknown timezone names) is a subclass ofException, so it was silently caught and re-raised as a plainValueError. This discarded the structuredINVALID_PARAMSerror code that the MCP transport layer uses to distinguish client errors from server faults — causing MCP clients that inspect error codes to see an opaque server error instead of a proper "invalid argument" response.Fix
Add an explicit
except McpError: raiseguard before the broad handler soMcpErrorpropagates to the transport layer with its code intact:Also fixes a minor description typo:
"Get current time in a specific timezones"→"Get current time in a specific timezone".Tests
Added three assertions that verify
McpErrorcarriesINVALID_PARAMSwhen an invalid timezone is supplied toget_current_timeand both positions ofconvert_time. These tests fail on the previous code (aValueErroris raised instead ofMcpError).