Derive capability-contributed toolset `id` from the capability for durable execution

Merged
DS
5mo ago
+344-14

Problem

Toolsets contributed by capabilities via get_toolset() were built with id=None:

  • Capability(tools=[...]) → FunctionToolset(tools)
  • MCP(url=...) / MCP(local=...) → local MCPToolset
  • NativeOrLocalTool(local=<callable>) → FunctionToolset([tool]) (base for WebSearch / WebFetch / ImageGeneration)

Durable-exec engines wrap leaf toolsets by id at construction time, so combining a capability with durable execution broke:

  • Temporal raised UserError at TemporalAgent(...) construction (the reported, reproduced symptom).
  • DBOS / Prefect couldn't give the leaf a stable, unique step/task name.

The error was correct but unactionable — neither Capability nor MCP exposed a way to set the contributed toolset's id.

Fix

Stamp the contributed leaf toolset's id from the capability, at construction time (per-capability):

  • Capability → FunctionToolset(tools, id=id)
  • NativeOrLocalTool (local callable) → FunctionToolset([...], id=self.id)
  • MCP → the local MCPToolset gets _derive_id(url) (explicit id → native MCPServerTool id → host+slug from the URL). This is fed to both the URL-built local toolset and the MCPToolset auto-wrapped from a non-URL local= input.

So the actionable contract is Capability(id='billing', tools=[...]) / MCP(id='docs', url='...'). User-provided toolsets (toolsets=[...], local=<MCPToolset>) keep their own ids and are never overwritten.

MCP._resolved_id was refactored to delegate to _derive_id. For the local='https://…' override (where self.url may be None) the id is derived from the concrete URL being connected to. When there's genuinely nothing to derive from — a native=<callable> factory paired with a local fallback, with no url= and no id= — it now raises an actionable UserError instead of the bare AssertionError it did before.

Design decisions for a sanity pass @DouweM

  1. Per-capability derivation vs centralized. I stamp the id at each capability rather than centralizing in CapabilityOwnedToolset. Centralizing would mutate ids on user-provided toolset instances (label/uniqueness side effects); per-capability is the safer first step. Happy to centralize if you'd prefer.
  2. id-less capability with nothing to derive from. A Capability(tools=...) / MCP(local=<client>) with no explicit id, no native tool, and no URL keeps id=None (status quo, non-breaking). The alternative is to raise a clearer error at construction telling the user to set id=. I left it as None; flagging the choice since it's the crux of "making the error actionable."
  3. MCP(local=<non-URL client>) (Path / transport / FastMCP / Client) hit the same id=None defect (reproduced: MCP(id='x', local=Path(...)) → id=None). I included it for sibling-consistency (one-line reorder + kwarg) rather than leaving a gap. Shout if you'd rather scope it out.

Notes

  • Issue #5885 (DBOS silently collided on id-less MCP leaf toolsets) was fixed by #6406, which makes DBOS reject id-less MCP leaf toolsets at construction — the same stricter contract Temporal already enforces. This PR makes that contract actionable for capability users: a capability-contributed MCP leaf now derives an id, so it passes both the Temporal and DBOS guards (covered by regression tests on each).

Tests

  • tests/test_capabilities.py — id stamping for Capability, NativeOrLocalTool, and every MCP derivation branch (URL slug, explicit id, native id, override-URL, non-URL client, nothing-to-derive → None), plus a native=<callable> with nothing to derive → actionable UserError.
  • tests/test_temporal.py — regression: TemporalAgent(Agent(capabilities=[Capability(id='billing', ...), MCP(id='docs', ...)])) no longer raises and registers activities named after the capability ids, plus a deferred-capability (defer_loading=True) variant.
  • tests/test_dbos.py — regression: a capability-contributed MCP(url=...) leaf with no explicit id derives its id from the URL and passes the DBOS id-less-MCP guard.

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.
  • PR title is fit for the release changelog.