Append changed MCP server instructions with `on_change='append'`, and give an id-less URL `MCPToolset` its hostname as `id`
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 useon_change='append', keyedtoolset:<id>. Unchanged instructions send a byte-identical prefix. Changed ones after a reconnect are appended as anInstructionDeltaPart, 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. AnMCPToolsetfor a server URL without anidnow takes the URL's hostname as itsid(-for an IPv6 literal's:s). It's a real toolsetid, so it keys instructions like any other, including a subclass's own blocks. A private_explicit_idrecords 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 uniqueid" errors still fire. - MCP Apps results (
toolset_id→serverId/serverHash) keep using only an explicitid, unchanged for id-less servers. ToolDefinition.toolset_idis only stamped with an explicitid, so tool definitions, which durable engines hash and serialize and telemetry exports, are unchanged for id-less servers. The derivedidshows in the toolset's label and repr.- Telemetry masks a derived
idlike theMCPcapability's URL-derived ids, through the same marker, kept in step with theidsetter.
- Durable execution (Temporal, DBOS, Prefect, and the legacy agents and Prefect cache keys) only accepts an explicit
- 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 oneUserWarningper key suggesting a distinctid=. - A derived
idthat matches an explicit one gives way to it, with the same warning. - Explicit duplicates still raise.
- Two toolsets that derive the same
- No provenance machinery: no per-part markers, no derived instruction keys, no
DynamicToolsetchanges, no durable re-marking.DynamicToolsetis as on main. - Temporal fix: the
get_instructionsactivity 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
MCPcapability's key; - URL keying (
no-id→testserver,id, andDynamicToolset(id=...)); - default-
idderivation (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), themcp-urlcase oftest_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. DynamicToolsetas on main:test_id_less_dynamic_toolsets_leave_their_toolsets_instructions_unkeyed.
Docs:
docs/mcp/client.md: trust, the defaultid, collisions, durable.- The DBOS, Temporal and Prefect pages: a derived
iddoesn't count. docs/agent.mdand 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