Skip to content

Commit 5441767

Browse files
samuelcolvinKludex
andauthored
improve server logging (#867)
Co-authored-by: Marcelo Trylesinski <[email protected]>
1 parent 05b7156 commit 5441767

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

src/mcp/server/lowlevel/server.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def __init__(
147147
}
148148
self.notification_handlers: dict[type, Callable[..., Awaitable[None]]] = {}
149149
self.notification_options = NotificationOptions()
150-
logger.debug(f"Initializing server '{name}'")
150+
logger.debug("Initializing server %r", name)
151151

152152
def create_initialization_options(
153153
self,
@@ -510,7 +510,7 @@ async def run(
510510

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

515515
tg.start_soon(
516516
self._handle_message,
@@ -543,7 +543,9 @@ async def _handle_message(
543543
await self._handle_notification(notify)
544544

545545
for warning in w:
546-
logger.info(f"Warning: {warning.category.__name__}: {warning.message}")
546+
logger.info(
547+
"Warning: %s: %s", warning.category.__name__, warning.message
548+
)
547549

548550
async def _handle_request(
549551
self,
@@ -553,10 +555,9 @@ async def _handle_request(
553555
lifespan_context: LifespanResultT,
554556
raise_exceptions: bool,
555557
):
556-
logger.info(f"Processing request of type {type(req).__name__}")
557-
if type(req) in self.request_handlers:
558-
handler = self.request_handlers[type(req)]
559-
logger.debug(f"Dispatching request of type {type(req).__name__}")
558+
logger.info("Processing request of type %s", type(req).__name__)
559+
if handler := self.request_handlers.get(type(req)): # type: ignore
560+
logger.debug("Dispatching request of type %s", type(req).__name__)
560561

561562
token = None
562563
try:
@@ -602,16 +603,13 @@ async def _handle_request(
602603
logger.debug("Response sent")
603604

604605
async def _handle_notification(self, notify: Any):
605-
if type(notify) in self.notification_handlers:
606-
assert type(notify) in self.notification_handlers
607-
608-
handler = self.notification_handlers[type(notify)]
609-
logger.debug(f"Dispatching notification of type {type(notify).__name__}")
606+
if handler := self.notification_handlers.get(type(notify)): # type: ignore
607+
logger.debug("Dispatching notification of type %s", type(notify).__name__)
610608

611609
try:
612610
await handler(notify)
613-
except Exception as err:
614-
logger.error(f"Uncaught exception in notification handler: {err}")
611+
except Exception:
612+
logger.exception("Uncaught exception in notification handler")
615613

616614

617615
async def _ping_handler(request: types.PingRequest) -> types.ServerResult:

0 commit comments

Comments
 (0)