Skip to content

Breaking: report an OpenAI Agents MCP server factory that returns the wrong type - #451

Merged
campoy merged 2 commits into
mainfrom
fix/openai-agents-mcp-server-factory-type-check
Nov 15, 2026
Merged

campoy merged 2 commits into
mainfrom
fix/openai-agents-mcp-server-factory-type-check

Conversation

@campoy

@campoy campoy commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

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 [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:

    • 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.

Split from #441. OpenAI Agents' mcp_servers adapter now checks that each factory returns an agents.mcp.MCPServer, and otherwise fails the Activity with a retryable error that names the server and the type it got. It doesn't depend on a temporalio-mcp release.
server = cast(MCPServer, invoke(argument))
server = invoke(argument)
if not isinstance(server, MCPServer):
# Retryable, so a run waits for the worker to be fixed instead of failing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

making it retryable doesn't match the rest of this project. I would make it non-retryable to match the checks next to it, and give it a type. Thoughts?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I updated it to be non-retryable to match the other errors as well as other plugins

campoy added a commit that referenced this pull request Nov 13, 2026
)

## What was changed

An MCP factory that returns `undefined`, `null` or an incomplete server
currently fails an Activity with an ordinary runtime error, which
Temporal can retry. Both OpenAI Agents TypeScript providers now reject
these results with a non-retryable `ApplicationFailure` of type
`MCPServerFactoryTypeError`.

A shared check requires callable `connect`, `close`, `listTools` and
`callTool` methods. It runs before the stateless Activities connect and
before the stateful session Activity registers or connects the server.
The message names the factory and the required methods. Plain objects
implementing those methods still work.

Exceptions thrown by the factory or by a valid server keep their
existing handling. This change needs no new `temporalio-mcp` release.

## Is it breaking? (Breaking)

An invalid factory result now stops the Activity without retries,
including on the next attempt of an in-flight Activity. A partial server
that previously worked for one operation must provide all four methods.

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

## Checklist

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

2. How was this tested:
- Locked dependencies with pnpm 10.27.0 and Node 24.22.0 in an existing
Docker test image.
- `make lint`: build and lint passed, with eight existing deprecation
warnings.
- `make test TEST_ARGS='lib/__tests__/mcp-provider.test.js
lib/__tests__/test-openai-agents-mcp-tools.js
lib/__tests__/test-openai-agents-mcp-retries.js'`: 16 tests passed
against a local Temporal server.
- Tests check both stateless operations and the stateful session, named
non-retryable failures, one factory call per Activity execution, and no
connect or close calls for invalid results. Existing structural-server
and connection-retry tests pass.
   - `git diff --check` passed.

3. Any docs updates needed? The README explains the check and the
upgrade impact.
campoy added a commit that referenced this pull request Nov 14, 2026
## What was changed

When a Vercel AI SDK MCP client factory resolves to `undefined`, `null`,
or an object without the required methods, the Activity fails with an
opaque `TypeError`. The default retry policy repeats that failure
without limit, and cleanup can hide the original error with another
`TypeError` about `close`.

Validate each factory result before using it. A client must expose
callable `tools()` and `close()` methods, the two methods this adapter
uses. An invalid result fails with a non-retryable
`MCPClientFactoryTypeError` that names the factory and what it returned:

```
MCP client factory 'weather' returned Client, not a client with tools() and close() methods.
```

If a rejected result still has a callable `close()`, such as a connected
MCP SDK `Client`, it is closed first so the connection doesn't leak. An
error from that `close()` doesn't replace the validation failure.

A factory that isn't a function, such as a connected client passed in
its place, fails with a `TypeError` when the Worker is built:

```
MCP client factory 'weather' is DefaultMCPClient, not a function.
```

The check covers list-tools and call-tool Activities, with connection
pooling enabled or disabled. Factories that throw or reject pass their
errors through to the Activity retry policy. Objects that implement the
required methods, including test doubles, remain supported.

## Why?

