You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: "Understand how MCP connects clients, servers, and LLMs"
4
4
---
5
5
6
-
The Model Context Protocol (MCP) is built on a flexible, extensible architecture that enables seamless communication between LLM applications and external services. This document covers the core architectural components and concepts.
6
+
The Model Context Protocol (MCP) is built on a flexible, extensible architecture that enables seamless communication between LLM applications and integrations. This document covers the core architectural components and concepts.
7
7
8
8
## Overview
9
9
10
10
MCP follows a client-server architecture where:
11
11
12
-
-**Clients** are LLM applications (like claude.ai or IDEs) that initiate connections
12
+
-**Hosts** are LLM applications (like Claude Desktop or IDEs) that initiate connections
13
+
-**Clients** maintain 1:1 connections with servers, inside the host application
13
14
-**Servers** provide context, tools, and prompts to clients
14
-
-**Hosts** are processes that run MCP clients
15
15
16
16
```mermaid
17
17
flowchart LR
18
-
subgraph "Host (e.g., claude.ai)"
19
-
client[MCP Client]
18
+
subgraph " Host (e.g., Claude Desktop) "
19
+
client1[MCP Client]
20
+
client2[MCP Client]
20
21
end
21
22
subgraph "Server Process"
22
-
server[MCP Server]
23
+
server1[MCP Server]
23
24
end
24
-
25
-
client <-->|Transport Layer| server
25
+
subgraph "Server Process"
26
+
server2[MCP Server]
27
+
end
28
+
29
+
client1 <-->|Transport Layer| server1
30
+
client2 <-->|Transport Layer| server2
26
31
```
27
32
28
33
## Core components
29
34
30
35
### Protocol layer
31
36
32
-
The protocol layer handles message framing, request/response linking, and high-level communication patterns. Key classes include:
37
+
The protocol layer handles message framing, request/response linking, and high-level communication patterns.
@@ -54,66 +74,48 @@ The transport layer handles the actual communication between clients and servers
54
74
1.**Stdio transport**
55
75
- Uses standard input/output for communication
56
76
- Ideal for local processes
57
-
```typescript
58
-
classStdioTransportimplementsTransport {
59
-
constructor(stdin:Readable, stdout:Writable)
60
-
send(message:JSONRPCMessage):Promise<void>
61
-
}
62
-
```
63
-
64
-
2.**WebSocket transport**
65
-
- Enables real-time bidirectional communication
66
-
- Supports remote connections
67
-
```typescript
68
-
classWebSocketTransportimplementsTransport {
69
-
constructor(url:URL)
70
-
send(message:JSONRPCMessage):Promise<void>
71
-
}
72
-
```
73
-
74
-
3.**SSE transport**
77
+
78
+
2.**HTTP with SSE transport**
75
79
- Uses Server-Sent Events for server-to-client messages
76
80
- HTTP POST for client-to-server messages
77
-
```typescript
78
-
classSSETransportimplementsTransport {
79
-
constructor(url:URL)
80
-
send(message:JSONRPCMessage):Promise<void>
81
-
}
82
-
```
81
+
82
+
All transports use [JSON-RPC](https://www.jsonrpc.org/) 2.0 to exchange messages. See the [specification](https://spec.modelcontextprotocol.io) for detailed information about the Model Context Protocol message format.
83
83
84
84
### Message types
85
85
86
-
MCP uses JSON-RPC 2.0 as its message format with three main types:
87
-
88
-
1.**Requests**
89
-
```typescript
90
-
interfaceRequest {
91
-
method:string
92
-
params?: {
93
-
_meta?: {
94
-
progressToken?:string|number
95
-
}
96
-
}
97
-
}
98
-
```
99
-
100
-
2.**Notifications**
101
-
```typescript
102
-
interfaceNotification {
103
-
method:string
104
-
params?: {
105
-
_meta?:Record<string, unknown>
106
-
}
107
-
}
108
-
```
109
-
110
-
3.**Results**
111
-
```typescript
112
-
interfaceResult {
113
-
_meta?:Record<string, unknown>
114
-
[key: string]:unknown
115
-
}
116
-
```
86
+
MCP has these main types of messages:
87
+
88
+
1.**Requests** expect a response from the other side:
89
+
```typescript
90
+
interfaceRequest {
91
+
method:string;
92
+
params?: { ... };
93
+
}
94
+
```
95
+
96
+
2.**Notifications** are one-way messages that don't expect a response:
97
+
```typescript
98
+
interfaceNotification {
99
+
method:string;
100
+
params?: { ... };
101
+
}
102
+
```
103
+
104
+
3.**Results** are successful responses to requests:
105
+
```typescript
106
+
interfaceResult {
107
+
[key:string]:unknown;
108
+
}
109
+
```
110
+
111
+
4.**Errors** indicate that a request failed:
112
+
```typescript
113
+
interfaceError {
114
+
code:number;
115
+
message:string;
116
+
data?:unknown;
117
+
}
118
+
```
117
119
118
120
## Connection lifecycle
119
121
@@ -123,27 +125,25 @@ MCP uses JSON-RPC 2.0 as its message format with three main types:
123
125
sequenceDiagram
124
126
participant Client
125
127
participant Server
126
-
128
+
127
129
Client->>Server: initialize request
128
130
Server->>Client: initialize response
129
131
Client->>Server: initialized notification
130
-
132
+
131
133
Note over Client,Server: Connection ready for use
132
134
```
133
135
134
-
1. Client sends `initialize` request with capabilities
135
-
2. Server responds with supported features
136
-
3. Client sends `initialized` notification
136
+
1. Client sends `initialize` request with protocol version and capabilities
137
+
2. Server responds with its protocol version and capabilities
138
+
3. Client sends `initialized` notification as acknowledgment
137
139
4. Normal message exchange begins
138
140
139
141
### 2. Message exchange
140
142
141
143
After initialization, the following patterns are supported:
142
144
143
-
-**Request-Response**: Client sends request, server responds
145
+
-**Request-Response**: Client or server sends requests, the other responds
144
146
-**Notifications**: Either party sends one-way messages
145
-
-**Progress Updates**: Long-running operations can report progress
146
-
-**Resource Updates**: Servers notify about changed resources
147
147
148
148
### 3. Termination
149
149
@@ -154,19 +154,21 @@ Either party can terminate the connection:
154
154
155
155
## Error handling
156
156
157
-
MCP defines standard error codes and handling:
157
+
MCP defines these standard error codes:
158
158
159
159
```typescript
160
160
enumErrorCode {
161
-
ConnectionClosed=-1,
162
-
ParseError=-32700,
163
-
InvalidRequest=-32600,
164
-
MethodNotFound=-32601,
165
-
InvalidParams=-32602,
166
-
InternalError=-32603
161
+
// Standard JSON-RPC error codes
162
+
ParseError=-32700,
163
+
InvalidRequest=-32600,
164
+
MethodNotFound=-32601,
165
+
InvalidParams=-32602,
166
+
InternalError=-32603
167
167
}
168
168
```
169
169
170
+
SDKs and applications can define their own error codes above -32000.
171
+
170
172
Errors are propagated through:
171
173
- Error responses to requests
172
174
- Error events on transports
@@ -176,31 +178,44 @@ Errors are propagated through:
176
178
177
179
Here's a basic example of implementing an MCP server:
178
180
179
-
```typescript
180
-
import { Server } from"@modelcontextprotocol/sdk/server";
0 commit comments