Repository navigation
fix(google_adk): make MCP protocol errors non-retryable in stateless MCP activities - #549
Conversation
…MCP activities The stateless list-tools and call-tool activities re-raised the ConnectionError that ADK's McpToolset.get_tools wraps around an MCP protocol error, so a request a retry cannot fix (invalid params, invalid request, method not found, parse error) was retried without limit under the default retry policy. Look up the McpError on the cause chain and map it to ApplicationError(type="MCPProtocolError"), non-retryable for those codes. A lost session and internal/timeout errors stay retryable. Signed-off-by: Alex Castillo <[email protected]>
brianstrauch
left a comment
There was a problem hiding this comment.
The 23 focused stateless tests pass with MCP 1.x, but the new compatibility code introduces type-checking failures with supported MCP 2.x dependencies.
|
The fallback imports now account for the symbol absent in MCP 2.x, matching the existing google_genai compatibility imports. The cause-chain lookup explicitly casts the value after the isinstance check so both alternative imports type-check. Lint and the 23 stateless MCP tests pass with MCP 1.30.0 and with MCP 2.2.0 on the otherwise locked dependencies. With latest dependencies (MCP 2.3.0), the reported import and narrowing errors are gone; a separate mypy error remains at test_google_adk_agents.py:1954 in TypeAdapter(schema). This comment was drafted with Claude Code. |
…ities (#668) No separate issue: Java sibling of #549 (and #548, #449), which make MCP protocol errors non-retryable in the Python integrations. **Cause.** `McpActivitiesImpl.listMcpTools` and `callMcpTool` wrap every `RuntimeException` in a retryable `googleadk.McpError`. An `McpError` for a request a retry cannot fix (invalid params, invalid request, method not found, parse error) therefore stays retryable and, with the default retry policy, is retried until the policy gives up. **Fix.** Look up the `McpError` along the cause chain (the call may wrap it) and, for the JSON-RPC codes `PARSE_ERROR`, `INVALID_REQUEST`, `METHOD_NOT_FOUND` and `INVALID_PARAMS`, raise the same `googleadk.McpError` failure as non-retryable, matching `python/google_adk`. `INTERNAL_ERROR`, transport errors, timeouts and unknown codes stay retryable. `FailureTypes.MCP_ERROR` docs updated. The failure type is unchanged, so workflows that match on it keep working. The toolset-factory wrapper uses the same classifier, so a JSON-RPC rejection while a factory initializes its session is classified the same way; startup and transport failures stay retryable. **Test.** `McpActivitiesImplTest` gains cases for each of the four codes (raised by the listing and by the `tools/call` request), an error behind a wrapper exception, `INTERNAL_ERROR` staying retryable for both, and a protocol error raised by a toolset factory. The README's MCP failure paragraph is updated. This change was prepared with the help of an AI assistant (Claude) and submitted by the account owner.
No separate issue: this is the
google_adkcounterpart of #449, which gavegoogle_genai's MCP activities the protocol-error mappingpython/mcpalready has.Cause. The stateless
{name}-list-toolsand{name}-call-toolactivities in_mcp.pylet whatevertoolset.get_tools()raised leave the activity. ADK does not raise the MCP protocol error itself there:McpToolset.get_toolswraps it inConnectionError("Failed to get tools from MCP server: ...")with theMcpErroras__cause__. An error a retry cannot fix (invalid params, invalid request, method not found, parse error) therefore reached Temporal as a plain retryable failure and, since the default config only setsstart_to_close_timeout, was retried without limit under the default retry policy.Fix. Look up the
McpErroron the__cause__chain and map it toApplicationError(type="MCPProtocolError"), non-retryable forPARSE_ERROR,INVALID_REQUEST,METHOD_NOT_FOUND,INVALID_PARAMS, with ADK'sConnectionErrorkept as the cause. A lost session (INVALID_REQUEST, no data, "Session terminated"/"Session not found") and internal/timeout/unknown codes stay retryable, other exceptions propagate unchanged, and the toolset is still closed infinally.Scope. Only the listing step is affected in practice: with ADK's default graceful MCP error handling,
McpTool.run_asyncalready turns anMcpErrorfrom the tool call into an{"error": ...}result. The cause-chain lookup also covers a rawMcpErrorif that handling is switched off. The stateful provider's activities share the listing path and are left as they are here; happy to follow up if wanted.Test.
test_mcp_protocol_error_retryability(8 codes x list-tools/call-tool) uses a realMcpToolsetwith only the MCP session faked, so the error arrives wrapped the way ADK reports it: 16 failed on main, 16 passed now.test_non_mcp_error_is_not_convertedguards the pass-through.tests/test_mcp.py,tests/test_optional_mcp.pyandtests/test_stateful_mcp.py: 74 passed;make lintclean.Review follow-up. Both fallback imports now cover the symbol absent from MCP 2.x, and the cause-chain lookup uses an explicit cast after the runtime type check. Full lint and the 23 focused stateless tests pass with MCP 1.30.0 and MCP 2.2.0 on otherwise locked dependencies. With latest dependencies (MCP 2.3.0), the 74 MCP tests pass and Pyright/Basedpyright pass; mypy reports a separate error in
test_google_adk_agents.pyatTypeAdapter(schema).Compatibility.
McpErroris imported frommcpasMCPError(mcp>=2) with a fallback tomcp.shared.exceptions.McpError(mcp<2), as in #449. Recorded histories: no workflow commands change; only the failure recorded for a failing MCP activity differs (MCPProtocolErrorinstead of a retriedConnectionError).This change was prepared with AI assistance (Claude and Codex).