Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/time/src/mcp_server_time/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
10 changes: 7 additions & 3 deletions src/time/test/time_server_test.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)


Expand Down
Loading