Repository navigation
Fix MCP HTTP session persistence across tool calls #1740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
6f730d3
Fix MCP HTTP session persistence across tool calls
openhands-agent ee6b11d
Fix pre-commit issues: pyright type errors and formatting
openhands-agent cb82210
Simplify MCP session persistence fix
openhands-agent 73d342b
Make MCPClient.sync_close() idempotent
openhands-agent b156a81
Add per-session state test proving session persistence fix works
openhands-agent 6a9f14b
Merge branch 'main' into openhands/fix-mcp-session-persistence
neubig 0232c81
Fix pre-commit issues in test_stateful_mcp.py
openhands-agent e9bc840
Address review comments: add connect()/disconnect() methods and conte…
openhands-agent d46a321
Move _closed flag to after cleanup in sync_close()
openhands-agent 598a572
Fix empty except clauses by adding logging
openhands-agent c86351f
Introduce MCPToolset for explicit lifecycle management
openhands-agent 5e50b4b
Simplify MCP connection management to single approach
openhands-agent da237e1
Simplify MCP lifecycle with MCPToolset class
openhands-agent f1ea8d6
Clean up MCP code review issues
openhands-agent 6840b82
Simplify: put tools directly on MCPClient, remove MCPToolset
openhands-agent 90e797f
Fix MCP session persistence with minimal changes
openhands-agent 89a5195
Fix pre-commit formatting issues
openhands-agent 7ab9207
Fix pyright type errors in test conftest
openhands-agent 8b07e37
Remove conftest.py and define test servers inline
openhands-agent 0d7778c
Merge branch 'main' into openhands/fix-mcp-session-persistence
neubig b85508d
Address code review feedback
openhands-agent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| """Tests for MCP session persistence across tool calls. | ||
|
|
||
| Verifies that MCP connections are reused across multiple tool calls, | ||
| avoiding the overhead of reconnecting for each call. | ||
|
|
||
| Related issue: https://github.com/OpenHands/software-agent-sdk/issues/1739 | ||
| """ | ||
|
|
||
| import asyncio | ||
| import socket | ||
| import threading | ||
| import time | ||
|
|
||
| import pytest | ||
| from fastmcp import FastMCP | ||
|
|
||
| from openhands.sdk.mcp import create_mcp_tools | ||
| from openhands.sdk.mcp.tool import MCPToolExecutor | ||
|
|
||
|
|
||
| def _find_free_port() -> int: | ||
| """Find an available port.""" | ||
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
| s.bind(("127.0.0.1", 0)) | ||
| return s.getsockname()[1] | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def live_server(): | ||
| """Fixture providing a live MCP test server with echo/add tools.""" | ||
| mcp = FastMCP("session-test-server") | ||
|
|
||
| @mcp.tool() | ||
| def echo(message: str) -> str: | ||
| """Echo a message.""" | ||
| return f"Echo: {message}" | ||
|
|
||
| @mcp.tool() | ||
| def add_numbers(a: int, b: int) -> str: | ||
| """Add two numbers.""" | ||
| return str(a + b) | ||
|
|
||
| port = _find_free_port() | ||
|
|
||
| def run(): | ||
| loop = asyncio.new_event_loop() | ||
| asyncio.set_event_loop(loop) | ||
| loop.run_until_complete( | ||
| mcp.run_http_async( | ||
| host="127.0.0.1", | ||
| port=port, | ||
| transport="http", | ||
| show_banner=False, | ||
| path="/mcp", | ||
| ) | ||
| ) | ||
|
|
||
| thread = threading.Thread(target=run, daemon=True) | ||
| thread.start() | ||
| time.sleep(0.5) | ||
| yield port | ||
|
|
||
|
|
||
| class TestSessionPersistence: | ||
| """Tests verifying session/connection persistence.""" | ||
|
|
||
| def test_connection_reused_across_tool_calls(self, live_server: int): | ||
| """Test that multiple tool calls reuse the same connection.""" | ||
| config = { | ||
| "mcpServers": { | ||
| "test": { | ||
| "transport": "http", | ||
| "url": f"http://127.0.0.1:{live_server}/mcp", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| with create_mcp_tools(config, timeout=10.0) as client: | ||
| assert len(client) == 2 | ||
|
|
||
| echo_tool = next(t for t in client if t.name == "echo") | ||
| add_tool = next(t for t in client if t.name == "add_numbers") | ||
|
|
||
| # Verify they share the same client | ||
| echo_executor = echo_tool.executor | ||
| add_executor = add_tool.executor | ||
| assert isinstance(echo_executor, MCPToolExecutor) | ||
| assert isinstance(add_executor, MCPToolExecutor) | ||
| assert echo_executor.client is add_executor.client | ||
|
|
||
| # Make multiple calls - should all use same connection | ||
| for i in range(3): | ||
| action = echo_tool.action_from_arguments({"message": f"test_{i}"}) | ||
| result = echo_executor(action) | ||
| assert f"test_{i}" in result.text | ||
|
|
||
| # Call different tool - same connection | ||
| action = add_tool.action_from_arguments({"a": 5, "b": 3}) | ||
| result = add_executor(action) | ||
| assert "8" in result.text | ||
|
|
||
| def test_close_releases_connection(self, live_server: int): | ||
| """Test that close() properly releases the connection.""" | ||
| config = { | ||
| "mcpServers": { | ||
| "test": { | ||
| "transport": "http", | ||
| "url": f"http://127.0.0.1:{live_server}/mcp", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| with create_mcp_tools(config, timeout=10.0) as client: | ||
| tool = next(t for t in client if t.name == "echo") | ||
| executor = tool.executor | ||
| assert isinstance(executor, MCPToolExecutor) | ||
|
|
||
| # Make a call | ||
| action = tool.action_from_arguments({"message": "test"}) | ||
| result = executor(action) | ||
| assert "test" in result.text |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.