Skip to content

Breaking: mcp: report an MCP factory that returns the wrong type - #441

Merged
campoy merged 3 commits into
mainfrom
fix/mcp-factory-type-check
Nov 9, 2026
Merged

campoy merged 3 commits into
mainfrom
fix/mcp-factory-type-check

Conversation

@campoy

@campoy campoy commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

What was changed

What happens when an MCPPlugin factory returns something that isn't an mcp.Client? Today the Activity fails with an AttributeError about a private class. It doesn't name the server or the factory, and the default retry policy retries it forever. With this PR, the Activity fails with an error that says what went wrong:

MCP server factory 'weather' returned contextlib._AsyncGeneratorContextManager, not mcp.Client

The OpenAI Agents half of this change now lives in its own PR, #451, so temporalio-mcp 0.3.0 only waits for this one.

  • _mcp_client_backend_factory in _client.py checks isinstance(client, Client) before using the client.
  • A new test runs the list-tools Activity with a factory that returns object().

The error is retryable, like the Strands check from #190. After a bad deploy, runs wait for a fixed Worker instead of failing. And the check runs before anything opens, so a wrong factory no longer starts a stdio server first.

Why?

How does a factory return the wrong type? It might return a stdio transport without the Client(...) wrapper. Or it might return a Strands MCPClient left over from before #190, which is what a review on #190 found. Python doesn't stop it unless you run a type checker.

Is it breaking? (Breaking)

Only for look-alikes. A look-alike is an object with every method the adapter calls that isn't an mcp.Client, such as a wrapper or a test double. It worked before, and now it fails every MCP Activity with this retryable error. Before upgrading, return the real client or a subclass. Any other wrong type already failed, with an AttributeError. The README shows factories that return the real type, and no plugin in this repository passes its own client type through this factory.

Replay isn't affected: the check runs in the Activity, and no Workflow code changes.

What else did we consider?

  • A non-retryable error. The run fails at once, so nothing loops unnoticed. But a bad deploy then fails every in-flight run that uses the server, and fixing the Worker doesn't bring them back. It would also behave differently from the Strands check.
  • Check for the methods with a typing.Protocol. This keeps look-alikes working. But a @runtime_checkable Protocol only checks that the names exist: a sync list_tools(self) passes, and since Python 3.12 a proxy that forwards through __getattr__ fails. Checking signatures too would mean our own inspect.signature checks on the ten mcp.Client members the adapter uses, kept in step with every SDK release.
  • Rewrite the AttributeError where it happens. This is compatible. But it also relabels real AttributeErrors from inside a correct client as "wrong type", and it reports the problem only after the object was entered.
  • Check when the plugin is built. The only way to learn the return type is to call the factory. That opens a connection or starts a process, and some factories need a factory_argument that only a Workflow supplies.

What if someone needs look-alikes later? We can loosen this without breaking anyone: keep isinstance(x, Client), and also accept objects that match a runtime_checkable Protocol of the members the adapter calls. Everything this check accepts still passes; only the message for rejected objects changes. The other order, a Protocol first and isinstance later, would break people, so this is the safe first step.

What's not covered?

Checklist

  1. Closes [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:

  • [Bug] mcp: an MCP factory that does not return mcp.Client fails with an unclear error that retries forever #320's script: on main it exits 1 with AttributeError: '_AsyncGeneratorContextManager' object has no attribute 'protocol_version'. With the change it exits 0 with the retryable error above.
  • The new test fails on main's code and passes with the change.
  • After merging the latest main, the whole python/mcp suite passes (113 tests), and ruff, pyright, basedpyright, mypy and pydocstyle are clean.
  • No Workflow code changes, so no replay check was needed.
  1. Any docs updates needed? No. The README already shows factories that return the real type.

@campoy
campoy force-pushed the fix/mcp-factory-type-check branch from b252717 to 595cb48 Compare November 6, 2026 23:15
@campoy
campoy marked this pull request as ready for review November 9, 2026 16:50
@campoy
campoy requested a review from a team as a code owner November 9, 2026 16:50
temporalio-mcp 0.3.0 only waits for the python/mcp check. The OpenAI Agents check doesn't depend on it and ships with that plugin's own release.
@campoy campoy changed the title Breaking: report an MCP factory that returns the wrong type Breaking: mcp: report an MCP factory that returns the wrong type Nov 9, 2026
@campoy
campoy removed the request for review from xumaple November 9, 2026 17:11
@brianstrauch brianstrauch reopened this Nov 9, 2026
@campoy
campoy merged commit 10f9082 into main Nov 9, 2026
40 checks passed
campoy added a commit that referenced this pull request Nov 15, 2026
… wrong type (#451)

## What was changed

An OpenAI Agents `mcp_servers` factory that returns the wrong type
currently fails its MCP Activity with an opaque `AttributeError`, which
the default retry policy retries without limit. This change reports the
server name and the returned type:

```
MCP server factory 'hello' returned builtins.object, not agents.mcp.MCPServer
```

The error has type `MCPServerFactoryTypeError` and is non-retryable,
matching the adapter's other factory configuration checks. The Activity
stops at once so the caller can handle the failure.

The check runs before the adapter uses the returned object. The existing
factory test checks the message, error type and non-retryable flag.

## Why?

Part of #320. OpenAI Agents has its own adapter for `MCPServer`
factories, so the `mcp.Client` check in #441 does not cover it. This
change works with `temporalio-mcp` 0.2.0.

## Is it breaking? (Breaking)

Objects that implement the methods the adapter calls without inheriting
from `MCPServer` previously worked. They now fail the Activity. Return
an `MCPServer` or a subclass before upgrading.

Other wrong types already failed, but their `AttributeError` was
retryable. They now fail without retries. This also applies to the next
attempt of an in-flight Activity, so fix these factories before
upgrading. Deploying a corrected Worker will not automatically resume an
Activity that has already failed.

The check runs in the Activity; no Workflow code changes.

## Checklist

1. Closes: no issue. Part of #320.

2. How was this tested:
- `make test PYTEST_ARGS='-n 0 tests/test_mcp_v2.py
tests/test_installed_matches_source.py -q'`: 31 passed.
- `make lint`: ruff, formatting, pyright, mypy, basedpyright and
pydocstyle passed.
   - `git diff --check` passed.

3. Any docs updates needed? No. The error names the server and the
required type.
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 factory that does not return mcp.Client fails with an unclear error that retries forever

2 participants