fix: make Context logging methods spec-compliant by accepting Any data type#2195
Open
Jimil-Joshi wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
Conversation
…a type Per the MCP specification, the data field in LoggingMessageNotificationParams allows any JSON serializable type, not just strings. The Context.log() and convenience methods (debug, info, warning, error) were typed as message: str, which prevented users from logging structured data like dicts, lists, numbers, etc. Changes: - Change message: str to data: Any in Context.log() and all convenience methods - Remove the extra parameter (structured data can now be passed directly as data) - Update docstrings to document that any JSON serializable type is accepted - Update class docstring with examples of structured data logging - Add test for logging structured data (dict, list, number, boolean, None) Fixes modelcontextprotocol#397
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
Fixes #397
Per the MCP specification, the
datafield inLoggingMessageNotificationParamsallows any JSON serializable type:However, the
Context.log()and convenience methods (debug,info,warning,error) were typed asmessage: str, which prevented users from logging structured data like dicts, lists, numbers, etc.Changes
Context.log(): Changedmessage: strparameter todata: Anyto match the MCP spec and the underlyingServerSession.send_log_message(data: Any)debug,info,warning,error): Updated to acceptdata: Anyinstead ofmessage: strextraparameter: Structured data can now be passed directly asdata(e.g.,ctx.info({"event": "processing", "count": 5})) making theextraworkaround unnecessaryExample
`python
@server.tool()
async def my_tool(x: int, ctx: Context) -> str:
# String messages still work (backward compatible)
await ctx.info(f"Processing {x}")
`
Test Results
All 87 tests in
tests/server/mcpserver/test_server.pypass, including the new structured data logging test.