--- title: 'Message types' description: 'Understanding the different types of messages in the Model Context Protocol' --- The Model Context Protocol (MCP) uses JSON-RPC 2.0 as its base protocol and defines several message types for communication between clients and servers. ## Core message types MCP uses three core types of messages for communication: Messages that expect a response, identified by a unique ID Messages sent in reply to requests, containing results or errors One-way messages that don't expect a response ## Message structure All messages in MCP follow the JSON-RPC 2.0 specification and include specific required fields. ```json { "jsonrpc": "2.0", "id": 1, "method": "resources/list", "params": { // Optional method parameters } } ``` Required fields: - `jsonrpc`: Must be "2.0" - `id`: A unique identifier for the request - `method`: The name of the method to invoke ```json { "jsonrpc": "2.0", "id": 1, "result": { // Method-specific result data } } ``` Required fields: - `jsonrpc`: Must be "2.0" - `id`: The same ID as the corresponding request - `result`: The result data (or `error` for error responses) ```json { "jsonrpc": "2.0", "method": "notifications/initialized", "params": { // Optional notification parameters } } ``` Required fields: - `jsonrpc`: Must be "2.0" - `method`: The name of the notification ## Error handling When a request cannot be fulfilled, the server responds with an error object instead of a result. ```json { "jsonrpc": "2.0", "id": 1, "error": { "code": -32601, "message": "Method not found", "data": { // Optional additional error information } } } ``` ### Standard error codes | Code | Message | Description | |------|---------|-------------| | -32700 | Parse error | Invalid JSON received | | -32600 | Invalid request | Request object invalid | | -32601 | Method not found | Requested method doesn't exist | | -32602 | Invalid params | Method parameters invalid | | -32603 | Internal error | Internal JSON-RPC error | ## Progress tracking MCP supports progress tracking for long-running operations through a combination of request parameters and notifications. Add a progress token in the request: ```json { "jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": { "_meta": { "progressToken": "abc123" } } } ``` The server sends progress updates as notifications: ```json { "jsonrpc": "2.0", "method": "notifications/progress", "params": { "progressToken": "abc123", "progress": 50, "total": 100 } } ``` ## Message content types MCP supports different content types for messages exchanged between the client and server. ```json Text content { "type": "text", "text": "This is a text message" } ``` ```json Image content { "type": "image", "data": "base64-encoded-image-data", "mimeType": "image/png" } ``` ## Special considerations When implementing MCP message handling, keep in mind: - All messages must be encoded in UTF-8 - Binary data must be base64 encoded - Message IDs should be unique within a session - Error messages should be concise and single-sentence - Progress notifications are optional and may not be supported by all servers ## Message flow examples Here are some common message flow patterns in MCP: 1. Client sends request: ```json { "jsonrpc": "2.0", "id": 1, "method": "resources/list" } ``` 2. Server responds with resources: ```json { "jsonrpc": "2.0", "id": 1, "result": { "resources": [ { "uri": "file:///example.txt", "name": "Example File", "mimeType": "text/plain" } ] } } ``` 1. Client sends tool request: ```json { "jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": { "name": "longRunningTool", "_meta": { "progressToken": "token123" } } } ``` 2. Server sends progress notifications: ```json { "jsonrpc": "2.0", "method": "notifications/progress", "params": { "progressToken": "token123", "progress": 50, "total": 100 } } ``` 3. Server sends final response: ```json { "jsonrpc": "2.0", "id": 2, "result": { "toolResult": "Operation completed successfully" } } ``` ## Best practices When working with MCP messages: Always include appropriate error codes and meaningful error messages Send regular progress updates for long-running operations Validate message content against the appropriate schemas Clean up resources when connections are closed