Skip to content

Commit 874bde1

Browse files
committed
fix: move logging configuration from MCPServer.__init__ to run()
MCPServer.__init__() called configure_logging() which invokes logging.basicConfig(), configuring the root logger with handlers and level on every instantiation. This violates Python's logging best practice for libraries: library code must never configure logging — only application entrypoints should. Move configure_logging() to run(), which is the actual application entrypoint. MCPServer used as a library no longer has logging side effects. Github-Issue: #1656 Reported-by: SimonZehetner
1 parent 62eb08e commit 874bde1

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/mcp/server/mcpserver/server.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,6 @@ def __init__(
194194
self._token_verifier = ProviderTokenVerifier(auth_server_provider)
195195
self._custom_starlette_routes: list[Route] = []
196196

197-
# Configure logging
198-
configure_logging(self.settings.log_level)
199-
200197
@property
201198
def name(self) -> str:
202199
return self._lowlevel_server.name
@@ -278,6 +275,8 @@ def run(
278275
transport: Transport protocol to use ("stdio", "sse", or "streamable-http")
279276
**kwargs: Transport-specific options (see overloads for details)
280277
"""
278+
configure_logging(self.settings.log_level)
279+
281280
TRANSPORTS = Literal["stdio", "sse", "streamable-http"]
282281
if transport not in TRANSPORTS.__args__: # type: ignore # pragma: no cover
283282
raise ValueError(f"Unknown transport: {transport}")

tests/server/mcpserver/test_server.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,3 +1481,17 @@ async def test_report_progress_passes_related_request_id():
14811481
message="halfway",
14821482
related_request_id="req-abc-123",
14831483
)
1484+
1485+
1486+
def test_init_does_not_configure_logging():
1487+
"""MCPServer.__init__ must not call logging.basicConfig or add handlers.
1488+
1489+
Libraries should never configure the root logger — that is the
1490+
application's responsibility. Logging should only be configured
1491+
when the server is run as a standalone process via ``run()``.
1492+
1493+
Regression test for https://github.com/modelcontextprotocol/python-sdk/issues/1656
1494+
"""
1495+
with patch("mcp.server.mcpserver.server.configure_logging") as mock_configure:
1496+
MCPServer("test")
1497+
mock_configure.assert_not_called()

0 commit comments

Comments
 (0)