Lab
Parse an MCP JSON-RPC Message
20 min
Beginner3 Free Attempts
Instructions
Objective
Write a Python function that parses MCP JSON-RPC messages and extracts key information.
Background
MCP uses JSON-RPC 2.0 for communication. Every message has a specific structure that includes version, method, parameters, and an optional ID for request-response correlation.
Requirements
Create a function parse_mcp_message(message: dict) -> dict that:
-
Validates the message format: Check for required fields (jsonrpc, method)
-
Extracts message type: Determine if it's a request, response, or notification
- Request: Has
idandmethod - Response: Has
idand eitherresultorerror - Notification: Has
methodbut noid
- Request: Has
-
Parses method details: Extract namespace and action from method (e.g., "tools/call" → namespace: "tools", action: "call")
-
Returns structured output:
{ "valid": bool, "type": "request" | "response" | "notification" | "invalid", "method": str | None, "namespace": str | None, "action": str | None, "id": int | str | None, "has_params": bool, "has_error": bool }
Example Input
message = {
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "search",
"arguments": {"query": "test"}
}
}
Example Output
{
"valid": True,
"type": "request",
"method": "tools/call",
"namespace": "tools",
"action": "call",
"id": 1,
"has_params": True,
"has_error": False
}
Hints
- Use
message.get("key")to safely access optional fields - The
methodfield uses "/" to separate namespace and action - A response message has no
methodfield
Grading Rubric
Correctly validates JSON-RPC 2.0 format25 points
Accurately determines message type (request/response/notification)25 points
Correctly parses method namespace and action25 points
Returns complete structured output with all fields25 points
Your Solution
Use any programming language
3 free attempts remaining