You open a new coding session. Your agent loads every file in the repository into context. Half a million tokens. The context window fills before you type a single instruction. The model reads your entire codebase, then forgets the middle of it because attention falls off at scale.

That approach is dying. A new pattern is replacing it: MCP context injection. Instead of preloading everything, the agent starts lean and pulls in relevant code on demand through vector search. The tool that popularized this shift just crossed 7,259 stars with 873 added today.

The Problem With Whole-Directory Loading

The old pattern works like this. Your system prompt includes a list of files. The agent reads them all at session start. For small projects, this is fine. For large codebases, it breaks.

First, token budget. Every file loaded at startup eats tokens that cannot be used for reasoning or tool results. A 200K context window with 150K of preloaded code leaves almost no room for the model to think.

Second, the lost-in-the-middle problem. Research from July 2023 (arXiv 2307.03172) showed that language models attend less reliably to content in the middle of long contexts. Your most important files, buried in the middle of a massive preload, get the least attention.

Third, cost. Loading an entire repository for every request is expensive. If you pay per token, you are paying for thousands of lines the model never references.

Anthropic's engineering blog frames the problem directly: treat context as "a precious, finite resource" and assemble "the smallest set of high-signal tokens that maximize the likelihood of your desired outcome."

How MCP Context Injection Works

The new pattern splits context into two layers.

Startup layer: Instructions, conventions, and tool descriptions. Small. Fixed. Always present.

On-demand layer: Documentation, file contents, search results. Fetched only when the task requires them.

The Model Context Protocol (MCP) makes this possible. MCP servers expose data sources as tools. The agent receives tool descriptions at startup, then fetches content when needed. Nothing enters the prompt until the agent asks for it.

The key shift: from "write everything in the system prompt" to "design retrieval-augmented context pipelines."

zilliztech/claude-context: The Tool That Made It Real

The project driving this shift is zilliztech/claude-context. At 7,259 stars and climbing fast, it is an MCP plugin that adds semantic code search to Claude Code and other AI coding agents.

The architecture is straightforward:

  • Your codebase gets indexed into a vector database (Milvus/Zilliz Cloud)
  • OpenAI embeddings encode each code chunk
  • When the agent needs context, it issues a semantic search query via MCP
  • Only relevant code snippets enter the prompt

The tool supports Claude Code, OpenAI Codex CLI, Gemini CLI, Qwen Code, and Cursor. That breadth matters. This is not a vendor-specific trick. It is a pattern that works across the entire agent ecosystem.

The cost argument is compelling. For a 1-million-line codebase, loading everything costs tens of dollars per request. Semantic search over the same codebase costs cents per query, because only the 10-50 most relevant snippets enter the model's context.

Why This Beats Negative-Prompt Approaches

You might think: just tell the agent which files to ignore. Put exclusions in the system prompt. That is still prompt stuffing, just with a shorter list. You are spending tokens on file paths and exclusion rules instead of code content.

MCP context injection skips the exclusion entirely. The agent never sees irrelevant code because the retrieval layer filters it out before it reaches the model. No tokens wasted. No attention diluted by irrelevant context.

This is the retrieval-augmented generation (RAG) pattern applied to agent context. The same principle that makes RAG work for question answering works here: selective retrieval beats wholesale loading.

The Progressive Context Loading Connection

This pattern connects to the broader progressive context loading trend. AgentPatterns.ai documents it as "Retrieval-Augmented Agent Workflows: On-Demand Context." The pattern has several aliases: Context Hub, Semantic Context Loading, JIT Context.

The trade-off is latency. On-demand retrieval adds round-trip time. Multi-step retrieval chains (search, read, search again) can slow throughput. Preloading eliminates that latency at the cost of context budget.

The right balance depends on task structure:

  • Repetitive access to the same document: preload it
  • Exploratory tasks where the relevant subset is unknown: retrieve on demand
  • Long-horizon tasks: combine both, then compact when context fills

What This Means for Prompt Engineering

MCP context injection shifts where prompt engineering effort goes.

Old approach: Spend hours writing system prompts that enumerate files, set boundaries, and list exclusions. The prompt is the context.

New approach: Spend time designing the retrieval pipeline. Choose the right embedding model. Configure the vector database. Set chunk sizes and overlap windows. The prompt stays lean. The retrieval system provides context.

This does not eliminate prompt engineering. It changes the surface area. Your system prompt becomes smaller and more focused. Your retrieval configuration becomes the primary lever for context quality.

For teams building with agents, the implication is clear. Invest in vector infrastructure now. The cost savings compound over time, and the quality improvements from selective context are measurable. Models perform better with fewer, more relevant tokens than with massive undifferentiated context dumps.

The Bigger Picture

MCP context injection is one instance of a broader shift. The agent ecosystem is moving from prompt-centric design to pipeline-centric design. The question is no longer "what do I put in my prompt?" It is "what pipeline feeds my prompt at runtime?"

Vercel's Skills CLI (15,295 stars) installs instruction packages across 41+ agents. Context7 provides on-demand documentation retrieval. Claude-context provides on-demand code search. Each tool narrows the gap between what the model needs and what it receives.

The pattern is converging. Agent context in 2026 looks like a lean startup layer plus a rich retrieval layer. The agents that perform best are the ones that ask for what they need, when they need it, instead of loading everything just in case.


Citations

  • zilliztech/claude-context — github.com/zilliztech/claude-context (7,259 stars, +873 today)
  • "Lost in the Middle: How Language Models Use Long Contexts" — arXiv 2307.03172, July 2023
  • Anthropic Engineering Blog: "Effective Context Engineering for AI Agents" — anthropic.com/engineering/effective-context-engineering-for-ai-agents
  • AgentPatterns.ai: "Retrieval-Augmented Agent Workflows: On-Demand Context" — agentpatterns.ai/context-engineering/retrieval-augmented-agent-workflows/
  • Vercel Skills CLI — github.com/vercel/skills (15,295 stars)
  • Contextual AI: "Context Engineering for your MCP Client" — contextual.ai/blog/context-engineering-for-your-mcp-client

Suggested Title

MCP Context Injection: Why Selective Retrieval Beats Stuffing Your Agent's Prompt

Word Count

928

Fact-Check Notes

  • Star count of 7,259 for zilliztech/claude-context comes from the task context provided at queue time. GitHub currently shows ~6,395 stars. The task context likely reflects a snapshot that included trending momentum. The discrepancy is minor and may reflect different snapshot times. I used the task context number but flagged this.
  • The "873 stars today" comes from the task context. Cannot independently verify daily star counts from static web fetches.
  • The arXiv 2307.03172 "Lost in the Middle" paper is well-established and confirmed.
  • Anthropic engineering blog reference is confirmed from multiple sources.
  • AgentPatterns.ai content was directly fetched and confirmed.
  • The claim that MCP context injection "saves cost" is a logical inference from token pricing models, not a direct quote from the claude-context repository. The repository does mention cost-effectiveness explicitly.