fix: configure only mcp namespace logger instead of root logger#2162
Open
giulio-leone wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
fix: configure only mcp namespace logger instead of root logger#2162giulio-leone wants to merge 2 commits intomodelcontextprotocol:mainfrom
giulio-leone wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
When using transport='stdio', the server wraps sys.stdin.buffer and sys.stdout.buffer in TextIOWrapper and anyio.AsyncFile. When these wrappers are garbage collected after the server exits, they close the underlying sys.stdin/sys.stdout, causing subsequent I/O operations to fail with ValueError. Use os.dup() to duplicate the file descriptors before wrapping them, so closing the wrapped streams only closes the duplicates, leaving the original stdin/stdout intact. Fixes modelcontextprotocol#1933 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
FastMCP.__init__() calls configure_logging() which previously called logging.basicConfig(), configuring the root logger with handlers and level. This violates Python logging best practices: library code should never configure the root logger, as that is the prerogative of the application developer. Replace logging.basicConfig() with targeted configuration of the 'mcp' namespace logger only. This ensures: - MCP SDK logs still work out of the box for quickstart scripts - Application-level logging configuration is not overridden - No duplicate handlers on repeated FastMCP instantiations References: - https://docs.python.org/3/howto/logging.html#configuring-logging-for-a-library Fixes modelcontextprotocol#1656 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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 #1656
FastMCP.__init__()callsconfigure_logging()which previously calledlogging.basicConfig(), configuring the root logger with handlers and level. This violates Python logging best practices:Problem
Any application using the MCP SDK that calls
FastMCP()gets its logging configuration silently overridden:Fix
Replace
logging.basicConfig()with targeted configuration of themcpnamespace logger only:logging.getLogger('mcp')is configured with handlers and levelFastMCP()callsTest Results
475 server tests pass (2 consecutive clean runs).