Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdio_client context manager hangs #360

Open
hwaxxer opened this issue Mar 24, 2025 · 7 comments · May be fixed by #361
Open

stdio_client context manager hangs #360

hwaxxer opened this issue Mar 24, 2025 · 7 comments · May be fixed by #361

Comments

@hwaxxer
Copy link

hwaxxer commented Mar 24, 2025

Describe the bug
When using the stdio_client context manager, the process is never cleaned up.

To Reproduce
Steps to reproduce the behavior:

import asyncio

from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client


async def run():
    server_params = StdioServerParameters(
        command="claude",
        args=["mcp", "serve"],
    )
    async with stdio_client(server_params) as (read, write):   
        async with ClientSession(read, write) as _:
            pass
        print("exit ClientSession")
    print("exit stdio_client")

if __name__ == "__main__":
    asyncio.run(run())

Expected behavior
The program would print:

exit ClientSession
exit stdio_client

But it never exits the stdio_client context.

@hwaxxer hwaxxer linked a pull request Mar 24, 2025 that will close this issue
9 tasks
@mroch
Copy link

mroch commented Mar 25, 2025

stdio_server seems to have a similar problem:

import asyncio

from mcp.server.stdio import stdio_server


async def run():
    async with stdio_server() as (read_stream, write_stream):
        print("inside")
    print("outside")


if __name__ == "__main__":
    asyncio.run(run())

this prints inside and never exits.

@hwaxxer
Copy link
Author

hwaxxer commented Mar 25, 2025

One could argue that's per design since stdio_server is built to run indefinitely. For the stdio_client however, I think it makes sense to be able to start up the server, run a discrete task and then exit.

@dsp-ant
Copy link
Member

dsp-ant commented Mar 25, 2025

Can you check with most recent master if it's fixed. I think #325 already addresses the issue

@hwaxxer
Copy link
Author

hwaxxer commented Mar 25, 2025

@dsp-ant Same problem still.

@hwaxxer
Copy link
Author

hwaxxer commented Mar 25, 2025

The proposed fix #361 still works fwiw, but I understand it's a bit of a workaround and not addressing the underlying issue.

@mroch
Copy link

mroch commented Mar 25, 2025

One could argue that's per design since stdio_server is built to run indefinitely. For the stdio_client however, I think it makes sense to be able to start up the server, run a discrete task and then exit.

I disagree, but can move that discussion to a different issue. For example, the server might want to exit if it gets an uncaught exception (the current bug I'm facing), but even in normal circumstances, it could be checked into your repo and you switch commits, so it needs to restart a different version. For the LSP, vscode-languageclient respawns the server when it exits.

@mroch
Copy link

mroch commented Mar 29, 2025

#372 probably fixed this. it also adds a process.terminate() like #361

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants