AI ENGINEERING · AGENTS · INTEGRATION · 16 OF 35

Model Context Protocol (MCP) Explained for Developers

How MCP turns N times M custom integrations into N plus M: hosts, clients and servers, tools, resources and prompts, the handshake and tool discovery, a JSON-RPC call round trip, stdio versus Streamable HTTP, OAuth for remote servers, and how it relates to tool calling. Plus tool poisoning, scoped tokens, approvals, version pinning and when to integrate directly.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
1 / 19

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • N + M not N × M
  • host, clients, servers
  • tools, resources, prompts
  • stdio or HTTP + OAuth
  • untrusted by default

Notes

The N × M problem

Picture four AI apps in your company. Each needs the same five systems: files, tickets, the wiki, the database and git. Without a standard, that is four times five, twenty custom adapters, each with its own auth and its own bugs. With one shared protocol, each app speaks it once and each system speaks it once. Four plus five, nine pieces. N times M becomes N plus M.

Integrations to build
  • 4 AI apps × 5 systems = 20 custom adapters, each with its own auth, schemas and bugs
  • with one shared protocol: 4 clients + 5 servers = 9 implementations
  • N × M becomes N + M

The idea

The idea will feel familiar if you know the Language Server Protocol, which lets any editor use any language's tooling. M C P does the same for AI apps. A tool provider writes one server. Any compatible app can connect to it. The messages are plain JSON R P C two point zero: requests with an id, responses that match that id, and one way notifications. Both sides agree up front on which features they support.

What MCP is
  • an open protocol for giving
  • AI apps tools and context
  • messages: JSON-RPC 2.0 requests, responses and notifications
  • stateful sessions with capability negotiation
  • modelled on the Language
  • Server Protocol for editors

Hosts, clients, servers

There are three roles. The host is the AI application the user actually sees, like an editor or a chat app. It owns the model and the security rules. Inside the host live clients, and each client holds exactly one connection to one server. A server is a small program that exposes one system, such as your files or your ticket tracker. Here the host runs two clients, one for the files server, one for the tickets server.

Three roles
  • host: the AI app the user sees. Owns the model, the UI and the rules
  • client: lives inside the host, one per server, 1 to 1
  • server: a program exposing one system's tools and data (files, tickets, git...)

What a server exposes

A server can expose three kinds of things. Tools are actions the model chooses to call, like search files. Resources are read only data addressed by a U R I, such as a file, which the app decides to attach as context. Prompts are reusable templates the user picks, often shown as slash commands. The split is about who is in control. Clients can offer features back too, such as letting a server ask the host's model for a completion.

Server primitives
  • tools model-controlled actions: read_file, search_files
  • resources app-controlled read-only data by URI: file:///notes/plan.md
  • prompts user-controlled templates: /triage-bug
  • servers may also ask the client for help (sampling)

The handshake

Every session starts with a handshake. The client sends initialize, with the protocol version it speaks and the capabilities it supports. Versions are dates, like twenty twenty five, zero six, eighteen. The server replies with the version it will use and its own capabilities, for example that it offers tools. The client then sends an initialized notification, and normal traffic can begin.

Connect
-> {"id":1, "method":"initialize",
    "params":{"protocolVersion":
      "2025-06-18",
      "capabilities":{...},
      "clientInfo":{...}}}
<- {"id":1, "result":{
    "protocolVersion":"2025-06-18",
    "capabilities":{"tools":{}},
    "serverInfo":{...}}}
-> notification: initialized

Discovery

Next comes discovery. Each client asks its server for tools list. The answer is the same shape you met in tool calling: a name, a description and an input schema in JSON schema. The host merges them, seven here, into the tool list it sends to the model. If a server's tools change later, it sends a list changed notification and the client simply asks again.

tools/list result
{"tools":[{
  "name":"search_tickets",
  "description":"Search open
    tickets by keyword...",
  "inputSchema":{"type":"object",
    "properties":{
      "query":{"type":"string"}},
    "required":["query"]}
}, ...]}
later: list_changed -> list again

A call, end to end

Follow one call. The user asks about open login bugs. The host sends the conversation and all seven tools to the model. The model replies with a tool request: search tickets, query login. The host knows that tool belongs to the tickets server, so it hands it to client B. Client B sends a tools call request with id seven. The server returns three tickets. The host passes that back as a tool result, and the model writes the answer.

