Skip to content

Commit a4446d8

Browse files
committed
fix(time): use McpError for proper 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').
1 parent d31124c commit a4446d8

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

src/time/src/mcp_server_time/server.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,10 @@ def get_local_tz(local_tz_override: str | None = None) -> ZoneInfo:
5353
def get_zoneinfo(timezone_name: str) -> ZoneInfo:
5454
try:
5555
return ZoneInfo(timezone_name)
56+
except KeyError:
57+
raise McpError(ErrorData(code=INVALID_PARAMS, message=f"Unknown timezone: {timezone_name}"))
5658
except Exception as e:
57-
raise McpError(ErrorData(code=INVALID_PARAMS, message=f"Invalid timezone: {str(e)}"))
59+
raise McpError(ErrorData(code=INVALID_PARAMS, message=f"Invalid timezone '{timezone_name}': {e}"))
5860

5961

6062
class TimeServer:
@@ -213,7 +215,7 @@ async def call_tool(
213215
]
214216

215217
except Exception as e:
216-
raise ValueError(f"Error processing mcp-server-time query: {str(e)}")
218+
raise McpError(ErrorData(code=INVALID_PARAMS, message=f"Error processing time query: {str(e)}"))
217219

218220
options = server.create_initialization_options()
219221
async with stdio_server() as (read_stream, write_stream):

0 commit comments

Comments
 (0)