Skip to content

fix(time): use McpError for proper JSON-RPC error propagation#4483

Open
isheng-eqi wants to merge 2 commits into
modelcontextprotocol:mainfrom
isheng-eqi:fix/time-error-propagation
Open

fix(time): use McpError for proper JSON-RPC error propagation#4483
isheng-eqi wants to merge 2 commits into
modelcontextprotocol:mainfrom
isheng-eqi:fix/time-error-propagation

Conversation

@isheng-eqi

Copy link
Copy Markdown

Summary

Two fixes in the time server for proper MCP protocol error handling.

Problem

  1. call_tool handler raised ValueError instead of McpError (line 216). The MCP protocol requires errors to be sent as JSON-RPC error responses via McpError. A bare ValueError gets caught by the SDK as a generic internal error, losing the error code and context.

  2. get_zoneinfo caught all exceptions generically (line 56). ZoneInfo("invalid") raises KeyError while ZoneInfo(bad_object) raises TypeError — the function gave the same "Invalid timezone" message for both, making debugging harder.

Changes

  • src/time/src/mcp_server_time/server.py (+4/-2):
    • get_zoneinfo: separate KeyError (unknown timezone name) from generic exceptions (invalid format), with distinct messages
    • call_tool handler: ValueErrorMcpError(ErrorData(code=INVALID_PARAMS, ...)) for correct JSON-RPC error propagation

Two fixes in the time server:

1. call_tool handler (line 216): Was raising plain ValueError instead of
   McpError. The MCP protocol requires JSON-RPC errors via McpError with
   proper error codes. A ValueError propagates as a generic internal error
   to the client, losing the error context.

2. get_zoneinfo (line 56): Catch KeyError specifically for unknown
   timezones vs. generic exceptions for invalid timezone strings. Gives
   the client more precise error messages ('Unknown timezone' vs
   'Invalid timezone').
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.

1 participant