Skip to content

[Bug] mcp: an MCP tool result the client rejects, such as one that breaks its output schema, retries forever #376

Closed

Description

@campoy

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

What happens

MCP Python SDK v2's Client rejects some results on the client side and raises RuntimeError. One case is a tool that declares an outputSchema but returns a result without structured content, or with structured content that doesn't match the schema (mcp/client/session.py). Another is InputRequiredRoundsExceededError, a RuntimeError subclass, when a multi-round call runs past its round limit.

_MCPActivities._run maps only MCPError and pydantic ValidationError to ApplicationError. A RuntimeError passes through, and Temporal retries it, so under the default retry policy the call-tool Activity retries forever. Each failure also retires the pooled connection, so for stdio every retry starts a new server process. A retry can't fix these errors, just as it can't fix the JSON-RPC codes that _NON_RETRYABLE_PROTOCOL_ERRORS already lists.

This affects MCPPlugin, OpenAIAgentsPlugin(mcp_servers=...) and, since #190, StrandsPlugin(mcp_clients=...), which all register these Activities. For Strands it's a change from earlier versions: their call-tool Activity returned any exception as a failed tool result (Tool execution failed: ...), as Strands' own MCPClient does, so the agent saw the error at once. Found in the review of #190.

Reproduction

One file, no Temporal server, network or API key. An in-process server whose tool declares an output schema answers with text only. The script runs the plugin's call-tool Activity with ActivityEnvironment and exits 1 when the Activity fails with an error that Temporal retries.

"""A tool result that the MCP client rejects fails the call-tool Activity with a retryable error.

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

The in-process MCP server's tool declares an output schema but answers with text
only. MCP Python SDK v2's client checks the result against the schema and raises
RuntimeError. temporalio-mcp maps only MCPError and pydantic ValidationError, so the
call-tool Activity fails with that RuntimeError, which Temporal retries: under the
default retry policy, forever. A retry cannot fix it, like the JSON-RPC errors that
temporalio-mcp already marks non-retryable. Exits 1 when the Activity fails that way.
"""

import asyncio
import sys
from typing import Any

from mcp import Client, types
from mcp.server.lowlevel import Server

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

TOOL = types.Tool(
    name="structured",
    input_schema={"type": "object"},
    output_schema={"type": "object", "properties": {"value": {"type": "string"}}},
)


async def list_tools(_ctx: Any, _params: Any) -> types.ListToolsResult:
    return types.ListToolsResult(tools=[TOOL])


async def call_tool(_ctx: Any, _params: Any) -> types.CallToolResult:
    return types.CallToolResult(content=[types.TextContent(type="text", text="no structured content")])


SERVER = Server("structured", on_list_tools=list_tools, on_call_tool=call_tool)


async def main() -> int:
    plugin = MCPPlugin({"structured": lambda: Client(SERVER)})
    activities = plugin.activities
    assert isinstance(activities, list)
    call = next(a for a in activities if a.__temporal_activity_definition.name.endswith("call-tool"))
    request = {"factory_argument": None, "meta": None, "name": "structured", "arguments": {}}
    try:
        result = await asyncio.wait_for(ActivityEnvironment().run(call, request), 20)
    except ApplicationError as err:
        print(f"ApplicationError non_retryable={err.non_retryable}: {err.message}")
        return 0 if err.non_retryable else 1
    except Exception as err:
        print(f"{type(err).__name__} (retried by Temporal): {err}")
        return 1
    print("call-tool returned:", result)
    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=$?"
RuntimeError (retried by Temporal): Tool structured has an output schema but did not return structured content
exit=1

Suggested change

In _run, raise these client-side rejections as a non-retryable ApplicationError(type="MCPProtocolError"): the output-schema checks and InputRequiredRoundsExceededError. Plugins then hand the agent a failed tool result at once, as they do for the other errors a retry can't fix. This sits next to #320, which covers factory results. The Strands README documents the current behavior (a rejected result is retried like a lost connection) and would change with the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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