Skip to content

Fix context logging methods to accept Any type per MCP spec#2427

Closed
Christian-Sidak wants to merge 1 commit intomodelcontextprotocol:mainfrom
Christian-Sidak:fix/context-logging-spec-compliance
Closed

Fix context logging methods to accept Any type per MCP spec#2427
Christian-Sidak wants to merge 1 commit intomodelcontextprotocol:mainfrom
Christian-Sidak:fix/context-logging-spec-compliance

Conversation

@Christian-Sidak
Copy link
Copy Markdown

Summary

Fixes #397

The MCP spec defines the logging notification data field as unknown (any JSON serializable type), but Context.log() and the convenience methods (debug, info, warning, error) only accepted str. This was inconsistent with both the spec and the existing ServerSession.send_log_message(data: Any) signature.

Changes:

  • Changed parameter from message: str to data: Any in Context.log() and all convenience methods
  • Removed the extra parameter, which was a workaround for the str-only limitation and caused confusion (calling with extra= kwargs raised TypeError since Context.log() did not accept them)
  • Updated tests to cover structured data logging (dicts, lists, numbers, None)

Before (broken):

# Type error - spec says this should work
ctx.info({"event": "user_login", "user_id": 123})

After (fixed):

# All JSON serializable types now accepted
ctx.info("simple string message")
ctx.info({"event": "user_login", "user_id": 123})
ctx.debug(["step1", "step2", "step3"])
ctx.warning(42)

Backward compatible -- all existing code passing strings continues to work unchanged since the positional argument just changed name from message to data.

Test plan

  • Existing test_context_logging passes (string messages)
  • New test_context_logging_structured_data passes (dict, list, int, None)
  • Updated test_logging_callback passes with new API

…MCP spec

The MCP spec defines the logging data field as 'unknown' (any JSON
serializable type), but Context.log() and convenience methods (debug,
info, warning, error) only accepted str. This changes the parameter
from 'message: str' to 'data: Any' to match the spec and the existing
ServerSession.send_log_message signature.

Also removes the 'extra' parameter which was a workaround for the
str-only limitation and caused confusion (as noted in the issue).

Fixes modelcontextprotocol#397
@Christian-Sidak
Copy link
Copy Markdown
Author

Closing -- I see this has been actively worked on by several contributors (#400, #1907). Apologies for the duplicate!

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.

Context logging function types are not spec compliant

1 participant