Skip to content

fix(mcp): preserve client lifecycle across toolkit copies#2152

Open
richuV wants to merge 1 commit into
agentscope-ai:mainfrom
richuV:codex/mcp-client-lifecycle-2139
Open

fix(mcp): preserve client lifecycle across toolkit copies#2152
richuV wants to merge 1 commit into
agentscope-ai:mainfrom
richuV:codex/mcp-client-lifecycle-2139

Conversation

@richuV

@richuV richuV commented Jul 12, 2026

Copy link
Copy Markdown

Closes #2139.

Summary

  • share reference-counted MCP transport ownership across Toolkit copies
  • expose Toolkit MCP client inventory and idempotent close
  • close final ReActAgent/Harness toolkit resources and roll back failed registration/build
  • verify last-owner close, per-copy removal, and failed initialization cleanup

Verification

  • mvn -pl agentscope-core -Dtest=McpClientManagerTest test (15/15)
  • mvn -pl agentscope-core,agentscope-harness -am -Dtest=ToolkitTest,McpClientManagerTest,ReActAgentTest,HarnessAgentTest,HarnessAgentToolsConfigTest test -Dsurefire.failIfNoSpecifiedTests=false (37/37)
  • JCode real stdio MCP lifecycle acceptance (12/12): final Toolkit remove and Harness close terminate the fixture process

@richuV richuV requested a review from a team July 12, 2026 02:17
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


u010143241 seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@codecov

codecov Bot commented Jul 12, 2026

Copy link
Copy Markdown

@AgentScopeJavaBot AgentScopeJavaBot added bug Something isn't working area/core/tool Tool, skill, RAG abstractions area/core/agent Agent runtime, pipeline, hooks, plan area/harness agentscope-harness (test/runtime support) labels Jul 12, 2026

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI Review

This PR resolves #2139 by introducing McpClientLifecycleOwner with reference counting, allowing multiple Toolkit copies to share MCP transport ownership. Key changes: ReActAgent.close() now properly closes the toolkit (was previously a no-op), HarnessAgent.close() propagates delegate.close(), and the build() path adds try-catch/finally for resource rollback on failure. The design is clean and effectively solves the MCP client resource leak issue.

(inline comments could not be attached — line numbers fell outside PR hunks. See archived report.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core/agent Agent runtime, pipeline, hooks, plan area/core/tool Tool, skill, RAG abstractions area/harness agentscope-harness (test/runtime support) bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Harness: preserve MCP client ownership across Toolkit.copy and close clients on agent shutdown

3 participants