Building MCPs

That Guide AI Coding Agents

terminal-arrows-control-layer

The Model Context Protocol, or MCP, is often described as a way for AI agents to connect with external systems. That is true, but in a serious agent workflow, the MCP can become much more than a connector. It can become the structure that teaches the agent how to work.

In this case, the MCP we built is used by AI coding agents that need to interact with our platform where databases are already connected and exposed as APIs. The agent is not mainly responsible for generating the APIs. Its job is to understand how to use those APIs correctly. It needs to know what resources exist, what fields are available, what filters can be composed, what relationships can be followed, what aggregations are supported, and what authentication or authorization rules apply.

This turns the MCP into a kind of harness. It gives the agent enough freedom to reason and solve the task, but it also makes it difficult for the agent to do the wrong thing.

MCP as a Harness

A normal API integration usually assumes that a human developer reads documentation, understands the schema, checks the authentication model, and writes code against the API. With AI coding agents, that same process has to be made much more explicit.

The agent cannot safely guess how an API works. It needs structured guidance. It needs to select the correct API, lock onto that target, inspect the schema, understand the available resources, and then plan the API calls. It must also understand which resources are protected, which fields can be queried, and what kinds of request and response shapes are valid.

The MCP can provide this guidance step by step. Instead of giving the agent a large block of documentation and hoping it uses it well, the MCP exposes tools and feedback at the right points in the workflow. The agent can fetch schema information, inspect a model or table, discover allowed actions, check field-level constraints, preview API calls, and receive validation feedback before executing something.

That is why the MCP acts like a harness. It does not remove the agent’s autonomy, but it channels that autonomy through a safe and understandable process.

layered-prompt-to-code-cards

Constrained Autonomy

One of the main ideas behind this approach is constrained autonomy.

The agent should still be able to reason. It should still be able to decide how to solve a task, compose queries, and integrate generated APIs into a codebase. But its freedom should exist inside clear boundaries.

Those boundaries are defined by the MCP. The MCP tells the agent what resources exist, what operations are valid, what parameters are accepted, and what permissions apply. The agent can make decisions inside that space, but it should not invent capabilities, call protected resources incorrectly, or use unsupported filters.

This is different from forcing the agent to follow a rigid script. A rigid script says, “Do exactly these steps every time.” A constrained autonomous system says, “Here are the allowed states, tools, transitions, and constraints. Work toward the goal within this environment.”

That difference matters. Agents are useful because they can adapt to the task in front of them. But they become risky when they adapt without enough structure. The MCP provides that structure.

State Management

State management is central to making this work.

An agent needs to know where it is in the workflow. Has it selected the target API? Has it locked onto that API? Has it fetched the overall schema? Has it inspected the specific resource it wants to query? Has it checked authorization rules? Has it planned the request? Has the request been validated?

Without state management, the agent can repeat steps, skip important checks, or continue based on stale assumptions. With state management, the MCP can guide the agent through the work in a controlled way.

State does not have to be heavy or complicated. It can be a simple record of what has already happened and what is required next. The important thing is that the agent receives feedback that is tied to the current state. If it tries to plan a query before selecting an API, the MCP should stop it and tell it what is missing. If it tries to query a protected resource without the right context, the MCP should surface that constraint early.

This makes the workflow easier for both capable and less capable models. The model does not have to hold the entire process in its working memory. The MCP carries part of that structure.

Context Capsules

A context capsule is a useful way to think about how information should be delivered to the agent.

Instead of giving the agent every piece of information at once, a context capsule gives it the specific information needed for the current step. If the agent is planning a filter, the capsule should include field types, allowed operators, value constraints, and relevant examples. If the agent is checking authorization, the capsule should include the permission rules that apply to the selected resource and operation. If the agent is composing an aggregation, the capsule should include supported aggregation functions and grouping rules.

The capsule is not just raw data. It is a small package of context, rules, and guidance for a specific part of the workflow.

This helps manage the agent’s cognitive load. Models have instruction and context limits. Even when the context window is large, too much irrelevant information can still make reasoning weaker. A good MCP does not simply throw more information at the agent. It gives the right information at the right time.

risograph-maze-checkpoints-mcp-route

Dynamic Context Switching

Dynamic context switching is the process of moving the agent from one context capsule to another as the workflow progresses.

When the agent is selecting an API, it needs broad information about available targets. Once it locks onto one API, it no longer needs the full list in the same level of detail. It now needs the schema for that target. When it chooses a resource, it needs the fields, relationships, filters, and permissions for that resource. When it prepares a request, it needs validation rules and request-shape expectations.

Each stage has a different context need. The MCP should help the agent load the current context and discard or de-prioritize information that is no longer relevant.

This is one of the key differences between documentation for humans and context for agents. Human documentation can be broad and reference-like. Agent context should be operational. It should be shaped around what the agent is trying to do right now.

Working With Complex Queries

The API use case becomes more interesting when the agent needs to build complex queries.

Simple reads are not enough. The agent may need to work with relationships, collections, nested filters, aggregations, grouping, sorting, pagination, and multi-step operations. It may need to understand how one resource connects to another, how to filter related records, or how to aggregate results across a collection.

This is where vague schema descriptions are not enough. The MCP should expose relationship metadata clearly. It should define which relationships can be traversed, which fields can be filtered, which operators are allowed for each field, and which aggregation functions are supported. If a relationship has special constraints, those constraints should be visible before the agent plans the query.

