fix: make context logging methods accept Any data type per MCP spec#2396
Open
muhammadrashid4587 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
messageparameter type fromstrtoAnyin context logging methods (log,debug,info,warning,error) to match the MCP spec which defines logged data asunknown(any JSON-serializable type)extra, the dicts are now merged rather than wrapping in a{"message": ...}envelopeextraContext
Per the MCP spec, the
datafield in log message notifications accepts any JSON-serializable type. The underlyingServerSession.send_log_messagealready acceptsdata: Any, but the context convenience methods restricted input tostronly.This is a backward-compatible change — existing code passing strings continues to work identically.
Closes #397
Test plan
test_context_loggingpasses (string data still works)test_context_logging_any_data— verifies dict, list, and number data typestest_context_logging_dict_with_extra— verifies dict merging behavior withextra