Get startedGet started for free

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

View Course

Exercise instructions

  • Give call_request an id of 1, so the server's reply can be matched back to it.
  • Set the initialized notification's method to "notifications/initialized" (notice it has no id).
  • Complete expects_reply() so it returns True only 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)
Edit and Run Code