1111
1212from anyio .streams .memory import MemoryObjectReceiveStream , MemoryObjectSendStream
1313from mcp import ClientSession , StdioServerParameters , Tool as MCPTool , stdio_client
14+ from mcp .client .session import MessageHandlerFnT
1415from mcp .client .sse import sse_client
1516from mcp .client .streamable_http import GetSessionIdCallback , streamablehttp_client
1617from mcp .shared .message import SessionMessage
@@ -103,6 +104,7 @@ def __init__(
103104 use_structured_content : bool = False ,
104105 max_retry_attempts : int = 0 ,
105106 retry_backoff_seconds_base : float = 1.0 ,
107+ message_handler : MessageHandlerFnT | None = None ,
106108 ):
107109 """
108110 Args:
@@ -124,6 +126,8 @@ def __init__(
124126 Defaults to no retries.
125127 retry_backoff_seconds_base: The base delay, in seconds, used for exponential
126128 backoff between retries.
129+ message_handler: Optional handler invoked for session messages as delivered by the
130+ ClientSession.
127131 """
128132 super ().__init__ (use_structured_content = use_structured_content )
129133 self .session : ClientSession | None = None
@@ -135,6 +139,7 @@ def __init__(
135139 self .client_session_timeout_seconds = client_session_timeout_seconds
136140 self .max_retry_attempts = max_retry_attempts
137141 self .retry_backoff_seconds_base = retry_backoff_seconds_base
142+ self .message_handler = message_handler
138143
139144 # The cache is always dirty at startup, so that we fetch tools at least once
140145 self ._cache_dirty = True
@@ -272,6 +277,7 @@ async def connect(self):
272277 timedelta (seconds = self .client_session_timeout_seconds )
273278 if self .client_session_timeout_seconds
274279 else None ,
280+ message_handler = self .message_handler ,
275281 )
276282 )
277283 server_result = await session .initialize ()
@@ -394,6 +400,7 @@ def __init__(
394400 use_structured_content : bool = False ,
395401 max_retry_attempts : int = 0 ,
396402 retry_backoff_seconds_base : float = 1.0 ,
403+ message_handler : MessageHandlerFnT | None = None ,
397404 ):
398405 """Create a new MCP server based on the stdio transport.
399406
@@ -421,6 +428,8 @@ def __init__(
421428 Defaults to no retries.
422429 retry_backoff_seconds_base: The base delay, in seconds, for exponential
423430 backoff between retries.
431+ message_handler: Optional handler invoked for session messages as delivered by the
432+ ClientSession.
424433 """
425434 super ().__init__ (
426435 cache_tools_list ,
@@ -429,6 +438,7 @@ def __init__(
429438 use_structured_content ,
430439 max_retry_attempts ,
431440 retry_backoff_seconds_base ,
441+ message_handler = message_handler ,
432442 )
433443
434444 self .params = StdioServerParameters (
@@ -492,6 +502,7 @@ def __init__(
492502 use_structured_content : bool = False ,
493503 max_retry_attempts : int = 0 ,
494504 retry_backoff_seconds_base : float = 1.0 ,
505+ message_handler : MessageHandlerFnT | None = None ,
495506 ):
496507 """Create a new MCP server based on the HTTP with SSE transport.
497508
@@ -521,6 +532,8 @@ def __init__(
521532 Defaults to no retries.
522533 retry_backoff_seconds_base: The base delay, in seconds, for exponential
523534 backoff between retries.
535+ message_handler: Optional handler invoked for session messages as delivered by the
536+ ClientSession.
524537 """
525538 super ().__init__ (
526539 cache_tools_list ,
@@ -529,6 +542,7 @@ def __init__(
529542 use_structured_content ,
530543 max_retry_attempts ,
531544 retry_backoff_seconds_base ,
545+ message_handler = message_handler ,
532546 )
533547
534548 self .params = params
@@ -595,6 +609,7 @@ def __init__(
595609 use_structured_content : bool = False ,
596610 max_retry_attempts : int = 0 ,
597611 retry_backoff_seconds_base : float = 1.0 ,
612+ message_handler : MessageHandlerFnT | None = None ,
598613 ):
599614 """Create a new MCP server based on the Streamable HTTP transport.
600615
@@ -625,6 +640,8 @@ def __init__(
625640 Defaults to no retries.
626641 retry_backoff_seconds_base: The base delay, in seconds, for exponential
627642 backoff between retries.
643+ message_handler: Optional handler invoked for session messages as delivered by the
644+ ClientSession.
628645 """
629646 super ().__init__ (
630647 cache_tools_list ,
@@ -633,6 +650,7 @@ def __init__(
633650 use_structured_content ,
634651 max_retry_attempts ,
635652 retry_backoff_seconds_base ,
653+ message_handler = message_handler ,
636654 )
637655
638656 self .params = params
0 commit comments