Skip to content

fix: make context logging methods accept Any data type per MCP spec#2396

Open
muhammadrashid4587 wants to merge 1 commit intomodelcontextprotocol:mainfrom
muhammadrashid4587:fix/logging-types-spec-compliance
Open

fix: make context logging methods accept Any data type per MCP spec#2396
muhammadrashid4587 wants to merge 1 commit intomodelcontextprotocol:mainfrom
muhammadrashid4587:fix/logging-types-spec-compliance

Conversation

@muhammadrashid4587
Copy link
Copy Markdown

Summary

  • Widens the message parameter type from str to Any in context logging methods (log, debug, info, warning, error) to match the MCP spec which defines logged data as unknown (any JSON-serializable type)
  • When dict data is passed alongside extra, the dicts are now merged rather than wrapping in a {"message": ...} envelope
  • Adds tests for non-string data types (dict, list, number) and dict merging with extra

Context

Per the MCP spec, the data field in log message notifications accepts any JSON-serializable type. The underlying ServerSession.send_log_message already accepts data: Any, but the context convenience methods restricted input to str only.

This is a backward-compatible change — existing code passing strings continues to work identically.

Closes #397

Test plan

  • Existing test_context_logging passes (string data still works)
  • New test_context_logging_any_data — verifies dict, list, and number data types
  • New test_context_logging_dict_with_extra — verifies dict merging behavior with extra

The MCP spec defines logged data as `unknown` (any JSON-serializable type),
but the context logging convenience methods (info, debug, warning, error)
restricted the message parameter to `str`. This widens the type to `Any`
to match the spec and the underlying ServerSession.send_log_message API.

When dict data is passed with extra, the dicts are merged instead of
wrapping in a {"message": ...} envelope.

Closes modelcontextprotocol#397

Github-Issue: modelcontextprotocol#397
Reported-by: alejandro5042
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

2 participants