|
| 1 | +--- |
| 2 | +title: Logging |
| 3 | +type: docs |
| 4 | +weight: 5 |
| 5 | +description: "MCP protocol specification for server logging capabilities" |
| 6 | +--- |
| 7 | + |
| 8 | +Logging enables servers to send log messages to clients in a structured way. Clients can control the verbosity of logging by setting the minimum log level they want to receive. Each log message includes a level indicating its severity, an optional logger name for organization, and arbitrary data that can be any JSON-serializable value. |
| 9 | + |
| 10 | +## Capabilities |
| 11 | + |
| 12 | +To indicate support for the Logging API, servers MUST include a `logging` capability in their `ServerCapabilities` during initialization. The `logging` capability SHOULD be an empty object: |
| 13 | + |
| 14 | +```json |
| 15 | +{ |
| 16 | + "capabilities": { |
| 17 | + "logging": {} |
| 18 | + } |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +Clients SHOULD check for this capability before attempting to set the log level. |
| 23 | + |
| 24 | +## Concepts |
| 25 | + |
| 26 | +### Log Levels |
| 27 | + |
| 28 | +MCP defines the following log levels in order of increasing severity: |
| 29 | + |
| 30 | +- `debug`: Detailed information for debugging |
| 31 | +- `info`: General informational messages |
| 32 | +- `notice`: Normal but significant events |
| 33 | +- `warning`: Warning conditions |
| 34 | +- `error`: Error conditions |
| 35 | +- `critical`: Critical conditions |
| 36 | +- `alert`: Action must be taken immediately |
| 37 | +- `emergency`: System is unusable |
| 38 | + |
| 39 | +These levels correspond to syslog severity levels as defined in [RFC 5424](https://datatracker.ietf.org/doc/html/rfc5424#section-6.2.1). |
| 40 | + |
| 41 | +### Log Messages |
| 42 | + |
| 43 | +Each log message contains: |
| 44 | +- A severity level |
| 45 | +- Optional logger name for categorization |
| 46 | +- Arbitrary data payload (any JSON-serializable value) |
| 47 | + |
| 48 | +## Use Cases |
| 49 | + |
| 50 | +Common use cases for logging include: |
| 51 | + |
| 52 | +### Informational Updates |
| 53 | + |
| 54 | +```json |
| 55 | +{ |
| 56 | + "level": "info", |
| 57 | + "logger": "file_processor", |
| 58 | + "data": "Processing file: example.txt" |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +### Transient errors |
| 63 | + |
| 64 | +{ |
| 65 | + "level": "error", |
| 66 | + "logger": "git_clone", |
| 67 | + "data": { |
| 68 | + "error": "Remote repository temporarily unreachable", |
| 69 | + "details": { |
| 70 | + "repository": "github.com/example/repo", |
| 71 | + "attempt": 2, |
| 72 | + "maxAttempts": 3, |
| 73 | + "willRetry": true, |
| 74 | + "retryIn": "5s" |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +## Diagram |
| 80 | + |
| 81 | +The following diagram visualizes the logging interaction between client and server: |
| 82 | + |
| 83 | +```mermaid |
| 84 | +sequenceDiagram |
| 85 | + participant Client |
| 86 | + participant Server |
| 87 | +
|
| 88 | + Note over Client,Server: Configure logging |
| 89 | + Client->>Server: logging/setLevel (info) |
| 90 | + Server-->>Client: EmptyResult |
| 91 | +
|
| 92 | + Note over Client,Server: Server sends log messages |
| 93 | + Server--)Client: notifications/message (info) |
| 94 | + Server--)Client: notifications/message (error) |
| 95 | +``` |
| 96 | + |
| 97 | +## Messages |
| 98 | + |
| 99 | +This section defines the protocol messages for logging in the Model Context Protocol (MCP). |
| 100 | + |
| 101 | +### Setting Log Level |
| 102 | + |
| 103 | +#### Request |
| 104 | + |
| 105 | +To configure the minimum log level to receive, the client MUST send a `logging/setLevel` request. |
| 106 | + |
| 107 | +Method: `logging/setLevel` |
| 108 | +Params: |
| 109 | + - `level`: The minimum severity level of logs to receive (string, required) |
| 110 | + |
| 111 | +Example: |
| 112 | +```json |
| 113 | +{ |
| 114 | + "jsonrpc": "2.0", |
| 115 | + "id": 1, |
| 116 | + "method": "logging/setLevel", |
| 117 | + "params": { |
| 118 | + "level": "info" |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +#### Response |
| 124 | + |
| 125 | +The server MUST respond with an empty result if successful: |
| 126 | + |
| 127 | +```json |
| 128 | +{ |
| 129 | + "jsonrpc": "2.0", |
| 130 | + "id": 1, |
| 131 | + "result": {} |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +### Log Message Notification |
| 136 | + |
| 137 | +The server sends log messages to the client using the `notifications/message` notification. |
| 138 | + |
| 139 | +Method: `notifications/message` |
| 140 | +Params: |
| 141 | + - `level`: The severity level of the message (string, required) |
| 142 | + - `logger`: Optional name of the logger (string) |
| 143 | + - `data`: The log message data (any JSON-serializable value, required) |
| 144 | + |
| 145 | +Example: |
| 146 | +```json |
| 147 | +{ |
| 148 | + "jsonrpc": "2.0", |
| 149 | + "method": "notifications/message", |
| 150 | + "params": { |
| 151 | + "level": "error", |
| 152 | + "logger": "database", |
| 153 | + "data": { |
| 154 | + "error": "Connection failed", |
| 155 | + "details": { |
| 156 | + "host": "localhost", |
| 157 | + "port": 5432 |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +## Error Handling |
| 165 | + |
| 166 | +Clients MUST be prepared to handle: |
| 167 | +- Invalid log levels in setLevel requests |
| 168 | +- Missing or malformed log message parameters |
| 169 | +- Unexpected data types in log messages |
| 170 | + |
| 171 | +## Security Considerations |
| 172 | + |
| 173 | +Implementations MUST carefully consider: |
| 174 | +- Sanitizing sensitive information from log messages |
| 175 | +- Rate limiting log messages to prevent flooding |
| 176 | +- Access control for log configuration |
| 177 | +- Validation of log data to prevent injection attacks |
0 commit comments