Agent Architecture, MCP, Tools, Context, and Memory
Define the complete, versioned runtime surrounding a model driven engineering worker.
A rapid review of the chapter’s existing Quick Read, principles, definitions, lessons, and review material.
The chapter in one pass.
- Purpose: Define the complete, versioned runtime surrounding a model-driven engineering worker.
- Best for: AI engineers, architects, platform teams, security reviewers, and technical leaders.
- Prerequisites: Control Plane and Execution Plane.
- Reading time: 18 minutes.
- You will learn: How identity, instructions, model profiles, tools, MCP, context, memory, policy, budgets, state, evidence, and evaluation form one governed agent configuration.
Keep five ideas:
- An agent is a runtime composition, not a model with a long prompt.
- The model may propose an action; only the runtime may authorize it.
- MCP standardizes interoperable sessions and capabilities; it does not grant trust, authority, or acceptance.
- Context and memory are governed inputs with provenance and lifecycle, not undifferentiated text.
- Reproducibility requires freezing the complete execution manifest and retaining the resulting trace and evidence.
Protocol version note: Normative MCP statements in this chapter are pinned
to the stable 2025-11-25 specification. Experimental features are labeled.
Draft behavior may change and should not silently redefine a production
contract.
Reconstruct and defend this chapter’s architecture.
Reconstruct the architecture, name each boundary, and defend the tradeoffs.
Jump to validation criteriaOpen the source exercise
Draw a host containing an agent runtime, one MCP client per connected server, a context compiler, memory view, model profile, policy gateway, durable state, tool adapters, and an independent evidence path.
Add a malicious repository document that asks the agent to read a synthetic secret and send it to an unapproved remote service. Show:
- where the content enters as untrusted reference context;
- which identity, scope, egress, and approval checks deny the action;
- how the denial and attempted call are recorded;
- how the agent continues or escalates safely; and
- which evidence a reviewer receives.
3. Enduring Principle
An agent is a governed runtime composition
An engineering agent is a versioned composition of:
identity + objective + instructions + model profile + tools + context + memory view + policy + budgets + state + evaluation profile
| Component | Responsibility | What must be frozen or recorded |
|---|---|---|
| Identity | Names the agent role and acting principal | Agent version, tenant, user or service principal |
| Objective | States the bounded outcome | WorkOrder and approved Plan references |
| Instructions | Defines trusted operating behavior | System instructions, skills, workflow versions |
| Model profile | Selects reasoning capability | Provider, model, parameters, routing policy |
| Tool grants | Defines possible actions | Tool names, versions, scopes, schema hashes |
| Context | Supplies decision-relevant knowledge | Source revisions, selection reasons, content hashes |
| Memory view | Supplies governed prior knowledge | Snapshot, query, scope, provenance, lifecycle filters |
| Policy | Limits authority | Policy bundle, risk class, required approvals |
| Budgets | Limits resource use | Time, tokens, cost, attempts, concurrency |
| State | Makes execution durable | Task, Attempt, lease, checkpoints, cancellation state |
| Evaluation profile | Defines expected behavior | Dataset, graders, thresholds, evidence requirements |
Changing any material component changes the worker configuration and may invalidate evidence from an earlier Attempt. A model name alone is never a sufficient reproducibility record.
Separate reasoning from authority
The model can interpret intent, form hypotheses, choose among allowed options, and propose a tool call. It cannot expand its own scope. Before execution, the runtime validates the acting identity, input schema, policy, repository and path scope, risk, budget, approval state, idempotency strategy, and environment.
The result returns as an observation, never as trusted instructions. The runtime records both approved and denied calls so a reviewer can reconstruct what the agent attempted, what actually ran, and why.
Freeze an execution manifest
Every Attempt should resolve mutable configuration into one immutable manifest before work begins. A minimal manifest contains:
attempt_id: attempt-123
work_order_revision: sha256:...
plan_revision: sha256:...
agent_version: agent-implementation-v4
model_profile: code-reasoning-standard-v2
instruction_bundle: sha256:...
tool_grants:
- name: repository.read_file
version: 3.2.0
schema_hash: sha256:...
scope: read
context_lock: sha256:...
memory_snapshot: sha256:...
policy_bundle: engineering-medium-risk-v5
budgets:
wall_clock_seconds: 1800
attempts: 3
evaluation_profile: repository-change-v7
The manifest is a contract, not a log assembled after execution. Runtime events and evidence must point back to it.
MCP is an interoperability boundary
The Model Context Protocol uses a host–client–server architecture over JSON-RPC. The host coordinates model access, consent, security policy, and context. It creates a client for each server connection. The client negotiates protocol version and capabilities, maintains the stateful session, and routes messages. The server exposes focused capabilities.
The stable specification defines stdio and Streamable HTTP transports. A
production contract must pin the protocol version, transport, server identity,
and negotiated capabilities; “supports MCP” is too vague to be meaningful.
MCP primitives have different control expectations:
| Primitive | Primary purpose | Typical control model | Factory treatment |
|---|---|---|---|
| Tools | Perform computation or side effects | Model-controlled, with host/runtime approval | Apply schema, scope, risk, budget, and evidence policy |
| Resources | Expose addressable data or content | Application-controlled | Treat content and annotations as untrusted; retain URI, revision, and provenance |
| Prompts | Expose reusable message templates | User-controlled | Treat server-supplied instructions as versioned content, not platform authority |
| Sampling | Let a server request model generation through the client | Client-controlled with user oversight | Constrain model access, tool loops, context, cost, and approvals |
| Elicitation | Let a server request additional user input | User-controlled | Make the requesting server visible; protect sensitive-data boundaries |
| Tasks | Represent deferred, durable request execution | Negotiated; experimental in 2025-11-25 | Bind task state to authorization context; set TTL, cancellation, polling, and audit rules |
Capability negotiation proves that both sides speak a compatible protocol. It does not prove that a server is trustworthy, that a tool is safe, or that the current WorkOrder authorizes its use.
Govern the MCP connection, not only the tool call
An enterprise MCP gateway should require a connection contract with:
| Control | Required decision |
|---|---|
| Server identity | Which organization, package, binary, endpoint, and version are trusted? |
| Transport | Is the connection local stdio or remote Streamable HTTP, and which network boundary does it cross? |
| Authentication | Which principal is connecting and how is it verified? |
| Authorization | Which server resource is the token intended for, which scopes apply, and can scope increase require new consent? |
| Capability allowlist | Which tools, resources, prompts, sampling, elicitation, or task features may be negotiated? |
| Data policy | What may leave the repository or tenant, and what must be redacted? |
| Invocation policy | Which calls are read-only, reversible, consequential, or prohibited? |
| Operational bounds | What timeout, concurrency, rate, output-size, retry, and cancellation limits apply? |
| Evidence | Which request, response, approval, denial, and artifact receipts must be retained? |
| Revocation | How can a server, capability, credential, or version be disabled immediately? |
For HTTP authorization, tokens must be audience-bound to the intended MCP server. Token passthrough to unrelated downstream services collapses trust boundaries and must not be treated as a shortcut.
Each MCP server is simultaneously a software supply-chain dependency, an identity boundary, and a possible data-egress path.
Tools are behavioral contracts
A tool is not safe because its arguments satisfy JSON Schema. Its contract must also define behavior under success, failure, retry, cancellation, and partial completion.
| Contract field | Why it matters |
|---|---|
| Input and output schema | Makes validation and downstream interpretation explicit |
| Acting identity | Attributes the action to the correct user, service, and agent |
| Scope | Limits repositories, paths, records, operations, and environments |
| Side-effect class | Separates observation from reversible and consequential mutation |
| Idempotency | Prevents retries from duplicating commits, messages, deployments, or records |
| Timeout and cancellation | Bounds abandoned or long-running work |
| Retry policy | Distinguishes safe transient recovery from repeated harmful action |
| Result envelope | Separates structured data, human-readable explanation, and error state |
| Evidence receipt | Ties the call to inputs, outputs, artifacts, and policy decisions |
| Version and schema hash | Makes behavioral changes detectable and evaluable |
Protocol errors and tool-execution errors should remain distinct. A malformed request is different from a valid request whose business operation failed. Structured failures help a model correct an input without hiding operational or policy failure.
Context engineering is controlled compilation
Context compilation selects the smallest sufficient set of trusted directives and relevant observations for a decision. It is not a bulk copy of everything the system can retrieve.
The compiler should process inputs in this order:
- Load authoritative intent, Plan, policy, identity, budgets, and exact source state.
- Resolve domain terms to canonical concepts and identifiers.
- Select applicable instructions, skills, and workflow contracts.
- Retrieve candidate repository facts, documentation, history, and memory within tenant, repository, sensitivity, and time boundaries.
- Rank candidates by authority, relevance, freshness, diversity, and risk of omission.
- Detect duplicates, conflicts, staleness, and unresolved terminology.
- Allocate the context budget, preserving governing constraints before optional examples or history.
- Emit an ordered context package with source, revision, hash, selection reason, trust class, and truncation record.
Good context architecture keeps five categories visibly separate:
- instructions: trusted runtime directives;
- authoritative context: approved contracts, policy, identity, and source state;
- reference context: documentation and retrieved knowledge;
- working context: transient hypotheses and scratch state; and
- evidence: observations tied to exact actions and artifacts.
More context is not automatically better. Irrelevant content consumes tokens, increases conflicting cues, and can hide the governing constraint.
Memory is governed, typed, and revisable
Memory should improve future decisions without becoming a shadow system of record.
| Memory type | Useful for | Main risk | Required control |
|---|---|---|---|
| Session | Current Attempt state and scratch work | Treating a hypothesis as fact | Attempt scope and automatic expiry |
| Episodic | What happened during prior runs | Copying a past solution into a different case | Artifact links, outcome, time, and similarity evidence |
| Semantic | Claims, entities, terminology, and relationships | Stale or contradictory knowledge | Provenance, confidence, validity interval, contradiction links |
| Procedural | Skills, prompts, workflows, and runbooks | Promoting an unsafe behavior | Evaluation, ownership, approval, versioning, rollback |
A safe lifecycle is:
observe → quarantine → classify → evaluate → approve → publish → retrieve → correct, expire, or revoke
Memory writes should retain source, scope, time, confidence, sensitivity, owner, lifecycle state, and the evidence supporting promotion. Retrieval must filter by the current identity and authority, return citations and “why retrieved,” and expose contradictions rather than silently choosing a winner.
Deletion, correction, expiry, tenant isolation, and permission changes are first-class lifecycle events. Operational records remain authoritative; memory may reference them but must not rewrite them.
Use multiple agents only for a measurable reason
Add another agent when independent verification, parallelism, context isolation, or specialized expertise produces a measurable gain. Do not create agents merely to imitate an organization chart.
Every handoff needs an explicit input contract, output schema, authority, budget, termination condition, and owner. Shared durable state belongs in the runtime, not in private message history. Independent validation requires a separate evidence path, different incentives or tools where appropriate, and protection against correlated failure—not a different persona name.
Evaluate the complete agent configuration and workflow. A model benchmark alone cannot show whether context selection, tool policy, memory, recovery, or multi-agent coordination works.
8. Notes and lessons learned
- Context locks make invisible behavioral inputs versioned and attributable.
- Schema validation is necessary but cannot express the complete behavioral or authority contract of a tool.
- Protocol capability and organizational permission are separate decisions.
- Memory quality depends more on write admission and correction than on vector search quality.
- Traceability improves only when records share stable Attempt, manifest, artifact, and policy identifiers.
- A small governed agent is usually safer and easier to improve than a large agent whose tools, memory, and context continually expand.
9. Interview and discussion questions
- What makes an agent different from a model invocation or provider-managed conversation?
- Which inputs must an execution manifest freeze, and which runtime events must refer back to it?
- What does MCP standardize, and which governance decisions remain with the host and factory runtime?
- How do tools, resources, prompts, sampling, elicitation, and tasks differ?
- Why are JSON Schema validation and capability negotiation insufficient authorization controls?
- How do you prevent retrieved text, tool descriptions, and tool results from becoming authority?
- What makes a retry safe for a consequential tool?
- How should memory contradictions, correction, expiry, and revocation work?
- When is multi-agent orchestration justified?
- Which evidence would prove that one complete agent configuration is safer or more effective than another?
Review this chapter.
Challenge a claim, boundary, missing failure mode, unclear term, or unsupported evidence statement.
- Claim
- Boundary
- Failure
- Evidence