Skip to content

mcp: fail an MCPError wrapped in an ExceptionGroup without retrying it - #411

Merged
brianstrauch merged 1 commit into
temporalio:mainfrom
pcbeingused333:fix/mcp-exceptiongroup-381
Nov 4, 2026
Merged

brianstrauch merged 1 commit into
temporalio:mainfrom
pcbeingused333:fix/mcp-exceptiongroup-381

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Fixes #381

Root cause. With a factory argument, _MCPConnectionPool.backend runs the operation inside async with factory(factory_argument) as backend. mcp.Client.__aexit__ re-raises an error from the body wrapped twice in an ExceptionGroup (its task groups), and the same happens to an MCPError raised while connecting. _MCPActivities._run only caught a bare MCPError, so the Activity failed with the group, which Temporal retries under the default policy, even for codes a retry cannot fix (e.g. invalid params).

Fix. _run now also catches BaseExceptionGroup and, when a nest of single-member groups carries an MCPError, maps it exactly like a bare one (same ApplicationError, type, details and retryability, with the session-lost check kept). The mapping moved to a small _protocol_error helper shared by both paths. Groups with several members or other exceptions are re-raised unchanged. On Python 3.10, BaseExceptionGroup comes from the exceptiongroup backport, added as a dependency for python_version < '3.11'. BaseExceptionGroup is a builtin only from Python 3.11 and the plugin supports 3.10, so it is imported from the exceptiongroup backport there (new python_version < '3.11' dependency, lock updated).

Test. test_protocol_error_wrapped_in_exception_group_fails_without_retry reads an unknown resource with a factory argument and expects a non-retryable MCPProtocolError with (INVALID_PARAMS, {"uri": ...}). It fails on main and passes now on Python 3.10 and 3.14. The suite passes on Python 3.10 and 3.14, and the make lint steps (ruff, pyright, mypy, basedpyright, pydocstyle) are clean.


This change was prepared with the help of an AI coding assistant (Claude); the submitter is responsible for it.

A client built with a factory argument is closed inside the operation, and
Client.__aexit__ re-raises an MCPError from the body wrapped twice in an
ExceptionGroup. The same happens to an error raised while connecting.
_MCPActivities._run only caught a bare MCPError, so the Activity failed with
the group and Temporal retried it, even for codes a retry cannot fix.

Unwrap an MCPError that a nest of ExceptionGroups carries alone and map it
to the same ApplicationError as a bare one. BaseExceptionGroup is a builtin
only from Python 3.11, so import it from the exceptiongroup backport on 3.10.
@pcbeingused333
pcbeingused333 requested a review from a team as a code owner November 4, 2026 06:22

