Backtestspydantic-airun-20260918-215936#5875

Issue #5875 Merged fix PR # Run 18 Sep 2026 · run-20260918-215936 pydantic/pydantic-ai

TemporalMCPServer.get_tools tool-definition cache breaks workflow replay determinism (TMPRL1100)

Error

The harness couldn't finish this ticket.

The test suite was unusable at the base commit.. No judgment was produced for this ticket. It counts as an error in the run total.

Solve

—

Wall-clock time in the container

Full test suite

Not run

The harness did not reach the test stage

Contamination audit

Unknown

See the audit notes for this run

Patch size

0vs 1,275 merged

Changed lines, ours vs merged

Initial Checks

  • I'm using the latest version of Pydantic AI
  • I've searched for my issue in the issue tracker before opening this issue

Description

TemporalMCPServer.get_tools consults the instance-level _cached_tool_defs cache before deciding whether to schedule the get_tools activity. With cache_tools=True on the wrapped server (the default), a warm cache returns definitions and schedules nothing.

get_tools runs in workflow code while request parameters are prepared for each model step. The Temporal docs require the TemporalAgent to be defined at the top level of the module, since it is needed both inside the workflow and to register worker activities, so the instance and its cache are shared by every workflow execution in the worker process. Whether a workflow emits a get_tools activity command therefore depends on what has already run in that process. Temporal replay requires that decision to depend only on the workflow's own history. A history recorded with a warm cache fails replay on a cold process, and a history recorded cold fails replay on a warm process:

[TMPRL1100] Nondeterminism error: Activity type of scheduled event
'agent__<name>__model_request' does not match activity type of
activity command 'agent__<name>__mcp_server__<id>__get_tools'

Observed on 1.88.0. The relevant code is unchanged on 1.107.0 and current main, in both _mcp_server.py and _mcp_toolset.py.

How we hit it. A production ECS deploy replaced a worker while a 27-minute agent workflow was in flight. The workflow had started on a long-running worker with a warm cache, so its history contained no get_tools events. The first workflow task after the restart replayed the history on the fresh process, the code emitted a get_tools command where the history had a model request scheduled, and the workflow task failed with the error above. The workflow stayed Running with the workflow task retrying, and a parent workflow sat blocked on it through a peer-call activity. Both recovered about 40 minutes later, after fresh traffic warmed the new process's cache and replay matched again. Diffing our release ruled out changes on our side; we traced the divergence to the conditional emission in _mcp_server.py. Restarts aren't the only trigger: a history recorded on a cold process (the first run after worker boot) contains a get_tools event and fails replay on the same process after the shared cache warms, for example following sticky cache eviction.

Reproduction. The script below runs against a local Temporal server, records two turns on one TemporalAgent instance (turn 1 records get_tools, turn 2 records none), then replays turn 2's history with Replayer against a freshly constructed instance, which fails with the TMPRL1100 above. Replaying against the original warm instance passes. Production hit this through model_request_stream; the script uses the non-streaming model_request to keep the example small, and the mismatch is the same either way. The default PydanticAIPlugin sandbox passes pydantic_ai through without reloading, so the shared cache survives the sandbox in real deployments; the script uses UnsandboxedWorkflowRunner to keep itself self-contained.

Impact. Anyone running MCP toolsets under Temporal with the default cache_tools=True is exposed. The bug fires on any worker restart (deploy, crash, scale-in) while a multi-step workflow is in flight, and on same-process replays of the first run after worker boot. A stuck workflow retries its workflow task indefinitely and recovers only if the process cache drifts back into the state the history was recorded under. Since the error message talks about workflow code changes and versioning, it's easy to misdiagnose this as a deployment mistake on the user's side. Setting cache_tools=False makes every turn record the activity and restores replayability; that's the workaround we deployed.

Origin and possible fix. The cache was added in #4331 to address #4317. The simplest fix we can see: always emit the get_tools activity from workflow code, and cache the tool definitions inside the activity implementation, where replay consumes recorded results and process state is safe. A per-run cache would also work if it lives in state that replay reconstructs, such as graph run state; keying the shared cache by run id doesn't help, since entries survive the replay boundary. #4331 added the same cache to the DBOS wrapper, which probably deserves a look as well.

Minimal, Reproducible Example

"""Repro: TemporalMCPServer's instance-level tool-defs cache makes workflow
histories non-replayable across worker restarts (TMPRL1100).

Connects to a Temporal server on localhost:7233, or starts a local test
server if that's unavailable. Requires: pydantic-ai, temporalio, mcp.
"""

import asyncio
import multiprocessing
import socket
import sys
import time
import uuid
from importlib.metadata import version

from pydantic_ai import Agent
from pydantic_ai.durable_exec.temporal import TemporalAgent
from pydantic_ai.mcp import MCPServerStreamableHTTP
from pydantic_ai.messages import ModelMessage, ModelResponse, TextPart
from pydantic_ai.models.function import AgentInfo, FunctionModel
from temporalio import workflow
from temporalio.client import Client
from temporalio.contrib.pydantic import pydantic_data_converter
from temporalio.worker import Replayer, UnsandboxedWorkflowRunner, Worker

GET_TOOLS_SUFFIX = "__mcp_server__dummy__get_tools"

# One TemporalAgent per worker process, shared by every workflow execution.
# Swapping the instance simulates a worker restart (fresh process, cold cache).
HOLDER: dict[str, TemporalAgent] = {}


def run_mcp_server(port: int) -> None:
    from mcp.server.fastmcp import FastMCP

    server = FastMCP("dummy", host="127.0.0.1", port=port, stateless_http=True, log_level="ERROR")

    @server.tool()
    def dummy_echo(text: str) -> str:
        """Echo the input text back."""
        return text

    server.run(transport="streamable-http")


def model_fn(messages: list[ModelMessage], info: AgentInfo) -> ModelResponse:
    return ModelResponse(parts=[TextPart("plain text, no tool calls")])


def make_temporal_agent(mcp_url: str) -> TemporalAgent:
    return TemporalAgent(
        Agent(
            FunctionModel(model_fn),
            name="repro_agent",
            instructions="You are a test agent.",
            toolsets=[MCPServerStreamableHTTP(url=mcp_url, id="dummy")],
        )
    )


@workflow.defn
class ReproWorkflow:
    @workflow.run
    async def run(self, prompt: str) -> str:
        result = await HOLDER["agent"].run(prompt)
        return result.output


def scheduled_activities(history) -> list[str]:
    return [
        ev.activity_task_scheduled_event_attributes.activity_type.name
        for ev in history.events
        if ev.HasField("activity_task_scheduled_event_attributes")
    ]


async def replay(history) -> Exception | None:
    replayer = Replayer(
        workflows=[ReproWorkflow],
        workflow_runner=UnsandboxedWorkflowRunner(),
        data_converter=pydantic_data_converter,
    )
    try:
        await replayer.replay_workflow(history)
        return None
    except Exception as e:  # noqa: BLE001
        return e


def error_text(e: Exception) -> str:
    parts = [f"{type(e).__name__}: {e}"]
    cause = e.__cause__
    while cause is not None:
        parts.append(f"  caused by {type(cause).__name__}: {cause}")
        cause = cause.__cause__
    return "\n".join(parts)


async def main() -> int:
    for pkg in ("pydantic-ai", "temporalio", "mcp"):
        print(f"{pkg}=={version(pkg)}")

    port = 18931  # fixed port for the local MCP server; change if taken
    proc = multiprocessing.Process(target=run_mcp_server, args=(port,), daemon=True)
    proc.start()
    env = None
    try:
        deadline = time.monotonic() + 15
        while True:
            try:
                socket.create_connection(("127.0.0.1", port), timeout=1).close()
                break
            except OSError:
                if time.monotonic() > deadline:
                    raise RuntimeError("MCP server did not come up")
                time.sleep(0.1)

        try:
            client = await Client.connect("localhost:7233", data_converter=pydantic_data_converter)
        except Exception:  # noqa: BLE001
            from temporalio.testing import WorkflowEnvironment

            env = await WorkflowEnvironment.start_local(data_converter=pydantic_data_converter)
            client = env.client

        mcp_url = f"http://127.0.0.1:{port}/mcp"
        task_queue = f"getools-repro-{uuid.uuid4().hex[:8]}"

        warm = make_temporal_agent(mcp_url)
        HOLDER["agent"] = warm
        histories = []
        async with warm:  # entered once at worker boot
            async with Worker(
                client,
                task_queue=task_queue,
                workflows=[ReproWorkflow],
                activities=warm.temporal_activities,
                workflow_runner=UnsandboxedWorkflowRunner(),
            ):
                for _ in range(2):
                    wf_id = f"getools-{uuid.uuid4().hex[:8]}"
                    await client.execute_workflow(
                        ReproWorkflow.run, "hello", id=wf_id, task_queue=task_queue
                    )
                    histories.append(await client.get_workflow_handle(wf_id).fetch_history())
        h1, h2 = histories
        a1, a2 = scheduled_activities(h1), scheduled_activities(h2)
        print(f"turn 1 activities: {a1}")
        print(f"turn 2 activities: {a2}")

        checks: list[tuple[str, bool]] = [
            (
                "turn 1 (cold cache) history records the get_tools activity",
                any(a.endswith(GET_TOOLS_SUFFIX) for a in a1),
            ),
            (
                "turn 2 (warm cache) history does NOT record get_tools",
                not any(a.endswith(GET_TOOLS_SUFFIX) for a in a2),
            ),
        ]

        # The bug: fresh (cold) instance replaying a warm-recorded history.
        HOLDER["agent"] = make_temporal_agent(mcp_url)
        err = await replay(h2)
        nondet = err is not None and "Nondeterminism" in error_text(err)
        checks.append(
            ("replay turn 2 vs FRESH (cold) instance fails with nondeterminism (the bug)", nondet)
        )
        if err is not None:
            print(f"\ncaptured replay error:\n{error_text(err)}\n")

        # Control: the same history replays fine on the still-warm instance.
        HOLDER["agent"] = warm
        err = await replay(h2)
        checks.append(("replay turn 2 vs original warm instance passes (control)", err is None))
        if err is not None:
            print(f"\nunexpected control replay error:\n{error_text(err)}\n")

        ok = all(passed for _, passed in checks)
        for label, passed in checks:
            print(f"[{'PASS' if passed else 'FAIL'}] {label}")
        return 0 if ok else 1
    finally:
        if env is not None:
            await env.shutdown()
        proc.terminate()
        proc.join(timeout=5)


if __name__ == "__main__":
    sys.exit(asyncio.run(main()))

Python, Pydantic AI & LLM client version

  • Python: 3.12.2
  • Pydantic AI: 1.88.0 (relevant code verified unchanged on 1.107.0 and current main)
  • LLM provider SDK: n/a (the repro uses FunctionModel)
  • temporalio: 1.26.0
  • mcp: 1.27.0

The two patches

