-
Notifications
You must be signed in to change notification settings - Fork 3.2k
fix: Make context logging functions spec-compliant - Fixes #397 #1907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
akshaykumarbedre
wants to merge
7
commits into
modelcontextprotocol:main
Choose a base branch
from
akshaykumarbedre:fix/issue-397-context-logging-spec-compliance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+91
−7
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f26a495
fix: Make context logging functions spec-compliant - Fixes #397
akshaykumarbedre 09295f8
style: Apply Ruff formatting to fix pre-commit checks
akshaykumarbedre b2fb1f9
fix: Update test_logging_callback.py to use correct log parameters
akshaykumarbedre e9a65a5
Refactor code structure for improved readability and maintainability
akshaykumarbedre ff86c75
fix: update logging function parameters to align with MCP spec
akshaykumarbedre 65b12a3
Allow structured payloads in Context logging helpers
akshaykumarbedre 3b95854
fix: enhance context logging to support null values and ensure compli…
akshaykumarbedre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1069,6 +1069,87 @@ async def logging_tool(msg: str, ctx: Context) -> str: | |
| mock_log.assert_any_call(level="warning", data="Warning message", logger=None, related_request_id="1") | ||
| mock_log.assert_any_call(level="error", data="Error message", logger=None, related_request_id="1") | ||
|
|
||
| async def test_context_logging_with_structured_data(self): | ||
| """Test that context logging accepts structured data per MCP spec (issue #397).""" | ||
| mcp = MCPServer() | ||
|
|
||
| async def structured_logging_tool(msg: str, ctx: Context) -> str: | ||
| # Test with dictionary | ||
| await ctx.info({"status": "success", "message": msg, "count": 42}) | ||
| # Test with list | ||
| await ctx.debug([1, 2, 3, "item"]) | ||
| # Test with number | ||
| await ctx.warning(404) | ||
| # Test with boolean | ||
| await ctx.error(True) | ||
|
Comment on lines
+1076
to
+1084
|
||
| # Test with null | ||
| await ctx.info(None) | ||
| # Test string still works (backward compatibility) | ||
| await ctx.info("Plain string message") | ||
| return f"Logged structured data for {msg}" | ||
|
|
||
| mcp.add_tool(structured_logging_tool) | ||
|
|
||
| with patch("mcp.server.session.ServerSession.send_log_message") as mock_log: | ||
| async with Client(mcp) as client: | ||
| result = await client.call_tool("structured_logging_tool", {"msg": "test"}) | ||
| assert len(result.content) == 1 | ||
| content = result.content[0] | ||
| assert isinstance(content, TextContent) | ||
| assert "Logged structured data for test" in content.text | ||
|
|
||
| # Verify all log calls were made with correct data types | ||
| assert mock_log.call_count == 6 | ||
|
|
||
| # Check dictionary logging | ||
| mock_log.assert_any_call( | ||
| level="info", | ||
| data={"status": "success", "message": "test", "count": 42}, | ||
| logger=None, | ||
| related_request_id="1", | ||
| ) | ||
|
|
||
| # Check list logging | ||
| mock_log.assert_any_call( | ||
| level="debug", | ||
| data=[1, 2, 3, "item"], | ||
| logger=None, | ||
| related_request_id="1", | ||
| ) | ||
|
|
||
| # Check number logging | ||
| mock_log.assert_any_call( | ||
| level="warning", | ||
| data=404, | ||
| logger=None, | ||
| related_request_id="1", | ||
| ) | ||
|
|
||
| # Check boolean logging | ||
| mock_log.assert_any_call( | ||
| level="error", | ||
| data=True, | ||
| logger=None, | ||
| related_request_id="1", | ||
| ) | ||
|
|
||
| # Check null logging | ||
| mock_log.assert_any_call( | ||
| level="info", | ||
| data=None, | ||
| logger=None, | ||
| related_request_id="1", | ||
| ) | ||
|
|
||
| # Check string still works | ||
| mock_log.assert_any_call( | ||
| level="info", | ||
| data="Plain string message", | ||
| logger=None, | ||
| related_request_id="1", | ||
| ) | ||
|
|
||
| @pytest.mark.anyio | ||
| async def test_optional_context(self): | ||
| """Test that context is optional.""" | ||
| mcp = MCPServer() | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR description says the
extraparameter was removed from all logging methods (and marks this as a breaking change), butContext.log()and the convenience methods still acceptextra. Either removeextrato match the stated API change or update the PR description/breaking-change note to reflect the actual behavior.