fix: move logging configuration from MCPServer.__init__ to run()#2513
Open
dragogargo wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: move logging configuration from MCPServer.__init__ to run()#2513dragogargo wants to merge 1 commit intomodelcontextprotocol:mainfrom
dragogargo wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
MCPServer.__init__() called configure_logging() which invokes logging.basicConfig(), adding handlers and setting the level on the root logger. This violates the Python logging best practice that library code should never configure the root logger — only application entrypoints should do that. Applications embedding MCPServer as a library had their logging config silently overwritten at import/construction time. Move configure_logging() to run(), which is the application entrypoint for standalone server processes. Library consumers who instantiate MCPServer directly now keep full control of their logging setup. Fixes modelcontextprotocol#1656
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.
Motivation and Context
Fixes #1656.
MCPServer.__init__()callsconfigure_logging()which invokeslogging.basicConfig(), adding handlers and setting the level on the root logger. This violates the Python logging best practice that library code should never configure the root logger.Applications embedding
MCPServeras a library had their logging config silently overwritten at construction time.Changes
configure_logging()call fromMCPServer.__init__()toMCPServer.run()— the actual application entrypoint for standalone server processesMCPServerdoes not modify the root loggerHow Has This Been Tested?
test_mcpserver_init_does_not_configure_logging) that captures root logger state before/afterMCPServer()construction and asserts no handlers were added and log level was not changedtest_server.pytests passBreaking Changes
No.
run()still configures logging before starting the server. Only consumers who instantiateMCPServerwithout callingrun()(library usage) are affected — they now keep full control of their logging setup, which is the desired behavior.Types of changes
Checklist