Skip to content

The agent that grows with you

fix(tools): a deactivated bridge tool keeps its slot on a prefix-preserving refresh

teknium1f16a431b5aad
diff --git a/tests/tools/test_refresh_agent_mcp_tools.py b/tests/tools/test_refresh_agent_mcp_tools.py
index a443c271fb3..b2101d2f351 100644
--- a/tests/tools/test_refresh_agent_mcp_tools.py
+++ b/tests/tools/test_refresh_agent_mcp_tools.py
@@ -354,6 +354,27 @@ def test_preserve_prefix_keeps_the_bridge_tools_byte_identical(monkeypatch):
assert [t["function"]["name"] for t in agent.tools][-1] == "mcp_late_tool"
+def test_preserve_prefix_keeps_a_deactivated_bridge_tool_in_place(monkeypatch):
+ """The bridge tools are synthesized by ``assemble_tool_defs``, never registered. When the
+ deferred set shrinks under the activation threshold (an MCP server drops), the fresh
+ snapshot carries no ``tool_search`` at all; the sent one must still hold its slot, or
+ every tool after it moves and the prefix re-prefills. Search reads the live catalog."""
+ built = _tool("tool_search")
+ built["function"]["description"] = "Search 21 additional tools."
+ agent = _agent(["read_file"])
+ agent.tools.append(built)
+ agent.tools.append(_tool("terminal"))
+ agent.valid_tool_names.update({"tool_search", "terminal"})
+ before = json.dumps(agent.tools, sort_keys=True)
+
+ _serve(monkeypatch, [_tool("read_file"), _tool("terminal")])
+ _registered(monkeypatch, ["read_file", "terminal"])
+
+ _mcp_agent.refresh_agent_mcp_tools(agent, preserve_prefix=True)
+
+ assert json.dumps(agent.tools, sort_keys=True) == before
+
+
# ---------------------------------------------------------------------------
# tools[] freeze: eviction rebuild + the /reload-mcp re-probe hatch
# ---------------------------------------------------------------------------
diff --git a/tools/mcp_tool_agent.py b/tools/mcp_tool_agent.py
index 105e0bfa461..b8a271d6559 100644
--- a/tools/mcp_tool_agent.py
+++ b/tools/mcp_tool_agent.py
@@ -258,13 +258,18 @@ def _merge_preserving_prefix(current_defs: list, new_defs: list, registered_name
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."""
+ (``preserve_prefix`` off) still takes the fresh schemas.
+
+ The bridge tools are synthesized by ``assemble_tool_defs``, never registered, and a fresh
+ snapshot omits them when the deferred set shrinks under the activation threshold: they
+ keep their slot regardless (search reads the live catalog at dispatch)."""
+ from tools.tool_search_catalog import BRIDGE_TOOL_NAMES
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 replacement is not None or (name and name in registered_names):
+ if replacement is not None or name in BRIDGE_TOOL_NAMES or (name and name in registered_names):
merged.append(entry)
merged.extend(fresh.values())
return merged, {_def_name(t) for t in merged}