|
| 1 | +--- |
| 2 | +title: Lifecycle |
| 3 | +type: docs |
| 4 | +weight: 30 |
| 5 | +--- |
| 6 | + |
| 7 | +{{< callout type="info" >}} |
| 8 | +**Protocol Revision**: 2024-11-05 (Final) |
| 9 | +{{< /callout >}} |
| 10 | + |
| 11 | +The Model Context Protocol (MCP) defines a rigorous lifecycle for client-server connections that ensures proper capability negotiation and state management. This lifecycle consists of four distinct phases: |
| 12 | + |
| 13 | +1. **Initialization**: Capability negotiation and protocol version agreement |
| 14 | +2. **Ready**: Final confirmation before beginning operations |
| 15 | +3. **Operation**: Normal protocol communication |
| 16 | +4. **Shutdown**: Graceful termination of the connection |
| 17 | + |
| 18 | +```mermaid |
| 19 | +sequenceDiagram |
| 20 | + participant Client |
| 21 | + participant Server |
| 22 | +
|
| 23 | + Note over Client,Server: Initialization Phase |
| 24 | + Client->>Server: initialize request |
| 25 | + Server-->>Client: initialize response |
| 26 | + Client--)Server: initialized notification |
| 27 | +
|
| 28 | + Note over Client,Server: Operation Phase |
| 29 | + activate Client |
| 30 | + activate Server |
| 31 | + rect rgb(200, 220, 250) |
| 32 | + note over Client,Server: Normal protocol operations |
| 33 | + end |
| 34 | + deactivate Client |
| 35 | + deactivate Server |
| 36 | +
|
| 37 | + Note over Client,Server: Shutdown Phase |
| 38 | + Client->>Server: shutdown request |
| 39 | + Server-->>Client: shutdown response |
| 40 | + Note over Client,Server: Connection closed |
| 41 | +``` |
| 42 | + |
| 43 | +## Lifecycle Phases |
| 44 | + |
| 45 | +### 1. Initialization Phase |
| 46 | + |
| 47 | +The initialization phase MUST be the first interaction between client and server. During this phase, the client and server: |
| 48 | + |
| 49 | +- Establish protocol version compatibility |
| 50 | +- Exchange and negotiate capabilities |
| 51 | +- Share implementation details |
| 52 | + |
| 53 | +The client MUST initiate this phase by sending an `initialize` request containing: |
| 54 | + |
| 55 | +- Protocol version supported |
| 56 | +- Client capabilities |
| 57 | +- Client implementation information |
| 58 | + |
| 59 | +```json |
| 60 | +{ |
| 61 | + "jsonrpc": "2.0", |
| 62 | + "id": 1, |
| 63 | + "method": "initialize", |
| 64 | + "params": { |
| 65 | + "protocolVersion": "2024-11-05", |
| 66 | + "capabilities": { |
| 67 | + "roots": { |
| 68 | + "listChanged": true |
| 69 | + }, |
| 70 | + "sampling": {} |
| 71 | + }, |
| 72 | + "clientInfo": { |
| 73 | + "name": "ExampleClient", |
| 74 | + "version": "1.0.0" |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +The server MUST respond with its own capabilities and information: |
| 81 | + |
| 82 | +```json |
| 83 | +{ |
| 84 | + "jsonrpc": "2.0", |
| 85 | + "id": 1, |
| 86 | + "result": { |
| 87 | + "protocolVersion": "2024-11-05", |
| 88 | + "capabilities": { |
| 89 | + "prompts": { |
| 90 | + "listChanged": true |
| 91 | + }, |
| 92 | + "resources": { |
| 93 | + "subscribe": true, |
| 94 | + "listChanged": true |
| 95 | + }, |
| 96 | + "tools": { |
| 97 | + "listChanged": true |
| 98 | + } |
| 99 | + }, |
| 100 | + "serverInfo": { |
| 101 | + "name": "ExampleServer", |
| 102 | + "version": "1.0.0" |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +#### Capability Negotiation |
| 109 | + |
| 110 | +The initialization phase establishes which optional protocol features will be available during the session. Key capabilities include: |
| 111 | + |
| 112 | +| Category | Capability | Description | |
| 113 | +|---------------|---------------|-----------------------------------------------| |
| 114 | +| Client | `roots` | Ability to provide filesystem roots | |
| 115 | +| Client | `sampling` | Support for LLM sampling requests | |
| 116 | +| Client | `experimental`| Support for non-standard experimental features| |
| 117 | +| Server | `prompts` | Offers prompt templates | |
| 118 | +| Server | `resources` | Provides readable resources | |
| 119 | +| Server | `tools` | Exposes callable tools | |
| 120 | +| Server | `logging` | Support for structured log messages | |
| 121 | +| Server | `experimental`| Support for non-standard experimental features| |
| 122 | + |
| 123 | +Capabilities can include sub-capabilities like: |
| 124 | +- `listChanged`: Support for list change notifications (for prompts, resources, and tools) |
| 125 | +- `subscribe`: Support for subscriptions (resources only) |
| 126 | + |
| 127 | +### 2. Ready Phase |
| 128 | + |
| 129 | +After successful initialization, the client MUST send an `initialized` notification to indicate it is ready to begin normal operations: |
| 130 | + |
| 131 | +```json |
| 132 | +{ |
| 133 | + "jsonrpc": "2.0", |
| 134 | + "method": "initialized" |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +The server MUST NOT send any protocol messages besides the initialization response until it receives this notification. |
| 139 | + |
| 140 | +### 3. Operation Phase |
| 141 | + |
| 142 | +During the operation phase, the client and server exchange messages according to the negotiated capabilities. Both parties MUST: |
| 143 | + |
| 144 | +- Respect the negotiated protocol version |
| 145 | +- Only use capabilities that were successfully negotiated |
| 146 | +- Handle messages according to the JSON-RPC 2.0 specification |
| 147 | + |
| 148 | +### 4. Shutdown Phase |
| 149 | +The shutdown phase cleanly terminates the protocol connection. No specific shutdown messages are defined - instead, the underlying transport mechanism should be used to signal connection termination: |
| 150 | + |
| 151 | +- For STDIO-based transport, the MCP server process should be terminated |
| 152 | +- For HTTP-based transport, the HTTP connection should be closed |
| 153 | + |
| 154 | +## Error Handling |
| 155 | + |
| 156 | +Implementations MUST handle these error cases: |
| 157 | + |
| 158 | +- Protocol version mismatch |
| 159 | +- Required capability negotiation failure |
| 160 | +- Initialize request timeout |
| 161 | +- Shutdown request timeout |
| 162 | + |
| 163 | +Server implementations are SHOULD implement appropriate timeouts for all lifecycle phases to prevent hung connections and resource exhaustion. |
| 164 | + |
| 165 | +When errors occur during initialization, the server MUST respond with an appropriate error code and SHOULD close the connection. |
| 166 | + |
| 167 | +Example initialization error: |
| 168 | +```json |
| 169 | +{ |
| 170 | + "jsonrpc": "2.0", |
| 171 | + "id": 1, |
| 172 | + "error": { |
| 173 | + "code": -32600, |
| 174 | + "message": "Unsupported protocol version", |
| 175 | + "data": { |
| 176 | + "supported": ["2024-11-05"], |
| 177 | + "requested": "1.0.0" |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | +``` |
0 commit comments