From 58023ccfbcbd9dabe2c1da850d4789d52cdcbc13 Mon Sep 17 00:00:00 2001 From: Anshu Date: Wed, 29 Jul 2026 15:35:58 +0530 Subject: [PATCH] Fix time server MCP error compatibility --- src/time/src/mcp_server_time/server.py | 12 ++++++++++-- src/time/test/time_server_test.py | 10 +++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/time/src/mcp_server_time/server.py b/src/time/src/mcp_server_time/server.py index 2cb0926134..9ab9eee33a 100644 --- a/src/time/src/mcp_server_time/server.py +++ b/src/time/src/mcp_server_time/server.py @@ -9,7 +9,11 @@ from mcp.server import Server from mcp.server.stdio import stdio_server from mcp.types import Tool, ToolAnnotations, TextContent, ImageContent, EmbeddedResource, ErrorData, INVALID_PARAMS -from mcp.shared.exceptions import McpError +try: + from mcp.shared.exceptions import MCPError +except ImportError: + # Fallback for older versions of the mcp SDK + from mcp.shared.exceptions import McpError as MCPError from pydantic import BaseModel @@ -54,7 +58,11 @@ def get_zoneinfo(timezone_name: str) -> ZoneInfo: try: return ZoneInfo(timezone_name) except Exception as e: - raise McpError(ErrorData(code=INVALID_PARAMS, message=f"Invalid timezone: {str(e)}")) + error_data = ErrorData(code=INVALID_PARAMS, message=f"Invalid timezone: {str(e)}") + try: + raise MCPError(INVALID_PARAMS, error_data.message, error_data) + except TypeError: + raise MCPError(error_data) class TimeServer: diff --git a/src/time/test/time_server_test.py b/src/time/test/time_server_test.py index 8d963508d7..da9e269000 100644 --- a/src/time/test/time_server_test.py +++ b/src/time/test/time_server_test.py @@ -1,10 +1,14 @@ from freezegun import freeze_time -from mcp.shared.exceptions import McpError import pytest from unittest.mock import patch from zoneinfo import ZoneInfo +try: + from mcp.shared.exceptions import MCPError +except ImportError: + from mcp.shared.exceptions import McpError as MCPError + from mcp_server_time.server import TimeServer, get_local_tz @@ -85,7 +89,7 @@ def test_get_current_time(test_time, timezone, expected): def test_get_current_time_with_invalid_timezone(): time_server = TimeServer() with pytest.raises( - McpError, + MCPError, match=r"Invalid timezone: 'No time zone found with key Invalid/Timezone'", ): time_server.get_current_time("Invalid/Timezone") @@ -116,7 +120,7 @@ def test_get_current_time_with_invalid_timezone(): ) def test_convert_time_errors(source_tz, time_str, target_tz, expected_error): time_server = TimeServer() - with pytest.raises((McpError, ValueError), match=expected_error): + with pytest.raises((MCPError, ValueError), match=expected_error): time_server.convert_time(source_tz, time_str, target_tz)