Skip to content

[python/mcp] breaking: fail an MCP factory that returns the wrong type without retries - #539

Open
campoy wants to merge 2 commits into
mainfrom
fix/mcp-factory-type-non-retryable
Open

campoy wants to merge 2 commits into
mainfrom
fix/mcp-factory-type-non-retryable

Conversation

@campoy

@campoy campoy commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

What was changed

When an MCPPlugin factory returns something other than an mcp.Client, the MCP Activity fails with MCP server factory 'weather' returned builtins.object, not mcp.Client. That error is retryable today, so the default retry policy repeats it without limit.

It is now non-retryable, with type MCPServerFactoryTypeError. The message doesn't change. The existing test now checks the type and the non-retryable flag.

It also adds the two checks #531 added to Google GenAI:

  • A factory that isn't callable, such as a client passed in its place, makes MCPPlugin(...) raise TypeError, so the Worker doesn't start:

    MCP server factory 'weather' is mcp.client.client.Client, not callable
    
  • When an async factory returns a coroutine, the coroutine is closed before the Activity fails, so Python doesn't warn that it was never awaited.

The startup check is in MCPPlugin, not in the shared _FactoryInvoker, so OpenAI Agents Python, which imports that class, keeps its current behavior.

Why?

A factory that returns the wrong type is a configuration error. Failing it without retries is the behavior we agreed for the MCP factory checks in the other plugins, and they use the same error type:

#441 added this check as retryable, before that agreement. This makes temporalio-mcp match the rest.

Is it breaking? (Breaking)

Factories that return the wrong type already failed, but those failures were retried. They now stop the Activity without retries. This also applies to the next attempt of an Activity that is already running. Fix these factories before upgrading: deploying a fixed Worker won't resume an Activity that has already failed.

A factory that isn't callable used to start the Worker and then fail every MCP Activity with a retryable TypeError. Now MCPPlugin(...) raises, even when no workflow uses that server.

Replay isn't affected: no Workflow code changes.

Checklist

  1. Closes: no issue. Related to Breaking: mcp: report an MCP factory that returns the wrong type #441, Breaking: report an OpenAI Agents MCP server factory that returns the wrong type #451 and [Bug] mcp: an MCP factory that does not return mcp.Client fails with an unclear error that retries forever #320.

  2. How was this tested:

    • The extended test fails on the current code (the error has no type) and passes with the change. It now also runs with an async factory, and first passes the returned value in place of the factory. Without the startup check it fails with DID NOT RAISE TypeError, and without the close() with CORO_CREATED.
    • make test in python/mcp on Python 3.12: 140 passed.
    • make lint in python/mcp: ruff, formatting, pyright, mypy, basedpyright and pydocstyle passed.
    • git diff --check passed.
    • Other plugins test against the published temporalio-mcp 0.4, so they pick this up only after a release.
  3. Any docs updates needed? The README's "Errors and retries" section now describes both factory errors.

A factory that returns something other than an mcp.Client now fails the
Activity with a non-retryable ApplicationError of type
MCPServerFactoryTypeError. The message doesn't change.

This matches the MCP factory checks in the other plugins, which all
fail without retries and use the same error type.
@campoy
campoy requested a review from a team as a code owner November 14, 2026 01:57
campoy added a commit that referenced this pull request Nov 16, 2026
#531)

## What was changed

A Google GenAI MCP factory that returns `None` or another value that is
not an async context manager currently fails with a retryable runtime
error.

