Skip to content

fix(time): preserve McpError INVALID_PARAMS code in call_tool error handler#4279

Open
basilalshukaili wants to merge 1 commit into
modelcontextprotocol:mainfrom
basilalshukaili:fix/time-preserve-mcp-error-on-invalid-timezone
Open

fix(time): preserve McpError INVALID_PARAMS code in call_tool error handler#4279
basilalshukaili wants to merge 1 commit into
modelcontextprotocol:mainfrom
basilalshukaili:fix/time-preserve-mcp-error-on-invalid-timezone

Conversation

@basilalshukaili
Copy link
Copy Markdown

Summary

call_tool in src/time/src/mcp_server_time/server.py wraps its entire body in try … except Exception as e: raise ValueError(…). McpError (raised by get_zoneinfo for unknown timezone names) is a subclass of Exception, so it was silently caught and re-raised as a plain ValueError. This discarded the structured INVALID_PARAMS error 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: raise guard before the broad handler so McpError propagates to the transport layer with its code intact:

except McpError:
    raise           # preserve INVALID_PARAMS / structured error code
except Exception as e:
    raise ValueError(f"Error processing mcp-server-time query: {str(e)}")

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 McpError carries INVALID_PARAMS when an invalid timezone is supplied to get_current_time and both positions of convert_time. These tests fail on the previous code (a ValueError is raised instead of McpError).

uv run pytest src/time/test/time_server_test.py  # 41 passed

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants