Resolve a `DynamicToolset` once per durable run instead of inside every durable unit

Merged
DO
3mo ago
+509-44

A DynamicToolset under durable execution was resolved, entered and torn down again inside every durable unit. A factory returning an MCPToolset — the documented per-user MCP pattern — therefore opened a fresh session per unit, and its cache_tools never survived. Three model requests and two tool calls cost five initialize/tools/list round trips instead of one.

The reported cause turned out to be a red herring. The tools/list that precedes each tools/call is the MCP SDK's own output-schema lookup (mcp/client/session.py:419-421), and a statically attached MCPToolset pays it too. Measured against an in-process server, three model requests and two tool calls:

pathinitializetools/listtools/call
plain MCPToolset, no durability112
static MCPToolset, durable332
DynamicToolset → MCPToolset, durable — before552
DynamicToolset → MCPToolset, durable — after112

Skipping the pre-call listing alone changes nothing on the wire: the SDK just does the listing instead (verified). The excess was entirely the per-unit resolve/enter/tear-down cycle.

What changed

A per_run_step=False factory is now resolved once for the run, the way a non-durable run resolves it — for_run then __aenter__, with get_tools on every step served by the toolset's own cache. Splitting that across the durable boundary:

  • Resolving performs no I/O (the factory builds the toolset object; it does not connect), so it happens in the durable container.
  • Entering does, so the first durable unit that needs the toolset enters it, and the engine retries a failed connection like any other unit failure. It is left entered for the rest of the run, and the run closes it.
  • The resolved toolset reaches the unit through the run context, so an engine that serializes it or dispatches the unit to another process — Temporal, whose workflow sandbox also forbids the I/O outright — sees none and resolves per unit exactly as before. No engine-specific code, no new engine_spec field.

Discovery still runs its own durable unit on every run step, so the recorded unit sequence is unchanged and replay stays deterministic; only the traffic inside those units drops. That is the line between this and #5884, which is about deciding whether to schedule a unit from local warmth.

A per_run_step=True factory keeps resolving per unit: its for_run_step swaps the inner toolset in place, which parallel tool-call units must not share — and re-resolution is what that flag asks for.

Tool dispatch also stops listing a toolset's tools just to find the one it already holds a recorded ToolDefinition for. AbstractToolset.get_tool_for_tool_def rebuilds it, defaulting to the listing it always did; MCPToolset answers from the definition alone, and DynamicToolset passes through to whatever its factory resolved. This is the async counterpart of the existing MCPToolset.tool_for_tool_def/FunctionToolset.tool_for_tool_def that the static durable paths already use.

Not a compatibility impact

For a per_run_step=False DynamicToolset under DBOS/Prefect, the factory now runs once per run in container code rather than once per durable unit, and sees the full RunContext rather than the reduced one a unit gets. Temporal is unaffected.

Both automated reviewers raised this as a possible compatibility impact. It isn't one: a durable factory has always had to be a deterministic build that leaves its I/O to the durable units — that is the contract for every factory that runs in container code, and the one the DBOS docs already spell out for DynamicCapability. A factory that connects, or that reads run state a unit's reduced context happens to carry, was never durable under any engine; it was simply not stated for DynamicToolset factories specifically. This PR states it, in dbos.md, prefect.md and AGENTS.md, which is the gap the reviewers actually found.

Follow-up

This leaves a statically attached MCPToolset under DBOS at 3/3 while the dynamic path reaches 1/1. That is not a regression — the static path behaves the same before and after — but the asymmetry, and the three-way disagreement between the engines about whether MCP discovery is journaled at all, are tracked in #8458.

Tests

tests/durable_exec/test_durable_operation.py gains engine-agnostic wire-count tests over an in-process MCP server: one session per run at per_run_step=False, per-unit resolution at per_run_step=True, per-unit resolution when the run context can't carry a resolved toolset, parallel tool-call units sharing one session, and the holder's enter-once/close-once lifecycle. tests/test_toolsets.py covers the get_tool_for_tool_def default and the DynamicToolset pass-through.

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
  • PR title is fit for the release changelog.

🤖 Generated with Claude Code