Derive the `MCP` capability's default `id` from the server URL's hostname only, and reject two servers on one host without explicit ids

Merged
DO
1mo ago
+234-55

This PR was written by Claude Code.

Without an explicit id=, the MCP capability used to derive its id from {hostname}-{last path segment} of the server URL. A path segment can carry a credential (a signed path, or a token-in-path server). That id names the capability's local MCPToolset and its durable operations (Temporal activities, DBOS steps, Prefect tasks), and it appears in telemetry. #10791 also wants to show it to the model as an instruction key.

The id is now the URL's hostname only. Two MCP capabilities for servers on one host now derive the same id, and the agent rejects them with a UserError that asks for an explicit id= on each.

[!WARNING]
Compatibility impact: an MCP capability with no explicit id= and a URL with a path, such as MCP(url='https://mcp.example.com/sse'), now has the id 'mcp.example.com' where it had 'mcp.example.com-sse'. That id is part of the names of its local toolset's durable operations: Temporal activities, DBOS steps and Prefect tasks. For example, the DBOS step <agent>__mcp_server__mcp.example.com-sse.call_tool becomes <agent>__mcp_server__mcp.example.com.call_tool. A Temporal workflow, DBOS workflow or Prefect flow started before the upgrade that is still in flight won't find its activities, steps or tasks under the new names on replay or recovery. An agent with two MCP capabilities for different servers on one host (for example https://a.com/api and https://a.com/v2/api), or a run-level MCP for a different server on the same host as an agent-level one, now raises a UserError at construction or run start. Before, they were told apart by their path, or collided only when the last segments matched.

Why this can ship in a minor release: this fixes a credential exposure. A token in a URL path flowed into ids that reach durable-execution backends, telemetry and, with #10791, the model. Explicit ids are unaffected, so the migration is one argument.

Migration: pass the old id explicitly to keep the old durable operation names, so in-flight durable workflows keep replaying, for example MCP(url='https://mcp.example.com/sse', id='mcp.example.com-sse'). The old id was {hostname}-{last path segment}, or just the hostname for a URL without a path. For two servers on one host, give each its own id=.

Collision detection. The check sits where capabilities sharing an id are already reconciled, _validate_capability_ids at Agent(...) construction and _combine_duplicate_capabilities at run setup, so it is reported the way other duplicate ids are.

  • Within one layer, two MCPs already collided, because MCP declares no class-level default id. Only the message is new: when the id was derived, it now says so and asks for id= on each.
  • Across layers, a run-level capability normally overrides its agent-level namesake. That's still right for the same server stated again (for example a run that adds headers). It's wrong for a different server on the same host, which the old path-based id kept apart. A new private hook, AbstractCapability._derived_id_source() (None by default, the URL for an MCP whose id was derived), lets the combine step reject duplicates that derived one id from different sources.
    • An MCP's source is its endpoint without userinfo or fragment: scheme, hostname, port, path (trailing / ignored) and query. A credential rotated in the userinfo still names the same server. A query change raises, because the query can select a tenant, and the message asks for an explicit id= on each: a distinct one for different servers, or the same one for one server.
    • A wrapper delegates to the capability it wraps only when it adopted that capability's id (recorded when the id is adopted). A wrapper given an id chooses it, even when it equals the wrapped capability's derived id.
    • The Agent(...) collision message sees both capabilities, so the wording doesn't depend on their order.

Telemetry masking. _telemetry_url_derived_id and the _telemetry_mcp_url_* masking exist because a URL-derived id could carry a credential. With a hostname-only id that reason is gone, and all the masking now hides is a hostname. Removing it touches about 100 lines across 12 source files, including the Temporal payload transport and realtime instrumentation, and their tests. That's a separate concern, so I've left the machinery working (it now masks hostnames, which is harmless) and updated only the tests that asserted a path-derived id. Follow-up for discussion: remove the masking in its own PR.

Unchanged:

  • A deferred MCP still needs an explicit id, because its id is listed for the model in the load_capability catalog. I updated that comment, which cited the path segment.
  • A URL with no hostname now derives no id, where it used to fall back to the whole URL.

Tests:

  • test_mcp_capabilities_on_one_host_need_explicit_ids: two derived ids on one host are rejected with the new message, a repeated explicit id keeps the general message, and distinct explicit ids work.
  • test_run_level_mcp_on_the_same_host_must_be_the_same_server: a different server on the same host at run level is rejected, and the same server overrides.
  • Existing tests that pinned path-derived ids ('mcp.example.com-api', 'a.com-api', 'mcp.example.test-SENTINEL-path…') now expect the hostname. The redaction tests now assert that the derived id never contains the path's token.

Docs: docs/capabilities/mcp.md (the default id and collisions), docs/capabilities/on-demand.md, docs/durable_execution/dbos.md and docs/durable_execution/temporal.md.

Checklist

  • 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