Wrong factory results are configuration errors. Failing them without
retries is the behavior we agreed for the MCP factory checks in the
other plugins: OpenAI Agents TypeScript (#530), OpenAI Agents Python
(#451), Strands Python (#377), Strands TypeScript (#535), Google GenAI
(#531) and Google ADK (#534, #536). This change is contained in the
Vercel AI SDK plugin and needs no `temporalio-mcp` release.

## Is it breaking? (Breaking)

Invalid clients now stop the Activity without retries. A client missing
`close()` could previously serve pooled requests until eviction; it is
now rejected before use. The check also runs on the next attempt of an
in-flight Activity. Fix these factories before upgrading: deploying a
corrected Worker will not automatically resume an Activity that has
already failed.

A factory that isn't a function now stops the Worker from starting, even
when no Workflow uses that server.

No Workflow code or Activity names change. The README includes the
factory requirements and upgrade warning.

## Checklist

1. Closes: no issue. Related to #451, #377 and #530.

2. How was this tested:
- The invalid-client cases fail on the original code with `TypeError`s
from `tools` or `close`, and pass with the change. They check the
message for each returned value, and that rejected clients with a
callable `close()` are closed, including when `close()` throws or
rejects. They also pass each invalid value in place of the factory and
check the `TypeError`.
- The same test returns a connected MCP SDK `Client` from the factory
and checks that its in-memory server sees the connection close, in all
four operation and pooling combinations.
- Removing each fix in turn (the factory check, closing rejected
clients, closing only the MCP SDK `Client`) fails all four
invalid-client tests.
- `make test TEST_ARGS='lib/__tests__/test-ai-sdk-mcp-connections.js'`:
14 passed. Covers invalid clients, unchanged factory failures, and
existing connection reuse and cleanup behavior.
- `make test TEST_ARGS='lib/__tests__/test-ai-sdk-mcp-tools.js
lib/__tests__/test-ai-sdk-plugin.js'`: 5 passed, 1 existing test
skipped. The MCP Workflow test uses a real AI SDK MCP client.
   - `make lint`, `make build` and `git diff --check` passed.
- Local checks used Node 22.23.4 and the repository's pinned pnpm
10.27.0. The lockfile and package-manager settings are unchanged.

3. Any docs updates needed? The plugin README is updated.
@campoy
campoy merged commit e3fef4e into main Nov 15, 2026
27 checks passed
@campoy
campoy deleted the fix/openai-agents-mcp-server-factory-type-check branch November 15, 2026 01:25
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.
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 added a commit that referenced this pull request Nov 16, 2026
…and correct the MCP README (#377)

## What was changed

Misconfigured Strands MCP factories can fail every Activity attempt and
retry without limit. This PR rejects factories that need arguments or
aren't callable when the plugin is built. It reports wrong client types
and function transports as typed, non-retryable Activity errors.

- `StrandsPlugin(mcp_clients=...)` refuses a factory that needs
arguments with a `TypeError` that names the server. On Python 3.14+, the
check reads the factory's parameters without evaluating its annotations,
so `-> Client` with `Client` imported only under `TYPE_CHECKING` still
works.
- It also refuses a factory that isn't callable, such as a client passed
in its place: `MCP server factory 'weather' is mcp.client.client.Client,
not callable`.
- A factory that returns something other than an `mcp.Client` fails with
type `MCPServerFactoryTypeError`. When an async factory returns a
coroutine, the coroutine is closed first, so Python doesn't warn that it
was never awaited.
- A factory that returns an `mcp.Client` built on a plain function fails
with type `MCPServerFactoryTransportError`. This catches `Client(lambda:
stdio_client(...))`, the shape an old Strands factory takes when only
its import changes.

Both Activity errors name the server and the fix, and set
`non_retryable=True`, matching the factory configuration checks in #451.

The README and plugin docstring also clarify that factories run for
every connection and must return a fresh client and transport. The
README explains the `factory_argument` limit, resumed interrupts, and
the retry, idle-timeout and connection changes from #190. It also covers
rejected MCP results. With `temporalio-mcp` 0.4, which this plugin
requires since #435, two kinds of rejected result reach the agent at
once as a failed tool result, without a retry: a malformed result the
MCP client can't parse, and a result that breaks the tool's output
schema.

The tests run the factory checks through the plugin's list-tools
Activity and check the error types and non-retryable flags. Factories
that take no arguments (a lambda, a defaulted parameter, a callable
object, a `functools.partial`, and on 3.14+ an annotation that exists
only under `TYPE_CHECKING`) list tools through the same Activity. The
tests also cover idle-timeout forwarding, tool filtering and failed tool
results. A malformed result and an output-schema mismatch go through the
call-tool Activity, with a valid result as the control. Both rejected
results fail with a non-retryable `MCPProtocolError`.

## Why?

Follow-up to the [last review of
#190](#190 (review)),
which found these factory checks and README gaps.

## Is it breaking? (Breaking)

Wrong client types and function transports already failed, but those
failures were retryable. They now stop the Activity without retries,
including on the next attempt of an in-flight Activity. Fix these
factories before upgrading: deploying a corrected 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
`StrandsPlugin(...)` raises, even when no workflow uses that server.

The README includes these upgrade warnings. No Workflow code changes.

## Checklist

1. Related: #320, #376, #435 and #451. No issue to close.

2. How was this tested:
- `make test PYTEST_ARGS='-n 4 -q'` on Python 3.13: 293 passed, 2
skipped.
   - The same on Python 3.14: 295 passed.
- `make lint` on Python 3.13 and 3.14: ruff import sorting and
formatting, pyright, mypy, basedpyright and pydocstyle passed. pyright,
mypy and basedpyright also passed when targeting Python 3.11 and 3.15. I
couldn't set up a 3.15 environment locally (`pydantic-core` has no 3.15
wheel yet), so the CI cell covers a real 3.15 run.
- After adding the startup check and the coroutine close: `make test` on
Python 3.12, 294 passed, and `make lint` passed. The extended factory
test fails without each of the two changes.
   - `git diff --check` passed.

3. Any docs updates needed?
   - The plugin README's MCP section is updated.
- Outside this repo, to land with the release:
`strands_plugin/mcp/run_worker.py` in temporalio/samples-python, and the
Strands page in temporalio/documentation, which renders that sample.
Both still build Strands' `MCPClient` and don't install the `mcp` extra.
campoy added a commit that referenced this pull request Nov 17, 2026
## What was changed

An MCP toolset factory that returns `None` or another wrong type
currently fails with a retryable `AttributeError`, often from cleanup
rather than the requested operation. Check that each factory result is
an ADK `BaseToolset`, such as `McpToolset`, before using it.

An invalid result fails with a non-retryable
`MCPToolsetFactoryTypeError` that names the factory and what it
returned:

```
MCP toolset factory 'weather' returned builtins.NoneType, not a BaseToolset
```

Both stateless operations and the stateful session use the check.
`BaseToolset` defines `get_tools()` and `close()`, the only methods the
providers call, and #534 does the same check in TypeScript. Factory
exceptions keep their existing handling.

A factory that isn't callable, such as a toolset passed in its place,
now fails with a `TypeError` when `TemporalMcpToolSetProvider` or
`TemporalStatefulMcpToolSetProvider` is built:

```
MCP toolset factory 'weather' is google.adk.tools.mcp_tool.mcp_toolset.McpToolset, not callable
```

This changes only factory validation and needs no new dependency or MCP
package release.

## Is it breaking? (Breaking)

Invalid results now stop the Activity without retries, including on the
next attempt of an in-flight Activity. This includes a duck-typed
toolset that doesn't subclass `BaseToolset`. Fix these factories before
upgrading Workers. Deploying a corrected Worker will not automatically
resume an Activity that has already failed. No production Workflow code
or Activity names change.

A toolset factory that isn't callable now stops the Worker from
starting, even when no Workflow uses that server.

## Checklist

1. Related: #451, #529 and #530. No issue to close.
2. Validation:
- The stateless wrong-result cases fail on the original code and pass
with the check. They cover `None`, `object()` and a duck-typed toolset
that isn't a `BaseToolset`, check the message for each, and pass each
value in place of the factory to both providers to check the
`TypeError`.
- The stateful case uses the default retry policy and verifies a typed
non-retryable failure, naming `builtins.NoneType`, after one factory
call. Existing valid-toolset and factory-failure cases pass.
- Removing the callable check fails both stateless cases. Removing the
`BaseToolset` check fails them and the stateful case, and going back to
the duck-typed check fails both stateless cases.
   - `make test PYTEST_ARGS='-n 4'`: 331 passed, 6 skipped.
   - `make lint`, `make build` and `git diff --check` passed.
- Python checks used uv 0.13.5, with the committed lockfile unchanged.
3. Docs: the README names the required type and has the upgrade warning.
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.

3 participants