Our patch+0 −0
No patch was produced.
Merged fix · PR #+218 −1057
pydantic_ai_slim/pydantic_ai/_agent_graph.pyindex f445057719..3cbe1db6a0 100644@@ -142,6 +142,14 @@ class GraphAgentState:     pending_messages: list[_enqueue.PendingMessage] = dataclasses.field(default_factory=list[_enqueue.PendingMessage])     """Internal: queue used by [`PendingMessageDrainCapability`][pydantic_ai.capabilities._pending_messages.PendingMessageDrainCapability]     for messages enqueued via [`enqueue`][pydantic_ai.tools.RunContext.enqueue] or [`AgentRun.enqueue`][pydantic_ai.run.AgentRun.enqueue]."""+    tool_defs_cache: dict[str, dict[str, ToolDefinition]] = dataclasses.field(+        default_factory=dict[str, dict[str, ToolDefinition]]+    )+    """Per-run cache of durable-execution toolset tool definitions, keyed by toolset `id`.++    Shared by reference into every `RunContext` this run (see `build_run_context`). Recreated per+    run and reconstructed identically on durable replay/recovery, which is what keeps the Temporal/DBOS+    MCP wrappers' `get_tools` scheduling replay-deterministic."""      def check_incomplete_tool_call(self) -> None:         """Raise `IncompleteToolCall` if the last model response was truncated mid-tool-call."""@@ -1418,6 +1426,7 @@ def build_run_context(ctx: GraphRunContext[GraphAgentState, GraphAgentDeps[DepsT         loaded_capability_ids=ctx.deps.loaded_capability_ids,         discovered_tool_names=ctx.deps.discovered_tool_names,         pending_messages=ctx.state.pending_messages,+        tool_defs_cache=ctx.state.tool_defs_cache,     )     validation_context = build_validation_context(ctx.deps.validation_context, run_context)     # Only `validation_context` may be passed to `replace`: it shallow-copies, preserving thepydantic_ai_slim/pydantic_ai/_run_context.pyindex 2889681bd7..09c05f6e98 100644@@ -112,6 +112,16 @@ class RunContext(Generic[RunContextAgentDepsT]):     Use [`enqueue`][pydantic_ai.tools.RunContext.enqueue] to add messages — don't append directly.     """ +    tool_defs_cache: dict[str, dict[str, ToolDefinition]] = field(default_factory=lambda: {}, repr=False)+    """Internal: per-run cache of tool definitions, keyed by toolset `id`.++    Read and written by durable-execution toolset wrappers (Temporal/DBOS) so a toolset's tool+    definitions are fetched at most once per run rather than before every model request. It lives+    on the run — recreated for each agent run and reconstructed identically on durable replay /+    recovery — not on the process-shared toolset instance, so whether a wrapper schedules its+    `get_tools` activity/step depends only on the run's own history and stays replay-deterministic.+    """+     tool_manager: ToolManager[RunContextAgentDepsT] | None = None     """The tool manager for the current run step. pydantic_ai_slim/pydantic_ai/durable_exec/dbos/_fastmcp_toolset.pyindex 28a5049d63..c7e54d68b4 100644@@ -26,6 +26,11 @@ class DBOSFastMCPToolset(DBOSMCPToolsetBase[AgentDepsT]):             step_config=step_config,         ) +    @property+    def _cache_tools(self) -> bool:+        # `FastMCPToolset` has no `cache_tools` setting, so tool defs are fetched every step.+        return False+     def tool_for_tool_def(self, tool_def: ToolDefinition) -> ToolsetTool[AgentDepsT]:         assert isinstance(self.wrapped, FastMCPToolset)         return self.wrapped.tool_for_tool_def(tool_def)pydantic_ai_slim/pydantic_ai/durable_exec/dbos/_mcp.pyindex 5eaaa056e9..a7e2167e21 100644@@ -81,6 +81,12 @@ class DBOSMCPToolsetBase(WrapperToolset[AgentDepsT], ABC):     def tool_for_tool_def(self, tool_def: ToolDefinition) -> ToolsetTool[AgentDepsT]:         raise NotImplementedError +    @property+    @abstractmethod+    def _cache_tools(self) -> bool:+        """Whether the wrapped MCP server/toolset has tool-definition caching enabled."""+        raise NotImplementedError+     @property     def id(self) -> str | None:         return self.wrapped.id@@ -100,7 +106,17 @@ class DBOSMCPToolsetBase(WrapperToolset[AgentDepsT], ABC):         return self      async def get_tools(self, ctx: RunContext[AgentDepsT]) -> dict[str, ToolsetTool[AgentDepsT]]:+        # The cache lives on the run (`ctx.tool_defs_cache`), recreated per run and reconstructed+        # identically on recovery, so whether the get_tools step is invoked depends only on the+        # workflow's own progress. Caching on the process-shared instance instead would make that+        # depend on what earlier runs warmed in the worker, shifting recorded step order on recovery.+        cache_key = self.id or ''+        if self._cache_tools and (cached := ctx.tool_defs_cache.get(cache_key)) is not None:+            return {name: self.tool_for_tool_def(tool_def) for name, tool_def in cached.items()}+         tool_defs = await self._dbos_wrapped_get_tools_step(ctx)+        if self._cache_tools:+            ctx.tool_defs_cache[cache_key] = tool_defs         return {name: self.tool_for_tool_def(tool_def) for name, tool_def in tool_defs.items()}      async def get_instructions(pydantic_ai_slim/pydantic_ai/durable_exec/dbos/_mcp_server.pyindex 7a36d877de..d9c2a447ce 100644@@ -2,7 +2,7 @@ from __future__ import annotations  from pydantic_ai import ToolsetTool from pydantic_ai.mcp import MCPServer-from pydantic_ai.tools import AgentDepsT, RunContext, ToolDefinition+from pydantic_ai.tools import AgentDepsT, ToolDefinition  from ._mcp import DBOSMCPToolsetBase from ._utils import StepConfig@@ -11,7 +11,7 @@ from ._utils import StepConfig class DBOSMCPServer(DBOSMCPToolsetBase[AgentDepsT]):     """A wrapper for MCPServer that integrates with DBOS, turning call_tool and get_tools into DBOS steps. -    Tool definitions are cached across steps to avoid redundant MCP server round-trips,+    Tool definitions are cached per run (on the run context) to avoid redundant MCP server round-trips,     respecting the wrapped server's `cache_tools` setting.     """ @@ -27,24 +27,15 @@ class DBOSMCPServer(DBOSMCPToolsetBase[AgentDepsT]):             step_name_prefix=step_name_prefix,             step_config=step_config,         )-        # Cached across steps to avoid redundant MCP connections per step.-        # Not invalidated by `tools/list_changed` notifications — users who need-        # dynamic tools during a workflow should set `cache_tools=False`.-        self._cached_tool_defs: dict[str, ToolDefinition] | None = None      @property     def _server(self) -> MCPServer:         assert isinstance(self.wrapped, MCPServer)         return self.wrapped +    @property+    def _cache_tools(self) -> bool:+        return self._server.cache_tools+     def tool_for_tool_def(self, tool_def: ToolDefinition) -> ToolsetTool[AgentDepsT]:         return self._server.tool_for_tool_def(tool_def)--    async def get_tools(self, ctx: RunContext[AgentDepsT]) -> dict[str, ToolsetTool[AgentDepsT]]:-        if self._server.cache_tools and self._cached_tool_defs is not None:-            return {name: self.tool_for_tool_def(td) for name, td in self._cached_tool_defs.items()}--        result = await super().get_tools(ctx)-        if self._server.cache_tools:-            self._cached_tool_defs = {name: tool.tool_def for name, tool in result.items()}-        return resultpydantic_ai_slim/pydantic_ai/durable_exec/dbos/_mcp_toolset.pyindex 886427195e..31b81985e5 100644@@ -2,7 +2,7 @@ from __future__ import annotations  from pydantic_ai import ToolsetTool from pydantic_ai.mcp import MCPToolset-from pydantic_ai.tools import AgentDepsT, RunContext, ToolDefinition+from pydantic_ai.tools import AgentDepsT, ToolDefinition  from ._mcp import DBOSMCPToolsetBase from ._utils import StepConfig@@ -11,7 +11,7 @@ from ._utils import StepConfig class DBOSMCPToolset(DBOSMCPToolsetBase[AgentDepsT]):     """A wrapper for `MCPToolset` that integrates with DBOS, turning `call_tool` and `get_tools` into DBOS steps. -    Tool definitions are cached across steps to avoid redundant MCP server round-trips,+    Tool definitions are cached per run (on the run context) to avoid redundant MCP server round-trips,     respecting the wrapped toolset's `cache_tools` setting.     """ @@ -27,24 +27,15 @@ class DBOSMCPToolset(DBOSMCPToolsetBase[AgentDepsT]):             step_name_prefix=step_name_prefix,             step_config=step_config,         )-        # Cached across steps to avoid redundant MCP connections per step.-        # Not invalidated by `tools/list_changed` notifications — users who need-        # dynamic tools during a workflow should set `cache_tools=False`.-        self._cached_tool_defs: dict[str, ToolDefinition] | None = None      @property     def _toolset(self) -> MCPToolset[AgentDepsT]:         assert isinstance(self.wrapped, MCPToolset)         return self.wrapped +    @property+    def _cache_tools(self) -> bool:+        return self._toolset.cache_tools+     def tool_for_tool_def(self, tool_def: ToolDefinition) -> ToolsetTool[AgentDepsT]:         return self._toolset.tool_for_tool_def(tool_def)--    async def get_tools(self, ctx: RunContext[AgentDepsT]) -> dict[str, ToolsetTool[AgentDepsT]]:-        if self._toolset.cache_tools and self._cached_tool_defs is not None:-            return {name: self.tool_for_tool_def(td) for name, td in self._cached_tool_defs.items()}--        result = await super().get_tools(ctx)-        if self._toolset.cache_tools:  # pragma: no branch-            self._cached_tool_defs = {name: tool.tool_def for name, tool in result.items()}-        return resultpydantic_ai_slim/pydantic_ai/durable_exec/temporal/_fastmcp_toolset.pyindex 3cf3fdaa85..3612d041e2 100644@@ -39,6 +39,11 @@ class TemporalFastMCPToolset(TemporalMCPToolsetBase[AgentDepsT]):             agent=agent,         ) +    @property+    def _cache_tools(self) -> bool:+        # `FastMCPToolset` has no `cache_tools` setting, so tool defs are fetched every step.+        return False+     def tool_for_tool_def(self, tool_def: ToolDefinition) -> ToolsetTool[AgentDepsT]:         assert isinstance(self.wrapped, FastMCPToolset)         return self.wrapped.tool_for_tool_def(tool_def)pydantic_ai_slim/pydantic_ai/durable_exec/temporal/_mcp.pyindex 978dfdd30d..8f713f18b0 100644@@ -109,6 +109,12 @@ class TemporalMCPToolsetBase(TemporalWrapperToolset[AgentDepsT], ABC):     def tool_for_tool_def(self, tool_def: ToolDefinition) -> ToolsetTool[AgentDepsT]:         raise NotImplementedError +    @property+    @abstractmethod+    def _cache_tools(self) -> bool:+        """Whether the wrapped MCP server/toolset has tool-definition caching enabled."""+        raise NotImplementedError+     @property     def temporal_activities(self) -> list[Callable[..., Any]]:         return [self.get_instructions_activity, self.get_tools_activity, self.call_tool_activity]@@ -150,6 +156,14 @@ class TemporalMCPToolsetBase(TemporalWrapperToolset[AgentDepsT], ABC):         if not workflow.in_workflow():  # pragma: no cover             return await super().get_tools(ctx) +        # The cache lives on the run (`ctx.tool_defs_cache`), recreated per run and reconstructed+        # identically on replay, so the choice to schedule the activity depends only on the workflow's+        # own history. Caching on the process-shared instance instead would make that choice depend on+        # what earlier runs warmed in the worker, breaking replay determinism (TMPRL1100).+        cache_key = self.id or ''+        if self._cache_tools and (cached := ctx.tool_defs_cache.get(cache_key)) is not None:+            return {name: self.tool_for_tool_def(tool_def) for name, tool_def in cached.items()}+         serialized_run_context = self.run_context_type.serialize_run_context(ctx)         activity_config: ActivityConfig = {'summary': f'get tools: {self.id}', **self.activity_config}         tool_defs = await workflow.execute_activity(@@ -160,6 +174,8 @@ class TemporalMCPToolsetBase(TemporalWrapperToolset[AgentDepsT], ABC):             ],             **activity_config,         )+        if self._cache_tools:+            ctx.tool_defs_cache[cache_key] = tool_defs         return {name: self.tool_for_tool_def(tool_def) for name, tool_def in tool_defs.items()}      async def call_tool(pydantic_ai_slim/pydantic_ai/durable_exec/temporal/_mcp_server.pyindex 12d9dd4c18..e96276358b 100644@@ -6,7 +6,7 @@ from temporalio.workflow import ActivityConfig  from pydantic_ai import ToolsetTool from pydantic_ai.mcp import MCPServer-from pydantic_ai.tools import AgentDepsT, RunContext, ToolDefinition+from pydantic_ai.tools import AgentDepsT, ToolDefinition  if TYPE_CHECKING:     from pydantic_ai.agent.abstract import AbstractAgent@@ -18,7 +18,7 @@ from ._run_context import TemporalRunContext class TemporalMCPServer(TemporalMCPToolsetBase[AgentDepsT]):     """A wrapper for MCPServer that integrates with Temporal, turning get_tools and call_tool into activities. -    Tool definitions are cached across activities to avoid redundant MCP server round-trips,+    Tool definitions are cached per run (on the run context) to avoid redundant MCP server round-trips,     respecting the wrapped server's `cache_tools` setting.     """ @@ -42,24 +42,15 @@ class TemporalMCPServer(TemporalMCPToolsetBase[AgentDepsT]):             run_context_type=run_context_type,             agent=agent,         )-        # Cached across activities to avoid redundant MCP connections per activity.-        # Not invalidated by `tools/list_changed` notifications — users who need-        # dynamic tools during a workflow should set `cache_tools=False`.-        self._cached_tool_defs: dict[str, ToolDefinition] | None = None      @property     def _server(self) -> MCPServer:         assert isinstance(self.wrapped, MCPServer)         return self.wrapped +    @property+    def _cache_tools(self) -> bool:+        return self._server.cache_tools+     def tool_for_tool_def(self, tool_def: ToolDefinition) -> ToolsetTool[AgentDepsT]:         return self._server.tool_for_tool_def(tool_def)--    async def get_tools(self, ctx: RunContext[AgentDepsT]) -> dict[str, ToolsetTool[AgentDepsT]]:-        if self._server.cache_tools and self._cached_tool_defs is not None:-            return {name: self.tool_for_tool_def(td) for name, td in self._cached_tool_defs.items()}--        result = await super().get_tools(ctx)-        if self._server.cache_tools:  # pragma: no branch-            self._cached_tool_defs = {name: tool.tool_def for name, tool in result.items()}-        return resultpydantic_ai_slim/pydantic_ai/durable_exec/temporal/_mcp_toolset.pyindex f7dd33425b..2ba5340a65 100644@@ -6,7 +6,7 @@ from temporalio.workflow import ActivityConfig  from pydantic_ai import ToolsetTool from pydantic_ai.mcp import MCPToolset-from pydantic_ai.tools import AgentDepsT, RunContext, ToolDefinition+from pydantic_ai.tools import AgentDepsT, ToolDefinition  if TYPE_CHECKING:     from pydantic_ai.agent.abstract import AbstractAgent@@ -18,7 +18,7 @@ from ._run_context import TemporalRunContext class TemporalMCPToolset(TemporalMCPToolsetBase[AgentDepsT]):     """A wrapper for `MCPToolset` that integrates with Temporal, turning `get_tools` and `call_tool` into activities. -    Tool definitions are cached across activities to avoid redundant MCP server round-trips,+    Tool definitions are cached per run (on the run context) to avoid redundant MCP server round-trips,     respecting the wrapped toolset's `cache_tools` setting.     """ @@ -42,24 +42,15 @@ class TemporalMCPToolset(TemporalMCPToolsetBase[AgentDepsT]):             run_context_type=run_context_type,             agent=agent,         )-        # Cached across activities to avoid redundant MCP connections per activity.-        # Not invalidated by `tools/list_changed` notifications — users who need-        # dynamic tools during a workflow should set `cache_tools=False`.-        self._cached_tool_defs: dict[str, ToolDefinition] | None = None      @property     def _toolset(self) -> MCPToolset[AgentDepsT]:         assert isinstance(self.wrapped, MCPToolset)         return self.wrapped +    @property+    def _cache_tools(self) -> bool:+        return self._toolset.cache_tools+     def tool_for_tool_def(self, tool_def: ToolDefinition) -> ToolsetTool[AgentDepsT]:         return self._toolset.tool_for_tool_def(tool_def)--    async def get_tools(self, ctx: RunContext[AgentDepsT]) -> dict[str, ToolsetTool[AgentDepsT]]:-        if self._toolset.cache_tools and self._cached_tool_defs is not None:-            return {name: self.tool_for_tool_def(td) for name, td in self._cached_tool_defs.items()}--        result = await super().get_tools(ctx)-        if self._toolset.cache_tools:  # pragma: no branch-            self._cached_tool_defs = {name: tool.tool_def for name, tool in result.items()}-        return resulttests/cassettes/test_temporal/test_mcp_tools_not_cached_when_disabled.yamldeleted file mode 100644index a567fd8632..0000000000@@ -1,955 +0,0 @@-interactions:-- request:-    headers:-      accept:-      - application/json-      accept-encoding:-      - gzip, deflate-      connection:-      - keep-alive-      content-length:-      - '4294'-      content-type:-      - application/json-      host:-      - api.openai.com-    method: POST-    parsed_body:-      messages:-      - content: 'Tell me: the capital of the country; the weather there; the product name'-        role: user-      model: gpt-4o-      stream: true-      stream_options:-        include_usage: true-      tool_choice: required-      tools:-      - function:-          description: ''-          name: get_weather-          parameters:-            additionalProperties: false-            properties:-              city:-                type: string-            required:-            - city-            type: object-          strict: true-        type: function-      - function:-          description: ''-          name: get_country-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: "Convert Celsius to Fahrenheit.\n\n    Args:\n        celsius: Temperature in Celsius\n\n    Returns:\n-            \       Temperature in Fahrenheit\n    "-          name: celsius_to_fahrenheit-          parameters:-            additionalProperties: false-            properties:-              celsius:-                type: number-            required:-            - celsius-            type: object-          strict: true-        type: function-      - function:-          description: "Get the weather forecast for a location.\n\n    Args:\n        location: The location to get the weather-            forecast for.\n\n    Returns:\n        The weather forecast for the location.\n    "-          name: get_weather_forecast-          parameters:-            additionalProperties: false-            properties:-              location:-                type: string-            required:-            - location-            type: object-          strict: true-        type: function-      - function:-          description: ''-          name: get_image_resource-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_image_resource_link-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_audio_resource-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_audio_resource_link-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_product_name-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_product_name_link-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_image-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_dict-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_error-          parameters:-            additionalProperties: false-            properties:-              value:-                default: false-                type: boolean-            type: object-        type: function-      - function:-          description: ''-          name: get_none-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_multiple_items-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: "Get the current log level.\n\n    Returns:\n        The current log level.\n    "-          name: get_log_level-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: "Echo the run context.\n\n    Args:\n        ctx: Context object containing request and session information.\n\n-            \   Returns:\n        Dictionary with an echo message and the deps.\n    "-          name: echo_deps-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: Use sampling callback.-          name: use_sampling-          parameters:-            additionalProperties: false-            properties:-              foo:-                type: string-            required:-            - foo-            type: object-          strict: true-        type: function-      - function:-          description: The final response which ends this conversation-          name: final_result-          parameters:-            $defs:-              Answer:-                additionalProperties: false-                properties:-                  answer:-                    type: string-                  label:-                    type: string-                required:-                - label-                - answer-                type: object-            additionalProperties: false-            properties:-              answers:-                items:-                  $ref: '#/$defs/Answer'-                type: array-            required:-            - answers-            type: object-          strict: true-        type: function-    uri: https://api.openai.com/v1/chat/completions-  response:-    body:-      string: |+-        data: {"id":"chatcmpl-C2QD1kGWsTW5OWiqAtOSFEAOfPfQH","object":"chat.completion.chunk","created":1754693439,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","usage":null,"choices":[{"index":0,"delta":{"role":"assistant","content":null},"logprobs":null,"finish_reason":null}],"obfuscation":"3Y6yaTIPlXV"}--        data: {"id":"chatcmpl-C2QD1kGWsTW5OWiqAtOSFEAOfPfQH","object":"chat.completion.chunk","created":1754693439,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","usage":null,"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_q2UyBRP7eXNTzAoR8lEhjc9Z","type":"function","function":{"name":"get_country","arguments":""}}]},"logprobs":null,"finish_reason":null}],"obfuscation":"g7emiFlCcG"}--        data: {"id":"chatcmpl-C2QD1kGWsTW5OWiqAtOSFEAOfPfQH","object":"chat.completion.chunk","created":1754693439,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","usage":null,"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{}"}}]},"logprobs":null,"finish_reason":null}],"obfuscation":"S8ct"}--        data: {"id":"chatcmpl-C2QD1kGWsTW5OWiqAtOSFEAOfPfQH","object":"chat.completion.chunk","created":1754693439,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","usage":null,"choices":[{"index":0,"delta":{"tool_calls":[{"index":1,"id":"call_b51ijcpFkDiTQG1bQzsrmtW5","type":"function","function":{"name":"get_product_name","arguments":""}}]},"logprobs":null,"finish_reason":null}],"obfuscation":"uomn5"}--        data: {"id":"chatcmpl-C2QD1kGWsTW5OWiqAtOSFEAOfPfQH","object":"chat.completion.chunk","created":1754693439,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","usage":null,"choices":[{"index":0,"delta":{"tool_calls":[{"index":1,"function":{"arguments":"{}"}}]},"logprobs":null,"finish_reason":null}],"obfuscation":"yrJZ"}--        data: {"id":"chatcmpl-C2QD1kGWsTW5OWiqAtOSFEAOfPfQH","object":"chat.completion.chunk","created":1754693439,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"tool_calls"}],"usage":null,"obfuscation":"onPp"}--        data: {"id":"chatcmpl-C2QD1kGWsTW5OWiqAtOSFEAOfPfQH","object":"chat.completion.chunk","created":1754693439,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[],"usage":{"prompt_tokens":364,"completion_tokens":40,"total_tokens":404,"prompt_tokens_details":{"cached_tokens":0,"audio_tokens":0},"completion_tokens_details":{"reasoning_tokens":0,"audio_tokens":0,"accepted_prediction_tokens":0,"rejected_prediction_tokens":0}},"obfuscation":"F1wZCrV0lEsbu"}--        data: [DONE]--    headers:-      access-control-expose-headers:-      - X-Request-ID-      alt-svc:-      - h3=":443"; ma=86400-      connection:-      - keep-alive-      content-type:-      - text/event-stream; charset=utf-8-      openai-organization:-      - pydantic-28gund-      openai-processing-ms:-      - '906'-      openai-project:-      - proj_dKobscVY9YJxeEaDJen54e3d-      openai-version:-      - '2020-10-01'-      strict-transport-security:-      - max-age=31536000; includeSubDomains; preload-      transfer-encoding:-      - chunked-    status:-      code: 200-      message: OK-- request:-    headers:-      accept:-      - application/json-      accept-encoding:-      - gzip, deflate-      connection:-      - keep-alive-      content-length:-      - '4720'-      content-type:-      - application/json-      cookie:-      - __cf_bm=zft6YwMMTPDlJv9nFzcyqjatDLWL51IWoXRjiBo8lxg-1754693440-1.0.1.1-FT8HyZDjEEZIx76hp4IYd2Ke6ga_YHmugNrkwXCbkPQJM7bAIax9kMz_DGNsvY5gt.sE2g60Jc0zEEp43vK95vUKIk62fCzcc3i.7eygET0;-        _cfuvid=I3WeF5lZoAzwC31zFIarQCYAjRSXcSCKQ0Z8Szv00_0-1754693440668-0.0.1.1-604800000-      host:-      - api.openai.com-    method: POST-    parsed_body:-      messages:-      - content: 'Tell me: the capital of the country; the weather there; the product name'-        role: user-      - role: assistant-        tool_calls:-        - function:-            arguments: '{}'-            name: get_country-          id: call_q2UyBRP7eXNTzAoR8lEhjc9Z-          type: function-        - function:-            arguments: '{}'-            name: get_product_name-          id: call_b51ijcpFkDiTQG1bQzsrmtW5-          type: function-      - content: Mexico-        role: tool-        tool_call_id: call_q2UyBRP7eXNTzAoR8lEhjc9Z-      - content: Pydantic AI-        role: tool-        tool_call_id: call_b51ijcpFkDiTQG1bQzsrmtW5-      model: gpt-4o-      stream: true-      stream_options:-        include_usage: true-      tool_choice: required-      tools:-      - function:-          description: ''-          name: get_weather-          parameters:-            additionalProperties: false-            properties:-              city:-                type: string-            required:-            - city-            type: object-          strict: true-        type: function-      - function:-          description: ''-          name: get_country-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: "Convert Celsius to Fahrenheit.\n\n    Args:\n        celsius: Temperature in Celsius\n\n    Returns:\n-            \       Temperature in Fahrenheit\n    "-          name: celsius_to_fahrenheit-          parameters:-            additionalProperties: false-            properties:-              celsius:-                type: number-            required:-            - celsius-            type: object-          strict: true-        type: function-      - function:-          description: "Get the weather forecast for a location.\n\n    Args:\n        location: The location to get the weather-            forecast for.\n\n    Returns:\n        The weather forecast for the location.\n    "-          name: get_weather_forecast-          parameters:-            additionalProperties: false-            properties:-              location:-                type: string-            required:-            - location-            type: object-          strict: true-        type: function-      - function:-          description: ''-          name: get_image_resource-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_image_resource_link-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_audio_resource-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_audio_resource_link-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_product_name-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_product_name_link-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_image-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_dict-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_error-          parameters:-            additionalProperties: false-            properties:-              value:-                default: false-                type: boolean-            type: object-        type: function-      - function:-          description: ''-          name: get_none-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_multiple_items-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: "Get the current log level.\n\n    Returns:\n        The current log level.\n    "-          name: get_log_level-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: "Echo the run context.\n\n    Args:\n        ctx: Context object containing request and session information.\n\n-            \   Returns:\n        Dictionary with an echo message and the deps.\n    "-          name: echo_deps-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: Use sampling callback.-          name: use_sampling-          parameters:-            additionalProperties: false-            properties:-              foo:-                type: string-            required:-            - foo-            type: object-          strict: true-        type: function-      - function:-          description: The final response which ends this conversation-          name: final_result-          parameters:-            $defs:-              Answer:-                additionalProperties: false-                properties:-                  answer:-                    type: string-                  label:-                    type: string-                required:-                - label-                - answer-                type: object-            additionalProperties: false-            properties:-              answers:-                items:-                  $ref: '#/$defs/Answer'-                type: array-            required:-            - answers-            type: object-          strict: true-        type: function-    uri: https://api.openai.com/v1/chat/completions-  response:-    body:-      string: |+-        data: {"id":"chatcmpl-C2QD2NQfRbWW5ww5we2oDjS1mgHtK","object":"chat.completion.chunk","created":1754693440,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"role":"assistant","content":null,"tool_calls":[{"index":0,"id":"call_LwxJUB9KppVyogRRLQsamRJv","type":"function","function":{"name":"get_weather","arguments":""}}],"refusal":null},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"NA5VZTdCK"}--        data: {"id":"chatcmpl-C2QD2NQfRbWW5ww5we2oDjS1mgHtK","object":"chat.completion.chunk","created":1754693440,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\""}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"MYr"}--        data: {"id":"chatcmpl-C2QD2NQfRbWW5ww5we2oDjS1mgHtK","object":"chat.completion.chunk","created":1754693440,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"city"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"N0"}--        data: {"id":"chatcmpl-C2QD2NQfRbWW5ww5we2oDjS1mgHtK","object":"chat.completion.chunk","created":1754693440,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\":\""}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"S"}--        data: {"id":"chatcmpl-C2QD2NQfRbWW5ww5we2oDjS1mgHtK","object":"chat.completion.chunk","created":1754693440,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"Mexico"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":""}--        data: {"id":"chatcmpl-C2QD2NQfRbWW5ww5we2oDjS1mgHtK","object":"chat.completion.chunk","created":1754693440,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" City"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"P"}--        data: {"id":"chatcmpl-C2QD2NQfRbWW5ww5we2oDjS1mgHtK","object":"chat.completion.chunk","created":1754693440,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\"}"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"FLs"}--        data: {"id":"chatcmpl-C2QD2NQfRbWW5ww5we2oDjS1mgHtK","object":"chat.completion.chunk","created":1754693440,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"tool_calls"}],"usage":null,"obfuscation":"d0LO"}--        data: {"id":"chatcmpl-C2QD2NQfRbWW5ww5we2oDjS1mgHtK","object":"chat.completion.chunk","created":1754693440,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[],"usage":{"prompt_tokens":423,"completion_tokens":15,"total_tokens":438,"prompt_tokens_details":{"cached_tokens":0,"audio_tokens":0},"completion_tokens_details":{"reasoning_tokens":0,"audio_tokens":0,"accepted_prediction_tokens":0,"rejected_prediction_tokens":0}},"obfuscation":"o9nP5OYjURlfY"}--        data: [DONE]--    headers:-      access-control-expose-headers:-      - X-Request-ID-      alt-svc:-      - h3=":443"; ma=86400-      connection:-      - keep-alive-      content-type:-      - text/event-stream; charset=utf-8-      openai-organization:-      - pydantic-28gund-      openai-processing-ms:-      - '379'-      openai-project:-      - proj_dKobscVY9YJxeEaDJen54e3d-      openai-version:-      - '2020-10-01'-      strict-transport-security:-      - max-age=31536000; includeSubDomains; preload-      transfer-encoding:-      - chunked-    status:-      code: 200-      message: OK-- request:-    headers:-      accept:-      - application/json-      accept-encoding:-      - gzip, deflate-      connection:-      - keep-alive-      content-length:-      - '4969'-      content-type:-      - application/json-      cookie:-      - __cf_bm=zft6YwMMTPDlJv9nFzcyqjatDLWL51IWoXRjiBo8lxg-1754693440-1.0.1.1-FT8HyZDjEEZIx76hp4IYd2Ke6ga_YHmugNrkwXCbkPQJM7bAIax9kMz_DGNsvY5gt.sE2g60Jc0zEEp43vK95vUKIk62fCzcc3i.7eygET0;-        _cfuvid=I3WeF5lZoAzwC31zFIarQCYAjRSXcSCKQ0Z8Szv00_0-1754693440668-0.0.1.1-604800000-      host:-      - api.openai.com-    method: POST-    parsed_body:-      messages:-      - content: 'Tell me: the capital of the country; the weather there; the product name'-        role: user-      - role: assistant-        tool_calls:-        - function:-            arguments: '{}'-            name: get_country-          id: call_q2UyBRP7eXNTzAoR8lEhjc9Z-          type: function-        - function:-            arguments: '{}'-            name: get_product_name-          id: call_b51ijcpFkDiTQG1bQzsrmtW5-          type: function-      - content: Mexico-        role: tool-        tool_call_id: call_q2UyBRP7eXNTzAoR8lEhjc9Z-      - content: Pydantic AI-        role: tool-        tool_call_id: call_b51ijcpFkDiTQG1bQzsrmtW5-      - role: assistant-        tool_calls:-        - function:-            arguments: '{"city":"Mexico City"}'-            name: get_weather-          id: call_LwxJUB9KppVyogRRLQsamRJv-          type: function-      - content: sunny-        role: tool-        tool_call_id: call_LwxJUB9KppVyogRRLQsamRJv-      model: gpt-4o-      stream: true-      stream_options:-        include_usage: true-      tool_choice: required-      tools:-      - function:-          description: ''-          name: get_weather-          parameters:-            additionalProperties: false-            properties:-              city:-                type: string-            required:-            - city-            type: object-          strict: true-        type: function-      - function:-          description: ''-          name: get_country-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: "Convert Celsius to Fahrenheit.\n\n    Args:\n        celsius: Temperature in Celsius\n\n    Returns:\n-            \       Temperature in Fahrenheit\n    "-          name: celsius_to_fahrenheit-          parameters:-            additionalProperties: false-            properties:-              celsius:-                type: number-            required:-            - celsius-            type: object-          strict: true-        type: function-      - function:-          description: "Get the weather forecast for a location.\n\n    Args:\n        location: The location to get the weather-            forecast for.\n\n    Returns:\n        The weather forecast for the location.\n    "-          name: get_weather_forecast-          parameters:-            additionalProperties: false-            properties:-              location:-                type: string-            required:-            - location-            type: object-          strict: true-        type: function-      - function:-          description: ''-          name: get_image_resource-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_image_resource_link-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_audio_resource-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_audio_resource_link-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_product_name-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_product_name_link-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_image-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_dict-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_error-          parameters:-            additionalProperties: false-            properties:-              value:-                default: false-                type: boolean-            type: object-        type: function-      - function:-          description: ''-          name: get_none-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: ''-          name: get_multiple_items-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: "Get the current log level.\n\n    Returns:\n        The current log level.\n    "-          name: get_log_level-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: "Echo the run context.\n\n    Args:\n        ctx: Context object containing request and session information.\n\n-            \   Returns:\n        Dictionary with an echo message and the deps.\n    "-          name: echo_deps-          parameters:-            additionalProperties: false-            properties: {}-            type: object-        type: function-      - function:-          description: Use sampling callback.-          name: use_sampling-          parameters:-            additionalProperties: false-            properties:-              foo:-                type: string-            required:-            - foo-            type: object-          strict: true-        type: function-      - function:-          description: The final response which ends this conversation-          name: final_result-          parameters:-            $defs:-              Answer:-                additionalProperties: false-                properties:-                  answer:-                    type: string-                  label:-                    type: string-                required:-                - label-                - answer-                type: object-            additionalProperties: false-            properties:-              answers:-                items:-                  $ref: '#/$defs/Answer'-                type: array-            required:-            - answers-            type: object-          strict: true-        type: function-    uri: https://api.openai.com/v1/chat/completions-  response:-    body:-      string: |+-        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"role":"assistant","content":null,"tool_calls":[{"index":0,"id":"call_CCGIWaMeYWmxOQ91orkmTvzn","type":"function","function":{"name":"final_result","arguments":""}}],"refusal":null},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"3HGTeJmv"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\""}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"5oT"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"answers"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"Bkpoehp1mPtgpuo"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\":["}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"XR"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\""}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"OrV"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"label"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"p"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\":\""}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"r"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"Capital"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"hPFa7nYTy1kqfHY"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\",\""}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"x"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"answer"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":""}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\":\""}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"1"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"The"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"Dfa"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" capital"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"uoFfXckIZzkxsw"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" of"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"4Iq"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" Mexico"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"0YTqpF2Qa9jYzl0"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" is"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"jh5"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" Mexico"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"rbLc3P4VrYs4CjM"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" City"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"5"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":".\""}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"Vu2"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"},{\""}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"i"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"label"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"2"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\":\""}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"G"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"Weather"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"G4dA7nClk85oh1y"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\",\""}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"Q"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"answer"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":""}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\":\""}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"I"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"The"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"i98"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" weather"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"vXmI31omHBLPWx"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" in"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"8Z7"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" Mexico"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"O2h4Rnr8tNEmqNy"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" City"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"8"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" is"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"dS8"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" currently"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"PaxQJe8HpcFV"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" sunny"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":""}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":".\""}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"oZX"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"},{\""}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"x"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"label"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"J"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\":\""}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"s"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"Product"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"zHxJUlHIGtbB0wY"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" Name"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"l"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\",\""}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"b"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"answer"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":""}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"\":\""}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"j"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"The"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"TlE"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" product"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"10zmpKfDgdH40s"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" name"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"t"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" is"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"YN8"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" P"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"Cy29"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"yd"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"skZD"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"antic"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"q"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":" AI"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"be9"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":".\""}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"5y4"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"}"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"IH76k"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"]}"}}]},"logprobs":null,"finish_reason":null}],"usage":null,"obfuscation":"5HnB"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"tool_calls"}],"usage":null,"obfuscation":"P7V8"}--        data: {"id":"chatcmpl-C2QD4vblfNcSDeoXmULJR4umoKNqY","object":"chat.completion.chunk","created":1754693442,"model":"gpt-4o-2024-08-06","service_tier":"default","system_fingerprint":"fp_07871e2ad8","choices":[],"usage":{"prompt_tokens":448,"completion_tokens":62,"total_tokens":510,"prompt_tokens_details":{"cached_tokens":0,"audio_tokens":0},"completion_tokens_details":{"reasoning_tokens":0,"audio_tokens":0,"accepted_prediction_tokens":0,"rejected_prediction_tokens":0}},"obfuscation":"dRC1SdJDw80tk"}--        data: [DONE]--    headers:-      access-control-expose-headers:-      - X-Request-ID-      alt-svc:-      - h3=":443"; ma=86400-      connection:-      - keep-alive-      content-type:-      - text/event-stream; charset=utf-8-      openai-organization:-      - pydantic-28gund-      openai-processing-ms:-      - '482'-      openai-project:-      - proj_dKobscVY9YJxeEaDJen54e3d-      openai-version:-      - '2020-10-01'-      strict-transport-security:-      - max-age=31536000; includeSubDomains; preload-      transfer-encoding:-      - chunked-    status:-      code: 200-      message: OK-version: 1-...tests/test_dbos.pyindex 517c0eb539..0670d2feae 100644@@ -110,15 +110,6 @@ def setup_logfire_instrumentation() -> Iterator[None]:     yield  -@pytest.fixture(autouse=True)-def _clear_mcp_tool_cache() -> None:-    """Clear cached tool defs on module-level DBOSMCPServer instances between tests."""-    for agent in (complex_dbos_agent, seq_complex_dbos_agent):-        for toolset in agent.toolsets:-            if isinstance(toolset, DBOSMCPServer):-                toolset._cached_tool_defs = None  # pyright: ignore[reportPrivateUsage]-- @contextmanager def workflow_raises(exc_type: type[Exception], exc_message: str) -> Generator[None]:     """Helper for asserting that a DBOS workflow fails with the expected error."""@@ -1628,6 +1619,10 @@ async def test_dbos_mcp_toolset_instructions_propagate(dbos: DBOS):   class _TestDBOSMCPToolset(DBOSMCPToolsetBase[int]):+    @property+    def _cache_tools(self) -> bool:+        return False+     def tool_for_tool_def(self, tool_def: ToolDefinition) -> ToolsetTool[int]:         raise AssertionError('tool_for_tool_def should not be invoked in this test')  # pragma: no cover @@ -1687,15 +1682,15 @@ def test_dbosify_mcptoolset_dispatches_to_dbosmcptoolset():   async def test_dbos_mcptoolset_returns_cached_tool_defs(dbos: DBOS):-    """When `_cached_tool_defs` is populated, `DBOSMCPToolset.get_tools` skips the step and returns from cache."""+    """When the run's tool-defs cache is populated, `DBOSMCPToolset.get_tools` returns from it without invoking the step."""     from pydantic_ai.durable_exec.dbos._mcp_toolset import DBOSMCPToolset      inner = MCPToolset('https://example.com/mcp', id='cache_return_test')     wrapper = DBOSMCPToolset(inner, step_name_prefix='cache_return_test', step_config={})-    wrapper._cached_tool_defs = {  # pyright: ignore[reportPrivateUsage]+    run_context = RunContext(deps=None, model=TestModel(), usage=RunUsage())+    run_context.tool_defs_cache['cache_return_test'] = {         'foo': ToolDefinition(name='foo', parameters_json_schema={'type': 'object'}),     }-    run_context = RunContext(deps=None, model=TestModel(), usage=RunUsage())      tools = await wrapper.get_tools(run_context)     assert list(tools.keys()) == ['foo']tests/test_temporal.pyindex 7c7b6f6566..168dd46ed2 100644@@ -81,14 +81,14 @@ try:     import temporalio.api.common.v1     from temporalio import workflow     from temporalio.activity import _Definition as ActivityDefinition  # pyright: ignore[reportPrivateUsage]-    from temporalio.client import Client, WorkflowFailureError+    from temporalio.client import Client, WorkflowFailureError, WorkflowHistory     from temporalio.common import RetryPolicy     from temporalio.contrib.opentelemetry import TracingInterceptor     from temporalio.contrib.pydantic import PydanticPayloadConverter, pydantic_data_converter     from temporalio.converter import DataConverter, DefaultPayloadConverter, PayloadCodec     from temporalio.exceptions import ApplicationError     from temporalio.testing import WorkflowEnvironment-    from temporalio.worker import Worker+    from temporalio.worker import Replayer, UnsandboxedWorkflowRunner, Worker     from temporalio.workflow import ActivityConfig      from pydantic_ai.durable_exec.temporal import (@@ -238,14 +238,6 @@ async def client_with_logfire(temporal_env: WorkflowEnvironment) -> Client:     )  -@pytest.fixture(autouse=True)-def _clear_mcp_tool_cache() -> None:-    """Clear cached tool defs on module-level TemporalMCPServer instances between tests."""-    for toolset in complex_temporal_agent.toolsets:-        if isinstance(toolset, TemporalMCPServer):-            toolset._cached_tool_defs = None  # pyright: ignore[reportPrivateUsage]-- # Can't use the `openai_api_key` fixture here because the workflow needs to be defined at the top level of the file. model = OpenAIChatModel(     'gpt-4o',@@ -838,9 +830,9 @@ async def test_mcp_tools_cached_across_activities(allow_model_requests: None, cl     """Verify that MCP tool caching reduces server round-trips across activities.      The complex agent makes 3 model requests, each preceded by a get_tools activity.-    With caching at the TemporalMCPServer wrapper level, only the first get_tools activity-    actually runs (opening an MCP connection and calling `tools/list`). Subsequent get_tools-    calls return the wrapper's cached tool definitions without scheduling an activity at all.+    With the run-scoped tool-defs cache, only the first get_tools activity actually runs+    (opening an MCP connection and calling `tools/list`). Subsequent get_tools calls return+    the run-cached tool definitions without scheduling an activity at all.     """      original_send_request = ClientSession.send_request@@ -875,29 +867,132 @@ async def test_mcp_tools_cached_across_activities(allow_model_requests: None, cl     assert methods_called.count('tools/call') == 1  -async def test_mcp_tools_not_cached_when_disabled(allow_model_requests: None):-    """Verify that wrapper-level caching is skipped when cache_tools=False.+def _call_mcp_then_finish(messages: list[ModelMessage], info: AgentInfo) -> ModelResponse:+    """Two model steps: call an MCP tool on the first request, return text on the second.++    Two model requests means `get_tools` is invoked twice on the MCP toolset within one run,+    so the run-scoped cache (and the activity it does or doesn't schedule each step) is exercised.+    """+    tool_returned = any(isinstance(part, ToolReturnPart) for message in messages for part in message.parts)+    if tool_returned:+        return ModelResponse(parts=[TextPart('done')])+    return ModelResponse(parts=[ToolCallPart('get_weather_forecast', {'location': 'Mexico City'})])+++# A holder lets the replay step swap in a freshly-constructed (cold-process) instance,+# reproducing the worker-restart scenario from #5875.+mcp_replay_holder: dict[str, TemporalAgent[None, str]] = {}+++def _make_mcp_replay_agent(cache_tools: bool = True) -> TemporalAgent[None, str]:+    with warnings.catch_warnings():+        warnings.filterwarnings('ignore', r'`MCPServerStdio` is deprecated', DeprecationWarning)+        agent = Agent(+            FunctionModel(_call_mcp_then_finish),+            name='mcp_replay_agent',+            toolsets=[+                MCPServerStdio('python', ['-m', 'tests.mcp_server'], timeout=20, id='mcp', cache_tools=cache_tools)  # pyright: ignore[reportDeprecated]+            ],+        )+    return TemporalAgent(agent, activity_config=BASE_ACTIVITY_CONFIG)+++mcp_replay_holder['agent'] = _make_mcp_replay_agent()+++@workflow.defn+class MCPReplayWorkflow:+    @workflow.run+    async def run(self, prompt: str) -> str:+        result = await mcp_replay_holder['agent'].run(prompt)+        return result.output+++def _scheduled_get_tools_count(history: WorkflowHistory) -> int:+    return sum(+        1+        for event in history.events+        if event.HasField('activity_task_scheduled_event_attributes')+        and event.activity_task_scheduled_event_attributes.activity_type.name.endswith('__get_tools')+    )+++async def test_temporal_mcp_get_tools_replay_deterministic(allow_model_requests: None, client: Client):+    """#5875 regression: `get_tools` activity scheduling must be replay-deterministic. -    Runs outside the Temporal workflow (via .override()) so coverage can track-    the TemporalMCPServer.get_tools() code path directly.+    The tool-defs cache must not let shared-process cache warmth decide whether a workflow+    emits a `get_tools` activity command — otherwise a history recorded on a warm worker fails+    replay on a cold one (and vice versa) with `TMPRL1100`. Each run must independently record+    exactly one `get_tools` activity: the #4331 within-run win (N calls collapse to one activity)+    without leaking cache state across the replay boundary.     """-    mcp_toolset = next(ts for ts in complex_temporal_agent.toolsets if isinstance(ts, TemporalMCPServer))-    server = mcp_toolset._server  # pyright: ignore[reportPrivateUsage]+    warm = _make_mcp_replay_agent()+    mcp_replay_holder['agent'] = warm -    original_cache_tools = server.cache_tools-    server.cache_tools = False+    histories: list[WorkflowHistory] = []+    # Unsandboxed so the module-level instance (and its cache) is shared across both runs,+    # exactly as a long-running worker process shares it in production — the condition under+    # which #5875 records a warm run with no `get_tools` event.+    async with Worker(+        client,+        task_queue=TASK_QUEUE,+        workflows=[MCPReplayWorkflow],+        activities=warm.temporal_activities,+        workflow_runner=UnsandboxedWorkflowRunner(),+    ):+        for i in range(2):+            wf_id = f'{MCPReplayWorkflow.__name__}_{i}'+            await client.execute_workflow(MCPReplayWorkflow.run, args=['hello'], id=wf_id, task_queue=TASK_QUEUE)+            histories.append(await client.get_workflow_handle(wf_id).fetch_history())+    h1, h2 = histories++    # Within a run, the run-scoped cache collapses the per-step `get_tools` calls to one activity...+    assert _scheduled_get_tools_count(h1) == 1+    # ...and each run records it independently — run 2 does not inherit run 1's warm process cache.+    assert _scheduled_get_tools_count(h2) == 1++    def replayer() -> Replayer:+        return Replayer(+            workflows=[MCPReplayWorkflow],+            workflow_runner=UnsandboxedWorkflowRunner(),+            data_converter=pydantic_data_converter,+        )      try:-        with complex_temporal_agent.override(deps=Deps(country='Mexico')):-            result = await complex_temporal_agent.run(-                'Tell me: the capital of the country; the weather there; the product name',-                deps=Deps(country='The Netherlands'),-            )-        assert result.output is not None-        # Wrapper-level cache should NOT be populated when cache_tools=False-        assert mcp_toolset._cached_tool_defs is None  # pyright: ignore[reportPrivateUsage]+        # Direction 1: cold-recorded history (run 1) replayed after the process cache warmed+        # (the same-process sticky-cache-eviction trigger). Holder still points at the warm instance.+        await replayer().replay_workflow(h1)++        # Direction 2: warm-recorded history (run 2) replayed on a freshly-constructed cold instance+        # (the worker-restart trigger).+        mcp_replay_holder['agent'] = _make_mcp_replay_agent()+        await replayer().replay_workflow(h2)+    finally:+        mcp_replay_holder['agent'] = warm+++async def test_temporal_mcp_get_tools_not_cached_when_disabled(allow_model_requests: None, client: Client):+    """With `cache_tools=False`, `get_tools` is scheduled for every model request (no run cache).++    The complementary case to the run-scoped cache: each of the two model requests records its own+    `get_tools` activity, so disabling the cache stays replay-deterministic by always scheduling.+    """+    agent = _make_mcp_replay_agent(cache_tools=False)+    mcp_replay_holder['agent'] = agent+    try:+        async with Worker(+            client,+            task_queue=TASK_QUEUE,+            workflows=[MCPReplayWorkflow],+            activities=agent.temporal_activities,+            workflow_runner=UnsandboxedWorkflowRunner(),+        ):+            wf_id = f'{MCPReplayWorkflow.__name__}_no_cache'+            await client.execute_workflow(MCPReplayWorkflow.run, args=['hello'], id=wf_id, task_queue=TASK_QUEUE)+            history = await client.get_workflow_handle(wf_id).fetch_history()+        assert _scheduled_get_tools_count(history) == 2     finally:-        server.cache_tools = original_cache_tools+        mcp_replay_holder['agent'] = _make_mcp_replay_agent()   async def test_complex_agent_run(allow_model_requests: None):@@ -3061,6 +3156,7 @@ def test_temporal_run_context_serialization_is_exhaustive():         'instrumentation_version',  # tracing config, not run state         'conversation_id',  # not currently exposed inside activities         'model_settings',  # not currently exposed inside activities+        'tool_defs_cache',  # run-local cache read/written in workflow code; never needed inside an activity     }     ctx = RunContext(deps=None, model=TestModel(), usage=RunUsage())     serialized = set(TemporalRunContext.serialize_run_context(ctx))