Skip to content

Commit 26b9502

Browse files
committed
added server wait timeout for incoming messages
1 parent 6353dd1 commit 26b9502

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/mcp/server/lowlevel/server.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ async def run(
491491
# the initialization lifecycle, but can do so with any available node
492492
# rather than requiring initialization for each connection.
493493
stateless: bool = False,
494+
timeout: int = 60
494495
):
495496
async with AsyncExitStack() as stack:
496497
lifespan_context = await stack.enter_async_context(self.lifespan(self))
@@ -504,16 +505,17 @@ async def run(
504505
)
505506

506507
async with anyio.create_task_group() as tg:
507-
async for message in session.incoming_messages:
508-
logger.debug(f"Received message: {message}")
509-
510-
tg.start_soon(
511-
self._handle_message,
512-
message,
513-
session,
514-
lifespan_context,
515-
raise_exceptions,
516-
)
508+
with anyio.move_on_after(timeout) as scope:
509+
async for message in session.incoming_messages:
510+
logger.debug(f"Received message: {message}")
511+
512+
tg.start_soon(
513+
self._handle_message,
514+
message,
515+
session,
516+
lifespan_context,
517+
raise_exceptions,
518+
)
517519

518520
async def _handle_message(
519521
self,

0 commit comments

Comments
 (0)