How MCP Works Under The Covers

Hello Coders! 👾

At work I’m currently implementing MCP, the Model Context Protocol. I’ve used MCP servers for a while as a developer, so I thought I knew it pretty well. It turned out I only knew my own side of it.

What actually happens inside the AI client? How does it find the tools, and how does the model decide to call one? And then there was OAuth. I didn’t know MCP needs OAuth 2.1 specifically, or what that means for my server.

So in this post I want to look under the covers. Everything here is based on the 2026-07-28 version of the MCP specification . That version changed quite a lot, so older tutorials might show you things that are gone now.

The big picture

MCP gives AI applications one standard way to talk to tools and data. Before MCP, every AI app had its own plugin format. Now you build a server once and every client that speaks MCP can use it.

There are three roles:

  • Host: the application the user works in, like an IDE, a chat app, or a coding agent.
  • Client: a component inside the host. It keeps one connection to one server.
  • Server: the program that offers tools, resources, and prompts.

A host usually runs several clients, one per server. The model itself is not part of MCP at all. The host sends the conversation and the tool definitions to the model, and that is where MCP stops.

  flowchart LR
    User((User)) --> Host
    subgraph Host[Host application]
        C1[MCP client A]
        C2[MCP client B]
    end
    Host <-- "conversation + tool definitions" --> Model[LLM]
    C1 <-- stdio --> S1[Local MCP server]
    C2 <-- Streamable HTTP --> S2[Remote MCP server]

Two ways to connect

MCP has two standard transports.

With stdio, the host starts the server as a child process. They send JSON-RPC messages over stdin and stdout, one message per line. The server can write logs to stderr, but only MCP messages may go to stdout. So one stray console.log can break the connection.

With Streamable HTTP, the server has one endpoint, for example https://example.com/mcp. The client sends every message as an HTTP POST. The server answers with plain JSON or with a stream of server-sent events.

Most of the interesting stuff, like OAuth, only applies to HTTP.

A stateless protocol

This is the biggest change in the 2026-07-28 spec. MCP is now stateless. Every request stands on its own, and the server doesn’t need to remember anything about the client between requests.

Older versions started with an initialize request. The client and server exchanged versions and capabilities, and on HTTP the server gave back a session ID. Every next request had to carry that Mcp-Session-Id header. That handshake is gone, and so is the session ID.

Instead, every request now carries the protocol version and the client capabilities in its _meta field. So any server instance can handle any request.

Why this matters

With sessions, a remote MCP server was hard to scale. The session lived in the memory of one server instance. So you needed sticky sessions on the load balancer, or a shared store for the session state. Serverless hosting was also tricky, because a function can disappear between two requests.

Now you can put a few instances behind a load balancer and it just works:

  flowchart LR
    C[MCP client] --> LB[Load balancer]
    LB --> I1[Server instance 1]
    LB --> I2[Server instance 2]
    LB --> I3[Server instance 3]

What else went away

A few things depended on that session, so they changed too:

  • The GET stream is gone. Before, a client could keep a stream open to get messages from the server. For updates, like a changed tool list, the client now opens a subscriptions/listen request.
  • The server can’t send its own requests anymore. Before, a server could ask the client for input in the middle of a tool call. Now the server returns a result with resultType: "input_required". The client gets the input from the user and sends the call again, with the answers.
  • State is up to you. If a tool needs to remember something, like an open document or a game, it returns a handle. The model passes that handle back as an argument in the next call.

Discovery and headers

Every server must implement server/discover. It tells the client which protocol versions, capabilities, and identity the server has.

On HTTP, a few values from the body are also copied into headers. That way a load balancer or gateway can route a request without parsing the JSON:

  • MCP-Protocol-Version on every POST
  • Mcp-Method on every request, for example tools/call
  • Mcp-Name on tools/call, resources/read, and prompts/get

If a header and the body don’t match, the server must reject the request with a 400. A nice side effect is that the traffic is much easier to read in the network logs.

Getting in with OAuth 2.1

I knew MCP servers on HTTP use OAuth. What I didn’t know is that MCP asks for OAuth 2.1 specifically. The authorization server must implement it, and the MCP server and client play the OAuth 2.1 roles. OAuth 2.1 is still a draft, but it bundles years of security best practices. For example, the authorization code flow uses PKCE.

