Skip to content

Commit 61f6e34

Browse files
committed
Update architecture.mdx
1 parent 0854b8e commit 61f6e34

File tree

1 file changed

+92
-96
lines changed

1 file changed

+92
-96
lines changed

docs/concepts/architecture.mdx

Lines changed: 92 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -3,117 +3,110 @@ title: "Core architecture"
33
description: "Understand how MCP connects clients, servers, and LLMs"
44
---
55

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.
77

88
## Overview
99

1010
MCP follows a client-server architecture where:
1111

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
1314
- **Servers** provide context, tools, and prompts to clients
14-
- **Hosts** are processes that run MCP clients
1515

1616
```mermaid
1717
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]
2021
end
2122
subgraph "Server Process"
22-
server[MCP Server]
23+
server1[MCP Server]
24+
end
25+
subgraph "Server Process"
26+
server2[MCP Server]
2327
end
2428
25-
client <-->|Transport Layer| server
29+
client1 <-->|Transport Layer| server1
30+
client2 <-->|Transport Layer| server2
2631
```
2732

2833
## Core components
2934

3035
### Protocol layer
3136

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.
3338

3439
```typescript
35-
class Protocol<SendRequestT, SendNotificationT, SendResultT> {
40+
class Protocol<Request, Notification, Result> {
3641
// Handle incoming requests
37-
setRequestHandler<T>(schema: T, handler: (request: T) => Result): void
42+
setRequestHandler<T>(schema: T, handler: (request: T, extra: RequestHandlerExtra) => Promise<Result>): void
3843

3944
// Handle incoming notifications
40-
setNotificationHandler<T>(schema: T, handler: (notification: T) => void): void
45+
setNotificationHandler<T>(schema: T, handler: (notification: T) => Promise<void>): void
4146

4247
// Send requests and await responses
43-
request<T>(request: Request, schema: T): Promise<T>
48+
request<T>(request: Request, schema: T, options?: RequestOptions): Promise<T>
4449

4550
// Send one-way notifications
4651
notification(notification: Notification): Promise<void>
4752
}
4853
```
4954

55+
Key classes include:
56+
57+
* `Protocol`
58+
* `Client`
59+
* `Server`
60+
5061
### Transport layer
5162

5263
The transport layer handles the actual communication between clients and servers. MCP supports multiple transport mechanisms:
5364

5465
1. **Stdio transport**
5566
- Uses standard input/output for communication
5667
- Ideal for local processes
57-
```typescript
58-
class StdioTransport implements Transport {
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-
class WebSocketTransport implements Transport {
69-
constructor(url: URL)
70-
send(message: JSONRPCMessage): Promise<void>
71-
}
72-
```
73-
74-
3. **SSE transport**
68+
69+
2. **SSE transport**
7570
- Uses Server-Sent Events for server-to-client messages
7671
- HTTP POST for client-to-server messages
77-
```typescript
78-
class SSETransport implements Transport {
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.
8374

8475
### Message types
8576

86-
MCP uses JSON-RPC 2.0 as its message format with three main types:
87-
88-
1. **Requests**
89-
```typescript
90-
interface Request {
91-
method: string
92-
params?: {
93-
_meta?: {
94-
progressToken?: string | number
95-
}
96-
}
97-
}
98-
```
99-
100-
2. **Notifications**
101-
```typescript
102-
interface Notification {
103-
method: string
104-
params?: {
105-
_meta?: Record<string, unknown>
106-
}
107-
}
108-
```
109-
110-
3. **Results**
111-
```typescript
112-
interface Result {
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+
interface Request {
82+
method: string;
83+
params?: { ... };
84+
}
85+
```
86+
87+
2. **Notifications** are one-way messages that don't expect a response:
88+
```typescript
89+
interface Notification {
90+
method: string;
91+
params?: { ... };
92+
}
93+
```
94+
95+
3. **Results** are successful responses to requests:
96+
```typescript
97+
interface Result {
98+
[key: string]: unknown;
99+
}
100+
```
101+
102+
4. **Errors** indicate that a request failed:
103+
```typescript
104+
interface Error {
105+
code: number;
106+
message: string;
107+
data?: unknown;
108+
}
109+
```
117110

118111
## Connection lifecycle
119112

@@ -131,19 +124,17 @@ sequenceDiagram
131124
Note over Client,Server: Connection ready for use
132125
```
133126

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
137130
4. Normal message exchange begins
138131

139132
### 2. Message exchange
140133

141134
After initialization, the following patterns are supported:
142135

143-
- **Request-Response**: Client sends request, server responds
136+
- **Request-Response**: Client or server sends requests, the other responds
144137
- **Notifications**: Either party sends one-way messages
145-
- **Progress Updates**: Long-running operations can report progress
146-
- **Resource Updates**: Servers notify about changed resources
147138

148139
### 3. Termination
149140

@@ -154,19 +145,21 @@ Either party can terminate the connection:
154145

155146
## Error handling
156147

157-
MCP defines standard error codes and handling:
148+
MCP defines these standard error codes:
158149

159150
```typescript
160151
enum ErrorCode {
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
167158
}
168159
```
169160

161+
SDKs and applications can define their own error codes above -32000.
162+
170163
Errors are propagated through:
171164
- Error responses to requests
172165
- Error events on transports
@@ -177,24 +170,28 @@ Errors are propagated through:
177170
Here's a basic example of implementing an MCP server:
178171

179172
```typescript
180-
import { Server } from "@modelcontextprotocol/sdk/server";
181-
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio";
173+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
174+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
182175

183176
const server = new Server({
184-
name: "example-server",
185-
version: "1.0.0"
177+
name: "example-server",
178+
version: "1.0.0"
179+
}, {
180+
capabilities: {
181+
resources: {}
182+
}
186183
});
187184

188185
// Handle requests
189186
server.setRequestHandler(ListResourcesRequestSchema, async () => {
190-
return {
191-
resources: [
192-
{
193-
uri: "example://resource",
194-
name: "Example Resource"
195-
}
196-
]
197-
};
187+
return {
188+
resources: [
189+
{
190+
uri: "example://resource",
191+
name: "Example Resource"
192+
}
193+
]
194+
};
198195
});
199196

200197
// Connect transport
@@ -212,9 +209,8 @@ await server.connect(transport);
212209
- Simple process management
213210

214211
2. **Remote communication**
215-
- WebSocket for full-duplex communication
216-
- SSE for scenarios requiring HTTP compatibility
217-
- Consider security implications
212+
- Use SSE for scenarios requiring HTTP compatibility
213+
- Consider security implications including authentication and authorization
218214

219215
### Message handling
220216

@@ -277,4 +273,4 @@ await server.connect(transport);
277273
- Test different transports
278274
- Verify error handling
279275
- Check edge cases
280-
- Load test servers
276+
- Load test servers

0 commit comments

Comments
 (0)