Skip to content

mcp: fail a repeated pagination cursor without retrying it when the client has a factory argument - #455

Merged
campoy merged 1 commit into
mainfrom
mcp-repeated-cursor-factory-argument
Nov 9, 2026
Merged

campoy merged 1 commit into
mainfrom
mcp-repeated-cursor-factory-argument

Conversation

@campoy

@campoy campoy commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

What was changed

When an MCP server repeats a pagination cursor, the list operations raise a non-retryable MCPProtocolError. That works without a factory argument. With one, the Activity fails with a retryable ExceptionGroup instead, and the default retry policy retries it forever.

Why?

A server that repeats a cursor will repeat it on every attempt, so a retry can't help. Each attempt also replaces the pooled connection. In a real workflow with maximum_attempts=2, the same server gave one attempt without a factory argument and two attempts (MAXIMUM_ATTEMPTS_REACHED) with "acme". With the default retry policy, there is no limit.

This reaches MCPPlugin, and OpenAIAgentsPlugin(mcp_servers=...) and StrandsPlugin(mcp_clients=...) once they use this release.

Checklist

  1. Closes: no issue.

  2. How was this tested:

  • This script lists tools from a server that always returns the same cursor:

    import asyncio
    
    from mcp import Client, types
    from mcp.server.lowlevel import Server
    
    from temporalio import activity
    from temporalio.exceptions import ApplicationError
    from temporalio.mcp import MCPPlugin
    from temporalio.testing import ActivityEnvironment
    
    
    async def list_tools(_ctx, _params):
        return types.ListToolsResult(tools=[], next_cursor="again")
    
    
    async def main():
        server = Server("repeats", on_list_tools=list_tools)
        for argument in (None, "acme"):
            plugin = MCPPlugin({"repeats": lambda _arg: Client(server)})
            fn = next(
                fn for fn in plugin.activities
                if activity._Definition.from_callable(fn).name.endswith("list-tools")
            )
            try:
                async with plugin._mcp_activities.run_context():
                    await ActivityEnvironment().run(fn, {"factory_argument": argument})
            except BaseException as error:
                non_retryable = isinstance(error, ApplicationError) and error.non_retryable
                print(f"factory_argument={argument!r}: {type(error).__name__}; non_retryable={non_retryable}")
                print(f"  {error}")
    
    
    asyncio.run(main())

    On main (224d5d5, mcp 2.3.0, temporalio 1.35.0):

    factory_argument=None: ApplicationError; non_retryable=True
      MCPProtocolError: MCP server returned a repeated pagination cursor
    factory_argument='acme': ExceptionGroup; non_retryable=False
      unhandled errors in a TaskGroup (1 sub-exception)
    

    With this change:

    factory_argument=None: ApplicationError; non_retryable=True
      MCPProtocolError: MCP server returned a repeated pagination cursor
    factory_argument='acme': ApplicationError; non_retryable=True
      MCPProtocolError: MCP server returned a repeated pagination cursor
    
  • Without the fix, the 4 factory-argument cases of the updated test fail with ExceptionGroup. With it, all 8 pass.

  • The whole python/mcp suite passes (119 tests), and ruff, pyright, mypy, basedpyright and pydocstyle are clean. My local uv is older than the repo's pin, so I ran them in a venv built from the committed lock (uv export --frozen), not through make. CI runs the real targets.

  1. Any docs updates needed? No.

…lient has a factory argument

The list operations raise a non-retryable MCPProtocolError when the server
repeats a pagination cursor. With a factory argument, mcp's Client wraps that
error in ExceptionGroups on exit, and _run did not unwrap an ApplicationError,
so Temporal retried the group forever. Unwrap a lone ApplicationError and
raise it as is.
@brianstrauch brianstrauch reopened this Nov 9, 2026
@campoy
campoy merged commit e9ba870 into main Nov 9, 2026
28 checks passed
@campoy
campoy deleted the mcp-repeated-cursor-factory-argument branch November 9, 2026 18:53
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