MoonshotAI / MoonshotAI/kimi-cli

[Feature] Lazy-load MCP tool schemas into context — only inject when tools are needed

Open
#2,147 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
11.4k
Forks
1.3k
Avg merge
9h 47m
Merged PRs (30d)
2

Description

Problem

When MCP servers are configured, all tool schemas (names, descriptions, input JSON schemas) are injected into the LLM context at the start of every session. With multiple MCP servers this can consume thousands of tokens of context budget before the user sends a single message.

This leaves less room for conversation history and actual work, and hurts quality on long tasks — especially in sessions that never end up using most of the configured MCP tools.

Related issues

  • #1177 — disabling specific tools is a partial mitigation, but tool schema injection still happens upfront at session start
  • PR #1486 — deferred async MCP server connection startup (great improvement, but doesn't address schemas being loaded into LLM context)

Proposed solution

Lazy-load MCP tool schemas into the LLM context only when tools are actually needed, rather than injecting all schemas at session start. A few approaches:

  1. On-demand discovery: inject a lightweight list_tools stub first; only fetch and inject full schemas when the model determines a tool domain is relevant to the current task
  2. Task-scoped injection: analyse the user's first message and only inject schemas for servers likely to be relevant
  3. Deferred full load: keep schemas out of the initial context and inject them progressively as tools are invoked

Prior art

  • Claude Code uses a ToolSearch deferred-tool primitive — MCP tool schemas are not loaded into context until explicitly fetched, keeping the base context lean
  • opencode tracks the same request at anomalyco/opencode#8277

Impact

Users with many MCP servers configured (10+ servers, 50+ tools) can lose a significant fraction of their context window before any work begins. Lazy loading would make Kimi Code CLI viable for heavily MCP-configured environments without sacrificing context budget.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files, tests, or code entry points. Start by tracing where MCP tool schemas enter the session context, then review related issue #1177 and PR #1486 alongside the three proposed approaches. Done means schemas are not loaded at session start but become available when needed without sacrificing tool functionality.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
ai, cli
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.