Skip to content

improve server logging #867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions src/mcp/server/lowlevel/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def __init__(
}
self.notification_handlers: dict[type, Callable[..., Awaitable[None]]] = {}
self.notification_options = NotificationOptions()
logger.debug(f"Initializing server '{name}'")
logger.debug("Initializing server %r", name)

def create_initialization_options(
self,
Expand Down Expand Up @@ -510,7 +510,7 @@ async def run(

async with anyio.create_task_group() as tg:
async for message in session.incoming_messages:
logger.debug(f"Received message: {message}")
logger.debug("Received message: %s", message)

tg.start_soon(
self._handle_message,
Expand Down Expand Up @@ -543,7 +543,9 @@ async def _handle_message(
await self._handle_notification(notify)

for warning in w:
logger.info(f"Warning: {warning.category.__name__}: {warning.message}")
logger.info(
"Warning: %s: %s", warning.category.__name__, warning.message
)

async def _handle_request(
self,
Expand All @@ -553,10 +555,9 @@ async def _handle_request(
lifespan_context: LifespanResultT,
raise_exceptions: bool,
):
logger.info(f"Processing request of type {type(req).__name__}")
if type(req) in self.request_handlers:
handler = self.request_handlers[type(req)]
logger.debug(f"Dispatching request of type {type(req).__name__}")
logger.info("Processing request of type %s", type(req).__name__)
if handler := self.request_handlers.get(type(req)): # type: ignore
logger.debug("Dispatching request of type %s", type(req).__name__)

token = None
try:
Expand Down Expand Up @@ -602,16 +603,13 @@ async def _handle_request(
logger.debug("Response sent")

async def _handle_notification(self, notify: Any):
if type(notify) in self.notification_handlers:
assert type(notify) in self.notification_handlers

handler = self.notification_handlers[type(notify)]
logger.debug(f"Dispatching notification of type {type(notify).__name__}")
if handler := self.notification_handlers.get(type(notify)): # type: ignore
logger.debug("Dispatching notification of type %s", type(notify).__name__)

try:
await handler(notify)
except Exception as err:
logger.error(f"Uncaught exception in notification handler: {err}")
except Exception:
logger.exception("Uncaught exception in notification handler")


async def _ping_handler(request: types.PingRequest) -> types.ServerResult:
Expand Down
Loading