The tricky part is that the client has never seen your server before. So it has to discover everything by itself:

  sequenceDiagram
    participant C as MCP client
    participant S as MCP server
    participant A as Authorization server
    C->>S: POST /mcp (no token)
    S-->>C: 401 + where to find the metadata
    C->>S: GET protected resource metadata
    S-->>C: Which authorization server to use
    C->>A: Log in the user (PKCE, resource)
    A-->>C: Access token
    C->>S: POST /mcp + Authorization: Bearer token

The MCP server only checks tokens. It doesn’t hand them out. For that it points the client to an authorization server, with Protected Resource Metadata (RFC 9728) .

For the MCP server I’m building, these were the important requirements:

  • Publish the protected resource metadata, and point to it in the WWW-Authenticate header of a 401.
  • Check that each token was issued for this server. The client sends the server’s URL in the resource parameter, so the token has the right audience.
  • Never forward the token to another API. If the server calls other APIs, it needs its own tokens.
  • Return a 403 with insufficient_scope when a token is missing a scope, so the client can ask for more.

The client side has more work, like getting a client ID and checking the issuer. The authorization part of the spec has all the details.

How the client knows the tools

Once connected, the client calls tools/list. The server returns every tool with a name, an optional title and description, and a JSON Schema for the input:

{
  "name": "get_high_scores",
  "title": "Get high scores",
  "description": "Returns the top scores for one of my games. Use this when the user asks about scores or rankings.",
  "inputSchema": {
    "type": "object",
    "properties": {
      "game": { "type": "string", "description": "Slug of the game, for example rainbow-reboot" },
      "limit": { "type": "integer", "minimum": 1, "maximum": 50 }
    },
    "required": ["game"]
  }
}

The result also has ttlMs and cacheScope, so the client knows how long it can cache the list. The list can depend on the token. A user with fewer permissions can see fewer tools.

When the tools change, the server can send notifications/tools/list_changed. The client has to subscribe first with a subscriptions/listen request. Then it calls tools/list again.

Tools aren’t the only thing a server can offer. Resources are data the host can read, like files or records. Prompts are templates the user can pick. Tools get the most attention, because they are what the model uses.

How the model picks a tool

This is the part that cleared up a lot for me. The model doesn’t run anything. It only produces text, and some of that text is a structured tool call.

The host puts the tool definitions into the request to the model, next to the conversation. When the model thinks a tool helps, it returns the tool name and the arguments as JSON. The host then does the real work:

  sequenceDiagram
    actor U as User
    participant H as Host + MCP client
    participant M as LLM
    participant S as MCP server
    U->>H: "What are the top 3 scores in Rainbow Reboot?"
    H->>M: Conversation + tool definitions
    M-->>H: Tool call: get_high_scores(game, limit)
    H->>U: Ask permission (if needed)
    H->>S: tools/call
    S-->>H: Result content
    H->>M: Conversation + tool result
    M-->>H: Final answer
    H->>U: "The top 3 scores are..."

The tools/call request over HTTP looks like this:

POST /mcp HTTP/1.1
Content-Type: application/json
Accept: application/json, text/event-stream
Authorization: Bearer eyJhbGciOi...
MCP-Protocol-Version: 2026-07-28
Mcp-Method: tools/call
Mcp-Name: get_high_scores

{
  "jsonrpc": "2.0",
  "id": 7,
  "method": "tools/call",
  "params": {
    "name": "get_high_scores",
    "arguments": { "game": "rainbow-reboot", "limit": 3 },
    "_meta": {
      "io.modelcontextprotocol/protocolVersion": "2026-07-28",
      "io.modelcontextprotocol/clientCapabilities": {}
    }
  }
}

And the server answers with content blocks:

{
  "jsonrpc": "2.0",
  "id": 7,
  "result": {
    "resultType": "complete",
    "content": [
      { "type": "text", "text": "1. Sorskoot 9120\n2. Pixel 8740\n3. Neo 8015" }
    ],
    "isError": false
  }
}

If the tool fails, the server still returns a result, with isError: true. The client gives that error to the model, so the model can try again or explain what went wrong.

Because the model only sees the name, description, and schema, those are basically your user interface. A vague description means the model calls the wrong tool, or no tool at all.

What is in the context