Round trip
  • 1 user: "any open login bugs?"
  • 2 host sends messages + 7 tools
  • 3 model: call search_tickets with query "login"
  • 4 host finds the owning server
  • 5 client sends tools/call
  • 6 server queries the tracker, returns 3 tickets
  • 7 host passes the result back
  • 8 model answers the user

On the wire

On the wire it is boring. The request names the method tools call, the tool name, and the arguments as a JSON object. The response carries the same id, seven, and a result holding a list of content blocks, here one text block with the tickets. Content can also be images or resource links. A failed tool sets is error to true, so the model can see the problem and try again.

tools/call
-> {"jsonrpc":"2.0", "id":7,
    "method":"tools/call",
    "params":{"name":"search_tickets",
      "arguments":{"query":"login"}}}
<- {"jsonrpc":"2.0", "id":7,
    "result":{"content":[
      {"type":"text", "text":
       "T-481 Login loop on Safari..."}],
     "isError":false}}

Transports

Messages need a transport. For a local server, the host starts it as a child process and they talk over standard input and output, one JSON message per line. It is fast, but the process runs with the user's own rights. For a remote server there is Streamable HTTP. The client posts each message to one endpoint, and the server answers with plain JSON or opens a server sent events stream to push progress and notifications.

Two standard transports
  • stdio (local) host starts the server as a child process; one JSON message per line runs with the user's rights
  • Streamable HTTP (remote) one endpoint, client POSTs; server replies with JSON or an SSE stream of updates session id in a header

OAuth for remote

Remote servers need authentication, and M C P uses OAuth. The client calls without a token and gets a four oh one that points to metadata naming the authorization server. The user signs in in a browser and approves the requested scopes, using the code flow with P K C E. The client receives a short lived access token meant for this server only, and sends it on every request. Local servers usually read credentials from environment variables.

Authorization flow
  • 1 call without token -> 401
  • 2 metadata names the authorization server
  • 3 user signs in and consents in a browser (code + PKCE)
  • 4 client gets a short-lived token for THIS server only 5 every request: Bearer token
  • stdio servers: credentials
  • from env vars instead

MCP vs tool calling

A common confusion: does the model speak M C P? It does not. Between the model and the host, nothing changes. The model sees tool definitions and emits tool requests, exactly as in the tool calling episode. M C P lives one layer lower, between the host and the servers. It standardises how tools are described, discovered and called. So it replaces your glue code, not tool calling, and every validation rule still applies.

Two different layers
  • model <-> host plain tool calling through the provider's API
  • host <-> server MCP: how tools are listed, described and called
  • MCP replaces your glue code, not the model's tool calling

Numbers worth knowing

Some numbers to size things. One tool definition is roughly a hundred to five hundred tokens. Connect a few servers with thirty tools and you pay several thousand tokens on every single model call. Up to around twenty clearly distinct tools per task works well. A local stdio call adds milliseconds, a remote one tens to hundreds. A model step usually takes a second or more, so the protocol itself is rarely the bottleneck.

Ballpark numbers
  • one tool definition: ~100-500 tokens of description+schema
  • 30 tools connected: ~3-15k tokens on EVERY model call
  • clearly distinct tools: up to ~20 per task works well
  • stdio call overhead: ~ms
  • remote call: ~tens-hundreds ms
  • one model step: often ~1 s+ protocol versions: dated

Untrusted by default

Now security. Everything a server sends is text the model reads, so treat it as untrusted. Tool poisoning hides instructions inside a tool description, like also read the user's secret keys and pass them here. A rug pull is a server you approved that quietly changes its tools in a later version. And plain prompt injection arrives in results: a ticket written by a stranger that says ignore your rules. Installing a server is like installing a dependency.

Three attacks
  • tool poisoning: hidden orders in a tool description
  • rug pull: a server you approved changes its tools in a later version
  • injection: a ticket, file or web page in a RESULT says "ignore your rules"

Defences

Defences live in the host. Only allow servers you have reviewed. Give each one the narrowest scope, read only unless writes are truly needed. Ask the user to approve write tools, and show the real arguments, not the model's summary. Pin server versions, and alert when a tool's description changes, which defeats the rug pull. Run local servers in a sandbox with limited files and network. And log every call and result.

