{"slug": "how-mcp-works-under-the-covers", "title": "How MCP Works Under The Covers", "summary": "The 2026-07-28 version of the Model Context Protocol (MCP) specification makes MCP a stateless protocol, removing the initialize handshake and the Mcp-Session-Id header so any server instance can handle any request, according to developer Timmy Kokke's technical breakdown. Kokke writes that each request now carries the protocol version and client capabilities in its _meta field, which lets remote MCP servers run behind a plain load balancer without sticky sessions or a shared session store, and that the GET stream is replaced by a subscriptions/listen request. Kokke also notes that MCP requires OAuth 2.1, which applies only to the Streamable HTTP transport, and that a stray console.log to stdout can break a stdio connection.", "body_md": "# [How MCP Works Under The Covers](https://timmykokke.com/blog/2026/2026-12-03-how-mcp-works-under-the-covers/)\n\nHello Coders! 👾\n\nAt 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.\n\nWhat 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.\n\nSo in this post I want to look under the covers. Everything here is based on the \n    [2026-07-28 version of the MCP specification](https://modelcontextprotocol.io/specification/2026-07-28)\n  . That version changed quite a lot, so older tutorials might show you things that are gone now.\n\n## The big picture\n\nMCP 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.\n\nThere are three roles:\n\n- **Host** : the application the user works in, like an IDE, a chat app, or a coding agent.\n- **Client** : a component inside the host. It keeps one connection to one server.\n- **Server** : the program that offers tools, resources, and prompts.\n\nA 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.\n\n``` php\n  flowchart LR\n    User((User)) --> Host\n    subgraph Host[Host application]\n        C1[MCP client A]\n        C2[MCP client B]\n    end\n    Host <-- \"conversation + tool definitions\" --> Model[LLM]\n    C1 <-- stdio --> S1[Local MCP server]\n    C2 <-- Streamable HTTP --> S2[Remote MCP server]\n```\n\n## Two ways to connect\n\nMCP has two standard transports.\n\nWith **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.\n\nWith **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.\n\nMost of the interesting stuff, like OAuth, only applies to HTTP.\n\n## A stateless protocol\n\nThis 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.\n\nOlder 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.\n\nInstead, every request now carries the protocol version and the client capabilities in its `_meta` field. So any server instance can handle any request.\n\n### Why this matters\n\nWith 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.\n\nNow you can put a few instances behind a load balancer and it just works:\n\n``` php\n  flowchart LR\n    C[MCP client] --> LB[Load balancer]\n    LB --> I1[Server instance 1]\n    LB --> I2[Server instance 2]\n    LB --> I3[Server instance 3]\n```\n\n### What else went away\n\nA few things depended on that session, so they changed too:\n\n- **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.\n- **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.\n- **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.\n\n### Discovery and headers\n\nEvery server must implement `server/discover`. It tells the client which protocol versions, capabilities, and identity the server has.\n\nOn 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:\n\n- `MCP-Protocol-Version` on every POST\n- `Mcp-Method` on every request, for example`tools/call`\n- `Mcp-Name` on`tools/call` ,`resources/read` , and`prompts/get`\n\nIf 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.\n\n## Getting in with OAuth 2.1\n\nI 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.\n\nThe tricky part is that the client has never seen your server before. So it has to discover everything by itself:\n\n```\n  sequenceDiagram\n    participant C as MCP client\n    participant S as MCP server\n    participant A as Authorization server\n    C->>S: POST /mcp (no token)\n    S-->>C: 401 + where to find the metadata\n    C->>S: GET protected resource metadata\n    S-->>C: Which authorization server to use\n    C->>A: Log in the user (PKCE, resource)\n    A-->>C: Access token\n    C->>S: POST /mcp + Authorization: Bearer token\n```\n\nThe MCP server only checks tokens. It doesn’t hand them out. For that it points the client to an authorization server, with \n    [Protected Resource Metadata (RFC 9728)](https://datatracker.ietf.org/doc/html/rfc9728)\n  .\n\nFor the MCP server I’m building, these were the important requirements:\n\n- Publish the protected resource metadata, and point to it in the `WWW-Authenticate` header of a 401.\n- 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.\n- Never forward the token to another API. If the server calls other APIs, it needs its own tokens.\n- Return a 403 with `insufficient_scope` when a token is missing a scope, so the client can ask for more.\n\nThe client side has more work, like getting a client ID and checking the issuer. The \n    [authorization part of the spec](https://modelcontextprotocol.io/specification/2026-07-28/basic/authorization)\n   has all the details.\n\n## How the client knows the tools\n\nOnce 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:\n\n```\n{\n  \"name\": \"get_high_scores\",\n  \"title\": \"Get high scores\",\n  \"description\": \"Returns the top scores for one of my games. Use this when the user asks about scores or rankings.\",\n  \"inputSchema\": {\n    \"type\": \"object\",\n    \"properties\": {\n      \"game\": { \"type\": \"string\", \"description\": \"Slug of the game, for example rainbow-reboot\" },\n      \"limit\": { \"type\": \"integer\", \"minimum\": 1, \"maximum\": 50 }\n    },\n    \"required\": [\"game\"]\n  }\n}\n```\n\nThe 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.\n\nWhen 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.\n\nTools 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.\n\n## How the model picks a tool\n\nThis 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.\n\nThe 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:\n\n```\n  sequenceDiagram\n    actor U as User\n    participant H as Host + MCP client\n    participant M as LLM\n    participant S as MCP server\n    U->>H: \"What are the top 3 scores in Rainbow Reboot?\"\n    H->>M: Conversation + tool definitions\n    M-->>H: Tool call: get_high_scores(game, limit)\n    H->>U: Ask permission (if needed)\n    H->>S: tools/call\n    S-->>H: Result content\n    H->>M: Conversation + tool result\n    M-->>H: Final answer\n    H->>U: \"The top 3 scores are...\"\n```\n\nThe `tools/call` request over HTTP looks like this:\n\n```\nPOST /mcp HTTP/1.1\nContent-Type: application/json\nAccept: application/json, text/event-stream\nAuthorization: Bearer eyJhbGciOi...\nMCP-Protocol-Version: 2026-07-28\nMcp-Method: tools/call\nMcp-Name: get_high_scores\n\n{\n  \"jsonrpc\": \"2.0\",\n  \"id\": 7,\n  \"method\": \"tools/call\",\n  \"params\": {\n    \"name\": \"get_high_scores\",\n    \"arguments\": { \"game\": \"rainbow-reboot\", \"limit\": 3 },\n    \"_meta\": {\n      \"io.modelcontextprotocol/protocolVersion\": \"2026-07-28\",\n      \"io.modelcontextprotocol/clientCapabilities\": {}\n    }\n  }\n}\n```\n\nAnd the server answers with content blocks:\n\n```\n{\n  \"jsonrpc\": \"2.0\",\n  \"id\": 7,\n  \"result\": {\n    \"resultType\": \"complete\",\n    \"content\": [\n      { \"type\": \"text\", \"text\": \"1. Sorskoot 9120\\n2. Pixel 8740\\n3. Neo 8015\" }\n    ],\n    \"isError\": false\n  }\n}\n```\n\nIf 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.\n\nBecause 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.\n\n## What is in the context\n\nIt helps to picture what the model actually receives on every turn:\n\n- The system prompt of the host\n- The definitions of all available tools\n- The conversation so far\n- All earlier tool calls and their results\n\nHere 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:\n\n```\n┌─ SYSTEM PROMPT ─────────────────────────────────────────────┐\nYou are an AI assistant running in <host app>.\n- Rules for behavior, tone, safety...\n- Environment: OS, working directory, today's date...\n- Project instructions...\n\n# MCP server instructions\n## games\nUse get_high_scores for anything about scores or rankings.\n└─────────────────────────────────────────────────────────────┘\n\n┌─ TOOLS (built-in + one entry per MCP tool) ─────────────────┐\n[\n  { \"name\": \"read_file\", \"description\": \"Reads a file...\", \"input_schema\": {...} },\n  { \"name\": \"run_command\", \"description\": \"Runs a command...\", \"input_schema\": {...} },\n\n  // these come from the tools/list response of the MCP server\n  {\n    \"name\": \"mcp__games__get_high_scores\",\n    \"description\": \"Returns the top scores for one of my games...\",\n    \"input_schema\": {\n      \"type\": \"object\",\n      \"properties\": {\n        \"game\":  { \"type\": \"string\" },\n        \"limit\": { \"type\": \"integer\", \"minimum\": 1, \"maximum\": 50 }\n      },\n      \"required\": [\"game\"]\n    }\n  }\n  // ...often dozens more\n]\n└─────────────────────────────────────────────────────────────┘\n\n┌─ MESSAGES ──────────────────────────────────────────────────┐\nuser:\n  What are the top 3 scores in Rainbow Reboot?\n\nassistant:\n  tool_use  mcp__games__get_high_scores\n            { \"game\": \"rainbow-reboot\", \"limit\": 3 }\n\nuser:\n  tool_result\n  \"1. Sorskoot 9120\\n2. Pixel 8740\\n3. Neo 8015\"\n\nassistant:\n  The top 3 scores in Rainbow Reboot are...\n└─────────────────────────────────────────────────────────────┘\n```\n\nA few things stand out when you look at it like this.\n\n### The model never talks to the server\n\nThe 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.\n\nIt 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.\n\n### Tool names get a prefix\n\nMany 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.\n\n### Server instructions end up in the system prompt\n\nA 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.\n\n### Tool definitions cost tokens on every turn\n\nEvery 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.\n\nSome 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.\n\n### Tool results stay in the context\n\nA 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.\n\nFor 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.\n\n## Security\n\nTool 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.\n\nThe 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.\n\nWhen I build an MCP server, this means:\n\n- Give each tool the smallest set of permissions that works.\n- Check authorization in the server for every call, and don’t count on the model to behave.\n- Treat everything that comes back from outside as data, not as instructions.\n\nI wrote more about this in my post on \n    [Trustworthy AI](https://timmykokke.com/blog/2026/2026-11-04-trustworthy-ai/)\n  .\n\n## What I took away\n\nBuilding 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.\n\nIf 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.\n\nHappy Coding! 🚀", "url": "https://wpnews.pro/news/how-mcp-works-under-the-covers", "canonical_source": "https://timmykokke.com/blog/2026/2026-12-03-how-mcp-works-under-the-covers/", "published_at": "2026-12-03 01:00:00+00:00", "updated_at": "2026-12-03 22:43:40.629566+00:00", "lang": "en", "topics": ["agent-protocols", "ai-agents", "ai-tools", "developer-tools"], "entities": ["Model Context Protocol", "Timmy Kokke", "OAuth 2.1"], "also_reported_by": [], "takeaway": "The 2026-07-28 MCP spec drops the initialize handshake and Mcp-Session-Id header, so servers must read protocol version and client capabilities from each request's _meta field and replace the GET stream with a subscriptions/listen request.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/how-mcp-works-under-the-covers", "markdown": "https://wpnews.pro/news/how-mcp-works-under-the-covers.md", "text": "https://wpnews.pro/news/how-mcp-works-under-the-covers.txt", "jsonld": "https://wpnews.pro/news/how-mcp-works-under-the-covers.jsonld"}}