It helps to picture what the model actually receives on every turn:

  • The system prompt of the host
  • The definitions of all available tools
  • The conversation so far
  • All earlier tool calls and their results

Here is a shortened example of the high score question from above. The exact format differs per host and model, but the parts are the same:

┌─ SYSTEM PROMPT ─────────────────────────────────────────────┐
You are an AI assistant running in <host app>.
- Rules for behavior, tone, safety...
- Environment: OS, working directory, today's date...
- Project instructions...

# MCP server instructions
## games
Use get_high_scores for anything about scores or rankings.
└─────────────────────────────────────────────────────────────┘

┌─ TOOLS (built-in + one entry per MCP tool) ─────────────────┐
[
  { "name": "read_file", "description": "Reads a file...", "input_schema": {...} },
  { "name": "run_command", "description": "Runs a command...", "input_schema": {...} },

  // these come from the tools/list response of the MCP server
  {
    "name": "mcp__games__get_high_scores",
    "description": "Returns the top scores for one of my games...",
    "input_schema": {
      "type": "object",
      "properties": {
        "game":  { "type": "string" },
        "limit": { "type": "integer", "minimum": 1, "maximum": 50 }
      },
      "required": ["game"]
    }
  }
  // ...often dozens more
]
└─────────────────────────────────────────────────────────────┘

┌─ MESSAGES ──────────────────────────────────────────────────┐
user:
  What are the top 3 scores in Rainbow Reboot?

assistant:
  tool_use  mcp__games__get_high_scores
            { "game": "rainbow-reboot", "limit": 3 }

user:
  tool_result
  "1. Sorskoot 9120\n2. Pixel 8740\n3. Neo 8015"

assistant:
  The top 3 scores in Rainbow Reboot are...
└─────────────────────────────────────────────────────────────┘

A few things stand out when you look at it like this.

The model never talks to the server

The host calls tools/list and turns each MCP tool into an entry in the tool list for the model. When the model returns a tool call, the host sends the tools/call request and puts the answer back into the conversation as a tool result. In this example, the tool result is sent back as a user message. For the model, it’s just more input.

It works the other way around too. The MCP server knows nothing about the conversation. It never sees the user’s question, the system prompt, or the other tools. It only gets the tool name and the arguments the model came up with. So if your tool needs some information, it has to be an argument in the schema.

Tool names get a prefix

Many hosts add the server name to the tool name, like mcp__games__get_high_scores. That way two servers can both have a tool called search without a clash.

Server instructions end up in the system prompt

A server can give the host some instructions on how to use its tools. Hosts usually add those to the system prompt, next to their own instructions.

Tool definitions cost tokens on every turn

Every name, description, and schema is sent on every turn, even when the tool isn’t used. Connect five servers with twenty tools each, and a big part of the context window is gone before the user types anything.

Some hosts solve this with deferred tools. Only the tool names are in the context, and the full definitions are loaded with a tool search when they’re needed.

Tool results stay in the context

A tool result isn’t gone after the model reads it. It stays in the conversation for every next turn. A big JSON response keeps eating tokens until the conversation ends or gets summarized.

For anyone building an MCP server, the lesson is simple: fewer tools with clear descriptions work better than many small ones. And keep tool results short.

Security

Tool results go straight into the model’s context. So any text a tool returns can try to give the model instructions. This is prompt injection, and an MCP server that reads emails, web pages, or tickets is a perfect way in.

The spec says there should always be a human who can deny a tool call. That’s why hosts ask for permission before running a tool. Clients must also treat tool annotations, like “read-only”, as untrusted unless they trust the server.

When I build an MCP server, this means:

  • Give each tool the smallest set of permissions that works.
  • Check authorization in the server for every call, and don’t count on the model to behave.
  • Treat everything that comes back from outside as data, not as instructions.

I wrote more about this in my post on Trustworthy AI .

What I took away

Building the server side was the easy part. Knowing what the client does with my tools made me write better descriptions and smaller results. Knowing the OAuth 2.1 requirements early saved me from rework later.

If you implement MCP yourself, read the authorization part of the spec before you start. And check which spec version your SDK and clients support, because the stateless change from July is quite big.

Happy Coding! 🚀

Robot reading about MCP under a blanket


Questions or thoughts? Reach out on LinkedIn or email me.

Support me