CodeWhale icon indicating copy to clipboard operation
CodeWhale copied to clipboard

feat(MCP): add `always_load` server field to skip defer_loading for high-frequency MCP tools

Open SparkofSpike opened this issue 5 months ago • 3 comments

Problem

CodeWhale defers MCP tools by default. They are not loaded into the model context until the model explicitly requests them via tool_search. This is a sensible default for rarely-used tools, but it introduces a mandatory retry round-trip for every first invocation of a tool, even when the tool is used many times per session.

Any MCP tool that gets called frequently within a session suffers from this overhead:

  1. Every first invocation requires two model turns: one to request the tool (which gets deferred), and one to actually use it.
  2. The accumulated latency is significant for hot-path tools.
  3. The model cannot batch multiple high-frequency MCP tools into a single turn because each deferred tool must be resolved individually.

Concrete example: The neural-memory MCP server provides persistent agent memory tools (nmem_recall, nmem_remember, nmem_consolidate). In a long agent session these fire many times — each hitting the defer round-trip. The same pattern applies to any frequently-used MCP server: search, database query, code analysis, etc.

Evidence: infrastructure already exists

CodeWhale's tool_catalog.rs already has the concept of an always-loaded tool set, exposed as an always_load: &HashSet<String> parameter.

// crates/tui/src/core/engine/tool_catalog.rs:101
pub(super) fn should_default_defer_tool(name: &str, always_load: &HashSet<String>) -> bool {
    if always_load.contains(name) {
        return false;                // ← skip deferral
    }
    if is_tool_search_tool(name) {
        return false;
    }
    !DEFAULT_ACTIVE_NATIVE_TOOLS
        .iter()
        .any(|core_tool| core_tool == &name)
}

The function is wired into apply_native_tool_deferral() and build_model_tool_catalog(). The always_load set is checked but never populated from user configuration — it is always empty in practice.

Similarly, apply_mcp_tool_deferral() at line 129 unconditionally defers all MCP tools (outside YOLO mode):

pub(super) fn apply_mcp_tool_deferral(catalog: &mut [Tool], mode: AppMode) {
    for tool in catalog {
        tool.defer_loading = Some(mode != AppMode::Yolo
            && !should_keep_mcp_tool_loaded(&tool.name));
    }
}

Proposed solution

Add an always_load field to the MCP server configuration in ~/.codewhale/mcp.json.

Schema (recommended: array of tool names)

{
  "mcpServers": {
    "neural-memory": {
      "always_load": ["nmem_recall", "nmem_remember"],
      "command": "python",
      "args": ["-m", "neural_memory.mcp"]
    }
  }
}

Takes an array of tool name strings. Short names resolve against the server's known tool list.

Alternative: server-level boolean

{
  "mcpServers": {
    "neural-memory": {
      "always_load": true,
      "command": "python",
      "args": ["-m", "neural_memory.mcp"]
    }
  }
}

The array form is recommended — not all tools from a server are necessarily hot-path.

Implementation sketch

Small change — approximately three locations:

  1. Config parser (likely crates/tui/src/mcp.rs or crates/tui/src/config/models.rs): parse always_load from each server entry.
  2. Catalog builder: thread the accumulated always_load: HashSet<String> into build_model_tool_catalog().
  3. Tool name resolution: decide whether names use full mcp_<server>_<tool> form or short form resolved against the server's tool list.

Alternative: tool-level _meta approach (complementary)

CodeWhale could also respect _meta.anthropic/alwaysLoad in MCP tool definitions from tools/list — matching Claude Code's approach. This lets MCP server authors declare hot-path tools without user config.

Both could coexist: _meta for server-declared hints, mcp.json always_load for user overrides.

Related

Config surface for reference

Current ~/.codewhale/mcp.json Server Fields:

command, args, env,
connect_timeout, execute_timeout, read_timeout,
disabled, enabled, required,
enabled_tools, disabled_tools,
url, transport, headers, env_headers, bearer_token_env_var,
scopes, oauth.client_id, oauth_resource

always_load fits naturally alongside enabled_tools and disabled_tools.

SparkofSpike avatar Jul 04 '26 03:07 SparkofSpike

Thanks @SparkofSpike for the report.

This issue is staying open for maintainer triage. CodeWhale gets better because people bring us real edge cases from real machines, providers, regions, and workflows.

If you can add a reproduction, logs, version output, screenshots, or the provider/model involved, that makes it much easier for us to verify and harvest the fix. Maintainers may comment /lgtmi to mark recurring issue reporters as approved so this intake note is skipped next time.

github-actions[bot] avatar Jul 04 '26 03:07 github-actions[bot]

Partial fix landed in 892f9d8dfb6b99a2199003157ca9348ad83cd5b5: apply_mcp_tool_deferral now respects [tools].always_load. Regression test tools_always_load_overrides_mcp_deferral in engine/tests.rs.

Hmbown avatar Jul 07 '26 03:07 Hmbown

Thanks for picking this up so quickly! Having always_load for MCP tools is actually pretty important for my workflow. Really appreciate you acting on the suggestion. BTW, noticed the CI failure on detect.rs, so I cant test it out yet — might need a quick follow-up. Keep up the great work! Wishing you and CodeWhale all the best!

SparkofSpike avatar Jul 07 '26 04:07 SparkofSpike