Repository navigation
Conversation
Contributor
Author
|
Hi @kalenkevich, Could you please take a look? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
2. Or, if no issue exists, describe the change:
If applicable, please follow the issue templates to provide as much detail as
possible.
Problem:
MCPToolset(getTools,listResources,getResourceInfo, andreadResource) andMCPTool.runAsynccreated a fresh MCPClientviaMCPSessionManager.createSession()and closed it in afinallyblock on every operation. Because stdio transports launch the MCP server as a child process per client session, every discovery, resource read, and tool call started a new server process and discarded all in-memory server state (such as navigated browser tabs, cookies, or login state) before the next call. Stateful Streamable HTTP and SSE MCP servers likewise received a new session on every call.Solution:
Added
MCPSessionManager.getSession(),MCPSessionManager.close(), andisDisconnectedSessionError()incore/src/tools/mcp/mcp_session_manager.tsto lazily establish and cache one shared MCP session per manager, deduplicate concurrent connection attempts, automatically reconnect if the transport closes or a Streamable HTTP session expires, and cleanly close active and in-flight sessions onclose(). UpdatedMCPTool.runAsyncandMCPToolsetmethods to reuse the shared session untilMCPToolset.close()(whichRunner.runAsyncinvokes automatically in itsfinallyblock), updatedAgentRegistrySingleMCPToolset.close()to close any execution sessions opened by its returnedMCPToolinstances, and updateddocs/guides/tools/mcp_tool/index.mdto document the shared session lifecycle.Testing Plan
Please describe the tests that you ran to verify your changes. This is required
for all PRs that are not small documentation or typo fixes.
Unit Tests:
Added and updated unit tests in
core/test/tools/mcp/mcp_session_manager_test.ts,core/test/tools/mcp/mcp_tool_test.ts,core/test/tools/mcp/mcp_toolset_test.ts, andcore/test/integrations/agent_registry_mcp_toolset_test.tscovering shared session caching, concurrentgetSession()deduplication, disconnect error eviction (-32000,404,Not connected,Session terminated), keeping healthy sessions open across regular tool errors, automatic reconnection afterclient.oncloseandtransport.onclose, race-safeclose()during in-flight connections, and reopening afterclose(). Rannpm run test:unit -- --run(345 test files passed, 6278 tests passed),tests/e2e/tools/mcp/load_mcp_resource_e2e_test.ts(3 passed),npm run build,npm run ts:check,npm run ts:check:samples,npm run lint,npm run format:check, andnpm run docs:check.Manual End-to-End (E2E) Tests:
Built the workspace with
npm run buildand tested against real stateful stdio and Streamable HTTP MCP servers exposingnavigate_page,list_pages,set_cookie,get_cookies, and aselected_pageresource:main): callinggetTools(),navigate_page({url: 'state-check'}),list_pages(), andreadResource('selected_page')spawned 5 stdio server processes andlist_pagesreturned1: about:blank [selected].LoadMcpResourceToolreads, and multi-stepInMemoryRunner.runAsyncagent turns shared 1 stdio process (1: state-check [selected]), automatically reconnected after simulated stdio process exit and Streamable HTTP404session expiry, and terminated child processes cleanly whentoolset.close()/Runner.runAsyncfinished.Checklist
Additional context
N/A