Skip to content

Commit 1ca882e

Browse files
author
Andrey Barchenkov
committed
fix: move logging configuration from MCPServer.__init__ to run()
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 #1656
1 parent 3d7b311 commit 1ca882e

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

src/mcp/server/mcpserver/server.py

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

206-
# Configure logging
207-
configure_logging(self.settings.log_level)
208-
209206
@property
210207
def name(self) -> str:
211208
return self._lowlevel_server.name
@@ -291,6 +288,8 @@ def run(
291288
if transport not in TRANSPORTS.__args__: # type: ignore # pragma: no cover
292289
raise ValueError(f"Unknown transport: {transport}")
293290

291+
configure_logging(self.settings.log_level)
292+
294293
match transport:
295294
case "stdio":
296295
anyio.run(self.run_stdio_async)

tests/server/mcpserver/test_server.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,3 +1516,22 @@ async def test_report_progress_passes_related_request_id():
15161516
message="halfway",
15171517
related_request_id="req-abc-123",
15181518
)
1519+
1520+
1521+
def test_mcpserver_init_does_not_configure_logging() -> None:
1522+
"""MCPServer.__init__ must not call logging.basicConfig or add handlers.
1523+
1524+
Regression test for https://github.com/modelcontextprotocol/python-sdk/issues/1656.
1525+
Library code should never configure the root logger — that is the
1526+
application's responsibility.
1527+
"""
1528+
import logging
1529+
1530+
root = logging.getLogger()
1531+
handlers_before = list(root.handlers)
1532+
level_before = root.level
1533+
1534+
MCPServer("test-logging")
1535+
1536+
assert root.handlers == handlers_before
1537+
assert root.level == level_before

0 commit comments

Comments
 (0)