@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 4, 2026 15:47
@brianstrauch
brianstrauch merged commit f3e1fb1 into temporalio:main Nov 4, 2026
15 checks passed
campoy added a commit that referenced this pull request Nov 6, 2026
Raise the floor from 0.2.0 to 0.3.0, so every install gets the lost-session retry (#379) and the factory-argument error fixes (#411, #433), and CI tests the only version allowed. The locks still resolve 0.2.0 and need a refresh once 0.3.0 is on PyPI.
campoy added a commit that referenced this pull request Nov 13, 2026
## What was changed

- OpenAI Agents and Strands (Python) require
`temporalio-mcp>=0.4.0,<0.5` instead of `>=0.2.0,<0.3`, in their `mcp`
extra and dev group.
- Both `uv.lock` files now resolve the published 0.4.0. No other locked
package changes.
- The `temporal_mcp_server()` docstring now says its MCP operations get
a 10-second heartbeat timeout by default, and how to turn it off.

## Why?

The `temporalio-mcp` 0.3.0 release notes say OpenAI Agents and Strands
keep 0.2.0 behavior until their pins move: both plugins pin `<0.3`. This
PR makes them require 0.4, which includes 0.3.0's changes, before GA.

0.3.0 changes how these plugins' MCP calls fail. I ran each change
through each plugin's own MCP wiring, against an in-process
streamable-HTTP MCP server, on 0.2.0 and 0.3.0:

| Case | OpenAI Agents (`mcp_servers`) | Strands (`mcp_clients`) |
|---|---|---|
| A tool result the MCP client rejects (#437) | Retried forever → fails
at once | Retried forever → fails at once |
| A lost session, HTTP 404 (#379) | No session in my test (see below) |
Failed at once → retried with a new session |
| Repeated pagination cursor, MCP error | Already failed at once |
Already failed at once |

- The rejected-result fix reaches both plugins, with and without a
factory argument. Strands turns the failure into a tool error the model
sees, instead of the run hanging.
- #379 is Caller-breaking for Strands: a lost session used to become a
tool error; now the run waits for a new session under its retry policy.
Taking it after GA would mean a new major, so before GA is the cheapest
moment.
- With OpenAI Agents, the Agents SDK used the current MCP protocol,
which has no sessions, so I couldn't trigger the 404 path.
- The factory-argument `ExceptionGroup` fixes (#411, #433, #455) don't
change either plugin: Strands' factories take no argument, and OpenAI
Agents' backend doesn't wrap errors in groups.

0.4.0 adds one more change (#494): MCP operations heartbeat while they
run, with a 10-second `heartbeat_timeout` when the Activity config
leaves it out. So a cancel reaches a running MCP request, and a request
longer than a `heartbeat_timeout` you set no longer fails with a
heartbeat timeout.

- OpenAI Agents: `workflow.temporal_mcp_server()` passes
`activity_config` to `TemporalMCPClient` as it is, so its MCP operations
now heartbeat by default. In Temporal Cloud, that's at most one Action
every 8 seconds of a running request. `heartbeat_timeout=None` turns it
off.
- Strands: `TemporalMCPClient` always passes `heartbeat_timeout`, `None`
unless you set it, so heartbeats stay off by default. If you set it,
long requests stop failing with a heartbeat timeout.

Why not allow 0.2 or 0.3 too? Every GA install should get the same retry
behavior, and CI only tests the locked version, so nobody would test
older versions with the GA plugins. The cost: if you use an older
`temporalio-mcp` with these plugins, you upgrade it together with the
plugin.

Does OpenAI Agents need code changes? No. It imports
`temporalio.mcp._backend`, which is unchanged since 0.2.0, and
`temporalio.mcp._activities._MCPActivities`, whose constructor and
Activities are unchanged; 0.3.0 changes only how they classify errors,
and 0.4.0 only adds the heartbeats.

## What's left?

- Add a Breaking line to both plugins' release notes: they now require
`temporalio-mcp` 0.4, with its lost-session retry and its default
heartbeat timeout.

## Checklist

1. Closes: no issue.

2. How was this tested:

- On this branch, with uv 0.12.24: `make sync`, `make lint` and `make
test` pass in both plugins with `temporalio-mcp` 0.4.0 installed (OpenAI
Agents: 626 passed, 2 skipped; Strands: 279 passed, 1 skipped).
- Before the release, I re-ran the analysis' 178 feature and defect
checks for both plugins with the 0.4.0 wheel the release dry run built,
which is byte for byte the one on PyPI. Only the intended #494 change
shows up: a `heartbeat_timeout` set on Strands' `TemporalMCPClient` no
longer times out MCP requests, and `temporal_mcp_server()` operations
are scheduled with a 10-second heartbeat timeout. Everything else
matches the 0.3.0 run.
- At `main` 10f9082, both suites give the same per-test results on 0.2.0
and 0.3.0 (OpenAI Agents: 565 passed, 1 skipped; Strands: 245 passed).

3. Any docs updates needed? The `temporal_mcp_server()` docstring is
updated here; no README changes. The release notes need the Breaking
line under "What's left?".
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]>
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.

[Bug] mcp: an MCP error from a client built with a factory argument is retried forever

2 participants