JA
header image

枝折 - bookmark

How MCP Works and How to Build It

Generative AI

CREATED: 2026 / 06 / 23 Tue

UPDATED: 2026 / 06 / 23 Tue

How MCP Works and How to Build It

RAG and Function Calling

Augmenting Responses with RAG

RAG (Retrieval Augmented Generation) is a mechanism for supplementing LLM responses with external data.

In traditional RAG setups, an application server receives a request from the user, passes it to the LLM, and pulls in external information as needed to guide the LLM toward a final answer.

Here’s a rough diagram of that architecture 👇

flowchart LR
    A[User]-- request -->B[Server]
    B[Server]-- inference request -->C[LLM]
    C[LLM]-- inference response -->B[Server]
    B[Server]-- fetch external info -->D[External Info (Knowledge Base)]
    D[External Info (Knowledge Base)]-- provide info -->B[Server]

For narrow, well-defined features this works fine, but to produce rich, accurate answers from varied user requests, the server had to make fine-grained decisions about which external information to fetch and when.

You might think “just let the LLM fetch the information itself” — but the non-deterministic nature of LLMs makes it hard to guarantee stable behavior, and calling external APIs is generally better handled by idempotent application code.

Anyway, because everyone implemented this differently, there was no standard pattern and the code tended to be hard to reuse.

This is the backdrop against which Function Calling emerged. Even before it arrived, people were already building systems where the LLM was shown a catalog of available data sources and asked to request the ones it needed — which is probably exactly why Function Calling was created. Those hand-rolled systems, however, struggled to achieve consistent stability.

Stabilizing Tool Selection with Function Calling

Function Calling is the mechanism of embedding a tool catalog (a list of callable functions) directly into the prompt so the LLM can choose which external sources it needs and request them on its own — all within the RAG flow.

As mentioned above, you could build this yourself, but ChatGPT and Claude implement it at the model level, which makes it significantly more reliable. Today it forms the foundation that MCP is built on.

When Function Calling is used, the prompt sent from the server to the LLM includes a catalog of available tools (diagram repeated 👇).

flowchart LR
    A[User]-- request -->B[Server]
    B[Server]-- inference request -->C[LLM]
    C[LLM]-- inference response -->B[Server]
    B[Server]-- fetch external info -->D[External Info (Knowledge Base)]
    D[External Info (Knowledge Base)]-- provide info -->B[Server]

The LLM can then return a response like this (simplified example) 🗒️

{
	"response_type": "function_call",
	"function_name": "request_api",
	"argument": {
		"words": ["how to build a breakout game with AI"]
	}
}

Upon receiving this, the application server recognizes that a Function Calling response has been returned requesting request_api, and calls the specified function to hit the configured API. The LLM is not involved in that step — it’s handled as idempotent application logic.

Function Calling is convenient, but embedding the entire tool catalog in the prompt consumes a significant portion of the context window. This is one of the well-known trade-offs of MCP as well 👀

There’s another friction point: the server is still responsible for implementing the actual call to each selected tool. The more external APIs you support, the messier this gets — each one has its own interface, and wiring them up individually is genuinely tedious.

MCP is the standard that addresses exactly this problem by unifying how those external tool calls are made. If each external data provider publishes an MCP server following the spec, users can access it through an MCP client (like Claude Code) without worrying about the details.

The Arrival of MCP

Function Calling made tool invocation much easier, but the next challenge was standardizing how those tools get called. MCP is the answer to that challenge.

MCP consists of an MCP server and an MCP client.

You could build your own MCP client, but realistically you’d just use something like Claude Code.

The MCP server receives requests from the MCP client and is published by whoever provides the external data.

Here’s the updated diagram 👇

flowchart LR
    A[User]-- request -->B[Server]
    B[Server]-- inference request -->C[LLM]
    C[LLM]-- inference response -->B[Server]
    B[Server]-- if tool call needed -->E[MCP Client]
    E[MCP Client]-- request external info -->F[MCP Server]
    F[MCP Server]-- return info -->E[MCP Client]

The application server first asks the MCP server (via the MCP client) for its tool catalog and embeds it in the prompt sent to the LLM. If the inference response includes a tool call, the server invokes the corresponding MCP server tool through the MCP client.

Rather than writing custom integration code for every external source, you just call MCP server tools through the MCP client. This dramatically improves the reusability of external data tools and simplifies implementation 💪

MCP is a standard that defines how clients and servers interact. When external data providers build MCP servers that follow this spec, anyone can use an MCP client to access them in a uniform way.

MCP Client / Server Connections

MCP client-server communication uses stdio (standard input/output), SSE (Server-Sent Events), or Streamable HTTP. Generally, stdio is used for local servers and SSE or Streamable HTTP for remote servers.

SSE is not widely recommended these days, so Streamable HTTP is the better choice for remote setups.

Model Context Protocol | Streamable HTTP

Data is exchanged via JSON-RPC — a JSON-based Remote Procedure Call format that engineers should find familiar.

The high-level interaction between client and server looks like this 👇

sequenceDiagram
    participant client as MCP Client
    participant server as MCP Server
    client->>server: 1. Connection request
    server->>client: 2. Connection established
    client->>server: 3. Request tool catalog
    server->>client: 4. Return tool catalog
    client->>server: 5. Invoke tool
    server->>client: 6. Return tool result
    client->>server: 7. Disconnect request
    server->>client: 8. Disconnection confirmed

Designing an MCP Server

When implementing an MCP server, you need to think carefully about which external data sources to reference, how much data to fetch, and what to return to the client.

For example: should you return raw weather forecast data, or should you also include information about tourist spots and activities suited to the conditions based on that forecast?

The same architectural principles that apply to systems in general apply here — aim for simplicity and high availability. If your components are tightly coupled and hard to extend, adding support for new tools later becomes a real headache 👀

The following book gives a solid overview of MCP server design, infrastructure considerations, and testing 👇 MCP Server Development: From Custom Server Implementation to Automated Testing https://amzn.asia/d/097Dyi6Y

References

This article focused on how MCP works. I plan to write about authorization and authentication in MCP separately 📝