How MCP Works: A Concrete Example Explained

    This article walks through the Model Context Protocol (MCP) using a concrete example, explaining why the protocol exists, its three roles (host, client, and server), and the sequence of a typical request. It covers configuration, gateway URLs, token handling, structured responses, capability discovery, transport and deployment options, common failures, and security practices, so you can understand how MCP gives models reusable and governable access to external data.

    How MCP Works: A Concrete Example Explained

    Where the Problem Comes From: Models Lack Hands and Eyes, Not Intelligence

    An LLM is essentially a function running inside a stateless inference container: it takes a context as input and outputs a sequence of tokens. It does not know today’s date, what the code in your private repository looks like, or how to issue a network request on its own. Early on, every vendor defined its own “function calling” convention: tool description formats differed, return structures differed, authentication methods differed, and error codes differed. As a result, the same data capability had to be adapted once for every model integration, and the integration cost grew multiplicatively with “number of models × number of data sources.”

    The Model Context Protocol (MCP) addresses exactly this multiplication problem: it decouples the “model side” from the “data side” and defines a standard channel in between that both sides follow. As long as a data source exposes capabilities according to the protocol and the model side consumes them according to the protocol, the N×M adaptation collapses into N+M.

    Three-Layer Architecture: Who a Request Passes Through

    The most effective way to understand MCP is to break it into three roles and then follow a single request through them.

    • Host: The AI client that actually faces the user. It could be a desktop assistant, an AI coding tool, or an AI panel inside an IDE. It has the LLM built in and is responsible for orchestrating the conversation and deciding when to invoke external capabilities.
    • Client: The protocol implementation inside the host. It reads configuration files, maintains a session with each server, injects the tool list into the model context, and translates the model’s invocation intent into protocol messages.
    • Server: The capability encapsulation layer. It is not the business system you want to access; instead, it packages actions such as crawling web pages, calling open APIs, and querying databases into standardized tools and resources, and it is responsible for carrying credentials and structuring results.

    A typical call proceeds like this: the user asks a question → at startup, the host handshakes with all servers according to the configuration and pulls their capability lists → the model determines that external data is needed and outputs a structured invocation request → the client sends the request to the corresponding server → the server accesses the target system with a token → the target system returns raw data → the server trims and structures it before sending it back → the model continues generating an answer based on the structured result.

    The point most often misunderstood here is this: what the model sees is never the raw web page, but the structured result processed by the server. How well the server is designed directly determines how accurate the model’s answer is.

    Walking Through an Example: Reading Private Data

    Suppose the scenario is “have the AI assistant read the issue list from a private repository on a code hosting platform and summarize it.” Note that this only requires a generic open API capability and does not involve any specific product.

    Step one, write the configuration. The configuration file read by the host declares a server entry:

    1. {
    2. "mcpServers": {
    3. "repo-issues": {
    4. "url": "https://mcp-gateway.example-internal",
    5. "env": {
    6. "ACCESS_TOKEN": "<your personal access token>"
    7. }
    8. }
    9. }
    10. }

    Step two, understand what the url field really means. It is not the address of the business system, but a capability gateway address: the entry point of a remote server that has already implemented the protocol and encapsulated the API call logic. The client establishes a long-lived connection with it over HTTP or WebSocket, and all subsequent requests go through this channel. Filling in the business address here is a common mistake—the business system does not speak the protocol, so the handshake will inevitably fail.

    Step three, understand the token in env. Most APIs are not publicly readable, and when the server actually issues the request, it puts the token into the request header:

    1. GET /repos/{owner}/{repo}/issues?state=open
    2. Authorization: Bearer <ACCESS_TOKEN>
    3. Accept: application/json

    Only after the target system validates it does it return data; anonymous requests are rejected. The token is injected by the client and held by the server, and the model never sees the plaintext throughout the process. This is a key security design of this architecture.

    Step four, look at what the server sends back. The raw response has verbose fields, so the server usually trims it into a structure that is easy for the model to read:

    1. {
    2. "count": 2,
    3. "issues": [
    4. { "id": 101, "title": "Login timeout", "state": "open", "labels": ["bug"] },
    5. { "id": 102, "title": "Add documentation", "state": "open", "labels": ["docs"] }
    6. ]
    7. }

    With this result, the model can generate a grounded answer such as “there are currently two open issues, one bug and one documentation item.”

    How Capabilities Become “Visible” to the Model

    After the server starts, the client pulls a capability list that typically contains three elements: the tool name, a description of its purpose, and the input schema (usually described with JSON Schema). This list is injected into the context, and the model uses it to decide whether to call, which one to call, and what parameters to pass.

    This leads to a practical conclusion: vague descriptions will cause the model to pick the wrong tool or pass the wrong parameters. A good description should clearly state “what it can query, what it cannot query, what the parameters mean, and what it returns,” and use enums to constrain value ranges when necessary. This is far more effective than repeatedly tuning the model.

    Transport and Deployment Forms

    • Local process form: The server runs as a child process and communicates over standard input and output. It suits accessing local files and local databases, and credentials never leave the machine.
    • Remote channel form: The server is deployed independently and accessed over HTTP or WebSocket. It suits team sharing and centralized auditing. For the remote form, it is recommended to use a message queue or caching layer to smooth out peaks and avoid high-frequency model calls overwhelming the backend.

    Common Failures and Troubleshooting Order

    1. Handshake failure: First confirm the address is a capability gateway rather than a business system, then confirm the protocol and port match.
    2. 401/403: The token is missing, expired, or has insufficient permission scope; check whether the header is correctly injected.
    3. 429: The call rate exceeds the limit, requiring backoff retries and result caching.
    4. The model does not call the tool: The capability description is unclear, or there are too many tools causing selection difficulty; split the servers and narrow down the list.
    5. The returned content is too long and crowds out the context: Have the server do pagination and field trimming, returning only the necessary information.

    Security Boundaries and Engineering Recommendations

    Tokens should be injected via environment variables or a key management service—never written in plaintext in configuration files, committed to version control, or written to logs. Servers should follow least privilege and prefer read-only. All external returned content should be treated as untrusted input to guard against prompt injection. In production, it is recommended to consolidate access through a unified gateway, and to use logging services and monitoring alerts for call auditing and anomaly alerting.

    If you need to build your own capability layer, you can deploy servers on a container platform, store large results in object storage, and build observability with logging and monitoring systems. For example, the general cloud infrastructure categories offered by Baidu AI Cloud can host this kind of standardized capability gateway.

    Summary in One Sentence

    The value of MCP lies not in “getting the model online,” but in abstracting external capabilities into protocol-based standard interfaces: the host handles orchestration, the client handles the protocol, and the server handles authentication and structuring. The address field points to the capability entry, and the token field carries the identity credential. With each playing its own role, the LLM gains reusable, governable, and auditable “hands and feet” for the first time.