fix: align Context logging methods with MCP spec data type (#2366)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Max Isbey <224885523+maxisbey@users.noreply.github.com>
This commit is contained in:
Gyeongjun Paik (Kent) 2026-04-15 06:41:51 +09:00 • committed by GitHub
commit 3d7b311de0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,4 +1,4 @@
from typing import Any, Literal
from typing import Literal
import pytest
@ -36,24 +36,20 @@ async def test_logging_callback():
message: str, level: Literal["debug", "info", "warning", "error"], logger: str, ctx: Context
) -> bool:
"""Send a log notification to the client."""
await ctx.log(level=level, message=message, logger_name=logger)
await ctx.log(level=level, data=message, logger_name=logger)
return True
@server.tool("test_tool_with_log_extra")
async def test_tool_with_log_extra(
message: str,
@server.tool("test_tool_with_log_dict")
async def test_tool_with_log_dict(
level: Literal["debug", "info", "warning", "error"],
logger: str,
extra_string: str,
extra_dict: dict[str, Any],
ctx: Context,
) -> bool:
"""Send a log notification to the client with extra fields."""
"""Send a log notification with a dict payload."""
await ctx.log(
level=level,
message=message,
data={"message": "Test log message", "extra_string": "example", "extra_dict": {"a": 1, "b": 2, "c": 3}},
logger_name=logger,
extra={"extra_string": extra_string, "extra_dict": extra_dict},
)
return True
@ -84,18 +80,15 @@ async def test_logging_callback():
"logger": "test_logger",
},
)
log_result_with_extra = await client.call_tool(
"test_tool_with_log_extra",
log_result_with_dict = await client.call_tool(
"test_tool_with_log_dict",
{
"message": "Test log message",
"level": "info",
"logger": "test_logger",
"extra_string": "example",
"extra_dict": {"a": 1, "b": 2, "c": 3},
},
)
assert log_result.is_error is False
assert log_result_with_extra.is_error is False
assert log_result_with_dict.is_error is False
assert len(logging_collector.log_messages) == 2
# Create meta object with related_request_id added dynamically
log = logging_collector.log_messages[0]
@ -103,10 +96,10 @@ async def test_logging_callback():
assert log.logger == "test_logger"
assert log.data == "Test log message"
log_with_extra = logging_collector.log_messages[1]
assert log_with_extra.level == "info"
assert log_with_extra.logger == "test_logger"
assert log_with_extra.data == {
log_with_dict = logging_collector.log_messages[1]
assert log_with_dict.level == "info"
assert log_with_dict.logger == "test_logger"
assert log_with_dict.data == {
"message": "Test log message",
"extra_string": "example",
"extra_dict": {"a": 1, "b": 2, "c": 3},