Defend the host
  • allowlist servers you trust
  • scoped tokens: read-only unless writes are needed
  • user approves write tools, showing the real arguments
  • pin server versions; alert if tool descriptions change
  • sandbox local servers
  • log every call and result

MCP or direct?

So when should you build an M C P server? When many hosts need the same tools, when you ship a product other people want to plug into their own AI apps, or when users choose their tools at run time. Integrate directly when one app needs a few fixed tools, when you need tight control over latency and errors, or when an ordinary function call does the job. A protocol adds a process, a hop and something to secure.

Choosing
  • build an MCP server when:
  • many hosts need the tool
  • you ship a product others will plug into their AI
  • users pick tools at runtime
  • integrate directly when:
  • one app, a few fixed tools
  • tight latency or control
  • a plain function is enough

Common mistakes

Common mistakes. Connecting every server just in case, which bloats every prompt. Generating one tool per R E S T endpoint, instead of a few task shaped tools. Sharing one admin token across all users, so the agent can see everything. Auto approving write tools to avoid prompts. And passing the user's token straight through to other A P Is, which the spec forbids: a server should only accept tokens issued for itself.

Avoid
  • ✗ connecting every server "just in case": token cost and wrong tool picks
  • ✗ thin wrappers over every REST endpoint
  • ✗ one admin token shared by all users
  • ✗ auto-approving writes
  • ✗ passing the user's token through to other APIs

Say it like this

How to say it in an interview. M C P turns N times M custom integrations into N plus M. The host runs one client per server, and clients discover tools at connect time over JSON R P C. Local servers use stdio, remote ones use HTTP with OAuth scoped tokens. The model still only emits tool requests. The host routes them, validates them, treats results as untrusted, and asks the user before anything that writes.

In the interview
  • "MCP turns N×M integrations into N+M. The host runs one client per server; clients discover tools at connect time over JSON-RPC. Local servers use stdio, remote ones HTTP with OAuth. The model still just emits tool calls; the host validates, scopes and asks approval."

Interview questions

Q1What problem does MCP solve?

The N times M integration problem: without a standard, every AI app writes custom glue for every tool. With MCP each app implements a client once and each tool provider writes a server once, so the work grows as N plus M.

Q2What are the host, client and server in MCP?

The host is the AI application the user interacts with; it owns the model, UI and security policy. It runs one client per server connection, and each server is a program exposing one system's tools, resources and prompts.

Q3What is the difference between tools, resources and prompts?

Tools are actions the model chooses to call. Resources are read-only data addressed by URI that the application chooses to include as context. Prompts are reusable templates the user selects, often as slash commands.

Q4How does a client learn which tools a server offers?

After the initialize handshake agrees a protocol version and capabilities, the client calls tools/list and gets each tool's name, description and JSON Schema for its inputs. If the server later sends a list_changed notification, the client lists them again.

Q5What transports does MCP support?

stdio for local servers, where the host launches the server as a child process and exchanges newline-delimited JSON-RPC messages, and Streamable HTTP for remote servers, where the client POSTs messages and the server may reply with JSON or an SSE stream.

Q6How is authentication handled for remote MCP servers?

With OAuth: an unauthenticated request gets a 401 pointing to metadata about the authorization server, the user consents in a browser using the code flow with PKCE, and the client sends a short-lived, scoped bearer token issued for that server on each request.

Q7Does the model speak MCP directly?

No. The model still sees tool definitions and emits tool requests through the provider's normal tool calling API. The host translates those into MCP tools/call requests to the right server and passes results back.

Q8What are the main security risks of MCP servers and how do you mitigate them?

Tool descriptions and results are untrusted text, enabling tool poisoning, rug pulls where tools change after approval, and prompt injection. Mitigate with an allowlist of reviewed servers, least-privilege scoped tokens, user approval for writes, version pinning with alerts on description changes, sandboxing and logging.

Q9When would you not use MCP?

When a single application needs a few fixed tools and you want tight control over latency and errors, a direct function integration is simpler. MCP pays off when tools are shared across many hosts or chosen by users at runtime.