Check the factory's return value with `isinstance(context,
AbstractAsyncContextManager)` before entering it. A wrong type fails
with a non-retryable `MCPServerFactoryTypeError` that names the factory
and the returned type. A returned coroutine is closed before rejection
so Python doesn't warn that it was never awaited.

This follows #451 and #530: invalid factory results are configuration
errors, while connection failures stay retryable. #377 and #539 propose
the same change for Strands Python and `temporalio-mcp`.

A factory that isn't callable, such as an already-built context manager
passed in its place, now fails with a `TypeError` when the plugin builds
its MCP Activities, so the Worker doesn't start:

```
MCP server factory 'weather' is contextlib._AsyncGeneratorContextManager, not callable
```

A factory that fails before its session is ready, including one that
returns the wrong type, no longer makes asyncio log "Task exception was
never retrieved". The connection's owner task now gives the error only
to the waiting calls, which raise it unchanged.

The yielded session is not inspected. No dependency changes or new
`temporalio-mcp` release are needed.

## Is it breaking? (Breaking)

Wrong factory return types now stop the Activity without retries,
including on the next attempt of an in-flight Activity.

Fix these factories before upgrading Workers. Deploying a corrected
Worker will not automatically resume an Activity that has already
failed. No Workflow code changes.

A factory that isn't callable now stops the plugin from being built, so
the Worker doesn't start, even when no Workflow uses that server.

## Checklist

1. Related: #451 and #530. No issue to close.

2. How was this tested:
- Existing Docker test image with Python 3.13.16, uv 0.13.1 and the
committed lockfile.
- Full Google GenAI suite: 221 passed, 1 skipped. The offline container
used its existing Temporal CLI 1.9.1 (Server 1.32.0), selected through a
temporary pytest plugin outside the repository. The test suite's pinned
CLI could not be downloaded offline.
   - Focused MCP factory, transport and protocol tests: 26 passed.
- Warning assertions cover only the coroutine created by each test, and
asyncio error assertions cover only its connection-owner task. Both
coroutine-return cases fail before the cleanup with "coroutine was never
awaited" and pass after it. The factory test also checks the
constructor's `TypeError` and the Activity's named, non-retryable error.
- The transport test now covers `ConnectionRefusedError` before
readiness for both MCP Activities. It checks that the same exception
reaches the caller, Temporal converts it to a retryable failure, the
connection is evicted, and asyncio reports no unobserved exception.
- `make lint` passed: ruff, formatting, pyright, mypy, basedpyright and
pydocstyle.
   - `git diff --check` passed.

3. Any docs updates needed? The README explains the factory contract and
upgrade impact.
MCPPlugin(...) now raises TypeError for a factory that isn't callable, so
the Worker doesn't start. When an async factory returns a coroutine, the
coroutine is closed before the Activity fails, so Python doesn't warn that
it was never awaited. Google GenAI does the same since #531.
@campoy campoy changed the title Breaking: mcp: fail an MCP factory that returns the wrong type without retries [mcp-python] breaking: fail an MCP factory that returns the wrong type without retries Nov 16, 2026
campoy added a commit that referenced this pull request Nov 16, 2026
…lable (#593)

## What was changed

`OpenAIAgentsPlugin(mcp_servers=...)` now checks that each factory is
callable when the plugin is built. If it isn't, it raises `TypeError`,
so the Worker doesn't start:

```
MCP server factory 'filesystem' is agents.mcp.server.MCPServerStreamableHttp, not callable
```

Today a server passed in place of its factory gets past Worker start.
Then every MCP Activity for that server fails with `TypeError:
'MCPServerStreamableHttp' object is not callable`, which the default
retry policy retries forever.

There's a second, smaller fix. When an async factory returns a
coroutine, the Activity already fails with `MCPServerFactoryTypeError`
(#451), but Python also warns that the coroutine was never awaited. The
coroutine is now closed first.

Both checks live in this plugin's `_mcp_server_backend_factory`, like
`MCPPlugin`'s in #539, not in the `_FactoryInvoker` this plugin imports
from `temporalio-mcp`.

## Why?

The other MCP plugins refuse a factory that isn't a function when the
Worker is built: #529, #531, #534, #535, #536 and #555, plus
`temporalio-mcp` and Strands Python with #539 and #377. #531 also closes
a returned coroutine, after its review asked for it. This brings OpenAI
Agents Python in line.

## Is it breaking? (Breaking)

A Worker with a factory that isn't callable used to start, even though
every MCP Activity for that server failed and retried. Now it doesn't
start, even when no workflow uses that server. Fix the factory before
upgrading. No Workflow code or Activity names change.

The deprecated `mcp_server_providers` route doesn't change. Its
providers don't check what the factory returns at all, and I left them
alone because they're deprecated.

## Checklist

1. Closes: no issue. Follow-up to #451.

2. How was this tested:
- The existing test in `test_mcp_v2.py` now also runs with an async
factory, and first passes the returned value in place of the factory.
Without the startup check it fails with `DID NOT RAISE TypeError`, and
without the `close()` with `CORO_CREATED`. It passes with both.
   - `make test` on Python 3.12: 644 passed, 2 skipped.
   - `make lint` passed.
   - `git diff --check` passed.

3. Any docs updates needed? The README's MCP section now describes both
factory errors.
@campoy campoy changed the title [mcp-python] breaking: fail an MCP factory that returns the wrong type without retries [python/mcp] breaking: fail an MCP factory that returns the wrong type without retries Nov 16, 2026

This branch has not been deployed

No deployments
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.

1 participant