CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.
DeferredToolGroup — Progressive Tool Disclosure Primitive
Date: 2026-06-09
Branch: feat/deferred-tool-group
Issue: cubepi#166
Problem
When a cubepi agent has many tools registered (MCP servers, plugins, host-provided builtins), their
full JSON schemas are sent to the model on every turn via tools=. This creates three scaling
problems:
- Context bloat. Tool definitions are part of the cached prefix. A handful of rich servers can push the tool block into tens of thousands of tokens before the user types anything.
- Cache cost. That block is billed at cache-write rate on changes and cache-read rate every turn. More tools = a bigger fixed tax per turn.
- Worse tool selection. Published benchmarks show accuracy drops as the toolset grows; the model has to scan dozens of similar schemas to pick one.
Host applications (cubebox, standalone cubepi users) need a way to register tool groups that are hidden by default and expanded on demand, without building the entire disclosure mechanism themselves.
Goals
- Provide a
DeferredToolGroupdataclass that host apps use to declare groups of tools that start collapsed. Agent(deferred_tool_groups=[...])as the primary API — declare deferred groups alongside regular tools; Agent handles all internal wiring. No middleware assembly required from the user.DeferredToolsMiddlewareavailable as a lower-level API for advanced customization (custom catalog header, custom middleware ordering).- Minimal changes to Agent, zero changes to loop.py. Agent gains one optional parameter;
internally it creates the middleware and binds
extra_refautomatically. - Expansion-order-preserving, append-only system-prompt growth for prompt-cache stability.
- Tool-source-agnostic: cubepi knows about "groups with loaders", not about MCP or any specific tool source.
Non-goals
- No MCP-specific integration in v1 (convenience bridge from
MCPDiscoveryResult→DeferredToolGroupis a later recipe, not core API). - No semantic / embedding retrieval — the catalog is designed to be small enough for the model to read directly.
- No "code mode" (tools as a callable API the model writes code against).
- No changes to the existing MCP loader module (
cubepi/mcp/).
Prior art
- hermes-agent Tool Search (
tools/tool_search.py, 735 lines): per-tool granularity, three bridge tools (search/describe/call), BM25 retrieval, stateless catalog rebuilt every turn, context-window-percentage threshold. Key lessons: (1) core tools must never defer, (2) catalog drift silently drops tools (OpenClaw #84141), (3) transparent unwrap so hooks see real tool names. - Anthropic Tool Search Tool (GA Feb 2026): provider-side
defer_loading: true, regex/BM25 search, ~85% token reduction. Provider-specific; cubepi must work provider-agnostically. - cubebox skills system (
SkillsMiddleware+load_skill): the direct analog in cubebox. Catalog in system prompt, model calls builtin to expand, middleware injects content into prompt suffix. DeferredToolGroup generalizes this pattern for arbitrary tool sources. - cubepi TodoListMiddleware: the in-repo pattern to follow —
toolsattribute for builtin injection,extra_reffor state persistence,transform_system_promptfor prompt augmentation.
Current cubepi architecture (relevant to this design)
Agent tool lifecycle
- Construction.
Agent.__init__receivestools: list[AgentTool]andmiddleware: list. Middlewaretoolsattributes are extracted and appended toagent.state.tools(agent.py:191-195). - Context snapshot.
_create_context_snapshot()(agent.py:713-719) buildsAgentContextwithtools=list(self._state._tools). This is the snapshot the loop runs against. - Loop reads.
run_agent_loopcreatescurrent_contextwithtools=context.tools(loop.py:61) — a reference, not a deep copy. Each iteration calls_stream_assistant_response(context=current_context)which re-readscontext.toolsat loop.py:705-706 to buildtools_defs.
Key insight: because current_context.tools is a reference to the same list object, appending
to it from an after_tool_call hook makes new tools visible to the model on the next iteration
of the same run. No changes to loop.py are needed for mid-run tool injection.
Middleware hooks (used by this feature)
tools: list[AgentTool]— class attribute; extracted at Agent construction and merged intoagent.state.tools. Used to inject theexpand_toolsbuiltin.transform_system_prompt(system_prompt, *, ctx, signal) → str— chained sequentially across middlewares. Used to append catalog text and expanded schema text.after_tool_call(ctx, *, signal) → AfterToolCallResult | None— fires after every tool execution. Used to detectexpand_toolscalls and inject the expanded group's tools.
State persistence
Agent._extra is a JsonObject dict shared with AgentContext.extra. Middlewares write state
into it (e.g. TodoListMiddleware writes todos). Host applications persist it via checkpointers.
DeferredToolGroup uses this for expansion state (extra["expanded_groups"]).
Agent-level deferred_tool_groups parameter
Agent.__init__ gains an optional deferred_tool_groups: list[DeferredToolGroup] | None
parameter. When provided, Agent automatically creates a DeferredToolsMiddleware and appends it
to the middleware chain — no manual middleware assembly from the caller.
class Agent:
def __init__(
self,
*,
model: BoundModel,
tools: list[AgentTool] | None = None,
middleware: list[Middleware] | None = None,
deferred_tool_groups: list[DeferredToolGroup] | None = None, # NEW
...
) -> None:
...
if deferred_tool_groups:
from cubepi.deferred import DeferredToolsMiddleware
deferred_mw = DeferredToolsMiddleware(
groups=deferred_tool_groups,
extra_ref=lambda: self._extra,
)
middleware = [*(middleware or []), deferred_mw]
...
This follows the same principle as other Agent conveniences: the user declares intent
(deferred_tool_groups=[...]) and Agent handles the wiring. The extra_ref binding to
self._extra is automatic — the caller never needs to think about it.
Users who need custom catalog_header or non-default middleware ordering can still construct
DeferredToolsMiddleware directly and pass it via middleware=[...].
Design
Data types
@dataclass
class DeferredToolGroup:
group_id: str # "mcp:github", "plugin:kanban"
display_name: str # "GitHub"
description: str # "Code hosting: issues, PRs"
tool_names: list[str] # for catalog display only
loader: Callable[[], Awaitable[list[AgentTool]]] # invoked on expand
loader is an async callback that returns callable AgentTools. It is invoked exactly once per
expansion (not per turn). The host application owns what happens inside: cubebox calls
load_workspace_mcp_tools_for_cubepi filtered to one server; a standalone user might call
load_mcp_tools_http directly.
tool_names is for catalog display only — these names appear in the system prompt so the model
can judge which group to expand. They are never used to construct tools or validate schemas.
DeferredToolsMiddleware
A single middleware that encapsulates the entire feature. Follows TodoListMiddleware patterns.
class DeferredToolsMiddleware(Middleware):
def __init__(
self,
*,
groups: list[DeferredToolGroup],
extra_ref: Callable[[], dict[str, Any]],
catalog_header: str = DEFAULT_CATALOG_HEADER,
) -> None: ...
Construction-time:
- Stores the deferred groups indexed by
group_id. - Creates the
expand_toolsbuiltin asself.tools = [_make_expand_tools(...)]. extra_refworks exactly like TodoListMiddleware: a closure returningagent._extraso the tool'sexecutecan write expansion state into the persisted dict.
tools attribute — exposes [expand_tools_agent_tool]. Agent construction merges this into
agent.state.tools automatically (agent.py:191-195).
transform_system_prompt:
- Append catalog section — sorted by
group_idfor byte-stability. For each group, shows only the not-yet-expanded tool names. A fully expanded group is omitted entirely; a partially expanded group shows only remaining tools with an updated count. - Append expanded schema section — for each expanded group, in expansion order (not sorted), append the expanded tools' full definitions (name + description + parameters JSON). When a group is partially expanded, only the selected tools' schemas appear. Expansion order is append-only: a newly expanded group (or newly expanded tools within a group) always lands after every already-rendered block, preserving earlier cache segments byte-identical.
Why expansion order, not sorted? Groups expand incrementally mid-conversation. Sorting could insert a later expansion before an already-cached block and invalidate the prompt-cache prefix.
after_tool_call:
- Fires on every tool call. Checks: is the tool name
expand_tools? Did it succeed? - If yes: parse the result to get the
group_idand optionaltool_names. Look up the group. On first access to a group, callloader()and cache the full result (the loader is invoked exactly once per group per run regardless of how many selective expansions follow). Filter the cached tools bytool_names(or take all iftool_namesis None). Append the filteredAgentTools toctx.context.tools(the live list reference — next iteration sees them). Record the expansion inextra["expanded_groups"]. - Store the loaded tools' definitions for
transform_system_promptto render in the expanded schema section.
expand_tools builtin
class ExpandToolsInput(BaseModel):
group_id: str = Field(
description="The group_id from your 'Deferred tool groups' catalog."
)
tool_names: list[str] | None = Field(
default=None,
description="Specific tools to expand. Omit to expand all tools in the group.",
)
class ExpandToolsOutput(BaseModel):
group_id: str
expanded: bool
tool_names: list[str] # the tools that were actually expanded this call
remaining: int # tools still deferred in this group
error: str | None = None
Behavior:
- Valid group_id, no tool_names → expand all tools, return full list.
- Valid group_id + tool_names → expand only those tools, return them. Unknown names within the list are silently ignored (the rest still expand).
- Unknown group_id → return
is_error=True+ error message. - Already-expanded tools → idempotent (no re-inject, no duplicate in context.tools). The response still lists them as expanded.
Incremental expansion. The model can call expand_tools multiple times for the same group with
different tool_names. Each call adds only the newly requested tools. Example flow:
expand_tools("mcp:github", tool_names=["create_issue"]) → 1 tool loaded, 11 remaining
expand_tools("mcp:github", tool_names=["search_repos"]) → 1 more loaded, 10 remaining
expand_tools("mcp:github") → remaining 10 loaded, 0 remaining
The tool result contains tool names + descriptions only, not full schemas. The middleware injects schema text into the system-prompt suffix (matching the skills pattern where content goes into the prompt, not the tool result).
Catalog rendering
Example output appended to system prompt:
# Deferred tool groups
These tool groups are available but not yet loaded. Call `expand_tools(group_id)`
to load a group's tools for the rest of this conversation.
- `mcp:linear` — Issue tracking (8 tools)
create_issue, update_issue, search_issues, get_issue, create_project,
list_projects, create_cycle, list_cycles
- `mcp:gdrive` — Google Drive (5 tools)
search_files, read_file, list_folders, get_file_metadata, create_file
Properties:
- Sorted by
group_id→ byte-identical every turn (for the non-expanded portion). - Fully expanded groups are omitted. Partially expanded groups show only the remaining (not-yet-expanded) tool names with an updated count.
- Tool names listed without descriptions or schemas — tool names in MCP/plugin conventions
(
verb_noun) are self-descriptive. ~40 tokens per group of 12 tools. catalog_headeris customizable via constructor for host apps with different wording needs.
Example after partial expansion of mcp:github (create_issue already expanded):
- `mcp:github` — Code hosting (11 remaining tools)
search_repos, create_pr, list_pull_requests, merge_pull_request,
search_code, list_commits, create_comment, ...
Expanded schema rendering
Appended after the catalog, in expansion order:
# Expanded tool groups
## mcp:linear
- **create_issue**: Open a new issue
Parameters: {"type": "object", "properties": {"title": {"type": "string"}, ...}}
- **update_issue**: Update an existing issue
Parameters: {...}
...
## mcp:gdrive
...
Properties:
- Expansion order preserved (append-only, never re-sorted).
- Schema text derived from the loaded
AgentTool.to_definition()at expansion time. - Stored on the middleware instance (not re-computed from tools list) so it's stable across turns.
Mid-run tool injection
When after_tool_call fires for a successful expand_tools:
# First access to this group: call loader, cache the full result
if group_id not in self._loader_cache:
self._loader_cache[group_id] = await group.loader()
all_tools = self._loader_cache[group_id]
# Filter by requested tool_names (or take all)
selected = [t for t in all_tools if t.name in requested_names] if requested_names else all_tools
# Only inject tools not already in context (idempotent)
existing_names = {t.name for t in ctx.context.tools}
new_tools = [t for t in selected if t.name not in existing_names]
ctx.context.tools.extend(new_tools) # visible next iteration
This works because current_context.tools in _run_loop is a reference to the same list — the
loop re-reads context.tools at each iteration (loop.py:705). No Agent or loop changes needed.
The expanded tools are real AgentTools in tools= — the model can call them directly. The
expanded schema section in the system prompt is supplementary context (descriptions, parameter
docs), not the mechanism that makes tools callable.
Expansion state persistence
extra["expanded_groups"]
# Ordered dict: group_id → list[str] | None
# None = all tools expanded; list = specific tool names
# Ordering = expansion order (first group expanded first)
#
# Example:
# {
# "mcp:github": ["create_issue", "search_repos"], # partial
# "mcp:linear": null, # all expanded
# }
- Written by
after_tool_callon each new expansion (append new group, or extend existing group's tool list). - Read by
transform_system_promptto decide what to render in catalog vs expanded sections. - Persisted by the host application's checkpointer (same mechanism as TodoListMiddleware's todos).
- Must be serialized as an ordered dict. If a checkpointer deserializes it as an unordered map,
the expansion-order invariant breaks and the prompt-cache prefix becomes unstable across turns.
Python
dictpreserves insertion order since 3.7, so standard JSON round-tripping is safe.
Replay on subsequent runs
When the host application creates a new run (next user turn), it should:
- Read persisted
extra["expanded_groups"]from the checkpointer (an ordered dict ofgroup_id → list[str] | None). - Pre-load the expanded tools: for each group with expanded tools, call loader and filter to the
expanded tool names (or take all if
None). - Pass the pre-loaded tools as regular
toolstoAgent(tools=[...builtins, ...pre_loaded], deferred_tool_groups=groups). - Agent creates the middleware internally; the middleware reads
extra["expanded_groups"]to know which tools are already expanded and adjusts the catalog accordingly.
This is the host application's responsibility, not the middleware's — the middleware only handles within-a-single-run expansion. cubepi provides a helper for step 2:
@dataclass
class ResumedState:
pre_loaded_tools: list[AgentTool] # merge into Agent(tools=...)
remaining_groups: list[DeferredToolGroup] # groups still fully/partially deferred
@staticmethod
async def prepare_resumed_state(
groups: list[DeferredToolGroup],
expanded: dict[str, list[str] | None],
) -> ResumedState:
"""Call loaders for expanded groups, filter to expanded tools, return split."""
...
Usage with Agent-level API:
resumed = await DeferredToolsMiddleware.prepare_resumed_state(groups, saved_extra["expanded_groups"])
agent = Agent(
model=model,
tools=[*builtins, *resumed.pre_loaded_tools],
deferred_tool_groups=resumed.remaining_groups,
extra=saved_extra,
)
File layout
cubepi/
agent/
agent.py # +deferred_tool_groups parameter, auto-creates middleware (~10 lines)
deferred/
__init__.py # public exports: DeferredToolGroup, DeferredToolsMiddleware
types.py # DeferredToolGroup dataclass
middleware.py # DeferredToolsMiddleware
_catalog.py # catalog + expanded schema rendering (pure functions)
_expand_tool.py # expand_tools builtin (AgentTool factory)
tests/
test_deferred/
test_catalog.py # catalog rendering determinism
test_middleware.py # middleware integration (after_tool_call, transform_system_prompt)
test_expand_tool.py # expand_tools builtin (valid/invalid/idempotent)
test_injection.py # mid-run tool injection via context.tools mutation
New cubepi/deferred/ package — does not import from cubepi/mcp/ (tool-source-agnostic).
agent.py change is small: accept the parameter, lazy-import DeferredToolsMiddleware, create
and append to middleware list.
Public API surface
Primary API — Agent-level parameter:
from cubepi.deferred import DeferredToolGroup
agent = Agent(
model=model,
system_prompt="...",
tools=[t1, t2], # always-visible tools
deferred_tool_groups=[ # groups that start collapsed
DeferredToolGroup(
group_id="mcp:github",
display_name="GitHub",
description="Code hosting: issues, PRs, repos",
tool_names=["create_issue", "search_repos", "create_pr", ...],
loader=lambda: load_my_github_tools(),
),
],
)
Agent creates the middleware internally, binds extra_ref to self._extra, and appends
expand_tools to the tool set. The caller never touches DeferredToolsMiddleware.
Advanced API — direct middleware construction:
from cubepi.deferred import DeferredToolGroup, DeferredToolsMiddleware
middleware = DeferredToolsMiddleware(
groups=groups,
extra_ref=lambda: agent._extra,
catalog_header="Custom header...", # override default catalog text
)
agent = Agent(
model=model,
tools=[...builtins],
middleware=[middleware, ...other_middleware], # explicit ordering control
)
Use when you need custom catalog_header text, specific middleware ordering, or other
customization that the Agent-level shorthand does not expose.
Testing strategy
Unit tests (no real LLM, no MCP server):
- Catalog rendering — sorted by group_id, byte-stable across input orderings, no schemas, fully expanded groups omitted, partially expanded groups show only remaining tools.
- Expanded schema rendering — expansion order preserved, append-only (second expansion's text starts with first expansion's text), JSON keys sorted for byte stability. Partial expansion renders only the selected tools' schemas.
- expand_tools builtin — valid group → expanded=True + tool names; unknown group → error; already-expanded → idempotent; with tool_names → selective expansion.
- Incremental expansion — expand one tool, then another from same group; loader called only once; both tools in context; catalog shows remaining.
- Mid-run injection — after expand_tools fires,
ctx.context.toolscontains the new tools; simulate a second_stream_assistant_responsecall and verifytools_defsincludes them. - Expansion state — after_tool_call writes to extra["expanded_groups"] correctly; duplicate tool names not added twice; transform_system_prompt uses the stored order.
- Loader failure — if loader raises, expand_tools returns is_error=True, no tools injected, no state change.
- Loader caching — two selective expands on the same group call loader exactly once.
Integration test (with Agent, mock provider):
- Full round-trip — Agent with
deferred_tool_groups=[...], mock provider that calls expand_tools then uses an expanded tool. Assert: first model call sees expand_tools but not deferred tools; after expansion, next iteration sees deferred tools in tools_defs. - Agent-level wiring —
Agent(deferred_tool_groups=[...])automatically creates the middleware, binds extra_ref, and merges expand_tools into the tool set.
Open questions
- Catalog in system prompt vs. in tool description. Current design puts the catalog in the
system prompt (via
transform_system_prompt). Alternative: put it in theexpand_toolstool's description field. Pro: shorter system prompt. Con: tool descriptions are less visible to models than system prompt text, and the catalog changes as groups expand (dynamic tool descriptions are unusual). Recommend: system prompt (current design). - Loader caching. Should the middleware cache loader results so a re-expand (or replay) doesn't re-invoke the loader? v1: no — the host application manages caching if needed. The middleware calls loader exactly once per group per run.
- Max groups / token budget. Should the middleware have a hard limit on how many groups can be
registered, or a token-budget gate like hermes-agent's threshold_pct? v1: no — the host
application decides what to defer. A future version could add an
automode that estimates token cost and only defers when it's worth the indirection overhead.
v1 scope vs later
v1:
DeferredToolGroupdataclass.Agent(deferred_tool_groups=[...])parameter (primary API).DeferredToolsMiddlewarewith catalog rendering,expand_toolsbuiltin (with selectivetool_names), mid-run injection, expansion-order tracking, expanded schema rendering.prepare_resumed_statehelper for cross-run replay.- Unit + integration tests.
Later:
- MCP convenience bridge (
deferred_group_from_mcp_server). - Auto-mode threshold (context-window-percentage gate).
- Unify with skills disclosure under a shared primitive.
- Semantic / embedding retrieval for very large catalogs.