sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[FEATURE] `force_stop` method in the `MCPClient` class

Open jmmfcoutinho opened this issue 5 months ago • 1 comments

Problem Statement

There should be a way to force stop the client to release resources without waiting for tasks to complete, because tasks might stall and "never" finish, so there should be a way around this.

Example of where this could be useful: I am currently building an agent that uses tools to surf the web (playwright mcp) I initialize the client, call client.call_tool_sync to load the website, resize, take a screenshot, and then pass this client to the callback of the agent to take screenshots after each interaction (predictable implementation, without polluting the agent's logic).

The tool that loads the website, on some websites, gets stuck indefinitely (bad implementation from playwright, I guess), making the process stall.

To come around this problem, I tried to implement a timeout, after which I'd stop the client, spin a new instance of the MCPClient and retry.

However, because the tool was "still running", even though stalled, on the first client, I was unable to release the resources from the client's thread using the client.stop(), because it awaits task completion.

I hope I was clear enough. Let me know if you need further detail.

Proposed Solution

There should be a force_stop method in the MCPClient class on the mcp_client.py file.

Use Case

People would use this to force stop the client and release resources when tasks fails to end.

Alternatives Solutions

No response

Additional Context

No response

jmmfcoutinho avatar Jul 25 '25 02:07 jmmfcoutinho

Hi,

We recently made it easier, definitely not great but easier, to configure timeouts on the MCPAgentTools - https://github.com/strands-agents/sdk-python/pull/1184

So if you wanted a tool to have a timeout you could set the agent_tool.timeout = ... property.


That being said, the idea of cancellation, not just related to MCP but in general for tools, as raised in #1275 and #501 is a larger issue Strands wants to tackle.

Let us know if the timeout helps you in the short term.

Thanks

dbschmigelski avatar Dec 04 '25 21:12 dbschmigelski