MCP's message format is based on the JSON-RPC 2.0 protocol, with extensions to support interactions between AI models and external systems. Here's a detailed explanation of the message format:
Basic Message Structure
All MCP messages follow the basic JSON-RPC 2.0 format:
json{ "jsonrpc": "2.0", "id": "unique-request-id", "method": "method-name", "params": { ... } }
1. Request Message
Used for clients to send tool invocation requests to the server:
json{ "jsonrpc": "2.0", "id": "req-123", "method": "tools/call", "params": { "name": "calculate", "arguments": { "expression": "2 + 2" } } }
2. Response Message
Server returns execution results:
json{ "jsonrpc": "2.0", "id": "req-123", "result": { "content": [ { "type": "text", "text": "Result: 4" } ] } }
3. Error Response
Returned when a request fails:
json{ "jsonrpc": "2.0", "id": "req-123", "error": { "code": -32602, "message": "Invalid params", "data": { "details": "Parameter 'expression' cannot be empty" } } }
4. Notification Message
Server-initiated messages (no response required):
json{ "jsonrpc": "2.0", "method": "notifications/progress", "params": { "progress": 0.5, "message": "Processing..." } }
Common Method Types
tools/list - Get list of available tools
json{ "jsonrpc": "2.0", "id": "req-001", "method": "tools/list" }
resources/list - Get list of available resources
json{ "jsonrpc": "2.0", "id": "req-002", "method": "resources/list" }
resources/read - Read resource content
json{ "jsonrpc": "2.0", "id": "req-003", "method": "resources/read", "params": { "uri": "file:///data/config.json" } }
prompts/list - Get list of prompts
json{ "jsonrpc": "2.0", "id": "req-004", "method": "prompts/list" }
Error Codes
MCP defines standard error codes:
| Code | Name | Description |
|---|---|---|
| -32700 | Parse error | JSON parse error |
| -32600 | Invalid Request | Invalid request |
| -32601 | Method not found | Method does not exist |
| -32602 | Invalid params | Invalid parameters |
| -32603 | Internal error | Internal error |
| -32000 | Server error | Server error |
Content Types
MCP supports multiple content types:
json{ "type": "text", "text": "Plain text content" }
json{ "type": "image", "data": "base64-encoded-image-data", "mimeType": "image/png" }
json{ "type": "resource", "uri": "file:///data/report.pdf", "mimeType": "application/pdf" }
Message Streaming
For long-running operations, streaming responses are supported:
json{ "jsonrpc": "2.0", "id": "req-005", "method": "tools/call", "params": { "name": "generate_report", "arguments": { "stream": true } } }
Best Practices:
- Unique ID: Each request must have a unique ID
- Type Validation: Strictly validate parameter types and formats
- Error Handling: Provide detailed error information and data
- Timeout Handling: Implement request timeout mechanisms
- Logging: Log all messages for debugging and auditing
Understanding MCP's message format is crucial for implementing compatible servers and clients.