For example, if a field only supports equality and range filters, the agent should not attempt a text search against it. If an aggregation is not supported by the underlying data source, the agent should know that before it composes the request. If a nested relationship can only be included in certain directions, that should be part of the context.

The goal is to prevent invalid plans early, before they become failed API calls or broken integration code.

Authentication and Authorization

Authentication and authorization are not separate from the agent workflow. They are part of the context the agent needs in order to act safely.

The MCP should help the agent understand which resources are public, which are protected, which roles can access which operations, and which fields are restricted. This is especially important when the agent is generating code that will later run inside a real product.

If the agent sees a resource in the schema but does not understand that it is restricted, it may plan an invalid or unsafe call. If it knows the permission model, it can avoid that mistake and choose a valid path.

This can be handled in different ways. The MCP might hide resources that are not available in the current state. It might mark resources as protected. It might include role-based access control details in the context capsule. It might require the agent to validate a planned request before execution.

The right approach depends on the system, but the principle is the same: permission context should be available before the agent acts.

Human in the Loop

The human-in-the-loop role is not just about approving every action. It is about providing judgment when the agent lacks enough context to continue confidently.

If the goal is clear, the constraints are known, and the MCP provides all necessary context, the agent should be able to work autonomously for long stretches. It should not ask the human questions just to avoid doing the work.

But when the agent discovers a gap, the human becomes important. Maybe the original goal was ambiguous. Maybe a business rule was missing. Maybe the agent reaches a decision point that requires product judgment rather than technical inference. In those cases, the agent should pause and surface the question clearly.

This means the human is not always a manual checkpoint. Sometimes the human is a dynamic context provider. The human fills gaps that only become visible during execution.

Front-Loading Context

One way to reduce interruptions is to front-load context before the agent begins.

This can look like a detailed interview or grilling session where the human is asked about goals, constraints, edge cases, expected behavior, and priorities. The result is a more complete description of the task. In theory, this allows the agent to work more autonomously because it starts with fewer unknowns.

There is a trade-off. A large document full of instructions can increase the model’s cognitive load. But missing context can be even worse. The practical answer is not to avoid writing the context. The answer is to structure it so the agent consumes it through a workflow.

This connects back to the MCP design. The information can exist in full, but it does not all need to be active at once. The MCP can break it into states, capsules, tools, and validations.

Manual Iterative Refinement

It is tempting to call this reinforcement learning, but in this case that would not be accurate.

The model is not being trained. There is no reward function directly updating the model. The team does not own the model weights. What is actually happening is human-driven iterative refinement of the MCP.

The team observes how agents behave. They look at where the workflow fails, where the agent gets confused, and where the MCP did not provide enough structure. Then they manually improve the MCP components.

Sometimes the fix is an instruction change. Sometimes it is a schema improvement. Sometimes it is a better validation step, a clearer state transition, a more precise context capsule, or a stronger authorization signal.

This is closer to debugging and product refinement than model training. The MCP gets better because humans study agent behavior and adjust the environment the agent operates in.

systems-map-mcp-workflow

Examples of Useful Refinements

If an agent keeps trying to query a protected table, the MCP can make the protection status more explicit or hide that table unless the current state is authorized to use it.

If an agent uses the wrong filter operator for a field, the schema can include stricter field types, allowed operators, and examples of valid filter shapes.

If the agent chooses the wrong endpoint for a task, the MCP can add clearer endpoint descriptions, tags, or task-oriented guidance that separates similar operations.

If the agent tries to use an unsupported aggregation, the context capsule can list the exact aggregation functions available for that resource.

If the agent fails during a multi-step operation, the state management can expose a dependency graph or required sequence so the agent knows what has to happen first.

If the agent formats tool parameters incorrectly, the MCP can provide stronger property constraints, request previews, and validation feedback before execution.

If the agent hits unexpected permission errors, the MCP can include role-based access rules earlier in the planning stage.

These refinements are not random patches. They are ways of turning observed failures into better structure.

Toward an Agent-Native Interface

The larger idea behind all of this is that an MCP can become an agent-native interface.

A traditional API describes endpoints for developers. An agent-native interface describes how an agent should understand and operate inside a system. It includes resources, actions, constraints, states, transitions, permissions, validations, and feedback.

That is the shift. The MCP is not only saying, “Here is what the API can do.” It is saying, “Here is how to think about using this API correctly.”

This is especially important for coding agents. They are not only calling APIs directly. They are often integrating those APIs into existing codebases. That means they need reliable context about request shapes, response structures, authentication, error handling, and operational limits. A well-designed MCP helps them produce better integration code because it gives them the right mental model of the platform.

Conclusion

A strong MCP for AI coding agents is not just a tool registry or a schema dump. It is a structured environment for agent work.

It gives the agent state. It manages context. It exposes constraints. It provides previews and feedback. It helps the agent plan safely before acting. It gives humans a clear role when context is missing. And it improves over time through careful observation and manual refinement.

The central challenge is balance. The agent needs enough autonomy to be useful, but enough structure to stay correct. The MCP is where that balance can be designed.

When done well, the MCP becomes the layer that turns a general AI coding agent into a more reliable worker inside a specific platform. It does not make the model smarter by itself. It makes the environment clearer, safer, and easier for the model to reason through.