Build and Deploy MCP Integrations
Build a Model Context Protocol client and server, then verify the integration with a compatible host.
Requests, notifications, errors and a dispatcher built from scratch.
Which part of an RPC message makes a server owe the caller a reply?
An ID identifies a request that expects an outcome. A notification omits that ID, and a result or error echoes the ID of an ordinary request.
Client supplies a method and a request ID
Dispatcher validates the message and attempts the method
Reply carries the original ID and one outcome
Client supplies a method and a request ID
Dispatcher validates the message and attempts the method
Reply carries the original ID and one outcome
Given Client supplies a method and a request ID and Reply carries the original ID and one outcome, write down the missing step that connects them.
Dispatcher validates the message and attempts the method
Now take Dispatcher validates the message and attempts the method and the result above. Write an input and an implementation that could produce this trace.
Client supplies a method and a request ID. Check that your implementation produces the result shown above.
One valid message is a notification and another has ID 8 with an unknown method. What replies should a server send?
The notification has no reply. The method lookup on request 8 produces an error response with ID 8, so the client can resolve the correct pending request.
Two requests are still pending and response 32 arrives before 31. How should the client decide which request to complete?
Match the response ID to the pending request ID. Arrival order does not identify a request when work can finish out of order.
Check this when debugging. Replying to notifications.
Every MCP message is JSON-RPC 2.0, so this is the floor everything else in the phase stands on. The spec fits on one page, but the details decide whether a server works with every client or only with yours. Notifications get no answer, ids must match, and a parse error has no id. Bugs here show up as hangs and mysterious timeouts in someone else's app, which makes them painful to debug.
Nothing: a message with no id is a notification
A message without an id is a notification: the sender doesn't expect an answer, and replying can confuse a client. Getting rules like this exactly right is what makes a server work with every client.
This rests on Model Context Protocol, where a tool call crossed the wire as JSON-RPC. Here is the whole format, and the rules that make a server work with every client.
You met JSON-RPC briefly in Phase 1. Here's the full picture, and a busy restaurant is a good way to see it.
A waiter writes an order on a numbered ticket. The kitchen sends back a dish with that number, or a note saying "we're out of that", also with the number on it. Now and then someone calls out "table six is leaving", and nobody answers, because nobody asked anything.
It asked nothing, so no reply is owed
Some messages only tell. With no numbered ticket, there is nothing to send an answer back to. JSON-RPC calls these notifications.
JSON-RPC works the same way. Every message is a JSON object with "jsonrpc": "2.0", and there are exactly four shapes:
| message | has id? | has method? | has result or error? | gets a reply? |
|---|---|---|---|---|
| request | yes | yes | no | yes, exactly one |
| notification | no | yes | no | never |
| result response | yes (same as the request) | no | result | no |
| error response | yes (or null if unknown) | no | error | no |
{"jsonrpc": "2.0", "id": 7, "method": "tools/call",
"params": {"name": "get_campaign", "arguments": {"id": "cmp_103"}}}
{"jsonrpc": "2.0", "id": 7,
"result": {"resultType": "complete", "content": [{"type": "text", "text": "..."}]}}
{"jsonrpc": "2.0", "id": 8,
"error": {"code": -32601, "message": "Method not found: tools/delete"}}
{"jsonrpc": "2.0", "method": "notifications/progress",
"params": {"progressToken": "t1", "progress": 40, "total": 100}}
The receiver tells them apart by which keys are present. A message with method and id is a request. With method and no id, it's a notification, and the receiver must not reply, not even with an error. Answering a notification is a classic bug: the other side gets a response for an id it never sent.
The id is how answers find their questions. Like tickets in a kitchen, a client can have many requests in flight at once, over one connection, and replies can come back in any order. So the id is the only link.
In plain words: an id means "someone is waiting for an answer to this". No id means nobody is, so stay quiet.
inputSchema becomes input_schema. What does this print?mcp_tool = {"name": "get_campaign", "description": "Look up one campaign.",
"inputSchema": {"type": "object"}}
claude_tool = {"name": mcp_tool["name"], "description": mcp_tool["description"],
"input_schema": mcp_tool["inputSchema"]}
print(sorted(claude_tool), claude_tool["name"])['description', 'input_schema', 'name'] get_campaign
Three fields carry over, and only one key changes its spelling. That tool list reached the host inside JSON-RPC messages, and so does every call the model makes afterward. This lesson is about those messages: their four shapes, and what a server may and may not send back.
Two questions identify any message: does it have an id, and does it have a method?
{"jsonrpc": "2.0", "method": "notifications/cancelled", "params": {"requestId": 4}}
No id means notification, and notifications never get a reply. The unknown method still gets an error reply, because it's a request. An id of 0 is a perfectly good id.
Error codes are like the stamps on returned mail: "address unreadable", "no such person", "wrong postage". JSON-RPC reserves a few codes for problems with the message itself:
| code | name | when |
|---|---|---|
| -32700 | Parse error | the text isn't valid JSON at all |
| -32600 | Invalid Request | valid JSON, but not a proper request (no method, wrong version) |
| -32601 | Method not found | no such method |
| -32602 | Invalid params | the method exists, but the params are wrong |
| -32603 | Internal error | the server failed while handling it |
JSON-RPC leaves -32000 to -32099 to implementations, and MCP splits it. It takes -32020 to -32099 for itself, with names such as UnsupportedProtocolVersion (-32022), which Versions and stateless requests uses. -32000 to -32019 is a legacy range: older SDKs and revisions used it, such as -32002 for a missing resource, and new servers shouldn't add codes there. A server's own error codes go outside -32768 to -32000 altogether.
Two subtle rules. First, for a parse error the server can't read the id, so the error response has "id": null. If a letter arrives soaked and unreadable, you can't tell who to answer. Second, an error has a message for people and an optional data field for machines, like the list of supported versions.
Each failure has its own code, and the first checks happen before your tool code runs. Follow one message at a time through the checks.
import json METHODS = {"tools/list": {"params": []}, "tools/call": {"params": ["name"]}} def check(raw): try: msg = json.loads(raw) except ValueError: return -32700, "parse error" if not isinstance(msg, dict) or msg.get("jsonrpc") != "2.0" or "method" not in msg: return -32600, "invalid request" if msg["method"] not in METHODS: return -32601, "method not found" for needed in METHODS[msg["method"]]["params"]: if needed not in msg.get("params", {}): return -32602, "invalid params" return 0, "ok" for raw in ['{"jsonrpc": ', '{"id": 1, "method": "tools/list"}', '{"jsonrpc": "2.0", "id": 2, "method": "tools/delete"}', '{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {}}', '{"jsonrpc": "2.0", "id": 4, "method": "tools/list"}']: print(check(raw))
The order runs from can't read it, to not a valid message, to no such method, to missing arguments. A precise code tells a client developer exactly where to look. An unhelpful -32603 on everything tells them nothing.
MCP adds a few rules on top of plain JSON-RPC:
null, and never reused while that request is still waiting. Watch out for true: Python's isinstance(True, int) is True, but a boolean id is invalid.params is always an object with named fields. JSON-RPC also allows a list, but MCP never uses one.{"jsonrpc": "2.0", "id": 3, "method": (cut off mid-message). What should it send back?An error with code -32700 and id null
It isn't valid JSON, so it's a parse error. The server can't trust anything inside, including the id, so the id is null.
Write a sentence or two, then compare it with a good answer in the lesson.
This part builds the dispatcher from the six checks, one message in and at most one out.
A dispatcher turns one incoming message into at most one outgoing message. Think of a mailroom clerk. Every envelope goes through the same steps in the same order, and whatever goes wrong, the clerk sends back a proper note instead of walking out. A good dispatcher has a fixed order of checks, and every failure becomes a proper error response:
null.jsonrpc is "2.0", method is a string, and the id (if present) is a string or an integer. Failure: -32600.params is an object (or missing). Failure: -32602.That last step is the framework idea from Phase 8 again: expected errors pass through, and surprises are caught and hidden.
METHODS = {"server/discover": lambda params: {"supportedVersions": ["2026-07-28"]}}
def dispatch(msg):
if "id" not in msg:
return None
if msg.get("method") not in METHODS:
return {"jsonrpc": "2.0", "id": msg["id"],
"error": {"code": -32601, "message": "Method not found"}}
return {"jsonrpc": "2.0", "id": msg["id"],
"result": METHODS[msg["method"]](msg.get("params"))}
note = {"jsonrpc": "2.0", "method": "notifications/progress"}
nope = {"jsonrpc": "2.0", "id": 4, "method": "tools/nope"}
discover = {"jsonrpc": "2.0", "id": 5, "method": "server/discover"}
print(dispatch(note), dispatch(nope)["error"]["code"],
dispatch(discover)["result"]["supportedVersions"])None -32601 ['2026-07-28']
The first message has no id, so it is a notification and gets no reply at all, not even an error. The second names a method the server doesn't have, which is error −32601. The third is server/discover, which every server must answer, and its result lists the versions the server supports. Every reply carries the id of the request it answers.
A tiny dispatcher with two methods. Notice the notification produces no output at all.
import json def handle(raw, methods): try: msg = json.loads(raw) except json.JSONDecodeError: return {"jsonrpc": "2.0", "id": None, "error": {"code": -32700, "message": "Parse error"}} if "id" not in msg: if msg.get("method") in methods: methods[msg["method"]](msg.get("params", {})) return None # notifications get no reply fn = methods.get(msg.get("method")) if fn is None: error = {"code": -32601, "message": f"Method not found: {msg.get('method')}"} return {"jsonrpc": "2.0", "id": msg["id"], "error": error} return {"jsonrpc": "2.0", "id": msg["id"], "result": fn(msg.get("params", {}))} seen = [] methods = {"echo": lambda p: {"you_said": p.get("text")}, "notifications/log": lambda p: seen.append(p["line"])} for raw in ['{"jsonrpc": "2.0", "id": 1, "method": "echo", "params": {"text": "hi"}}', '{"jsonrpc": "2.0", "method": "notifications/log",' ' "params": {"line": "started"}}', '{"jsonrpc": "2.0", "id": 2, "method": "missing"}', '{"jsonrpc": "2.0", "id": 3, "method": ']: print(handle(raw, methods)) print("logged:", seen)
Four messages in, three replies out: the notification ran (it logged "started") but sent nothing back. This version skips several checks from the list above. The exercise adds them all, which is what makes a dispatcher safe to expose.
Write kind(msg) for a parsed JSON-RPC message (a dict): "request" if it has a method and an id; "notification" if it has a method and no id; "response" if it has an id and exactly one of result or error; otherwise "invalid". Anything that isn't a dict, or lacks "jsonrpc": "2.0", is invalid.
def kind(msg): return "request" if "method" in msg else "response" print(kind({"jsonrpc": "2.0", "method": "notifications/cancelled", "params": {"requestId": 4}}))
Write error_reply(msg_id, code, data=None): return a JSON-RPC error response with the standard message for the code, from MESSAGES, and a data field inside the error only when data is given. Raise ValueError for a code that isn't in the table.
MESSAGES = {
-32700: "Parse error",
-32600: "Invalid Request",
-32601: "Method not found",
-32602: "Invalid params",
-32603: "Internal error",
}
def error_reply(msg_id, code, data=None):
return {"id": msg_id, "error": code}
print(error_reply(7, -32601, {"method": "tools/run"}))
import json msg = json.loads( '{"jsonrpc": "2.0", "method": "notifications/progress", "params": {"progress": 5}}') print("id" in msg)
False
The message has no "id" key, so in gives False. No id means a notification, and notifications never get a reply, not even an error.
Write a sentence or two, then compare it with a good answer in the lesson.
Purpose. Know where your dispatcher lives in a real server, and how to read a real exchange when a call hangs.
In an SDK server you never write the dispatcher: the SDK parses, checks the shape, matches methods, wraps your handler's errors and numbers the replies. It does exactly the six checks in this part. What you gain from having written one is the ability to read a host's log when a call hangs.
In an engineer's week. A user says "the tool never answers". You open the host's log or the Inspector's history and find request 7. If reply 7 is missing, the server crashed or hung on it. If a reply with a different id appears, the server answered the wrong message, usually a notification. If the reply is an error, the code tells you which of the six checks failed.
Write a sentence or two, then compare it with a good answer in the lesson.
Start with a short baseline or choose guided, independent, or AI-assisted practice.
What makes a JSON-RPC message a notification?
575 lessons in thirteen phases. 176 are free without an account, and a free account opens the rest. See every lesson.
Build a Model Context Protocol client and server, then verify the integration with a compatible host.
Never trained an AI? Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account. Teach it to write