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

[PORT] McpClient does not implement ToolProvider; shutdown() never calls client.disconnect()

Open jimbrub opened this issue 1 month ago • 1 comments

Description

#3162 ported the generic ToolProvider lifecycle (reference counting, addProvider/toolProviders/cleanup() on ToolRegistry, and Agent#shutdown() driving it) to strands-ts. That PR deliberately did not make McpClient implement ToolProvider, so the original resource leak #3162 was filed about is still open:

  • flattenTools still special-cases McpClient via instanceof McpClient, bypassing the new provider machinery entirely.
  • Agent#shutdown() calls toolRegistry.cleanup() (which releases ToolProviders) and memoryManager?.flush(), but never calls client.disconnect() on any McpClient passed into Agent({ tools: [...] }).
  • An McpClient passed into an agent's tools therefore still leaks its connection on shutdown(), exactly as before #3162 landed.

Proposed change

Make McpClient implement (or wrap with) ToolProvider, turning its connect/close lifecycle into a consumer-counted one like strands-py's MCPClient, so Agent#shutdown()'s existing toolRegistry.cleanup() call tears it down along with any other provider. This likely also means removing the special-cased instanceof McpClient branch in flattenTools once McpClient is a ToolProvider.

Context

Called out explicitly as a deliberate scope decision in #3162's PR (strands-agents/harness-sdk#4984), per review feedback on that PR:

McpClient does not yet implement ToolProvider. The issue flags McpClient as the natural first implementer, but doing so means turning its connect/close lifecycle into a consumer-counted one, which is a larger, separate change deserving its own review — left as a follow-up.

Related: #3163 (tool filesystem loader/watcher port) will also need to hook into the same shutdown() teardown path.

jimbrub avatar Oct 08 '26 17:10 jimbrub

Design note to resolve before implementing: tool-change notifications

Carrying forward a design question raised in the #4984 API review (by strandly-the-agent) so it isn't lost once that PR's API contract is signed off:

The agent currently wires client.onToolsChanged per-agent (agent.ts:827-831) for McpClient, but the new public ToolProvider abstract class has no change-notification member. When McpClient becomes a ToolProvider here, this work will have to pick one of:

  1. Grow the ToolProvider contract with an optional change-notification hook (e.g. onToolsChanged?(listener): Unsubscribe or a subscribe method), so the agent can observe dynamic tool updates generically instead of only for McpClient. This expands the public API frozen in #4984 — ideally decided now so #4984's api/needs-review sign-off accounts for it, even if the member only lands here.
  2. Keep the instanceof McpClient special-case in flattenTools indefinitely purely for the change-notification wiring, even after McpClient implements ToolProvider for lifecycle. This leaves a permanent carve-out and undercuts the point of the generic provider abstraction.

Recommend settling (1) vs (2) as part of this issue's design before coding, since option (1) is an additive change to a just-published public abstraction and is cheaper to reason about alongside #4984's API review than after.

github-actions[bot] avatar Oct 08 '26 17:10 github-actions[bot]