Append changed MCP server instructions with `on_change='append'`, and give an id-less URL `MCPToolset` its hostname as `id`

Merged
DO
1mo ago
+974-120

This PR was written by Claude Code.

Follow-up to #8188, part of the audit in #10756, building on #10807 (merged). There's no separate issue.

MCPToolset.get_instructions returned the server's instructions as a plain InstructionPart(dynamic=False). The toolset reads them each time it connects, once per agent run by default, so a server that sent different instructions between turns of a conversation (for example after a redeploy) rewrote the cached instruction prefix.

What changes:

  • Appending. On a toolset with an id, server instructions use on_change='append', keyed toolset:<id>. Unchanged instructions send a byte-identical prefix. Changed ones after a reconnect are appended as an InstructionDeltaPart, and a keyed server with no instructions still contributes an empty block, so instructions it adds or drops later append too. The model sees them in <context id="toolset:<id>">.
  • A default id. An MCPToolset for a server URL without an id now takes the URL's hostname as its id (- for an IPv6 literal's :s). It's a real toolset id, so it keys instructions like any other, including a subclass's own blocks. A private _explicit_id records whether it was chosen:
    • Durable execution (Temporal, DBOS, Prefect, and the legacy agents and Prefect cache keys) only accepts an explicit id. A derived one never names an activity, step or task, and the existing "needs a unique id" errors still fire.
    • MCP Apps results (toolset_id → serverId/serverHash) keep using only an explicit id, unchanged for id-less servers.
    • ToolDefinition.toolset_id is only stamped with an explicit id, so tool definitions, which durable engines hash and serialize and telemetry exports, are unchanged for id-less servers. The derived id shows in the toolset's label and repr.
    • Telemetry masks a derived id like the MCP capability's URL-derived ids, through the same marker, kept in step with the id setter.
  • Collisions are resolved at instruction collection:
    • Two toolsets that derive the same id (two servers on one host) leave their instructions unkeyed and rewriting, as on main, with one UserWarning per key suggesting a distinct id=.
    • A derived id that matches an explicit one gives way to it, with the same warning.
    • Explicit duplicates still raise.
  • No provenance machinery: no per-part markers, no derived instruction keys, no DynamicToolset changes, no durable re-marking. DynamicToolset is as on main.
  • Temporal fix: the get_instructions activity result was decoded as tool definitions; it now has its own transport (_GetInstructionsTransport). It was only exposed once server instructions became non-None.

[!WARNING]
Compatibility impact: an MCPToolset built from a server URL without id= now has an id, its hostname, where it had None. That shows in its label and repr and the key of its server instructions (masked in telemetry). Tool definitions' toolset_id, durable execution, MCP Apps payloads and Prefect cache keys are unchanged, because they only use an explicit id. With include_instructions=True, the model now gets server instructions in a <context id="toolset:<id>"> element (one cache miss per conversation on upgrade).

Why this can ship in a minor release: no public API is removed, and code that set id= behaves as before. None → hostname is an additive default.

Migration: to keep toolset_id and instruction keys as before, pass id= explicitly. Durable users already must.

Separately, an id-less DynamicToolset that builds a keyed MCPToolset with include_instructions=True speaks for it without a key, so the server's block is downgraded with the "has no id" warning on every request, where main sent it silently as rewriting text. Give the DynamicToolset an id to append it.

Where appending isn't supported: realtime sessions and deferred capabilities warn when an author's on_change='append' can't be honoured. MCPToolset declares, through a private toolset-level _appends_instructions_by_default, that its server instructions' append is the framework's choice, so blocks keyed under that toolset's own id rewrite there without a warning. An author's own append block keeps the warning (test_server_instructions_in_a_deferred_capability_only_warn_for_an_authors_own_opt_in). Known edges, noted in a code comment: a subclass's own append blocks under the same id are silent too, and a DynamicToolset with an id that re-keys the server's block isn't recognised, so that block warns.

Tests:

  • tests/test_mcp.py:
    • prefix stability and the reconnect delta;
    • None → text → None;
    • forged framing tags;
    • stdio/in-process rewriting;
    • the MCP capability's key;
    • URL keying (no-id → testserver, id, and DynamicToolset(id=...));
    • default-id derivation (case, port, query, IPv6, setter);
    • the three collision cases, with warning dedupe;
    • an id-less URL subclass whose own blocks are keyed by the hostname too.
  • Durable rejection of a derived-only id: test_durability_rejects_a_url_mcp_toolset_with_only_a_derived_id (Temporal), the mcp-url case of test_prefect_durability_rejects_idless_toolsets, and the existing DBOS URL test.
  • MCP Apps: test_a_hostname_derived_id_doesnt_identify_the_server_to_an_app.
  • DynamicToolset as on main: test_id_less_dynamic_toolsets_leave_their_toolsets_instructions_unkeyed.

Docs:

  • docs/mcp/client.md: trust, the default id, collisions, durable.
  • The DBOS, Temporal and Prefect pages: a derived id doesn't count.
  • docs/agent.md and the agent skill.

Interaction with #8770: no common files. The baseline reaches the prepared instruction parts and the deltas travel as InstructionDeltaParts, which _mcp.py already maps.

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