Skip to content

    AI Architect Case Files

    Case: Engineering Assistant, MCP and Context Overflow

    An engineering assistant with repo and ticket tools over MCP filled its context window with tool output. Scoped tools and a session budget fixed it.

    By Published 7 min read

    • ai-architect
    • case-file
    • industry-developer-tools
    • pattern-mcp
    • failure-context-overflow
    • kind-composite
    Share

    Composite scenario, not a real company. Assembled from common industry patterns; every number is illustrative and its assumptions are listed at the end.

    This case belongs to Agents, Tools and MCP.

    Context

    • A software company with 300 engineers builds an internal assistant for questions such as "why did the checkout build fail after PR 4812, and is there a ticket for it?".
    • It reaches the company's systems through two MCP servers (Model Context Protocol, a standard for exposing tools to model applications), each owned by a different team.
    • The redesign replaces whole-object tools such as get_diff with scoped ones such as list_changed_files.
    MCP server Tools
    Repo search_code, read_file, get_diff, list_commits
    Tickets search_tickets, get_ticket

    Problem statement

    Pilot traces showed that a typical question needed about 6 tool calls (7 model calls, the last one writing the answer). The tools returned whole objects: a large diff, a ticket with its entire comment thread.

    Pilot measure Value
    Average tool result 18,000 tokens
    Base (instructions plus tool definitions) 6,000 tokens
    Context window (assumed) 128,000 tokens
    Final call of a typical session 114,000 tokens
    A seventh tool call 132,000 tokens: over the window, so it fails
    Input tokens across the seven model calls 420,000

    Goals: median session under 75,000 input tokens, no context-length failures, and every answer citing its file or ticket.

    Constraints

    • Fixed: the assistant is read-only, and repo access must follow the asking engineer's own permissions.
    • Fixed: tickets can contain customer data; only approved model providers may see them.
    • Fixed: the MCP servers belong to other teams.
    • Limit: a 30-second answer target for typical questions.

    Why the traditional approach fell short

    Approach Where it breaks
    Code search Finds strings but cannot connect a failing build, the diff that caused it and the ticket that describes it, so engineers hop between three tools
    Wikis, runbooks, a scripted bot Cover the questions someone expected
    A model with tools Can connect the evidence, but only if it fits in its context

    Options considered

    Option Verdict
    A. A dashboard linking build, PR and ticket (non-AI): one page per failing build with the diff summary and the matching tickets Kept as the baseline and as a link target in answers
    B. Assistant with scoped, paginated, truncating tools and a session budget: smaller tools, capped results, cursors, a token budget per session with a defined behaviour at the limit Accepted
    C. Keep the broad tools and buy a bigger context window Rejected: the same session still processes about 5.7 times the tokens (see Results) and the window only delays the failure

    Solution architecture

    Figure 1. Assistant components with two MCP tool servers

    Engineering assistant componentsClient app to API gateway; API gateway to Orchestrator; Orchestrator to Model gateway; Model gateway to LLM provider; Orchestrator to Policy engine; Orchestrator to Tool server: repo (MCP tools/call); Orchestrator to Tool server: tickets (MCP tools/call); Tool server: repo to Repo host; Tool server: tickets to Ticket system; Orchestrator to Trace storeCOMPANY BOUNDARYLLM provider: approved listLLM providerapproved listModel gatewayModel gatewayTrace store: tokens per callTrace storetokens per callPolicy engine: asker's repo accessPolicy engineasker's repoaccessClient app: IDE or chatClient appIDE or chatAPI gatewayAPI gatewayOrchestrator: budget, result shapingOrchestratorbudget, resultshapingTool server: repoTool server:repoTool server: ticketsTool server:ticketsRepo host: existingRepo hostexistingTicket system: existingTicket systemexistingMCP tools/callMCP tools/call

    How to read this: tool results pass through the Orchestrator before the model sees them, so that is where the size limit and the budget are enforced.

    Figure 2. One question, with the budget-exhausted path

    sequenceDiagram accTitle: Runtime path of an engineering question accDescr: The Orchestrator lists tools once per session, then the model calls small scoped tools. Each result is capped and carries a cursor and a note on what was left out. After each result the Orchestrator checks the session token budget. Within budget the model answers with citations. If the budget is spent, the engineer gets a partial answer that says what was not read. autonumber participant CA as Client app participant OR as Orchestrator participant MG as Model gateway participant R as Tool server repo participant K as Tool server tickets participant TS as Trace store CA->>OR: why did the checkout build fail after PR 4812 OR->>R: tools/list, once per session (the ticket server likewise) R-->>OR: tool definitions OR->>MG: question and tool definitions MG-->>OR: call list_changed_files for PR 4812 OR->>R: tools/call R-->>OR: first 25 of 40 files, cursor for the rest OR->>MG: result of about 1,500 tokens MG-->>OR: call get_file_diff for one path OR->>R: tools/call R-->>OR: first 2,000 tokens of the diff, note on what was cut MG-->>OR: call search_tickets with limit 5 OR->>K: tools/call K-->>OR: 5 concise records Note over OR: Budget check after every result alt within the session budget OR->>MG: results MG-->>OR: answer citing file and ticket OR-->>CA: answer with links else budget spent OR-->>CA: partial answer, lists what was read and what was not end OR-)TS: trace with tokens per tool call

    How to read this: steps 5 to 14 repeat for each tool call, and the alt block is the decision the Orchestrator makes after every result.

    How AI solved it, step by step

    Step through one question, then switch to "Without the fix".

    One engineering question (interactive when JavaScript is on)

    As designed

    1. Question and identity. The engineer asks the question. The Orchestrator identifies the user, so the repo server applies that user's permissions.
    2. List tools once. At session start the Orchestrator lists the tools (tools/list) and sends the model only the definitions it needs.
    3. Changed files. The model calls list_changed_files for the PR. The result is capped, shows 25 of 40 files and carries a cursor, about 1,500 tokens.
    4. One scoped diff. The model picks the files that touch checkout and calls get_file_diff for one path. The server returns at most 2,000 tokens and says how much was cut and how to ask for the next page.
    5. Concise tickets. The model calls search_tickets with a limit of 5. Results use a concise format: title, id, status and one line, not the thread.
    6. Budget check. After each result the Orchestrator adds the result's tokens to the session total and compares it with the budget.
    7. Answer or partial answer. Within budget, the model answers citing the file and ticket it used. If the budget is spent first, the Orchestrator returns what was read and asks the engineer to narrow the question.
    8. Trace. The Orchestrator writes a trace with tokens per tool call.

    Without the fix

    1. Whole-object tools. get_diff returns a large diff and get_ticket the entire comment thread. The average result is 18,000 tokens.
    2. Context grows each call. Call k carries 6,000 + (k - 1) x 18,000 tokens. The sixth tool result is in the context by the seventh model call: 114,000 tokens.
    3. A seventh tool call. The next call would carry 132,000 tokens, over the 128,000 window, and fails.
    4. Even when it fits. Answers begin to miss details from early in the session, and the seven calls process 420,000 input tokens.

    Under the hood

    MCP messages are JSON-RPC 2.0, sent over stdio or Streamable HTTP. In the specification revision 2025-11-25:

    Step Message
    1 The client sends initialize with its protocol version, capabilities and clientInfo
    2 The server answers with its capabilities (for example tools with listChanged)
    3 The client sends notifications/initialized
    4 tools/list returns the tool definitions; tools/call runs one

    The latest revision on the specification site as of 2026-10-21, 2026-07-28, drops the handshake: every request carries the protocol version, and its compatibility page says a modern client fails against a legacy server. Pin the revision.

    What matters for context:

    • The tool definitions go into every model request, and Anthropic's documentation counts the tools parameter, tool_use blocks and tool_result blocks as input tokens.
    • The MCP tools page (revision 2026-07-28) defines no maximum result size, so limiting is your job. It also says servers SHOULD return tools in a deterministic order, which helps prompt-cache hit rates.
    • Anthropic's "Writing effective tools for agents" says Claude Code restricts tool responses to 25,000 tokens by default, recommends pagination, filtering, range selection and truncation with sensible defaults, and shows a concise response of 72 tokens against 206 for the detailed one.

    See the lesson's MCP section.

    What went wrong / failure modes

    Failure Mitigation
    A diff or thread floods the context A default cap of 2,000 tokens per result, cursors, and a concise format by default
    Truncation hides the key line The result says what was cut and how to ask for it; Anthropic's post suggests steering agents to "many small and targeted searches"
    Redundant calls The Orchestrator returns the earlier result and counts the call against a limit of 12
    Tool list growth: every added server adds definitions to every request A cap on tool count
    Permission leak: a shared service token exposes private repos The server uses the asking user's identity and the Policy engine checks the repo argument
    Protocol drift: a library upgrade changes the MCP revision A contract test (tools/list and one tools/call) in CI
    Customer data and injected text in tickets Read-only tools, approved providers only, ticket text treated as untrusted

    Results

    These are illustrative targets, not measurements. For a typical session of seven model calls, the input tokens processed are the sum of 6,000 + (k - 1) x r over k = 1 to 7.

    Measure First design (r = 18,000) Redesign (r = 1,500)
    Input tokens across 7 calls 7 x 6,000 + 18,000 x 21 = 420,000 42,000 + 31,500 = 73,500
    Ratio 420,000 / 73,500 = 5.7
    Tokens carried into a seventh tool call 6,000 + 7 x 18,000 = 132,000, over the 128,000 window 6,000 + 7 x 1,500 = 16,500
    Goal of under 75,000 Missed Met

    Tradeoffs and when NOT to use this approach

    • Smaller tools need more calls for broad questions, so latency rises while tokens fall.
    • Skip MCP for a single in-house tool; a direct function call is simpler (MCP vs function calling).
    • Do not launch if repo permissions cannot be enforced per user.

    Design rules extracted

    1. Size every tool result: a default cap, a cursor, a concise default format.
    2. Set a per-session token budget and decide in advance what the user sees at the limit.
    3. Say what was omitted inside the result so the model can narrow its next call.
    4. Run tools as the asking user, not as a shared service account.
    5. Pin the protocol revision and contract-test every server.

    Assumptions

    • All numbers are chosen for illustration: a 128,000-token window, a 6,000-token base, 7 model calls (6 tool results and the answer), an average of 18,000 tokens per result in the first design and 1,500 in the second.
    • Input tokens for call k are 6,000 + (k - 1) x r; the model's own output tokens are ignored. 18,000 x (0 + 1 + ... + 6) = 378,000, plus 42,000, gives 420,000.
    • The 2,000-token cap, the 25-of-40 file page, the limit of 12 calls and the 75,000-token goal are illustrative choices.
    • The company, its repo and ticket systems, and PR 4812 are invented.

    Last verified: 2026-10-22

    Ask about this article

    Get answers grounded in this post. AI-generated — based on this article, and may be imperfect.

    Scaled AI Weekly

    Enjoyed this? Get more like it every Monday.

    Real architecture decisions, LLMOps patterns that survive production, and engineering leadership advice — from 12+ years of building at enterprise scale. Free. No spam. Unsubscribe anytime.

    Get new articles by email

    Free. We email a confirmation link first, and you can unsubscribe from any email.

    Join engineers building production AI systems

    Comments