ai claude-code anthropic lsp mcp software-engineering automation context-management agentic-workflows

Architecting the Claude Code Harness: Optimizing Context Management via LSP, MCP, and Hierarchical Instruction Sets

6 min read

Beyond Grep: Engineering a Robust "Harness" for Claude Code in Large-Scale Repositories

When deploying Claude Code across massive, multi-million line monorepos or legacy distributed architectures, developers frequently encounter a critical failure mode: the model begins to "lose the plot." Symptoms include hallucinated file paths, incorrect edits, and a general degradation of logic. While the instinctive reaction is to demand a more powerful model (e.g., moving from Sonnet to Opus), the bottleneck is rarely the model's inherent reasoning capability. Instead, the issue lies in the harness—the environmental configuration surrounding the model.

The Fundamental Limitation: No Pre-Indexing

To understand why Claude Code fails at scale, we must first dispel the myth that it operates via a RAG (Retrieable Augmented Generation) layer. Unlike many AI coding assistants, Claude Code does not pre-index your repository into a vector database. There is no embedding step and no background RAG layer running to provide semantic retrieval.

Instead, Claude Code operates via a highly literal, engineer-like navigation pattern: it executes grep commands and follows explicit import statements. In a small, contained project, this is highly efficient. However, in a large-scale codebase, the lack of a semantic index means the model has no way of knowing where to look. It is essentially navigating a dark room with a flashlight, relying entirely on the traces left by imports and text matches.

Anthropic’s recent technical playbook outlines a seven-point "harness" designed to provide the necessary structure to prevent context drift and hallucination.

1. Hierarchical Instruction Sets: The claude.md Layering Mechanism

The most critical component of the harness is the claude.md file. This is not merely a README for humans; it is a machine-readable instruction set for the agent. The most effective implementation utilizes a hierarchical, layered approach.

Rather than a single, bloated clarad.md at the repository root containing hundreds of lines of rules, you should implement a tiered configuration:

  • Root claude.md: Contains high-level, immutable project truths (e.g., "All secret keys must be handled via environment variables," or "The project structure consists of /dashboard and /agents").
  • Directory-Specific claude.md: Located within specific subdirectories (e.g., /agents/claude.md), these files contain localized rules (e.g., "Use the following specific LLM model for all prompts in this folder").

Claude Code automatically implements context stacking. When the agent enters a specific directory, it reads the root instructions and then layers the local instructions on top. This prevents "context noise"—the phenomenon where irrelevant rules from unrelated parts of the codebase consume the context window and dilute the signal-to-noise ratio.

Pro-tip: Periodically audit your claude.md files. Use a "deletion test": if you remove a rule and the model's performance does not degrade, the rule is dead weight and should be purged to save tokens.

2. Self-Improving Loops via Hooks

Hooks are often misunderstood as simple guardrails to prevent file deletion. While they can serve that purpose, their true power lies in making the development environment self-improving.

  • Start Hooks: These fire at the beginning of a session. They are ideal for injecting dynamic, role-based context. For example, a backend engineer's session could trigger a start hook that loads specific infrastructure context, while a frontend engineer's session loads UI-specific constraints.
  • Stop Hooks: These fire after the model completes a turn. The most advanced use case for a stop hook is to trigger a "reflection" session. A stop hook can launch a headless Claude instance to review the recent session history and propose updates to the claude.md files while the context is still fresh.

Critical Warning: When implementing stop hooks, ensure they remain passive. If a stop hook triggers a command that forces Claude to respond again, you risk creating an infinite execution loop. Always use a stop_hook_active flag to break out of such loops.

3. Path-Scoped Skills and Plugins

Skills are reusable, programmable workflows. A skill allows you to wrap a complex, multi-step process (like a deployment sequence or a security audit) into a single, callable command.

The key to scaling skills is path-scoping using glob patterns. By binding a skill to a specific directory pattern (eera: path: "agents/**"), the skill only enters the model's context when the agent is working within that specific subtree. This keeps the context window clean and prevents the model from being overwhelmed by irrelevant workflows.

Plugins take this a step further by bundling skills, hooks, and MCP configurations into a single, distributable package. This is the solution to "tribal knowledge" in engineering teams; a new engineer can install a single plugin and immediately inherit the entire optimized harness of the senior staff.

4. Precision Navigation via LSP (Language Server Protocol)

The introduction of LSP support in Claude Code (v2.0.7.4) is perhaps the most significant technical upgrade for large-scale navigation.

Without LSP, Claude relies on grep, which is a text-based pattern matcher. If a function named process() exists in five different files, grep returns all five, forcing the model to read all five to identify the correct one. This consumes unnecessary tokens and increases the risk of error.

With LSP integration, Claude can utilize the same symbol-based navigation as a human developer in an IDE. It can perform "Go to Definition" and "Find References" operations. This allows the model to follow a function call straight to its actual implementation, bypassing irrelevant symbols and significantly reducing the token overhead required for codebase exploration.

_5. External Integration via MCP (Model Context Protocol)

The Model Context Protocol (MCP) acts as the interface between Claude Code and your external ecosystem. MCP servers allow Claude to interact with Jira, GitHub, Slack, Sentry, and internal databases.

The beauty of MCP is that it respects existing OAuth-based permissions. If you have access to a Sentry issue, Claude has access to it via the MCP server. This allows for a seamless transition from "reading code" to "acting on operational data."

6. Context Splitting with Sub-agents

The most powerful tool for managing massive context windows is the use of sub-agents. A sub-agent is a separate Claude instance with its own independent context window.

The optimal pattern is to split exploration from editing. Instead of asking the main agent to "crawl the entire database layer and then implement a change," you should:

  1. Spin up three parallel sub-agents (one for the database, one for the dashboard, one for the change scope).
  2. Each sub-agent performs a thorough, read-only crawl of its specific subsystem.
  3. Each sub-agent returns a clean, high-level summary to the main agent.

The main agent then receives three concise summaries instead of three massive, token-heavy crawls. This architecture allows you to scale your development tasks horizontally, effectively bypassing the limitations of a single context window.

Conclusion: The Rise of the AI-Native Developer Experience (DX)

As we move toward more complex, agentic workflows, the role of the engineer is shifting toward managing the harness. Whether you are a solo developer or part of a large enterprise, the goal is to build a structured, hierarchical, and automated environment that guides the model. By mastering claude.md layering, LSP integration, and sub-agent orchestration, you can transform Claude Code from a simple coding assistant into a highly reliable, autonomous engineering partner.