Module 7 · MCP
Lesson 7.1
Host, client, server, and JSON-RPC
MCP, the Model Context Protocol, is a standard way for an app to reach tools and data.
The problem
Why it matters
Without it
Each app writes its own code for each tool. Every new tool needs new glue code in every app.
With it
Tools follow one protocol. A host connects to any server that speaks it, with no custom glue code.
How it works
Three roles, one protocol
The host is the app the user runs. Its client talks to one server, and the server offers tools and data. A tool such as get_order is the function the model can finally ask for.
- Host app
- Client
- Server
- get_order tool
The roles in one chain, from left to right. A request is one JSON object
Each message is a JSON object with a version, an id, a method, and params. The method names the action, such as tools/call. The params hold its inputs, such as the tool name and the order id.
Field Value Meaning jsonrpc 2.0 the protocol version id 3 matches the reply method tools/call the action to run params name and arguments the inputs The fields of the request in the snippet. const callTool = { jsonrpc: "2.0", id: 3, method: "tools/call", params: { name: "get_order", arguments: { orderId: "A-1042" } },};console.log(JSON.stringify(callTool));Output
{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"get_order","arguments":{"orderId":"A-1042"}}}The id matches a reply to its request
A reply repeats the id of the request it answers. The client compares the two ids to find the match. Ids let many requests share one connection without mixing up the answers.
- Request, id 3tools/call
- Reply, id 3Shipped Monday.
Two messages on one connection, linked by their ids. const request = { id: 3, method: "tools/call" };const reply = { id: 3, result: { text: "Shipped Monday." } };console.log("Reply matches call:", reply.id === request.id);Output
Reply matches call: true
Initialize opens the session
The client sends the initialize request, with a protocol version such as 2024-11-05. Other requests follow only after the server answers. After that, the client can list the tools and call them.
- initialize
- Server reply
- tools/list
- tools/call
The order of the first messages in a session. const initialize = { jsonrpc: "2.0", id: 1, method: "initialize", params: { protocolVersion: "2024-11-05" },};console.log(JSON.stringify(initialize));Output
{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05"}}
Common mistakes
What goes wrong
- Mixing up the roles. The client belongs to the host, and each client talks to one server.
- Reusing an id while the first request is still open. Each open request needs its own id.
- Calling tools before initialize finishes. A server may refuse requests before its session is open.
Use it when and skip it when
Is it the right tool?
Use it when
- Several apps must use the same tools.
- You want to add a new tool server without writing new code in each app.
Skip it when
- One app calls one function that lives in its own code.
Words to know
Terms in this lesson
- Host
- The app the user runs, such as a chat window.
- Client
- The part of the host that talks to one server.
- Server
- A program that offers tools, resources, or prompts.
- JSON-RPC
- A message format with an id, a method name, and params.