@@ -149,7 +149,8 @@ def __init__(
149
149
"""Initializes the MCP session manager.
150
150
151
151
Args:
152
- connection_params: Parameters for the MCP connection (Stdio, SSE or Streamable HTTP).
152
+ connection_params: Parameters for the MCP connection (Stdio, SSE or
153
+ Streamable HTTP).
153
154
errlog: (Optional) TextIO stream for error logging. Use only for
154
155
initializing a local stdio MCP session.
155
156
"""
@@ -203,9 +204,23 @@ async def create_session(self) -> ClientSession:
203
204
transports = await self ._exit_stack .enter_async_context (client )
204
205
# The streamable http client returns a GetSessionCallback in addition to the read/write MemoryObjectStreams
205
206
# needed to build the ClientSession, we limit then to the two first values to be compatible with all clients.
206
- session = await self ._exit_stack .enter_async_context (
207
- ClientSession (* transports [:2 ])
208
- )
207
+ # The StdioServerParameters does not provide a timeout parameter for the
208
+ # session, so we need to set a default timeout for it. Other clients
209
+ # (SseServerParams and StreamableHTTPServerParams) already provide a
210
+ # timeout parameter in their configuration.
211
+ if isinstance (self ._connection_params , StdioServerParameters ):
212
+ # Default timeout for MCP session is 5 seconds, same as SseServerParams
213
+ # and StreamableHTTPServerParams.
214
+ session = await self ._exit_stack .enter_async_context (
215
+ ClientSession (
216
+ * transports [:2 ],
217
+ read_timeout_seconds = timedelta (seconds = 5 ),
218
+ )
219
+ )
220
+ else :
221
+ session = await self ._exit_stack .enter_async_context (
222
+ ClientSession (* transports [:2 ])
223
+ )
209
224
await session .initialize ()
210
225
211
226
self ._session = session
0 commit comments