Skip to content

fix(google_adk): make MCP protocol errors non-retryable in stateless MCP activities - #549

Merged
brianstrauch merged 2 commits into
temporalio:mainfrom
pcbeingused333:fix/google-adk-mcp-error-nonretryable
Nov 15, 2026
Merged

brianstrauch merged 2 commits into
temporalio:mainfrom
pcbeingused333:fix/google-adk-mcp-error-nonretryable

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

No separate issue: this is the google_adk counterpart of #449, which gave google_genai's MCP activities the protocol-error mapping python/mcp already has.

Cause. The stateless {name}-list-tools and {name}-call-tool activities in _mcp.py let whatever toolset.get_tools() raised leave the activity. ADK does not raise the MCP protocol error itself there: McpToolset.get_tools wraps it in ConnectionError("Failed to get tools from MCP server: ...") with the McpError as __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 sets start_to_close_timeout, was retried without limit under the default retry policy.

Fix. Look up the McpError on the __cause__ chain and map it to ApplicationError(type="MCPProtocolError"), non-retryable for PARSE_ERROR, INVALID_REQUEST, METHOD_NOT_FOUND, INVALID_PARAMS, with ADK's ConnectionError kept 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 in finally.

Scope. Only the listing step is affected in practice: with ADK's default graceful MCP error handling, McpTool.run_async already turns an McpError from the tool call into an {"error": ...} result. The cause-chain lookup also covers a raw McpError if 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 real McpToolset with 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_converted guards the pass-through. tests/test_mcp.py, tests/test_optional_mcp.py and tests/test_stateful_mcp.py: 74 passed; make lint clean.

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.py at TypeAdapter(schema).

Compatibility. McpError is imported from mcp as MCPError (mcp>=2) with a fallback to mcp.shared.exceptions.McpError (mcp<2), as in #449. Recorded histories: no workflow commands change; only the failure recorded for a failing MCP activity differs (MCPProtocolError instead of a retried ConnectionError).

This change was prepared with AI assistance (Claude and Codex).

…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]>
@pcbeingused333
pcbeingused333 requested a review from a team as a code owner November 14, 2026 17:53

@brianstrauch brianstrauch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@pcbeingused333

Copy link
Copy Markdown
Contributor Author

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.

@brianstrauch
brianstrauch enabled auto-merge (squash) November 15, 2026 20:36
@brianstrauch
brianstrauch merged commit 0c7fc5d into temporalio:main Nov 15, 2026
27 checks passed
brianstrauch pushed a commit that referenced this pull request Nov 19, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants