Repository navigation
Add configure_logging opt-out to MCPServer (#1656) #3071
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
Closed
gee-46
wants to merge
2
commits into
modelcontextprotocol:main
from
gee-46:fix/1656-configure-logging-opt-out
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import logging | ||
|
|
||
| import pytest | ||
|
|
||
| from mcp.server.mcpserver import MCPServer | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def clean_root_logger(): | ||
| """Save and restore the root logger's handlers/level around each test. | ||
|
|
||
| `MCPServer` can mutate the root logger via `logging.basicConfig()`, so | ||
| tests that exercise this behavior must not leak handlers into other | ||
| tests (or into pytest's own log capturing, which itself attaches a | ||
| handler to the root logger -- hence comparing against a baseline | ||
| snapshot below, rather than assuming an empty handler list). | ||
| """ | ||
| root = logging.getLogger() | ||
| original_handlers = list(root.handlers) | ||
| original_level = root.level | ||
| yield root | ||
| root.handlers.clear() | ||
| root.handlers.extend(original_handlers) | ||
| root.setLevel(original_level) | ||
|
|
||
|
|
||
| class TestConfigureLogging: | ||
| def test_default_does_not_override_existing_handlers(self, clean_root_logger): | ||
| """If an application has already configured the root logger before | ||
| creating an `MCPServer`, the default behavior must not replace it. | ||
|
|
||
| This matches the standard library's own `logging.basicConfig()` | ||
| contract: it is a no-op if the root logger already has handlers. | ||
| """ | ||
| baseline = list(clean_root_logger.handlers) | ||
| app_handler = logging.StreamHandler() | ||
| clean_root_logger.addHandler(app_handler) | ||
|
|
||
| MCPServer("test-server") | ||
|
|
||
| assert clean_root_logger.handlers == [*baseline, app_handler] | ||
|
|
||
| def test_configure_logging_false_leaves_root_logger_untouched(self, clean_root_logger): | ||
| """`configure_logging=False` opts a server out of touching the root | ||
| logger entirely, so an application can safely configure its own | ||
| logging before or after constructing the server. | ||
|
|
||
| See: https://github.com/modelcontextprotocol/python-sdk/issues/1656 | ||
| """ | ||
| baseline = list(clean_root_logger.handlers) | ||
|
|
||
| MCPServer("test-server", configure_logging=False) | ||
| assert clean_root_logger.handlers == baseline | ||
|
|
||
| # The application's own logging setup, performed afterwards, must | ||
| # take effect (i.e. must not have been pre-empted by the server). | ||
| # Clear first so `basicConfig` (a no-op if handlers already exist) | ||
| # is guaranteed to actually apply the new format. | ||
| clean_root_logger.handlers.clear() | ||
| logging.basicConfig(format="MYAPP: %(message)s") | ||
| assert len(clean_root_logger.handlers) == 1 | ||
| assert clean_root_logger.handlers[0].formatter._fmt == "MYAPP: %(message)s" | ||
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.
P2: The new opt-out test can pass even if
configure_logging=Falseis ignored when pytest has already attached root handlers. Becauselogging.basicConfig()is a no-op with existing handlers, constructingMCPServeragainst thebaselinehandler list does not exercise the race this option fixes; clearing the root handlers before constructing the server (or spying on_configure_logging) would make the regression test cover the intended no-handlers case.Prompt for AI agents
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.
Good catch, thanks — you're right that comparing root logger state was
fragile since pytest's own log capture handler masks the exact race
this fixes. Pushed a fix that spies on
_configure_loggingdirectlyinstead, and verified it actually fails if the
configure_loggingflagis ignored (I temporarily reverted the fix locally to confirm).