Skip to content

mcp: fail an invalid response wrapped in an ExceptionGroup without retrying it - #433

Merged
campoy merged 3 commits into
mainfrom
fix/mcp-validation-error-in-exception-group
Nov 6, 2026
Merged

campoy merged 3 commits into
mainfrom
fix/mcp-validation-error-in-exception-group

Conversation

@campoy

@campoy campoy commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

What was changed

Follow-up to #411, for the rest of #381's bug.

  • _MCPActivities._run now unwraps a lone ValidationError from the ExceptionGroups that Client.__aexit__ adds, the same way mcp: fail an MCPError wrapped in an ExceptionGroup without retrying it #411 unwraps a lone MCPError. An invalid server response from a factory-argument client now fails as MCPProtocolError without retry, as it already did without a factory argument.
  • The invalid-response failure now carries pydantic's JSON form of the validation errors. Why? A model validator can leave an exception object in an error's ctx, for example an input_required result with neither inputRequests nor requestState. Temporal couldn't serialize that, so it swapped our non-retryable failure for a retryable "Failed building exception result" error. This happened with and without a factory argument.
  • test_invalid_server_response_fails_without_retry now runs with and without a factory argument, with mcp's real Client (only the real client wraps errors in groups). It covers a malformed GetPromptResult and an InputRequiredResult whose validator raises ValueError, and checks that the failure details serialize.
  • test_lost_session_from_the_sdk_session_manager_is_retried now also runs with a factory argument. That's where Breaking: retry an MCP operation whose session the server lost (HTTP 404) #379 and mcp: fail an MCPError wrapped in an ExceptionGroup without retrying it #411 meet, and no test covered it.

Why?

#411, by pcbeingused333, stopped retrying an MCPError that a factory-argument client wraps in ExceptionGroups. Great fix! But what about an invalid response from the server? That one still came back wrapped, and Temporal retried it forever. This PR closes that gap.

With a factory argument, an invalid server response (a pydantic ValidationError) arrived wrapped in an ExceptionGroup. It wasn't an ApplicationError, so Temporal retried it under the default policy, with no limit. And validation details holding an exception object couldn't be serialized, so those failures were retried without limit too.

Checklist

  1. Closes: no issue. Follow-up to mcp: fail an MCPError wrapped in an ExceptionGroup without retrying it #411, for the rest of [Bug] mcp: an MCP error from a client built with a factory argument is retried forever #381's bug.

  2. How was this tested:

  • Without the fix, test_invalid_server_response_fails_without_retry[acme] fails; the other cases pass.
  • With the fix, the whole python/mcp suite passes (97 tests), and ruff, pyright, basedpyright, mypy and pydocstyle are clean.
  • A small workflow on a local dev server, with maximum_attempts=2, calls the get-prompt Activity with a client that returns an invalid result:
    • malformed messages: one attempt, non-retryable MCPProtocolError, with and without a factory argument;
    • input_required with neither alternative: before, two attempts ending in "Object of type ValueError is not JSON serializable"; now one attempt, non-retryable MCPProtocolError, with and without a factory argument.
  1. Any docs updates needed? No. The MCP README already says schema-invalid server responses are non-retryable.

…trying it

#411 unwraps a lone MCPError from the ExceptionGroups that Client.__aexit__
adds when a client comes from a factory argument, but an invalid server
response (a pydantic ValidationError) still arrived wrapped and was
retried. Unwrap a lone ValidationError too, so it fails as
MCPProtocolError without retry, as it does without a factory argument.

Extend the invalid-response and SDK lost-session tests to run with and
without a factory argument.
@campoy
campoy requested a review from a team as a code owner November 6, 2026 00:11
@campoy
campoy marked this pull request as draft November 6, 2026 00:13
@campoy
campoy marked this pull request as ready for review November 6, 2026 00:23
@campoy
campoy requested a review from brianstrauch November 6, 2026 00:40
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

campoy commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Here's what I ran with this fix in place, on top of the tests in the description.

How did I test it with the plugins?

I built a local candidate: the GA release candidate plus #433, #434 (before its test-helper change) and #435. I built temporalio-mcp from that tree as 0.3.0 and installed it over each plugin's lock. Then I reran everything we have for the two plugins that use temporalio-mcp: OpenAI Agents and Strands (both Python). I compared each result with the same check on the release candidate, which used temporalio-mcp 0.2.0.

Check OpenAI Agents Strands
Plugin CI suite on temporalio-mcp 0.3.0 544 passed, 1 skipped 230 passed
Defect fix checks 14, all the same as before 15, all the same as before
Feature evidence checks 80, all the same as before all of them, the same as before

temporalio-mcp's own CI passed on #433 and #434: macOS, Ubuntu (Python 3.10 and 3.14) and Windows.

What changed, on purpose?

What didn't change?

The items that failed on the release candidate still fail the same way here, and none of them comes from temporalio-mcp. In OpenAI Agents, two defect fix checks and five feature checks were already failing on the release candidate. Only three defect checks and one feature item in OpenAI Agents go through the temporalio-mcp route; the rest of its MCP evidence uses the deprecated providers, which don't import it.

Anything flaky, or not run?

  • A few first runs broke while my laptop was asleep or Docker restarted. Every rerun matched the earlier result.
  • Not run, as on the release candidate: the Docker and ssh routes of the credential checks (the image has neither) and two replay items that were never part of the run.

@campoy
campoy merged commit 8495a45 into main Nov 6, 2026
15 checks passed
@campoy
campoy deleted the fix/mcp-validation-error-in-exception-group branch November 6, 2026 17:33
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?".
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