Skip to content

[Bug] mcp: an MCP factory that does not return mcp.Client fails with an unclear error that retries forever #320

Closed

Description

@campoy

Package: temporalio-mcp 0.2.0 and main (1e8ae93), whose factory and pool code differs only in type annotations.

What happens

MCPPlugin calls each factory inside the Activity and uses the result as an mcp.Client without checking it. A factory that returns something else fails every MCP Activity with an AttributeError about that object. Examples: the stdio transport without the Client(...) wrapper, or another framework's client left over from an upgrade. Temporal retries that error, so under the default retry policy the Activity retries forever. The message also doesn't say that the factory returned the wrong type.

temporalio-openai-agents builds its MCP activities on the same code and casts its factory's result to an OpenAI MCPServer the same way (_mcp_server_backend_factory), so its factories behave the same.

#190 adds this check to the Strands plugin's own factory wrapper. A review there found that a factory left returning Strands' MCPClient after the upgrade retried forever with 'MCPClient' object has no attribute '__aenter__'.

Reproduction

One file, no Temporal server, network or API key. It runs the plugin's list-tools Activity with ActivityEnvironment and exits 1 when the Activity fails with an error that is retried and doesn't name mcp.Client.

"""An MCPPlugin factory that does not return an mcp.Client fails with an unclear, retryable error.

python issue_repro.py    (no Temporal server, network or API key needed)

The factory below forgets to wrap the stdio transport in mcp.Client. MCPPlugin
registers it without complaint, and the list-tools Activity then fails with an
AttributeError about the transport object. Temporal retries any error that is not
a non-retryable ApplicationError, so under the default retry policy the Activity
retries forever, and the message does not say that the factory returned the wrong
type. Exits 1 when the Activity fails that way.
"""

import asyncio
import sys

from mcp import StdioServerParameters, stdio_client

from temporalio.exceptions import ApplicationError
from temporalio.mcp import MCPPlugin
from temporalio.testing import ActivityEnvironment

SERVER = StdioServerParameters(command=sys.executable, args=["-c", "import time; time.sleep(30)"])


async def main() -> int:
    plugin = MCPPlugin({"weather": lambda: stdio_client(SERVER)})  # should be Client(stdio_client(...))
    activities = plugin.activities
    assert isinstance(activities, list)
    list_tools = next(a for a in activities if a.__temporal_activity_definition.name.endswith("list-tools"))
    try:
        await asyncio.wait_for(ActivityEnvironment().run(list_tools, {"factory_argument": None}), 20)
    except ApplicationError as err:
        print(f"ApplicationError non_retryable={err.non_retryable}: {err.message}")
        return 0 if err.non_retryable or "mcp.Client" in err.message else 1
    except Exception as err:
        print(f"{type(err).__name__} (retried by Temporal): {err}")
        return 1
    print("list-tools succeeded")
    return 0


sys.exit(asyncio.run(main()))

Output, with temporalio 1.34.0, temporalio-mcp 0.2.0 and mcp 2.1.1 on Python 3.13:

$ python issue_repro.py; echo "exit=$?"
AttributeError (retried by Temporal): '_AsyncGeneratorContextManager' object has no attribute 'protocol_version'
exit=1

Suggested change

In _mcp_client_backend_factory's create(), check that the factory returned an mcp.Client. If not, raise an ApplicationError that names the server and the returned type. Keep it retryable, as #190 does, so a run waits for the worker to be fixed instead of failing. The OpenAI Agents adapter could check for MCPServer the same way. Once temporalio-mcp checks, the Strands plugin can drop its own check.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions