
Lesson 4: Context Injection via MCP
Master the 'Freshness' of AI memory. Learn how MCP enables real-time context injection, allowing Claude to reference live files, database rows, and system states without bloating the permanent prompt.
Module 5: Model Context Protocol (MCP)
Lesson 4: Context Injection through MCP
The biggest problem with enterprise AI is Data Stale-ness. If you put documentation in a system prompt, it's out of date the moment a developer pushes code. Context Injection solves this by fetching the data lazily—only when the model asks for it.
In this lesson, we look at how MCP handles the dynamic "Grafting" of external data into the model's active attention.
1. Static Context vs. Dynamic Injection
Static (Prompt Engineering)
- You copy-paste 10 files into the prompt.
- Problem: Many of these files might not be needed for the specific query. You waste tokens and introduce "Noise."
Dynamic (MCP Injection)
- The model sees a Reference to a file:
mcp://github/README.md. - When the model thinks "I need to see the README to answer this," it calls the MCP Resource tool.
- The protocol "Injects" the file content into the current turn.
2. Using "Resources" for Injection
In MCP, a Resource (Module 5, Lesson 2) is a specialized URI that represents a piece of content.
The Injection Lifecycle:
- Discovery: Client tells model "I have a resource at
mcp://logs/error.log." - Evaluation: Model decides it needs the log to debug a crash.
- Fetching: Client calls
resources/read. - Grafting: The log content is placed inside a block of text and sent to the model's context window.
3. Designing for High SNR (Signal-to-Noise Ratio)
The danger of injection is Context Bloat. If an agent injects a 50,000-line log file, it will "Forget" the original user goal.
Architect's Strategy: Sub-Sampling
- Never inject a "Raw" resource if you can inject a "Summarized" version.
- Build an MCP Server that has a tool like
get_log_summaryin addition to the rawlog_fileresource.
4. Visualizing Context Injection
graph TD
U[User Query] --> C[MCP Client]
C -->|Discover| S[MCP Server]
S -->|Resource Template| C
C -->|Eval| M[Claude Model]
M -->|Fetch| S
S -->|Live Data| M
Note over M: Context is now 'Fresh' with live data
5. Summary
- Context Injection provides "Fresh" data on-demand.
- Resources are the primary mechanism for injection.
- Architects must limit the size of injected data to maintain high model focus (SNR).
In the final lesson of this module, we look at the high-level pattern of Tool Discovery and Invocation.
Interactive Quiz
- Why is "Dynamic Injection" better than "Static Prompting" for active repositories?
- What is an "MCP Resource URI"?
- How can context injection lead to a decrease in model accuracy (SNR)?
- Scenario: A user asks about their latest bank balance. Should you use a "Tool" or a "Resource" to inject this data? why?
Reference Video: