Build a request and a notification
Time to hand-write two MCP messages. One asks the server to run the add tool and expects a result back; the other is a one-way notification telling the server the client is ready. On the wire, the difference is a single field: a request carries an id so its reply can be matched to it, while a notification has no id at all.
No imports or setup are needed — you'll work with plain Python dictionaries.
This exercise is part of the course
Model Context Protocol: Advanced Topics
Exercise instructions
- Give
call_requestanidof1, so the server's reply can be matched back to it. - Set the
initializednotification'smethodto"notifications/initialized"(notice it has noid). - Complete
expects_reply()so it returnsTrueonly when a message carries an"id"key.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Build the request with an id
call_request = {
"jsonrpc": "2.0",
"id": ____,
"method": "tools/call",
"params": {"name": "add", "arguments": {"a": 5, "b": 3}},
}
# Build the one-way notification
initialized = {
"jsonrpc": "2.0",
"method": ____,
}
# Return True only if an id is present
def expects_reply(message):
return ____ in message
request_expects_reply = expects_reply(call_request)
notification_expects_reply = expects_reply(initialized)
print(request_expects_reply, notification_expects_reply)