The Infrastructure Layer Behind AI Agents: Building vs Operating


Rohit Gite, Founder @MewCP
The agent code barely changes as you grow. Everything between the agent and its tools changes completely. Here is the reference architecture for that layer.
There is a moment in most agent projects when the work changes shape without anyone announcing it.
Before that moment, the questions are about the agent. Does the tool call work? Which prompt produces better plans? Did it answer correctly? You iterate on prompts, tool descriptions and model choice, and progress is visible in the chat window.
After that moment, the questions are about everything around the agent. Why did Acme's Slack token stop working at 3am? Why is every customer slow when one customer runs a batch job? Which upstream is rate-limiting us, and whose quota did we burn? What exactly did the agent do for this user last Tuesday, and can we prove it?
The first set of questions is building. The second set is operating. They are different jobs, they need different architecture, and teams that have only planned for the first one tend to discover the second in production.
Day 26 laid out the ten-layer stack an agent platform needs. This post zooms into one part of it: the layer that sits between the agent and its tools. It covers what that layer is responsible for, a reference architecture with clear component boundaries, the life of a single request through it, how authorization works for remote MCP servers, per-tenant rate limiting with code, what operating it looks like week to week, and a build-versus-adopt call for each component.
The clearest sign of which job you are doing is where your time goes as usage grows.
As an agent goes from one user to a thousand, the agent code itself changes surprisingly little. The loop is the same loop. The planning prompt might get tuned. A few tools get added. Meanwhile, the work around the agent multiplies:
Almost none of that growth happens inside the agent. It happens in the space between the agent deciding "call send_message with these arguments" and a real HTTP request reaching Slack. That space is the infrastructure layer.
| Building asks | Operating asks |
|---|---|
| Does the tool call work? | Does it work for every tenant at 9am on a Monday? |
| Which prompt is best? | Whose token is this call using, and whose quota does it spend? |
| Did it answer correctly? | Can we prove what it did, and reconstruct it later? |
| Can the agent use Slack? | What happens to 400 in-flight runs when Slack returns 503 for ten minutes? |
| Does the new tool help? | Who is allowed to see the new tool, and what does it cost us per call? |
Neither column is more important. But they need different people thinking about them, and the right column needs infrastructure the left column never touches.
The infrastructure layer decides who can call what, with whose credentials, how often, and records what happened.
Every component in the reference architecture exists to answer one of those five questions. Grouped by purpose, there are nine:
Access (who can reach which tool)
Control (keeping it running under load) 6. Gateway - one entry point where policy is enforced 7. Rate limiting - per tenant, per tool and per upstream 8. Execution - timeouts, retries, idempotency and cancellation 9. Observability - traces, cost and execution history for every call

Here is the component map, top to bottom:
┌───────────────────────────────────────────────────────────┐
│ AI APPLICATION (your product UI, API, schedulers) │
└──────────────────────────┬────────────────────────────────┘
│ user session / API token
┌──────────────────────────▼────────────────────────────────┐
│ AGENT RUNTIME (loop, planning, memory, model calls) │
└──────────────────────────┬────────────────────────────────┘
│ MCP (Streamable HTTP) + request identity
╔══════════════════════════▼════════════════════════════════╗
║ GATEWAY ║
║ authenticate → resolve tenant → tool access policy → ║
║ validate args → rate limit → resolve credential → ║
║ dispatch with timeout → record trace ║
║ ║
║ ├─ Tool registry (what exists, who may see it) ║
║ ├─ Credential vault (encrypted tokens, refresh) ║
The double-bordered box is the infrastructure layer. Three boundaries in this diagram matter more than the boxes.
Boundary one: the agent never holds upstream credentials. The agent runtime authenticates to the gateway with an identity that says who the user and tenant are. It never sees a Gmail token or a Slack token. Those are resolved inside the gateway, injected at the last moment, and never travel back up. This is the single most important property of the layer, because the agent is the component that reads untrusted text.
Boundary two: every tool call passes through one policy point. There is no path from the agent to an upstream that skips the gateway. That is what makes access policy, rate limits and audit trails complete rather than best-effort. A second path, even one built "just for internal tools," becomes the place where the next incident happens.
Boundary three: MCP servers are replaceable. Whether a server is one you host, one a platform hosts for you, or one a vendor publishes, it sits behind the gateway and speaks MCP. Swapping it does not change the agent.
Walk one tool call through the layer. A user at Acme asks the agent to "post the release notes to #eng." The agent decides to call slack.send_message with a channel and text.
tools/call request to the gateway, over Streamable HTTP, carrying an access token that identifies the user and tenant. The tool arguments contain the channel and the text. They do not contain a tenant id, a user id or a Slack token.slack.send_message enabled for Acme's plan? Does this user's role allow write tools? Has Acme connected Slack at all? If any answer is no, the call fails here with a clear, structured error.Step 10 is underrated. An agent that receives raw upstream errors has to learn every API's error dialect. An agent that receives "needs user action: Slack connection revoked" can tell the user exactly what to do.
MCP defines two standard transports. stdio runs the server as a local subprocess of the client, which is ideal on a developer's machine: no network, no hosting, credentials read from the local environment. Streamable HTTP runs the server as a network service that clients connect to remotely. It replaced the older HTTP plus Server-Sent Events transport in the 2025 revisions of the specification.
That distinction is the first infrastructure decision. stdio servers are per-process and per-machine. They do not serve many users, they cannot be shared across a fleet of agent workers, and their credentials are whoever launched them. The moment tools need to serve many users from shared infrastructure, they become remote servers, and a remote server is a service: it needs deployment, uptime, versioning, scaling, logging and authentication like any other.
Hosting one remote MCP server is a small project. Hosting twenty, each wrapping a different third-party API with its own OAuth app, scopes, token lifetime and rate limits, is an ongoing operational commitment. Day 28 covers the self-host versus hosted decision in detail; for this architecture the important point is that the gateway treats all servers the same way, so that decision can be made per server rather than once for everything.
In a prototype, the agent sees every tool that exists. In a platform, the tool list is a function of who is asking: tenant plan, user role, which accounts are connected, and which tools are enabled.
Two scaling problems appear together here.
Visibility. A tool that a tenant cannot use should not appear in that tenant's tool list at all. Refusing it at call time is necessary but not sufficient, because a model that can see a tool will sometimes try to use it, and each failed attempt costs a turn.
Volume. Every tool definition in the model's context costs tokens and adds one more option to confuse with another. Past a few dozen tools, exposing everything up front becomes expensive and hurts selection accuracy. The common response is a discovery pattern: the agent is given a small, fixed set of meta-tools (search for tools by intent, fetch a specific tool's schema, then call it) and loads only the definitions it needs for the current step. This moves tool discovery out of the prompt and into the infrastructure layer, where it can be filtered by policy before the model ever sees a result.
The gateway is the policy enforcement point, and for remote MCP it is also where the protocol's authorization model applies.
For HTTP-based transports, the MCP specification bases authorization on OAuth 2.1. In the current revisions of the spec:
That last rule is why the architecture has two distinct authorization hops, and why credential management is a separate component:
Conflating those two hops is the root cause of most agent credential bugs. The first token proves identity to your infrastructure. The second token is a secret your infrastructure holds on the user's behalf. They have different issuers, different lifetimes and different blast radii.
A gateway's policy pipeline, reduced to its shape, looks like this:
type Step = (ctx: CallContext) => Promise<void>;
interface CallContext {
readonly identity: { tenantId: string; userId: string; scopes: ReadonlySet<string> };
readonly
Authentication happens before a CallContext exists at all, which is why identity is readonly and not a pipeline step: nothing downstream should be able to construct or alter it. Tracing wraps handleToolCall from the outside, so rejected calls are recorded as well as successful ones.
The credential component has four responsibilities.
Storage. Tokens are encrypted at rest, keyed by tenant, user and provider. All three are part of the key. A key that omits the user means two people at Acme share one Slack identity. A key that omits the tenant is a cross-customer leak waiting for a collision.
Injection at call time. Tokens are decrypted only when a call is dispatched, attached to that one upstream request, and discarded. They are never returned to the agent, never placed in a model's context, never written to a span or log.
Refresh. Access tokens expire, often within an hour. Refresh should happen shortly before expiry, under a per-key lock, so that twenty concurrent calls for the same user trigger one refresh instead of twenty racing ones. Some providers rotate refresh tokens on every use, which makes a race not just wasteful but destructive: the loser of the race may invalidate the winner's token.
Revocation. Users revoke access from the provider's side without telling you. The first sign is usually an invalid_grant on refresh. The right response is to mark the connection as revoked and surface a reconnect prompt, not to keep retrying and fail every future call with an opaque error.
Rate limiting in an agent platform has two jobs that pull in different directions: protect each tenant from every other tenant, and protect every tenant from the upstream's limits.
That takes at least three buckets per call:
A token bucket per key is the usual primitive. Here is a minimal in-process version that shows the logic:
interface BucketSpec {
capacity: number; // max burst
refillPerSec: number; // sustained rate
}
interface BucketState {
tokens: number;
updatedAt: number;
}
export class TokenBuckets {
private
Two production caveats. First, an in-process map only works for a single gateway instance; with several instances, the buckets need to live in a shared store, typically Redis with an atomic script so that read, refill and decrement happen in one step. Second, the checks above consume from the tenant bucket even when a later bucket rejects the call. That is usually acceptable, but if it matters for your billing model, check all three before consuming from any.
The RateLimited error should carry the wait time back to the agent as a structured, retriable result. An agent that is told "retry after 1.2 seconds" can wait. An agent that is told "error" will often try a different, worse tool.

Days 22 and 23 covered state and reliability in depth. For the infrastructure layer, four rules carry most of the weight:
Because every call passes through the gateway, the gateway is the natural place to produce the most complete record of what the agent did. For each call, record:
Emit these as spans that share a trace with the agent runtime's model calls, so one run can be read end to end. OpenTelemetry has semantic conventions for generative AI spans that are useful here; check their current status before depending on specific attribute names, since that area of the conventions has been evolving.
Two things not to record by default: credentials, ever, and raw argument or result payloads in any store shared across tenants. Payloads are where the sensitive data is. Keep them in tenant-scoped storage or redact them before export.
Building is mostly done in bursts. Operating is a steady rhythm of small recurring work. For this layer, that rhythm looks like:
None of that is exotic. It is the ordinary work of running a platform. It is also work that never appears in an agent demo, which is why it catches teams by surprise.
The takeaway slide put this layer in a lineage: databases, authentication and payments each started as something every team built and became infrastructure most teams adopt. Agent tool access is following the same path. That does not mean you should adopt everything. It means you should decide component by component.
| Component | Typical call | Why |
|---|---|---|
| MCP servers for your own domain | Build | They encode your business logic and data model. |
| MCP servers for common third-party APIs | Adopt | Wrapping Gmail, Slack or GitHub well is undifferentiated, ongoing maintenance. |
| Tool access policy | Build the rules, adopt the mechanism | Who may do what is product logic. Enforcing it consistently is plumbing. |
| Authentication | Adopt | Use an identity provider. |
| Credential vault and OAuth lifecycle | Adopt, or build as a dedicated service | High blast radius and fiddly edge cases. If you build it, give it its own review. |
| Multi-tenancy rules | Build | Isolation is tied to your data model. |
| Gateway | Adopt or build thin | The pipeline is simple; the completeness guarantee (no bypass path) is what matters. |
| Rate limiting |
The pattern is consistent: own the rules, adopt the machinery. Your tenancy model, your tool access policy, your limits per plan and your domain tools are product. The vault, the OAuth dance, the gateway mechanics, the hosted integrations and the telemetry backend are infrastructure.
This is the category MewCP works in. It provides a hosted catalogue of MCP servers behind a single gateway endpoint, keeps upstream credentials encrypted in a vault and injects them only at call time so they never reach the agent or the model, refreshes OAuth tokens automatically, and exposes a small discovery-first tool surface (search, get schema, list accounts, call tool) so the agent's context does not grow with every app a user connects. It is one example of the category, not the only way to fill it, and it does not replace the parts of the table marked "build."
Access
Control
Evidence
Operations
The agent is your product. The layer underneath it is infrastructure you still have to account for, whether you build it, adopt it or, most commonly, some of each. What does not work is pretending it is not there, because the moment real users arrive it starts doing its job whether or not you designed it, and an undesigned version of this layer is just a collection of incidents waiting for a timestamp.
Next in the series: the first component on the access list, MCP servers themselves, and an honest look at when self-hosting them makes sense and when hosted MCP infrastructure does.
| Adopt the primitive, build the policy |
| Buckets are solved; limits per plan and per tool are yours. |
| Execution runtime | Adopt for long-running work | Durable workflow engines exist; write your timeout and retry policy, not the engine. |
| Observability backend | Adopt | Use OpenTelemetry-compatible tooling; own the attributes. |