[PORT] McpClient does not implement ToolProvider; shutdown() never calls client.disconnect()
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:
flattenToolsstill special-casesMcpClientviainstanceof McpClient, bypassing the new provider machinery entirely.Agent#shutdown()callstoolRegistry.cleanup()(which releasesToolProviders) andmemoryManager?.flush(), but never callsclient.disconnect()on anyMcpClientpassed intoAgent({ tools: [...] }).- An
McpClientpassed into an agent'stoolstherefore still leaks its connection onshutdown(), 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:
McpClientdoes not yet implementToolProvider. The issue flagsMcpClientas 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.
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:
- Grow the
ToolProvidercontract with an optional change-notification hook (e.g.onToolsChanged?(listener): Unsubscribeor a subscribe method), so the agent can observe dynamic tool updates generically instead of only forMcpClient. This expands the public API frozen in #4984 — ideally decided now so #4984'sapi/needs-reviewsign-off accounts for it, even if the member only lands here. - Keep the
instanceof McpClientspecial-case inflattenToolsindefinitely purely for the change-notification wiring, even afterMcpClientimplementsToolProviderfor 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.