Skip to content

google_genai: make MCP protocol errors non-retryable in MCP activities - #449

Merged
brianstrauch merged 1 commit into
temporalio:mainfrom
pcbeingused333:fix/genai-mcp-error-nonretryable
Nov 9, 2026
Merged

brianstrauch merged 1 commit into
temporalio:mainfrom
pcbeingused333:fix/genai-mcp-error-nonretryable

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

No separate issue: this gives google_genai's MCP activities the protocol-error mapping python/mcp already has (_activities.py).

Cause. {server}-list-tools and {server}-call-tool in _mcp.py evict the connection and re-raise whatever the session raised. An McpError for a request 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. Map McpError to ApplicationError(type="MCPProtocolError"), non-retryable for PARSE_ERROR, INVALID_REQUEST, METHOD_NOT_FOUND, INVALID_PARAMS, as python/mcp does. A lost session (INVALID_REQUEST, no data, "Session terminated"/"Session not found") and internal/timeout errors stay retryable, and the broken connection is still evicted.

Test. test_mcp_protocol_error_retryability (8 codes x list-tools/call-tool): 16 failed on main, 16 passed now. tests/test_gemini_mcp.py + tests/test_optional_mcp.py: 25 passed; make lint clean.

Compatibility. McpError is imported from mcp as MCPError (mcp>=2) with a fallback to mcp.shared.exceptions.McpError (mcp<2), so this keeps working with #424, which widens the extra to mcp<3; err.error.code works on both. Checked the mapping on mcp 1.x and 2.x.

This change was prepared with the help of an AI assistant (Claude) and submitted by the account owner.

The list-tools and call-tool activities re-raised a raw McpError, so an
invalid-params, invalid-request, method-not-found or parse error was
retried (the default policy has no attempt limit). Map them to a non-retryable
ApplicationError(type=MCPProtocolError), as the mcp plugin does. A lost
session (INVALID_REQUEST without data, 'Session terminated' / 'Session
not found') and internal/timeout errors stay retryable.
@pcbeingused333
pcbeingused333 requested a review from a team as a code owner November 8, 2026 21:55

@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.

Thank you!

@brianstrauch
brianstrauch enabled auto-merge (squash) November 9, 2026 00:46
@brianstrauch
brianstrauch merged commit 224d5d5 into temporalio:main Nov 9, 2026
15 checks passed
brianstrauch pushed a commit that referenced this pull request Nov 15, 2026
…MCP activities (#549)

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).

---------

Signed-off-by: Alex Castillo <[email protected]>
brianstrauch pushed a commit that referenced this pull request Nov 15, 2026
…P activities (#548)

Follow-up to #449 (and #411): the same non-retryable mapping of MCP
protocol errors, applied to the stateless MCP activities in
`openai_agents`.

**Cause.** The stateless `{server}-list-tools`, `-call-tool-v2`,
`-list-prompts` and `-get-prompt-v2` activities in `_mcp.py` connect,
run one operation and clean up, but let whatever the server raised
escape. An `McpError` for a request a retry cannot fix (invalid params,
invalid request, method not found, parse error) therefore reached
Temporal as a plain retryable failure and, with the default retry
policy, was retried without limit.

**Fix.** Map `McpError` to `ApplicationError(type="MCPProtocolError")`,
non-retryable for `PARSE_ERROR`, `INVALID_REQUEST`, `METHOD_NOT_FOUND`,
`INVALID_PARAMS`, as `python/mcp` and (since #449) `google_genai` do.
`INTERNAL_ERROR`, timeouts and unknown codes stay retryable. Unlike the
stateful MCP path there is no session-lost exception: each stateless
activity opens a fresh connection, so a retry already reconnects.
`McpError` is imported as `MCPError` (mcp>=2) with a fallback to
`mcp.shared.exceptions.McpError` (mcp<2), matching `google_genai`.

**Test.** `tests/test_stateless_mcp_protocol_errors.py` (5 codes x 4
operations): 20 failed on main, 20 passed now. `pytest
tests/test_openai.py -k mcp` plus the new file: 71 passed; ruff
check/format clean.

This change was prepared with the help of an AI assistant (Claude) and
submitted by the account owner.

---------

Signed-off-by: Alex Castillo <[email protected]>
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