Skip to content

Fix stale MCP tools after disconnect#2570

Open
KXHXK wants to merge 1 commit into
huggingface:mainfrom
KXHXK:fix/mcp-client-disconnect-state
Open

Fix stale MCP tools after disconnect#2570
KXHXK wants to merge 1 commit into
huggingface:mainfrom
KXHXK:fix/mcp-client-disconnect-state

Conversation

@KXHXK

@KXHXK KXHXK commented Jul 25, 2026

Copy link
Copy Markdown

What does this PR do?

Fixes #2569.

MCPClient.disconnect() now clears the session's tool list after exiting the adapter. This prevents get_tools() from returning tools bound to a closed MCP connection and restores its documented behavior of raising until a new session is connected.

The cleanup runs in finally, so stale tools are also removed when the adapter raises during shutdown. The original shutdown exception still propagates unchanged.

Testing

  • Regression-first: 2 tests failed before the implementation
  • tests/test_mcp_client.py: 7 passed
  • TestToolCollection::test_from_mcp: 1 passed
  • Ruff lint and format checks: passed
  • git diff --check: passed
  • Secret-pattern scan of changed files: passed

AI assistance: Codex helped identify the issue, implement the change, and run validation; the contribution was reviewed and submitted by KXH.

@KXHXK
KXHXK marked this pull request as ready for review July 26, 2026 10:19
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 this pull request may close these issues.

MCPClient.get_tools returns stale tools after disconnect

1 participant