MCP Context Access Layer: Architecture Design and Engineering Practice

    This article explains why AI applications need a standardized context access layer, how the Model Context Protocol (MCP) separates hosts, clients, and servers, and how connections, capability contracts, context orchestration, security controls, version compatibility, and pre-launch checks should be designed.

    1. Why a Standardized Context Access Layer Is Needed

    Early implementations pasted database query results, file contents, and third-party API responses directly into prompts. The drawbacks are clear: integration logic is scattered, parameters have no shared contract, it is unclear who may read which data, and failures are hard to audit.

    The Model Context Protocol (MCP) defines how model applications interact with external data and tools. An application that connects to a server using the same message format can discover and invoke the capabilities that server exposes. The protocol covers both local resources—such as local files, databases, and internal services—and remote resources, such as collaboration platforms, code-hosting platforms, and third-party business APIs.

    MCP does not replace business systems. Those systems still own their data and rules. An MCP server only wraps the required capabilities in a form the model can understand.

    2. Roles: Host, Client, and Server

    • Host: The AI application the user interacts with directly, such as a desktop assistant or code editor. The host calls the model, decides which context enters the prompt, presents results to the user, and obtains authorization before a tool runs.
    • Client: A connection component inside the host that maintains a one-to-one connection with one server. If the host connects to three servers, it creates three clients.
    • Server: A program that exposes tools, resources, or prompts. It may run as a local process or be deployed as a remote service. The server executes requests, checks permissions, and returns results.

    This split means the host does not hard-code a server’s implementation details. It reads the capability list the server declares and calls capabilities from that list.

    3. Connection Lifecycle: From Initialization to Capability Calls

    A connection typically proceeds in this order:

    1. The client sends an initialize request with the protocol versions and capabilities it supports.
    2. The server returns the protocol version selected by both sides and its own capability declaration. If there is no commonly supported version, the connection cannot continue.
    3. The client sends a notifications/initialized notification, and the connection becomes ready.
    4. The client calls tools/list, resources/list, and prompts/list to obtain capability lists, then calls tools/call, resources/read, and prompts/get as needed.

    Capability declarations determine which features may be used. If the server does not declare resource capabilities, the client should not call resource interfaces.

    Protocol versions are named by date, for example 2025-06-18. The specific changes in each version should be checked against the official specification and its changelog, item by item, before an upgrade.

    There are two main transports. Local scenarios commonly use standard input and output (stdio). Remote scenarios commonly use Streamable HTTP. In a remote scenario, the server may return a session identifier (Mcp-Session-Id) in the response headers, and the client must include it in later requests. This is an optional transport-layer mechanism. If the server returns 404 for an expired session, the client should start initialization again.

    An example tool-call request:

    1. {
    2. "jsonrpc": "2.0",
    3. "id": 7,
    4. "method": "tools/call",
    5. "params": {
    6. "name": "query_order_status",
    7. "arguments": { "order_id": "A20261119-0031" }
    8. }
    9. }

    4. Contracts for the Three Capability Types

    Capability Purpose Call method Contract focus Main risks
    Tools Run an action or query state tools/call Name, input schema, output schema, annotations Wrong calls, side effects, repeated execution
    Resources Provide readable data resources/read URI, MIME type, pagination cursor, access scope Unauthorized reads, excessive data
    Prompts Reuse instruction templates prompts/get Name, parameters, message content Version drift, conflicting instructions

    Fields a tool contract should include:

    • name: Unique and stable within one server. A “verb plus object” form is recommended.
    • description: States when to use the tool and when not to. The model relies on this text to choose a tool.
    • inputSchema: A JSON Schema that declares required fields, types, enums, and value ranges.
    • outputSchema (optional): Declares a structured return value so the host can validate the result.
    • annotations: Examples include readOnlyHint, destructiveHint, and idempotentHint. These are hints, not a security basis. The server must still authorize independently.

    Resource contract points: URIs should be stable. Large objects should be returned with pagination cursors, and the format should be indicated by MIME type. Access control is decided on the server. Do not assume that a model cannot read data simply because it does not know the address.

    Error semantics should be layered. Unknown tools and invalid parameters are protocol errors and should be returned as JSON-RPC error objects. Business execution failures should set isError: true in the result and include an error description. The model can read that error and adjust parameters. The two error types should be counted separately in logs; otherwise it is hard to tell whether the problem is on the model side or the service side.

    Timeouts and retries should follow each tool’s side effects. Read-only tools may be retried. A write tool may be retried only when it has an idempotency key; otherwise the action may run more than once.

    5. Context Orchestration on the Host

    The access layer fetches, validates, and normalizes data. The orchestration layer decides which results enter the prompt. Keeping the two layers separate means the access layer does not need to change when the model or the context strategy changes.

    When aggregating multiple servers, the host typically processes results as follows:

    1. candidates = combine content returned by each server
    2. candidates = deduplicate by resource URI
    3. candidates = sort by source priority and relevance
    4. for result in candidates:
    5. if accumulated token count + result token count > context budget:
    6. truncate or summarize, and record the reason
    7. if result is marked as an error or the call timed out:
    8. use a fallback source, or tell the model the data is unavailable
    9. add it to the prompt, and record the source server and protocol version

    The host should not place a server’s raw response directly in the prompt. Raw data may contain many irrelevant fields and may also contain sensitive information.

    6. Security and Access Control

    • User authorization: Before a tool runs, the host should show the user the tool name and parameters. Write operations require explicit user confirmation.
    • Server authorization: A remote HTTP server should use an authorization mechanism defined by the specification. An OAuth-based authorization framework is one example. The server must check the caller’s identity and authorization scope.
    • Least exposure: The server should declare only the tools and resources the business requires.
    • Credential isolation: In a multi-tenant setup, each tenant should use separate credentials and, when necessary, a separate server instance.
    • Write protection: Configure quotas, approval flows, and rollback plans. Operations that delete data or move funds should add a second confirmation.
    • Audit fields: Each call should record the caller, tool name, resource URI, latency, and result status. Sensitive parameters must be redacted before they are written to logs.

    If a remote deployment needs to scale horizontally, the server should avoid depending on session state held in process memory. Required information should live in request parameters or external storage, and duplicate requests should be protected against replay.

    7. Version Evolution and Compatibility

    • Protocol versions are negotiated during initialize. Both sides use only a version they both support.
    • Maintain a compatibility matrix that records host, client SDK, and server versions, plus the protocol versions each has verified.
    • A practical upgrade order is: upgrade the server first so it supports both old and new versions, then upgrade the host. After both are done, retire the old version gradually.
    • Extension capabilities are enabled according to capability declarations. If the host finds that a server does not support an extension, it should fall back to the base feature.

    8. Pre-Launch Checklist

    • Every tool has a clear description, input schema, and error semantics.
    • Write tools have user confirmation, an idempotency key, and an audit log.
    • Resource reads are authorized by URI and have a maximum response size.
    • Timeouts and retries are configured, and retries are used only for idempotent tools.