Repository navigation
Conversation
| _SESSION_LOST_MESSAGE = re.compile( | ||
| r'session (?:not found|has been terminated)|expired session', | ||
| re.IGNORECASE, | ||
| ) | ||
|
|
||
|
|
||
| def _reports_session_terminated(exc: McpError) -> bool: | ||
| """Whether this MCP error is the server's own session-terminated report.""" | ||
| if exc.error.code == _SESSION_TERMINATED_ERROR_CODE: | ||
| return True | ||
| if exc.error.code == _INVALID_REQUEST_ERROR_CODE and ( | ||
| exc.error.message == _SESSION_TERMINATED_MESSAGE | ||
| ): | ||
| return True | ||
| return bool( | ||
| exc.error.code == _INVALID_REQUEST_ERROR_CODE | ||
| and exc.error.message == _SESSION_TERMINATED_MESSAGE | ||
| exc.error.code | ||
| in (_INVALID_REQUEST_ERROR_CODE, _SESSION_NOT_FOUND_ERROR_CODE) | ||
| and _SESSION_LOST_MESSAGE.search(exc.error.message or '') | ||
| ) |
There was a problem hiding this comment.
.search makes this match any INVALID_REQUEST that mentions a session somewhere, e.g. a tool raising "Browser session not found for tab 3" or "Login failed: expired session cookie". mcp_tool.run_async then drops the pooled session and retries on the assumption the tool never ran, so a non idempotent tool can run twice. Matching only the known transport spellings whole keeps the fix and closes that:
| _SESSION_LOST_MESSAGE = re.compile( | |
| r'session (?:not found|has been terminated)|expired session', | |
| re.IGNORECASE, | |
| ) | |
| def _reports_session_terminated(exc: McpError) -> bool: | |
| """Whether this MCP error is the server's own session-terminated report.""" | |
| if exc.error.code == _SESSION_TERMINATED_ERROR_CODE: | |
| return True | |
| if exc.error.code == _INVALID_REQUEST_ERROR_CODE and ( | |
| exc.error.message == _SESSION_TERMINATED_MESSAGE | |
| ): | |
| return True | |
| return bool( | |
| exc.error.code == _INVALID_REQUEST_ERROR_CODE | |
| and exc.error.message == _SESSION_TERMINATED_MESSAGE | |
| exc.error.code | |
| in (_INVALID_REQUEST_ERROR_CODE, _SESSION_NOT_FOUND_ERROR_CODE) | |
| and _SESSION_LOST_MESSAGE.search(exc.error.message or '') | |
| ) | |
| # Matched whole: a tool's own INVALID_REQUEST about some other session (a | |
| # browser or chat session, say) must not pass for a lost MCP session, since that | |
| # triggers a retry that assumes the tool never ran. | |
| _SESSION_LOST_MESSAGE = re.compile( | |
| r'session not found' | |
| r'|not found: invalid or expired session id' | |
| r'|not found: session has been terminated', | |
| re.IGNORECASE, | |
| ) | |
| def _reports_session_terminated(exc: McpError) -> bool: | |
| """Whether this MCP error is the server's own session-terminated report.""" | |
| if exc.error.code == _SESSION_TERMINATED_ERROR_CODE: | |
| return True | |
| if exc.error.code == _INVALID_REQUEST_ERROR_CODE and ( | |
| exc.error.message == _SESSION_TERMINATED_MESSAGE | |
| ): | |
| return True | |
| return bool( | |
| exc.error.code | |
| in (_INVALID_REQUEST_ERROR_CODE, _SESSION_NOT_FOUND_ERROR_CODE) | |
| and _SESSION_LOST_MESSAGE.fullmatch((exc.error.message or '').strip()) | |
| ) |
plus a negative test in test_mcp_session_manager.py:
@pytest.mark.parametrize(
"message",
[
"Browser session not found for tab 3",
"Chat session not found: abc123",
"Login failed: expired session cookie",
],
)
def test_a_tools_own_session_error_is_not_session_terminated(self, message):
assert not _is_session_terminated_error(make_mcp_error(-32600, message))have this plus the negative test ready on my side, can put it up as a commit if that helps
…ives A tool returning an MCP error that mentions "session" in its own message (e.g., "Browser session not found for tab 3") could be misdetected as a server session loss, triggering an automatic retry. The retry assumes the tool never ran, so a non-idempotent call could execute twice. Switch from .search to .fullmatch and list the exact server spellings. Add a negative test with tool messages that must not trigger recovery. Addresses: google#8500 (comment)
|
@its-amann thanks for catching this. Pushed 7e4ead6: the message now has to match one of the known transport spellings whole ( |
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
_is_session_disconnectedonly checks local stream flags (scope of closed #3321, still broken in v2.7.1) #6822Problem:
_is_session_terminated_errorrecognises a Streamable HTTP session loss only asINVALID_REQUESTwith the exact message"Session terminated", which the 2.x MCP SDK uses when a 404 has no usable body. When the server's 404 carries a JSON-RPC error, the SDK surfaces that error instead: python-sdk servers send-32600 "Session not found"(older releases:"Not Found: Invalid or expired session ID","Not Found: Session has been terminated"), TypeScript SDK servers send-32001 "Session not found". None of these match, soMCPSessionManagerkeeps the dead session and the agent loses its MCP tools until the process restarts.Solution:
Also treat
-32600or-32001as a session loss when the message says the session was not found, terminated or expired.-32600for an ordinary bad request and unrelated-32001errors still don't count.Testing Plan
Unit Tests:
pytest tests/unittests/tools/mcp_tool: 507 passed. The new cases inTestIsSessionTerminatedErrorcover the three server spellings and an unrelated-32001error; the 404-body cases fail without the change.Manual End-to-End (E2E) Tests:
https://github.com/Koobi33/seams-lab/blob/main/repro/adk-session-not-found/repro.py runs an
LlmAgentwith a scripted model and anMcpToolsetagainst a python-sdk Streamable HTTP server and restarts the server between turns. Before: every turn after the restart gets "The tools you can call are: none". After: both turns callechoand gethello.Checklist