diff --git a/src/mcp/client/stdio/__init__.py b/src/mcp/client/stdio/__init__.py index 83de57a2b..db7443e3b 100644 --- a/src/mcp/client/stdio/__init__.py +++ b/src/mcp/client/stdio/__init__.py @@ -177,6 +177,8 @@ async def stdin_writer(): await terminate_windows_process(process) else: process.terminate() + await read_stream.aclose() + await write_stream.aclose() def _get_executable_command(command: str) -> str: diff --git a/tests/client/test_stdio.py b/tests/client/test_stdio.py index 95747ffd1..daf0f432d 100644 --- a/tests/client/test_stdio.py +++ b/tests/client/test_stdio.py @@ -8,6 +8,13 @@ tee: str = shutil.which("tee") # type: ignore +@pytest.mark.anyio +@pytest.mark.skipif(tee is None, reason="could not find tee command") +async def test_stdio_context_manager_exiting(): + async with stdio_client(StdioServerParameters(command=tee)) as (_, _): + pass + + @pytest.mark.anyio @pytest.mark.skipif(tee is None, reason="could not find tee command") async def test_stdio_client():