Skip to content

The agent that grows with you

fix(tools): keep sent tool schemas stable across a live session

Adrian Bolinger2676f5fbc307
diff --git a/tests/tools/test_refresh_agent_mcp_tools.py b/tests/tools/test_refresh_agent_mcp_tools.py
index 96767278b0e..a443c271fb3 100644
--- a/tests/tools/test_refresh_agent_mcp_tools.py
+++ b/tests/tools/test_refresh_agent_mcp_tools.py
@@ -279,6 +279,32 @@ def test_preserve_prefix_appends_late_arrivals_at_the_tail(monkeypatch):
]
+def test_preserve_prefix_keeps_sent_tool_schemas_byte_identical(monkeypatch):
+ """A between-turns refresh must not rewrite a tool this session already sent.
+
+ The chat template prints every tool schema before the conversation. A local
+ server reuses the previous prompt only when the next one starts with those
+ exact tokens, so a new description on a tool that was already sent makes the
+ follow-up read the whole prompt again. New tools may append at the tail.
+ """
+ agent = _agent(["read_file"])
+ agent.tools[0]["function"]["description"] = "Read a file. schema v1"
+ sent = json.dumps(agent.tools)
+
+ fresh = _tool("read_file")
+ fresh["function"]["description"] = "Read a file. schema v2"
+ fresh["function"]["parameters"] = {
+ "type": "object", "properties": {"path": {"type": "string"}},
+ }
+ _serve(monkeypatch, [fresh, _tool("mcp_late")])
+ _registered(monkeypatch, ["read_file", "mcp_late"])
+
+ _mcp_agent.refresh_agent_mcp_tools(agent, preserve_prefix=True)
+
+ assert json.dumps(agent.tools[:1]) == sent
+ assert [t["function"]["name"] for t in agent.tools] == ["read_file", "mcp_late"]
+
+
def test_explicit_reload_replaces_a_same_name_schema(monkeypatch):
"""``/reload-mcp`` takes the fresh schema when the tool name stays the same.
diff --git a/tools/mcp_tool_agent.py b/tools/mcp_tool_agent.py
index 4207c0f832e..105e0bfa461 100644
--- a/tools/mcp_tool_agent.py
+++ b/tools/mcp_tool_agent.py
@@ -109,9 +109,10 @@ def refresh_agent_mcp_tools(
``preserve_prefix``: for rebuilds inside a live conversation the tool array is a cached
request prefix and any moved byte re-prefills the whole history — existing tools keep their
- slot (schemas still refresh), a still-registered tool whose ``check_fn`` merely flapped is
- carried forward (``check_fn`` gates exposure, never invocation), a deregistered tool is
- dropped, new tools append at the tail. The caller owns the prompt-cache contract.
+ slot and the schema bytes already sent (a refreshed description is rendered ahead of the
+ conversation), a still-registered tool whose ``check_fn`` merely flapped is carried forward
+ (``check_fn`` gates exposure, never invocation), a deregistered tool is dropped, new tools
+ append at the tail. The caller owns the prompt-cache contract.
With ``preserve_prefix`` off (explicit ``/reload-mcp``, and a rebuild before the
first turn) a same-name schema change is published. The caller has already
@@ -250,25 +251,20 @@ def restore_agent_tool_prefix(agent, saved) -> bool:
def _merge_preserving_prefix(current_defs: list, new_defs: list, registered_names: set) -> tuple[list, set]:
"""Fold a fresh tool snapshot into a live one without moving existing bytes. Ordered by
- ``current_defs`` (the cached request prefix): a name in both keeps its slot but takes the
- fresh schema; a name only in the live list is kept if still registered (``check_fn``
- flapped), else dropped; a name only in the fresh list is appended at the tail.
-
- The bridge tools keep their BUILT entry, not the fresh one: ``tool_search``'s description
- is derived from the session (deferred count, listing, whether ``manage_connections`` was
- present), so a late MCP server or a ``check_fn`` flap would rewrite it every turn. Search
- reads the live catalog at dispatch, so the stale count costs nothing."""
- from tools.tool_search_catalog import BRIDGE_TOOL_NAMES
+ ``current_defs`` (the cached request prefix): a name already sent keeps its slot and the
+ schema bytes already sent; a name only in the live list is kept if still registered
+ (``check_fn`` flapped), else dropped; a name only in the fresh list is appended at the tail.
+
+ A refreshed schema is not substituted in place. Tool JSON is rendered ahead of the
+ conversation, so one changed description means the next prompt no longer starts with the
+ previous one and a local prefix cache reads the whole prompt again. An explicit reload
+ (``preserve_prefix`` off) still takes the fresh schemas."""
fresh = {_def_name(entry): entry for entry in new_defs if _def_name(entry)}
merged = []
for entry in current_defs:
name = _def_name(entry)
replacement = fresh.pop(name, None)
- if name in BRIDGE_TOOL_NAMES:
- merged.append(entry)
- elif replacement is not None:
- merged.append(replacement)
- elif name and name in registered_names:
+ if replacement is not None or (name and name in registered_names):
merged.append(entry)
merged.extend(fresh.values())
return merged, {_def_name(t) for t in merged}