Skip to content

fix: move logging configuration from MCPServer.__init__ to run()#2285

Open
omar-y-abdi wants to merge 1 commit intomodelcontextprotocol:mainfrom
omar-y-abdi:fix/logging-init-side-effects
Open

fix: move logging configuration from MCPServer.__init__ to run()#2285
omar-y-abdi wants to merge 1 commit intomodelcontextprotocol:mainfrom
omar-y-abdi:fix/logging-init-side-effects

Conversation

@omar-y-abdi
Copy link

Problem

MCPServer.__init__() calls configure_logging() which invokes logging.basicConfig() — configuring the root logger with handlers and a level on every instantiation. Per Python's logging documentation:

It is strongly advised that you do not add any handlers other than NullHandler to your library's loggers.

Any application that imports and instantiates MCPServer gets its logging configuration silently overwritten.

Fix

Move the single configure_logging(self.settings.log_level) call from __init__ to run(). This is the actual application entrypoint — the method users call when they want to start the server as a standalone process.

  • MCPServer("test") (library usage) — zero logging side effects
  • mcp.run() (standalone entrypoint) — configures logging before starting

Changes

  • src/mcp/server/mcpserver/server.py — moved 1 call (3 lines removed, 2 added)
  • tests/server/mcpserver/test_server.py — regression test verifying __init__ does not call configure_logging

Full test suite passes with 100% coverage.

Closes #1656

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: modelcontextprotocol#1656
Reported-by: SimonZehetner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FastMCP configures logging on init, which messes up application-level logging

1 participant