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
Copy file name to clipboardExpand all lines: docs/concepts/architecture.mdx
+92-96Lines changed: 92 additions & 96 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,117 +3,110 @@ title: "Core architecture"
3
3
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]
24
+
end
25
+
subgraph "Server Process"
26
+
server2[MCP Server]
23
27
end
24
28
25
-
client <-->|Transport Layer| server
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.
The transport layer handles the actual communication between clients and servers. MCP supports multiple transport mechanisms:
53
64
54
65
1.**Stdio transport**
55
66
- Uses standard input/output for communication
56
67
- 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**
68
+
69
+
2.**SSE transport**
75
70
- Uses Server-Sent Events for server-to-client messages
76
71
- HTTP POST for client-to-server messages
77
-
```typescript
78
-
classSSETransportimplementsTransport {
79
-
constructor(url:URL)
80
-
send(message:JSONRPCMessage):Promise<void>
81
-
}
82
-
```
72
+
73
+
All transports use [JSON-RPC](https://www.jsonrpc.org/) 2.0 to exchange messages. See the [specification](https://spec.modelcontextprotocol.io) for more information.
83
74
84
75
### Message types
85
76
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
-
```
77
+
MCP has these main types of messages:
78
+
79
+
1.**Requests** expect a response from the other side:
80
+
```typescript
81
+
interfaceRequest {
82
+
method:string;
83
+
params?: { ... };
84
+
}
85
+
```
86
+
87
+
2.**Notifications** are one-way messages that don't expect a response:
88
+
```typescript
89
+
interfaceNotification {
90
+
method:string;
91
+
params?: { ... };
92
+
}
93
+
```
94
+
95
+
3.**Results** are successful responses to requests:
96
+
```typescript
97
+
interfaceResult {
98
+
[key:string]:unknown;
99
+
}
100
+
```
101
+
102
+
4.**Errors** indicate that a request failed:
103
+
```typescript
104
+
interfaceError {
105
+
code:number;
106
+
message:string;
107
+
data?:unknown;
108
+
}
109
+
```
117
110
118
111
## Connection lifecycle
119
112
@@ -131,19 +124,17 @@ sequenceDiagram
131
124
Note over Client,Server: Connection ready for use
132
125
```
133
126
134
-
1. Client sends `initialize` request with capabilities
135
-
2. Server responds with supported features
136
-
3. Client sends `initialized` notification
127
+
1. Client sends `initialize` request with protocol version and capabilities
128
+
2. Server responds with its protocol version and capabilities
129
+
3. Client sends `initialized` notification as acknowledgment
137
130
4. Normal message exchange begins
138
131
139
132
### 2. Message exchange
140
133
141
134
After initialization, the following patterns are supported:
142
135
143
-
-**Request-Response**: Client sends request, server responds
136
+
-**Request-Response**: Client or server sends requests, the other responds
144
137
-**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
138
148
139
### 3. Termination
149
140
@@ -154,19 +145,21 @@ Either party can terminate the connection:
154
145
155
146
## Error handling
156
147
157
-
MCP defines standard error codes and handling:
148
+
MCP defines these standard error codes:
158
149
159
150
```typescript
160
151
enumErrorCode {
161
-
ConnectionClosed=-1,
162
-
ParseError=-32700,
163
-
InvalidRequest=-32600,
164
-
MethodNotFound=-32601,
165
-
InvalidParams=-32602,
166
-
InternalError=-32603
152
+
// Standard JSON-RPC error codes
153
+
ParseError=-32700,
154
+
InvalidRequest=-32600,
155
+
MethodNotFound=-32601,
156
+
InvalidParams=-32602,
157
+
InternalError=-32603
167
158
}
168
159
```
169
160
161
+
SDKs and applications can define their own error codes above -32000.
162
+
170
163
Errors are propagated through:
171
164
- Error responses to requests
172
165
- Error events on transports
@@ -177,24 +170,28 @@ Errors are propagated through:
177
170
Here's a basic example of implementing an MCP server:
178
171
179
172
```typescript
180
-
import { Server } from"@modelcontextprotocol/sdk/server";
0 commit comments