Context Hydration: Abstracting Merge Request Metadata for LLM Consumption
The Hook Feed a raw, unprojected enterprise GraphQL Merge Request graph directly into an autonomous coding agent's context window, and you will watch 40% of your token budget vanish into deeply nested pagination edges,
The Hook
Feed a raw, unprojected enterprise GraphQL Merge Request graph directly into an autonomous coding agent's context window, and you will watch 40% of your token budget vanish into deeply nested pagination edges, redundant color hex codes, and internal global IDs before the model ever reasons about a single line of diff.
The Deep Dive
In enterprise AI tooling, context hydration is the architectural boundary between how a relational database or GraphQL schema stores domain entities and how an LLM consumes them via the Model Context Protocol (MCP). A human browser client rendering a Merge Request dashboard needs UI-specific presentation metadataβavatar URLs, label background colors, text contrast ratios, and GraphQL __typename nodes. An autonomous DevSecOps agent triaging a review queue, by contrast, requires high-entropy operational state: who is assigned to review (reviewers), semantic classification (labels), readiness gates (draft, detailedMergeStatus), temporal freshness (createdAt, updatedAt), and exact cryptographic commit state (diffHeadSha).
While authoring MR !270521 (feat(mcp): add merge request read fields and reviewer filter) to resolve Issue #639870 in gitlab-org/gitlab, I engineered the context hydration layer for GitLab's list_merge_requests and get_merge_request MCP tools. Exposing reviewers, labels, and merge-status fields across both GraphQL-backed (ListMergeRequestsService) and REST-backed (get_merge_request) tool pipelines revealed a severe token-economics bottleneck. When querying 100 merge requests on gitlab-org/gitlab, projecting full label objects (id, title, color, description, textColor) inflated the serialized JSON response from 80 KB to 132 KBβan extra 52 KB (~13,000 tokens) of pure structural noise on every agent turn.
To solve this without violating GitLab's strict GraphQL linter (@graphql-eslint/require-selections), I restricted the GraphQL selection set in app/graphql/queries/mcp/merge_requests/list_merge_requests.query.graphql to the minimal scalar leaf labels { nodes { title } } and implemented a post-query hydration transform in format_merge_request that collapses the nested { "nodes": [{ "title": "..." }] } graph into a flat scalar array of label strings (["backend", "type::feature"]). Combined with batch-preloaded associations verified via ActiveRecord::QueryRecorder across both CE (FOSS_ONLY=1) and EE specs, this reduced the 100-MR payload back down to 80 KBβcutting context-window consumption by ~40% while delivering zero-N+1 database execution.
The GitLab Hero Angle
GitLab's MCP server (MR !270521) treats context hydration as a first-class systems disciplineβabstracting complex GraphQL and REST merge request graphs into token-dense, N+1-free payloads so GitLab Duo and MCP agents can reason over 100+ enterprise MRs in a single pass.
The Takeaway
Never treat an MCP tool as a raw pass-through proxy for your UI GraphQL schema; project only decision-critical scalars (detailedMergeStatus, diffHeadSha, reviewers) and flatten nested connection nodes into compact scalar arrays before hydrating the LLM context window.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.